Skip to content

Commit 978e1aa

Browse files
committed
Introduce a database setting wizard.
Basic functionalities underwent minor changes.
1 parent bec1ae9 commit 978e1aa

5 files changed

Lines changed: 333 additions & 82 deletions

File tree

‎database_wizard.php‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
/**
4+
* \file
5+
* \brief Create / Edit database connection
6+
*
7+
* Call: database_wizard.php
8+
*
9+
* @package Lwt
10+
* @author HugoFara <hugo.farajallah@protonmail.com>
11+
* @license Unlicense <http://unlicense.org/>
12+
* @link https://hugofara.github.io/lwt/docs/html/database__wizard_8php.html
13+
* @since 2.5.0-fork
14+
*/
15+
16+
require_once 'inc/kernel_utility.php';
17+
18+
class Database_Connection {
19+
public string $server;
20+
public string $userid;
21+
public string $passwd;
22+
public string $dbname;
23+
24+
function __construct($server = "", $userid = "", $passwd = "", $dbname = "")
25+
{
26+
$this->server = $server;
27+
$this->userid = $userid;
28+
$this->passwd = $passwd;
29+
$this->dbname = $dbname;
30+
}
31+
32+
public function load_file(string $file_name) {
33+
include $file_name;
34+
$this->server = $server;
35+
$this->userid = $userid;
36+
$this->passwd = $passwd;
37+
$this->dbname = $dbname;
38+
}
39+
40+
public function get_as_text() {
41+
return '<?php
42+
43+
/**
44+
* \file
45+
* \brief DB variables for MAMP
46+
*/
47+
48+
$server = "' . $this->server . '";
49+
$userid = "' . $this->userid . '";
50+
$passwd = "' . $this->passwd . '";
51+
$dbname = "' . $this->dbname . '";
52+
53+
?>
54+
';
55+
}
56+
57+
}
58+
59+
function database_wizard_change($conn) {
60+
$handle = fopen(__DIR__ . "/connect.inc.php", 'w');
61+
fwrite($handle, $conn->get_as_text());
62+
fclose($handle);
63+
}
64+
65+
function database_wizard_do_operation($op) {
66+
$message = null;
67+
if ($op == "Autocomplete") {
68+
$server = $_SERVER['SERVER_ADDR'];
69+
$userid = $_SERVER['SERVER_NAME'];
70+
$passwd = "";
71+
$dbname = "";
72+
} else if ($op == "Check") {
73+
//require_once 'inc/database_connect.php';
74+
$server = getreq("server");
75+
$userid = getreq("userid");
76+
$passwd = getreq("passw");
77+
$dbname = getreq("dbname");
78+
$conn = mysqli_init();
79+
if ($conn === false) {
80+
$message = "MySQL is not accessible!";
81+
} else {
82+
try {
83+
$success = mysqli_real_connect($conn, $server, $userid, $passwd, $dbname);
84+
if (!$success) {
85+
$message = "Can't connect!";
86+
} else if (mysqli_errno($conn) != 0) {
87+
$message = "ERROR: " . mysqli_error($conn) . " error number: " . mysqli_errno($conn);
88+
} else {
89+
$message = "Connection established with success!";
90+
}
91+
} catch (Exception $exept) {
92+
$message = (string)$exept;
93+
}
94+
}
95+
//$message = "RRRRRRR";
96+
} else if ($op == "Change") {
97+
$server = getreq("server");
98+
$userid = getreq("userid");
99+
$passwd = getreq("passw");
100+
$dbname = getreq("dbname");
101+
}
102+
$conn = new Database_Connection($server, $userid, $passwd, $dbname);
103+
if ($op == "Change") {
104+
database_wizard_change($conn);
105+
}
106+
database_wizard_form($conn, $message);
107+
}
108+
109+
/**
110+
* Generate a form to edit the connection.
111+
*
112+
* @param \Database_Connection conn Database connection object
113+
*/
114+
function database_wizard_form($conn, $error_message=null) {
115+
pagestart_kernel_nobody("Database Connection Wizard", true);
116+
if ($error_message != null) {
117+
//error_message_with_hide($error_message, true);
118+
echo $error_message;
119+
}
120+
?>
121+
<form name="database_connect" action="<?= $_SERVER['PHP_SELF']; ?>" method="post">
122+
<p>
123+
<label for="server">Server address:</label>
124+
<input type="text" name="server" id="server" value="<?= htmlspecialchars($conn->server) ?>" required placeholder="localhost">
125+
</p>
126+
<p>
127+
<label for="userid">Database User Name:</label>
128+
<input type="text" name="userid" id="userid" value="<?= htmlspecialchars($conn->userid); ?>" required placeholder="root">
129+
</p>
130+
<p>
131+
<label for="passwd">Password:</label>
132+
<input type="password" name="passwd" id="passwd" value="<?= htmlspecialchars($conn->passwd); ?>" placeholder="abcxyz">
133+
</p>
134+
<p>
135+
<label for="dbname">Database Name:</label>
136+
<input type="text" name="dbname" id="dbname" value="<?= htmlspecialchars($conn->dbname); ?>" required placeholder="lwt">
137+
</p>
138+
<input type="submit" name="op" value="Autocomplete" />
139+
<input type="submit" name="op" value="Check" />
140+
<input type="submit" name="op" value="Change" />
141+
</form>
142+
<?php
143+
pageend();
144+
}
145+
146+
// May be dangerous to expose passwords in clear
147+
function edit_database_connection() {
148+
$conn = new Database_Connection();
149+
$conn->load_file('connect.inc.php');
150+
database_wizard_form($conn);
151+
}
152+
153+
function new_database_connection() {
154+
database_wizard_form(new Database_Connection());
155+
}
156+
157+
if (getreq('op') != '') {
158+
database_wizard_do_operation(getreq('op'));
159+
} else {
160+
if (file_exists('connect.inc.php')) {
161+
edit_database_connection();
162+
} else {
163+
new_database_connection();
164+
}
165+
}
166+
167+
?>

