helpers.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. # Return codes
  4. const E_ARGS = 1;
  5. const E_INVALID = 2;
  6. const E_NOTEXIST = 3;
  7. const E_EXISTS = 4;
  8. const E_SUSPENDED = 5;
  9. const E_UNSUSPENDED = 6;
  10. const E_INUSE = 7;
  11. const E_LIMIT = 8;
  12. const E_PASSWORD = 9;
  13. const E_FORBIDEN = 10;
  14. const E_FORBIDDEN = 10;
  15. const E_DISABLED = 11;
  16. const E_PARSING = 12;
  17. const E_DISK = 13;
  18. const E_LA = 14;
  19. const E_CONNECT = 15;
  20. const E_FTP = 16;
  21. const E_DB = 17;
  22. const E_RRD = 18;
  23. const E_UPDATE = 19;
  24. const E_RESTART = 20;
  25. /**
  26. * Looks for a code equivalent to "exit_code" to use in http_code.
  27. *
  28. * @param int $exit_code
  29. * @param int $default
  30. * @return int
  31. */
  32. function exit_code_to_http_code(
  33. int $exit_code,
  34. int $default = 400
  35. // int $default = 500
  36. ): int {
  37. switch ($exit_code) {
  38. case 0:
  39. return 200;
  40. case E_ARGS:
  41. // return 500;
  42. return 400;
  43. case E_INVALID:
  44. return 422;
  45. // case E_NOTEXIST:
  46. // return 404;
  47. // case E_EXISTS:
  48. // return 302;
  49. case E_PASSWORD:
  50. return 401;
  51. case E_SUSPENDED:
  52. case E_UNSUSPENDED:
  53. case E_FORBIDEN:
  54. case E_FORBIDDEN:
  55. return 401;
  56. // return 403;
  57. case E_DISABLED:
  58. return 400;
  59. // return 503;
  60. }
  61. return $default;
  62. }
  63. function check_local_ip($addr) {
  64. if (in_array($addr, array($_SERVER['SERVER_ADDR'], '127.0.0.1'))) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. }
  70. function get_real_user_ip() {
  71. $ip = $_SERVER['REMOTE_ADDR'];
  72. if (isset($_SERVER['HTTP_CLIENT_IP']) && !check_local_ip($_SERVER['HTTP_CLIENT_IP'])) {
  73. if (filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
  74. $ip = $_SERVER['HTTP_CLIENT_IP'];
  75. }
  76. }
  77. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !check_local_ip($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  78. if (filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) {
  79. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  80. }
  81. }
  82. if (isset($_SERVER['HTTP_FORWARDED_FOR']) && !check_local_ip($_SERVER['HTTP_FORWARDED_FOR'])) {
  83. if (filter_var($_SERVER['HTTP_FORWARDED_FOR'], FILTER_VALIDATE_IP)) {
  84. $ip = $_SERVER['HTTP_FORWARDED_FOR'];
  85. }
  86. }
  87. if (isset($_SERVER['HTTP_X_FORWARDED']) && !check_local_ip($_SERVER['HTTP_X_FORWARDED'])) {
  88. if (filter_var($_SERVER['HTTP_X_FORWARDED'], FILTER_VALIDATE_IP)) {
  89. $ip = $_SERVER['HTTP_X_FORWARDED'];
  90. }
  91. }
  92. if (isset($_SERVER['HTTP_FORWARDED']) && !check_local_ip($_SERVER['HTTP_FORWARDED'])) {
  93. if (filter_var($_SERVER['HTTP_FORWARDED'], FILTER_VALIDATE_IP)) {
  94. $ip = $_SERVER['HTTP_FORWARDED'];
  95. }
  96. }
  97. if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !check_local_ip($_SERVER['HTTP_CF_CONNECTING_IP'])) {
  98. if (filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP)) {
  99. $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
  100. }
  101. }
  102. return $ip;
  103. }
  104. /**
  105. * Create a history log using 'v-log-action' script.
  106. *
  107. * @param string $message The message for log.
  108. * @param string $category A category for log. Ex: Auth, Firewall, API...
  109. * @param string $level Info|Warning|Error.
  110. * @param string $user A username for save in the user history ou 'system' to save in Hestia history.
  111. * @return int The script result code.
  112. */
  113. function hst_add_history_log($message, $category = 'System', $level = 'Info', $user = 'system') {
  114. //$message = ucfirst($message);
  115. //$message = str_replace("'", "`", $message);
  116. $category = ucfirst(strtolower($category));
  117. $level = ucfirst(strtolower($level));
  118. $command_args = quoteshellarg($user).' '.quoteshellarg($level).' '.quoteshellarg($category).' '.quoteshellarg($message);
  119. exec(HESTIA_CMD."v-log-action ".$command_args, $output, $return_var);
  120. unset($output);
  121. return $return_var;
  122. }
  123. function get_hostname(){
  124. $badValues = array(false, null, 0, '', "localhost", "127.0.0.1", "::1", "0000:0000:0000:0000:0000:0000:0000:0001");
  125. $ret = gethostname();
  126. if(in_array($ret, $badValues, true)){
  127. throw new Exception('gethostname() failed');
  128. }
  129. $ret2 = gethostbyname($ret);
  130. if(in_array($ret2, $badValues, true)){
  131. return $ret;
  132. }
  133. $ret3 = gethostbyaddr($ret2);
  134. if(in_array($ret3, $badValues, true)){
  135. return $ret2;
  136. }
  137. return $ret3;
  138. }