This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
setup.php
399 lines (331 loc) · 12.4 KB
/
setup.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Weave Minimal Server
#
# The Initial Developer of the Original Code is
# Stefan Fischer
# Portions created by the Initial Developer are Copyright (C) 2012
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Daniel Triendl <daniel@pew.cc>
# balu
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
// --------------------------------------------
// variables start
// --------------------------------------------
$action = null;
$dbType = null;
$dbUser = null;
$dbName = null;
$dbPass = null;
$dbHost = null;
// --------------------------------------------
// variables end
// --------------------------------------------
// --------------------------------------------
// post handling start
// --------------------------------------------
if ( isset( $_POST['action'] ) ) {
$action = check_input($_POST['action']);
}
if ( isset( $_POST['dbType'] ) ) {
$dbType = check_input($_POST['dbType']);
}
if ( isset( $_POST['dbhost'] ) ) {
$dbHost = check_input($_POST['dbhost']);
}
if ( isset( $_POST['dbname'] ) ) {
$dbName = check_input($_POST['dbname']);
}
if ( isset( $_POST['dbuser'] ) ) {
$dbUser = check_input($_POST['dbuser']);
}
if ( isset( $_POST['dbpass'] ) ) {
$dbPass = check_input($_POST['dbpass']);
}
// --------------------------------------------
// post handling end
// --------------------------------------------
// --------------------------------------------
// functions start
// --------------------------------------------
/*
ensure that the input is not total waste
*/
function check_input( $data ) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
/*
create the config file with the database type
and the given connection credentials
*/
function write_config_file($dbt, $dbh, $dbn, $dbu, $dbp, $fsRoot) {
// construct the name of config file
//
$path = explode('/', $_SERVER['SCRIPT_FILENAME']);
array_pop($path);
array_push($path, 'settings.php');
$cfg_file_name = implode('/', $path);
if ( file_exists($cfg_file_name) && filesize( $cfg_file_name ) > 0 ) {
echo "<hr>The config file $cfg_file_name is already present</hr>";
return;
}
echo "Creating cfg file: " . $cfg_file_name;
// now build the content of the config file
//
$cfg_content = "<?php\n\n";
$cfg_content .= " // you can disable registration to the firefox sync server here,\n";
$cfg_content .= " // by setting ENABLE_REGISTER to false\n";
$cfg_content .= " // \n";
$cfg_content .= " define(\"ENABLE_REGISTER\", true);\n\n";
$cfg_content .= " // firefox sync server url, this should end with a /\n";
$cfg_content .= " // e.g. https://YourDomain.de/Folder_und_ggf_/index.php/\n";
$cfg_content .= " // \n";
$cfg_content .= " define(\"FSYNCMS_ROOT\", \"$fsRoot\");\n\n";
$cfg_content .= " // Database connection credentials\n";
$cfg_content .= " // \n";
$cfg_content .= " define(\"SQLITE_FILE\", \"weave_db\");\n";
if ( $dbt != "mysql" ) {
$cfg_content .= " define(\"MYSQL_ENABLE\", false);\n";
$cfg_content .= " define(\"MYSQL_HOST\", \"localhost\");\n";
$cfg_content .= " define(\"MYSQL_DB\", \"fsync\");\n";
$cfg_content .= " define(\"MYSQL_USER\", \"fsyncUserName\");\n";
$cfg_content .= " define(\"MYSQL_PASSWORD\", \"fsyncUserPassword\");\n";
} else {
$cfg_content .= " define(\"MYSQL_ENABLE\", true);\n";
$cfg_content .= " define(\"MYSQL_HOST\", \"$dbh\");\n";
$cfg_content .= " define(\"MYSQL_DB\", \"$dbn\");\n";
$cfg_content .= " define(\"MYSQL_USER\", \"$dbu\");\n";
$cfg_content .= " define(\"MYSQL_PASSWORD\", \"$dbp\");\n";
}
$cfg_content .= "\n";
$cfg_content .= " // Use bcrypt instead of MD5 for password hashing\n";
$cfg_content .= " define(\"BCRYPT\", true);\n";
$cfg_content .= " define(\"BCRYPT_ROUNDS\", 12);\n";
$cfg_content .= "\n?>\n";
// now write everything
//
$cfg_file = fopen($cfg_file_name, "a");
fputs($cfg_file, "$cfg_content");
fclose($cfg_file);
}
/*
print the html header for the form
*/
function print_header( $title ) {
if ( ! isset( $title ) ) {
$title = "";
}
print '<html><header><title>' . $title . '</title><body>
<h1>Setup FSyncMS</h1>
<form action="setup.php" method="post">';
}
/*
print the html footer
*/
function print_footer() {
print '</form></body></html>';
}
/*
print the html for for the mysql connection credentials
*/
function print_mysql_connection_form() {
print_header("MySQL database connection setup");
print 'MySQL database connection setup
<table>
<tr>
<td>Host</td>
<td><input type="text" name="dbhost" /></td>
</tr>
<tr>
<td>Instance name</td>
<td><input type="text" name="dbname" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="dbuser" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="dbpass" /></td>
</tr>
</table>
<input type="hidden" name="action" value="step2">
<input type="hidden" name="dbType" value="mysql">
<p><input type="submit" value="OK"></p>';
print_footer();
}
// --------------------------------------------
// functions end
// --------------------------------------------
// check if we have no configuration at the moment
//
if ( file_exists("settings.php") && filesize( "settings.php" ) > 0 ) {
echo "<hr><h2>The setup looks like it's completed, please delete settings.php</h2><hr>";
exit;
}
// inital page - select the database type
//
if ( ! $action ) {
// first check if we have pdo installed (untested)
//
if ( ! extension_loaded('PDO') ) {
print "ERROR - PDO is missing in the php installation!";
exit();
}
$validPdoDriver = 0;
print_header("Setup FSyncMS - DB Selection");
print 'Which database type should be used?<br>';
if ( extension_loaded('pdo_mysql') ) {
print '<input type="radio" name="dbType" value="mysql" /> MySQL <br>';
$validPdoDriver++;
} else {
print 'MySQL not possible (Driver missing) <br>';
}
if ( extension_loaded('pdo_sqlite') ) {
print '<input type="radio" name="dbType" value="sqlite" checked="checked" /> SQLite ';
$validPdoDriver++;
} else {
print 'SQLite not possible (Driver missing) <br>';
}
if ( $validPdoDriver < 1 ) {
print '<hr> No valid pdo driver found! Please install a valid pdo driver first <hr>';
} else {
print '<input type="hidden" name="action" value="step1">
<p><input type="submit" value="OK" /></p>';
}
// ensure we bail out at this point ;)
exit();
};
// step 2 (connection data) below
//
if ( $action == "step1" ) {
// now check if the database is in place
//
print_header("Setup FSyncMS - DB Setup: $dbType!");
switch ( $dbType ) {
case "sqlite":
$action = "step2";
break;
case "mysql":
print_mysql_connection_form();
break;
default:
print "ERROR - This type of database ($dbType) is not valid at the moment!";
exit();
break;
}
}
// now generate the database
//
if ( $action == "step2" ) {
$dbInstalled = false;
$dbHandle = null;
try {
if ( $dbType == "sqlite" ) {
$path = explode('/', $_SERVER['SCRIPT_FILENAME']);
$db_name = 'weave_db';
array_pop($path);
array_push($path, $db_name);
$db_name = implode('/', $path);
if ( file_exists($db_name) && filesize( $db_name ) > 0 ) {
$dbInstalled = true;
} else {
echo("Creating sqlite weave storage: ". $db_name ."<br>");
$dbHandle = new PDO('sqlite:' . $db_name);
$dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
} else if ( $dbType == "mysql" ) {
$dbHandle = new PDO("mysql:host=". $dbHost .";dbname=". $dbName, $dbUser, $dbPass);
$select_stmt = "show tables like 'wbo'";
$sth = $dbHandle->prepare($select_stmt);
$sth->execute();
$count = $sth->rowCount();
if ( $count > 0 ) {
$dbInstalled = true;
}
};
} catch ( PDOException $exception ) {
echo("database unavailable " . $exception->getMessage());
throw new Exception("Database unavailable " . $exception->getMessage() , 503);
}
if ( $dbInstalled ) {
echo "DB is already installed!<br>";
} else {
echo "Now going to install the new database! Type is: $dbType<br>";
try {
$create_statement = " create table wbo ( username varchar(100), id varchar(65), collection varchar(100),
parentid varchar(65), predecessorid int, modified real, sortindex int,
payload text, payload_size int, ttl int, primary key (username,collection,id))";
$create_statement2 = " create table users ( username varchar(255), md5 varchar(124), primary key (username)) ";
$index1 = 'create index parentindex on wbo (username, parentid)';
$index2 = 'create index predecessorindex on wbo (username, predecessorid)';
$index3 = 'create index modifiedindex on wbo (username, collection, modified)';
$sth = $dbHandle->prepare($create_statement);
$sth->execute();
$sth = $dbHandle->prepare($create_statement2);
$sth->execute();
$sth = $dbHandle->prepare($index1);
$sth->execute();
$sth = $dbHandle->prepare($index2);
$sth->execute();
$sth = $dbHandle->prepare($index3);
$sth->execute();
echo "Database created <br>";
} catch( PDOException $exception ) {
throw new Exception("Database unavailable", 503);
}
}
//guessing fsroot
// get the FSYNC_ROOT url
//
$fsRoot ="https://";
if ( ! isset($_SERVER['HTTPS']) ) {
$fsRoot = "http://";
}
$fsRoot .= $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . "/";
if( strpos( $_SERVER['REQUEST_URI'], 'index.php') !== 0 ) {
$fsRoot .= "index.php/";
}
// write settings.php, if not possible, display the needed contant
//
write_config_file($dbType, $dbHost, $dbName, $dbUser, $dbPass, $fsRoot);
echo "<hr><hr> Finished the setup, please delete setup.php and go on with the FFSync<hr><hr>";
echo <<<EOT
<hr><hr>
<h4>This script has guessed the Address of your installation, this might not be accurate,<br/>
Please check if this script can be reached by <a href="$fsRoot">$fsRoot</a> .<br/>
If thats not the case you have to ajust the settings.php<br />
</h4>
EOT;
}
?>