system.php 41 KB

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