main.php 16 KB

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