main.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. session_start();
  3. define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/bin/');
  4. define('JS_LATEST_UPDATE', '1491697868');
  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. session_destroy();
  35. session_start();
  36. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  37. header("Location: /login/");
  38. exit;
  39. }
  40. // Load Hestia Config directly
  41. load_hestia_config();
  42. // Check system settings
  43. if ((!isset($_SESSION['VERSION'])) && (!defined('NO_AUTH_REQUIRED'))) {
  44. session_destroy();
  45. session_start();
  46. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  47. header("Location: /login/");
  48. exit;
  49. }
  50. // Check user session
  51. if ((!isset($_SESSION['user'])) && (!defined('NO_AUTH_REQUIRED'))) {
  52. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  53. header("Location: /login/");
  54. exit;
  55. }
  56. // Generate CSRF Token
  57. if (isset($_SESSION['user'])) {
  58. if(!isset($_SESSION['token'])){
  59. $token = bin2hex(file_get_contents('/dev/urandom', false, null, 0, 16));
  60. $_SESSION['token'] = $token;
  61. }
  62. }
  63. if (isset($_SESSION['user'])) {
  64. $user = $_SESSION['user'];
  65. }
  66. if (isset($_SESSION['look']) && ( $_SESSION['look'] != 'admin' )) {
  67. $user = $_SESSION['look'];
  68. }
  69. require_once(dirname(__FILE__).'/i18n.php');
  70. function check_error($return_var) {
  71. if ( $return_var > 0 ) {
  72. header("Location: /error/");
  73. exit;
  74. }
  75. }
  76. function check_return_code($return_var,$output) {
  77. if ($return_var != 0) {
  78. $error = implode('<br>', $output);
  79. if (empty($error)) $error = _('Error code:',$return_var);
  80. $_SESSION['error_msg'] = $error;
  81. }
  82. }
  83. function render_page($user, $TAB, $page) {
  84. $__template_dir = dirname(__DIR__) . '/templates/';
  85. $__pages_js_dir = dirname(__DIR__) . '/js/pages/';
  86. // Header
  87. include($__template_dir . 'header.html');
  88. // Panel
  89. top_panel(empty($_SESSION['look']) ? $_SESSION['user'] : $_SESSION['look'], $TAB);
  90. // Extarct global variables
  91. // I think those variables should be passed via arguments
  92. extract($GLOBALS, EXTR_SKIP);
  93. // Body
  94. if (($_SESSION['user'] !== 'admin') && (@include($__template_dir . "user/$page.html"))) {
  95. // User page loaded
  96. } else {
  97. // Not admin or user page doesn't exist
  98. // Load admin page
  99. @include($__template_dir . "admin/$page.html");
  100. }
  101. // Including common js files
  102. @include_once(dirname(__DIR__) . '/templates/scripts.html');
  103. // Including page specific js file
  104. if(file_exists($__pages_js_dir.$page.'.js'))
  105. echo '<script type="text/javascript" src="/js/pages/'.$page.'.js?'.JS_LATEST_UPDATE.'"></script>';
  106. // Footer
  107. include($__template_dir . 'footer.html');
  108. }
  109. function top_panel($user, $TAB) {
  110. global $panel;
  111. $command = HESTIA_CMD."v-list-user ".escapeshellarg($user)." 'json'";
  112. exec ($command, $output, $return_var);
  113. if ( $return_var > 0 ) {
  114. header("Location: /error/");
  115. exit;
  116. }
  117. $panel = json_decode(implode('', $output), true);
  118. unset($output);
  119. if ( $user == 'admin' ) {
  120. include(dirname(__FILE__).'/../templates/admin/panel.html');
  121. } else {
  122. include(dirname(__FILE__).'/../templates/user/panel.html');
  123. }
  124. }
  125. function translate_date($date){
  126. $date = strtotime($date);
  127. return strftime("%d &nbsp;", $date)._(strftime("%b", $date)).strftime(" &nbsp;%Y", $date);
  128. }
  129. function humanize_time($usage) {
  130. if ( $usage > 60 ) {
  131. $usage = $usage / 60;
  132. if ( $usage > 24 ) {
  133. $usage = $usage / 24;
  134. $usage = number_format($usage);
  135. if ( $usage == 1 ) {
  136. $usage = $usage." "._('day');
  137. } else {
  138. $usage = $usage." "._('days');
  139. }
  140. } else {
  141. $usage = number_format($usage);
  142. if ( $usage == 1 ) {
  143. $usage = $usage." "._('hour');
  144. } else {
  145. $usage = $usage." "._('hours');
  146. }
  147. }
  148. } else {
  149. if ( $usage == 1 ) {
  150. $usage = $usage." "._('minute');
  151. } else {
  152. $usage = $usage." "._('minutes');
  153. }
  154. }
  155. return $usage;
  156. }
  157. function humanize_usage_size($usage) {
  158. if ( $usage > 1024 ) {
  159. $usage = $usage / 1024;
  160. if ( $usage > 1024 ) {
  161. $usage = $usage / 1024 ;
  162. if ( $usage > 1024 ) {
  163. $usage = $usage / 1024 ;
  164. $usage = number_format($usage, 2);
  165. } else {
  166. $usage = number_format($usage, 2);
  167. }
  168. } else {
  169. $usage = number_format($usage, 2);
  170. }
  171. }
  172. return $usage;
  173. }
  174. function humanize_usage_measure($usage) {
  175. $measure = 'kb';
  176. if ( $usage > 1024 ) {
  177. $usage = $usage / 1024;
  178. if ( $usage > 1024 ) {
  179. $usage = $usage / 1024 ;
  180. if ( $usage > 1024 ) {
  181. $measure = 'pb';
  182. } else {
  183. $measure = 'tb';
  184. }
  185. } else {
  186. $measure = 'gb';
  187. }
  188. } else {
  189. $measure = 'mb';
  190. }
  191. return _($measure);
  192. }
  193. function get_percentage($used,$total) {
  194. if (!isset($total)) $total = 0;
  195. if (!isset($used)) $used = 0;
  196. if ( $total == 0 ) {
  197. $percent = 0;
  198. } else {
  199. $percent = $used / $total;
  200. $percent = $percent * 100;
  201. $percent = number_format($percent, 0, '', '');
  202. if ( $percent > 100 ) {
  203. $percent = 100;
  204. }
  205. if ( $percent < 0 ) {
  206. $percent = 0;
  207. }
  208. }
  209. return $percent;
  210. }
  211. function send_email($to,$subject,$mailtext,$from) {
  212. $charset = "utf-8";
  213. $to = '<'.$to.'>';
  214. $boundary = '--' . md5( uniqid("myboundary") );
  215. $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
  216. $priority = $priorities[2];
  217. $ctencoding = "8bit";
  218. $sep = chr(13) . chr(10);
  219. $disposition = "inline";
  220. $subject = "=?$charset?B?".base64_encode($subject)."?=";
  221. $header = "From: $from \nX-Priority: $priority\nCC:\n";
  222. $header .= "Mime-Version: 1.0\nContent-Type: text/plain; charset=$charset \n";
  223. $header .= "Content-Transfer-Encoding: $ctencoding\nX-Mailer: Php/libMailv1.3\n";
  224. $message = $mailtext;
  225. mail($to, $subject, $message, $header);
  226. }
  227. function list_timezones() {
  228. $tz = new DateTimeZone('AKST');
  229. $timezone_offsets['AKST'] = $tz->getOffset(new DateTime);
  230. $tz = new DateTimeZone('AKDT');
  231. $timezone_offsets['AKDT'] = $tz->getOffset(new DateTime);
  232. $tz = new DateTimeZone('PST');
  233. $timezone_offsets['PST'] = $tz->getOffset(new DateTime);
  234. $tz = new DateTimeZone('PDT');
  235. $timezone_offsets['PDT'] = $tz->getOffset(new DateTime);
  236. $tz = new DateTimeZone('MST');
  237. $timezone_offsets['MST'] = $tz->getOffset(new DateTime);
  238. $tz = new DateTimeZone('MDT');
  239. $timezone_offsets['MDT'] = $tz->getOffset(new DateTime);
  240. $tz = new DateTimeZone('CST');
  241. $timezone_offsets['CST'] = $tz->getOffset(new DateTime);
  242. $tz = new DateTimeZone('CDT');
  243. $timezone_offsets['CDT'] = $tz->getOffset(new DateTime);
  244. $tz = new DateTimeZone('EST');
  245. $timezone_offsets['EST'] = $tz->getOffset(new DateTime);
  246. $tz = new DateTimeZone('EDT');
  247. $timezone_offsets['EDT'] = $tz->getOffset(new DateTime);
  248. $tz = new DateTimeZone('AST');
  249. $timezone_offsets['AST'] = $tz->getOffset(new DateTime);
  250. $tz = new DateTimeZone('ADT');
  251. $timezone_offsets['ADT'] = $tz->getOffset(new DateTime);
  252. foreach(DateTimeZone::listIdentifiers() as $timezone){
  253. $tz = new DateTimeZone($timezone);
  254. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime);
  255. }
  256. foreach($timezone_offsets as $timezone => $offset){
  257. $offset_prefix = $offset < 0 ? '-' : '+';
  258. $offset_formatted = gmdate( 'H:i', abs($offset) );
  259. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  260. $t = new DateTimeZone($timezone);
  261. $c = new DateTime(null, $t);
  262. $current_time = $c->format('H:i:s');
  263. $timezone_list[$timezone] = "$timezone [ $current_time ] ${pretty_offset}";
  264. }
  265. return $timezone_list;
  266. }
  267. /**
  268. * A function that tells is it MySQL installed on the system, or it is MariaDB.
  269. *
  270. * Explaination:
  271. * $_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.
  272. * So, this function will make it clear.
  273. *
  274. * If MySQL is installed, function will return 'mysql' as a string.
  275. * If MariaDB is installed, function will return 'mariadb' as a string.
  276. *
  277. * Hint: if you want to check if PostgreSQL is installed - check value of $_SESSION['DB_SYSTEM']
  278. *
  279. * @return string
  280. */
  281. function is_it_mysql_or_mariadb() {
  282. exec (HESTIA_CMD."v-list-sys-services json", $output, $return_var);
  283. $data = json_decode(implode('', $output), true);
  284. unset($output);
  285. $mysqltype='mysql';
  286. if (isset($data['mariadb'])) $mysqltype='mariadb';
  287. return $mysqltype;
  288. }
  289. function load_hestia_config() {
  290. // Check system configuration
  291. exec (HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  292. $data = json_decode(implode('', $output), true);
  293. $sys_arr = $data['config'];
  294. foreach ($sys_arr as $key => $value) {
  295. $_SESSION[$key] = $value;
  296. }
  297. }
  298. /**
  299. * Returns the list of all web domains from all users grouped by Backend Template used and owner
  300. *
  301. * @return array
  302. */
  303. function backendtpl_with_webdomains() {
  304. exec (HESTIA_CMD . "v-list-users json", $output, $return_var);
  305. $users = json_decode(implode('', $output), true);
  306. unset($output);
  307. $backend_list=[];
  308. foreach ($users as $user => $user_details) {
  309. exec (HESTIA_CMD . "v-list-web-domains ". escapeshellarg($user) . " json", $output, $return_var);
  310. $domains = json_decode(implode('', $output), true);
  311. unset($output);
  312. foreach ($domains as $domain => $domain_details) {
  313. if (!empty($domain_details['BACKEND'])) {
  314. $backend = $domain_details['BACKEND'];
  315. $backend_list[$backend][$user][] = $domain;
  316. }
  317. }
  318. }
  319. return $backend_list;
  320. }