helpers.php 4.4 KB

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