system.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  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 isSecure() {
  7. $is_secure = false;
  8. if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
  9. $is_secure = true;
  10. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
  11. $is_secure = true;
  12. }
  13. return $is_secure;
  14. }
  15. public static function url($all = true)
  16. {
  17. if($_SERVER['REQUEST_URI'] == '/')
  18. return $all ? NULL : 'index';
  19. $url = array();
  20. $string = str_replace('//', '/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
  21. $aUrl = explode('/', trim($string, ' /'));
  22. if(!$all)
  23. return $aUrl[0];
  24. unset($aUrl[0]);
  25. $i = 1;
  26. $m = count($aUrl)+1;
  27. for($i; $i < $m; $i+=1)
  28. $url[$aUrl[$i]] = isset($aUrl[++$i]) ? $aUrl[$i] : true;
  29. return $url;
  30. }
  31. public static function user($user)
  32. {
  33. global $sql, $start_point;
  34. if($user['time']+10 < $start_point)
  35. $sql->query('UPDATE `users` set `time`="'.$start_point.'" WHERE `id`="'.$user['id'].'" LIMIT 1');
  36. return NULL;
  37. }
  38. public static function users($users, $user, $authkey, $del = false)
  39. {
  40. global $mcache;
  41. if($del)
  42. unset($users[md5($user['login'].$user['authkey'].$user['passwd'])]);
  43. else
  44. $users[md5($user['login'].$user['authkey'].$user['passwd'])] = $user;
  45. $mcache->set('users_auth', $users, false, 1000);
  46. return NULL;
  47. }
  48. public static function nav($server, $sid, $active)
  49. {
  50. global $cfg, $html, $sql, $mcache, $start_point;
  51. $notice_sid = $mcache->get('notice_'.$sid);
  52. $notice = is_array($notice_sid) ? $notice_sid : $mcache->get('notice_'.$server['unit']);
  53. if(!is_array($notice))
  54. {
  55. $sql->query('SELECT `server`, `text`, `color` FROM `notice` WHERE `server`="'.$sid.'" AND `time`>"'.$start_point.'" ORDER BY `id` DESC LIMIT 1');
  56. if(!$sql->num())
  57. $sql->query('SELECT `unit`, `text`, `color` FROM `notice` WHERE `unit`="'.$server['unit'].'" AND `time`>"'.$start_point.'" ORDER BY `id` DESC LIMIT 1');
  58. if($sql->num())
  59. {
  60. $notice = $sql->get();
  61. $nmc = $notice['server'] ? 'notice_'.$sid : 'notice_'.$server['unit'];
  62. $mcache->set('notice_'.$nmc, $notice, false, 10);
  63. }else
  64. $mcache->set('notice_'.$server['unit'], NULL, false, 10);
  65. }
  66. $aUnit = array('index', 'console', 'settings', 'plugins', 'maps', 'owners', 'filetp', 'tarif', 'copy', 'graph', 'web', 'boost');
  67. $html->get('gmenu', 'sections/servers/'.$server['game']);
  68. $html->set('id', $sid);
  69. $html->set('home', $cfg['http']);
  70. if(is_array($notice))
  71. {
  72. global $device;
  73. if($device == '!mobile')
  74. $html->set('notice', '<div class="informer '.$notice['color'].' topifon">'.$notice['text'].'</div><div class="space"></div>');
  75. else
  76. $html->set('notice', '<div class="heading-style-1 container"><div class="smaller-text color-'.$notice['color'].'-light">'.$notice['text'].'</div><div class="heading-decoration bg-'.$notice['color'].'-light" style="margin-top: 0px"></div></div>');
  77. }else
  78. $html->set('notice', '');
  79. if($server['console_use']) $html->unit('console_use', 1); else $html->unit('console_use');
  80. if($server['plugins_use']) $html->unit('plugins_use', 1); else $html->unit('plugins_use');
  81. if($server['ftp_use']) $html->unit('ftp_use', 1); else $html->unit('ftp_use');
  82. if($server['stats_use']) $html->unit('graph_use', 1); else $html->unit('graph_use');
  83. if($server['web_use']) $html->unit('web_use', 1); else $html->unit('web_use');
  84. if($server['copy_use']) $html->unit('copy_use', 1); else $html->unit('copy_use');
  85. foreach($aUnit as $unit)
  86. if($unit == $active) $html->unit($unit, 1); else $html->unit($unit);
  87. $html->pack('main');
  88. $html->get('vmenu', 'sections/servers/'.$server['game']);
  89. $html->set('id', $sid);
  90. $html->set('home', $cfg['http']);
  91. if($server['console_use']) $html->unit('console_use', 1); else $html->unit('console_use');
  92. if($server['plugins_use']) $html->unit('plugins_use', 1); else $html->unit('plugins_use');
  93. if($server['ftp_use']) $html->unit('ftp_use', 1); else $html->unit('ftp_use');
  94. if($server['stats_use']) $html->unit('graph_use', 1); else $html->unit('graph_use');
  95. if($server['web_use']) $html->unit('web_use', 1); else $html->unit('web_use');
  96. if($server['copy_use']) $html->unit('copy_use', 1); else $html->unit('copy_use');
  97. foreach($aUnit as $unit)
  98. if($unit == $active) $html->unit($unit, 1); else $html->unit($unit);
  99. $html->pack('vmenu');
  100. return NULL;
  101. }
  102. public static function route($server, $inc, $go, $all = false)
  103. {
  104. global $device, $start_point;
  105. $dir = $device == '!mobile' ? '' : 'megp/';
  106. $use = true;
  107. if(in_array($inc, array('plugins', 'ftp', 'console', 'graph', 'copy', 'web')))
  108. {
  109. $server['graph_use'] = $server['stats_use'];
  110. if(!$server[$inc.'_use'])
  111. $use = false;
  112. }
  113. if(!$use || $server['time'] < $start_point || in_array($server['status'], array('install', 'reinstall', 'update', 'recovery', 'blocked')))
  114. {
  115. if($go)
  116. sys::out('Раздел недоступен');
  117. if(!$use)
  118. return SEC.$dir.'servers/'.$server['game'].'/index.php';
  119. return SEC.$dir.'servers/noaccess.php';
  120. }
  121. if($all)
  122. return SEC.'servers/games/'.$inc.'.php';
  123. if(!file_exists(SEC.$dir.'servers/'.$server['game'].'/'.$inc.'.php'))
  124. return SEC.$dir.'servers/'.$server['game'].'/index.php';
  125. return SEC.$dir.'servers/'.$server['game'].'/'.$inc.'.php';
  126. }
  127. public static function int($data, $width = false)
  128. {
  129. if($width)
  130. return preg_replace("([^0-9]{0, ".$width."})", '', $data);
  131. return preg_replace("([^0-9])", '', $data);
  132. }
  133. public static function b64js($data)
  134. {
  135. return base64_encode(json_encode($data));
  136. }
  137. public static function b64djs($data)
  138. {
  139. return json_decode(base64_decode($data), true);
  140. }
  141. public static function hb64($data)
  142. {
  143. return base64_encode(htmlspecialchars($data));
  144. }
  145. public static function hb64d($data)
  146. {
  147. return htmlspecialchars_decode(base64_decode($data));
  148. }
  149. public static function outjs($val, $cache = false)
  150. {
  151. global $mcache;
  152. if($cache)
  153. $mcache->delete($cache);
  154. die(json_encode($val));
  155. }
  156. public static function out($val = '', $cache = false)
  157. {
  158. global $mcache;
  159. if($cache)
  160. $mcache->delete($cache);
  161. die(''.$val.'');
  162. }
  163. public static function outhtml($text, $time = 3, $url = false, $cache = false)
  164. {
  165. global $device, $mcache, $html, $cfg;
  166. if($cache)
  167. $mcache->delete($cache);
  168. $tpl = $device == '!mobile' ? '' : '/megp';
  169. $html->get('out');
  170. $html->set('title', $cfg['name']);
  171. $html->set('home', $cfg['http']);
  172. $html->set('css', $cfg['http'].'template'.$tpl.'/css/');
  173. $html->set('js', $cfg['http'].'template'.$tpl.'/js/');
  174. $html->set('img', $cfg['http'].'template'.$tpl.'/images/');
  175. $html->set('text', $text);
  176. $html->pack('out');
  177. if(!$url)
  178. $url = $cfg['http'];
  179. header('Refresh: '.$time.'; URL='.$url);
  180. die($html->arr['out']);
  181. }
  182. public static function valid($val, $type, $preg = '')
  183. {
  184. $val = (isset($val) ? $val : '');
  185. switch($type)
  186. {
  187. case 'promo':
  188. if(!preg_match("/^[A-Za-z0-9]{2,20}$/", $val))
  189. return true;
  190. return false;
  191. case 'en':
  192. if(!preg_match("/^[A-Za-z0-9]$/", $val))
  193. return true;
  194. return false;
  195. case 'ru':
  196. if(!preg_match("/^[А-Яа-я]$/u", $val))
  197. return true;
  198. return false;
  199. case 'wm':
  200. if(!preg_match('/^R[0-9]{12,12}$|^Z[0-9]{12,12}$|^U[0-9]{12,12}$/m', $val))
  201. return true;
  202. return false;
  203. case 'ip':
  204. 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))
  205. return true;
  206. return false;
  207. case 'steamid':
  208. 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))
  209. return true;
  210. return false;
  211. case 'steamid3':
  212. if(!preg_match("/^\[U:[01]:[0-9]{3,12}\]$/i", $val))
  213. return true;
  214. return false;
  215. case 'num':
  216. if(!preg_match('/[^0-9]/', $val))
  217. return true;
  218. return false;
  219. case 'md5':
  220. if(!preg_match("/^[a-z0-9]{32,32}$/", $val))
  221. return true;
  222. return false;
  223. case 'other':
  224. if(!preg_match($preg, $val))
  225. return true;
  226. return false;
  227. }
  228. return true;
  229. }
  230. public static function mail($name, $text, $mail)
  231. {
  232. global $cfg;
  233. require_once(LIB.'smtp.php');
  234. $tpl = file_get_contents(DATA.'mail.ini', "r");
  235. $text = str_replace(
  236. array('[name]', '[text]', '[http]', '[img]', '[css]'),
  237. array($cfg['name'], $text, $cfg['http'], $cfg['http'].'template/images/', $cfg['http'].'template/css/'),
  238. $tpl
  239. );
  240. $smtp = new smtp($cfg['smtp_login'], $cfg['smtp_passwd'], $cfg['smtp_url'], $cfg['smtp_mail'], 465);
  241. $headers = "MIME-Version: 1.0\r\n";
  242. $headers .= "Content-type: text/html; charset=utf-8\r\n";
  243. $headers .= "From: ".$cfg['smtp_name']." <".$cfg['smtp_mail'].">\r\n";
  244. if($smtp->send($mail, $name, $text, $headers))
  245. return true;
  246. return false;
  247. }
  248. public static function mail_domain($mail)
  249. {
  250. $domain = explode('@', $mail);
  251. $domain = end($domain);
  252. if(in_array($domain, array('list.ru', 'bk.ru', 'inbox.ru')))
  253. $domain = 'mail.ru';
  254. switch($domain)
  255. {
  256. case 'mail.ru':
  257. return $domain;
  258. case 'yandex.ru':
  259. return 'mail.yandex.ru';
  260. case 'google.com':
  261. return 'mail.google.com';
  262. default:
  263. return '';
  264. }
  265. }
  266. public static function domain($domain)
  267. {
  268. $domain = explode('.', $domain);
  269. unset($domain[0]);
  270. return implode('.', $domain);
  271. }
  272. public static function updtext($text, $data)
  273. {
  274. foreach($data as $name => $val)
  275. $text = str_replace('['.$name.']', $val, $text);
  276. return $text;
  277. }
  278. public static function login($mail, $lchar)
  279. {
  280. if(!$lchar)
  281. return str_replace(array('.', '_', '+', '-'), '', sys::first(explode('@', $mail)));
  282. $list = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZz0123456789';
  283. $a = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZz';
  284. $selections = strlen($list)-1;
  285. $start = strlen($a)-1;
  286. $b = rand(0, $start);
  287. $start = $a[$b];
  288. $login = array();
  289. $i = 0;
  290. for($i; $i <= 10; $i+=1)
  291. {
  292. $n = rand(0, $selections);
  293. $login[] = $list[$n];
  294. }
  295. return $start.implode('', $login);
  296. }
  297. public static function passwd($length = 8)
  298. {
  299. $list = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZz0123456789';
  300. $a = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZz';
  301. $selections = strlen($list)-1;
  302. $start = strlen($a)-1;
  303. $b = rand(0, $start);
  304. $start = $a[$b];
  305. $passwd = array();
  306. $i = 0;
  307. for($i; $i <= $length-2; $i+=1)
  308. {
  309. $n = rand(0, $selections);
  310. $passwd[] = $list[$n];
  311. }
  312. return $start.implode('', $passwd);
  313. }
  314. public static function passwdkey($passwd)
  315. {
  316. return md5($passwd);
  317. }
  318. public static function cookie($name, $value, $expires)
  319. {
  320. global $cfg;
  321. $expires = time() + ($expires * 86400);
  322. \Delight\Cookie\Cookie::setcookie($name, $value, $expires, '/', $cfg['url'], self::isSecure(), true, $cfg['cookie_same_site']);
  323. }
  324. public static function auth()
  325. {
  326. global $auth, $go, $text, $cfg;
  327. if($auth)
  328. {
  329. if($go)
  330. sys::outjs(array('e' => sys::text('output', 'auth')));
  331. global $device;
  332. $link = $device == '!mobile' ? 'user/section/lk' : '';
  333. exit(header('Refresh: 0; URL='.$cfg['http'].$link));
  334. }
  335. return NULL;
  336. }
  337. public static function noauth()
  338. {
  339. global $auth, $go, $text, $cfg;
  340. if(!$auth)
  341. {
  342. if($go)
  343. sys::outjs(array('e' => sys::text('output', 'noauth')));
  344. global $device;
  345. $link = $device == '!mobile' ? 'user/section/auth' : 'auth';
  346. exit(header('Refresh: 0; URL='.$cfg['http'].$link));
  347. }
  348. return NULL;
  349. }
  350. public static function browser($agent)
  351. {
  352. if(strpos($agent, 'Firefox') !== false)
  353. return 'Mozilla Firefox';
  354. if(strpos($agent, 'Opera') !== false)
  355. return 'Opera';
  356. if(strpos($agent, 'Chrome') !== false)
  357. return 'Google Chrome';
  358. if(strpos($agent, 'MSIE') !== false)
  359. return 'Internet Explorer';
  360. if(strpos($agent, 'Safari') !== false)
  361. return 'Safari';
  362. return 'Неизвестный';
  363. }
  364. public static function date($lenght, $date)
  365. {
  366. global $start_point;
  367. $check_time = $date-$start_point;
  368. if($check_time < 1)
  369. return 'время истекло.';
  370. $days = floor($check_time/86400);
  371. $hours = floor(($check_time%86400)/3600);
  372. $minutes = floor(($check_time%3600)/60);
  373. $seconds = $check_time%60;
  374. $adata = array(
  375. 'min' => array(
  376. 'days' => array('день', 'дня', 'дней'),
  377. 'hours' => array('ч.', 'ч.', 'ч.'),
  378. 'minutes' => array('мин.', 'мин.', 'мин.'),
  379. 'seconds' => array('сек.', 'сек.', 'сек.')
  380. ),
  381. 'max' => array(
  382. 'days' => array('день', 'дня', 'дней'),
  383. 'hours' => array('час', 'часа', 'часов'),
  384. 'minutes' => array('минуту','минуты','минут'),
  385. 'seconds' => array('секунду','секунды','секунд')
  386. )
  387. );
  388. $text = '';
  389. if($days > 0)
  390. $text .= sys::date_decl($days, $adata[$lenght]['days']);
  391. if($days < 1 AND $hours > 0)
  392. $text .= ' '.sys::date_decl($hours, $adata[$lenght]['hours']);
  393. if($days < 1 AND $minutes > 0)
  394. $text .= ' '.sys::date_decl($minutes, $adata[$lenght]['minutes']);
  395. if($days < 1 AND $seconds > 0)
  396. $text .= ' '.sys::date_decl($seconds, $adata[$lenght]['seconds']);
  397. return $text;
  398. }
  399. public static function date_decl($digit, $expr, $onlyword = false)
  400. {
  401. if(!is_array($expr))
  402. $expr = array_filter(explode(' ', $expr));
  403. if(empty($expr[2]))
  404. $expr[2] = $expr[1];
  405. $i = sys::int($digit)%100;
  406. if($onlyword)
  407. $digit = '';
  408. if($i > 4 AND $i < 21)
  409. $res = $digit.' '.$expr[2];
  410. else
  411. $i%=10;
  412. if($i == 1)
  413. $res = $digit.' '.$expr[0];
  414. elseif($i > 1 AND $i < 5)
  415. $res = $digit.' '.$expr[1];
  416. else
  417. $res = $digit.' '.$expr[2];
  418. return trim($res);
  419. }
  420. public static function today($time, $cp = false)
  421. {
  422. global $start_point;
  423. $today = date('d.m.Y', $start_point);
  424. $day = date('d.m.Y', $time);
  425. if($day == $today)
  426. {
  427. if($cp)
  428. return 'Сегодня '.date('H:i', $time);
  429. return 'Сегодня '.date('- H:i', $time);
  430. }
  431. $yesterday_first = sys::int(sys::first(explode('.', $today)))-1;
  432. $yesterday_full = date('m.Y', $time);
  433. if($day == $yesterday_first.'.'.$yesterday_full AND !$yesterday_first)
  434. {
  435. if($cp)
  436. return 'Вчера '.date('H:i', $time);
  437. return 'Вчера '.date('- H:i', $time);
  438. }
  439. if($cp)
  440. return date('d.m.Y H:i', $time);
  441. return date('d.m.Y - H:i', $time);
  442. }
  443. public static function day($time)
  444. {
  445. $days = array('день', 'дня', 'дней');
  446. $time = $time % 100;
  447. if($n > 10 AND $n < 20)
  448. return $days[2];
  449. $time = $time % 10;
  450. if($time > 1 AND $time < 5)
  451. return $days[1];
  452. if($time == 1)
  453. return $days[0];
  454. return $days[2];
  455. }
  456. public static function bbc($text)
  457. {
  458. global $cfg;
  459. $lines = explode("\n", $text);
  460. $str_search = array(
  461. "#\\\n#is",
  462. "#\[spoiler\](.+?)\[\/spoiler\]#is",
  463. "#\[sp\](.+?)\[\/sp\]#is",
  464. "#\[b\](.+?)\[\/b\]#is",
  465. "#\[u\](.+?)\[\/u\]#is",
  466. "#\[code\](.+?)\[\/code\]#is",
  467. "#\[quote\](.+?)\[\/quote\]#is",
  468. "#\[url=(.+?)\](.+?)\[\/url\]#is",
  469. "#\[img=(.+?)\] \[\/img\]#is",
  470. "#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is"
  471. );
  472. $str_replace = array(
  473. "<br />",
  474. "<div><b class='spoiler'>Посмотреть содержимое</b><div class='spoiler_main'>\\1</div></div>",
  475. "<div><b class='spoiler'>Посмотреть содержимое</b><div class='spoiler_main'>\\1</div></div>",
  476. "<b>\\1</b>",
  477. "<u>\\1</u>",
  478. "<div><b class='spoiler'>Посмотреть содержимое</b><div class='spoiler_main'><pre><code>\\1</code></pre></div></div>",
  479. "<blockquote><p>\\1</p></blockquote>",
  480. "<a href='\\1' target='_blank'>\\2</a>",
  481. "<a href='\\1' target='_blank' style='display: block;'><img src='".$cfg['url']."template/images/help_screenshot.png' alt='Изображение'></a>",
  482. "<a href='\\2' target='_blank'> \\2</a>"
  483. );
  484. $uptext = '';
  485. foreach($lines as $line)
  486. $uptext .= preg_replace($str_search, $str_replace, $line)."<br>";
  487. return $uptext;
  488. }
  489. public static function first($array = array())
  490. {
  491. return $array[0];
  492. }
  493. public static function back($url)
  494. {
  495. exit(header('Refresh: 0; URL='.$url));
  496. }
  497. public static function strlen($str)
  498. {
  499. return iconv_strlen($str, 'UTF-8');
  500. }
  501. public static function text($section, $name)
  502. {
  503. global $cfg, $user;
  504. $group = isset($user['group']) ? $user['group'] : 'user';
  505. if($section != 'error' || !$cfg['text_group'])
  506. $group = 'all';
  507. include(DATA.'text/'.$section.'.php');
  508. return isset($text[$name][$group]) ? $text[$name][$group] : $text[$name];
  509. }
  510. public static function key($param = 'defegp')
  511. {
  512. return md5(sha1(rand(1, 15).$param.rand(16, 30).rand(200, 1000).rand(1, 100)));
  513. }
  514. public static function captcha($type, $ip)
  515. {
  516. global $mcache;
  517. $cod = '';
  518. $width = 100;
  519. $height = 45;
  520. $font_size = 16;
  521. $symbols = 3;
  522. $symbols_fon = 20;
  523. $font = LIB.'captcha/text.ttf';
  524. $chars = array('a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z','2','3','4','5','6','7','9');
  525. $colors = array('20','50','80','100');
  526. $src = imagecreatetruecolor($width, $height);
  527. $fon = imagecolorallocate($src, 255, 255, 255);
  528. imagefill($src, 0, 0, $fon);
  529. $i = 0;
  530. for($i; $i < $symbols_fon; $i+=1)
  531. {
  532. $color = imagecolorallocatealpha($src, rand(0,255), rand(0,255), rand(0,255), 100);
  533. $char = $chars[rand(0, sizeof($chars)-1)];
  534. $size = rand($font_size-2, $font_size+2);
  535. imagettftext($src, $size, rand(0,45), rand($width*0.1,$width-$width*0.1), rand($height*0.2,$height), $color, $font, $char);
  536. }
  537. $i = 0;
  538. for($i; $i < $symbols; $i+=1)
  539. {
  540. $color = imagecolorallocatealpha($src, $colors[rand(0,sizeof($colors)-1)], $colors[rand(0,sizeof($colors)-1)], $colors[rand(0,sizeof($colors)-1)], rand(20,40));
  541. $char = $chars[rand(0, sizeof($chars)-1)];
  542. $size = rand($font_size*2.1-2, $font_size*2.1+2);
  543. $x = ($i+1)*$font_size + rand(6,8);
  544. $y = (($height*2)/3) + rand(3,7);
  545. $cod .= $char;
  546. imagettftext($src, $size, rand(0,15), $x, $y, $color, $font, $char);
  547. }
  548. $mcache->set($type.'_captcha_'.$ip, $cod, false, 120);
  549. header("Content-type: image/gif");
  550. imagegif($src);
  551. imagedestroy($src);
  552. exit;
  553. }
  554. public static function captcha_check($type, $ip, $cod = '')
  555. {
  556. global $cfg, $mcache;
  557. // Если повтор ввода капчи выключен и в кеше есть подтвержденный сеанс
  558. if(!$cfg['recaptcha'] AND $mcache->get($type.'_captcha_valid_'.$ip))
  559. return false;
  560. if($mcache->get($type.'_captcha_'.$ip) != strtolower($cod))
  561. {
  562. $mcache->set($type.'_captcha_valid_'.$ip, true, false, 60);
  563. return true;
  564. }
  565. return false;
  566. }
  567. public static function ismail($data)
  568. {
  569. $aData = explode('@', $data);
  570. if(count($aData) > 1)
  571. return true;
  572. return false;
  573. }
  574. public static function smscode()
  575. {
  576. return rand(1,9).rand(100,500).rand(10,99);
  577. }
  578. public static function code($length = 8)
  579. {
  580. $list = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZz0123456789';
  581. $a = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZz';
  582. $selections = strlen($list)-1;
  583. $start = strlen($a)-1;
  584. $b = rand(0, $start);
  585. $start = $a[$b];
  586. $code = array();
  587. $i = 0;
  588. for($i; $i <= $length-2; $i+=1)
  589. {
  590. $n = rand(0, $selections);
  591. $code[] = $list[$n];
  592. }
  593. return $start.implode('', $code);
  594. }
  595. public static function sms($text, $phone)
  596. {
  597. global $cfg;
  598. $out = file_get_contents($cfg['sms_gateway'].'&'.$cfg['sms_to'].'='.$phone.'&'.$cfg['sms_text'].'='.urlencode($text));
  599. $aOut = explode("\n", $out);
  600. if(trim($aOut[0]) == $cfg['sms_ok'])
  601. return true;
  602. return false;
  603. }
  604. public static function find($text, $find)
  605. {
  606. $words = explode(' ', $find);
  607. foreach($words as $word)
  608. if(strlen($word) >= 2)
  609. $text = preg_replace('#'.quotemeta($word).'#iu', '<span style="color: #F66A6A;">$0</span>', $text);
  610. return $text;
  611. }
  612. public static function str_first_replace($search, $replace, $text)
  613. {
  614. $pos = strpos($text, $search);
  615. return $pos!==false ? substr_replace($text, $replace, $pos, strlen($search)) : $text;
  616. }
  617. public static function cmd($command)
  618. {
  619. $text = preg_replace('/\\$/', '$ы', trim($command));
  620. mb_internal_encoding('UTF-8');
  621. if(mb_substr($text, -1) == 'ы')
  622. $text = quotemeta(substr($text, 0, -2));
  623. return $text;
  624. }
  625. public static function map($map)
  626. {
  627. $name = quotemeta(trim($map));
  628. if(substr($name, -1) == '$')
  629. $name = substr($name, 0, -2).'$';
  630. return str_replace(array('\.', '\*'), array('.', '*'), $name);
  631. }
  632. public static function temp($text)
  633. {
  634. $temp = TEMP.md5(time().rand(5, 100).rand(10, 20).rand(1, 20).rand(40, 80));
  635. $file = fopen($temp, "w");
  636. fputs($file, $text);
  637. fclose($file);
  638. return $temp;
  639. }
  640. public static function size($val)
  641. {
  642. $aSize = array(' Байт', ' Кб', ' Мб', ' Гб', ' Тб', ' Пб');
  643. return $val ? round($val/pow(1024, ($i = floor(log($val, 1024)))), 2) . $aSize[$i] : '0 Байт';
  644. }
  645. public static function unidate($date)
  646. {
  647. $aDate = explode('-', $date);
  648. $aFirst = explode(' ', $aDate[2]);
  649. return $aFirst[1].' - '.$aFirst[0].'.'.$aDate[1].'.'.$aDate[0];
  650. }
  651. public static function page($page, $nums, $num)
  652. {
  653. $ceil = ceil($nums/$num);
  654. if($page > $ceil)
  655. $page = $ceil;
  656. $next = $page*$num;
  657. if($next <= $nums)
  658. $next = $next-$num;
  659. if($next > $nums)
  660. $next = $next-$num;
  661. if($next < 1)
  662. $next = 0;
  663. $num_go = $next;
  664. if($page == '')
  665. $page = 1;
  666. $aPage = array(
  667. 'page' => $page,
  668. 'num' => $num_go,
  669. 'ceil' => $ceil
  670. );
  671. return $aPage;
  672. }
  673. public static function page_list($countnum, $actnum)
  674. {
  675. if($countnum == 0 || $countnum == 1)
  676. return array();
  677. if($countnum > 10)
  678. {
  679. if($actnum <= 4 || $actnum + 3 >= $countnum)
  680. {
  681. for($i = 0; $i <= 4; $i++)
  682. $numlist[$i] = $i + 1;
  683. $numlist[5] = '...';
  684. for($j = 6, $k = 4; $j <= 10; $j+=1, $k-=1)
  685. $numlist[$j] = $countnum - $k;
  686. }else{
  687. $numlist[0] = 1;
  688. $numlist[1] = 2;
  689. $numlist[2] = '...';
  690. $numlist[3] = $actnum - 2;
  691. $numlist[4] = $actnum - 1;
  692. $numlist[5] = $actnum;
  693. $numlist[6] = $actnum + 1;
  694. $numlist[7] = $actnum + 2;
  695. $numlist[8] = '...';
  696. $numlist[9] = $countnum - 1;
  697. $numlist[10] = $countnum;
  698. }
  699. }else
  700. for($n = 0; $n < $countnum; $n+=1)
  701. $numlist[$n] = $n + 1;
  702. return $numlist;
  703. }
  704. public static function page_gen($ceil, $page, $actnum, $section)
  705. {
  706. global $cfg, $html;
  707. $aNum = sys::page_list($ceil, $actnum);
  708. $pages = '';
  709. $html->get('pages');
  710. if($ceil)
  711. {
  712. if($page != 1)
  713. {
  714. $next = $page-1;
  715. $pages .= '<a href="'.$cfg['http'].$section.'/page/'.$next.'"><span>Предыдущая</span></a>';
  716. }
  717. foreach($aNum as $v)
  718. {
  719. if($v != $page && $v != '...')
  720. $pages .= '<a href="'.$cfg['http'].$section.'/page/'.$v.'">'.$v.'</a>';
  721. if($v == $page)
  722. $pages .= '<a href="#" onclick="return false" class="active">'.$v.'</a>';
  723. if($v == '...')
  724. $pages .= '<a href="#" onclick="return false">...</a>';
  725. }
  726. if($ceil > $page)
  727. {
  728. if($page < $ceil)
  729. {
  730. $next = $page+1;
  731. $pages .= '<a href="'.$cfg['http'].$section.'/page/'.$next.'"><span class="num_right">Следующая</span></a>';
  732. }else
  733. $pages .= '<a href="#" onclick="return false;"><span class="num_right">Следующая</span></a>';
  734. }
  735. }
  736. $html->set('pages', $pages);
  737. $html->pack('pages');
  738. return NULL;
  739. }
  740. public static function country($name)
  741. {
  742. global $cfg;
  743. $fileimg = file_exists(TPL.'/images/country/'.$name.'.png');
  744. if($fileimg)
  745. return $cfg['http'].'template/images/country/'.$name.'.png';
  746. return $cfg['http'].'template/images/country/none.png';
  747. }
  748. public static function ipproxy()
  749. {
  750. global $_SERVER;
  751. if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !empty($_SERVER['HTTP_CF_CONNECTING_IP']))
  752. return $_SERVER['HTTP_CF_CONNECTING_IP'];
  753. return NULL;
  754. }
  755. public static function ip()
  756. {
  757. $ip = sys::ipproxy();
  758. if(sys::valid($ip, 'ip'))
  759. return $_SERVER['REMOTE_ADDR'];
  760. return $ip;
  761. }
  762. public static function whois($ip)
  763. {
  764. $stack = fsockopen('whois.ripe.net', 43, $errno, $errstr);
  765. if(!$stack)
  766. return 'не определена';
  767. fputs($stack, $ip."\r\n");
  768. $subnetwork = '';
  769. while(!feof($stack))
  770. {
  771. $str = fgets($stack, 128);
  772. if(strpos($str, 'route:') !== FALSE)
  773. {
  774. $subnetwork = trim(str_replace('route:', '', $str));
  775. break;
  776. }
  777. }
  778. fclose($stack);
  779. return isset($subnetwork{0}) ? $subnetwork : 'не определена';
  780. }
  781. public static function rep_act($name, $time = 20)
  782. {
  783. global $go, $mcache;
  784. if(!$go)
  785. return NULL;
  786. if($mcache->get($name))
  787. sys::outjs(array('e' => sys::text('other', 'mcache')));
  788. $mcache->set($name, true, false, $time);
  789. return $name;
  790. }
  791. public static function check_php_config($file, &$error)
  792. {
  793. exec('php -l '.$file, $error, $code);
  794. if(!$code)
  795. return true;
  796. return false;
  797. }
  798. public static function cpu_idle($pros_stat = array(), $unit = false, $fcpu = false, $ctrl = false)
  799. {
  800. return sys::cpu_get_idle(sys::parse_cpu($pros_stat[0]), sys::parse_cpu($pros_stat[1]), $unit, $fcpu, $ctrl);
  801. }
  802. public static function cpu_get_idle($first, $second, $unit, $fcpu, $ctrl)
  803. {
  804. global $sql;
  805. if(count($first) !== count($second))
  806. return;
  807. $cpus = array();
  808. for($i = 0, $l = count($first); $i < $l; $i+=1)
  809. {
  810. $dif = array();
  811. $dif['use'] = $second[$i]['use']-$first[$i]['use'];
  812. $dif['nice'] = $second[$i]['nice']-$first[$i]['nice'];
  813. $dif['sys'] = $second[$i]['sys']-$first[$i]['sys'];
  814. $dif['idle'] = $second[$i]['idle']-$first[$i]['idle'];
  815. $total = array_sum($dif);
  816. $cpu = array();
  817. foreach($dif as $x => $y)
  818. $cpu[$x] = $y ? round($y/$total*100, 1) : 0;
  819. $cpus['cpu'.$i] = $cpu;
  820. }
  821. if($fcpu)
  822. return $cpus;
  823. $threads = array();
  824. $l = count($first);
  825. for($i = 0; $i < $l; $i+=1)
  826. $threads[$i] = $cpus['cpu'.$i]['idle'];
  827. if(count($first) > 1)
  828. unset($threads[0]);
  829. $max = max($threads);
  830. foreach($threads as $idle)
  831. {
  832. $core = array_search($max, $threads);
  833. if($ctrl)
  834. $sql->query('SELECT `id` FROM `control_servers` WHERE `unit`="'.$unit.'" AND `core_fix`="'.($core+1).'" LIMIT 1');
  835. else
  836. $sql->query('SELECT `id` FROM `servers` WHERE `unit`="'.$unit.'" AND `core_fix`="'.($core+1).'" AND `core_fix_one`="1" LIMIT 1');
  837. if($sql->num())
  838. {
  839. unset($threads[$core]);
  840. if(!count($threads))
  841. return NULL;
  842. $max = max($threads);
  843. }
  844. }
  845. return array_search($max, $threads);
  846. }
  847. public static function parse_cpu($data)
  848. {
  849. $data = explode("\n", $data);
  850. $cpu = array();
  851. foreach($data as $line)
  852. {
  853. if(preg_match('/^cpu[0-9]/', $line))
  854. {
  855. $info = explode(' ', $line);
  856. $cpu[] = array(
  857. 'use' => $info[1],
  858. 'nice' => $info[2],
  859. 'sys' => $info[3],
  860. 'idle' => $info[4]
  861. );
  862. }
  863. }
  864. return $cpu;
  865. }
  866. public static function reset_mcache($nmch, $id, $data = array(), $ctrl = false)
  867. {
  868. global $mcache;
  869. $cache = array(
  870. 'name' => $data['name'],
  871. 'status' => sys::status($data['status'], $data['game']),
  872. 'online' => $data['online'],
  873. 'image' => '<img src="'.sys::status($data['status'], $data['game'], '', 'img').'">',
  874. );
  875. $cache = $ctrl ? sys::buttons($id, $data['status'], $data['game'], $ctrl) : sys::buttons($id, $data['status'], $data['game']);
  876. if(isset($data['players']))
  877. $cache['players'] = $data['players'];
  878. $mcache->set($nmch, $cache, false, 5);
  879. return NULL;
  880. }
  881. public static function status($status, $game, $map = '', $get = 'text')
  882. {
  883. global $cfg;
  884. switch($status)
  885. {
  886. case 'working':
  887. if($get == 'img')
  888. {
  889. if(in_array($game, array('samp', 'crmp', 'mta', 'mc')))
  890. $map = $game;
  891. return sys::img($map, $game);
  892. }
  893. return 'Карта: '.($map == '' ? '-' : $map);
  894. case 'off':
  895. if($get == 'img')
  896. return $cfg['http'].'template/images/status/off.jpg';
  897. return 'Статус: <span style="color: #C46666;">выключен</span>';
  898. case 'start':
  899. if($get == 'img')
  900. return $cfg['http'].'template/images/status/start.gif';
  901. return 'Статус: <span style="color: #22B93C;">запускается</span>';
  902. case 'restart':
  903. if($get == 'img')
  904. return $cfg['http'].'template/images/status/restart.gif';
  905. return 'Статус: <span style="color: #22B93C;">перезапускается</span>';
  906. case 'change':
  907. if($get == 'img')
  908. return $cfg['http'].'template/images/status/change.gif';
  909. return 'Статус: <span style="color: #52BEFC;">меняется карта</span>';
  910. case 'install':
  911. if($get == 'img')
  912. return $cfg['http'].'template/images/status/install.gif';
  913. return 'Статус: <span style="color: #22B93C;">устанавливается</span>';
  914. case 'reinstall':
  915. if($get == 'img')
  916. return $cfg['http'].'template/images/status/reinstall.gif';
  917. return 'Статус: <span style="color: #22B93C;">переустанавливается</span>';
  918. case 'update':
  919. if($get == 'img')
  920. return $cfg['http'].'template/images/status/update.gif';
  921. return 'Статус: <span style="color: #F2CF41;">обновляется</span>';
  922. case 'recovery':
  923. if($get == 'img')
  924. return $cfg['http'].'template/images/status/recovery.gif';
  925. return 'Статус: <span style="color: #22B93C;">восстанавливается</span>';
  926. case 'overdue':
  927. if($get == 'img')
  928. return $cfg['http'].'template/images/status/overdue.jpg';
  929. return 'Статус: просрочен';
  930. case 'blocked':
  931. if($get == 'img')
  932. return $cfg['http'].'template/images/status/blocked.jpg';
  933. return 'Статус: заблокирован';
  934. }
  935. }
  936. public static function img($name, $game)
  937. {
  938. global $cfg;
  939. $filename = 'http://cdn.enginegp.ru/maps/'.$game.'/'.$name.'.jpg';
  940. $file_headers = @get_headers($filename) ;
  941. $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://";
  942. if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found' ||trim($file_headers[0]) == 'HTTP/1.1 403 Forbidden') {
  943. return $cfg['http'].'template/images/status/none.jpg';
  944. }
  945. else {
  946. return '' . $protocol .'cdn.enginegp.ru/maps/'.$game.'/'.$name.'.jpg';
  947. }
  948. }
  949. public static function buttons($id, $status, $game = false, $ctrl = false)
  950. {
  951. global $html;
  952. if(isset($html->arr['buttons']))
  953. unset($html->arr['buttons']);
  954. $other = in_array($game, array('samp', 'crmp', 'mta', 'mc'));
  955. $dir = $ctrl ? 'control/servers' : 'servers';
  956. if(in_array($status, array('working', 'change', 'start', 'restart')))
  957. {
  958. $html->get('stop', 'sections/'.$dir.'/buttons');
  959. $html->set('id', $id);
  960. if($ctrl)
  961. $html->set('ctrl', $ctrl);
  962. $html->pack('buttons');
  963. $html->get('restart', 'sections/'.$dir.'/buttons');
  964. $html->set('id', $id);
  965. if($ctrl)
  966. $html->set('ctrl', $ctrl);
  967. $html->pack('buttons');
  968. if(!$other)
  969. {
  970. $html->get('change', 'sections/'.$dir.'/buttons');
  971. $html->set('id', $id);
  972. if($ctrl)
  973. $html->set('ctrl', $ctrl);
  974. $html->pack('buttons');
  975. }
  976. return $html->arr['buttons'];
  977. }
  978. if($status == 'off')
  979. {
  980. $html->get('start', 'sections/'.$dir.'/buttons');
  981. $html->set('id', $id);
  982. if($ctrl)
  983. $html->set('ctrl', $ctrl);
  984. $html->pack('buttons');
  985. $html->get('reinstall', 'sections/'.$dir.'/buttons');
  986. $html->set('id', $id);
  987. if($ctrl)
  988. $html->set('ctrl', $ctrl);
  989. $html->pack('buttons');
  990. if(!$other)
  991. {
  992. $html->get('update', 'sections/'.$dir.'/buttons');
  993. $html->set('id', $id);
  994. if($ctrl)
  995. $html->set('ctrl', $ctrl);
  996. $html->pack('buttons');
  997. }
  998. return $html->arr['buttons'];
  999. }
  1000. $html->get('other', 'sections/'.$dir.'/buttons');
  1001. $html->pack('buttons');
  1002. return $html->arr['buttons'];
  1003. }
  1004. public static function entoru($month)
  1005. {
  1006. $ru = array(
  1007. 1 => 'Янв', 2 => 'Фев', 3 => 'Мар', 4 => 'Апр',
  1008. 5 => 'Май', 6 => 'Июн', 7 => 'Июл', 8 => 'Авг',
  1009. 9 => 'Сен', 10 => 'Окт', 11 => 'Ноя', 12 => 'Дек'
  1010. );
  1011. return $ru[$month];
  1012. }
  1013. public static function head($head)
  1014. {
  1015. global $route, $header;
  1016. if($head == 'description')
  1017. {
  1018. global $description;
  1019. if(isset($description))
  1020. {
  1021. $text = str_replace(array('"', '-'), array('', '—'), strip_tags($description));
  1022. if(strlen($text) > 160)
  1023. {
  1024. mb_internal_encoding('UTF-8');
  1025. $text = mb_substr($text, 0, 157).'...';
  1026. }
  1027. return $text;
  1028. }
  1029. }else{
  1030. global $keywords;
  1031. if(isset($keywords))
  1032. return str_replace(array('"', '-'), array('', '—'), strip_tags($keywords));
  1033. }
  1034. return array_key_exists($route, $header) ? $header[$route][$head] : $header['index'][$head];
  1035. }
  1036. public static function tags($tags)
  1037. {
  1038. $aTags = explode(',', $tags);
  1039. $text = '';
  1040. foreach($aTags as $tag)
  1041. $text .= '<strong>'.trim($tag).'</strong>, ';
  1042. return isset($text{0}) ? substr($text, 0, -2) : 'отсутствуют';
  1043. }
  1044. public static function benefitblock($id, $nmch = false)
  1045. {
  1046. global $cfg, $sql, $start_point;
  1047. if($cfg['benefitblock'])
  1048. {
  1049. $sql->query('SELECT `benefit` FROM `servers` WHERE `id`="'.$id.'" LIMIT 1');
  1050. $info = $sql->get();
  1051. if($info['benefit'] > $start_point)
  1052. sys::outjs(array('e' => 'Операция недоступна до '.date('d.m.Y - H:i:s', $info['benefit'])), $nmch);
  1053. }
  1054. return NULL;
  1055. }
  1056. function outfile($file, $name, $del = false)
  1057. {
  1058. if(file_exists($file))
  1059. {
  1060. if(ob_get_level())
  1061. ob_end_clean();
  1062. header('Content-Description: File Transfer');
  1063. header('Content-Type: application/octet-stream');
  1064. header('Content-Disposition: attachment; filename='.$name);
  1065. header('Content-Transfer-Encoding: binary');
  1066. header('Expires: 0');
  1067. header('Cache-Control: must-revalidate');
  1068. header('Pragma: public');
  1069. header('Content-Length: '.filesize($file));
  1070. readfile($file);
  1071. if($del)
  1072. unlink($file);
  1073. exit;
  1074. }
  1075. }
  1076. }
  1077. ?>