<?php
// @codingStandardsIgnoreFile
$language = 'en';
if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) && preg_match('~^de-[A-Z]+,de~', $_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
	$language = 'de';
}

if (isset($_GET['language']) && in_array($_GET['language'], ['de', 'en'])) {
	$language = $_GET['language'];
}

?><!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	
	<title>WSC System Requirements Test</title>
	
	<style>
		html {
			background-color: #2D2D2D;
			box-sizing: border-box;
			color: #c0c0c0;
			font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol";
			font-size: 14px;
			line-height: 1.5;
		}
		
		*,
		*::before,
		*::after {
			box-sizing: inherit;
			min-width: 0;
		}
		
		a {
			color: inherit;
		}
		
		.layout-boundary {
			margin: 50px auto;
			max-width: 980px;
		}
		
		main {
			background-color: #3D3D3D;
			border-radius: 3px;
			padding: 40px 20px;
		}
		
		header {
			align-items: flex-end;
			display: flex;
			justify-content: space-between;
			margin-bottom: 15px;
		}
		
		header > img {
			flex: 0 auto;
		}
		
		.language-switcher {
			flex: 0 auto;
			list-style-type: none;
			margin: 0;
			padding: 0;
		}
		
		.language-switcher > li {
			display: inline;
		}
		
		.language-switcher > li + li {
			border-left: 1px solid #444444;
			margin-left: 10px;
			padding-left: 10px;
		}
		
		.language {
			font-size: 18px;
		}
		
		.language.active {
			color: inherit;
			pointer-events: none;
			text-decoration: none;
		}
		
		.language:not(.active) {
			color: #fff;
		}
		
		footer {
			color: #9D9D9D;
			padding-top: 10px;
			text-align: right;
		}
		
		footer > a {
			color: inherit;
			text-decoration: none;
		}
		
		h2, h3 {
			margin: 0 0 10px 0;
			font-weight: 300;
			padding: 0;
		}
		
		h2:not(:first-child),
		h3:not(:first-child) {
			margin-top: 40px;
		}
		
		h2 {
			font-size: 32px;
		}
		
		h3 {
			font-size: 24px;
		}
		
		ul.system-requirements {
			padding: 0;
		}
		
		ul.system-requirements li {
			list-style: none;
			margin-bottom: 10px;
			padding: 0 20px;
		}
		ul.system-requirements li::before {
			font-family: Georgia, "Times New Roman", serif;
			margin-right: 10px;
		}
		
		li.success {
			color: #00c291;
		}
		li.success::before {
			content: '✔';
		}
		li.failure {
			color: #f08f84;
		}
		li.failure::before {
			content: '✘';
		}
		li.info {
			color: #63b0e3;
		}
		li.info::before {
			content: '✔';
		}
		
		p.success,
		p.failure {
			border-radius: 3px;
			color: #fff;
			padding: 10px 20px;
		}
		p.success::before,
		p.failure::before {
			font-family: Georgia, "Times New Roman", serif;
			margin-right: 10px;
		}
		
		p.success {
			background-color: #008563;
		}
		p.success::before {
			content: '✔';
		}
		
		p.failure {
			background-color: #de2f1b;
		}
		p.failure::before {
			content: '✘';
		}
		
		.button {
			background-color: #375a7f;
			border-radius: 3px;
			border-width: 0;
			color: #fff;
			cursor: pointer;
			font-family: inherit;
			font-size: 1rem;
			padding: 15px 30px;
			text-decoration: none;
			vertical-align: middle;
			text-transform: uppercase;
		}
		
		.button:hover {
			background-color: #2b4764;
		}
	</style>
</head>
<body>
<div class="layout-boundary">
	<header>
		<img src="data:image/png;base64, " style="height: 40px; width: 281px" alt="WoltLab Suite">
		<ul class="language-switcher">
			<li><a href="./test.php?language=de" class="language<?=($language === 'de' ? ' active' : '')?>">Deutsch</a></li>
			<li><a href="./test.php?language=en" class="language<?=($language === 'en' ? ' active' : '')?>">English</a></li>
		</ul>
	</header>
	
	<main>
