forked from spotweb/spotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.php
executable file
·574 lines (488 loc) · 16.9 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
error_reporting(2147483647);
require_once "lib/SpotClassAutoload.php";
SpotClassAutoload::register();
try {
@include('settings.php');
@include('dbsettings.inc.php');
}
catch(Exception $x) {
// ignore errors
} # catch
set_error_handler("ownWarning",E_WARNING);
if (file_exists('reallymyownsettings.php'))
{
include_once('reallymyownsettings.php');
}
/*
* We output headers after already sending HTML, make
* sure output buffering is turned on.
*/
ob_start();
/*
* We default to a succeeded install, let it prove
* otherwise
*/
global $_testInstall_Ok;
$_testInstall_Ok = true;
session_start();
function showTemplate($tplname, $vars) {
global $settings;
global $_testInstall_Ok;
/*
* Make the variables availbale to the local context
*/
extract($vars, EXTR_REFS);
require_once "templates/installer/includes/header.inc.php";
require_once "templates/installer/" . $tplname;
} # showTemplate
function performAndPrintTests() {
global $settings;
global $_testInstall_Ok;
/*
* Load all the SSL signing code, we need it to create a private key
*/
require_once "lib/services/Signing/Services_Signing_Base.php";
require_once "lib/services/Signing/Services_Signing_Php.php";
require_once "lib/services/Signing/Services_Signing_Openssl.php";
$spotSigning = Services_Signing_Base::factory();
$privKey = $spotSigning->createPrivateKey($settings['openssl_cnf_path']);
/* We need either one of those 3 extensions, so set the error flag manually */
if ( (!extension_loaded('openssl')) && (!extension_loaded('gmp')) && (!extension_loaded('bcmath'))) {
$_testInstall_Ok = false;
} # if
/*
* Try to create the cache directory
*/
@mkdir('./cache', 0777);
/*
* Load the template
*/
showTemplate("step-001.inc.php", array('privKey' => $privKey));
} # performAndPrintTests
function askDbSettings() {
global $settings;
global $_testInstall_Ok;
if (!isset($settings['mydb'])) {
$form = array('engine' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'spotweb',
'user' => 'spotweb',
'pass' => 'spotweb',
'submit' => '');
} else {
$form = $settings['mydb'];
unset($settings['mydb']);
} # else
if (isset($_POST['dbform'])) {
$form = array_merge($form, $_POST['dbform']);
} # if
/*
* Did the user press submit? If so, try to
* connect to the database
*/
$databaseCreated = false;
if ($form['submit'] === 'Verify database') {
try {
$dbCon = dbeng_abs::getDbFactory($form['engine']);
$dbCon->connect($form['host'],
$form['user'],
$form['pass'],
$form['dbname']);
$databaseCreated = true;
/*
* Store the given database settings in the
* SESSION object, we need it later to generate
* a 'dbsettings.inc.php' file
*/
$_SESSION['spotsettings']['db'] = $form;
/*
* and call the next stage in the setup
*/
Header("Location: " . $_SERVER['SCRIPT_NAME'] . '?page=3');
}
catch(Exception $x) {
showTemplate("fatalerror.inc.php", array('x' => $x));
} # exception
} # if
if (!$databaseCreated) {
showTemplate("step-002.inc.php", array('form' => $form));
} # else
} # askDbSettings
function askNntpSettings() {
global $settings;
global $_testInstall_Ok;
/*
* Loading the file directly seems to sometimes result
* in a weird error. GH issue #1861
*/
$serverList = simplexml_load_string(file_get_contents('usenetservers.xml'));
if (!isset($settings['mynntp'])) {
$form = array('name' => 'custom',
'host' => '',
'user' => '',
'pass' => '',
'port' => 119,
'enc' => false,
'submit' => '');
} else {
$form = $settings['mynntp'];
unset($settings['mynntp']);
} # else
if (isset($_POST['nntpform'])) {
$form = array_merge($form, $_POST['nntpform']);
} # if
/*
* Did the user press submit? If so, try to
* connect to the database
*/
$nntpVerified = false;
if (($form['submit'] === 'Verify usenet server') ||
($form['submit'] === 'Skip validation')) {
try {
/*
* Convert the selected NNTP name to an actual
* server record.
*/
if ($form['name'] == 'custom') {
$form['buggy'] = false;
$form['hdr'] = $form;
$form['nzb'] = $form;
$form['post'] = $form;
} else {
foreach($serverList->usenetservers->server as $provider) {
if (extension_loaded('openssl') && isset($provider->ssl)) {
$server = $provider->ssl;
} else {
$server = $provider->plain;
} # if
if ( (string) $provider['name'] == $form['name'] ) {
# Header usenet server
$form['hdr']['host'] = (string) $server->header;
$form['hdr']['user'] = $form['user'];
$form['hdr']['pass'] = $form['pass'];
if ( (string) $server->header['ssl'] == 'yes') {
$form['hdr']['enc'] = 'ssl';
} else {
$form['hdr']['enc'] = false;
} # else
$form['hdr']['port'] = (int) $server->header['port'];
$form['hdr']['buggy'] = (boolean) $server['buggy'];
# NZB usenet server
$form['nzb']['host'] = (string) $server->nzb;
$form['nzb']['user'] = $form['user'];
$form['nzb']['pass'] = $form['pass'];
if ( (string) $server->nzb['ssl'] == 'yes') {
$form['nzb']['enc'] = 'ssl';
} else {
$form['nzb']['enc'] = false;
} # else
$form['nzb']['port'] = (int) $server->nzb['port'];
$form['nzb']['buggy'] = (boolean) $server['buggy'];
# Posting usenet server
$form['post']['host'] = (string) $server->post;
$form['post']['user'] = $form['user'];
$form['post']['pass'] = $form['pass'];
if ( (string) $server->post['ssl'] == 'yes') {
$form['post']['enc'] = 'ssl';
} else {
$form['post']['enc'] = false;
} # else
$form['post']['port'] = (int) $server->post['port'];
$form['post']['buggy'] = (boolean) $server['buggy'];
} # if
} # foreach
} # else
/* and try to connect to the usenet server */
if ($form['submit'] === 'Verify usenet server') {
$nntp = new Services_Nntp_Engine($form['hdr']);
$nntp->validateServer();
} # if
$nntpVerified = true;
/*
* Store the given NNTP settings in the
* SESSION object, we need it later to update
* the settings in the database
*/
$_SESSION['spotsettings']['nntp'] = $form;
/*
* and call the next stage in the setup
*/
Header("Location: " . $_SERVER['SCRIPT_NAME'] . '?page=4');
}
catch(Exception $x) {
showTemplate("fatalerror.inc.php", array('x' => $x));
} # exception
} # if
if (!$nntpVerified) {
showTemplate("step-003.inc.php", array('form' => $form,
'nntpVerified' > $nntpVerified,
'serverList' => $serverList));
} # else
} # askNntpSettings
function askSpotwebSettings() {
global $settings;
global $_testInstall_Ok;
if (!isset($settings['myadminuser'])) {
$form = array('systemtype' => 'public',
'username' => '', 'newpassword1' => '', 'newpassword2' => '', 'firstname' => '',
'lastname' => '', 'mail' => '', 'userid' => -1);
} else {
$form = $settings['myadminuser'];
unset($settings['myadminuser']);
}
if (isset($_POST['settingsform'])) {
$form = array_merge($form, $_POST['settingsform']);
} # if
/*
* Did the user press submit? If so, try to
* connect to the database
*/
$userVerified = false;
if ((isset($form['submit'])) && ($form['submit'] === 'Create system')) {
try {
/*
* Store the given user settings in the
* SESSION object, we need it later to update
* the settings in the database
*/
$_SESSION['spotsettings']['adminuser'] = $form;
/*
* Override the Service_User_Record class so we can override userEmailExists()
* to not require database access.
*/
class Services_ValidateUser_Record extends Services_User_Record {
function validateUserEmailExists($user) {
$result = new Dto_FormResult();
if (($user['mail'] == '[email protected]') || ($user['mail'] == '[email protected]')) {
$result->addError(_('Mailaddress is already in use'));
} # if
return $result;
} # validateUserRecord
}
/*
* Get the schema version and other constants
*/
require_once "lib/Bootstrap.php";
$bootstrap = new Bootstrap();
/*
* And initiate the user system, this allows us to use
* validateUserRecord()
*/
$dbsettings = $_SESSION['spotsettings']['db'];
$dbCon = dbeng_abs::getDbFactory($dbsettings['engine']);
$dbCon->connect($dbsettings['host'],
$dbsettings['user'],
$dbsettings['pass'],
$dbsettings['dbname']);
$daoFactory = Dao_Factory::getDAOFactory($dbsettings['engine']);
$daoFactory->setConnection($dbCon);
$svcUserRecord = new Services_ValidateUser_Record($daoFactory, $bootstrap->getSettings($daoFactory, false));
$errorList = $svcUserRecord->validateUserRecord($form, false)->getErrors();
if (!empty($errorList)) {
throw new Exception($errorList[0]);
} # if
/*
* and call the next stage in the setup
*/
Header("Location: " . $_SERVER['SCRIPT_NAME'] . '?page=99');
}
catch(Exception $x) {
showTemplate("fatalerror.inc.php", array('x' => $x));
} # exception
} # if
if (!$userVerified) {
showTemplate("step-004.inc.php", array('form' => $form,
'userVerified' => $userVerified));
} # else
} # askSpotwebSettings
function createSystem() {
global $settings;
global $_testInstall_Ok;
try {
/*
* The settings system is used to create a lot of output,
* we swallow it all
*/
ob_start();
/*
* Get the schema version and other constants
*/
require_once "lib/Bootstrap.php";
$bootstrap = new Bootstrap();
/*
* Now create the database
*/
$dbsettings = $_SESSION['spotsettings']['db'];
$dbCon = dbeng_abs::getDbFactory($dbsettings['engine']);
$dbCon->connect($dbsettings['host'],
$dbsettings['user'],
$dbsettings['pass'],
$dbsettings['dbname']);
$daoFactory = Dao_Factory::getDAOFactory($dbsettings['engine']);
$daoFactory->setConnection($dbCon);
/*
* The database must exist before we can get the Service_Settings_Base instance
*/
$dbStruct = SpotStruct_abs::factory($dbsettings['engine'], $daoFactory->getConnection());
$dbStruct->updateSchema();
$spotSettings = $bootstrap->getSettings($daoFactory, false);
$svcUpgradeBase = new Services_Upgrade_Base($daoFactory, $spotSettings, $dbsettings['engine']);
/*
* Create all the different settings (only the default) ones
*/
$svcUpgradeBase->settings();
/*
* Create the users
*/
$svcUpgradeBase->users();
/*
* print all the output as HTML comment for debugging
*/
$dbCreateOutput = ob_get_contents();
ob_end_clean();
/*
* Now it is time to do something with
* the information the user has given to us
*/
/*
* Update the NNTP settings in the databas
*/
$spotSettings->set('nntp_nzb', $_SESSION['spotsettings']['nntp']['nzb']);
$spotSettings->set('nntp_hdr', $_SESSION['spotsettings']['nntp']['hdr']);
$spotSettings->set('nntp_post', $_SESSION['spotsettings']['nntp']['post']);
/*
* Create the given user
*/
$svcUserRecord = new Services_User_Record($daoFactory, $spotSettings);
$spotUser = $_SESSION['spotsettings']['adminuser'];
/*
* and actually add the user
*/
$spotUser['userid'] = $svcUserRecord->createUserRecord($spotUser)->getData('userid');
/*
* When the new user was created a random password was assigned,
* so now have to set the supplied password
*/
$svcUserRecord->setUserPassword($spotUser);
# Change the administrators' account password to that of this created user
$adminUser = $svcUserRecord->getUser(SPOTWEB_ADMIN_USERID);
$adminUser['newpassword1'] = $spotUser['newpassword1'];
$svcUserRecord->setUserPassword($adminUser);
# update the settings with our system type and our admin id
$spotSettings->set('custom_admin_userid', $spotUser['userid']);
$spotSettings->set('systemtype', $spotUser['systemtype']);
# Set the system type
$svcUpgradeBase->resetSystemType($spotUser['systemtype']);
/*
* Create the necessary database connection information
*/
$dbConnectionString = '';
switch ($_SESSION['spotsettings']['db']['engine']) {
case 'pdo_mysql' : {
$dbConnectionString .= "\$dbsettings['engine'] = 'pdo_mysql';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['host'] = '" . $_SESSION['spotsettings']['db']['host'] . "';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['dbname'] = '" . $_SESSION['spotsettings']['db']['dbname'] . "';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['user'] = '" . $_SESSION['spotsettings']['db']['user'] . "';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['pass'] = '" . $_SESSION['spotsettings']['db']['pass'] . "';" . PHP_EOL;
break;
} # mysql
case 'pdo_pgsql' : {
$dbConnectionString .= "\$dbsettings['engine'] = 'pdo_pgsql';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['host'] = '" . $_SESSION['spotsettings']['db']['host'] . "';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['dbname'] = '" . $_SESSION['spotsettings']['db']['dbname'] . "';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['user'] = '" . $_SESSION['spotsettings']['db']['user'] . "';" . PHP_EOL;
$dbConnectionString .= "\$dbsettings['pass'] = '" . $_SESSION['spotsettings']['db']['pass'] . "';" . PHP_EOL;
break;
} # pdo_pgsql
} # switch
# Try to create the dbsettings.inc.php file for the user
@file_put_contents("dbsettings.inc.php", "<?php" . PHP_EOL . $dbConnectionString);
$createdDbSettings = file_exists("dbsettings.inc.php");
showTemplate("step-final.inc.php", array('createdDbSettings' => $createdDbSettings,
'dbCreateOutput' => $dbCreateOutput,
'dbConnectionString' => $dbConnectionString));
} # try
catch(Exception $x) {
showTemplate("fatalerror.inc.php", array('x' => $x));
} # exception
} # createSystem
function return_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
// The 'G' modifier is available since PHP 5.1.0
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
} # return_bytes
function showResult($b, $isRequired, $okMsg="", $nokMsg="") {
global $_testInstall_Ok;
if ($b) {
echo "OK";
if (!empty($okMsg)) {
echo ' (' . $okMsg . ')';
}
} else {
echo "NOT OK";
if (!empty($nokMsg)) {
echo ' (' . $nokMsg . ')';
}
if ($isRequired) {
$_testInstall_Ok = true;
} # if
} # else
return null;
} # showResult
/*
* Dummy translate function
*/
if (!function_exists('_')) {
function _($s) {
return $s;
} # _()
} # if
function ownWarning($errno, $errstr) {
/* don't show errors if they are being suppressed by silent (@) operator */
if (error_reporting() == 0) {
return;
}
$GLOBALS['iserror'] = true;
error_log($errstr);
echo $errstr;
} # ownWarning
function testInclude($fname) {
@include_once($fname);
foreach (get_included_files() as $filename) {
if (strpos($filename, $fname, strlen($filename) - strlen($fname)) !== false) {
return dirname($filename);
}
}
return false;
} # testInclude
/*
* Only run the wizard when no database settings have been entered yet, to prevent
* any information disclosure
*/
if ((isset($dbsettings)) && (isset($_GET['page']))) {
showTemplate("fatalerror.inc.php",
array('x' => new Exception("Spotweb has already been setup. If you want to run this wizard again, please remove the file 'dbsettings.inc.php'")));
die();
} # if
/*
* determine what page of the wizzard we are on, and display that one
*/
$pageNumber = (isset($_GET['page']) ? $_GET['page'] : 1);
switch($pageNumber) {
case 2 : askDbSettings(); break;
case 3 : askNntpSettings(); break;
case 4 : askSpotwebSettings(); break;
case 99 : createSystem(); break;
default : performAndPrintTests(); break;
} # switch
ob_end_flush();