Setup.php 25.5 KB
Newer Older
redmatrix's avatar
redmatrix committed
1
2
3
<?php
namespace Zotlabs\Module;
/**
4
 * @file Zotlabs/Module/Setup.php
redmatrix's avatar
redmatrix committed
5
 *
6
 * @brief Controller for the initial setup/installation.
redmatrix's avatar
redmatrix committed
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * @todo This setup module could need some love and improvements.
 */


/**
 * @brief Initialisation for the setup module.
 *
 */
class Setup extends \Zotlabs\Web\Controller {

	private static $install_wizard_pass = 1;

20
21
22
23
	/**
	 * {@inheritDoc}
	 * @see \\Zotlabs\\Web\\Controller::init()
	 */
redmatrix's avatar
redmatrix committed
24
25
26
27
28
	function init() {
		// Ensure that if somebody hasn't read the install documentation and doesn't have all
		// the required modules or has a totally borked shared hosting provider and they can't
		// figure out what the hell is going on - that we at least spit out an error message which
		// we can inquire about when they write to tell us that our software doesn't work.
29

redmatrix's avatar
redmatrix committed
30
31
32
33
34
35
		// The worst thing we can do at this point is throw a white screen of death and rely on
		// them knowing about servers and php modules and logfiles enough so that we can guess
		// at the source of the problem. As ugly as it may be, we need to throw a technically worded
		// PHP error message in their face. Once installation is complete application errors will
		// throw a white screen because these error messages divulge information which can
		// potentially be useful to hackers.
36

redmatrix's avatar
redmatrix committed
37
38
39
		error_reporting(E_ERROR | E_WARNING | E_PARSE );
		ini_set('log_errors', '0');
		ini_set('display_errors', '1');
40

Felix Wolfsteller's avatar
Felix Wolfsteller committed
41
		// $baseurl/setup/testrewrite to test if rewrite in .htaccess is working
redmatrix's avatar
redmatrix committed
42
43
44
45
		if (argc() == 2 && argv(1) == "testrewrite") {
			echo 'ok';
			killme();
		}
46

47
		if (x($_POST, 'pass')) {
redmatrix's avatar
redmatrix committed
48
			$this->install_wizard_pass = intval($_POST['pass']);
49
		} else {
redmatrix's avatar
redmatrix committed
50
			$this->install_wizard_pass = 1;
51
		}
redmatrix's avatar
redmatrix committed
52
	}
53

redmatrix's avatar
redmatrix committed
54
55
56
57
	/**
	 * @brief Handle the actions of the different setup steps.
	 *
	 */
58
	function post() {
59

redmatrix's avatar
redmatrix committed
60
61
62
63
		switch($this->install_wizard_pass) {
			case 1:
			case 2:
				return;
64
				// implied break;
redmatrix's avatar
redmatrix committed
65
66
67
68
69
70
71
72
73
74
75
			case 3:
				$urlpath = \App::get_path();
				$dbhost = trim($_POST['dbhost']);
				$dbport = intval(trim($_POST['dbport']));
				$dbuser = trim($_POST['dbuser']);
				$dbpass = trim($_POST['dbpass']);
				$dbdata = trim($_POST['dbdata']);
				$dbtype = intval(trim($_POST['dbtype']));
				$phpath = trim($_POST['phpath']);
				$adminmail = trim($_POST['adminmail']);
				$siteurl = trim($_POST['siteurl']);
76

redmatrix's avatar
redmatrix committed
77
				// $siteurl should not have a trailing slash
78

redmatrix's avatar
redmatrix committed
79
				$siteurl = rtrim($siteurl,'/');
80

redmatrix's avatar
redmatrix committed
81
				require_once('include/dba/dba_driver.php');
redmatrix's avatar
redmatrix committed
82
83

				$db = \DBA::dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true);
84

redmatrix's avatar
redmatrix committed
85
				if(! \DBA::$dba->connected) {
86
					echo 'Database Connect failed: ' . \DBA::$dba->error;
redmatrix's avatar
redmatrix committed
87
88
89
					killme();
				}
				return;
90
				// implied break;
redmatrix's avatar
redmatrix committed
91
92
			case 4:
				$urlpath = \App::get_path();
Zot's avatar
Zot committed
93
94
95
96
97
98
99
100
101
102
				$dbhost = trim($_POST['dbhost']);
				$dbport = intval(trim($_POST['dbport']));
				$dbuser = trim($_POST['dbuser']);
				$dbpass = trim($_POST['dbpass']);
				$dbdata = trim($_POST['dbdata']);
				$dbtype = intval(trim($_POST['dbtype']));
				$phpath = trim($_POST['phpath']);
				$timezone = trim($_POST['timezone']);
				$adminmail = trim($_POST['adminmail']);
				$siteurl = trim($_POST['siteurl']);
103

redmatrix's avatar
redmatrix committed
104
105
106
107
108
109
110
111
				if($siteurl != z_root()) {
					$test = z_fetch_url($siteurl."/setup/testrewrite");
					if((! $test['success']) || ($test['body'] != 'ok'))  {
						\App::$data['url_fail'] = true;
						\App::$data['url_error'] = $test['error'];
						return;
					}
				}
112

redmatrix's avatar
redmatrix committed
113
114
115
116
117
118
				if(! \DBA::$dba->connected) {
					// connect to db
					$db = \DBA::dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true);
				}

