summaryrefslogtreecommitdiffstats
path: root/install.php
blob: 6cbbe91f506e5be99f786cfc99e3b0315bf5c8fb (plain) (blame)
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
<?php

/*
 * Modular installation system.
 *
 * Since most of slx-admin is now modularized, it might not make too much sense to have
 * static central SQL dumps of the database scheme. Tables usually belong to specific modules,
 * so they should only be created if the module is actually in use.
 * Thus, we have a modular install system where modules should create (and update) database
 * tables that belong to them.
 * The system should not be abused to modify tables of other modules by adding or changing
 * columns, which could lead to a maintenance nightmare over time.
 *
 * The install/update mechanism could also be used to setup other requirements, like directories,
 * permissions etc. Each module's install hook should report back the status of the module's
 * requirements. For examples see some modules like baseconfig, main, statistics, ...
 *
 * Warning: Be careful which classes/functionality of slx-admin you use in your scripts, since
 * they might depend on some tables that do not exist yet. ;)
 */

use JetBrains\PhpStorm\NoReturn;

if (PHP_INT_SIZE < 8) {
	echo "32bit platforms no longer supported\n";
	exit(1);
}

/**
 * Report back the update status to the browser/client and terminate execution.
 * This has to be called by an update module at some point to signal the result
 * of its execution.
 *
 * @param string $status one of the UPDATE_* status codes
 * @param string $message Human readable description of the status (optional)
 */
#[NoReturn]
function finalResponse(string $status, string $message = '')
{
	if (!DIRECT_MODE && AJAX) {
		echo json_encode(array('status' => $status, 'message' => $message));
	} else {
		echo 'STATUS=', $status, "\n";
		echo 'MESSAGE=', str_replace("\n", " -- ", $message);
	}
	exit;
}

define('UPDATE_DONE', 'UPDATE_DONE'); // Process completed successfully. This is a success return code.
define('UPDATE_NOOP', 'UPDATE_NOOP'); // Nothing had to be done, everything is up to date. This is also a success code.
define('UPDATE_RETRY', 'UPDATE_RETRY'); // Install/update process failed, but should be retried later.
define('UPDATE_FAILED', 'UPDATE_FAILED'); // Fatal error occurred, retry will not resolve the issue.

/**
 * Take the return value of a Database::exec() call and emit failure
 * if it's false.
 */
function handleUpdateResult($res)
{
	if ($res !== false)
		return;
	finalResponse(UPDATE_FAILED, Database::lastError());
}

/*
 * Helper functions for dealing with the database
 */

function tableHasColumn(string $table, string $column): bool
{
	return tableColumnType($table, $column) !== false;
}

/**
 * Get type of column, as reported by DESCRIBE <table>;
 */
function tableColumnType(string $table, string $column)
{
	return tableGetDescribeColumn($table, $column, 'Type');
}

function tableColumnKeyType($table, $column)
{
	return tableGetDescribeColumn($table, $column, 'Key');
}

/**
 * For internal use
 * @param string|string[] $column
 * @return string|false
 */
function tableGetDescribeColumn(string $table, $column, string $what)
{
	$table = preg_replace('/\W/', '', $table);
	$res = Database::simpleQuery("DESCRIBE `$table`", array(), true);
	if ($res !== false) {
		foreach ($res as $row) {
			if ((is_array($column) && in_array($row['Field'], $column)) || (is_string($column) && $row['Field'] === $column))
				return $row[$what];
		}
	}
	return false;
}

/**
 * Return name of index that spans all the columns given, in the same order.
 * Returns false if not found
 *
 * @param string[] $columns
 * @return false|string
 */
function tableGetIndex(string $table, array $columns)
{
	$table = preg_replace('/\W/', '', $table);
	$res = Database::simpleQuery("SHOW INDEX FROM `$table`", array(), true);
	if ($res !== false) {
		$matches = [];
		foreach ($res as $row) {
			$i = $row['Seq_in_index'] - 1;
			if (isset($columns[$i]) && $columns[$i] === $row['Column_name']) {
				if (!isset($matches[$row['Key_name']])) {
					$matches[$row['Key_name']] = 0;
				}
				$matches[$row['Key_name']]++;
			}
		}
		foreach ($matches as $key => $m) {
			if ($m === count($columns))
				return $key;
		}
	}
	return false;
}

function tableDropColumn(string $table, string $column): void
{
	$table = preg_replace('/\W/', '', $table);
	$column = preg_replace('/\W/', '', $column);
	$res = Database::simpleQuery("DESCRIBE `$table`", array(), true);
	if ($res !== false) {
		foreach ($res as $row) {
			if ((is_array($column) && in_array($row['Field'], $column)) || (is_string($column) && $row['Field'] === $column))
				Database::exec("ALTER TABLE `$table` DROP `{$row['Field']}`");
		}
	}
}

