system.php 34 KB

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