main.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. session_start();
  3. define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/bin/');
  4. define('JS_LATEST_UPDATE', time());
  5. define('DEFAULT_PHP_VERSION', "php-" . exec('php -r "echo (float)phpversion();"'));
  6. $i = 0;
  7. // Saving user IPs to the session for preventing session hijacking
  8. $user_combined_ip = $_SERVER['REMOTE_ADDR'];
  9. if (isset($_SERVER['HTTP_CLIENT_IP'])){
  10. $user_combined_ip .= '|'. $_SERVER['HTTP_CLIENT_IP'];
  11. }
  12. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
  13. $user_combined_ip .= '|'. $_SERVER['HTTP_X_FORWARDED_FOR'];
  14. }
  15. if (isset($_SERVER['HTTP_FORWARDED_FOR'])){
  16. $user_combined_ip .= '|'. $_SERVER['HTTP_FORWARDED_FOR'];
  17. }
  18. if (isset($_SERVER['HTTP_X_FORWARDED'])){
  19. $user_combined_ip .= '|'. $_SERVER['HTTP_X_FORWARDED'];
  20. }
  21. if (isset($_SERVER['HTTP_FORWARDED'])){
  22. $user_combined_ip .= '|'. $_SERVER['HTTP_FORWARDED'];
  23. }
  24. if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])){
  25. if(!empty($_SERVER['HTTP_CF_CONNECTING_IP'])){
  26. $user_combined_ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
  27. }
  28. }
  29. if (!isset($_SESSION['user_combined_ip'])){
  30. $_SESSION['user_combined_ip'] = $user_combined_ip;
  31. }
  32. // Checking user to use session from the same IP he has been logged in
  33. if ($_SESSION['user_combined_ip'] != $user_combined_ip && $_SERVER['REMOTE_ADDR'] != '127.0.0.1'){
  34. $v_user = escapeshellarg($_SESSION['user']);
  35. $v_session_id = escapeshellarg($_SESSION['token']);
  36. exec(HESTIA_CMD."v-log-user-logout ".$v_user." ".$v_session_id, $output, $return_var);
  37. session_destroy();
  38. session_start();
  39. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  40. header("Location: /login/");
  41. exit;
  42. }
  43. // Load Hestia Config directly
  44. load_hestia_config();
  45. // Check system settings
  46. if ((!isset($_SESSION['VERSION'])) && (!defined('NO_AUTH_REQUIRED'))) {
  47. session_destroy();
  48. session_start();
  49. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  50. header("Location: /login/");
  51. exit;
  52. }
  53. // Check user session
  54. if ((!isset($_SESSION['user'])) && (!defined('NO_AUTH_REQUIRED'))) {
  55. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  56. header("Location: /login/");
  57. exit;
  58. }
  59. // Generate CSRF Token
  60. if (isset($_SESSION['user'])) {
  61. if(!isset($_SESSION['token'])){
  62. $token = bin2hex(file_get_contents('/dev/urandom', false, null, 0, 16));
  63. $_SESSION['token'] = $token;
  64. }
  65. }
  66. if (!defined('NO_AUTH_REQUIRED')){
  67. if (empty($_SESSION['LAST_ACTIVITY']) || empty($_SESSION['INACTIVE_SESSION_TIMEOUT'])){
  68. session_destroy();
  69. header("Location: /login/");
  70. } else if ($_SESSION['INACTIVE_SESSION_TIMEOUT'] * 60 + $_SESSION['LAST_ACTIVITY'] < time()) {
  71. $v_user = escapeshellarg($_SESSION['user']);
  72. $v_session_id = escapeshellarg($_SESSION['token']);
  73. exec(HESTIA_CMD."v-log-user-logout ".$v_user." ".$v_session_id, $output, $return_var);
  74. session_destroy();
  75. header("Location: /login/");
  76. } else {
  77. $_SESSION['LAST_ACTIVITY'] = time();
  78. }
  79. }
  80. if (isset($_SESSION['user'])) {
  81. $user = $_SESSION['user'];
  82. }
  83. if (isset($_SESSION['look']) && ($_SESSION['userContext'] === 'admin')) {
  84. $user = $_SESSION['look'];
  85. }
  86. require_once(dirname(__FILE__).'/i18n.php');
  87. function check_error($return_var) {
  88. if ( $return_var > 0 ) {
  89. header("Location: /error/");
  90. exit;
  91. }
  92. }
  93. function check_return_code($return_var,$output) {
  94. if ($return_var != 0) {
  95. $error = implode('<br>', $output);
  96. if (empty($error)) $error = sprintf(_('Error code:'),$return_var);
  97. $_SESSION['error_msg'] = $error;
  98. }
  99. }
  100. function render_page($user, $TAB, $page) {
  101. $__template_dir = dirname(__DIR__) . '/templates/';
  102. $__pages_js_dir = dirname(__DIR__) . '/js/pages/';
  103. // Header
  104. include($__template_dir . 'header.html');
  105. // Panel
  106. top_panel(empty($_SESSION['look']) ? $_SESSION['user'] : $_SESSION['look'], $TAB);
  107. // Extarct global variables
  108. // I think those variables should be passed via arguments
  109. extract($GLOBALS, EXTR_SKIP);
  110. // Policies controller
  111. @include_once(dirname(__DIR__) . '/inc/policies.php');
  112. // Body
  113. include($__template_dir . "pages/$page.html");
  114. // Including common js files
  115. @include_once(dirname(__DIR__) . '/templates/includes/end_js.html');
  116. // Including page specific js file
  117. if(file_exists($__pages_js_dir.$page.'.js'))
  118. echo '<script type="text/javascript" src="/js/pages/'.$page.'.js?'.JS_LATEST_UPDATE.'"></script>';
  119. // Footer
  120. include($__template_dir . 'footer.html');
  121. }
  122. function top_panel($user, $TAB) {
  123. global $panel;
  124. $command = HESTIA_CMD."v-list-user ".escapeshellarg($user)." 'json'";
  125. exec ($command, $output, $return_var);
  126. if ( $return_var > 0 ) {
  127. echo "<span style='font-size: 18px;'><b>ERROR: Unable to retrieve account details.</b><br>Please <b><a href='/login/'>log in</a></b> again.</span>";
  128. session_destroy();
  129. header("Location: /login/");
  130. exit;
  131. }
  132. $panel = json_decode(implode('', $output), true);
  133. unset($output);
  134. // Log out active sessions for suspended users
  135. if (($panel[$user]['SUSPENDED'] === 'yes') && ($_SESSION['POLICY_USER_VIEW_SUSPENDED'] !== 'yes')) {
  136. $_SESSION['error_msg'] = "You have been logged out. Please log in again.";
  137. session_destroy();
  138. header("Location: /login/");
  139. }
  140. // Reset user permissions if changed while logged in
  141. if (($panel[$user]['ROLE']) !== ($_SESSION['userContext']) && (!isset($_SESSION['look']))) {
  142. unset($_SESSION['userContext']);
  143. $_SESSION['userContext'] = $panel[$user]['ROLE'];
  144. }
  145. // Load user's selected theme and do not change it when impersonting user
  146. if ( (isset($panel[$user]['THEME'])) && (!isset($_SESSION['look']) )) {
  147. $_SESSION['userTheme'] = $panel[$user]['THEME'];
  148. }
  149. // Unset userTheme override variable if POLICY_USER_CHANGE_THEME is set to no
  150. if ($_SESSION['POLICY_USER_CHANGE_THEME'] === 'no') {
  151. unset($_SESSION['userTheme']);
  152. }
  153. // Set preferred sort order
  154. $_SESSION['userSortOrder'] = $panel[$user]['PREF_UI_SORT'];
  155. // Set home location URLs
  156. if (($_SESSION['userContext'] === 'admin') && (!isset($_SESSION['look']))) {
  157. // Display users list for administrators unless they are impersonating a user account
  158. $home_url = "/list/user/";
  159. } else {
  160. // Set home location URL based on available package features from account
  161. if($panel[$user]['WEB_DOMAINS'] != "0") {
  162. $home_url = "/list/web/";
  163. } else if ($panel[$user]['DNS_DOMAINS'] != "0") {
  164. $home_url = "/list/dns/";
  165. } else if ($panel[$user]['MAIL_DOMAINS'] != "0") {
  166. $home_url = "/list/mail/";
  167. } else if ($panel[$user]['DATABASES'] != "0") {
  168. $home_url = "/list/db/";
  169. } else if ($panel[$user]['CRON_JOBS'] != "0") {
  170. $home_url = "/list/cron/";
  171. } else if ($panel[$user]['BACKUPS'] != "0") {
  172. $home_url = "/list/backups/";
  173. }
  174. }
  175. include(dirname(__FILE__).'/../templates/includes/panel.html');
  176. }
  177. function translate_date($date){
  178. $date = strtotime($date);
  179. return strftime("%d &nbsp;", $date)._(strftime("%b", $date)).strftime(" &nbsp;%Y", $date);
  180. }
  181. function humanize_time($usage) {
  182. if ( $usage > 60 ) {
  183. $usage = $usage / 60;
  184. if ( $usage > 24 ) {
  185. $usage = $usage / 24;
  186. $usage = number_format($usage);
  187. if ( $usage == 1 ) {
  188. $usage = $usage." "._('day');
  189. } else {
  190. $usage = $usage." "._('days');
  191. }
  192. } else {
  193. $usage = number_format($usage);
  194. if ( $usage == 1 ) {
  195. $usage = $usage." "._('hour');
  196. } else {
  197. $usage = $usage." "._('hours');
  198. }
  199. }
  200. } else {
  201. if ( $usage == 1 ) {
  202. $usage = $usage." "._('minute');
  203. } else {
  204. $usage = $usage." "._('minutes');
  205. }
  206. }
  207. return $usage;
  208. }
  209. function humanize_usage_size($usage) {
  210. if ( $usage > 1024 ) {
  211. $usage = $usage / 1024;
  212. if ( $usage > 1024 ) {
  213. $usage = $usage / 1024 ;
  214. if ( $usage > 1024 ) {
  215. $usage = $usage / 1024 ;
  216. $usage = number_format($usage, 2);
  217. } else {
  218. $usage = number_format($usage, 2);
  219. }
  220. } else {
  221. $usage = number_format($usage, 2);
  222. }
  223. }
  224. return $usage;
  225. }
  226. function humanize_usage_measure($usage) {
  227. $measure = 'kb';
  228. if ( $usage > 1024 ) {
  229. $usage = $usage / 1024;
  230. if ( $usage > 1024 ) {
  231. $usage = $usage / 1024 ;
  232. if ( $usage > 1024 ) {
  233. $measure = 'pb';
  234. } else {
  235. $measure = 'tb';
  236. }
  237. } else {
  238. $measure = 'gb';
  239. }
  240. } else {
  241. $measure = 'mb';
  242. }
  243. return _($measure);
  244. }
  245. function get_percentage($used,$total) {
  246. if (!isset($total)) $total = 0;
  247. if (!isset($used)) $used = 0;
  248. if ( $total == 0 ) {
  249. $percent = 0;
  250. } else {
  251. $percent = $used / $total;
  252. $percent = $percent * 100;
  253. $percent = number_format($percent, 0, '', '');
  254. if ( $percent > 100 ) {
  255. $percent = 100;
  256. }
  257. if ( $percent < 0 ) {
  258. $percent = 0;
  259. }
  260. }
  261. return $percent;
  262. }
  263. function send_email($to,$subject,$mailtext,$from) {
  264. $charset = "utf-8";
  265. $to = '<'.$to.'>';
  266. $boundary = '--' . md5( uniqid("myboundary") );
  267. $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
  268. $priority = $priorities[2];
  269. $ctencoding = "8bit";
  270. $sep = chr(13) . chr(10);
  271. $disposition = "inline";
  272. $subject = "=?$charset?B?".base64_encode($subject)."?=";
  273. $header = "From: $from \nX-Priority: $priority\nCC:\n";
  274. $header .= "Mime-Version: 1.0\nContent-Type: text/plain; charset=$charset \n";
  275. $header .= "Content-Transfer-Encoding: $ctencoding\nX-Mailer: Php/libMailv1.3\n";
  276. $message = $mailtext;
  277. mail($to, $subject, $message, $header);
  278. }
  279. function list_timezones() {
  280. $tz = new DateTimeZone('AKST');
  281. $timezone_offsets['AKST'] = $tz->getOffset(new DateTime);
  282. $tz = new DateTimeZone('AKDT');
  283. $timezone_offsets['AKDT'] = $tz->getOffset(new DateTime);
  284. $tz = new DateTimeZone('PST');
  285. $timezone_offsets['PST'] = $tz->getOffset(new DateTime);
  286. $tz = new DateTimeZone('PDT');
  287. $timezone_offsets['PDT'] = $tz->getOffset(new DateTime);
  288. $tz = new DateTimeZone('MST');
  289. $timezone_offsets['MST'] = $tz->getOffset(new DateTime);
  290. $tz = new DateTimeZone('MDT');
  291. $timezone_offsets['MDT'] = $tz->getOffset(new DateTime);
  292. $tz = new DateTimeZone('CST');
  293. $timezone_offsets['CST'] = $tz->getOffset(new DateTime);
  294. $tz = new DateTimeZone('CDT');
  295. $timezone_offsets['CDT'] = $tz->getOffset(new DateTime);
  296. $tz = new DateTimeZone('EST');
  297. $timezone_offsets['EST'] = $tz->getOffset(new DateTime);
  298. $tz = new DateTimeZone('EDT');
  299. $timezone_offsets['EDT'] = $tz->getOffset(new DateTime);
  300. $tz = new DateTimeZone('AST');
  301. $timezone_offsets['AST'] = $tz->getOffset(new DateTime);
  302. $tz = new DateTimeZone('ADT');
  303. $timezone_offsets['ADT'] = $tz->getOffset(new DateTime);
  304. foreach(DateTimeZone::listIdentifiers() as $timezone){
  305. $tz = new DateTimeZone($timezone);
  306. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime);
  307. }
  308. foreach($timezone_offsets as $timezone => $offset){
  309. $offset_prefix = $offset < 0 ? '-' : '+';
  310. $offset_formatted = gmdate( 'H:i', abs($offset) );
  311. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  312. $t = new DateTimeZone($timezone);
  313. $c = new DateTime(null, $t);
  314. $current_time = $c->format('H:i:s');
  315. $timezone_list[$timezone] = "$timezone [ $current_time ] ${pretty_offset}";
  316. }
  317. return $timezone_list;
  318. }
  319. /**
  320. * A function that tells is it MySQL installed on the system, or it is MariaDB.
  321. *
  322. * Explaination:
  323. * $_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.
  324. * So, this function will make it clear.
  325. *
  326. * If MySQL is installed, function will return 'mysql' as a string.
  327. * If MariaDB is installed, function will return 'mariadb' as a string.
  328. *
  329. * Hint: if you want to check if PostgreSQL is installed - check value of $_SESSION['DB_SYSTEM']
  330. *
  331. * @return string
  332. */
  333. function is_it_mysql_or_mariadb() {
  334. exec (HESTIA_CMD."v-list-sys-services json", $output, $return_var);
  335. $data = json_decode(implode('', $output), true);
  336. unset($output);
  337. $mysqltype='mysql';
  338. if (isset($data['mariadb'])) $mysqltype='mariadb';
  339. return $mysqltype;
  340. }
  341. function load_hestia_config() {
  342. // Check system configuration
  343. exec (HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  344. $data = json_decode(implode('', $output), true);
  345. $sys_arr = $data['config'];
  346. foreach ($sys_arr as $key => $value) {
  347. $_SESSION[$key] = $value;
  348. }
  349. }
  350. /**
  351. * Returns the list of all web domains from all users grouped by Backend Template used and owner
  352. *
  353. * @return array
  354. */
  355. function backendtpl_with_webdomains() {
  356. exec (HESTIA_CMD . "v-list-users json", $output, $return_var);
  357. $users = json_decode(implode('', $output), true);
  358. unset($output);
  359. $backend_list=[];
  360. foreach ($users as $user => $user_details) {
  361. exec (HESTIA_CMD . "v-list-web-domains ". escapeshellarg($user) . " json", $output, $return_var);
  362. $domains = json_decode(implode('', $output), true);
  363. unset($output);
  364. foreach ($domains as $domain => $domain_details) {
  365. if (!empty($domain_details['BACKEND'])) {
  366. $backend = $domain_details['BACKEND'];
  367. $backend_list[$backend][$user][] = $domain;
  368. }
  369. }
  370. }
  371. return $backend_list;
  372. }
  373. /**
  374. * Check if password is valid
  375. *
  376. * @return int; 1 / 0
  377. */
  378. function validate_password($password){
  379. return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(.){8,}$/', $password);
  380. }