				if(! \DBA::$dba->connected) {
redmatrix's avatar
redmatrix committed
119
120
121
					echo 'CRITICAL: DB not connected.';
					killme();
				}
122

redmatrix's avatar
redmatrix committed
123
124
				$tpl = get_intltext_template('htconfig.tpl');
				$txt = replace_macros($tpl,array(
125
126
127
128
129
130
					'$dbhost'      => $dbhost,
					'$dbport'      => $dbport,
					'$dbuser'      => $dbuser,
					'$dbpass'      => $dbpass,
					'$dbdata'      => $dbdata,
					'$dbtype'      => $dbtype,
Zot's avatar
Zot committed
131
					'$server_role' => 'pro',
132
133
134
135
					'$timezone'    => $timezone,
					'$siteurl'     => $siteurl,
					'$site_id'     => random_string(),
					'$phpath'      => $phpath,
redmatrix's avatar
redmatrix committed
136
137
					'$adminmail' => $adminmail
				));
138

redmatrix's avatar
redmatrix committed
139
140
141
142
				$result = file_put_contents('.htconfig.php', $txt);
				if(! $result) {
					\App::$data['txt'] = $txt;
				}
143

redmatrix's avatar
redmatrix committed
144
				$errors = $this->load_database($db);
145

redmatrix's avatar
redmatrix committed
146
147
148
149
				if($errors)
					\App::$data['db_failed'] = $errors;
				else
					\App::$data['db_installed'] = true;
150

redmatrix's avatar
redmatrix committed
151
				return;
152
153
				// implied break;
			default:
redmatrix's avatar
redmatrix committed
154
155
156
				break;
		}
	}
157

redmatrix's avatar
redmatrix committed
158
159
160
161
162
163
164
	/**
	 * @brief Get output for the setup page.
	 *
	 * Depending on the state we are currently in it returns different content.
	 *
	 * @return string parsed HTML output
	 */
165

