functions.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. $required_extentions = array("openssl", "gd", "mysql", "PDO", "mbstring", "tokenizer", "bcmath", "xml", "curl", "zip", "fpm");
  3. $requirements = [
  4. "php" => "7.4",
  5. "mysql" => "5.7.22",
  6. ];
  7. function checkPhpVersion()
  8. {
  9. global $requirements;
  10. if (version_compare(phpversion(), $requirements["php"], '>=')) {
  11. return "OK";
  12. }
  13. return "not OK";
  14. }
  15. function getMySQLVersion()
  16. {
  17. global $requirements;
  18. $output = shell_exec('mysql -V');
  19. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  20. $versionoutput = $version[0] ?? "0";
  21. return (intval($versionoutput) > intval($requirements["mysql"]) ? "OK" : $versionoutput);
  22. }
  23. function getZipVersion()
  24. {
  25. $output = shell_exec('zip -v');
  26. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  27. $versionoutput = $version[0] ?? 0;
  28. return ($versionoutput != 0 ? "OK" : "not OK");
  29. }
  30. function getGitVersion()
  31. {
  32. $output = shell_exec('git --version');
  33. preg_match('@[0-9]+\.[0-9]+\.[0-9]+@', $output, $version);
  34. $versionoutput = $version[0] ?? 0;
  35. return ($versionoutput != 0 ? "OK" : "not OK");
  36. }
  37. function getTarVersion()
  38. {
  39. $output = shell_exec('tar --version');
  40. preg_match('@[0-9]+\.[0-9]+@', $output, $version);
  41. $versionoutput = $version[0] ?? 0;
  42. return ($versionoutput != 0 ? "OK" : "not OK");
  43. }
  44. function checkExtensions()
  45. {
  46. global $required_extentions;
  47. $not_ok = [];
  48. $extentions = get_loaded_extensions();
  49. foreach ($required_extentions as $ext) {
  50. if (!preg_grep("/^(?=.*" . $ext . ").*$/", $extentions))
  51. array_push($not_ok, $ext);
  52. }
  53. return $not_ok;
  54. }
  55. function setEnvironmentValue($envKey, $envValue)
  56. {
  57. $envFile = dirname(__FILE__, 3) . "/.env";
  58. $str = file_get_contents($envFile);
  59. $str .= "\n"; // In case the searched variable is in the last line without \n
  60. $keyPosition = strpos($str, "{$envKey}=");
  61. $endOfLinePosition = strpos($str, PHP_EOL, $keyPosition);
  62. $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
  63. $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
  64. $str = substr($str, 0, -1);
  65. $fp = fopen($envFile, 'w');
  66. fwrite($fp, $str);
  67. fclose($fp);
  68. }
  69. function getEnvironmentValue($envKey)
  70. {
  71. $envFile = dirname(__FILE__, 3) . "/.env";
  72. $str = file_get_contents($envFile);
  73. $str .= "\n"; // In case the searched variable is in the last line without \n
  74. $keyPosition = strpos($str, "{$envKey}=");
  75. $endOfLinePosition = strpos($str, PHP_EOL, $keyPosition);
  76. $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
  77. $value = substr($oldLine, strpos($oldLine, "=") + 1);
  78. return $value;
  79. }
  80. function run_console($command)
  81. {
  82. $path = dirname(__FILE__, 3);
  83. $cmd = "cd '$path' && bash -c 'exec -a ServerCPP $command' 2>&1";
  84. return shell_exec($cmd);
  85. }
  86. ?>