main.php 16 KB

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