‎docs/CHANGELOG.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
Changelog
2-
=========
1+
# Changelog
32

43
This project's changelog. Versions marked with "-fork" come from the community, other versions come from the canonical LWT ("official" branch on Git).
54
For git tags, official releases are marked like "v1.0.0", while unofficial ones are marked like "v1.0.0-fork".
65

6+
## [Unreleased]
7+
8+
### Added
9+
10+
* Database creation wizard (``database_wizard.php``) to help setting the connection with the database.
11+
* In ``index.php``, ``get_server_data_table`` replaces ``get_server_data`` as a better designed function.
12+
* ``pagestart_kernel_nobody`` in ``inc/kernel.php`` that display the minimal HTML formatting.
13+
714
## 2.6.0-fork (January 01 2023)
815
### Added
916
* Frames resizing is back! The text reading and test interfaces updated in several ways. Based several suggestions as [#60](https://github.com/HugoFara/lwt/issues/60).

‎inc/kernel_utility.php‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,73 @@ function quickMenu(): void
357357
<?php
358358
}
359359

360+
/**
361+
* Start a page without connecting to the database with a complete header and a non-closed body.
362+
*
363+
* @param string $title Title of the page
364+
* @param string $addcss Some CSS to be embed in a style tag
365+
*
366+
* @global string $tbpref The database table prefix if true
367+
* @global int $debug Show the requests if true
368+
*/
369+
function pagestart_kernel_nobody($title, $addcss=''): void
370+
{
371+
global $tbpref, $debug;
372+
@header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
373+
@header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
374+
@header('Cache-Control: no-cache, must-revalidate, max-age=0');
375+
@header('Pragma: no-cache');
376+
?><!DOCTYPE html>
377+
<?php
378+
echo '<html lang="en">';
379+
?>
380+
<head>
381+
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
382+
<!--
383+
<?php echo file_get_contents("UNLICENSE.md");?>
384+
-->
385+
<meta name="viewport" content="width=900" />
386+
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
387+
<link rel="apple-touch-icon" href="img/apple-touch-icon-57x57.png" />
388+
<link rel="apple-touch-icon" sizes="72x72" href="img/apple-touch-icon-72x72.png" />
389+
<link rel="apple-touch-icon" sizes="114x114" href="img/apple-touch-icon-114x114.png" />
390+
<link rel="apple-touch-startup-image" href="img/apple-touch-startup.png" />
391+
<meta name="apple-mobile-web-app-capable" content="yes" />
392+
393+
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
394+
<link rel="stylesheet" type="text/css" href="css/jquery.tagit.css" />
395+
<link rel="stylesheet" type="text/css" href="css/styles.css" />
396+
<link rel="stylesheet" type="text/css" href="css/feed_wizard.css" />
397+
<style type="text/css">
398+
<?php echo $addcss . "\n"; ?>
399+
</style>
400+
401+
<script type="text/javascript" src="js/jquery.js" charset="utf-8"></script>
402+
<script type="text/javascript" src="js/jquery.scrollTo.min.js" charset="utf-8"></script>
403+
<script type="text/javascript" src="js/jquery-ui.min.js" charset="utf-8"></script>
404+
<script type="text/javascript" src="js/jquery.jeditable.mini.js" charset="utf-8"></script>
405+
<script type="text/javascript" src="js/tag-it.js" charset="utf-8"></script>
406+
<script type="text/javascript" src="js/overlib/overlib_mini.js" charset="utf-8"></script>
407+
<!-- URLBASE : "<?php echo tohtml(url_base()); ?>" -->
408+
<!-- TBPREF : "<?php echo tohtml($tbpref); ?>" -->
409+
<script type="text/javascript">
410+
//<![CDATA[
411+
var STATUSES = <?php echo json_encode(get_statuses()); ?>;
412+
//]]>
413+
</script>
414+
415+
<title>LWT :: <?php echo tohtml($title); ?></title>
416+
</head>
417+
<?php
418+
echo '<body>';
419+
?>
420+
<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
421+
<?php
422+
flush();
423+
if ($debug) {
424+
showRequest();
425+
}
426+
}
360427

