main.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. // Check system settiongs
  3. if ((!isset($_SESSION['VERSION'])) && (!defined('NO_AUTH_REQUIRED'))) {
  4. session_destroy();
  5. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  6. header("Location: /login/");
  7. exit;
  8. }
  9. // Check user session
  10. if ((!isset($_SESSION['user'])) && (!defined('NO_AUTH_REQUIRED'))) {
  11. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  12. header("Location: /login/");
  13. exit;
  14. }
  15. if (isset($_SESSION['user'])) {
  16. require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$_SESSION['language'].'.php');
  17. }
  18. /**
  19. * Translates string by a given key in first parameter to current session language. Works like sprintf
  20. * @global array $LANG Associative array of language pharses
  21. * @return string Translated string
  22. * @see _translate()
  23. */
  24. function __() {
  25. $args = func_get_args();
  26. array_unshift($args,$_SESSION['language']);
  27. return call_user_func_array("_translate",$args);
  28. }
  29. /**
  30. * Translates string to given language in first parameter, key given in second parameter (dynamically loads required language). Works like spritf from second parameter
  31. * @global array $LANG Associative array of language pharses
  32. * @return string Translated string
  33. */
  34. function _translate() {
  35. global $LANG;
  36. $args = func_get_args();
  37. $l = $args[0];
  38. if (!$l) return 'NO LANGUAGE DEFINED';
  39. $key = $args[1];
  40. if (!isset($LANG[$l])) {
  41. require_once($_SERVER['DOCUMENT_ROOT'].'/inc/i18n/'.$l.'.php');
  42. }
  43. if (!isset($LANG[$l][$key])) {
  44. $text=$key;
  45. } else {
  46. $text=$LANG[$l][$key];
  47. }
  48. array_shift($args);
  49. if (count($args)>1) {
  50. $args[0] = $text;
  51. return call_user_func_array("sprintf",$args);
  52. } else {
  53. return $text;
  54. }
  55. }
  56. define('VESTA_CMD', '/usr/bin/sudo /usr/local/vesta/bin/');
  57. $i = 0;
  58. if (isset($_SESSION['language'])) {
  59. switch ($_SESSION['language']) {
  60. case 'ro':
  61. setlocale(LC_ALL, 'ro_RO.utf8');
  62. break;
  63. case 'ru':
  64. setlocale(LC_ALL, 'ru_RU.utf8');
  65. break;
  66. case 'ua':
  67. setlocale(LC_ALL, 'uk_UA.utf8');
  68. break;
  69. case 'es':
  70. setlocale(LC_ALL, 'es_ES.utf8');
  71. break;
  72. default:
  73. setlocale(LC_ALL, 'en_US.utf8');
  74. }
  75. }
  76. if (isset($_SESSION['user'])) {
  77. $user = $_SESSION['user'];
  78. }
  79. if (isset($_SESSION['look']) && ( $_SESSION['look'] != 'admin' )) {
  80. $user = $_SESSION['look'];
  81. }
  82. function check_error($return_var) {
  83. if ( $return_var > 0 ) {
  84. header("Location: /error/");
  85. exit;
  86. }
  87. }
  88. function check_return_code($return_var,$output) {
  89. if ($return_var != 0) {
  90. $error = implode('<br>', $output);
  91. if (empty($error)) $error = __('Error code:',$return_var);
  92. $_SESSION['error_msg'] = $error;
  93. }
  94. }
  95. function top_panel($user, $TAB) {
  96. global $panel;
  97. $command = VESTA_CMD."v-list-user '".$user."' 'json'";
  98. exec ($command, $output, $return_var);
  99. if ( $return_var > 0 ) {
  100. header("Location: /error/");
  101. exit;
  102. }
  103. $panel = json_decode(implode('', $output), true);
  104. unset($output);
  105. if ( $user == 'admin' ) {
  106. include($_SERVER['DOCUMENT_ROOT'].'/templates/admin/panel.html');
  107. } else {
  108. include($_SERVER['DOCUMENT_ROOT'].'/templates/user/panel.html');
  109. }
  110. }
  111. function humanize_time($usage) {
  112. if ( $usage > 60 ) {
  113. $usage = $usage / 60;
  114. if ( $usage > 24 ) {
  115. $usage = $usage / 24;
  116. $usage = number_format($usage);
  117. if ( $usage == 1 ) {
  118. $usage = $usage." ".__('day');
  119. } else {
  120. $usage = $usage." ".__('days');
  121. }
  122. } else {
  123. $usage = number_format($usage);
  124. if ( $usage == 1 ) {
  125. $usage = $usage." ".__('hour');
  126. } else {
  127. $usage = $usage." ".__('hours');
  128. }
  129. }
  130. } else {
  131. if ( $usage == 1 ) {
  132. $usage = $usage." ".__('minute');
  133. } else {
  134. $usage = $usage." ".__('minutes');
  135. }
  136. }
  137. return $usage;
  138. }
  139. function humanize_usage($usage) {
  140. if ( $usage > 1024 ) {
  141. $usage = $usage / 1024;
  142. if ( $usage > 1024 ) {
  143. $usage = $usage / 1024 ;
  144. if ( $usage > 1024 ) {
  145. $usage = $usage / 1024 ;
  146. $usage = number_format($usage, 2);
  147. $usage = $usage." ".__('pb');
  148. } else {
  149. $usage = number_format($usage, 2);
  150. $usage = $usage." ".__('tb');
  151. }
  152. } else {
  153. $usage = number_format($usage, 2);
  154. $usage = $usage." ".__('gb');
  155. }
  156. } else {
  157. $usage = $usage." ".__('mb');
  158. }
  159. return $usage;
  160. }
  161. function get_percentage($used,$total) {
  162. if (!isset($total)) $total = 0;
  163. if (!isset($used)) $used = 0;
  164. if ( $total == 0 ) {
  165. $percent = 0;
  166. } else {
  167. $percent = $used / $total;
  168. $percent = $percent * 100;
  169. $percent = number_format($percent, 0, '', '');
  170. if ( $percent > 100 ) {
  171. $percent = 100;
  172. }
  173. if ( $percent < 0 ) {
  174. $percent = 0;
  175. }
  176. }
  177. return $percent;
  178. }
  179. function send_email($to,$subject,$mailtext,$from) {
  180. $charset = "utf-8";
  181. $to = '<'.$to.'>';
  182. $boundary = '--' . md5( uniqid("myboundary") );
  183. $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
  184. $priority = $priorities[2];
  185. $ctencoding = "8bit";
  186. $sep = chr(13) . chr(10);
  187. $disposition = "inline";
  188. $subject = "=?$charset?B?".base64_encode($subject)."?=";
  189. $header = "From: $from \nX-Priority: $priority\nCC:\n";
  190. $header .= "Mime-Version: 1.0\nContent-Type: text/plain; charset=$charset \n";
  191. $header .= "Content-Transfer-Encoding: $ctencoding\nX-Mailer: Php/libMailv1.3\n";
  192. $message = $mailtext;
  193. mail($to, $subject, $message, $header);
  194. }
  195. function display_error_block() {
  196. if (!empty($_SESSION['error_msg'])) {
  197. echo '
  198. <div>
  199. <script type="text/javascript">
  200. $(function() {
  201. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  202. $( "#dialog-message" ).dialog({
  203. modal: true,
  204. buttons: {
  205. Ok: function() {
  206. $( this ).dialog( "close" );
  207. }
  208. }
  209. });
  210. });
  211. </script>
  212. <div id="dialog-message" title="">
  213. <p>'. $_SESSION['error_msg'] .'</p>
  214. </div>
  215. </div>'."\n";
  216. unset($_SESSION['error_msg']);
  217. }
  218. }
  219. function list_timezones() {
  220. $tz = new DateTimeZone('HAST');
  221. $timezone_offsets['HAST'] = $tz->getOffset(new DateTime);
  222. $tz = new DateTimeZone('HADT');
  223. $timezone_offsets['HADT'] = $tz->getOffset(new DateTime);
  224. $tz = new DateTimeZone('AKST');
  225. $timezone_offsets['AKST'] = $tz->getOffset(new DateTime);
  226. $tz = new DateTimeZone('AKDT');
  227. $timezone_offsets['AKDT'] = $tz->getOffset(new DateTime);
  228. $tz = new DateTimeZone('PST');
  229. $timezone_offsets['PST'] = $tz->getOffset(new DateTime);
  230. $tz = new DateTimeZone('PDT');
  231. $timezone_offsets['PDT'] = $tz->getOffset(new DateTime);
  232. $tz = new DateTimeZone('MST');
  233. $timezone_offsets['MST'] = $tz->getOffset(new DateTime);
  234. $tz = new DateTimeZone('MDT');
  235. $timezone_offsets['MDT'] = $tz->getOffset(new DateTime);
  236. $tz = new DateTimeZone('CST');
  237. $timezone_offsets['CST'] = $tz->getOffset(new DateTime);
  238. $tz = new DateTimeZone('CDT');
  239. $timezone_offsets['CDT'] = $tz->getOffset(new DateTime);
  240. $tz = new DateTimeZone('EST');
  241. $timezone_offsets['EST'] = $tz->getOffset(new DateTime);
  242. $tz = new DateTimeZone('EDT');
  243. $timezone_offsets['EDT'] = $tz->getOffset(new DateTime);
  244. $tz = new DateTimeZone('AST');
  245. $timezone_offsets['AST'] = $tz->getOffset(new DateTime);
  246. $tz = new DateTimeZone('ADT');
  247. $timezone_offsets['ADT'] = $tz->getOffset(new DateTime);
  248. foreach(DateTimeZone::listIdentifiers() as $timezone){
  249. $tz = new DateTimeZone($timezone);
  250. $timezone_offsets[$timezone] = $tz->getOffset(new DateTime);
  251. }
  252. foreach($timezone_offsets as $timezone => $offset){
  253. $offset_prefix = $offset < 0 ? '-' : '+';
  254. $offset_formatted = gmdate( 'H:i', abs($offset) );
  255. $pretty_offset = "UTC${offset_prefix}${offset_formatted}";
  256. $t = new DateTimeZone($timezone);
  257. $c = new DateTime(null, $t);
  258. $current_time = $c->format('H:i:s');
  259. $timezone_list[$timezone] = "$timezone [ $current_time ] ${pretty_offset}";
  260. }
  261. return $timezone_list;
  262. }
  263. ?>