main.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. session_start();
  3. define('VESTA_CMD', '/usr/bin/sudo /usr/local/vesta/bin/');
  4. define('JS_LATEST_UPDATE', '1491697868');
  5. $i = 0;
  6. require_once(dirname(__FILE__).'/i18n.php');
  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($_SESSION['user_combined_ip'])){
  25. $_SESSION['user_combined_ip'] = $user_combined_ip;
  26. }
  27. // Checking user to use session from the same IP he has been logged in
  28. if($_SESSION['user_combined_ip'] != $user_combined_ip && $_SERVER['REMOTE_ADDR'] != '127.0.0.1'){
  29. session_destroy();
  30. session_start();
  31. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  32. header("Location: /login/");
  33. exit;
  34. }
  35. // Check system settings
  36. if ((!isset($_SESSION['VERSION'])) && (!defined('NO_AUTH_REQUIRED'))) {
  37. session_destroy();
  38. session_start();
  39. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  40. header("Location: /login/");
  41. exit;
  42. }
  43. // Check user session
  44. if ((!isset($_SESSION['user'])) && (!defined('NO_AUTH_REQUIRED'))) {
  45. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  46. header("Location: /login/");
  47. exit;
  48. }
  49. // Generate CSRF Token
  50. if (isset($_SESSION['user'])) {
  51. if (!isset($_SESSION['token'])){
  52. $token = bin2hex(file_get_contents('/dev/urandom', false, null, 0, 16));
  53. $_SESSION['token'] = $token;
  54. }
  55. }
  56. if (isset($_SESSION['language'])) {
  57. switch ($_SESSION['language']) {
  58. case 'ro':
  59. setlocale(LC_ALL, 'ro_RO.utf8');
  60. break;
  61. case 'ru':
  62. setlocale(LC_ALL, 'ru_RU.utf8');
  63. break;
  64. case 'ua':
  65. setlocale(LC_ALL, 'uk_UA.utf8');
  66. break;
  67. case 'es':
  68. setlocale(LC_ALL, 'es_ES.utf8');
  69. break;
  70. case 'ja':
  71. setlocale(LC_ALL, 'ja_JP.utf8');
  72. break;
  73. default:
  74. setlocale(LC_ALL, 'en_US.utf8');
  75. }
  76. }
  77. if (isset($_SESSION['user'])) {
  78. $user = $_SESSION['user'];
  79. }
  80. if (isset($_SESSION['look']) && ( $_SESSION['look'] != 'admin' )) {
  81. $user = $_SESSION['look'];
  82. }
  83. function get_favourites(){
  84. exec (VESTA_CMD."v-list-user-favourites ".$_SESSION['user']." json", $output, $return_var);
  85. // $data = json_decode(implode('', $output).'}', true);
  86. $data = json_decode(implode('', $output), true);
  87. $data = array_reverse($data,true);
  88. $favourites = array();
  89. foreach($data['Favourites'] as $key => $favourite){
  90. $favourites[$key] = array();
  91. $items = explode(',', $favourite);
  92. foreach($items as $item){
  93. if($item)
  94. $favourites[$key][trim($item)] = 1;
  95. }
  96. }
  97. $_SESSION['favourites'] = $favourites;
  98. }
  99. function check_error($return_var) {
  100. if ( $return_var > 0 ) {
  101. header("Location: /error/");
  102. exit;
  103. }
  104. }
  105. function check_return_code($return_var,$output) {
  106. if ($return_var != 0) {
  107. $error = implode('<br>', $output);
  108. if (empty($error)) $error = __('Error code:',$return_var);
  109. $_SESSION['error_msg'] = $error;
  110. }
  111. }
  112. function top_panel($user, $TAB) {
  113. global $panel;
  114. $command = VESTA_CMD."v-list-user '".$user."' 'json'";
  115. exec ($command, $output, $return_var);
  116. if ( $return_var > 0 ) {
  117. header("Location: /error/");
  118. exit;
  119. }
  120. $panel = json_decode(implode('', $output), true);
  121. unset($output);
  122. // getting notifications
  123. $command = VESTA_CMD."v-list-user-notifications '".$user."' 'json'";
  124. exec ($command, $output, $return_var);
  125. $notifications = json_decode(implode('', $output), true);
  126. foreach($notifications as $message){
  127. if($message['ACK'] == 'no'){
  128. $panel[$user]['NOTIFICATIONS'] = 'yes';
  129. break;
  130. }
  131. }
  132. unset($output);
  133. }
  134. function translate_date($date){
  135. $date = strtotime($date);
  136. return strftime("%d &nbsp;", $date).__(strftime("%b", $date)).strftime(" &nbsp;%Y", $date);
  137. }
  138. function humanize_time($usage) {
  139. if ( $usage > 60 ) {
  140. $usage = $usage / 60;
  141. if ( $usage > 24 ) {
  142. $usage = $usage / 24;
  143. $usage = number_format($usage);
  144. if ( $usage == 1 ) {
  145. $usage = $usage." ".__('day');
  146. } else {
  147. $usage = $usage." ".__('days');
  148. }
  149. } else {
  150. $usage = number_format($usage);
  151. if ( $usage == 1 ) {
  152. $usage = $usage." ".__('hour');
  153. } else {
  154. $usage = $usage." ".__('hours');
  155. }
  156. }
  157. } else {
  158. if ( $usage == 1 ) {
  159. $usage = $usage." ".__('minute');
  160. } else {
  161. $usage = $usage." ".__('minutes');
  162. }
  163. }
  164. return $usage;
  165. }
  166. function humanize_usage_size($usage) {
  167. if ( $usage > 1024 ) {
  168. $usage = $usage / 1024;
  169. if ( $usage > 1024 ) {
  170. $usage = $usage / 1024 ;
  171. if ( $usage > 1024 ) {
  172. $usage = $usage / 1024 ;
  173. $usage = number_format($usage, 2);
  174. } else {
  175. $usage = number_format($usage, 2);
  176. }
  177. } else {
  178. $usage = number_format($usage, 2);
  179. }
  180. }
  181. return $usage;
  182. }
  183. function humanize_usage_measure($usage) {
  184. $measure = 'kb';
  185. if ( $usage > 1024 ) {
  186. $usage = $usage / 1024;
  187. if ( $usage > 1024 ) {
  188. $usage = $usage / 1024 ;
  189. if ( $usage > 1024 ) {
  190. $measure = 'pb';
  191. } else {
  192. $measure = 'tb';
  193. }
  194. } else {
  195. $measure = 'gb';
  196. }
  197. } else {
  198. $measure = 'mb';
  199. }
  200. return __($measure);
  201. }
  202. function get_percentage($used,$total) {
  203. if (!isset($total)) $total = 0;
  204. if (!isset($used)) $used = 0;
  205. if ( $total == 0 ) {
  206. $percent = 0;
  207. } else {
  208. $percent = $used / $total;
  209. $percent = $percent * 100;
  210. $percent = number_format($percent, 0, '', '');
  211. if ( $percent > 100 ) {
  212. $percent = 100;
  213. }
  214. if ( $percent < 0 ) {
  215. $percent = 0;
  216. }
  217. }
  218. return $percent;
  219. }
  220. function send_email($to,$subject,$mailtext,$from) {
  221. $charset = "utf-8";
  222. $to = '<'.$to.'>';
  223. $boundary = '--' . md5( uniqid("myboundary") );
  224. $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
  225. $priority = $priorities[2];
  226. $ctencoding = "8bit";
  227. $sep = chr(13) . chr(10);
  228. $disposition = "inline";
  229. $subject = "=?$charset?B?".base64_encode($subject)."?=";
  230. $header = "From: $from \nX-Priority: $priority\nCC:\n";
  231. $header .= "Mime-Version: 1.0\nContent-Type: text/plain; charset=$charset \n";
  232. $header .= "Content-Transfer-Encoding: $ctencoding\nX-Mailer: Php/libMailv1.3\n";
  233. $message = $mailtext;
  234. mail($to, $subject, $message, $header);
  235. }
  236. function list_timezones() {
  237. $tz = new DateTimeZone('HAST');
  238. $timezone_offsets['HAST'] = $tz->getOffset(new DateTime);
  239. $tz = new DateTimeZone('HADT');
  240. $timezone_offsets['HADT'] = $tz->getOffset(new DateTime);
  241. $tz = new DateTimeZone('AKST');
  242. $timezone_offsets['AKST'] = $tz->getOffset(new DateTime);
  243. $tz = new DateTimeZone('AKDT');
  244. $timezone_offsets['AKDT'] = $tz->getOffset(new DateTime);
  245. $tz = new DateTimeZone('PST');
  246. $timezone_offsets['PST'] = $tz->getOffset(new DateTime);
  247. $tz = new DateTimeZone('PDT');
  248. $timezone_offsets['PDT'] = $tz->getOffset(new DateTime);
  249. $tz = new DateTimeZone('MST');
  250. $timezone_offsets['MST'] = $tz->getOffset(new DateTime);
  251. $tz = new DateTimeZone('MDT');
  252. $timezone_offsets['MDT'] = $tz->getOffset(new DateTime);
  253. $tz = new DateTimeZone('CST');
  254. $timezone_offsets['CST'] = $tz->getOffset(new DateTime);
  255. $tz = new DateTimeZone('CDT');
  256. $timezone_offsets['CDT'] = $tz->getOffset(new DateTime);
  257. $tz = new DateTimeZone('EST');
  258. $timezone_offsets['EST'] = $tz->getOffset(new DateTime);
  259. $tz = new DateTimeZone('EDT');
  260. $timezone_offsets['EDT'] = $tz->getOffset(new DateTime);
  261. $tz = new DateTimeZone('AST');
  262. $timezone_offsets['AST'] = $tz->getOffset(new DateTime);
  263. $tz = new DateTimeZone('ADT');
  264. $timezone_offsets['ADT'] = $tz->getOffset(new DateTime);
  265. foreach(DateTimeZone::listIdentifiers() as $timezone){
  266. $tz = new DateTimeZone($timezone);
  267. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime);
  268. }
  269. foreach($timezone_offsets as $timezone => $offset){
  270. $offset_prefix = $offset < 0 ? '-' : '+';
  271. $offset_formatted = gmdate( 'H:i', abs($offset) );
  272. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  273. $t = new DateTimeZone($timezone);
  274. $c = new DateTime(null, $t);
  275. $current_time = $c->format('H:i:s');
  276. $timezone_list[$timezone] = "$timezone [ $current_time ] ${pretty_offset}";
  277. }
  278. return $timezone_list;
  279. }
  280. /**
  281. * A function that tells is it MySQL installed on the system, or it is MariaDB.
  282. *
  283. * Explaination:
  284. * $_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.
  285. * So, this function will make it clear.
  286. *
  287. * If MySQL is installed, function will return 'mysql' as a string.
  288. * If MariaDB is installed, function will return 'mariadb' as a string.
  289. *
  290. * Hint: if you want to check if PostgreSQL is installed - check value of $_SESSION['DB_SYSTEM']
  291. *
  292. * @return string
  293. */
  294. function is_it_mysql_or_mariadb() {
  295. exec (VESTA_CMD."v-list-sys-services json", $output, $return_var);
  296. $data = json_decode(implode('', $output), true);
  297. unset($output);
  298. $mysqltype='mysql';
  299. if (isset($data['mariadb'])) $mysqltype='mariadb';
  300. return $mysqltype;
  301. }