main.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. // Body
  111. if (($_SESSION['userContext'] != 'admin') && (@include($__template_dir . "user/$page.html"))) {
  112. // User page loaded
  113. } else {
  114. // Not admin or user page doesn't exist
  115. // Load admin page
  116. @include($__template_dir . "admin/$page.html");
  117. }
  118. // Including common js files
  119. @include_once(dirname(__DIR__) . '/templates/scripts.html');
  120. // Including page specific js file
  121. if(file_exists($__pages_js_dir.$page.'.js'))
  122. echo '<script type="text/javascript" src="/js/pages/'.$page.'.js?'.JS_LATEST_UPDATE.'"></script>';
  123. // Footer
  124. include($__template_dir . 'footer.html');
  125. }
  126. function top_panel($user, $TAB) {
  127. global $panel;
  128. $command = HESTIA_CMD."v-list-user ".escapeshellarg($user)." 'json'";
  129. exec ($command, $output, $return_var);
  130. if ( $return_var > 0 ) {
  131. header("Location: /logout/");
  132. exit;
  133. }
  134. $panel = json_decode(implode('', $output), true);
  135. unset($output);
  136. // Load user's selected theme and do not change it when impersonting user
  137. if ( (isset($panel[$user]['THEME'])) && (!isset($_SESSION['look']) )) {
  138. $_SESSION['userTheme'] = $panel[$user]['THEME'];
  139. }
  140. // Unset userTheme override variable if POLICY_USER_CHANGE_THEME is set to no
  141. if ($_SESSION['POLICY_USER_CHANGE_THEME'] === 'no') {
  142. unset($_SESSION['userTheme']);
  143. }
  144. // Set home location URLs
  145. if (($_SESSION['userContext'] === 'admin') && (!isset($_SESSION['look']))) {
  146. // Display users list for administrators unless they are impersonating a user account
  147. $home_url = "/list/user/";
  148. } else {
  149. // Set home location URL based on available package features from account
  150. if($panel[$user]['WEB_DOMAINS'] != "0") {
  151. $home_url = "/list/web/";
  152. } else if ($panel[$user]['DNS_DOMAINS'] != "0") {
  153. $home_url = "/list/dns/";
  154. } else if ($panel[$user]['MAIL_DOMAINS'] != "0") {
  155. $home_url = "/list/mail/";
  156. } else if ($panel[$user]['DATABASES'] != "0") {
  157. $home_url = "/list/db/";
  158. } else if ($panel[$user]['CRON_JOBS'] != "0") {
  159. $home_url = "/list/cron/";
  160. } else if ($panel[$user]['BACKUPS'] != "0") {
  161. $home_url = "/list/backups/";
  162. }
  163. }
  164. if (($_SESSION['userContext'] === 'admin')) {
  165. include(dirname(__FILE__).'/../templates/admin/panel.html');
  166. } else {
  167. include(dirname(__FILE__).'/../templates/user/panel.html');
  168. }
  169. }
  170. function translate_date($date){
  171. $date = strtotime($date);
  172. return strftime("%d &nbsp;", $date)._(strftime("%b", $date)).strftime(" &nbsp;%Y", $date);
  173. }
  174. function humanize_time($usage) {
  175. if ( $usage > 60 ) {
  176. $usage = $usage / 60;
  177. if ( $usage > 24 ) {
  178. $usage = $usage / 24;
  179. $usage = number_format($usage);
  180. if ( $usage == 1 ) {
  181. $usage = $usage." "._('day');
  182. } else {
  183. $usage = $usage." "._('days');
  184. }
  185. } else {
  186. $usage = number_format($usage);
  187. if ( $usage == 1 ) {
  188. $usage = $usage." "._('hour');
  189. } else {
  190. $usage = $usage." "._('hours');
  191. }
  192. }
  193. } else {
  194. if ( $usage == 1 ) {
  195. $usage = $usage." "._('minute');
  196. } else {
  197. $usage = $usage." "._('minutes');
  198. }
  199. }
  200. return $usage;
  201. }
  202. function humanize_usage_size($usage) {
  203. if ( $usage > 1024 ) {
  204. $usage = $usage / 1024;
  205. if ( $usage > 1024 ) {
  206. $usage = $usage / 1024 ;
  207. if ( $usage > 1024 ) {
  208. $usage = $usage / 1024 ;
  209. $usage = number_format($usage, 2);
  210. } else {
  211. $usage = number_format($usage, 2);
  212. }
  213. } else {
  214. $usage = number_format($usage, 2);
  215. }
  216. }
  217. return $usage;
  218. }
  219. function humanize_usage_measure($usage) {
  220. $measure = 'kb';
  221. if ( $usage > 1024 ) {
  222. $usage = $usage / 1024;
  223. if ( $usage > 1024 ) {
  224. $usage = $usage / 1024 ;
  225. if ( $usage > 1024 ) {
  226. $measure = 'pb';
  227. } else {
  228. $measure = 'tb';
  229. }
  230. } else {
  231. $measure = 'gb';
  232. }
  233. } else {
  234. $measure = 'mb';
  235. }
  236. return _($measure);
  237. }
  238. function get_percentage($used,$total) {
  239. if (!isset($total)) $total = 0;
  240. if (!isset($used)) $used = 0;
  241. if ( $total == 0 ) {
  242. $percent = 0;
  243. } else {
  244. $percent = $used / $total;
  245. $percent = $percent * 100;
  246. $percent = number_format($percent, 0, '', '');
  247. if ( $percent > 100 ) {
  248. $percent = 100;
  249. }
  250. if ( $percent < 0 ) {
  251. $percent = 0;
  252. }
  253. }
  254. return $percent;
  255. }
  256. function send_email($to,$subject,$mailtext,$from) {
  257. $charset = "utf-8";
  258. $to = '<'.$to.'>';
  259. $boundary = '--' . md5( uniqid("myboundary") );
  260. $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
  261. $priority = $priorities[2];
  262. $ctencoding = "8bit";
  263. $sep = chr(13) . chr(10);
  264. $disposition = "inline";
  265. $subject = "=?$charset?B?".base64_encode($subject)."?=";
  266. $header = "From: $from \nX-Priority: $priority\nCC:\n";
  267. $header .= "Mime-Version: 1.0\nContent-Type: text/plain; charset=$charset \n";
  268. $header .= "Content-Transfer-Encoding: $ctencoding\nX-Mailer: Php/libMailv1.3\n";
  269. $message = $mailtext;
  270. mail($to, $subject, $message, $header);
  271. }
  272. function list_timezones() {
  273. $tz = new DateTimeZone('AKST');
  274. $timezone_offsets['AKST'] = $tz->getOffset(new DateTime);
  275. $tz = new DateTimeZone('AKDT');
  276. $timezone_offsets['AKDT'] = $tz->getOffset(new DateTime);
  277. $tz = new DateTimeZone('PST');
  278. $timezone_offsets['PST'] = $tz->getOffset(new DateTime);
  279. $tz = new DateTimeZone('PDT');
  280. $timezone_offsets['PDT'] = $tz->getOffset(new DateTime);
  281. $tz = new DateTimeZone('MST');
  282. $timezone_offsets['MST'] = $tz->getOffset(new DateTime);
  283. $tz = new DateTimeZone('MDT');
  284. $timezone_offsets['MDT'] = $tz->getOffset(new DateTime);
  285. $tz = new DateTimeZone('CST');
  286. $timezone_offsets['CST'] = $tz->getOffset(new DateTime);
  287. $tz = new DateTimeZone('CDT');
  288. $timezone_offsets['CDT'] = $tz->getOffset(new DateTime);
  289. $tz = new DateTimeZone('EST');
  290. $timezone_offsets['EST'] = $tz->getOffset(new DateTime);
  291. $tz = new DateTimeZone('EDT');
  292. $timezone_offsets['EDT'] = $tz->getOffset(new DateTime);
  293. $tz = new DateTimeZone('AST');
  294. $timezone_offsets['AST'] = $tz->getOffset(new DateTime);
  295. $tz = new DateTimeZone('ADT');
  296. $timezone_offsets['ADT'] = $tz->getOffset(new DateTime);
  297. foreach(DateTimeZone::listIdentifiers() as $timezone){
  298. $tz = new DateTimeZone($timezone);
  299. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime);
  300. }
  301. foreach($timezone_offsets as $timezone => $offset){
  302. $offset_prefix = $offset < 0 ? '-' : '+';
  303. $offset_formatted = gmdate( 'H:i', abs($offset) );
  304. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  305. $t = new DateTimeZone($timezone);
  306. $c = new DateTime(null, $t);
  307. $current_time = $c->format('H:i:s');
  308. $timezone_list[$timezone] = "$timezone [ $current_time ] ${pretty_offset}";
  309. }
  310. return $timezone_list;
  311. }
  312. /**
  313. * A function that tells is it MySQL installed on the system, or it is MariaDB.
  314. *
  315. * Explaination:
  316. * $_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.
  317. * So, this function will make it clear.
  318. *
  319. * If MySQL is installed, function will return 'mysql' as a string.
  320. * If MariaDB is installed, function will return 'mariadb' as a string.
  321. *
  322. * Hint: if you want to check if PostgreSQL is installed - check value of $_SESSION['DB_SYSTEM']
  323. *
  324. * @return string
  325. */
  326. function is_it_mysql_or_mariadb() {
  327. exec (HESTIA_CMD."v-list-sys-services json", $output, $return_var);
  328. $data = json_decode(implode('', $output), true);
  329. unset($output);
  330. $mysqltype='mysql';
  331. if (isset($data['mariadb'])) $mysqltype='mariadb';
  332. return $mysqltype;
  333. }
  334. function load_hestia_config() {
  335. // Check system configuration
  336. exec (HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  337. $data = json_decode(implode('', $output), true);
  338. $sys_arr = $data['config'];
  339. foreach ($sys_arr as $key => $value) {
  340. $_SESSION[$key] = $value;
  341. }
  342. }
  343. /**
  344. * Returns the list of all web domains from all users grouped by Backend Template used and owner
  345. *
  346. * @return array
  347. */
  348. function backendtpl_with_webdomains() {
  349. exec (HESTIA_CMD . "v-list-users json", $output, $return_var);
  350. $users = json_decode(implode('', $output), true);
  351. unset($output);
  352. $backend_list=[];
  353. foreach ($users as $user => $user_details) {
  354. exec (HESTIA_CMD . "v-list-web-domains ". escapeshellarg($user) . " json", $output, $return_var);
  355. $domains = json_decode(implode('', $output), true);
  356. unset($output);
  357. foreach ($domains as $domain => $domain_details) {
  358. if (!empty($domain_details['BACKEND'])) {
  359. $backend = $domain_details['BACKEND'];
  360. $backend_list[$backend][$user][] = $domain;
  361. }
  362. }
  363. }
  364. return $backend_list;
  365. }
  366. /**
  367. * Check if password is valid
  368. *
  369. * @return int; 1 / 0
  370. */
  371. function validate_password($password){
  372. return preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(.){8,}$/', $password);
  373. }