<?php
const WSC_SRT_VERSION = '5.2.0';
$requiredExtensions = ['dom', 'json', 'hash', 'libxml', 'mbstring', 'pcre', 'pdo', 'pdo_mysql', 'zlib'];
$requiredPHPVersion = '7.0.22';
$phrases = [
	'php_requirements' => [
		'de' => 'PHP',
		'en' => 'PHP Requirements'
	],
	'php_version_success' => [
		'de' => 'PHP-Version %s',
		'en' => 'PHP version %s',
	],
	'php_version_failure' => [
		'de' => 'Gefundene PHP-Version %s ist unzureichend. PHP %s oder höher wird benötigt.',
		'en' => 'PHP version %s is insufficient. PHP %s or above is required.',
	],
	'php_extension_success' => [
		'de' => 'Erweiterung %s vorhanden',
		'en' => '%s extension loaded',
	],
	'php_extension_failure' => [
		'de' => 'Erweiterung %s fehlt',
		'en' => '%s extension missing',
	],
	'php_extension_gd_or_imagick_failure' => [
		'de' => 'Erweiterung für Bildverarbeitung (GD oder Imagick) fehlt',
		'en' => 'Extension for image processing (GD or Imagick) missing',
	],
	'php_sha256_success' => [
		'de' => 'Unterstützung für SHA-256-Hashfunktion vorhanden',
		'en' => 'Support for SHA-256 algorithm available',
	],
	'php_sha256_failure' => [
		'de' => 'Unterstützung für SHA-256-Hashfunktion fehlt',
		'en' => 'Support for SHA-256 algorithm missing',
	],
	'php_memory_limit_success' => [
		'de' => 'Arbeitsspeicher-Limit %s',
		'en' => '%s memory limit',
	],
	'php_memory_limit_failure' => [
		'de' => 'Arbeitsspeicher-Limit %s ist nicht ausreichend. 128M oder mehr wird benötigt.',
		'en' => 'Memory limit %s is too low. It needs to be set to 128M or more.',
	],
	'mysql_requirements' => [
		'de' => 'MySQL',
		'en' => 'MySQL Requirements'
	],
	'mysql_version' => [
		'de' => 'Bitte stellen Sie sicher, dass MySQL 5.5.35+/MySQL 8.0.14+  oder MariaDB 10.0.22+ mit InnoDB-Unterstützung vorhanden ist.',
		'en' => 'Please make sure that MySQL 5.5.35+/MySQL 8.0.14+ or MariaDB 10.0.22+, with InnoDB support is available.'
	],
	'result' => [
		'de' => 'Ergebnis',
		'en' => 'Summary'
	],
	'result_success' => [
		'de' => 'Alle Systemvoraussetzungen sind erfüllt. Sie können die Installation von WoltLab Suite beginnen.',
		'en' => 'Your system fulfills all of WoltLab Suite\'s system requirements. You are ready to install WoltLab Suite!'
	],
	'result_failure' => [
		'de' => 'Die Systemvoraussetzungen sind nicht erfüllt. Bitte beachten Sie die oben genannten Informationen. Wenden Sie sich ggf. an Ihren Webhoster oder Ihren Serveradministrator.',
		'en' => 'The system requirements are not met. Please note the above information and contact your web host or server administrator if necessary.'
	],
	'button_start_installation' => [
		'de' => 'Installation starten',
		'en' => 'Start Installation'
	]
];
function getPhrase($phrase, array $values = []) {
	global $language, $phrases;
	
	if (!isset($phrases[$phrase]) || !isset($phrases[$phrase][$language])) {
		return "[unknown:{$phrase}]";
	}
	
	return vsprintf($phrases[$phrase][$language], $values);
}
function checkPHPVersion() {
	global $requiredPHPVersion;
	
	$comparePhpVersion = preg_replace('/^(\d+\.\d+\.\d+).*$/', '\\1', phpversion());
	return version_compare($comparePhpVersion, $requiredPHPVersion) >= 0;
}
function checkMemoryLimit() {
	$memoryLimit = ini_get('memory_limit');
	
	// no limit
	if ($memoryLimit == -1) {
		return true;
	}
	
	// completely numeric, PHP assumes byte
	if (is_numeric($memoryLimit)) {
		$memoryLimit = $memoryLimit / 1024 / 1024;
		return ($memoryLimit >= 128);
	}
	
	// PHP supports 'K', 'M' and 'G' shorthand notation
	if (preg_match('~^(\d+)([KMG])$~', $memoryLimit, $matches)) {
		switch ($matches[2]) {
			case 'K':
				$memoryLimit = $matches[1] * 1024;
				return ($memoryLimit >= 128);
				break;
			
			case 'M':
				return ($matches[1] >= 128);
				break;
			
			case 'G':
				return ($matches[1] >= 1);
				break;
		}
	}
	
	return false;
}
function checkHashAlgorithms() {
	return extension_loaded('hash') && in_array('sha256', hash_algos());
}
function checkResult() {
	global $requiredExtensions;
	
	if (!checkPHPVersion() || !checkHashAlgorithms() || !checkMemoryLimit()) return false;
	
	foreach ($requiredExtensions as $extension) {
		if (!extension_loaded($extension)) return false;
	}
	
	if (!extension_loaded('gd') && !extension_loaded('imagick')) return false;
	
	return true;
}
function checkInstallFile() {
	return @file_exists('install.php');
}
?>
		<h2>WoltLab Suite System Requirements Test</h2>
		
		<h3><?=getPhrase('php_requirements')?></h3>
		
		<ul class="system-requirements">
			<?php if (checkPHPVersion()) { ?>
			<li class="success"><?=getPhrase('php_version_success', [phpversion()])?></li>
			<?php } else { ?>
			<li class="failure"><?=getPhrase('php_version_failure', [phpversion(), $requiredPHPVersion])?></li>
			<?php } ?>
			
			<?php foreach ($requiredExtensions as $extension) {
				if (extension_loaded($extension)) { ?>
				<li class="success"><?=getPhrase('php_extension_success', [$extension])?></li>
				<?php } else { ?>
				<li class="failure"><?=getPhrase('php_extension_failure', [$extension])?></li>
				<?php }
			} ?>
			
			<?php if (extension_loaded('imagick')) { ?>
				<li class="success"><?=getPhrase('php_extension_success', ['Imagick'])?></li>
			<?php } else if (extension_loaded('gd')) { ?>
				<li class="success"><?=getPhrase('php_extension_success', ['GD'])?></li>
			<?php } else { ?>	
				<li class="failure"><?=getPhrase('php_extension_gd_or_imagick_failure')?></li>
			<?php } ?>
			
			<?php if (checkHashAlgorithms()) { ?>
				<li class="success"><?=getPhrase('php_sha256_success')?></li>
			<?php } else { ?>
				<li class="failure"><?=getPhrase('php_sha256_failure')?></li>
			<?php } ?>
			
			<?php if (checkMemoryLimit()) { ?>
				<li class="success"><?=getPhrase('php_memory_limit_success', [ini_get('memory_limit')])?></li>
			<?php } else { ?>
				<li class="failure"><?=getPhrase('php_memory_limit_failure', [ini_get('memory_limit')])?></li>
			<?php } ?>
		</ul>
		
		<h3><?=getPhrase('mysql_requirements')?></h3>
		
		<ul class="system-requirements">
			<li class="info"><?=getPhrase('mysql_version')?></li>
		</ul>
		
		<h2><?=getPhrase('result')?></h2>
		
		<?php if (checkResult()) { ?>
			<p class="success"><?=getPhrase('result_success')?></p>
		<?php } else { ?>
			<p class="failure"><?=getPhrase('result_failure')?></p>
		<?php } ?>
		
		<?php if (checkInstallFile()) { ?>
			<p style="margin-top: 50px; text-align: center;"><a href="install.php" class="button"><?=getPhrase('button_start_installation')?></a></p>
		<?php } ?>
	</main>
	<footer>
		<a href="https://www.woltlab.com">WoltLab Suite System Requirements Test v<?=WSC_SRT_VERSION?></a>
	</footer>
</div>
</body>
</html>
