main.php 13 KB

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