system.php 46 KB

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