main.php 15 KB

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