361428
/**
362429
* Add a closing body tag.
@@ -581,4 +648,28 @@ function getsqlscoreformula($method): string
581648
return '0';
582649
}
583650

651+
652+
/**
653+
* Display a error message vanishing after a few seconds.
654+
*
655+
* @param string $msg Message to display.
656+
* @param bool $noback If true, don't display a button to go back
657+
*
658+
* @return string HTML-formatted string for an automating vanishing message.
659+
*/
660+
function error_message_with_hide($msg, $noback): string
661+
{
662+
if (trim($msg) == '') {
663+
return '';
664+
}
665+
if (substr($msg, 0, 5) == "Error" ) {
666+
return '<p class="red">*** ' . tohtml($msg) . ' ***' .
667+
($noback ?
668+
'' :
669+
'<br /><input type="button" value="&lt;&lt; Go back and correct &lt;&lt;" onclick="history.back();" />' ) .
670+
'</p>';
671+
}
672+
return '<p id="hide3" class="msgblue">+++ ' . tohtml($msg) . ' +++</p>';
673+
}
674+
584675
?>

‎inc/session_utility.php‎

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,35 +1721,7 @@ function get_playbackrate_selectoptions($v): string
17211721
return $r;
17221722
}
17231723

1724-
// -------------------------------------------------------------
17251724

1726-
function error_message_with_hide($msg,$noback): string
1727-
{
1728-
if (trim($msg) == '') { return '';
1729-
}
1730-
if (substr($msg, 0, 5) == "Error" ) {
1731-
return '<p class="red">*** ' . tohtml($msg) . ' ***' .
1732-
($noback ?
1733-
'' :
1734-
'<br /><input type="button" value="&lt;&lt; Go back and correct &lt;&lt;" onclick="history.back();" />' ) .
1735-
'</p>';
1736-
}
1737-
else {
1738-
return '<p id="hide3" class="msgblue">+++ ' . tohtml($msg) . ' +++</p>';
1739-
}
1740-
}
1741-
1742-
// -------------------------------------------------------------
1743-
1744-
function errorbutton($msg): string
1745-
{
1746-
if (substr($msg, 0, 5) == "Error" ) {
1747-
return '<input type="button" value="&lt;&lt; Back" onclick="history.back();" />';
1748-
}
1749-
else {
1750-
return '';
1751-
}
1752-
}
17531725

17541726

17551727
// -------------------------------------------------------------
@@ -1759,15 +1731,6 @@ function remove_soft_hyphens($str): string
17591731
return str_replace('­', '', $str); // first '..' contains Softhyphen 0xC2 0xAD
17601732
}
17611733

1762-
// -------------------------------------------------------------
1763-
1764-
function limitlength($s, $l)
1765-
{
1766-
if (mb_strlen($s, 'UTF-8') <= $l) { return $s;
1767-
}
1768-
return mb_substr($s, 0, $l, 'UTF-8');
1769-
}
1770-
17711734

17721735
// -------------------------------------------------------------
17731736

@@ -1781,15 +1744,16 @@ function replace_supp_unicode_planes_char($s): ?string
17811744

17821745
function makeCounterWithTotal($max, $num): string
17831746
{
1784-
if ($max == 1) { return '';
1747+
if ($max == 1) {
1748+
return '';
17851749
}
1786-
if ($max < 10) { return $num . "/" . $max;
1750+
if ($max < 10) {
1751+
return $num . "/" . $max;
17871752
}
17881753
return substr(
17891754
str_repeat("0", strlen($max)) . $num,
17901755
-strlen($max)
1791-
) .
1792-
"/" . $max;
1756+
) . "/" . $max;
17931757
}
17941758

17951759
// -------------------------------------------------------------

0 commit comments

Comments
 (0)