PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/html/includes/functions.php

https://github.com/graywh/utstats
PHP | 575 lines | 461 code | 99 blank | 15 comment | 89 complexity | 836728c030bf0bcb3547272084e61c1b MD5 | raw file
  1. <?php
  2. error_reporting(E_ALL & ~E_NOTICE);
  3. @ini_set('track_errors', '1');
  4. // Image Rotation Code
  5. $charimages[] = "char1.jpg";
  6. $charimages[] = "char2.jpg";
  7. $charimages[] = "char3.jpg";
  8. $charimages[] = "char4.jpg";
  9. $charimages[] = "char5.jpg";
  10. $charimages[] = "char6.jpg";
  11. $charimages[] = "char7.jpg";
  12. $charimages[] = "char8.jpg";
  13. $charimages[] = "char9.jpg";
  14. $charimages[] = "char10.jpg";
  15. $charimages[] = "char11.jpg";
  16. $charimages[] = "char12.jpg";
  17. $charimages[] = "char13.jpg";
  18. $charimages[] = "char14.jpg";
  19. srand(microtime() * 1000);
  20. $charimg = $charimages[rand(0, count($charimages)-1)];
  21. // Two letter codes and their correspionding country names
  22. require_once(dirname(__FILE__) .'/countries.php');
  23. // Addslashes if magic_quotes are off
  24. function my_addslashes($data) {
  25. IF (!get_magic_quotes_gpc()) {
  26. $data = addslashes($data);
  27. }
  28. return $data;
  29. }
  30. function my_stripslashes($data) {
  31. IF (!get_magic_quotes_gpc()) {
  32. $data = $data;
  33. } else {
  34. $data = stripslashes($data);
  35. }
  36. return $data;
  37. }
  38. function my_fgets($fp, $length = -1, $compression = 'none') {
  39. static $use_fgets = NULL;
  40. if ($use_fgets === NULL) $use_fgets = (version_compare(phpversion(), "4.3.0", ">=")) ? true : false;
  41. if ($use_fgets and $compression == 'none') {
  42. if ($length == -1) {
  43. return(fgets($fp));
  44. } else {
  45. return(fgets($fp, $length));
  46. }
  47. }
  48. $buffer = '';
  49. $i = 0;
  50. while(!feof($fp)) {
  51. if ($length != -1 and $i >= $length) break;
  52. $i++;
  53. switch($compression) {
  54. case 'bz2': $char = bzread($fp, 1); break;
  55. case 'zlib': $char = gzread($fp, 1); break;
  56. default: $char = fread($fp, 1);
  57. }
  58. $buffer .= $char;
  59. if ($char == "\n") break;
  60. }
  61. if (empty($buffer) and feof($fp)) return(false);
  62. return($buffer);
  63. }
  64. function my_fopen($filename, $mode, &$compression) {
  65. if ($compression === NULL) {
  66. $compression = 'none';
  67. if (substr($filename, -4) == '.bz2') {
  68. if (check_extension('bz2')) {
  69. $compression = 'bz2';
  70. } else {
  71. return(false);
  72. }
  73. }
  74. if (substr($filename, -3) == '.gz') {
  75. if (check_extension('zlib')) {
  76. $compression = 'zlib';
  77. } else {
  78. return(false);
  79. }
  80. }
  81. }
  82. switch($compression) {
  83. case 'bz2': $fp = @bzopen($filename, $mode); break;
  84. case 'zlib': $fp = @gzopen($filename, $mode); break;
  85. default: $fp = @fopen($filename, $mode); break;
  86. }
  87. return($fp);
  88. }
  89. function my_fclose($fp, $compression) {
  90. switch($compression) {
  91. case 'bz2': return(@bzclose($fp));
  92. case 'zlib': return(@gzclose($fp));
  93. default: return(@fclose($fp));
  94. }
  95. }
  96. // Run query
  97. function run_query($query) {
  98. $sql_run = "$query";
  99. $r_run = mysql_query($sql_run) or die(mysql_error());
  100. return $r_run;
  101. }
  102. // Small query
  103. function small_query($query) {
  104. $sql_small = "$query";
  105. $q_small = mysql_query($sql_small) or die(mysql_error());
  106. $r_small = mysql_fetch_array($q_small);
  107. return $r_small;
  108. }
  109. // Small query count
  110. function small_count($query) {
  111. $sql_small = "$query";
  112. $q_small = mysql_query($sql_small) or die(mysql_error());
  113. $r_small = mysql_num_rows($q_small);
  114. return $r_small;
  115. }
  116. // uid generator
  117. function str_rand($length = 8, $seeds = 'abcdefghijklmnopqrstuvwxyz0123456789')
  118. {
  119. $str = '';
  120. $seeds_count = strlen($seeds);
  121. // Seed
  122. list($usec, $sec) = explode(' ', microtime());
  123. $seed = (float) $sec + ((float) $usec * 100000);
  124. mt_srand($seed);
  125. // Generate
  126. for ($i = 0; $length > $i; $i++) {
  127. $str .= $seeds{mt_rand(0, $seeds_count - 1)};
  128. }
  129. return $str;
  130. }
  131. function zero_out($data) {
  132. if (!is_array($data)) return($data);
  133. foreach($data as $key => $value) {
  134. if ($value == '0') $data[$key] = '';
  135. }
  136. return($data);
  137. }
  138. function get_dp($number) {
  139. $dp = number_format($number, 2, '.', '');
  140. return ($dp);
  141. }
  142. function sec2min($number) {
  143. $dp = $number/60;
  144. $dp = number_format($dp, 2, '.', '');
  145. return ($dp);
  146. }
  147. function sec2hour($number) {
  148. $dp = $number/3600;
  149. $dp = number_format($dp, 2, '.', '');
  150. return ($dp);
  151. }
  152. function un_ut($name) {
  153. $gname = str_replace("Botpack.", "", "$name");
  154. $gname = str_replace("Class ", "", "$gname");
  155. $gname = str_replace("CTFGame", "Capture The Flag", "$gname");
  156. $gname = str_replace(".unr", "", "$gname");
  157. return ($gname);
  158. }
  159. function mtimestamp($date) {
  160. $hour = substr($date, 8, 2);
  161. $minute = substr($date, 10, 2);
  162. $second = 00;
  163. $day = substr($date, 6, 2);
  164. $month = substr($date, 4, 2);
  165. $year = substr($date, 0, 4);
  166. return(mktime($hour,$minute,$second,$month,$day,$year));
  167. }
  168. function mdate($date) {
  169. $ourdate = date('D, M j Y \a\t g:i a', mtimestamp($date));
  170. return ($ourdate);
  171. }
  172. function mdate2($date) {
  173. $hour = substr("$date", 8, 2);
  174. $minute = substr("$date", 10, 2);
  175. $second = "00";
  176. $day = substr("$date", 6, 2);
  177. $month = substr("$date", 4, 2);
  178. $year = substr("$date", 0, 4);
  179. $ourdate = mktime($hour,$minute,$second,$month,$day,$year);
  180. $ourdate = date('Y-m-d g:i a', $ourdate);
  181. return ($ourdate);
  182. }
  183. function utdate($gametime) {
  184. $year = substr("$gametime", 0, 4);
  185. $month = substr("$gametime", 5, 2);
  186. $day = substr("$gametime", 8, 2);
  187. $hour = substr("$gametime", 11, 2);
  188. $minute = substr("$gametime", 14, 2);
  189. $second = substr("$gametime", 17, 2);
  190. $gametime = $year . $month . $day . $hour . $minute . $second;
  191. return ($gametime);
  192. }
  193. // UT Server Query Functions
  194. function GetItemInfo ($itemname, $itemchunks)
  195. {
  196. $retval = "N/A";
  197. for ($i = 0; $i < count($itemchunks); $i++) {
  198. //Found this item
  199. if (strcasecmp($itemchunks[$i], $itemname) == 0) {
  200. $retval = $itemchunks[$i+1];
  201. }
  202. }
  203. //Return value
  204. return $retval;
  205. }
  206. function GetMinutes($seconds)
  207. {
  208. $timemins = intval($seconds / 60);
  209. $timesecs = ($seconds % 60);
  210. $Reqlength = 2; //Amount of digits we need
  211. if ($Reqlength-strlen($timemins) > 0) $timemins = str_repeat("0",($Reqlength-strlen($timemins))) . $timemins;
  212. if ($Reqlength-strlen($timesecs) > 0) $timesecs = str_repeat("0",($Reqlength-strlen($timesecs))) . $timesecs;
  213. return $timemins . ":" . $timesecs;
  214. }
  215. function FlagImage($country, $mini = true) {
  216. global $a_countries;
  217. $width = ($mini) ? 15 : 18;
  218. $height = ($mini) ? 10 : 12;
  219. if (empty($country)) return('');
  220. if (!file_exists("images/flags/$country.png")) return(''); //18*12
  221. $countryname = (isset($a_countries[$country])) ? $a_countries[$country] : '';
  222. return('<img src="images/flags/'. $country .'.png" width="'.$width.'" height="'.$height.'" style="border:0;" alt="'. $country .'" title="'. $countryname .'">');
  223. }
  224. function RankMovement($diff) {
  225. $diff = round($diff, 2);
  226. if ($diff == 0) {
  227. $chimg = 'same';
  228. $chtext = "ranking not affected";
  229. }
  230. if ($diff > 0) {
  231. $chimg = 'up';
  232. $chtext = "gained ". get_dp($diff) ." ranking points";
  233. }
  234. if ($diff < 0) {
  235. $chimg = 'down';
  236. $chtext = "lost ". get_dp($diff * -1) ." ranking points";
  237. }
  238. $moveimg = '';
  239. if (file_exists("images/ranks/$chimg.png")) {
  240. $infos = getimagesize("images/ranks/$chimg.png");
  241. $width = $infos[0];
  242. $height = $infos[1];
  243. $moveimg = '<img src="images/ranks/'. $chimg .'.png" width="'.$width.'" height="'.$height.'" style="border:0;" alt="" title="'. $chtext .'">';
  244. }
  245. return($moveimg);
  246. }
  247. function ordinal($number) {
  248. // when fed a number, adds the English ordinal suffix. Works for any
  249. // number, even negatives
  250. if ($number % 100 > 10 && $number %100 < 14):
  251. $suffix = "th";
  252. else:
  253. switch($number % 10) {
  254. case 0:
  255. $suffix = "th";
  256. break;
  257. case 1:
  258. $suffix = "st";
  259. break;
  260. case 2:
  261. $suffix = "nd";
  262. break;
  263. case 3:
  264. $suffix = "rd";
  265. break;
  266. default:
  267. $suffix = "th";
  268. break;
  269. }
  270. endif;
  271. return $suffix;
  272. }
  273. function RankImageOrText($pid, $name, $rank, $gid, $gamename, $mini = true, $format = NULL, $rankchange = NULL) {
  274. $points = 0;
  275. if (empty($rank)) {
  276. $r_rank = small_query("SELECT rank FROM uts_rank WHERE pid = '$pid' AND gid= '$gid';");
  277. if (!$r_rank) return('');
  278. $points = get_dp($r_rank['rank']);
  279. $r_no = small_query("SELECT (COUNT(*) + 1) AS no FROM uts_rank WHERE gid = '$gid' and rank > ${points}9");
  280. $rank = $r_no['no'];
  281. }
  282. $ranktext = $rank.ordinal($rank);
  283. if (file_exists("images/ranks/$rank.png")) {
  284. $width = ($mini) ? 14 : 16;
  285. $height = ($mini) ? 10 : 13;
  286. $img = '<img src="images/ranks/'. $rank .'.png" width="'.$width.'" height="'.$height.'" style="border:0;" alt="'. $rank .'" title="'. $ranktext .' in '. $gamename .'">';
  287. } else {
  288. $img = '';
  289. }
  290. $moveimg = '';
  291. if ($rankchange !== NULL) {
  292. $moveimg = ' '. RankMovement($rankchange);
  293. }
  294. if (empty($format)) {
  295. if ($img) {
  296. return($img.$moveimg);
  297. } else {
  298. return('<span class="rangtext">('.$ranktext.$moveimg.')</span>');
  299. }
  300. }
  301. $imageortext = ($img) ? $img : $ranktext;
  302. $search = array('%RT%', '%RN%', '%RP%', '%RI%', '%GN%', '%PN%', '%IT%');
  303. $replace = array($ranktext, $rank, $points, $img, $gamename, $name, $imageortext);
  304. return(str_replace($search, $replace, $format));
  305. }
  306. function FormatPlayerName($country, $pid, $name, $gid = NULL, $gamename = NULL, $mini = true, $rankchange = NULL) {
  307. static $cache = array();
  308. if (isset($cache[$pid])) return($cache[$pid]);
  309. $ranktext = false;
  310. if (!empty($gamename) and $pid !== NULL) {
  311. $ranktext = RankImageOrText($pid, $name, 0, $gid, $gamename, $mini, NULL, $rankchange);
  312. }
  313. $ret = '';
  314. if (!empty($country)) $ret .= FlagImage($country, $mini) ." ";
  315. $ret .= htmlentities($name);
  316. if ($ranktext) $ret .= " " . $ranktext;
  317. $cache[$pid] = $ret;
  318. return($ret);
  319. }
  320. function QuoteHintText($text) {
  321. $search = array('\\', '\'', '(', ')');
  322. $replace = array('\\\\', '\\\'', '\\(', '\\)');
  323. return(str_replace($search, $replace, $text));
  324. }
  325. function OverlibPrintHint($name, $text = NULL, $caption = NULL) {
  326. include(dirname(__FILE__) .'/hints.php');
  327. if (!isset($hint[$name]) and empty($text)) return('');
  328. if ($text === NULL) $text = $hint[$name]['text'];
  329. if ($caption === NULL and isset($hint[$name]['caption'])) $caption = $hint[$name]['caption'];
  330. $rv = 'onmouseover="return overlib(\''. QuoteHintText($text) .'\'';
  331. if ($caption !== NULL) $rv .= ', CAPTION, \''. QuoteHintText($caption) .'\'';
  332. $rv .= ');" ';
  333. $rv .= 'onmouseout="return nd();"';
  334. return($rv);
  335. }
  336. function debug_output($desc, $data) {
  337. echo '<div align="left"><pre>';
  338. echo $desc .": ";
  339. $len = strlen($data);
  340. for ($i = 0; $i < $len; $i++) {
  341. echo substr($data, $i, 1) .' ';
  342. }
  343. echo "\n";
  344. echo str_repeat(' ', (strlen($desc) + 2));
  345. for ($i = 0; $i < $len; $i++) {
  346. echo ord(substr($data, $i, 1)) .' ';
  347. }
  348. echo "</pre></div>";
  349. }
  350. function check_extension($name) {
  351. if (extension_loaded($name)) return(true);
  352. if( !(bool)ini_get("enable_dl") or (bool)ini_get( "safe_mode" )) return(false);
  353. $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
  354. return(@dl($prefix . $name . PHP_SHLIB_SUFFIX));
  355. }
  356. function compress_file($method, $in, $out, $stripx00) {
  357. if ((!file_exists($out) and !is_writeable(dirname($out))) or (file_exists($out) and !is_writable($out))) return(false);
  358. $blocksize = 8192;
  359. switch($method) {
  360. case 'bz2': $suffix = '.bz2'; break;
  361. case 'zlib': $suffix = '.gz'; break;
  362. case 'none': $suffix = ''; break;
  363. default: return(false);
  364. }
  365. if (substr($out, strlen($out) - strlen($suffix)) != $suffix) $out .= $suffix;
  366. $fp_in = fopen($in, 'rb');
  367. if (!$fp_in) return(false);
  368. switch($method) {
  369. case 'bz2': $fp_out = @bzopen($out, 'wb'); break;
  370. case 'zlib': $fp_out = @gzopen($out, 'wb6'); break;
  371. case 'none': $fp_out = @fopen($out, 'wb'); break;
  372. }
  373. if (!$fp_out) return(false);
  374. while (!feof($fp_in)) {
  375. $buffer = @fread($fp_in, $blocksize);
  376. if ($buffer === false) return(false);
  377. if ($stripx00) $buffer = preg_replace('/[\x00]/', '', $buffer);
  378. switch($method) {
  379. case 'bz2': $bytes = @bzwrite($fp_out, $buffer, strlen($buffer)); break;
  380. case 'zlib': $bytes = @gzwrite($fp_out, $buffer, strlen($buffer)); break;
  381. case 'none': $bytes = @fwrite($fp_out, $buffer, strlen($buffer)); break;
  382. }
  383. if ($bytes === false) return(false);
  384. }
  385. @fclose($fp_in);
  386. switch($method) {
  387. case 'bz2': @bzclose($fp_out); break;
  388. case 'zlib': @gzclose($fp_out); break;
  389. case 'none': @fclose($fp_out); break;
  390. }
  391. return(true);
  392. }
  393. function backup_logfile($method, $filename, $backupfilename, $stripx00) {
  394. switch($method) {
  395. case 'compress':
  396. if (!check_extension('bz2') or !compress_file('bz2', $filename, $backupfilename, $stripx00)) {
  397. return(backup_logfile('gzip', $filename, $backupfilename, $stripx00));
  398. }
  399. return('Succeeded (bz2)');
  400. break;
  401. case 'gzip':
  402. if (!check_extension('zlib') or !compress_file('zlib', $filename, $backupfilename, $stripx00)) {
  403. return(backup_logfile('yes', $filename, $backupfilename, $stripx00));
  404. }
  405. return('Succeeded (gzip)');
  406. break;
  407. case 'no':
  408. return('NO (disabled in config)');
  409. break;
  410. default:
  411. if ($stripx00) {
  412. if (compress_file('none', $filename, $backupfilename, $stripx00)) {
  413. return('Succeeded (uncompressed)');
  414. } else {
  415. return('FAILED' . (!empty($php_errormsg) ? ': '. $php_errormsg : ''));
  416. }
  417. }
  418. if (@copy($filename, $backupfilename)) {
  419. return('Succeeded (uncompressed)');
  420. } else {
  421. return('FAILED' . (!empty($php_errormsg) ? ': '. $php_errormsg : ''));
  422. }
  423. }
  424. }
  425. function purge_backups($dir, $maxage) {
  426. if (empty($maxage) or rand(0, 5) != 0) return(NULL);
  427. // $maxage is days but we need seconds
  428. $maxage = $maxage * 86400;
  429. $deleted = 0;
  430. $dh = opendir($dir);
  431. while (false !== ($filename = readdir($dh))) {
  432. if ($filename == '.htaccess' or $filename == 'index.htm') continue;
  433. $cna = $dir .'/'. $filename;
  434. if (@is_file($cna) and (@filemtime($cna) + $maxage) < time()) {
  435. unlink($cna);
  436. $deleted++;
  437. }
  438. }
  439. closedir($dh);
  440. return($deleted);
  441. }
  442. function file_size_info($filesize) {
  443. $bytes = array('KB', 'KB', 'MB', 'GB', 'TB'); # values are always displayed
  444. if ($filesize < 1024) $filesize = 1; # in at least kilobytes.
  445. for ($i = 0; $filesize > 1024; $i++) $filesize /= 1024;
  446. $file_size_info['size'] = ceil($filesize);
  447. $file_size_info['type'] = $bytes[$i];
  448. return $file_size_info;
  449. }
  450. function GetCurrentWatchlist() {
  451. if (!isset($_COOKIE['uts_watchlist'])) return(array());
  452. $watchlist = @explode(',', $_COOKIE['uts_watchlist']);
  453. if (!$watchlist or !is_array($watchlist)) return(array());
  454. foreach($watchlist as $key => $value) {
  455. $watchlist[$key] = addslashes($value);
  456. }
  457. return($watchlist);
  458. }
  459. function PlayerOnWatchlist($pid) {
  460. $watchlist = GetCurrentWatchlist();
  461. return(in_array($pid, $watchlist));
  462. }
  463. function ToggleWatchStatus($pid) {
  464. $watchlist = GetCurrentWatchlist();
  465. if (in_array($pid, $watchlist)) {
  466. $key = array_search($pid, $watchlist);
  467. unset($watchlist[$key]);
  468. $status = 0;
  469. } else {
  470. $watchlist[] = $pid;
  471. $status = 1;
  472. }
  473. setcookie('uts_watchlist', implode(',',$watchlist), time()+60*60*24*30*365*5);
  474. return($status);
  475. }
  476. ?>