166
	function get() {
167

redmatrix's avatar
redmatrix committed
168
169
170
		$o = '';
		$wizard_status = '';
		$install_title = t('$Projectname Server - Setup');
171

redmatrix's avatar
redmatrix committed
172
173
174
175
176
177
178
179
180
181
		if(x(\App::$data, 'db_conn_failed')) {
			$this->install_wizard_pass = 2;
			$wizard_status =  t('Could not connect to database.');
		}
		if(x(\App::$data, 'url_fail')) {
			$this->install_wizard_pass = 3;
			$wizard_status =  t('Could not connect to specified site URL. Possible SSL certificate or DNS issue.');
			if(\App::$data['url_error'])
				$wizard_status .= ' ' . \App::$data['url_error'];
		}
182

redmatrix's avatar
redmatrix committed
183
184
185
186
187
188
		if(x(\App::$data, 'db_create_failed')) {
			$this->install_wizard_pass = 2;
			$wizard_status =  t('Could not create table.');
		}
		$db_return_text = '';
		if(x(\App::$data, 'db_installed')) {
Mario's avatar
Mario committed
189
190
191
			$pass = 'Installation succeeded!';
			$icon = 'check';
			$txt = t('Your site database has been installed.') . EOL;
redmatrix's avatar
redmatrix committed
192
193
194
			$db_return_text .= $txt;
		}
		if(x(\App::$data, 'db_failed')) {
Mario's avatar
Mario committed
195
196
			$pass = 'Database install failed!';
			$icon = 'exclamation-triangle';
redmatrix's avatar
redmatrix committed
197
198
			$txt = t('You may need to import the file "install/schema_xxx.sql" manually using a database client.') . EOL;
			$txt .= t('Please see the file "install/INSTALL.txt".') . EOL ."<hr>" ;
Mario's avatar
Mario committed
199
			$txt .= "<pre>" . \App::$data['db_failed'] . "</pre>". EOL ;
redmatrix's avatar
redmatrix committed
200
201
			$db_return_text .= $txt;
		}
redmatrix's avatar
redmatrix committed
202
		if(\DBA::$dba && \DBA::$dba->connected) {
Zot's avatar
Zot committed
203
			$r = q("SELECT COUNT(*) as total FROM account");
redmatrix's avatar
redmatrix committed
204
205
206
207
208
209
210
211
212
213
			if($r && count($r) && $r[0]['total']) {
				$tpl = get_markup_template('install.tpl');
				return replace_macros($tpl, array(
					'$title' => $install_title,
					'$pass' => '',
					'$status' => t('Permission denied.'),
					'$text' => '',
				));
			}
		}
214

redmatrix's avatar
redmatrix committed
215
216
217
		if(x(\App::$data, 'txt') && strlen(\App::$data['txt'])) {
			$db_return_text .= $this->manual_config($a);
		}
218

redmatrix's avatar
redmatrix committed
219
220
221
222
		if ($db_return_text != "") {
			$tpl = get_markup_template('install.tpl');
			return replace_macros($tpl, array(
				'$title' => $install_title,
Mario's avatar
Mario committed
223
224
225
226
				'$icon' => $icon,
				'$pass' => $pass,
				'$text' => $db_return_text,
				'$what_next' => $this->what_next()
redmatrix's avatar
redmatrix committed
227
228
			));
		}
229

redmatrix's avatar
redmatrix committed
230
231
		switch ($this->install_wizard_pass){
			case 1: { // System check
232

redmatrix's avatar
redmatrix committed
233
				$checks = array();
234

redmatrix's avatar
redmatrix committed
235
				$this->check_funcs($checks);
236

redmatrix's avatar
redmatrix committed
237
				$this->check_htconfig($checks);
238

redmatrix's avatar
redmatrix committed
239
				$this->check_store($checks);
240

redmatrix's avatar
redmatrix committed
241
				$this->check_smarty3($checks);
242

redmatrix's avatar
redmatrix committed
243
				$this->check_keys($checks);
244

redmatrix's avatar
redmatrix committed
245
246
				if (x($_POST, 'phpath'))
					$phpath = notags(trim($_POST['phpath']));
247

redmatrix's avatar
redmatrix committed
248
				$this->check_php($phpath, $checks);
249

redmatrix's avatar
redmatrix committed
250
				$this->check_phpconfig($checks);
251

redmatrix's avatar
redmatrix committed
252
				$this->check_htaccess($checks);
253

redmatrix's avatar
redmatrix committed
254
				$checkspassed = array_reduce($checks, "self::check_passed", true);
255

redmatrix's avatar
redmatrix committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
				$tpl = get_markup_template('install_checks.tpl');
				$o .= replace_macros($tpl, array(
					'$title' => $install_title,
					'$pass' => t('System check'),
					'$checks' => $checks,
					'$passed' => $checkspassed,
					'$see_install' => t('Please see the file "install/INSTALL.txt".'),
					'$next' => t('Next'),
					'$reload' => t('Check again'),
					'$phpath' => $phpath,
					'$baseurl' => z_root(),
				));
				return $o;
			}; break;
270

redmatrix's avatar
redmatrix committed
271
			case 2: { // Database config
272

273
274
275
276
277
278
279
280
281
				$dbhost = ((x($_POST,'dbhost')) ? trim($_POST['dbhost']) : '127.0.0.1');
				$dbuser = trim($_POST['dbuser']);
				$dbport = intval(trim($_POST['dbport']));
				$dbpass = trim($_POST['dbpass']);
				$dbdata = trim($_POST['dbdata']);
				$dbtype = intval(trim($_POST['dbtype']));
				$phpath = trim($_POST['phpath']);
				$adminmail = trim($_POST['adminmail']);
				$siteurl = trim($_POST['siteurl']);
282

redmatrix's avatar
redmatrix committed
283
284
285
286
287
288
289
				$tpl = get_markup_template('install_db.tpl');
				$o .= replace_macros($tpl, array(
					'$title' => $install_title,
					'$pass' => t('Database connection'),
					'$info_01' => t('In order to install $Projectname we need to know how to connect to your database.'),
					'$info_02' => t('Please contact your hosting provider or site administrator if you have questions about these settings.'),
					'$info_03' => t('The database you specify below should already exist. If it does not, please create it before continuing.'),
290

redmatrix's avatar
redmatrix committed
291
					'$status' => $wizard_status,
292

redmatrix's avatar
redmatrix committed
293
294
295
296
297
298
					'$dbhost' => array('dbhost', t('Database Server Name'), $dbhost, t('Default is 127.0.0.1')),
					'$dbport' => array('dbport', t('Database Port'), $dbport, t('Communication port number - use 0 for default')),
					'$dbuser' => array('dbuser', t('Database Login Name'), $dbuser, ''),
					'$dbpass' => array('dbpass', t('Database Login Password'), $dbpass, ''),
					'$dbdata' => array('dbdata', t('Database Name'), $dbdata, ''),
					'$dbtype' => array('dbtype', t('Database Type'), $dbtype, '', array( 0=>'MySQL', 1=>'PostgreSQL' )),
299

redmatrix's avatar
redmatrix committed
300
301
302
					'$adminmail' => array('adminmail', t('Site administrator email address'), $adminmail, t('Your account email address must match this in order to use the web admin panel.')),
					'$siteurl' => array('siteurl', t('Website URL'), z_root(), t('Please use SSL (https) URL if available.')),
					'$lbl_10' => t('Please select a default timezone for your website'),
303

redmatrix's avatar
redmatrix committed
304
					'$baseurl' => z_root(),
305

redmatrix's avatar
redmatrix committed
306
					'$phpath' => $phpath,
307

redmatrix's avatar
redmatrix committed
308
309
310
311
312
313
					'$submit' => t('Submit'),
				));
				return $o;
			}; break;
			case 3: { // Site settings
				require_once('include/datetime.php');
314
315
316
317
318
319
320
				$dbhost = ((x($_POST,'dbhost')) ? trim($_POST['dbhost']) : '127.0.0.1');
				$dbport = intval(trim($_POST['dbuser']));
				$dbuser = trim($_POST['dbuser']);
				$dbpass = trim($_POST['dbpass']);
				$dbdata = trim($_POST['dbdata']);
				$dbtype = intval(trim($_POST['dbtype']));
				$phpath = trim($_POST['phpath']);
321

322
323
				$adminmail = trim($_POST['adminmail']);
				$siteurl = trim($_POST['siteurl']);
redmatrix's avatar
redmatrix committed
324
				$timezone = ((x($_POST,'timezone')) ? ($_POST['timezone']) : 'America/Los_Angeles');
325

326

redmatrix's avatar
redmatrix committed
327
328
329
330
331
				$tpl = get_markup_template('install_settings.tpl');
				$o .= replace_macros($tpl, array(
					'$title' => $install_title,
					'$pass' => t('Site settings'),
					'$status' => $wizard_status,
332

redmatrix's avatar
redmatrix committed
333
334
335
336
337
338
339
					'$dbhost' => $dbhost,
					'$dbport' => $dbport,
					'$dbuser' => $dbuser,
					'$dbpass' => $dbpass,
					'$dbdata' => $dbdata,
					'$phpath' => $phpath,
					'$dbtype' => $dbtype,
340

redmatrix's avatar
redmatrix committed
341
					'$adminmail' => array('adminmail', t('Site administrator email address'), $adminmail, t('Your account email address must match this in order to use the web admin panel.')),
342

redmatrix's avatar
redmatrix committed
343
					'$siteurl' => array('siteurl', t('Website URL'), z_root(), t('Please use SSL (https) URL if available.')),
344

redmatrix's avatar
redmatrix committed
345
					'$timezone' => array('timezone', t('Please select a default timezone for your website'), $timezone, '', get_timezones()),
346

redmatrix's avatar
redmatrix committed
347
					'$baseurl' => z_root(),
348

redmatrix's avatar
redmatrix committed
349
350
351
352
353
354
					'$submit' => t('Submit'),
				));
				return $o;
			}; break;
		}
	}
