functions.php 6.0 KB

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