helpers.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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(int $exit_code, int $default = 400): int {
  33. switch ($exit_code) {
  34. case 0:
  35. return 200;
  36. case E_ARGS:
  37. // return 500;
  38. return 400;
  39. case E_INVALID:
  40. return 422;
  41. // case E_NOTEXIST:
  42. // return 404;
  43. // case E_EXISTS:
  44. // return 302;
  45. case E_PASSWORD:
  46. return 401;
  47. case E_SUSPENDED:
  48. case E_UNSUSPENDED:
  49. case E_FORBIDEN:
  50. case E_FORBIDDEN:
  51. return 401;
  52. // return 403;
  53. case E_DISABLED:
  54. return 400;
  55. // return 503;
  56. }
  57. return $default;
  58. }
  59. function check_local_ip($addr) {
  60. if (in_array($addr, [$_SERVER["SERVER_ADDR"], "127.0.0.1"])) {
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. }
  66. function get_real_user_ip() {
  67. $ip = $_SERVER["REMOTE_ADDR"];
  68. if (isset($_SERVER["HTTP_CLIENT_IP"]) && !check_local_ip($_SERVER["HTTP_CLIENT_IP"])) {
  69. if (filter_var($_SERVER["HTTP_CLIENT_IP"], FILTER_VALIDATE_IP)) {
  70. $ip = $_SERVER["HTTP_CLIENT_IP"];
  71. }
  72. }
  73. if (
  74. isset($_SERVER["HTTP_X_FORWARDED_FOR"]) &&
  75. !check_local_ip($_SERVER["HTTP_X_FORWARDED_FOR"])
  76. ) {
  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 (
  97. isset($_SERVER["HTTP_CF_CONNECTING_IP"]) &&
  98. !check_local_ip($_SERVER["HTTP_CF_CONNECTING_IP"])
  99. ) {
  100. if (filter_var($_SERVER["HTTP_CF_CONNECTING_IP"], FILTER_VALIDATE_IP)) {
  101. $ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
  102. }
  103. }
  104. return $ip;
  105. }
  106. /**
  107. * Create a history log using 'v-log-action' script.
  108. *
  109. * @param string $message The message for log.
  110. * @param string $category A category for log. Ex: Auth, Firewall, API...
  111. * @param string $level Info|Warning|Error.
  112. * @param string $user A username for save in the user history ou 'system' to save in Hestia history.
  113. * @return int The script result code.
  114. */
  115. function hst_add_history_log($message, $category = "System", $level = "Info", $user = "system") {
  116. //$message = ucfirst($message);
  117. //$message = str_replace("'", "`", $message);
  118. $category = ucfirst(strtolower($category));
  119. $level = ucfirst(strtolower($level));
  120. $command_args =
  121. quoteshellarg($user) .
  122. " " .
  123. quoteshellarg($level) .
  124. " " .
  125. quoteshellarg($category) .
  126. " " .
  127. quoteshellarg($message);
  128. exec(HESTIA_CMD . "v-log-action " . $command_args, $output, $return_var);
  129. unset($output);
  130. return $return_var;
  131. }
  132. function get_hostname() {
  133. $badValues = [
  134. false,
  135. null,
  136. 0,
  137. "",
  138. "localhost",
  139. "127.0.0.1",
  140. "::1",
  141. "0000:0000:0000:0000:0000:0000:0000:0001",
  142. ];
  143. $ret = gethostname();
  144. if (in_array($ret, $badValues, true)) {
  145. throw new Exception("gethostname() failed");
  146. }
  147. $ret2 = gethostbyname($ret);
  148. if (in_array($ret2, $badValues, true)) {
  149. return $ret;
  150. }
  151. $ret3 = gethostbyaddr($ret2);
  152. if (in_array($ret3, $badValues, true)) {
  153. return $ret2;
  154. }
  155. return $ret3;
  156. }
  157. function display_title($tab) {
  158. $array1 = ["{{page}}", "{{hostname}}", "{{ip}}", "{{appname}}"];
  159. $array2 = [$tab, get_hostname(), $_SERVER["REMOTE_ADDR"], $_SESSION["APP_NAME"]];
  160. return str_replace($array1, $array2, $_SESSION["TITLE"]);
  161. }