355

redmatrix's avatar
redmatrix committed
356
357
358
359
360
361
362
	/**
	 * @brief Add a check result to the array for output.
	 *
	 * @param[in,out] array &$checks array passed to template
	 * @param string $title a title for the check
	 * @param boolean $status
	 * @param boolean $required
363
	 * @param string $help optional help string
redmatrix's avatar
redmatrix committed
364
365
366
367
368
369
370
371
372
	 */
	function check_add(&$checks, $title, $status, $required, $help = '') {
		$checks[] = array(
			'title'    => $title,
			'status'   => $status,
			'required' => $required,
			'help'     => $help
		);
	}
373

redmatrix's avatar
redmatrix committed
374
375
376
377
378
379
380
381
	/**
	 * @brief Checks the PHP environment.
	 *
	 * @param[in,out] string &$phpath
	 * @param[out] array &$checks
	 */
	function check_php(&$phpath, &$checks) {
		$help = '';
382

383
384
		if(version_compare(PHP_VERSION, '5.5') < 0) {
			$help .= t('PHP version 5.5 or greater is required.');
385
386
387
			$this->check_add($checks, t('PHP version'), false, false, $help);
		}

redmatrix's avatar
redmatrix committed
388
389
		if (strlen($phpath)) {
			$passed = file_exists($phpath);
390
391
		}
		elseif(function_exists('shell_exec')) {
redmatrix's avatar
redmatrix committed
392
393
394
395
			if(is_windows())
				$phpath = trim(shell_exec('where php'));
			else
				$phpath = trim(shell_exec('which php'));
396

redmatrix's avatar
redmatrix committed
397
398
			$passed = strlen($phpath);
		}
399

redmatrix's avatar
redmatrix committed
400
401
402
		if(!$passed) {
			$help .= t('Could not find a command line version of PHP in the web server PATH.'). EOL;
			$help .= t('If you don\'t have a command line version of PHP installed on server, you will not be able to run background polling via cron.') . EOL;
Mario's avatar
Mario committed
403
			$help .= EOL;
redmatrix's avatar
redmatrix committed
404
405
406
407
408
409
			$tpl = get_markup_template('field_input.tpl');
			$help .= replace_macros($tpl, array(
				'$field' => array('phpath', t('PHP executable path'), $phpath, t('Enter full path to php executable. You can leave this blank to continue the installation.')),
			));
			$phpath = '';
		}
410

redmatrix's avatar
redmatrix committed
411
		$this->check_add($checks, t('Command line PHP').($passed?" (<tt>$phpath</tt>)":""), $passed, false, $help);
412

redmatrix's avatar
redmatrix committed
413
414
415
416
		if($passed) {
			$str = autoname(8);
			$cmd = "$phpath install/testargs.php $str";
			$help = '';
417
418
419
420

			if(function_exists('shell_exec'))
				$result = trim(shell_exec($cmd));
			else
Zot's avatar
Zot committed
421
				$help .= t('Unable to check command line PHP, as shell_exec() is disabled. This is required.') . EOL;
422
			$passed2 = (($result == $str) ? true : false);
redmatrix's avatar
redmatrix committed
423
424
425
426
			if(!$passed2) {
				$help .= t('The command line version of PHP on your system does not have "register_argc_argv" enabled.'). EOL;
				$help .= t('This is required for message delivery to work.');
			}
427

redmatrix's avatar
redmatrix committed
428
429
430
			$this->check_add($checks, t('PHP register_argc_argv'), $passed, true, $help);
		}
	}
