helpers.php 4.4 KB

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