main.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. <?php
  2. session_start();
  3. use PHPMailer\PHPMailer\PHPMailer;
  4. use PHPMailer\PHPMailer\SMTP;
  5. use PHPMailer\PHPMailer\Exception;
  6. use function Hestiacp\quoteshellarg\quoteshellarg;
  7. try {
  8. require_once "vendor/autoload.php";
  9. } catch (Throwable $ex) {
  10. $errstr =
  11. "Unable to load required libraries. Please run v-add-sys-dependencies in command line. Error: " .
  12. $ex->getMessage();
  13. trigger_error($errstr);
  14. echo $errstr;
  15. exit(1);
  16. }
  17. define("HESTIA_DIR_BIN", "/usr/local/hestia/bin/");
  18. define("HESTIA_CMD", "/usr/bin/sudo /usr/local/hestia/bin/");
  19. define("DEFAULT_PHP_VERSION", "php-" . exec('php -r "echo substr(phpversion(),0,3);"'));
  20. // Load Hestia Config directly
  21. load_hestia_config();
  22. require_once dirname(__FILE__) . "/prevent_csrf.php";
  23. require_once dirname(__FILE__) . "/helpers.php";
  24. $root_directory = dirname(__FILE__) . "/../../";
  25. function destroy_sessions() {
  26. unset($_SESSION);
  27. session_unset();
  28. session_destroy();
  29. session_start();
  30. }
  31. $i = 0;
  32. // Saving user IPs to the session for preventing session hijacking
  33. $user_combined_ip = "";
  34. if (isset($_SERVER["REMOTE_ADDR"])) {
  35. $user_combined_ip = $_SERVER["REMOTE_ADDR"];
  36. }
  37. if (isset($_SERVER["HTTP_CLIENT_IP"])) {
  38. $user_combined_ip .= "|" . $_SERVER["HTTP_CLIENT_IP"];
  39. }
  40. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  41. $user_combined_ip .= "|" . $_SERVER["HTTP_X_FORWARDED_FOR"];
  42. }
  43. if (isset($_SERVER["HTTP_FORWARDED_FOR"])) {
  44. $user_combined_ip .= "|" . $_SERVER["HTTP_FORWARDED_FOR"];
  45. }
  46. if (isset($_SERVER["HTTP_X_FORWARDED"])) {
  47. $user_combined_ip .= "|" . $_SERVER["HTTP_X_FORWARDED"];
  48. }
  49. if (isset($_SERVER["HTTP_FORWARDED"])) {
  50. $user_combined_ip .= "|" . $_SERVER["HTTP_FORWARDED"];
  51. }
  52. if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  53. if (!empty($_SERVER["HTTP_CF_CONNECTING_IP"])) {
  54. $user_combined_ip = $_SERVER["HTTP_CF_CONNECTING_IP"];
  55. }
  56. }
  57. if (!isset($_SESSION["user_combined_ip"])) {
  58. $_SESSION["user_combined_ip"] = $user_combined_ip;
  59. }
  60. // Checking user to use session from the same IP he has been logged in
  61. if (
  62. $_SESSION["user_combined_ip"] != $user_combined_ip &&
  63. isset($_SESSION["user"]) &&
  64. $_SESSION["DISABLE_IP_CHECK"] != "yes"
  65. ) {
  66. $v_user = quoteshellarg($_SESSION["user"]);
  67. $v_session_id = quoteshellarg($_SESSION["token"]);
  68. exec(HESTIA_CMD . "v-log-user-logout " . $v_user . " " . $v_session_id, $output, $return_var);
  69. destroy_sessions();
  70. header("Location: /login/");
  71. exit();
  72. }
  73. // Check system settings
  74. if (!isset($_SESSION["VERSION"]) && !defined("NO_AUTH_REQUIRED")) {
  75. destroy_sessions();
  76. header("Location: /login/");
  77. exit();
  78. }
  79. // Check user session
  80. if (!isset($_SESSION["user"]) && !defined("NO_AUTH_REQUIRED")) {
  81. destroy_sessions();
  82. header("Location: /login/");
  83. exit();
  84. }
  85. // Generate CSRF Token
  86. if (isset($_SESSION["user"])) {
  87. if (!isset($_SESSION["token"])) {
  88. $token = bin2hex(random_bytes(16));
  89. $_SESSION["token"] = $token;
  90. }
  91. }
  92. if ($_SESSION["RELEASE_BRANCH"] == "release" && $_SESSION["DEBUG_MODE"] == "false") {
  93. define("JS_LATEST_UPDATE", "v=" . $_SESSION["VERSION"]);
  94. } else {
  95. define("JS_LATEST_UPDATE", "r=" . time());
  96. }
  97. if (!defined("NO_AUTH_REQUIRED")) {
  98. if (empty($_SESSION["LAST_ACTIVITY"]) || empty($_SESSION["INACTIVE_SESSION_TIMEOUT"])) {
  99. destroy_sessions();
  100. header("Location: /login/");
  101. } elseif ($_SESSION["INACTIVE_SESSION_TIMEOUT"] * 60 + $_SESSION["LAST_ACTIVITY"] < time()) {
  102. $v_user = quoteshellarg($_SESSION["user"]);
  103. $v_session_id = quoteshellarg($_SESSION["token"]);
  104. exec(
  105. HESTIA_CMD . "v-log-user-logout " . $v_user . " " . $v_session_id,
  106. $output,
  107. $return_var,
  108. );
  109. destroy_sessions();
  110. header("Location: /login/");
  111. exit();
  112. } else {
  113. $_SESSION["LAST_ACTIVITY"] = time();
  114. }
  115. }
  116. function ipUsed() {
  117. [$http_host, $port] = explode(":", $_SERVER["HTTP_HOST"] . ":");
  118. if (filter_var($http_host, FILTER_VALIDATE_IP)) {
  119. return true;
  120. } else {
  121. return false;
  122. }
  123. }
  124. if (isset($_SESSION["user"])) {
  125. $user = quoteshellarg($_SESSION["user"]);
  126. $user_plain = htmlentities($_SESSION["user"]);
  127. }
  128. if (isset($_SESSION["look"]) && $_SESSION["look"] != "" && $_SESSION["userContext"] === "admin") {
  129. $user = quoteshellarg($_SESSION["look"]);
  130. $user_plain = htmlentities($_SESSION["look"]);
  131. }
  132. if (empty($user_plain)) {
  133. $user_plain = "";
  134. }
  135. require_once dirname(__FILE__) . "/i18n.php";
  136. function check_error($return_var) {
  137. if ($return_var > 0) {
  138. header("Location: /error/");
  139. exit();
  140. }
  141. }
  142. function check_return_code($return_var, $output) {
  143. if ($return_var != 0) {
  144. $error = implode("<br>", $output);
  145. if (empty($error)) {
  146. $error = sprintf(_("Error code: %s"), $return_var);
  147. }
  148. $_SESSION["error_msg"] = $error;
  149. }
  150. }
  151. function check_return_code_redirect($return_var, $output, $location) {
  152. if ($return_var != 0) {
  153. $error = implode("<br>", $output);
  154. if (empty($error)) {
  155. $error = sprintf(_("Error code: %s"), $return_var);
  156. }
  157. $_SESSION["error_msg"] = $error;
  158. header("Location:" . $location);
  159. }
  160. }
  161. function render_page($user, $TAB, $page) {
  162. $__template_dir = dirname(__DIR__) . "/templates/";
  163. // Extract global variables
  164. // I think those variables should be passed via arguments
  165. extract($GLOBALS, EXTR_SKIP);
  166. // Header
  167. include $__template_dir . "header.php";
  168. // Panel
  169. $panel = top_panel(empty($_SESSION["look"]) ? $_SESSION["user"] : $_SESSION["look"], $TAB);
  170. // Policies controller
  171. @include_once dirname(__DIR__) . "/inc/policies.php";
  172. // Body
  173. include $__template_dir . "pages/" . $page . ".php";
  174. // Footer
  175. include $__template_dir . "footer.php";
  176. }
  177. // Match $_SESSION['token'] against $_GET['token'] or $_POST['token']
  178. // Usage: verify_csrf($_POST) or verify_csrf($_GET); Use verify_csrf($_POST,true) to return on failure instead of redirect
  179. function verify_csrf($method, $return = false) {
  180. if (
  181. $method["token"] !== $_SESSION["token"] ||
  182. empty($method["token"]) ||
  183. empty($_SESSION["token"])
  184. ) {
  185. if ($return === true) {
  186. return false;
  187. } else {
  188. header("Location: /login/");
  189. die();
  190. }
  191. } else {
  192. return true;
  193. }
  194. }
  195. function show_alert_message($data) {
  196. $msgIcon = "";
  197. $msgText = "";
  198. $msgClass = "";
  199. if (!empty($data["error_msg"])) {
  200. $msgIcon = "fa-circle-exclamation";
  201. $msgText = htmlentities($data["error_msg"]);
  202. $msgClass = "inline-alert-danger";
  203. } elseif (!empty($data["ok_msg"])) {
  204. $msgIcon = "fa-circle-check";
  205. $msgText = $data["ok_msg"];
  206. $msgClass = "inline-alert-success";
  207. }
  208. if (!empty($msgText)) {
  209. printf(
  210. '<div class="inline-alert %s u-mb20" role="alert"><i class="fas %s"></i><p>%s</p></div>',
  211. $msgClass,
  212. $msgIcon,
  213. $msgText,
  214. );
  215. }
  216. }
  217. function top_panel($user, $TAB) {
  218. $command = HESTIA_CMD . "v-list-user " . $user . " 'json'";
  219. exec($command, $output, $return_var);
  220. if ($return_var > 0) {
  221. destroy_sessions();
  222. $_SESSION["error_msg"] = _("You are logged out, please log in again.");
  223. header("Location: /login/");
  224. exit();
  225. }
  226. $panel = json_decode(implode("", $output), true);
  227. unset($output);
  228. // Log out active sessions for suspended users
  229. if ($panel[$user]["SUSPENDED"] === "yes" && $_SESSION["POLICY_USER_VIEW_SUSPENDED"] !== "yes") {
  230. if (empty($_SESSION["look"])) {
  231. destroy_sessions();
  232. $_SESSION["error_msg"] = _("You are logged out, please log in again.");
  233. header("Location: /login/");
  234. }
  235. }
  236. // Reset user permissions if changed while logged in
  237. if ($panel[$user]["ROLE"] !== $_SESSION["userContext"] && !isset($_SESSION["look"])) {
  238. unset($_SESSION["userContext"]);
  239. $_SESSION["userContext"] = $panel[$user]["ROLE"];
  240. }
  241. // Load user's selected theme and do not change it when impersonting user
  242. if (isset($panel[$user]["THEME"]) && !isset($_SESSION["look"])) {
  243. $_SESSION["userTheme"] = $panel[$user]["THEME"];
  244. }
  245. // Unset userTheme override variable if POLICY_USER_CHANGE_THEME is set to no
  246. if ($_SESSION["POLICY_USER_CHANGE_THEME"] === "no") {
  247. unset($_SESSION["userTheme"]);
  248. }
  249. // Set preferred sort order
  250. if (!isset($_SESSION["look"])) {
  251. $_SESSION["userSortOrder"] = $panel[$user]["PREF_UI_SORT"];
  252. }
  253. // Set home location URLs
  254. if ($_SESSION["userContext"] === "admin" && empty($_SESSION["look"])) {
  255. // Display users list for administrators unless they are impersonating a user account
  256. $home_url = "/list/user/";
  257. } else {
  258. // Set home location URL based on available package features from account
  259. if ($panel[$user]["WEB_DOMAINS"] != "0") {
  260. $home_url = "/list/web/";
  261. } elseif ($panel[$user]["DNS_DOMAINS"] != "0") {
  262. $home_url = "/list/dns/";
  263. } elseif ($panel[$user]["MAIL_DOMAINS"] != "0") {
  264. $home_url = "/list/mail/";
  265. } elseif ($panel[$user]["DATABASES"] != "0") {
  266. $home_url = "/list/db/";
  267. } elseif ($panel[$user]["CRON_JOBS"] != "0") {
  268. $home_url = "/list/cron/";
  269. } elseif ($panel[$user]["BACKUPS"] != "0") {
  270. $home_url = "/list/backups/";
  271. }
  272. }
  273. include dirname(__FILE__) . "/../templates/includes/panel.php";
  274. return $panel;
  275. }
  276. function translate_date($date) {
  277. $date = new DateTime($date);
  278. return $date->format("d") . " " . _($date->format("M")) . " " . $date->format("Y");
  279. }
  280. function humanize_time($usage) {
  281. if ($usage > 60) {
  282. $usage = $usage / 60;
  283. if ($usage > 24) {
  284. $usage = $usage / 24;
  285. $usage = number_format($usage);
  286. return sprintf(ngettext("%d day", "%d days", $usage), $usage);
  287. } else {
  288. $usage = round($usage);
  289. return sprintf(ngettext("%d hour", "%d hours", $usage), $usage);
  290. }
  291. } else {
  292. $usage = round($usage);
  293. return sprintf(ngettext("%d minute", "%d minutes", $usage), $usage);
  294. }
  295. }
  296. function humanize_usage_size($usage, $round = 2) {
  297. if ($usage == "unlimited") {
  298. return "∞";
  299. }
  300. $display_usage = $usage;
  301. if ($usage > 1024) {
  302. $usage = $usage / 1024;
  303. if ($usage > 1024) {
  304. $usage = $usage / 1024;
  305. if ($usage > 1024) {
  306. $usage = $usage / 1024;
  307. $display_usage = number_format($usage, $round);
  308. } else {
  309. if ($usage > 999) {
  310. $usage = $usage / 1024;
  311. }
  312. $display_usage = number_format($usage, $round);
  313. }
  314. } else {
  315. if ($usage > 999) {
  316. $usage = $usage / 1024;
  317. }
  318. $display_usage = number_format($usage, $round);
  319. }
  320. } else {
  321. if ($usage > 999) {
  322. $usage = $usage / 1024;
  323. }
  324. $display_usage = number_format($usage, $round);
  325. }
  326. if (strlen($display_usage) > 4) {
  327. return number_format($usage, $round - 1);
  328. }
  329. return $display_usage;
  330. }
  331. function humanize_usage_measure($usage) {
  332. if ($usage == "unlimited") {
  333. return;
  334. }
  335. $measure = "kb";
  336. if ($usage > 1024) {
  337. $usage = $usage / 1024;
  338. if ($usage > 1024) {
  339. $usage = $usage / 1024;
  340. $measure = $usage < 1024 ? "tb" : "pb";
  341. if ($usage > 999) {
  342. $usage = $usage / 1024;
  343. $measure = "pb";
  344. }
  345. } else {
  346. $measure = $usage < 1024 ? "gb" : "tb";
  347. if ($usage > 999) {
  348. $usage = $usage / 1024;
  349. $measure = "tb";
  350. }
  351. }
  352. } else {
  353. $measure = $usage < 1024 ? "mb" : "gb";
  354. if ($usage > 999) {
  355. $measure = "gb";
  356. }
  357. }
  358. return $measure;
  359. }
  360. function get_percentage($used, $total) {
  361. if ($total = "unlimited") {
  362. //return 0 if unlimited
  363. return 0;
  364. }
  365. if (!isset($total)) {
  366. $total = 0;
  367. }
  368. if (!isset($used)) {
  369. $used = 0;
  370. }
  371. if ($total == 0) {
  372. $percent = 0;
  373. } else {
  374. $percent = $used / $total;
  375. $percent = $percent * 100;
  376. $percent = number_format($percent, 0, "", "");
  377. if ($percent < 0) {
  378. $percent = 0;
  379. } elseif ($percent > 100) {
  380. $percent = 100;
  381. }
  382. }
  383. return $percent;
  384. }
  385. function send_email($to, $subject, $mailtext, $from, $from_name, $to_name = "") {
  386. $mail = new PHPMailer();
  387. if (isset($_SESSION["USE_SERVER_SMTP"]) && $_SESSION["USE_SERVER_SMTP"] == "true") {
  388. if (!empty($_SESSION["SERVER_SMTP_ADDR"]) && $_SESSION["SERVER_SMTP_ADDR"] != "") {
  389. if (filter_var($_SESSION["SERVER_SMTP_ADDR"], FILTER_VALIDATE_EMAIL)) {
  390. $from = $_SESSION["SERVER_SMTP_ADDR"];
  391. }
  392. }
  393. $mail->IsSMTP();
  394. $mail->Mailer = "smtp";
  395. $mail->SMTPDebug = 0;
  396. $mail->SMTPAuth = true;
  397. $mail->SMTPSecure = $_SESSION["SERVER_SMTP_SECURITY"];
  398. $mail->Port = $_SESSION["SERVER_SMTP_PORT"];
  399. $mail->Host = $_SESSION["SERVER_SMTP_HOST"];
  400. $mail->Username = $_SESSION["SERVER_SMTP_USER"];
  401. $mail->Password = $_SESSION["SERVER_SMTP_PASSWD"];
  402. }
  403. $mail->IsHTML(true);
  404. $mail->ClearReplyTos();
  405. if (empty($to_name)) {
  406. $mail->AddAddress($to);
  407. } else {
  408. $mail->AddAddress($to, $to_name);
  409. }
  410. $mail->SetFrom($from, $from_name);
  411. $mail->CharSet = "utf-8";
  412. $mail->Subject = $subject;
  413. $content = $mailtext;
  414. $content = nl2br($content);
  415. $mail->MsgHTML($content);
  416. $mail->Send();
  417. }
  418. function list_timezones() {
  419. foreach (
  420. ["AKST", "AKDT", "PST", "PDT", "MST", "MDT", "CST", "CDT", "EST", "EDT", "AST", "ADT"]
  421. as $timezone
  422. ) {
  423. $tz = new DateTimeZone($timezone);
  424. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime());
  425. }
  426. foreach (DateTimeZone::listIdentifiers() as $timezone) {
  427. $tz = new DateTimeZone($timezone);
  428. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime());
  429. }
  430. foreach ($timezone_offsets as $timezone => $offset) {
  431. $offset_prefix = $offset < 0 ? "-" : "+";
  432. $offset_formatted = gmdate("H:i", abs($offset));
  433. $pretty_offset = "UTC{$offset_prefix}{$offset_formatted}";
  434. $c = new DateTime(gmdate("Y-M-d H:i:s"), new DateTimeZone("UTC"));
  435. $c->setTimezone(new DateTimeZone($timezone));
  436. $current_time = $c->format("H:i:s");
  437. $timezone_list[$timezone] = "$timezone [ $current_time ] {$pretty_offset}";
  438. #$timezone_list[$timezone] = "$timezone ${pretty_offset}";
  439. }
  440. return $timezone_list;
  441. }
  442. /**
  443. * A function that tells is it MySQL installed on the system, or it is MariaDB.
  444. *
  445. * Explanation:
  446. * $_SESSION['DB_SYSTEM'] has 'mysql' value even if MariaDB is installed, so you can't figure out is it really MySQL or it's MariaDB.
  447. * So, this function will make it clear.
  448. *
  449. * If MySQL is installed, function will return 'mysql' as a string.
  450. * If MariaDB is installed, function will return 'mariadb' as a string.
  451. *
  452. * Hint: if you want to check if PostgreSQL is installed - check value of $_SESSION['DB_SYSTEM']
  453. *
  454. * @return string
  455. */
  456. function is_it_mysql_or_mariadb() {
  457. exec(HESTIA_CMD . "v-list-sys-services json", $output, $return_var);
  458. $data = json_decode(implode("", $output), true);
  459. unset($output);
  460. $mysqltype = "mysql";
  461. if (isset($data["mariadb"])) {
  462. $mysqltype = "mariadb";
  463. }
  464. return $mysqltype;
  465. }
  466. function load_hestia_config() {
  467. // Check system configuration
  468. exec(HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  469. $data = json_decode(implode("", $output), true);
  470. $sys_arr = $data["config"];
  471. foreach ($sys_arr as $key => $value) {
  472. $_SESSION[$key] = $value;
  473. }
  474. }
  475. /**
  476. * Returns the list of all web domains from all users grouped by Backend Template used and owner
  477. *
  478. * @return array
  479. */
  480. function backendtpl_with_webdomains() {
  481. exec(HESTIA_CMD . "v-list-users json", $output, $return_var);
  482. $users = json_decode(implode("", $output), true);
  483. unset($output);
  484. $backend_list = [];
  485. foreach ($users as $user => $user_details) {
  486. exec(
  487. HESTIA_CMD . "v-list-web-domains " . quoteshellarg($user) . " json",
  488. $output,
  489. $return_var,
  490. );
  491. $domains = json_decode(implode("", $output), true);
  492. unset($output);
  493. foreach ($domains as $domain => $domain_details) {
  494. if (!empty($domain_details["BACKEND"])) {
  495. $backend = $domain_details["BACKEND"];
  496. $backend_list[$backend][$user][] = $domain;
  497. }
  498. }
  499. }
  500. return $backend_list;
  501. }
  502. /**
  503. * Check if password is valid
  504. *
  505. * @return int; 1 / 0
  506. */
  507. function validate_password($password) {
  508. return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(.){8,}$/', $password);
  509. }
  510. function unset_alerts() {
  511. if (!empty($_SESSION["unset_alerts"])) {
  512. if (!empty($_SESSION["error_msg"])) {
  513. unset($_SESSION["error_msg"]);
  514. }
  515. if (!empty($_SESSION["ok_msg"])) {
  516. unset($_SESSION["ok_msg"]);
  517. }
  518. unset($_SESSION["unset_alerts"]);
  519. }
  520. }
  521. register_shutdown_function("unset_alerts");