function tableExists(string $table): bool
{
	$res = Database::simpleQuery("SHOW TABLES", array(), true);
	while ($row = $res->fetch(PDO::FETCH_NUM)) {
		if ($row[0] === $table)
			return true;
	}
	return false;
}

function tableRename(string $old, string $new): bool
{
	return Database::simpleQuery("RENAME TABLE $old TO $new", []) !== false;
}


/**
 * Get all constraint details for given combo.
 *
 * @param string $table source table, being constrained
 * @param string $column source column
 * @param string $refTable referenced table, dictating the constraints
 * @param string $refColumn referenced column
 * @return false|string[] false == doesn't exist, assoc array otherwise
 */
function tableGetConstraints(string $table, string $column, string $refTable, string $refColumn)
{
	$db = 'openslx';
	if (defined('CONFIG_SQL_DB')) {
		$db = CONFIG_SQL_DB;
	} elseif (defined('CONFIG_SQL_DSN')) {
		if (preg_match('/dbname\s*=\s*([^;\s]+)\s*(;|$)/i', CONFIG_SQL_DSN, $out)) {
			$db = $out[1];
			define('CONFIG_SQL_DB', $db);
		}
	}
	return Database::queryFirst('SELECT b.CONSTRAINT_NAME, b.UPDATE_RULE, b.DELETE_RULE
			FROM information_schema.KEY_COLUMN_USAGE a
			INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS b ON (
			    a.CONSTRAINT_CATALOG = b.CONSTRAINT_CATALOG
			        AND a.CONSTRAINT_SCHEMA = b.CONSTRAINT_SCHEMA
			        AND a.CONSTRAINT_NAME = b.CONSTRAINT_NAME)
			WHERE a.TABLE_SCHEMA = :db AND a.TABLE_NAME = :table AND a.COLUMN_NAME = :column
			AND a.REFERENCED_TABLE_NAME = :refTable AND a.REFERENCED_COLUMN_NAME = :refColumn',
		compact('db', 'table', 'column', 'refTable', 'refColumn'));
}

/**
 * Add constraint to table if it doesn't exist already.
 * On failure, trigger finalResponse with error message.
 *
 * @param string $table table to add constraint to
 * @param string $column foreign key column of that table
 * @param string $refTable destination table
 * @param string $refColumn primary key column in destination table
 * @param string $actions "ON xxx ON yyy" string
 * @return string UPDATE_* result code
 */