431

redmatrix's avatar
redmatrix committed
432
433
434
435
436
437
438
439
440
441
	/**
	 * @brief Some PHP configuration checks.
	 *
	 * @todo Change how we display such informational text. Add more description
	 *       how to change them.
	 *
	 * @param[out] array &$checks
	 */
	function check_phpconfig(&$checks) {
		require_once 'include/environment.php';
442

redmatrix's avatar
redmatrix committed
443
		$help = '';
444

redmatrix's avatar
redmatrix committed
445
446
447
448
449
450
		$result = getPhpiniUploadLimits();
		$help = sprintf(t('Your max allowed total upload size is set to %s. Maximum size of one file to upload is set to %s. You are allowed to upload up to %d files at once.'),
				userReadableSize($result['post_max_size']),
				userReadableSize($result['max_upload_filesize']),
				$result['max_file_uploads']
				);
Mario's avatar
Mario committed
451
		$help .= '<br><br>' . t('You can adjust these settings in the server php.ini file.');
452

redmatrix's avatar
redmatrix committed
453
454
		$this->check_add($checks, t('PHP upload limits'), true, false, $help);
	}
455

redmatrix's avatar
redmatrix committed
456
457
458
459
460
461
462
463
	/**
	 * @brief Check if the openssl implementation can generate keys.
	 *
	 * @param[out] array $checks
	 */
	function check_keys(&$checks) {
		$help = '';
		$res = false;
464

redmatrix's avatar
redmatrix committed
465
466
467
468
469
470
471
		if (function_exists('openssl_pkey_new')) {
			$res = openssl_pkey_new(array(
					'digest_alg' => 'sha1',
					'private_key_bits' => 4096,
					'encrypt_key' => false)
			);
		}
472

redmatrix's avatar
redmatrix committed
473
		// Get private key
474

redmatrix's avatar
redmatrix committed
475
476
477
478
		if (! $res) {
			$help .= t('Error: the "openssl_pkey_new" function on this system is not able to generate encryption keys'). EOL;
			$help .= t('If running under Windows, please see "http://www.php.net/manual/en/openssl.installation.php".');
		}
479

redmatrix's avatar
redmatrix committed
480
481
		$this->check_add($checks, t('Generate encryption keys'), $res, true, $help);
	}
482

