functions.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. require '../../vendor/autoload.php';
  3. use Illuminate\Encryption\Encrypter;
  4. use Illuminate\Support\Str;
  5. $required_extensions = ['openssl', 'gd', 'mysql', 'PDO', 'mbstring', 'tokenizer', 'bcmath', 'xml', 'curl', 'zip', 'intl'];
  6. $requirements = [
  7. 'minPhp' => '8.1',
  8. 'maxPhp' => '8.2', // This version is not supported
  9. 'mysql' => '5.7.22',
  10. ];
  11. /**
  12. * Check if the minimum PHP version is present
  13. * @return string 'OK' on success and 'not OK' on failure.
  14. */
  15. function checkPhpVersion(): string
  16. {
  17. global $requirements;
  18. if (version_compare(phpversion(), $requirements['minPhp'], '>=') && version_compare(phpversion(), $requirements['maxPhp'], '<=')) {
  19. return 'OK';
  20. }
  21. return 'not OK';
  22. }
  23. /**
  24. * Check if the environment file is writable
  25. * @return bool Returns true on writable and false on not writable.
  26. */
  27. function checkWriteable(): bool
  28. {
  29. return is_writable('../../.env');
  30. }
  31. /**
  32. * Check if the server runs using HTTPS
  33. * @return bool Returns true on HTTPS or false on HTTP.
  34. */
  35. function checkHTTPS(): bool
  36. {
  37. return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
  38. || $_SERVER['SERVER_PORT'] == 443;
  39. }
  40. /**
  41. * Check if MySQL is installed and runs the correct version using a shell command
  42. * @return mixed|string 'OK' if required version is met, returns MySQL version if not met.
  43. */
  44. function getMySQLVersion(): mixed
  45. {
  46. global $requirements;
  47. $output = shell_exec('mysql -V') ?? '';
  48. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  49. $versionoutput = $version[0] ?? '0';
  50. return intval($versionoutput) > intval($requirements['mysql']) ? 'OK' : $versionoutput;
  51. }
  52. /**
  53. * Check if zip is installed using a shell command
  54. * @return string 'OK' on success and 'not OK' on failure.
  55. */
  56. function getZipVersion(): string
  57. {
  58. $output = shell_exec('zip -v') ?? '';
  59. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  60. $versionoutput = $version[0] ?? 0;
  61. return $versionoutput != 0 ? 'OK' : 'not OK';
  62. }
  63. /**
  64. * Check if git is installed using a shell command
  65. * @return string 'OK' on success and 'not OK' on failure.
  66. */
  67. function getGitVersion(): string
  68. {
  69. $output = shell_exec('git --version') ?? '';
  70. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  71. $versionoutput = $version[0] ?? 0;
  72. return $versionoutput != 0 ? 'OK' : 'not OK';
  73. }
  74. /**
  75. * Check if tar is installed using a shell command
  76. * @return string 'OK' on success and 'not OK' on failure.
  77. */
  78. function getTarVersion(): string
  79. {
  80. $output = shell_exec('tar --version') ?? '';
  81. preg_match('@[0-9]+\.[0-9]+@', $output, $version);
  82. $versionoutput = $version[0] ?? 0;
  83. return $versionoutput != 0 ? 'OK' : 'not OK';
  84. }
  85. /**
  86. * Check all extensions to see if they have loaded or not
  87. * @return array Returns an array of extensions that failed to load.
  88. */
  89. function checkExtensions(): array
  90. {
  91. global $required_extensions;
  92. $not_ok = [];
  93. $extentions = get_loaded_extensions();
  94. foreach ($required_extensions as $ext) {
  95. if (!preg_grep('/^(?=.*' . $ext . ').*$/', $extentions)) {
  96. array_push($not_ok, $ext);
  97. }
  98. }
  99. return $not_ok;
  100. }
  101. /**
  102. * Sets the environment variable into the env file
  103. * @param string $envKey The environment key to set or modify
  104. * @param string $envValue The environment variable to set
  105. * @return bool true on success or false on failure.
  106. */
  107. function setenv(string $envKey, $envValue)
  108. {
  109. $str = "{$envKey}={$envValue}";
  110. return putenv($str);
  111. }
  112. /**
  113. * Encrypt the given value
  114. * @param mixed $value The variable to be encrypted
  115. * @param bool $serialize If the encryption should be serialized
  116. * @return string Returns the encrypted variable.
  117. */
  118. function encryptSettingsValue(mixed $value, $serialize = true): string
  119. {
  120. $appKey = getEnvironmentValue('APP_KEY');
  121. $appKey = base64_decode(Str::after($appKey, 'base64:'));
  122. $encrypter = new Encrypter($appKey, 'AES-256-CBC');
  123. $encryptedKey = $encrypter->encrypt($value, $serialize);
  124. return $encryptedKey;
  125. }
  126. /**
  127. * Decrypt the given value
  128. * @param mixed $payload The payload to be decrypted
  129. * @param bool $unserialize If the encryption should be unserialized
  130. * @return mixed Returns the decrypted variable on success, throws otherwise.
  131. */
  132. function decryptSettingsValue(mixed $payload, $unserialize = true)
  133. {
  134. $appKey = getEnvironmentValue('APP_KEY');
  135. $appKey = base64_decode(Str::after($appKey, 'base64:'));
  136. $encrypter = new Encrypter($appKey, 'AES-256-CBC');
  137. $decryptedKey = $encrypter->decrypt($payload, $unserialize);
  138. return $decryptedKey;
  139. }
  140. /**
  141. * Run a shell command
  142. * @param string $command The command string to run
  143. * @return false|string|null Returns the result from the command.
  144. */
  145. function run_console(string $command)
  146. {
  147. $path = dirname(__FILE__, 3);
  148. $cmd = "cd '$path' && bash -c 'exec -a ServerCPP $command' 2>&1";
  149. return shell_exec($cmd);
  150. }
  151. /**
  152. * Log to installer.log in the install folder
  153. * @param string $log_msg the message to log
  154. * @return void No output.
  155. */
  156. function wh_log(string $log_msg)
  157. {
  158. $log_filename = 'logs';
  159. if (!file_exists($log_filename)) {
  160. // create directory/folder uploads.
  161. mkdir($log_filename, 0777, true);
  162. }
  163. $log_file_data = $log_filename . '/installer.log';
  164. // if you don't add `FILE_APPEND`, the file will be erased each time you add a log
  165. file_put_contents($log_file_data, '[' . date('h:i:s') . '] ' . $log_msg . "\n", FILE_APPEND);
  166. }
  167. /**
  168. * Generate a random string
  169. * @param int $length The length of the random string
  170. * @return string The randomly generated string.
  171. */
  172. function generateRandomString(int $length = 8): string
  173. {
  174. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  175. $charactersLength = strlen($characters);
  176. $randomString = '';
  177. for ($i = 0; $i < $length; $i++) {
  178. $randomString .= $characters[rand(0, $charactersLength - 1)];
  179. }
  180. return $randomString;
  181. }