functions.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. $required_extentions = array("openssl", "gd", "mysql", "PDO", "mbstring", "tokenizer", "bcmath", "xml", "curl", "zip", "intl");
  3. $requirements = [
  4. "minPhp" => "7.4",
  5. "maxPhp" => "8.1", // 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. return $not_ok;
  64. }
  65. function setEnvironmentValue($envKey, $envValue)
  66. {
  67. $envFile = dirname(__FILE__, 3) . "/.env";
  68. $str = file_get_contents($envFile);
  69. $str .= "\n"; // In case the searched variable is in the last line without \n
  70. $keyPosition = strpos($str, "{$envKey}=");
  71. $endOfLinePosition = strpos($str, PHP_EOL, $keyPosition);
  72. $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
  73. $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
  74. $str = substr($str, 0, -1);
  75. $fp = fopen($envFile, 'w');
  76. fwrite($fp, $str);
  77. fclose($fp);
  78. }
  79. function getEnvironmentValue($envKey)
  80. {
  81. $envFile = dirname(__FILE__, 3) . "/.env";
  82. $str = file_get_contents($envFile);
  83. $str .= "\n"; // In case the searched variable is in the last line without \n
  84. $keyPosition = strpos($str, "{$envKey}=");
  85. $endOfLinePosition = strpos($str, PHP_EOL, $keyPosition);
  86. $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
  87. $value = substr($oldLine, strpos($oldLine, "=") + 1);
  88. return $value;
  89. }
  90. function run_console($command)
  91. {
  92. $path = dirname(__FILE__, 3);
  93. $cmd = "cd '$path' && bash -c 'exec -a ServerCPP $command' 2>&1";
  94. return shell_exec($cmd);
  95. }
  96. function wh_log($log_msg)
  97. {
  98. $log_filename = "logs";
  99. if (!file_exists($log_filename)) {
  100. // create directory/folder uploads.
  101. mkdir($log_filename, 0777, true);
  102. }
  103. $log_file_data = $log_filename . '/installer.log';
  104. // if you don't add `FILE_APPEND`, the file will be erased each time you add a log
  105. file_put_contents($log_file_data, "[" . date('h:i:s') . "] " . $log_msg . "\n", FILE_APPEND);
  106. }