acpsystem.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. if(!DEFINED('EGP'))
  3. exit(header('Refresh: 0; URL=http://'.$_SERVER['SERVER_NAME'].'/404'));
  4. class sys
  5. {
  6. public static function url($all = true)
  7. {
  8. if($_SERVER['REQUEST_URI'] == '/acp/')
  9. return $all ? NULL : 'index';
  10. $url = array();
  11. $string = str_replace('//', '/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
  12. $aUrl = explode('/', trim($string, ' /'));
  13. array_shift($aUrl);
  14. if(!$all)
  15. return $aUrl[0];
  16. unset($aUrl[0]);
  17. $i = 1;
  18. $m = count($aUrl)+1;
  19. for($i; $i < $m; $i+=1)
  20. $url[$aUrl[$i]] = isset($aUrl[++$i]) ? $aUrl[$i] : true;
  21. return $url;
  22. }
  23. public static function int($data, $width = false)
  24. {
  25. if($width)
  26. return preg_replace("([^0-9]{0, ".$width."})", '', $data);
  27. return preg_replace("([^0-9])", '', $data);
  28. }
  29. public static function first($array = array())
  30. {
  31. return $array[0];
  32. }
  33. public static function b64js($data)
  34. {
  35. return base64_encode(json_encode($data));
  36. }
  37. public static function b64djs($data)
  38. {
  39. return json_decode(base64_decode($data), true);
  40. }
  41. public static function outjs($val, $cache = false)
  42. {
  43. global $mcache;
  44. if($cache)
  45. $mcache->delete($cache);
  46. die(json_encode($val));
  47. }
  48. public static function out($val = '', $cache = false)
  49. {
  50. global $mcache;
  51. if($cache)
  52. $mcache->delete($cache);
  53. die(''.$val.'');
  54. }
  55. public static function valid($val, $type, $preg = '')
  56. {
  57. $val = isset($val) ? $val : '';
  58. switch($type)
  59. {
  60. case 'promo':
  61. if(!preg_match("/^[A-Za-z0-9]{2,20}$/", $val))
  62. return true;
  63. return false;
  64. case 'en':
  65. if(!preg_match("/^[A-Za-z0-9]$/", $val))
  66. return true;
  67. return false;
  68. case 'ru':
  69. if(!preg_match("/^[А-Яа-я]$/u", $val))
  70. return true;
  71. return false;
  72. case 'wm':
  73. if(!preg_match('/^R[0-9]{12,12}$|^Z[0-9]{12,12}$|^U[0-9]{12,12}$/m', $val))
  74. return true;
  75. return false;
  76. case 'ip':
  77. if(!preg_match("/^(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])(\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[0-9]{2}|[0-9])){3}$/", $val))
  78. return true;
  79. return false;
  80. case 'steamid':
  81. if(!preg_match("/^STEAM_[0-9]:[0-9]:[0-9]{6,12}$|^HLTV$|^STEAM_ID_LAN$|^STEAM_ID_PENDING$|^VALVE_ID_LAN$|^VALVE_ID_PENDING$|^STEAM_666:88:666$/", $val))
  82. return true;
  83. return false;
  84. case 'steamid3':
  85. if(!preg_match("/^\[U:[01]:[0-9]{3,12}\]$/i", $val))
  86. return true;
  87. return false;
  88. case 'num':
  89. if(!preg_match('/[^0-9]/', $val))
  90. return true;
  91. return false;
  92. case 'md5':
  93. if(!preg_match("/^[a-z0-9]{32,32}$/", $val))
  94. return true;
  95. return false;
  96. case 'other':
  97. if(!preg_match($preg, $val))
  98. return true;
  99. return false;
  100. }
  101. return true;
  102. }
  103. public static function page($page, $nums, $num)
  104. {
  105. $ceil = ceil($nums/$num);
  106. if($page > $ceil)
  107. $page = $ceil;
  108. $next = $page*$num;
  109. if($next <= $nums)
  110. $next = $next-$num;
  111. if($next > $nums)
  112. $next = $next-$num;
  113. if($next < 1)
  114. $next = 0;
  115. $num_go = $next;
  116. if($page == '')
  117. $page = 1;
  118. $aPage = array(
  119. 'page' => $page,
  120. 'num' => $num_go,
  121. 'ceil' => $ceil
  122. );
  123. return $aPage;
  124. }
  125. public static function page_list($countnum, $actnum)
  126. {
  127. if($countnum == 0 || $countnum == 1)
  128. return array();
  129. if($countnum > 10)
  130. {
  131. if($actnum <= 4 || $actnum + 3 >= $countnum)
  132. {
  133. for($i = 0; $i <= 4; $i++)
  134. $numlist[$i] = $i + 1;
  135. $numlist[5] = '...';
  136. for($j = 6, $k = 4; $j <= 10; $j+=1, $k-=1)
  137. $numlist[$j] = $countnum - $k;
  138. }else{
  139. $numlist[0] = 1;
  140. $numlist[1] = 2;
  141. $numlist[2] = '...';
  142. $numlist[3] = $actnum - 2;
  143. $numlist[4] = $actnum - 1;
  144. $numlist[5] = $actnum;
  145. $numlist[6] = $actnum + 1;
  146. $numlist[7] = $actnum + 2;
  147. $numlist[8] = '...';
  148. $numlist[9] = $countnum - 1;
  149. $numlist[10] = $countnum;
  150. }
  151. }else
  152. for($n = 0; $n < $countnum; $n+=1)
  153. $numlist[$n] = $n + 1;
  154. return $numlist;
  155. }
  156. public static function page_gen($ceil, $page, $actnum, $section)
  157. {
  158. global $cfg, $html;
  159. $aNum = sys::page_list($ceil, $actnum);
  160. $pages = '';
  161. $html->get('pages');
  162. if($ceil)
  163. {
  164. if($page != 1)
  165. {
  166. $next = $page-1;
  167. $pages .= '<a href="'.$cfg['http'].$section.'/page/'.$next.'"><i class="fa fa-angle-double-left"></i></a>';
  168. }
  169. foreach($aNum as $v)
  170. {
  171. if($v != $page && $v != '...')
  172. $pages .= '<a href="'.$cfg['http'].$section.'/page/'.$v.'">'.$v.'</a>';
  173. if($v == $page)
  174. $pages .= '<a href="#" onclick="return false" class="active">'.$v.'</a>';
  175. if($v == '...')
  176. $pages .= '<a href="#" onclick="return false">...</a>';
  177. }
  178. if($ceil > $page)
  179. {
  180. if($page < $ceil)
  181. {
  182. $next = $page+1;
  183. $pages .= '<a href="'.$cfg['http'].$section.'/page/'.$next.'"><i class="fa fa-angle-double-right"></i></a>';
  184. }else
  185. $pages .= '<a href="#" onclick="return false;"><i class="fa fa-angle-double-right"></i></a>';
  186. }
  187. }
  188. $html->set('pages', $pages);
  189. $html->pack('pages');
  190. return NULL;
  191. }
  192. public static function ago($time, $brackets = false)
  193. {
  194. global $start_point;
  195. $diff = $start_point-$time;
  196. if($diff < 0)
  197. return '';
  198. if(!$diff)
  199. $diff = 1;
  200. $seconds = array('секунду', 'секунды', 'секунд');
  201. $minutes = array('минуту', 'минуты', 'минут');
  202. $hours = array('час', 'часа', 'часов');
  203. $days = array('день', 'дня', 'дней');
  204. $weeks = array('неделю', 'недели', 'недель');
  205. $months = array('месяц', 'месяца', 'месяцев');
  206. $years = array('год', 'года', 'лет');
  207. $phrase = array($seconds, $minutes, $hours, $days, $weeks, $months, $years);
  208. $length = array(1, 60, 3600, 86400, 604800, 2630880, 31570560);
  209. for($i = 6; ($i >= 0) AND (($no = $diff/$length[$i]) <= 1); $i-=1);
  210. if($i < 0)
  211. $i = 0;
  212. $_time = $start_point-($diff % $length[$i]);
  213. $no = ceil($no);
  214. if($brackets)
  215. return '('.$no.' '.sys::parse_ago($no, $phrase[$i]).' назад)';
  216. return $no.' '.sys::parse_ago($no, $phrase[$i]).' назад';
  217. }
  218. private static function parse_ago($number, $titles)
  219. {
  220. $cases = array(2, 0, 1, 1, 1, 2);
  221. return $titles[($number % 100 > 4 AND $number % 100 < 20 ) ? 2 : $cases[min($number % 10, 5)]];
  222. }
  223. public static function date($lenght, $date)
  224. {
  225. global $start_point;
  226. $check_time = $date-$start_point;
  227. if($check_time < 1)
  228. return 'время истекло.';
  229. $days = floor($check_time/86400);
  230. $hours = floor(($check_time%86400)/3600);
  231. $minutes = floor(($check_time%3600)/60);
  232. $seconds = $check_time%60;
  233. $adata = array(
  234. 'min' => array(
  235. 'days' => array('день', 'дня', 'дней'),
  236. 'hours' => array('ч.', 'ч.', 'ч.'),
  237. 'minutes' => array('мин.', 'мин.', 'мин.'),
  238. 'seconds' => array('сек.', 'сек.', 'сек.')
  239. ),
  240. 'max' => array(
  241. 'days' => array('день', 'дня', 'дней'),
  242. 'hours' => array('час', 'часа', 'часов'),
  243. 'minutes' => array('минуту','минуты','минут'),
  244. 'seconds' => array('секунду','секунды','секунд')
  245. )
  246. );
  247. $text = '';
  248. if($days > 0)
  249. $text .= sys::date_decl($days, $adata[$lenght]['days']);
  250. if($days < 1 AND $hours > 0)
  251. $text .= ' '.sys::date_decl($hours, $adata[$lenght]['hours']);
  252. if($days < 1 AND $minutes > 0)
  253. $text .= ' '.sys::date_decl($minutes, $adata[$lenght]['minutes']);
  254. if($days < 1 AND $seconds > 0)
  255. $text .= ' '.sys::date_decl($seconds, $adata[$lenght]['seconds']);
  256. return $text;
  257. }
  258. public static function date_decl($digit, $expr, $onlyword = false)
  259. {
  260. if(!is_array($expr))
  261. $expr = array_filter(explode(' ', $expr));
  262. if(empty($expr[2]))
  263. $expr[2] = $expr[1];
  264. $i = sys::int($digit)%100;
  265. if($onlyword)
  266. $digit = '';
  267. if($i > 4 AND $i < 21)
  268. $res = $digit.' '.$expr[2];
  269. else
  270. $i%=10;
  271. if($i == 1)
  272. $res = $digit.' '.$expr[0];
  273. elseif($i > 1 AND $i < 5)
  274. $res = $digit.' '.$expr[1];
  275. else
  276. $res = $digit.' '.$expr[2];
  277. return trim($res);
  278. }
  279. public static function today($time, $cp = false)
  280. {
  281. global $start_point;
  282. $today = date('d.m.Y', $start_point);
  283. $day = date('d.m.Y', $time);
  284. if($day == $today)
  285. {
  286. if($cp)
  287. return 'Сегодня '.date('H:i', $time);
  288. return 'Сегодня '.date('- H:i', $time);
  289. }
  290. $yesterday_first = sys::int(sys::first(explode('.', $today)))-1;
  291. $yesterday_full = date('m.Y', $time);
  292. if($day == $yesterday_first.'.'.$yesterday_full AND !$yesterday_first)
  293. {
  294. if($cp)
  295. return 'Вчера '.date('H:i', $time);
  296. return 'Вчера '.date('- H:i', $time);
  297. }
  298. if($cp)
  299. return date('d.m.Y H:i', $time);
  300. return date('d.m.Y - H:i', $time);
  301. }
  302. public static function browser($agent)
  303. {
  304. if(strpos($agent, 'Firefox') !== false)
  305. return 'Mozilla Firefox';
  306. if(strpos($agent, 'Opera') !== false)
  307. return 'Opera';
  308. if(strpos($agent, 'Chrome') !== false)
  309. return 'Google Chrome';
  310. if(strpos($agent, 'MSIE') !== false)
  311. return 'Internet Explorer';
  312. if(strpos($agent, 'Safari') !== false)
  313. return 'Safari';
  314. return 'Неизвестный';
  315. }
  316. public static function whois($ip)
  317. {
  318. $stack = fsockopen('whois.ripe.net', 43, $errno, $errstr);
  319. if(!$stack)
  320. return 'не определена';
  321. fputs($stack, $ip."\r\n");
  322. $subnetwork = '';
  323. while(!feof($stack))
  324. {
  325. $str = fgets($stack, 128);
  326. if(strpos($str, 'route:') !== FALSE)
  327. {
  328. $subnetwork = trim(str_replace('route:', '', $str));
  329. break;
  330. }
  331. }
  332. fclose($stack);
  333. return isset($subnetwork[0]) ? $subnetwork : 'не определена';
  334. }
  335. public static function uptime_load($time)
  336. {
  337. $uptime = '';
  338. $day = floor($time/60/60/24);
  339. if($day)
  340. $uptime .= $day.'д. ';
  341. $hour = (int)($time/60/60)%24;
  342. if($hour)
  343. $uptime .= $hour.'ч. ';
  344. $min = (int)($time/60)%60;
  345. if($min)
  346. $uptime .= $min.'м. ';
  347. return $uptime.($time%60).'с.';
  348. }
  349. public static function ram_load($data)
  350. {
  351. $aData = explode(' ', $data);
  352. return ceil(($aData[0]-($aData[1]+$aData[2]+$aData[3]))*100/$aData[0]);
  353. }
  354. public static function cpu_load($data)
  355. {
  356. $aData = explode(' ', $data);
  357. $load = ceil($aData[0]/$aData[1]);
  358. return $load > 100 ? 100 : $load;
  359. }
  360. public static function cpu_idle($pros_stat = array(), $fcpu = false)
  361. {
  362. return sys::cpu_get_idle(sys::parse_cpu($pros_stat[0]), sys::parse_cpu($pros_stat[1]), $fcpu);
  363. }
  364. public static function cpu_get_idle($first, $second, $fcpu)
  365. {
  366. if(count($first) !== count($second))
  367. return;
  368. $cpus = array();
  369. for($i = 0, $l = count($first); $i < $l; $i+=1)
  370. {
  371. $dif = array();
  372. $dif['use'] = $second[$i]['use']-$first[$i]['use'];
  373. $dif['nice'] = $second[$i]['nice']-$first[$i]['nice'];
  374. $dif['sys'] = $second[$i]['sys']-$first[$i]['sys'];
  375. $dif['idle'] = $second[$i]['idle']-$first[$i]['idle'];
  376. $total = array_sum($dif);
  377. $cpu = array();
  378. foreach($dif as $x => $y)
  379. $cpu[$x] = $y ? round($y/$total*100, 1) : 0;
  380. $cpus['cpu'.$i] = $cpu;
  381. }
  382. if($fcpu)
  383. return $cpus;
  384. $threads = array();
  385. $l = count($first);
  386. for($i = 0; $i < $l; $i+=1)
  387. $threads[$i] = $cpus['cpu'.$i]['idle'];
  388. if(count($first) > 1)
  389. unset($threads[0]);
  390. $max = max($threads);
  391. return array_search($max, $threads);
  392. }
  393. public static function parse_cpu($data)
  394. {
  395. $data = explode("\n", $data);
  396. $cpu = array();
  397. foreach($data as $line)
  398. {
  399. if(preg_match('/^cpu[0-9]/', $line))
  400. {
  401. $info = explode(' ', $line);
  402. $cpu[] = array(
  403. 'use' => $info[1],
  404. 'nice' => $info[2],
  405. 'sys' => $info[3],
  406. 'idle' => $info[4]
  407. );
  408. }
  409. }
  410. return $cpu;
  411. }
  412. public static function checkdate($time)
  413. {
  414. $time = explode(' ', $time);
  415. if(count($time) != 2)
  416. sys::outjs(array('e' => 'Указанная дата неправильная.'));
  417. $aDate = explode('/', $time[0]);
  418. $aTime = explode(':', $time[1]);
  419. if(!isset($aDate[1], $aDate[0], $aDate[2]) || !checkdate($aDate[1], $aDate[0], $aDate[2]))
  420. sys::outjs(array('e' => 'Указанная дата неправильная.'));
  421. return mktime($aTime[0], $aTime[1], 0, $aDate[1], $aDate[0], $aDate[2]);
  422. }
  423. public static function passwdkey($passwd)
  424. {
  425. return md5(sha1($passwd));
  426. }
  427. public static function mail($name, $text, $mail)
  428. {
  429. global $cfg;
  430. require_once(LIB.'smtp.php');
  431. $tpl = file_get_contents(DATA.'mail.ini', "r");
  432. $text = str_replace(
  433. array('[name]', '[text]', '[http]', '[img]', '[css]'),
  434. array($cfg['name'], $text, $cfg['http'], $cfg['http'].'template/images/', $cfg['http'].'template/css/'),
  435. $tpl
  436. );
  437. $smtp = new smtp($cfg['smtp_login'], $cfg['smtp_passwd'], $cfg['smtp_url'], $cfg['smtp_mail'], 465);
  438. $headers = "MIME-Version: 1.0\r\n";
  439. $headers .= "Content-type: text/html; charset=utf-8\r\n";
  440. $headers .= "From: ".$cfg['smtp_name']." <".$cfg['smtp_mail'].">\r\n";
  441. if($smtp->send($mail, $name, $text, $headers))
  442. return true;
  443. return false;
  444. }
  445. public static function country($address)
  446. {
  447. global $SxGeo;
  448. if(sys::valid($address, 'ip'))
  449. return 'не определена';
  450. $data = $SxGeo->getCityFull($address);
  451. return $data['country']['name_ru'] != '' ? $data['country']['name_ru'] : 'не определена';
  452. }
  453. public static function ipproxy()
  454. {
  455. global $_SERVER;
  456. if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !empty($_SERVER['HTTP_CF_CONNECTING_IP']))
  457. return $_SERVER['HTTP_CF_CONNECTING_IP'];
  458. return NULL;
  459. }
  460. public static function ip()
  461. {
  462. $ip = sys::ipproxy();
  463. if(sys::valid($ip, 'ip'))
  464. return $_SERVER['REMOTE_ADDR'];
  465. return $ip;
  466. }
  467. public static function status($data)
  468. {
  469. if(strpos($data, 'is running') || strpos($data, '(running)'))
  470. return true;
  471. return false;
  472. }
  473. public static function strlen($str)
  474. {
  475. return iconv_strlen($str, 'UTF-8');
  476. }
  477. public static function bbc($text)
  478. {
  479. global $cfg;
  480. $lines = explode("\n", $text);
  481. $str_search = array(
  482. "#\[spoiler\](.+?)\[\/spoiler\]#is",
  483. "#\[sp\](.+?)\[\/sp\]#is",
  484. "#\[b\](.+?)\[\/b\]#is",
  485. "#\[u\](.+?)\[\/u\]#is",
  486. "#\[code\](.+?)\[\/code\]#is",
  487. "#<code>(.+?)<\/code>#isUe",
  488. "#\[quote\](.+?)\[\/quote\]#is",
  489. "#\[url=(.+?)\](.+?)\[\/url\]#is",
  490. "#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is"
  491. );
  492. $str_replace = array(
  493. "<div><b class='spoiler'>Посмотреть содержимое</b><div class='spoiler_main'>\\1</div></div>",
  494. "<div><b class='spoiler'>Посмотреть содержимое</b><div class='spoiler_main'>\\1</div></div>",
  495. "<b>\\1</b>",
  496. "<u>\\1</u>",
  497. "<div><b class='spoiler'>Посмотреть содержимое</b><div class='spoiler_main'><pre><code>\\1</code></pre></div></div>",
  498. "'<code>'.htmlspecialchars('$1').'</code>'",
  499. "<blockquote><p>\\1</p></blockquote>",
  500. "<a href='\\1'>\\2</a>",
  501. "<a href='\\2'>\\2</a>"
  502. );
  503. $uptext = '';
  504. foreach($lines as $line)
  505. $uptext .= preg_replace($str_search, $str_replace, $line).PHP_EOL;
  506. return $uptext;
  507. }
  508. public static function text($section, $name)
  509. {
  510. global $cfg, $user;
  511. $group = isset($user['group']) ? $user['group'] : 'user';
  512. if($section != 'error' || !$cfg['text_group'])
  513. $group = 'all';
  514. include(DATA.'text/'.$section.'.php');
  515. return isset($text[$name][$group]) ? $text[$name][$group] : $text[$name];
  516. }
  517. public static function updtext($text, $data)
  518. {
  519. foreach($data as $name => $val)
  520. $text = str_replace('['.$name.']', $val, $text);
  521. return $text;
  522. }
  523. }
  524. ?>