/
install.php
331 lines (254 loc) · 7.25 KB
/
install.php
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/* include ("../header.php"); */
$on = 0;
if ($on) die("Currently disabled.");
$uname="";
$upasswd="";
$udb="";
$dbinfo = array("username"=>$uname, "password"=>$upasswd, "database"=>$udb, "hostname"=>"localhost");
$db = mysql_connect("localhost",$uname,$upasswd);
function backup($dbinfo) {
// Make a backup of this entire database to file.
// Filename is:
$backupname = strftime("backups/localhost-".$dbinfo['database']."_%Y%m%d-%H%M%S.sql");
$retval = false;
$cmd = "mysqldump --add-drop-table -B ".$dbinfo['database']." -h ".$dbinfo['hostname']." -u ".$dbinfo['username']." --password=".$dbinfo['password']." > ".$backupname;
printf("Attempting to backup database ... ");
system($cmd, $success);
switch($success) {
case 0: printf(" success! Saved to $backupname.<br />"); break;
case 1: printf(" malformed command line.<br />"); break;
case 2: printf(" incorrect password.<br />"); break;
}
if ($success != 0) {
// Clean up after ourselves.
system("rm $backupname");
$retval = false;
} else {
// Or zip up the results
system("bzip2 $backupname");
$retval = true;
}
return $retval;
}
function testdb($db,$udb) {
// The user & database should already have been created
if (!mysql_select_db($udb,$db)) {
printf("Connection failed: ".mysql_error($db)."<br>");
} else {
printf("Hua! Everything worked.<br><br><a href='?typ=forms'>Return</a>");
}
}
function install($db,$udb,$fname,$dbinfo) {
mysql_select_db($udb,$db);
if (!backup($dbinfo)) {
printf("Backup failed. Cowardly refusing to continue.<br><a href='?typ=forms'>Return</a>");
return;
}
// Load the file into a variable, it gets loaded as an array
//$mytxt = file(dirname(__FILE__) . '/' . 'sqlsetup/setupWSE' . '.sql');
//$mytxt = file(dirname(__FILE__) . '/' . $fname . '.sql');
$mytxt = file(dirname(__FILE__) . '/' . $fname);
$current_statement = "";
// Loop through the array, reassemble the statements
foreach ($mytxt as $count => $line)
{
// Remove the -- comments
if (substr(trim($line), 0, 2) != '#')
$current_statement .= "\n" . rtrim($line);
// This marks the end of an SQL statement
if ((preg_match('~;[\s]*$~s', $line)) == 1) {
if ($_GET["opt"] != "supress") {
printf("Executing: <b>".$current_statement."</b><br>");
} else {
printf("Executing: <b>".htmlspecialchars($current_statement)."</b><br>");
}
// Attempt to execute the statement
if (!mysql_query($current_statement))
printf("Error: ".mysql_error($db)."<br>");
// Reset the statement to blank, to build the next one.
$current_statement = "";
printf("<br>");
}
}
printf("Installation finished.<br><a href='?typ=forms'>Return</a>");
}
function runsql($db,$udb,$sql) {
mysql_select_db($udb,$db);
$rslt = mysql_query($sql);
if (!$rslt) {
printf("Error: ".mysql_error($db)."<br>");
} else {
if (mysql_num_rows($rslt) > 0) {
echo("<table>");
/* for ($x = 0; $x < count(mysql_fetch_row($rslt)); $x++) {
echo("<th>Row ".$x."</th>");
}
echo("</tr>");
*/
while ($myrow = mysql_fetch_row($rslt)) {
echo("<tr>");
for ($x = 0; $x < count($myrow); $x++) {
echo("<td>".$myrow[$x]."</td>");
}
echo("</tr>");
}
echo("</table>");
} else {
printf($sql);
}
}
echo("<br><a href='?typ=forms'>Return</a>");
}
if (isset($_GET['typ'])) {
$typ = $_GET['typ'];
}else{
$typ = "forms";
}
switch ($typ) {
case "forms":
?>
<style>
h4 {margin-bottom: 3px; background-color: #99ddff; padding-left: 5px;}
table {border: 1px solid gray;}
th {background-color: #cceeff;}
.odd {background-color: #ddeeff;}
</style>
<h4>Generate Password</h4>
<form action="?typ=password" method="post">
<input size=50 type=text name="password"><input type=submit value="Get sha1 hash">
</form>
<h4>Database test utilities</h4>
<a href="?typ=test">Test database connection</a><br><br>
<a href="?typ=backup">Backup database</a><br><br>
<a href="?typ=delall">Clear all tables</a><br><br>
<a href="?typ=iall">Install all SQL</a><br><br>
<a href="?typ=reset">Reset (clear / install all)</a><br>
<h4>Table instantiation</h4>
<table width=450>
<tr>
<th>SQL file</th>
<th>Install</th>
<th>Delete</th>
</tr>
<?php
$d = dir(".");
$oddline = false;
while (false !== ($entry = $d->read())) {
if ($entry!="." && $entry!=".." && $entry!="null.sql"){
$farray = explode(".",$entry);
$issql = false;
$flen = count($farray);
if ($farray[$flen-1] == "sql") $issql = true;
//echo $farray[$flen-1];
if ($issql) {
echo '<tr>';
echo '<td'.($oddline?' class="odd"':'').'>'.$entry.'</td>';
echo '<td'.($oddline?' class="odd"':'').'><a href="?typ=install&opt=supress&file='.$entry.'">Install '.$farray[0].'</a></td>';
echo '<td'.($oddline?' class="odd"':'').'><a href="?typ=delete&opt=supress&file='.$entry.'">Delete '.$farray[0].'</a></td>';
echo '</tr>';
$oddline = !$oddline;
}
}
}
$d->close();
?>
</table>
<br />
<a href="?typ=install&opt=supress&file=null.sql">Install null.sql</a>
<br />
<h4>Current tables</h4>
<?php
runsql($db,$udb,"SHOW TABLES");
?>
<br /><br />
<form action="?typ=execute" method="post">
<input size=50 type=text name="query"><input type=submit>
</form>
<?php
break;
case "test":
testdb($db,$udb);
break;
case "install":
install($db,$udb,$_GET["file"], $dbinfo);
break;
case "backup":
if (!backup($dbinfo)) {
printf("Backup failed. Cowardly refusing to continue.<br><a href='?typ=forms'>Return</a>");
return;
} else {
printf("Backup completed. Nice. <br><a href='?typ=forms'>Return</a>");
}
break;
case "delete":
if (!backup($dbinfo)) {
printf("Backup failed. Cowardly refusing to continue.<br><a href='?typ=forms'>Return</a>");
return;
}
$farray = explode(".",$_GET["file"]);
runsql($db,$udb,"DROP TABLE ".$farray[0]);
break;
case "iall":
$d = dir(".");
while (false !== ($entry = $d->read())) {
if ($entry!="." && $entry!=".." && $entry!="null.sql"){
$farray = explode(".",$entry);
$issql = false;
$flen = count($farray);
if ($farray[$flen-1] == "sql") $issql = true;
//echo $farray[$flen-1];
if ($issql) {
install($db,$udb,$entry, $dbinfo);
}
}
}
$d->close();
break;
case "delall":
$d = dir(".");
while (false !== ($entry = $d->read())) {
if ($entry!="." && $entry!=".." && $entry!="null.sql"){
$farray = explode(".",$entry);
$issql = false;
$flen = count($farray);
if ($farray[$flen-1] == "sql") $issql = true;
//echo $farray[$flen-1];
if ($issql) {
runsql($db,$udb,"DROP TABLE ".$farray[0]);
}
}
}
$d->close();
break;
case "reset":
if (!backup($dbinfo)) {
printf("Backup failed. Cowardly refusing to continue.<br><a href='?typ=forms'>Return</a>");
return;
}
$d = dir(".");
while (false !== ($entry = $d->read())) {
if ($entry!="." && $entry!=".." && $entry!="null.sql"){
$farray = explode(".",$entry);
$issql = false;
$flen = count($farray);
if ($farray[$flen-1] == "sql") $issql = true;
//echo $farray[$flen-1];
if ($issql) {
runsql($db,$udb,"DROP TABLE ".$farray[0]);
install($db,$udb,$entry, $dbinfo);
}
}
}
$d->close();
break;
case "execute":
runsql($db,$udb,$_POST["query"]);
break;
case "password":
$passwordhash = sha1($_GET['password']);
printf($_GET['password']." hashes to: ".$passwordhash);
break;
}
/* include ("../footer.php"); */
?>