main.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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_error_panel($data)
  191. {
  192. $msg_id = '';
  193. $msg_icon = '';
  194. $msg_text = '';
  195. if (!empty($data['error_msg'])) {
  196. $msg_icon = 'fa-exclamation-circle status-icon red';
  197. $msg_text = htmlentities($data['error_msg']);
  198. $msg_id = 'vst-error';
  199. } else {
  200. if (!empty($data['ok_msg'])) {
  201. $msg_icon = 'fa-check-circle status-icon green';
  202. $msg_text = $data['ok_msg'];
  203. $msg_id = 'vst-ok';
  204. }
  205. } ?>
  206. <span class="<?=$msg_id; ?>"> <i class="fas <?=$msg_icon; ?>"></i> <?=$msg_text; ?></span>
  207. <?php
  208. }
  209. function top_panel($user, $TAB)
  210. {
  211. $command = HESTIA_CMD . 'v-list-user ' . $user . " 'json'";
  212. exec($command, $output, $return_var);
  213. if ($return_var > 0) {
  214. destroy_sessions();
  215. $_SESSION['error_msg'] = _('You have been logged out. Please log in again.');
  216. header('Location: /login/');
  217. exit;
  218. }
  219. $panel = json_decode(implode('', $output), true);
  220. unset($output);
  221. // Log out active sessions for suspended users
  222. if (($panel[$user]['SUSPENDED'] === 'yes') && ($_SESSION['POLICY_USER_VIEW_SUSPENDED'] !== 'yes')) {
  223. if (empty($_SESSION['look'])) {
  224. destroy_sessions();
  225. $_SESSION['error_msg'] = _('You have been logged out. Please log in again.');
  226. header('Location: /login/');
  227. }
  228. }
  229. // Reset user permissions if changed while logged in
  230. if (($panel[$user]['ROLE']) !== ($_SESSION['userContext']) && (!isset($_SESSION['look']))) {
  231. unset($_SESSION['userContext']);
  232. $_SESSION['userContext'] = $panel[$user]['ROLE'];
  233. }
  234. // Load user's selected theme and do not change it when impersonting user
  235. if ((isset($panel[$user]['THEME'])) && (!isset($_SESSION['look']))) {
  236. $_SESSION['userTheme'] = $panel[$user]['THEME'];
  237. }
  238. // Unset userTheme override variable if POLICY_USER_CHANGE_THEME is set to no
  239. if ($_SESSION['POLICY_USER_CHANGE_THEME'] === 'no') {
  240. unset($_SESSION['userTheme']);
  241. }
  242. // Set preferred sort order
  243. if (!isset($_SESSION['look'])) {
  244. $_SESSION['userSortOrder'] = $panel[$user]['PREF_UI_SORT'];
  245. }
  246. // Set home location URLs
  247. if (($_SESSION['userContext'] === 'admin') && (empty($_SESSION['look']))) {
  248. // Display users list for administrators unless they are impersonating a user account
  249. $home_url = '/list/user/';
  250. } else {
  251. // Set home location URL based on available package features from account
  252. if ($panel[$user]['WEB_DOMAINS'] != '0') {
  253. $home_url = '/list/web/';
  254. } elseif ($panel[$user]['DNS_DOMAINS'] != '0') {
  255. $home_url = '/list/dns/';
  256. } elseif ($panel[$user]['MAIL_DOMAINS'] != '0') {
  257. $home_url = '/list/mail/';
  258. } elseif ($panel[$user]['DATABASES'] != '0') {
  259. $home_url = '/list/db/';
  260. } elseif ($panel[$user]['CRON_JOBS'] != '0') {
  261. $home_url = '/list/cron/';
  262. } elseif ($panel[$user]['BACKUPS'] != '0') {
  263. $home_url = '/list/backups/';
  264. }
  265. }
  266. include(dirname(__FILE__) . '/../templates/includes/panel.html');
  267. return $panel;
  268. }
  269. function translate_date($date)
  270. {
  271. $date = new DateTime($date);
  272. return $date -> format('d').' '. _($date -> format('M')).' '.$date -> format('Y');
  273. }
  274. function humanize_time($usage)
  275. {
  276. if ($usage > 60) {
  277. $usage = $usage / 60;
  278. if ($usage > 24) {
  279. $usage = $usage / 24;
  280. $usage = number_format($usage);
  281. return sprintf(ngettext('%d day', '%d days', $usage), $usage);
  282. } else {
  283. $usage = round($usage);
  284. return sprintf(ngettext('%d hour', '%d hours', $usage), $usage);
  285. }
  286. } else {
  287. $usage = round($usage);
  288. return sprintf(ngettext('%d minute', '%d minutes', $usage), $usage);
  289. }
  290. }
  291. function humanize_usage_size($usage)
  292. {
  293. if ($usage == 'unlimited') {
  294. return '∞';
  295. }
  296. if ($usage > 1024) {
  297. $usage = $usage / 1024;
  298. if ($usage > 1024) {
  299. $usage = $usage / 1024 ;
  300. if ($usage > 1024) {
  301. $usage = $usage / 1024 ;
  302. $usage = number_format($usage, 2);
  303. } else {
  304. $usage = number_format($usage, 2);
  305. }
  306. } else {
  307. $usage = number_format($usage, 2);
  308. }
  309. }
  310. return $usage;
  311. }
  312. function humanize_usage_measure($usage)
  313. {
  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. {
  333. if ($total = "unlimited") {
  334. //return 0 if unlimited
  335. return 0;
  336. }
  337. if (!isset($total)) {
  338. $total = 0;
  339. }
  340. if (!isset($used)) {
  341. $used = 0;
  342. }
  343. if ($total == 0) {
  344. $percent = 0;
  345. } else {
  346. $percent = $used / $total;
  347. $percent = $percent * 100;
  348. $percent = number_format($percent, 0, '', '');
  349. if ($percent < 0) {
  350. $percent = 0;
  351. } elseif ($percent > 100) {
  352. $percent = 100;
  353. }
  354. }
  355. return $percent;
  356. }
  357. function send_email($to, $subject, $mailtext, $from, $from_name, $to_name = '')
  358. {
  359. $mail = new PHPMailer();
  360. if (isset($_SESSION['USE_SERVER_SMTP']) && $_SESSION['USE_SERVER_SMTP'] == "true") {
  361. $from = $_SESSION['SERVER_SMTP_ADDR'];
  362. $mail->IsSMTP();
  363. $mail->Mailer = "smtp";
  364. $mail->SMTPDebug = 0;
  365. $mail->SMTPAuth = true;
  366. $mail->SMTPSecure = $_SESSION['SERVER_SMTP_SECURITY'];
  367. $mail->Port = $_SESSION['SERVER_SMTP_PORT'];
  368. $mail->Host = $_SESSION['SERVER_SMTP_HOST'];
  369. $mail->Username = $_SESSION['SERVER_SMTP_USER'];
  370. $mail->Password = $_SESSION['SERVER_SMTP_PASSWD'];
  371. }
  372. $mail->IsHTML(true);
  373. $mail->ClearReplyTos();
  374. if (empty($to_name)) {
  375. $mail->AddAddress($to);
  376. } else {
  377. $mail->AddAddress($to, $to_name);
  378. }
  379. $mail->SetFrom($from, $from_name);
  380. $mail->CharSet = "utf-8";
  381. $mail->Subject = $subject;
  382. $content = $mailtext;
  383. $content = nl2br($content);
  384. $mail->MsgHTML($content);
  385. $mail->Send();
  386. }
  387. function list_timezones()
  388. {
  389. foreach (['AKST', 'AKDT', 'PST', 'PDT', 'MST', 'MDT', 'CST', 'CDT', 'EST', 'EDT', 'AST', 'ADT'] as $timezone) {
  390. $tz = new DateTimeZone($timezone);
  391. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime());
  392. }
  393. foreach (DateTimeZone::listIdentifiers() as $timezone) {
  394. $tz = new DateTimeZone($timezone);
  395. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime());
  396. }
  397. foreach ($timezone_offsets as $timezone => $offset) {
  398. $offset_prefix = $offset < 0 ? '-' : '+';
  399. $offset_formatted = gmdate('H:i', abs($offset));
  400. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  401. $c = new DateTime(gmdate('Y-M-d H:i:s'), new DateTimeZone('UTC'));
  402. $c->setTimezone(new DateTimeZone($timezone));
  403. $current_time = $c->format('H:i:s');
  404. $timezone_list[$timezone] = "$timezone [ $current_time ] ${pretty_offset}";
  405. #$timezone_list[$timezone] = "$timezone ${pretty_offset}";
  406. }
  407. return $timezone_list;
  408. }
  409. /**
  410. * A function that tells is it MySQL installed on the system, or it is MariaDB.
  411. *
  412. * Explaination:
  413. * $_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.
  414. * So, this function will make it clear.
  415. *
  416. * If MySQL is installed, function will return 'mysql' as a string.
  417. * If MariaDB is installed, function will return 'mariadb' as a string.
  418. *
  419. * Hint: if you want to check if PostgreSQL is installed - check value of $_SESSION['DB_SYSTEM']
  420. *
  421. * @return string
  422. */
  423. function is_it_mysql_or_mariadb()
  424. {
  425. exec(HESTIA_CMD . 'v-list-sys-services json', $output, $return_var);
  426. $data = json_decode(implode('', $output), true);
  427. unset($output);
  428. $mysqltype = 'mysql';
  429. if (isset($data['mariadb'])) {
  430. $mysqltype = 'mariadb';
  431. }
  432. return $mysqltype;
  433. }
  434. function load_hestia_config()
  435. {
  436. // Check system configuration
  437. exec(HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  438. $data = json_decode(implode('', $output), true);
  439. $sys_arr = $data['config'];
  440. foreach ($sys_arr as $key => $value) {
  441. $_SESSION[$key] = $value;
  442. }
  443. }
  444. /**
  445. * Returns the list of all web domains from all users grouped by Backend Template used and owner
  446. *
  447. * @return array
  448. */
  449. function backendtpl_with_webdomains()
  450. {
  451. exec(HESTIA_CMD . 'v-list-users json', $output, $return_var);
  452. $users = json_decode(implode('', $output), true);
  453. unset($output);
  454. $backend_list=[];
  455. foreach ($users as $user => $user_details) {
  456. exec(HESTIA_CMD . 'v-list-web-domains '. quoteshellarg($user) . ' json', $output, $return_var);
  457. $domains = json_decode(implode('', $output), true);
  458. unset($output);
  459. foreach ($domains as $domain => $domain_details) {
  460. if (!empty($domain_details['BACKEND'])) {
  461. $backend = $domain_details['BACKEND'];
  462. $backend_list[$backend][$user][] = $domain;
  463. }
  464. }
  465. }
  466. return $backend_list;
  467. }
  468. /**
  469. * Check if password is valid
  470. *
  471. * @return int; 1 / 0
  472. */
  473. function validate_password($password)
  474. {
  475. return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(.){8,}$/', $password);
  476. }