function tableAddConstraint(string $table, string $column, string $refTable, string $refColumn, string $actions,
									 bool $ignoreError = false, $name = ''): string
{
	$test = tableExists($refTable) && tableHasColumn($refTable, $refColumn);
	if ($test === false) {
		// Most likely, destination table does not exist yet or isn't up-to-date
		return UPDATE_RETRY;
	}
	// TODO: Refactor function, make this two args
	$update = 'RESTRICT';
	$delete = 'RESTRICT';
	if (preg_match('/on\s+update\s+(RESTRICT|SET\s+NULL|CASCADE)/im', $actions, $out)) {
		$update = preg_replace('/\s+/m', ' ', strtoupper($out[1]));
	}
	if (preg_match('/on\s+delete\s+(RESTRICT|SET\s+NULL|CASCADE)/im', $actions, $out)) {
		$delete = preg_replace('/\s+/m', ' ', strtoupper($out[1]));
	}
	$test = tableGetConstraints($table, $column, $refTable, $refColumn);
	if ($test !== false) {
		// Exists, check if same
		if ($test['UPDATE_RULE'] === $update && $test['DELETE_RULE'] === $delete) {
			// Yep, nothing more to do here
			return UPDATE_NOOP;
		}
		error_log("$table, $column, $refTable, $refColumn, $actions");
		error_log("Have: {$test['UPDATE_RULE']} want: $update && Have: {$test['DELETE_RULE']} want: $delete");
		// Kill the old one
		tableDeleteConstraint($table, $test['CONSTRAINT_NAME']);
	}
	if ($delete === 'CASCADE') {
		// Deletes are cascaded, so make sure first that all rows get purged that would
		// violate the constraint
		Database::exec("DELETE `$table` FROM `$table`
				LEFT JOIN `$refTable` ON (`$table`.`$column` = `$refTable`.`$refColumn`)
				WHERE `$refTable`.`$refColumn` IS NULL");
	} elseif ($delete === 'SET NULL') {
		// Similar to above; SET NULL constraint, so do that for violating entries
		Database::exec("UPDATE `$table`
				LEFT JOIN `$refTable` ON (`$table`.`$column` = `$refTable`.`$refColumn`)
				SET `$table`.`$column` = NULL
				WHERE `$refTable`.`$refColumn` IS NULL");
	}
	// Need to create
	if (!empty($name)) {
		$name = "`$name`";
	}
	$ret = Database::exec("ALTER TABLE `$table` ADD CONSTRAINT $name FOREIGN KEY (`$column`)
			REFERENCES `$refTable` (`$refColumn`)
			ON DELETE $delete ON UPDATE $update");
	if ($ret === false) {
		if ($ignoreError) {
			return UPDATE_FAILED;
		}
		finalResponse(UPDATE_FAILED, "Cannot add constraint $table.$column -> $refTable.$refColumn: " . Database::lastError());
	}
	return UPDATE_DONE;
}

/**
 * Drop constraint from a table.
 *
 * @param string $table table name
 * @param string $constraint constraint name
 * @return bool success indicator
 */
function tableDeleteConstraint(string $table, string $constraint): bool
{
	return Database::exec("ALTER TABLE `$table` DROP FOREIGN KEY `$constraint`") !== false;
}

function tableCreate(string $table, string $structure, bool $fatalOnError = true): string
{
	if (tableExists($table)) {
		return UPDATE_NOOP;
	}
	$ret = Database::exec("CREATE TABLE IF NOT EXISTS `{$table}` ( {$structure} ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
	if ($ret !== false) {
		return UPDATE_DONE;
	}
	if ($fatalOnError) {
		finalResponse(UPDATE_FAILED, 'DB-Error: ' . Database::lastError());
	}
	return UPDATE_FAILED;
}

#[NoReturn]
function responseFromArray(array $array)
{
	if (in_array(UPDATE_FAILED, $array)) {
		finalResponse(UPDATE_FAILED, 'Update failed!');
	}
	if (in_array(UPDATE_RETRY, $array)) {
		finalResponse(UPDATE_RETRY, 'Temporary failure, will try again.');
	}
	if (in_array(UPDATE_DONE, $array)) {
		finalResponse(UPDATE_DONE, 'Tables created/updated successfully');
	}

	finalResponse(UPDATE_NOOP, 'Everything already up to date');
}

/*
 * Rest of install script....
 */

if (!isset($_SERVER['REMOTE_ADDR']) || isset($_REQUEST['direct'])) {
	define('DIRECT_MODE', true);
} else {
	define('DIRECT_MODE', false);
}

define('AJAX', ((isset($_REQUEST['async'])) || (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest')));

error_reporting(E_ALL);
chdir(dirname($_SERVER['SCRIPT_FILENAME']));

// Autoload classes from ./inc which adhere to naming scheme <lowercasename>.inc.php
spl_autoload_register(function ($class) {
	$file = 'inc/' . preg_replace('/[^a-z0-9]/', '', mb_strtolower($class)) . '.inc.php';
	if (!file_exists($file))
		return;
	require_once $file;
});

if (!is_readable('config.php')) {
	finalResponse(UPDATE_FAILED, 'config.php does not exist!');
}

require_once 'config.php';

if (CONFIG_SQL_PASS === '%MYSQL_OPENSLX_PASS%') {
	finalResponse(UPDATE_FAILED, 'mysql credentials not configured yet!');
}

// Explicitly connect to the database so it won't call Util::traceError() on failure
if (!Database::init(true)) {
	finalResponse(UPDATE_RETRY, 'Connecting to the database failed');
}

// Good to go so far

function hasUpdateScript(Module $module): bool
{
	return is_readable($module->getDir() . '/install.inc.php');
}

function runUpdateScript(Module $module): void
{
	require_once $module->getDir() . '/install.inc.php';
}

// Update collation/encoding etc
$charsetUpdate = '';
$COLLATION = 'utf8mb4_unicode_520_ci';
$res = Database::queryFirst("SELECT @@character_set_database, @@collation_database");
if ($res['@@character_set_database'] !== 'utf8mb4' || $res['@@collation_database'] !== $COLLATION) {
	if (!preg_match('/dbname=(\w+)/', CONFIG_SQL_DSN, $out)) {
		$charsetUpdate = 'Cannot update charset: DB Name unknown';
	} else {
		$db = $out[1];
		$columns = Database::simpleQuery("SELECT
				TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLUMN_TYPE, EXTRA, COLUMN_COMMENT
				FROM information_schema.COLUMNS
				WHERE TABLE_SCHEMA = :db AND COLLATION_NAME LIKE 'utf8%' AND COLLATION_NAME <> :collation",
			['db' => $db, 'collation' => $COLLATION]);
		$idx = 0;
		foreach ($columns as $c) {
			$idx++;
			$args = [];
			$str = $c['COLUMN_TYPE'] . ' CHARACTER SET utf8mb4 ' . $c['EXTRA'];
			if ($c['IS_NULLABLE'] === 'NO') {
				$str .= ' NOT NULL';
			}
			if (!($c['IS_NULLABLE'] === 'NO' && $c['COLUMN_DEFAULT'] === null)) {
				$str .= " DEFAULT :def_$idx";
				$args["def_$idx"] = $c['COLUMN_DEFAULT'];
			}
			if (!empty($c['COLUMN_COMMENT'])) {
				$str .= ' COMMENT :comment';
				$args['comment'] = $c['COLUMN_COMMENT'];
			}
			$str .= ' COLLATE ' . $COLLATION;
			$query = "ALTER TABLE `{$c['TABLE_NAME']}` MODIFY `{$c['COLUMN_NAME']}` $str";
			if (Database::exec($query, $args) === false) {
				$charsetUpdate .= "\n\n--------------------------\n" .
					"+++ {$c['TABLE_NAME']}.{$c['COLUMN_NAME']} failed: " . Database::lastError();
				$charsetUpdate .= "\n$query";
			}
		}
		if (empty($charsetUpdate) && Database::exec("ALTER DATABASE `$db` CHARACTER SET utf8mb4 COLLATE $COLLATION") === false) {
			$charsetUpdate .= "\nCannot update database charset or collation: " . Database::lastError();
		}
	}
} // End utf8 stuff

// Build dependency tree
Module::init();
$modules = Module::getEnabled();
if (empty($modules)) {
	finalResponse(UPDATE_NOOP, 'No active modules, nothing to do');
}

if (DIRECT_MODE) {
	//
	// Direct mode - plain
	$new = array();
	foreach ($modules as $entry) {
		if (hasUpdateScript($entry)) {
			$new[] = $entry;
		}
	}
	/* @var Module[] $new */
	$modules = $new;
	if (empty($modules)) {
		finalResponse(UPDATE_NOOP, 'No modules with install scripts, nothing to do');
	}
	// Get array where the key maps a module identifier to the next module object
	$assoc = array();
	$count = count($modules);
	for ($i = 0; $i < $count; ++$i) {
		$assoc[$modules[$i]->getIdentifier()] = $modules[($i + 1) % $count];
	}
	/* @var Module[] $assoc */

	if (!empty($argv[1])) {
		$last = $argv[1];
	} else {
		$last = Request::any('last', '', 'string');
	}
	if (!empty($last) && isset($assoc[$last])) {
		$module = $assoc[$last];
	}
	if (!isset($module)) {
		$module = $modules[0];
	}
	echo 'MODULE=', $module->getIdentifier(), "\n";
	runUpdateScript($module);
} else {
	//
	// Interactive web based mode
	$mod = Request::any('module', false, 'string');
	if ($mod !== false) {
		// Execute specific module
		$module = Module::get($mod, true);
		if ($module === false) {
			finalResponse(UPDATE_NOOP, 'Given module does not exist!');
		}
		if (!hasUpdateScript($module)) {
			finalResponse(UPDATE_NOOP, 'Given module has no install script');
		}
		runUpdateScript($module);
		finalResponse(UPDATE_DONE, 'Module did not report status; assuming OK');
	}
	// Show the page that shows status and triggers jobs
	echo <<<HERE
<!DOCTYPE html>
<html>
	<head>
		<title>Install/Update SLXadmin</title>
		<meta charset="utf-8"> 
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<style>
		body, html {
			color: #000;
			background: #fff;
		}
		table, tr, td {
			border: 1px solid #ccc;
			border-collapse: collapse;
			padding: 3px;
		}
		button {
			font-weight: bold;
			padding: 8px;
		}
		</style>
	</head>
	<body>
		<pre>$charsetUpdate</pre>
		<h1>Modules</h1>
		<button onclick="slxRunInstall()" class="install-btn">Install/Upgrade</button>
		<br>
		<br>
		<table>
			<tr><th>Module</th><th>Status</th></tr>
HERE;
	foreach ($modules as $module) {
		$id = $module->getIdentifier();
		echo "<tr><td class=\"id-col\">{$id}</td><td id=\"mod-{$id}\">Waiting...</td></tr>";
	}
	echo <<<HERE
		</table>
		<br><br>
		<button onclick="slxRunInstall()" class="install-btn">Install/Upgrade</button>
		<script src="script/jquery.js"></script>
		<script src="script/install.js"></script>
	</body>
</html>
HERE;

}