redmatrix's avatar
redmatrix committed
483
484
485
486
487
488
489
	/**
	 * @brief Check for some PHP functions and modules.
	 *
	 * @param[in,out] array &$checks
	 */
	function check_funcs(&$checks) {
		$ck_funcs = array();
490

491
492
493
494
495
		$disabled = explode(',',ini_get('disable_functions'));
		if($disabled)
			array_walk($disabled,'array_trim');


redmatrix's avatar
redmatrix committed
496
497
498
499
		// add check metadata, the real check is done bit later and return values set
		$this->check_add($ck_funcs, t('libCurl PHP module'), true, true);
		$this->check_add($ck_funcs, t('GD graphics PHP module'), true, true);
		$this->check_add($ck_funcs, t('OpenSSL PHP module'), true, true);
500
		$this->check_add($ck_funcs, t('PDO database PHP module'), true, true);
redmatrix's avatar
redmatrix committed
501
502
		$this->check_add($ck_funcs, t('mb_string PHP module'), true, true);
		$this->check_add($ck_funcs, t('xml PHP module'), true, true);
Mario's avatar
Mario committed
503
		$this->check_add($ck_funcs, t('zip PHP module'), true, true);
504

redmatrix's avatar
redmatrix committed
505
506
507
508
509
510
511
		if(function_exists('apache_get_modules')){
			if (! in_array('mod_rewrite', apache_get_modules())) {
				$this->check_add($ck_funcs, t('Apache mod_rewrite module'), false, true, t('Error: Apache webserver mod-rewrite module is required but not installed.'));
			} else {
				$this->check_add($ck_funcs, t('Apache mod_rewrite module'), true, true);
			}
		}
512
		if((! function_exists('exec')) || in_array('exec',$disabled)) {
513
514
515
516
517
			$this->check_add($ck_funcs, t('exec'), false, true, t('Error: exec is required but is either not installed or has been disabled in php.ini'));
		}
		else {
			$this->check_add($ck_funcs, t('exec'), true, true);
		}
518
		if((! function_exists('shell_exec')) || in_array('shell_exec',$disabled)) {
519
520
521
522
523
			$this->check_add($ck_funcs, t('shell_exec'), false, true, t('Error: shell_exec is required but is either not installed or has been disabled in php.ini'));
		}
		else {
			$this->check_add($ck_funcs, t('shell_exec'), true, true);
		}
524

redmatrix's avatar
redmatrix committed
525
526
527
528
529
530
531
532
533
534
535
536
		if(! function_exists('curl_init')) {
			$ck_funcs[0]['status'] = false;
			$ck_funcs[0]['help'] = t('Error: libCURL PHP module required but not installed.');
		}
		if(! function_exists('imagecreatefromjpeg')) {
			$ck_funcs[1]['status'] = false;
			$ck_funcs[1]['help'] = t('Error: GD graphics PHP module with JPEG support required but not installed.');
		}
		if(! function_exists('openssl_public_encrypt')) {
			$ck_funcs[2]['status'] = false;
			$ck_funcs[2]['help'] = t('Error: openssl PHP module required but not installed.');
		}
537
		if(! class_exists('PDO')) {
redmatrix's avatar
redmatrix committed
538
			$ck_funcs[3]['status'] = false;
539
			$ck_funcs[3]['help'] = t('Error: PDO database PHP module required but not installed.');
redmatrix's avatar
redmatrix committed
540
541
542
543
544
545
		}
		if(! function_exists('mb_strlen')) {
			$ck_funcs[4]['status'] = false;
			$ck_funcs[4]['help'] = t('Error: mb_string PHP module required but not installed.');
		}
		if(! extension_loaded('xml')) {
Mario's avatar
Mario committed
546
547
548
549
			$ck_funcs[5]['status'] = false;
			$ck_funcs[5]['help'] = t('Error: xml PHP module required for DAV but not installed.');
		}
		if(! extension_loaded('zip')) {
redmatrix's avatar
redmatrix committed
550
			$ck_funcs[6]['status'] = false;
Mario's avatar
Mario committed
551
			$ck_funcs[6]['help'] = t('Error: zip PHP module required but not installed.');
redmatrix's avatar
redmatrix committed
552
		}
553

redmatrix's avatar
redmatrix committed
554
555
		$checks = array_merge($checks, $ck_funcs);
	}
556

redmatrix's avatar
redmatrix committed
557
558
559
560
561
562
563
564
	/**
	 * @brief Check for .htconfig requirements.
	 *
	 * @param[out] array &$checks
	 */
	function check_htconfig(&$checks) {
		$status = true;
		$help = '';
565

redmatrix's avatar
redmatrix committed
566
567
568
569
570
571
572
573
		if( (file_exists('.htconfig.php') && !is_writable('.htconfig.php')) ||
			(!file_exists('.htconfig.php') && !is_writable('.')) ) {
				$status = false;
				$help = t('The web installer needs to be able to create a file called ".htconfig.php" in the top folder of your web server and it is unable to do so.') .EOL;
				$help .= t('This is most often a permission setting, as the web server may not be able to write files in your folder - even if you can.').EOL;
				$help .= t('At the end of this procedure, we will give you a text to save in a file named .htconfig.php in your Red top folder.').EOL;
				$help .= t('You can alternatively skip this procedure and perform a manual installation. Please see the file "install/INSTALL.txt" for instructions.').EOL;
		}
574

redmatrix's avatar
redmatrix committed
575
576
		$this->check_add($checks, t('.htconfig.php is writable'), $status, false, $help);
	}
577

