helpers.php 4.8 KB

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