2022-01-11 18:54:32 +00:00
|
|
|
<?php
|
2023-03-29 22:45:42 +00:00
|
|
|
require '../../vendor/autoload.php';
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
use Illuminate\Encryption\Encrypter;
|
|
|
|
use Illuminate\Support\Str;
|
2022-01-11 11:45:36 +00:00
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
$required_extensions = ['openssl', 'gd', 'mysql', 'PDO', 'mbstring', 'tokenizer', 'bcmath', 'xml', 'curl', 'zip', 'intl'];
|
2022-01-11 11:45:36 +00:00
|
|
|
|
2022-01-11 13:09:20 +00:00
|
|
|
$requirements = [
|
2023-01-11 15:44:44 +00:00
|
|
|
'minPhp' => '8.1',
|
2023-01-17 13:28:13 +00:00
|
|
|
'maxPhp' => '8.2', // This version is not supported
|
2023-01-05 17:01:42 +00:00
|
|
|
'mysql' => '5.7.22',
|
2022-01-11 13:09:20 +00:00
|
|
|
];
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Check if the minimum PHP version is present
|
|
|
|
* @return string 'OK' on success and 'not OK' on failure.
|
|
|
|
*/
|
|
|
|
function checkPhpVersion(): string
|
2022-01-11 18:54:32 +00:00
|
|
|
{
|
|
|
|
global $requirements;
|
2023-01-11 15:44:44 +00:00
|
|
|
if (version_compare(phpversion(), $requirements['minPhp'], '>=') && version_compare(phpversion(), $requirements['maxPhp'], '<=')) {
|
2023-01-05 17:01:42 +00:00
|
|
|
return 'OK';
|
2022-01-11 18:54:32 +00:00
|
|
|
}
|
2023-01-05 17:01:42 +00:00
|
|
|
|
|
|
|
return 'not OK';
|
2022-01-11 11:45:36 +00:00
|
|
|
}
|
2023-03-29 22:30:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the environment file is writable
|
|
|
|
* @return bool Returns true on writable and false on not writable.
|
|
|
|
*/
|
|
|
|
function checkWriteable(): bool
|
2022-01-26 14:17:51 +00:00
|
|
|
{
|
2023-01-05 17:01:42 +00:00
|
|
|
return is_writable('../../.env');
|
2022-01-26 14:17:51 +00:00
|
|
|
}
|
2023-03-29 22:30:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the server runs using HTTPS
|
|
|
|
* @return bool Returns true on HTTPS or false on HTTP.
|
|
|
|
*/
|
|
|
|
function checkHTTPS(): bool
|
2022-01-26 12:24:03 +00:00
|
|
|
{
|
2023-03-29 23:11:18 +00:00
|
|
|
return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
|
2022-01-26 12:24:03 +00:00
|
|
|
|| $_SERVER['SERVER_PORT'] == 443;
|
|
|
|
}
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Check if MySQL is installed and runs the correct version using a shell command
|
|
|
|
* @return mixed|string 'OK' if required version is met, returns MySQL version if not met.
|
|
|
|
*/
|
|
|
|
function getMySQLVersion(): mixed
|
2022-01-11 18:54:32 +00:00
|
|
|
{
|
|
|
|
global $requirements;
|
2022-01-11 11:45:36 +00:00
|
|
|
|
2023-03-29 21:30:51 +00:00
|
|
|
$output = shell_exec('mysql -V') ?? '';
|
2022-01-11 18:54:32 +00:00
|
|
|
preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
|
2022-01-11 11:45:36 +00:00
|
|
|
|
2023-01-05 17:01:42 +00:00
|
|
|
$versionoutput = $version[0] ?? '0';
|
2022-01-11 11:45:36 +00:00
|
|
|
|
2023-01-05 17:01:42 +00:00
|
|
|
return intval($versionoutput) > intval($requirements['mysql']) ? 'OK' : $versionoutput;
|
2022-01-11 13:09:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Check if zip is installed using a shell command
|
|
|
|
* @return string 'OK' on success and 'not OK' on failure.
|
|
|
|
*/
|
|
|
|
function getZipVersion(): string
|
2022-01-11 18:54:32 +00:00
|
|
|
{
|
2023-03-29 21:30:51 +00:00
|
|
|
$output = shell_exec('zip -v') ?? '';
|
2022-01-11 18:54:32 +00:00
|
|
|
preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2022-01-11 18:54:32 +00:00
|
|
|
$versionoutput = $version[0] ?? 0;
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2023-01-05 17:01:42 +00:00
|
|
|
return $versionoutput != 0 ? 'OK' : 'not OK';
|
2022-01-11 13:09:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Check if git is installed using a shell command
|
|
|
|
* @return string 'OK' on success and 'not OK' on failure.
|
|
|
|
*/
|
|
|
|
function getGitVersion(): string
|
2022-01-11 18:54:32 +00:00
|
|
|
{
|
2023-03-29 21:30:51 +00:00
|
|
|
$output = shell_exec('git --version') ?? '';
|
2022-01-11 18:54:32 +00:00
|
|
|
preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2022-01-11 18:54:32 +00:00
|
|
|
$versionoutput = $version[0] ?? 0;
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2023-01-05 17:01:42 +00:00
|
|
|
return $versionoutput != 0 ? 'OK' : 'not OK';
|
2022-01-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Check if tar is installed using a shell command
|
|
|
|
* @return string 'OK' on success and 'not OK' on failure.
|
|
|
|
*/
|
|
|
|
function getTarVersion(): string
|
2022-01-11 18:54:32 +00:00
|
|
|
{
|
2023-03-29 21:30:51 +00:00
|
|
|
$output = shell_exec('tar --version') ?? '';
|
2022-01-11 18:54:32 +00:00
|
|
|
preg_match('@[0-9]+\.[0-9]+@', $output, $version);
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2022-01-11 18:54:32 +00:00
|
|
|
$versionoutput = $version[0] ?? 0;
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2023-01-05 17:01:42 +00:00
|
|
|
return $versionoutput != 0 ? 'OK' : 'not OK';
|
2022-01-11 13:09:20 +00:00
|
|
|
}
|
2022-01-11 11:45:36 +00:00
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Check all extensions to see if they have loaded or not
|
|
|
|
* @return array Returns an array of extensions that failed to load.
|
|
|
|
*/
|
|
|
|
function checkExtensions(): array
|
2022-01-11 18:54:32 +00:00
|
|
|
{
|
2023-03-29 22:30:19 +00:00
|
|
|
global $required_extensions;
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2022-01-11 18:54:32 +00:00
|
|
|
$not_ok = [];
|
|
|
|
$extentions = get_loaded_extensions();
|
2022-01-11 11:45:36 +00:00
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
foreach ($required_extensions as $ext) {
|
2023-03-29 23:11:18 +00:00
|
|
|
if (!preg_grep('/^(?=.*' . $ext . ').*$/', $extentions)) {
|
2022-01-11 18:54:32 +00:00
|
|
|
array_push($not_ok, $ext);
|
2023-01-05 17:01:42 +00:00
|
|
|
}
|
2022-01-11 18:54:32 +00:00
|
|
|
}
|
2023-01-05 17:01:42 +00:00
|
|
|
|
2022-01-11 18:54:32 +00:00
|
|
|
return $not_ok;
|
2022-01-11 11:45:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Sets the environment variable into the env file
|
|
|
|
* @param string $envKey The environment key to set or modify
|
|
|
|
* @param string $envValue The environment variable to set
|
|
|
|
* @return bool true on success or false on failure.
|
|
|
|
*/
|
2023-03-30 08:32:31 +00:00
|
|
|
function setenv(string $envKey, $envValue)
|
2022-01-11 18:54:32 +00:00
|
|
|
{
|
2023-03-29 22:30:19 +00:00
|
|
|
$str = "{$envKey}={$envValue}";
|
|
|
|
return putenv($str);
|
2022-01-11 18:54:32 +00:00
|
|
|
}
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2022-01-20 08:26:46 +00:00
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
2023-03-29 22:37:15 +00:00
|
|
|
* Encrypt the given value
|
2023-03-29 22:30:19 +00:00
|
|
|
* @param mixed $value The variable to be encrypted
|
2023-03-29 22:37:15 +00:00
|
|
|
* @param bool $serialize If the encryption should be serialized
|
2023-03-29 22:30:19 +00:00
|
|
|
* @return string Returns the encrypted variable.
|
|
|
|
*/
|
2023-03-29 22:37:15 +00:00
|
|
|
function encryptSettingsValue(mixed $value, $serialize = true): string
|
2023-03-29 22:30:19 +00:00
|
|
|
{
|
|
|
|
$appKey = getEnvironmentValue('APP_KEY');
|
|
|
|
$appKey = base64_decode(Str::after($appKey, 'base64:'));
|
2023-03-30 08:37:12 +00:00
|
|
|
$encrypter = new Encrypter($appKey);
|
2023-03-29 22:37:15 +00:00
|
|
|
$encryptedKey = $encrypter->encrypt($value, $serialize);
|
2023-03-29 22:30:19 +00:00
|
|
|
|
|
|
|
return $encryptedKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-03-29 22:37:15 +00:00
|
|
|
* Decrypt the given value
|
2023-03-29 22:30:19 +00:00
|
|
|
* @param mixed $payload The payload to be decrypted
|
2023-03-29 22:37:15 +00:00
|
|
|
* @param bool $unserialize If the encryption should be unserialized
|
2023-03-29 22:30:19 +00:00
|
|
|
* @return mixed Returns the decrypted variable on success, throws otherwise.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function decryptSettingsValue(mixed $payload, $unserialize = true)
|
|
|
|
{
|
|
|
|
$appKey = getEnvironmentValue('APP_KEY');
|
|
|
|
$appKey = base64_decode(Str::after($appKey, 'base64:'));
|
2023-03-30 08:37:12 +00:00
|
|
|
$encrypter = new Encrypter($appKey);
|
2023-03-29 22:30:19 +00:00
|
|
|
$decryptedKey = $encrypter->decrypt($payload, $unserialize);
|
2022-01-20 08:26:46 +00:00
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
return $decryptedKey;
|
2022-01-20 08:26:46 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Run a shell command
|
|
|
|
* @param string $command The command string to run
|
2023-03-30 10:50:35 +00:00
|
|
|
* @param array|null $descriptors [optional]<p>
|
|
|
|
* An indexed array where the key represents the descriptor number and the value represents how PHP will pass that descriptor to the child process. 0 is stdin, 1 is stdout, while 2 is stderr.
|
|
|
|
* Default descriptors when null are 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']
|
|
|
|
* </p>
|
|
|
|
* @param string|null $cwd [optional] <p>
|
|
|
|
* The initial working dir for the command. This must be an
|
|
|
|
* absolute directory path, or null
|
|
|
|
* if you want to use the default value (the working dir of the current
|
|
|
|
* PHP process)
|
|
|
|
* </p>
|
|
|
|
* @param array|null $options [optional] <p>
|
|
|
|
* Allows you to specify additional options.
|
|
|
|
* @link https://www.php.net/manual/en/function.proc-open.php proc_open
|
|
|
|
* </p>
|
2023-03-29 22:30:19 +00:00
|
|
|
* @return false|string|null Returns the result from the command.
|
|
|
|
*/
|
2023-03-30 10:50:35 +00:00
|
|
|
function run_console(string $command, array $descriptors = null, string $cwd = null, array $options = null)
|
2022-01-24 11:12:50 +00:00
|
|
|
{
|
|
|
|
$path = dirname(__FILE__, 3);
|
2023-03-30 10:50:35 +00:00
|
|
|
$descriptors = $descriptors ?? [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']];
|
|
|
|
$handle = proc_open("cd '$path' && bash -c 'exec -a ServerCPP $command'", $descriptors, $pipes, $cwd, null, $options);
|
2023-01-05 17:01:42 +00:00
|
|
|
|
2023-03-30 10:50:35 +00:00
|
|
|
return stream_get_contents($pipes[1]);
|
2022-01-24 11:12:50 +00:00
|
|
|
}
|
2022-01-11 13:09:20 +00:00
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Log to installer.log in the install folder
|
|
|
|
* @param string $log_msg the message to log
|
|
|
|
* @return void No output.
|
|
|
|
*/
|
|
|
|
function wh_log(string $log_msg)
|
2022-02-08 07:13:13 +00:00
|
|
|
{
|
2023-01-05 17:01:42 +00:00
|
|
|
$log_filename = 'logs';
|
2023-03-29 23:11:18 +00:00
|
|
|
if (!file_exists($log_filename)) {
|
2022-02-08 07:13:13 +00:00
|
|
|
// create directory/folder uploads.
|
|
|
|
mkdir($log_filename, 0777, true);
|
|
|
|
}
|
2023-03-29 23:11:18 +00:00
|
|
|
$log_file_data = $log_filename . '/installer.log';
|
2022-02-08 07:13:13 +00:00
|
|
|
// if you don't add `FILE_APPEND`, the file will be erased each time you add a log
|
2023-03-29 23:11:18 +00:00
|
|
|
file_put_contents($log_file_data, '[' . date('h:i:s') . '] ' . $log_msg . "\n", FILE_APPEND);
|
2022-02-08 07:13:13 +00:00
|
|
|
}
|
2022-06-07 15:30:38 +00:00
|
|
|
|
2023-03-29 22:30:19 +00:00
|
|
|
/**
|
|
|
|
* Generate a random string
|
|
|
|
* @param int $length The length of the random string
|
|
|
|
* @return string The randomly generated string.
|
|
|
|
*/
|
|
|
|
function generateRandomString(int $length = 8): string
|
2023-01-05 17:01:42 +00:00
|
|
|
{
|
2022-06-07 15:30:38 +00:00
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
|
|
$charactersLength = strlen($characters);
|
|
|
|
$randomString = '';
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)];
|
|
|
|
}
|
2023-01-05 17:01:42 +00:00
|
|
|
|
2022-06-07 15:30:38 +00:00
|
|
|
return $randomString;
|
|
|
|
}
|