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