redmatrix's avatar
redmatrix committed
578
579
580
581
582
583
584
585
	/**
	 * @brief Checks for our templating engine Smarty3 requirements.
	 *
	 * @param[out] array &$checks
	 */
	function check_smarty3(&$checks) {
		$status = true;
		$help = '';
586

redmatrix's avatar
redmatrix committed
587
588
		if(! is_writable(TEMPLATE_BUILD_PATH) ) {
			$status = false;
589
			$help = t('This software uses the Smarty3 template engine to render its web views. Smarty3 compiles templates to PHP to speed up rendering.') .EOL;
redmatrix's avatar
redmatrix committed
590
			$help .= sprintf( t('In order to store these compiled templates, the web server needs to have write access to the directory %s under the top level web folder.'), TEMPLATE_BUILD_PATH) . EOL;
redmatrix's avatar
redmatrix committed
591
592
593
			$help .= t('Please ensure that the user that your web server runs as (e.g. www-data) has write access to this folder.').EOL;
			$help .= sprintf( t('Note: as a security measure, you should give the web server write access to %s only--not the template files (.tpl) that it contains.'), TEMPLATE_BUILD_PATH) . EOL;
		}
594

redmatrix's avatar
redmatrix committed
595
596
		$this->check_add($checks, sprintf( t('%s is writable'), TEMPLATE_BUILD_PATH), $status, true, $help);
	}
597

redmatrix's avatar
redmatrix committed
598
599
600
601
602
603
604
605
	/**
	 * @brief Check for store directory.
	 *
	 * @param[out] array &$checks
	 */
	function check_store(&$checks) {
		$status = true;
		$help = '';
606

redmatrix's avatar
redmatrix committed
607
		@os_mkdir(TEMPLATE_BUILD_PATH, STORAGE_DEFAULT_PERMISSIONS, true);
608

redmatrix's avatar
redmatrix committed
609
610
		if(! is_writable('store')) {
			$status = false;
611
			$help = t('This software uses the store directory to save uploaded files. The web server needs to have write access to the store directory under the top level web folder') . EOL;
redmatrix's avatar
redmatrix committed
612
613
			$help .= t('Please ensure that the user that your web server runs as (e.g. www-data) has write access to this folder.').EOL;
		}
614

redmatrix's avatar
redmatrix committed
615
616
		$this->check_add($checks, t('store is writable'), $status, true, $help);
	}
617

redmatrix's avatar
redmatrix committed
618
619
620
621
622
623
624
625
626
	/**
	 * @brief Check URL rewrite und SSL certificate.
	 *
	 * @param[out] array &$checks
	 */
	function check_htaccess(&$checks) {
		$status = true;
		$help = '';
		$ssl_error = false;
627

redmatrix's avatar
redmatrix committed
628
		$url = z_root() . '/setup/testrewrite';
629

redmatrix's avatar
redmatrix committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
		if (function_exists('curl_init')){
			$test = z_fetch_url($url);
			if(! $test['success']) {
				if(strstr($url,'https://')) {
					$test = z_fetch_url($url,false,0,array('novalidate' => true));
					if($test['success']) {
						$ssl_error = true;
					}
				}
				else {
					$test = z_fetch_url(str_replace('http://','https://',$url),false,0,array('novalidate' => true));
					if($test['success']) {
						$ssl_error = true;
					}
				}
645

redmatrix's avatar
redmatrix committed
646
647
648
649
650
651
652
				if($ssl_error) {
					$help = t('SSL certificate cannot be validated. Fix certificate or disable https access to this site.') . EOL;
					$help .= t('If you have https access to your website or allow connections to TCP port 443 (the https: port), you MUST use a browser-valid certificate. You MUST NOT use self-signed certificates!') . EOL;
					$help .= t('This restriction is incorporated because public posts from you may for example contain references to images on your own hub.') . EOL;
					$help .= t('If your certificate is not recognized, members of other sites (who may themselves have valid certificates) will get a warning message on their own site complaining about security issues.') . EOL;
					$help .= t('This can cause usability issues elsewhere (not just on your own site) so we must insist on this requirement.') .EOL;
					$help .= t('Providers are available that issue free certificates which are browser-valid.'). EOL;
653
654
655

					$help .= t('If you are confident that the certificate is valid and signed by a trusted authority, check to see if you have failed to install an intermediate cert. These are not normally required by browsers, but are required for server-to-server communications.') . EOL;

redmatrix's avatar
redmatrix committed
656
657
658
					$this->check_add($checks, t('SSL certificate validation'), false, true, $help);
				}
			}
659

redmatrix's avatar
redmatrix committed
660
661
662
663
			if ((! $test['success']) || ($test['body'] != "ok")) {
				$status = false;
				$help = t('Url rewrite in .htaccess is not working. Check your server configuration.'.'Test: '.var_export($test,true));
			}
664

redmatrix's avatar
redmatrix committed
665
666
667
668
669
			$this->check_add($checks, t('Url rewrite is working'), $status, true, $help);
		} else {
			// cannot check modrewrite if libcurl is not installed
		}
	}
