123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- use Illuminate\Encryption\Encrypter;
- use Illuminate\Support\Str;
- $required_extensions = ['openssl', 'gd', 'mysql', 'PDO', 'mbstring', 'tokenizer', 'bcmath', 'xml', 'curl', 'zip', 'intl'];
- $requirements = [
- 'minPhp' => '8.1',
- 'maxPhp' => '8.2', // This version is not supported
- 'mysql' => '5.7.22',
- ];
- /**
- * Check if the minimum PHP version is present
- * @return string 'OK' on success and 'not OK' on failure.
- */
- function checkPhpVersion(): string
- {
- global $requirements;
- if (version_compare(phpversion(), $requirements['minPhp'], '>=') && version_compare(phpversion(), $requirements['maxPhp'], '<=')) {
- return 'OK';
- }
- return 'not OK';
- }
- /**
- * Check if the environment file is writable
- * @return bool Returns true on writable and false on not writable.
- */
- function checkWriteable(): bool
- {
- return is_writable('../../.env');
- }
- /**
- * Check if the server runs using HTTPS
- * @return bool Returns true on HTTPS or false on HTTP.
- */
- function checkHTTPS(): bool
- {
- return (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
- || $_SERVER['SERVER_PORT'] == 443;
- }
- /**
- * 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
- {
- global $requirements;
- $output = shell_exec('mysql -V') ?? '';
- preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
- $versionoutput = $version[0] ?? '0';
- return intval($versionoutput) > intval($requirements['mysql']) ? 'OK' : $versionoutput;
- }
- /**
- * Check if zip is installed using a shell command
- * @return string 'OK' on success and 'not OK' on failure.
- */
- function getZipVersion(): string
- {
- $output = shell_exec('zip -v') ?? '';
- preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
- $versionoutput = $version[0] ?? 0;
- return $versionoutput != 0 ? 'OK' : 'not OK';
- }
- /**
- * Check if git is installed using a shell command
- * @return string 'OK' on success and 'not OK' on failure.
- */
- function getGitVersion(): string
- {
- $output = shell_exec('git --version') ?? '';
- preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
- $versionoutput = $version[0] ?? 0;
- return $versionoutput != 0 ? 'OK' : 'not OK';
- }
- /**
- * Check if tar is installed using a shell command
- * @return string 'OK' on success and 'not OK' on failure.
- */
- function getTarVersion(): string
- {
- $output = shell_exec('tar --version') ?? '';
- preg_match('@[0-9]+\.[0-9]+@', $output, $version);
- $versionoutput = $version[0] ?? 0;
- return $versionoutput != 0 ? 'OK' : 'not OK';
- }
- /**
- * 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
- {
- global $required_extensions;
- $not_ok = [];
- $extentions = get_loaded_extensions();
- foreach ($required_extensions as $ext) {
- if (! preg_grep('/^(?=.*'.$ext.').*$/', $extentions)) {
- array_push($not_ok, $ext);
- }
- }
- return $not_ok;
- }
- /**
- * 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.
- */
- function setEnvironmentValue(string $envKey, $envValue)
- {
- $str = "{$envKey}={$envValue}";
- return putenv($str);
- }
- /**
- * Gets the variable from the env file
- * @param string $envKey The environment variable to look for
- * @return array|false|string Returns the value if found, otherwise returns false.
- */
- function getEnvironmentValue(string $envKey): array|false|string
- {
- return getenv($envKey);
- }
- /**
- * Encrypts the variable passed and returns the encrypted version
- * @param mixed $value The variable to be encrypted
- * @return string Returns the encrypted variable.
- */
- function encryptSettingsValue(mixed $value): string
- {
- $appKey = getEnvironmentValue('APP_KEY');
- $appKey = base64_decode(Str::after($appKey, 'base64:'));
- $encrypter = new Encrypter($appKey, 'AES-256-CBC');
- $encryptedKey = $encrypter->encrypt($value);
- return $encryptedKey;
- }
- /**
- * Decrypts the payload passed and returns the decrypted version
- * @param mixed $payload The payload to be decrypted
- * @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:'));
- $encrypter = new Encrypter($appKey, 'AES-256-CBC');
- $decryptedKey = $encrypter->decrypt($payload, $unserialize);
- return $decryptedKey;
- }
- /**
- * Run a shell command
- * @param string $command The command string to run
- * @return false|string|null Returns the result from the command.
- */
- function run_console(string $command)
- {
- $path = dirname(__FILE__, 3);
- $cmd = "cd '$path' && bash -c 'exec -a ServerCPP $command' 2>&1";
- return shell_exec($cmd);
- }
- /**
- * 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)
- {
- $log_filename = 'logs';
- if (! file_exists($log_filename)) {
- // create directory/folder uploads.
- mkdir($log_filename, 0777, true);
- }
- $log_file_data = $log_filename.'/installer.log';
- // if you don't add `FILE_APPEND`, the file will be erased each time you add a log
- file_put_contents($log_file_data, '['.date('h:i:s').'] '.$log_msg."\n", FILE_APPEND);
- }
- /**
- * 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
- {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
|