functions.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. $required_extentions = ['openssl', 'gd', 'mysql', 'PDO', 'mbstring', 'tokenizer', 'bcmath', 'xml', 'curl', 'zip', 'intl'];
  3. $requirements = [
  4. 'minPhp' => '8.1',
  5. 'maxPhp' => '8.2', // This version is not supported
  6. 'mysql' => '5.7.22',
  7. ];
  8. function checkPhpVersion()
  9. {
  10. global $requirements;
  11. if (version_compare(phpversion(), $requirements['minPhp'], '>=') && version_compare(phpversion(), $requirements['maxPhp'], '<=')) {
  12. return 'OK';
  13. }
  14. return 'not OK';
  15. }
  16. function checkWriteable()
  17. {
  18. return is_writable('../../.env');
  19. }
  20. function checkHTTPS()
  21. {
  22. return (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
  23. || $_SERVER['SERVER_PORT'] == 443;
  24. }
  25. function getMySQLVersion()
  26. {
  27. global $requirements;
  28. $output = shell_exec('mysql -V');
  29. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  30. $versionoutput = $version[0] ?? '0';
  31. return intval($versionoutput) > intval($requirements['mysql']) ? 'OK' : $versionoutput;
  32. }
  33. function getZipVersion()
  34. {
  35. $output = shell_exec('zip -v');
  36. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  37. $versionoutput = $version[0] ?? 0;
  38. return $versionoutput != 0 ? 'OK' : 'not OK';
  39. }
  40. function getGitVersion()
  41. {
  42. $output = shell_exec('git --version');
  43. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  44. $versionoutput = $version[0] ?? 0;
  45. return $versionoutput != 0 ? 'OK' : 'not OK';
  46. }
  47. function getTarVersion()
  48. {
  49. $output = shell_exec('tar --version');
  50. preg_match('@[0-9]+\.[0-9]+@', $output, $version);
  51. $versionoutput = $version[0] ?? 0;
  52. return $versionoutput != 0 ? 'OK' : 'not OK';
  53. }
  54. function checkExtensions()
  55. {
  56. global $required_extentions;
  57. $not_ok = [];
  58. $extentions = get_loaded_extensions();
  59. foreach ($required_extentions as $ext) {
  60. if (! preg_grep('/^(?=.*'.$ext.').*$/', $extentions)) {
  61. array_push($not_ok, $ext);
  62. }
  63. }
  64. return $not_ok;
  65. }
  66. function setEnvironmentValue($envKey, $envValue)
  67. {
  68. $envFile = dirname(__FILE__, 3).'/.env';
  69. $str = file_get_contents($envFile);
  70. $str .= "\n"; // In case the searched variable is in the last line without \n
  71. $keyPosition = strpos($str, "{$envKey}=");
  72. $endOfLinePosition = strpos($str, PHP_EOL, $keyPosition);
  73. $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
  74. $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
  75. $str = substr($str, 0, -1);
  76. $fp = fopen($envFile, 'w');
  77. fwrite($fp, $str);
  78. fclose($fp);
  79. }
  80. function getEnvironmentValue($envKey)
  81. {
  82. $envFile = dirname(__FILE__, 3).'/.env';
  83. $str = file_get_contents($envFile);
  84. $str .= "\n"; // In case the searched variable is in the last line without \n
  85. $keyPosition = strpos($str, "{$envKey}=");
  86. $endOfLinePosition = strpos($str, PHP_EOL, $keyPosition);
  87. $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
  88. $value = substr($oldLine, strpos($oldLine, '=') + 1);
  89. return $value;
  90. }
  91. function run_console($command)
  92. {
  93. $path = dirname(__FILE__, 3);
  94. $cmd = "cd '$path' && bash -c 'exec -a ServerCPP $command' 2>&1";
  95. return shell_exec($cmd);
  96. }
  97. function wh_log($log_msg)
  98. {
  99. $log_filename = 'logs';
  100. if (! file_exists($log_filename)) {
  101. // create directory/folder uploads.
  102. mkdir($log_filename, 0777, true);
  103. }
  104. $log_file_data = $log_filename.'/installer.log';
  105. // if you don't add `FILE_APPEND`, the file will be erased each time you add a log
  106. file_put_contents($log_file_data, '['.date('h:i:s').'] '.$log_msg."\n", FILE_APPEND);
  107. }
  108. function generateRandomString($length = 8)
  109. {
  110. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  111. $charactersLength = strlen($characters);
  112. $randomString = '';
  113. for ($i = 0; $i < $length; $i++) {
  114. $randomString .= $characters[rand(0, $charactersLength - 1)];
  115. }
  116. return $randomString;
  117. }