main.php 16 KB

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