670
671
672
673
674
675
676

	/**
	 * @brief
	 *
	 * @param App &$a
	 * @return string with paresed HTML
	 */
redmatrix's avatar
redmatrix committed
677
678
679
680
	function manual_config(&$a) {
		$data = htmlspecialchars(\App::$data['txt'], ENT_COMPAT, 'UTF-8');
		$o = t('The database configuration file ".htconfig.php" could not be written. Please use the enclosed text to create a configuration file in your web server root.');
		$o .= "<textarea rows=\"24\" cols=\"80\" >$data</textarea>";
681

redmatrix's avatar
redmatrix committed
682
683
		return $o;
	}
684

redmatrix's avatar
redmatrix committed
685
686
687
688
689
690
691
692
	function load_database_rem($v, $i){
		$l = trim($i);
		if (strlen($l)>1 && ($l[0]=="-" || ($l[0]=="/" && $l[1]=="*"))){
			return $v;
		} else  {
			return $v."\n".$i;
		}
	}
693
694


redmatrix's avatar
redmatrix committed
695
	function load_database($db) {
redmatrix's avatar
redmatrix committed
696
		$str = file_get_contents(\DBA::$dba->get_install_script());
697
		$arr = explode(';', $str);
redmatrix's avatar
redmatrix committed
698
699
700
		$errors = false;
		foreach($arr as $a) {
			if(strlen(trim($a))) {
redmatrix's avatar
redmatrix committed
701
				$r = dbq(trim($a));
redmatrix's avatar
redmatrix committed
702
703
704
705
706
				if(! $r) {
					$errors .=  t('Errors encountered creating database tables.') . $a . EOL;
				}
			}
		}
707

redmatrix's avatar
redmatrix committed
708
709
		return $errors;
	}
710
711
712
713
714
715

	/**
	 * @brief
	 *
	 * @return string with parsed HTML
	 */
redmatrix's avatar
redmatrix committed
716
717
718
	function what_next() {
		// install the standard theme
		set_config('system', 'allowed_themes', 'redbasic');
719

redmatrix's avatar
redmatrix committed
720
721
722
723
		// Set a lenient list of ciphers if using openssl. Other ssl engines
		// (e.g. NSS used in RedHat) require different syntax, so hopefully
		// the default curl cipher list will work for most sites. If not,
		// this can set via config. Many distros are now disabling RC4,
724
		// but many existing sites still use it and are unable to change it.
redmatrix's avatar
redmatrix committed
725
726
727
728
		// We do not use SSL for encryption, only to protect session cookies.
		// z_fetch_url() is also used to import shared links and other content
		// so in theory most any cipher could show up and we should do our best
		// to make the content available rather than tell folks that there's a
729
730
731
		// weird SSL error which they can't do anything about. This does not affect
		// the SSL server, but is only a client negotiation to find something workable.
		// Hence it will not make your system susceptible to POODL or other nasties.
732

redmatrix's avatar
redmatrix committed
733
734
735
		$x = curl_version();
		if(stristr($x['ssl_version'],'openssl'))
			set_config('system','curl_ssl_ciphers','ALL:!eNULL');
736

redmatrix's avatar
redmatrix committed
737
		// Create a system channel
738
		require_once ('include/channel.php');
redmatrix's avatar
redmatrix committed
739
		create_sys_channel();
740

redmatrix's avatar
redmatrix committed
741
742
		$baseurl = z_root();
		return
Mario's avatar
Mario committed
743
744
			t('<h1>What next?</h1>')
			."<div class=\"alert alert-info\">".t('IMPORTANT: You will need to [manually] setup a scheduled task for the poller.').EOL
redmatrix's avatar
redmatrix committed
745
			.t('Please see the file "install/INSTALL.txt".')
Mario's avatar
Mario committed
746
			."</div><div>"
redmatrix's avatar
redmatrix committed
747
			.t("Go to your new hub <a href='$baseurl/register'>registration page</a> and register as new member. Remember to use the same email you have entered as administrator email. This will allow you to enter the site admin panel.")
Mario's avatar
Mario committed
748
			."</div>";
redmatrix's avatar
redmatrix committed
749
750
	}

751
752
753
754
755
756
757
	/**
	 * @brief
	 *
	 * @param unknown $v
	 * @param array $c
	 * @return array
	 */
redmatrix's avatar
redmatrix committed
758
759
760
	static private function check_passed($v, $c) {
		if ($c['required'])
			$v = $v && $c['status'];
761

redmatrix's avatar
redmatrix committed
762
763
764
765
		return $v;
	}

}