main.php 16 KB

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