main.php 12 KB

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