Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. <?php
  2.  
  3. function Number($b1, $b2 = 254, $b3 = 254, $b4 = 254)
  4. {
  5. if ($b1 == 0 || $b1 == 254) $b1 = 1;
  6. if ($b2 == 0 || $b2 == 254) $b2 = 1;
  7. if ($b3 == 0 || $b3 == 254) $b3 = 1;
  8. if ($b4 == 0 || $b4 == 254) $b4 = 1;
  9.  
  10. --$b1;
  11. --$b2;
  12. --$b3;
  13. --$b4;
  14.  
  15. return ($b4*16194277 + $b3*64009 + $b2*253 + $b1);
  16. }
  17.  
  18. function timeago_full($pre,$now=NULL,$suffix=true)
  19. {
  20. if ($now === NULL)
  21. {
  22. $now = time();
  23. }
  24.  
  25. $times = array(
  26. array(1,'second'),
  27. array(60,'minute'),
  28. array(60*60,'hour'),
  29. array(24*60*60,'day'),
  30. array(7*60*60*24,'week'),
  31. array(52*60*60*24*7,'year'),
  32. );
  33.  
  34. $diff = $now - $pre;
  35.  
  36. if ($suffix)
  37. {
  38. $ago = ($diff >= 0)?' ago':' from now';
  39. }
  40. else
  41. {
  42. $ago = '';
  43. }
  44.  
  45. $diff = abs($diff);
  46. $text = '';
  47.  
  48. for ($i=count($times)-1; $i>=0; --$i)
  49. {
  50. $x = floor($diff/$times[$i][0]);
  51. $diff -= $x*$times[$i][0];
  52. if ($x > 0)
  53. {
  54. $text .= "$x ".$times[$i][1].(($x == 1)?'':'s').', ';
  55. }
  56. }
  57.  
  58. if ($text == '')
  59. {
  60. $text = '0 seconds, ';
  61. }
  62.  
  63. return substr($text,0,-2).$ago;
  64. }
  65.  
  66. function webcp_error_handler($errno, $errstr, $errfile, $errline)
  67. {
  68. global $tpl;
  69. $errfile = basename($errfile);
  70. if ((error_reporting() & $errno) != $errno)
  71. {
  72. return;
  73. }
  74. if (isset($tpl) && !$tpl->MainExecuted())
  75. {
  76. $tpl->error = "$errstr ($errfile:$errline)";
  77. $tpl->Execute('error');
  78. exit;
  79. }
  80. else
  81. {
  82. exit("<br><b>Error:</b> $errstr ($errfile:$errline)<br>");
  83. }
  84. }
  85. set_error_handler("webcp_error_handler");
  86.  
  87. function webcp_exception_handler($e)
  88.  
  89. set_exception_handler("webcp_exception_handler");
  90.  
  91. function webcp_debug_info()
  92. {
  93. global $db;
  94. global $starttime;
  95. $exectime = number_format((microtime(true) - $starttime)*1000, 1);
  96. echo "Total execution time: $exectime ms<br>";
  97. foreach ($db->Debug() as $query)
  98. {
  99. $exectime = number_format($query[1], 1);
  100. echo htmlentities($query[0])." -- ($exectime ms)<br>";
  101. }
  102. }
  103.  
  104. if (!function_exists('hash'))
  105. {
  106. exit("Could not find the the hash PHP extension.");
  107. }
  108.  
  109. if (array_search('sha256',hash_algos()) === false)
  110. {
  111. exit("Could not find the the sha256 hash algorithm.");
  112. }
  113.  
  114.  
  115. define('ADMIN_HGM', 4);
  116. define('ADMIN_GM', 3);
  117. define('ADMIN_GUARDIAN', 2);
  118. define('ADMIN_GUIDE', 1);
  119. define('ADMIN_PLAYER', 0);
  120.  
  121. define('RACE_WHITE', 0);
  122. define('RACE_YELLOW', 1);
  123. define('RACE_TAN', 2);
  124. define('RACE_ORC', 3);
  125. define('RACE_PANDA', 4);
  126. define('RACE_SKELETON', 5);
  127. define('RACE_FISH', 6);
  128.  
  129. require 'config.php';
  130.  
  131. if (!empty($DEBUG))
  132. {
  133. $starttime = microtime(true);
  134. register_shutdown_function('webcp_debug_info');
  135. }
  136.  
  137. {
  138. $db = new Database($dbtype, $dbhost, $dbuser, $dbpass, $dbname);
  139. }
  140. catch (Exception $e)
  141. {
  142. exit("Database connection failed. (".$e->getMessage().")");
  143. }
  144. $tpl = new Template('tpl/'.$template, true);
  145. $sess = new Session($cpid.'_EOSERVCP');
  146.  
  147. $tpl->pagetitle = $pagetitle;
  148. $tpl->sitename = $sitename;
  149. $tpl->homeurl = $homeurl;
  150. $tpl->php = $phpext;
  151. $tpl->onlinecharacters = 0;
  152. $tpl->maxplayers = $maxplayers;
  153. $tpl->serverhost = $serverhost;
  154. $tpl->serverport = $serverport;
  155.  
  156.  
  157. if (!is_dir($pubfiles))
  158. {
  159. exit("Directory not found: $pubfiles");
  160. }
  161.  
  162. if (!is_file($pubfiles.'/dat001.eif'))
  163. {
  164. exit("File not found: $pubfiles/dat001.eif");
  165. }
  166.  
  167. if (!is_file($pubfiles.'/dat001.ecf'))
  168. {
  169. exit("File not found: $pubfiles/dat001.ecf");
  170. }
  171.  
  172. if (!empty($NEEDPUB))
  173.  
  174.  
  175. if (((isset($checkcsrf) && $checkcsrf) || $_SERVER['REQUEST_METHOD'] == 'POST') && (!isset($_REQUEST['csrf']) || !isset($sess->csrf) || $_REQUEST['csrf'] != $sess->csrf))
  176. {
  177. header('HTTP/1.1 400 Bad Request');
  178. exit("<h1>400 - Bad Request</h1>");
  179. }
  180.  
  181. if ($dynamiccsrf || !isset($sess->csrf))
  182. {
  183. $tpl->csrf = $sess->csrf = $csrf = mt_rand();
  184. }
  185. else
  186. {
  187. $tpl->csrf = $csrf = $sess->csrf;
  188. }
  189.  
  190. if (!file_exists('online.cache') || filemtime('online.cache')+$onlinecache < time())
  191. {
  192. $serverconn = @fsockopen($serverhost, $serverport, $errno, $errstr, 2.0);
  193. $tpl->online = $online = (bool)$serverconn;
  194. $onlinelist = array();
  195. if ($online)
  196. {
  197. $request_online = chr(5).chr(254).chr(1).chr(22).chr(254).chr(255);
  198. fwrite($serverconn, $request_online);
  199. $raw = fread($serverconn, 1024*256); // Read up to 256KB of data
  200. fclose($serverconn);
  201. $raw = substr($raw, 5); // length, ID, replycode
  202. $chars = Number(ord($raw[0]), ord($raw[1])); $raw = substr($raw, 2); // Number of characters
  203. $raw = substr($raw, 1); // separator
  204. for ($i = 0; $i < $chars; ++$i)
  205. {
  206. $newchar = array(
  207. 'name' => '',
  208. 'title' => '',
  209. 'admin' => '',
  210. 'guild' => '',
  211. );
  212.  
  213. $pos = strpos($raw, chr(255));
  214. $newchar['name'] = substr($raw, 0, $pos);
  215. $raw = substr($raw, $pos+1);
  216.  
  217. $pos = strpos($raw, chr(255));
  218. $newchar['title'] = substr($raw, 0, $pos);
  219. $raw = substr($raw, $pos+1);
  220.  
  221. $raw = substr($raw, 1); // ?
  222.  
  223. $newchar['admin'] = Number(ord(substr($raw, 0, 1)));
  224. $newchar['admin'] = ($newchar['admin'] == 4 || $newchar['admin'] == 5 || $newchar['admin'] == 9 || $newchar['admin'] == 10);
  225. $raw = substr($raw, 1);
  226.  
  227. $newchar['guild'] = trim(substr($raw, 0, 3));
  228. $raw = substr($raw, 3);
  229.  
  230. $raw = substr($raw, 1); // separator
  231.  
  232. $onlinelist[] = $newchar;
  233. }
  234. ksort($onlinelist);
  235. file_put_contents('online.cache', serialize($onlinelist));
  236. }
  237. else
  238. {
  239. file_put_contents('online.cache', 'OFFLINE');
  240. }
  241. }
  242. else
  243. {
  244. $onlinedata = file_get_contents('online.cache');
  245. if ($onlinedata == 'OFFLINE')
  246. {
  247. $tpl->online = $online = false;
  248. }
  249. else
  250. {
  251. $tpl->online = $online = true;
  252. $onlinelist = unserialize($onlinedata);
  253. }
  254. }
  255.  
  256. $tpl->onlinecharacters = isset($onlinelist)?count($onlinelist):0;
  257.  
  258. if ($online)
  259. {
  260. $statusstr = '<span class="online">Online</span>';
  261. }
  262. else
  263. {
  264. $statusstr = '<span class="offline">Offline</span>';
  265. }
  266.  
  267. $tpl->statusstr = $statusstr;
  268.  
  269. if (isset($_REQUEST['action']))
  270. {
  271. switch ($_REQUEST['action'])
  272. {
  273. case 'logout':
  274. unset($sess->username);
  275.  
  276. case 'login':
  277. if (isset($_POST['username'], $_POST['password']))
  278. {
  279. $password = hash('sha256',$salt.strtolower($_POST['username']).substr($_POST['password'],0,12));
  280. $checklogin = $db->SQL("SELECT username FROM accounts WHERE username = '$' AND password = '$'", strtolower($_POST['username']), $password);
  281. if (empty($checklogin))
  282. {
  283. $tpl->message = "Login failed.";
  284. break;
  285. }
  286. else
  287. {
  288. $sess->username = $checklogin[0]['username'];
  289. $tpl->message = "Logged in.";
  290. }
  291. }
  292. break;
  293. }
  294. }
  295.  
  296. $tpl->logged = $logged = isset($sess->username);
  297. $tpl->username = $sess->username;
  298. $userdata = $db->SQL("SELECT * FROM accounts WHERE username = '$'", $sess->username);
  299.  
  300. if ($logged && empty($userdata))
  301. {
  302. $tpl->message = "Your account has been deleted, logging out...";
  303. $tpl->logged = $logged = false;
  304. }
  305.  
  306. $tpl->GUARDIAN = $GUARDIAN = false;
  307. $tpl->GM = $GM = false;
  308. $tpl->HGM = $HGM = false;
  309.  
  310. $chardata_guilds = array();
  311. if (isset($userdata[0]))
  312. {
  313. $userdata = $userdata[0];
  314. $chardata = $db->SQL("SELECT * FROM characters WHERE account = '$'", $sess->username);
  315. foreach ($chardata as $cd)
  316. {
  317. if ($cd['admin'] >= ADMIN_GUARDIAN)
  318. {
  319. $tpl->GUARDIAN = $GUARDIAN = true;
  320. }
  321.  
  322. if ($cd['admin'] >= ADMIN_GM)
  323. {
  324. $tpl->GM = $GM = true;
  325. }
  326.  
  327. if ($cd['admin'] >= ADMIN_HGM)
  328. {
  329. $tpl->HGM = $HGM = true;
  330. }
  331.  
  332. if ($cd['guild'])
  333. {
  334. if (!isset($chardata_guilds[$cd['guild']]))
  335. {
  336. $chardata_guilds[$cd['guild']] = array(
  337. 'leader' => false
  338. );
  339. }
  340. if ($cd['guild_rank'] == 1)
  341. {
  342. $chardata_guilds[$cd['guild']]['leader'] = true;
  343. }
  344. }
  345. }
  346. }
  347. else
  348. {
  349. $chardata = array();
  350. }
  351.  
  352. $tpl->numchars = $numchars = count($chardata);
  353. $tpl->userdata = $sess->userdata = $userdata;
  354. $tpl->chardata_guilds = $chardata_guilds;
  355.  
  356. function trans_form($buffer)
  357. {
  358. global $csrf;
  359. $buffer = str_replace('</form>','<input type="hidden" name="csrf" value="'.$csrf.'">'."\n".'</form>', $buffer);
  360. return $buffer;
  361. }
  362.  
  363. ob_start('trans_form',0);
  364.  
  365. function generate_pagination($pages, $page, $prefix = '')
  366. {
  367. if (strpos($prefix, '?') === false)
  368. {
  369. $prefix .= '?';
  370. }
  371. else
  372. {
  373. $prefix .= '&';
  374. }
  375. $ret = "<div class=\"pagination\">";
  376. if ($page == 1)
  377. {
  378. $ret .= "<< ";
  379. }
  380. else
  381. {
  382. $ret .= "<a href=\"{$prefix}page=".($page-1)."\"><<</a> ";
  383. }
  384. $elip = false;
  385. for ($i = 1; $i <= $pages; ++$i)
  386. {
  387. if ($pages < 15 || abs($i - $page) < 3 || abs($i - $pages) < 2 || abs($i - 1) < 2)
  388. {
  389. if ($i == $page)
  390. {
  391. $ret .= "<span class=\"current\">$i</span> ";
  392. }
  393. else
  394. {
  395. $ret .= "<a href=\"{$prefix}page=$i\">$i</a> ";
  396. }
  397. $elip = true;
  398. }
  399. else
  400. {
  401. if ($elip)
  402. {
  403. $ret .= "... ";
  404. $elip = false;
  405. }
  406. }
  407. }
  408.  
  409. if ($page == $pages)
  410. {
  411. $ret .= ">>";
  412. }
  413. else
  414. {
  415. $ret .= "<a href=\"{$prefix}page=".($page+1)."\">>></a>";
  416. }
  417.  
  418. $ret .= "</div>";
  419.  
  420. return $ret;
  421. }
  422.  
  423. function unserialize_inventory($str)
  424. {
  425. global $eoserv_items;
  426. $items = explode(';', $str);
  427. array_pop($items);
  428.  
  429. foreach ($items as &$item)
  430. {
  431. $xitem = explode(',', $item);
  432. $item = array(
  433. 'id' => (int)$xitem[0],
  434. 'name' => $eoserv_items->Get($xitem[0])->name,
  435. 'amount' => $xitem[1]
  436. );
  437. }
  438. unset($item);
  439.  
  440. return $items;
  441. }
  442.  
  443. function unserialize_paperdoll($str)
  444. {
  445. global $eoserv_items;
  446. $items = explode(',', $str);
  447. array_pop($items);
  448.  
  449. if (count($items) != 15)
  450. {
  451. $items = array_fill(0, 15, 0);
  452. }
  453.  
  454. foreach ($items as &$item)
  455. {
  456. $item = array(
  457. 'id' => (int)$item,
  458. 'slot' => EIFReader::TypeString($eoserv_items->Get($item)->type),
  459. 'name' => $eoserv_items->Get($item)->name
  460. );
  461. }
  462. unset($item);
  463.  
  464. return $items;
  465. }
  466.  
  467. function unserialize_guildranks($str)
  468. {
  469. global $eoserv_items;
  470. $ranks = explode(',', $str);
  471. array_pop($ranks);
  472.  
  473. if (count($ranks) != 9)
  474. {
  475. $ranks = array_fill(0, 9, 0);
  476. }
  477.  
  478. return $ranks;
  479. }
  480.  
  481. function unserialize_spells()
  482. {
  483. return array();
  484. }
  485.  
  486. function karma_str($karma)
  487. {
  488. // NOTE: These values are unconfirmed guesses
  489. $table = array(
  490. 0 => 'Demonic',
  491. 250 => 'Doomed',
  492. 500 => 'Cursed',
  493. 750 => 'Evil',
  494. 1000 => 'Neutral',
  495. 1250 => 'Good',
  496. 1500 => 'Blessed',
  497. 1750 => 'Saint',
  498. 2000 => 'Pure'
  499. );
  500.  
  501. $last = $table[0];
  502.  
  503. foreach ($table as $k => $v)
  504. {
  505. if ($karma < $k)
  506. {
  507. return $last;
  508. }
  509. $last = $v;
  510. }
  511.  
  512. return $last;
  513. }
  514.  
  515. function haircolor_str($color)
  516. {
  517. $table = array(
  518. 'Brown',
  519. 'Green',
  520. 'Pink',
  521. 'Red',
  522. 'Yellow',
  523. 'Blue',
  524. 'Purple',
  525. 'Luna',
  526. 'White',
  527. 'Black'
  528. );
  529.  
  530. return isset($table[$color])?$table[$color]:'Unknown';
  531. }
  532.  
  533. function race_str($race)
  534. {
  535. $table = array(
  536. 'Human (White)',
  537. 'Human (Yellow)',
  538. 'Human (Tan)',
  539. 'Orc',
  540. 'Panda',
  541. 'Skeleton',
  542. 'Fish'
  543. );
  544.  
  545. return isset($table[$race])?$table[$race]:'Unknown';
  546. }
  547.  
  548. function adminrank_str($admin)
  549. {
  550. $table = array(
  551. 'Player',
  552. 'Light Guide',
  553. 'Guardian',
  554. 'Game Master',
  555. 'High Game Master'
  556. );
  557.  
  558. return isset($table[$admin])?$table[$admin]:'Unknown';
  559. }
  560.  
  561. function class_str($class)
  562. {
  563. global $eoserv_classes;
  564. if ($class == 0)
  565. {
  566. return '-';
  567. }
  568.  
  569. return $eoserv_classes->Get($class)->name;
  570. }
  571.  
  572. function guildrank_str($ranks, $rank)
  573. {
  574. return isset($ranks[$rank-1])?$ranks[$rank-1]:'Unknown';
  575. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement