PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/global_funcs.php

https://github.com/brandonlamb/aatraders2
PHP | 566 lines | 465 code | 81 blank | 20 comment | 98 complexity | 128630ae05963a1d10117036dad930f0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0, AGPL-1.0
  1. <?php
  2. // This program is free software; you can redistribute it and/or modify it
  3. // under the terms of the GNU General Public License as published by the
  4. // Free Software Foundation; either version 2 of the License, or (at your
  5. // option) any later version.
  6. //
  7. // File: global_funcs.php
  8. if (preg_match("/global_funcs.php/i", $_SERVER['PHP_SELF']))
  9. {
  10. echo "You can not access this file directly!";
  11. die();
  12. }
  13. if ((!isset($_SESSION['langdir'])) || ($_SESSION['langdir'] == ''))
  14. {
  15. $_SESSION['langdir'] = $default_lang;
  16. }
  17. $langdir = $_SESSION['langdir'];
  18. include ("languages/$langdir/lang_common.inc");
  19. if (!preg_match("/create_game.php/i", $_SERVER['PHP_SELF']))
  20. {
  21. $shiptypes = $db->GetToFieldArray("SELECT * FROM {$db_prefix}ship_types", '', 'type_id');
  22. }
  23. function checklogin()
  24. {
  25. $flag = 0;
  26. global $db, $db_prefix, $playerinfo, $shipinfo, $sectorinfo, $galaxyinfo, $shipdevice, $shipcommodities, $galaxyinfo;
  27. global $enable_spies, $response, $player_onlinetime_left, $playeroutoftime;
  28. global $l_global_needlogin, $l_global_died, $l_login_died, $l_die_please;
  29. global $start_fighters, $start_armor, $start_energy, $noreturn, $silent, $create_game, $refreshcount, $refresh_max, $idle_max;
  30. global $server_closed, $player_online_timelimit, $l_global_limitreached;
  31. global $l_login_closed_message, $onlinetime_left, $tournament_setup_access, $enable_mass_logging;
  32. //echo print_r($_SESSION) . "<br>";
  33. if(!array_key_exists ('session_player_id', $_SESSION))
  34. {
  35. if($noreturn != 1)
  36. {
  37. echo $l_global_needlogin;
  38. }
  39. return 1;
  40. }
  41. if ($_SESSION['session_player_id'] == '' || empty($_SESSION['session_player_id']))
  42. {
  43. if($noreturn != 1)
  44. {
  45. echo $l_global_needlogin;
  46. }
  47. return 1;
  48. }
  49. $temp = $silent;
  50. $silent = 1;
  51. $debug_query = $db->SelectLimit("SELECT {$db_prefix}players.*, {$db_prefix}teams.team_name, {$db_prefix}teams.id
  52. FROM {$db_prefix}players
  53. LEFT JOIN {$db_prefix}teams ON {$db_prefix}players.team = {$db_prefix}teams.id
  54. WHERE {$db_prefix}players.player_id=$_SESSION[session_player_id]", 1);
  55. db_op_result($debug_query,__LINE__,__FILE__);
  56. // $debug_query = $db->SelectLimit("SELECT * FROM {$db_prefix}players WHERE player_id='$_SESSION[session_player_id]'", 1);
  57. $playerinfo = $debug_query->fields;
  58. $_SESSION['character_name'] = $playerinfo['character_name'];
  59. $_SESSION['team_name'] = $playerinfo['team_name'];
  60. $_SESSION['team_id'] = $playerinfo['id'];
  61. $_SESSION['langdir'] = $playerinfo['langdir'];
  62. $debug_query->close();
  63. if ($playerinfo['logged_out'] == 'Y')
  64. {
  65. if($noreturn != 1)
  66. echo $l_global_needlogin;
  67. return 1;
  68. }
  69. if($playerinfo['allow_avatar'] == 0)
  70. $playerinfo['avatar'] = '';
  71. // Check sessionid against database
  72. if (session_id() != $playerinfo['sessionid'])
  73. {
  74. echo $l_global_needlogin;
  75. $flag = 1;
  76. }
  77. $stamp = date("Y-m-d H:i:s");
  78. $time_diff = (TIME() - strtotime($playerinfo['last_login']));
  79. $delay_flag = 0;
  80. if(empty($_SESSION['artifact']) || !isset($_SESSION['artifact']))
  81. {
  82. $_SESSION['artifact'] = 0;
  83. }
  84. if(((TIME() - $_SESSION['artifact']) / 60) > $idle_max)
  85. {
  86. $_SESSION['artifact'] = 0;
  87. }
  88. if($time_diff / 60 > $idle_max || $refreshcount >= $refresh_max)
  89. {
  90. // echo rawurldecode($_SERVER['PHP_SELF']) . "</br>";
  91. $_SESSION['artifact'] = 0;
  92. if (stristr($_SERVER['PHP_SELF'], "team_forum.php") ||
  93. stristr($_SERVER['PHP_SELF'], "casino.php") ||
  94. stristr($_SERVER['PHP_SELF'], "message_send.php") ||
  95. stristr($_SERVER['PHP_SELF'], "feedback.php") ||
  96. stristr($_SERVER['PHP_SELF'], "sector_notes.php"))
  97. {
  98. $flag = 1;
  99. $delay_flag = 1;
  100. }
  101. else
  102. {
  103. $stamp = date("Y-m-d H:i:s", (TIME() - 360));
  104. $debug_query = $db->Execute("UPDATE {$db_prefix}players SET last_login='$stamp', logged_out='Y', profile_cached='Y', player_total_online=player_total_online+$time_diff, player_online_time=player_online_time+$time_diff, sessionid='' WHERE player_id = $playerinfo[player_id]");
  105. session_destroy();
  106. if($noreturn != 1)
  107. {
  108. echo $l_global_needlogin;
  109. }
  110. return 1;
  111. }
  112. }
  113. $onlinetime_left = ($player_online_timelimit == 0) ? 0 : $player_online_timelimit - floor(($playerinfo['player_online_time'] + $time_diff) / 60);
  114. $player_onlinetime_left = ($player_online_timelimit * 60) - ($playerinfo['player_online_time'] + $time_diff);
  115. $playeroutoftime = 0;
  116. if($player_online_timelimit != 0 && $onlinetime_left < 0)
  117. {
  118. $playeroutoftime = 1;
  119. $tournament_setup_access = 1;
  120. }
  121. if($tournament_setup_access == 1)
  122. {
  123. $time_diff = 0;
  124. }
  125. $silent = $temp;
  126. $debug_query = $db->SelectLimit("SELECT * FROM {$db_prefix}ships WHERE player_id=$playerinfo[player_id] AND ship_id=$playerinfo[currentship]", 1);
  127. db_op_result($debug_query,__LINE__,__FILE__);
  128. $shipinfo = $debug_query->fields;
  129. $debug_query->close();
  130. $shipdevice = $db->GetToFieldArray("SELECT * FROM {$db_prefix}ship_devices WHERE ship_id=$playerinfo[currentship]", '', 'class');
  131. $shipcommodities = $db->GetToFieldArray("SELECT * FROM {$db_prefix}ship_holds WHERE ship_id=$playerinfo[currentship]", '', 'cargo_name');
  132. $result2 = $db->SelectLimit("SELECT {$db_prefix}universe.*, {$db_prefix}universe_galaxy.galaxy_name FROM {$db_prefix}universe, {$db_prefix}universe_galaxy WHERE {$db_prefix}universe.sector_id='$shipinfo[sector_id]' and {$db_prefix}universe_galaxy.galaxy_id={$db_prefix}universe.galaxy_id", 1);
  133. db_op_result($result2,__LINE__,__FILE__);
  134. $sectorinfo = $result2->fields;
  135. $result2->close();
  136. $result2 = $db->SelectLimit("SELECT * FROM {$db_prefix}universe_galaxy WHERE galaxy_id='$sectorinfo[galaxy_id]'", 1);
  137. db_op_result($result2,__LINE__,__FILE__);
  138. $galaxyinfo = $result2->fields;
  139. $result2->close();
  140. if ($shipinfo['cleared_defenses'] > ' ')
  141. {
  142. header("location: $shipinfo[cleared_defenses]\n");
  143. }
  144. if ($playerinfo['destroyed'] == "Y") // Check for destroyed ship
  145. {
  146. if ($shipdevice['dev_escapepod']['amount'] == 1) // If the player has an escapepod, set the player up with a new ship.
  147. {
  148. include ("globals/player_ship_destroyed.inc");
  149. player_ship_destroyed($shipinfo['ship_id'], $playerinfo['player_id'], $playerinfo['rating'], 0, 0);
  150. echo $l_login_died;
  151. $flag = 1;
  152. }
  153. else
  154. {
  155. if(basename($_SESSION['currentprogram']) != 'login_process.pgp' && basename($_SESSION['currentprogram']) != 'log.php')
  156. {
  157. echo $l_global_died . "<br>";
  158. echo $l_die_please . "<br>";
  159. echo $l_global_needlogin . "<br>";
  160. $flag = 1;
  161. }
  162. }
  163. }
  164. if ($playerinfo['destroyed'] == "K") // Check for destroyed ship
  165. {
  166. $debug_query = $db->Execute("UPDATE {$db_prefix}players SET destroyed='N' WHERE player_id=" . $playerinfo['player_id']);
  167. db_op_result($debug_query,__LINE__,__FILE__);
  168. }
  169. if ($server_closed && $flag == 0 && $playerinfo['admin'] == 0)
  170. {
  171. echo $l_login_closed_message;
  172. $flag = 1;
  173. }
  174. if($flag != 1)
  175. {
  176. $debug_query = $db->Execute("UPDATE {$db_prefix}players SET last_login='$stamp', player_total_online=player_total_online+$time_diff, player_online_time=player_online_time+$time_diff WHERE player_id = $playerinfo[player_id]");
  177. }
  178. if($playerinfo['admin_extended_logging'] == 1 || $enable_mass_logging == 1)
  179. {
  180. $get_data = "";
  181. $post_data = "";
  182. if (!empty($_GET)) {
  183. $get_data = print_r($_GET, true);
  184. } else if (!empty($HTTP_GET_VARS)) {
  185. $get_data = print_r($HTTP_GET_VARS, true);
  186. }
  187. if (!empty($_POST)) {
  188. $post_data = print_r($_POST, true);
  189. } else if (!empty($HTTP_POST_VARS)) {
  190. $post_data = print_r($HTTP_POST_VARS, true);
  191. }
  192. $query = "INSERT INTO {$db_prefix}admin_extended_logging (player_id, time, request_uri, get_data, post_data, score, ip_address, currentship, credits, turns, player_online_time)
  193. VALUES ($playerinfo[player_id], '$stamp', " . $db->qstr($_SERVER['REQUEST_URI']) . ", " . $db->qstr($get_data) . ", " . $db->qstr($post_data) . ", $playerinfo[score], '$playerinfo[ip_address]', $playerinfo[currentship], $playerinfo[credits], $playerinfo[turns], $player_onlinetime_left)";
  194. $debug_query = $db->Execute($query);
  195. db_op_result($debug_query,__LINE__,__FILE__);
  196. }
  197. if($delay_flag == 1)
  198. {
  199. $flag = 0;
  200. }
  201. return $flag;
  202. }
  203. function mypw($one,$two)
  204. {
  205. return exp($two * log(max($one, 1)));
  206. }
  207. function MaxCreditsPOW($one,$two)
  208. {
  209. global $max_tech_level;
  210. $increment = 0;
  211. $oldvalue = 0;
  212. $breakpoint = floor($max_tech_level * 0.435);
  213. if($two > $breakpoint)
  214. {
  215. $oldvalue = pow($one,$breakpoint);
  216. $multiplier = pow($one,$breakpoint) - pow($one,($breakpoint - 1));
  217. $numberlevels = $two - $breakpoint;
  218. $newvalue = $numberlevels * ($multiplier + pow($one, ($two - $breakpoint)));
  219. $breakpoint2 = floor($max_tech_level * 0.62);
  220. if($two > $breakpoint2)
  221. {
  222. $numberlevels = $breakpoint2 - $breakpoint;
  223. $newvalue = $numberlevels * ($multiplier + pow($one, ($breakpoint2 - $breakpoint)));
  224. $numberlevels = $breakpoint2 - $breakpoint + 1;
  225. $newvalue2 = $numberlevels * ($multiplier + pow($one, ($breakpoint2 - $breakpoint)));
  226. $increment = $newvalue2 - $newvalue;
  227. $newvalue = ($increment * ($two - $breakpoint)) + (110000 * ($two - $breakpoint2));
  228. return $newvalue + $oldvalue;
  229. }
  230. else
  231. {
  232. return $newvalue + $oldvalue;
  233. }
  234. }
  235. else
  236. {
  237. return pow($one,$two);
  238. }
  239. }
  240. function phpChangeDelta($desiredvalue,$currentvalue)
  241. {
  242. global $upgrade_cost, $upgrade_factor;
  243. return (mypw($upgrade_factor, $desiredvalue) - mypw($upgrade_factor, $currentvalue)) * $upgrade_cost;
  244. }
  245. function phpChangePlanetDelta($desiredvalue,$currentvalue)
  246. {
  247. global $upgrade_cost, $planet_upgrade_factor;
  248. return (mypw($planet_upgrade_factor, $desiredvalue) - mypw($planet_upgrade_factor, $currentvalue)) * $upgrade_cost;
  249. }
  250. function phpChangePlanetSDDelta($desiredvalue,$currentvalue)
  251. {
  252. global $upgrade_cost, $planet_SD_upgrade_factor;
  253. return (mypw($planet_SD_upgrade_factor, $desiredvalue) - mypw($planet_SD_upgrade_factor, $currentvalue)) * $upgrade_cost;
  254. }
  255. function phpMaxCreditsDelta($desiredvalue,$currentvalue)
  256. {
  257. global $upgrade_cost, $planet_upgrade_factor;
  258. return MaxCreditsPOW($planet_upgrade_factor, $desiredvalue) * $upgrade_cost;
  259. }
  260. function NUMBER($number, $decimals = 0)
  261. {
  262. global $local_number_dec_point;
  263. global $local_number_thousands_sep;
  264. return number_format($number, $decimals, $local_number_dec_point, $local_number_thousands_sep);
  265. }
  266. function NUM_PER_LEVEL($level)
  267. {
  268. global $level_factor;
  269. return round(mypw($level_factor, $level) * 10);
  270. }
  271. function NUM_HOLDS($level_hull)
  272. {
  273. return NUM_PER_LEVEL($level_hull);
  274. }
  275. function NUM_ENERGY($level_power)
  276. {
  277. global $level_factor;
  278. return round(mypw($level_factor, $level_power) * 50);
  279. }
  280. function NUM_FIGHTERS($level_fighter)
  281. {
  282. return NUM_PER_LEVEL($level_fighter);
  283. }
  284. function NUM_TORPEDOES($level_torp_launchers)
  285. {
  286. return NUM_PER_LEVEL($level_torp_launchers);
  287. }
  288. function NUM_armor($level_armor)
  289. {
  290. return NUM_PER_LEVEL($level_armor);
  291. }
  292. function NUM_SENSORS($level_sensors)
  293. {
  294. return NUM_PER_LEVEL($level_sensors);
  295. }
  296. function NUM_BEAMS($level_beams)
  297. {
  298. return NUM_PER_LEVEL($level_beams);
  299. }
  300. function NUM_SHIELDS($level_shields)
  301. {
  302. return NUM_PER_LEVEL($level_shields);
  303. }
  304. function SCAN_SUCCESS($level_scan, $level_cloak, $hullsize = 0)
  305. {
  306. if($hullsize > 0)
  307. {
  308. $level_cloak += ( 150 - $hullsize);
  309. }
  310. $level_scan = max(0.01, $level_scan);
  311. $level_cloak = max(0.01, $level_cloak);
  312. $success = $level_scan / $level_cloak;
  313. if($success > 1)
  314. {
  315. $success = -$level_cloak / $level_scan;
  316. $success = 50 + (50 - (abs($success) * 50));
  317. }
  318. else if($success == 1)
  319. {
  320. $success = 50;
  321. }
  322. else
  323. {
  324. $success = ($success * 50) - 10;
  325. }
  326. $success = min(max($success, 1), 90);
  327. return floor($success);
  328. }
  329. function SCAN_ERROR($level_scan, $level_cloak, $correct_value)
  330. {
  331. $level_scan = max(0.01, $level_scan);
  332. $level_cloak = max(0.01, $level_cloak);
  333. $success = $level_scan / $level_cloak;
  334. if($success > 1)
  335. {
  336. $success = -$level_cloak / $level_scan;
  337. $success = 50 + (50 - (abs($success) * 50));
  338. }
  339. else if($success == 1)
  340. {
  341. $success = 50;
  342. }
  343. else
  344. {
  345. $success = ($success * 50) - 10;
  346. }
  347. $success = min(max($success, 1), 100);
  348. $sc_error = 100 - floor($success);
  349. if(mt_rand(1, 100) <= $sc_error)
  350. {
  351. // scan errored and returned false info
  352. $halfpercent = floor($sc_error / 2);
  353. $sc_error = floor($correct_value * (mt_rand((100000 - ($halfpercent * 1000)), (100000 + ($halfpercent * 1000)))) / 100000);
  354. }
  355. else
  356. {
  357. // scan worked and returned almost correct info
  358. // 99% to 101% of the correct value
  359. $sc_error = floor($correct_value * (mt_rand(99999, 100999) / 100000));
  360. $sc_error = $correct_value;
  361. }
  362. return $sc_error;
  363. }
  364. function get_dirlist($dirPath)
  365. {
  366. if ($handle = opendir($dirPath))
  367. {
  368. while (false !== ($file = readdir($handle)))
  369. if ($file != "." && $file != "..")
  370. $filesArr[] = trim($file);
  371. closedir($handle);
  372. }
  373. return $filesArr;
  374. }
  375. function update_player_experience($player_id, $experience){
  376. global $db, $db_prefix;
  377. if ($experience==""){
  378. $experience=0;
  379. }
  380. $debug_query = $db->Execute("UPDATE {$db_prefix}players SET experience=GREATEST(experience + $experience, 0) WHERE player_id=$player_id");
  381. db_op_result($debug_query,__LINE__,__FILE__);
  382. // adminlog("LOG0_RAW","UPDATE {$db_prefix}players SET experience=GREATEST(experience + $experience, 0) WHERE player_id=$player_id");
  383. }
  384. // remove all non-numerics but leave decimal point
  385. function stripnum($str)
  386. {
  387. $str = (string)$str;
  388. $output = AAT_ereg_replace("[^0-9.]","",$str);
  389. return (float)$output;
  390. }
  391. //remove all non-numerics
  392. function StripNonNum($str)
  393. {
  394. $str=(string)$str;
  395. $output = AAT_ereg_replace("[^0-9]","",$str);
  396. return $output;
  397. }
  398. function close_database(){
  399. global $db;
  400. // $db->close();
  401. }
  402. function sign( $data )
  403. {
  404. if ($data > 0)
  405. {
  406. return 1;
  407. }
  408. elseif ($data < 0)
  409. {
  410. return -1;
  411. }
  412. else
  413. {
  414. return 0;
  415. }
  416. }
  417. function db_op_result($query,$served_line,$served_page)
  418. {
  419. global $db, $db_prefix, $silent, $_SERVER, $cumulative, $db_type;
  420. if ($db->ErrorNo() == 0)
  421. {
  422. if (!$silent)
  423. {
  424. echo "<font color=\"lime\">- operation completed successfully.</font><br>\n";
  425. }
  426. }
  427. else
  428. {
  429. $temp_error = $db->ErrorMsg();
  430. $dberror = "A Database error occurred in " . $served_page . " on line " . ($served_line-1) . " (called from: $_SERVER[PHP_SELF]): " . $temp_error . " - Original SQL: " . $db->sql;
  431. $dberror = AAT_ereg_replace("'","&#039;",$dberror); // Allows the use of apostrophes.
  432. adminlog("LOG0_ADMIN_DBERROR", $dberror);
  433. $cumulative = 1; // For areas with multiple actions needing status - 0 is all good so far, 1 is at least one bad.
  434. if(strstr(AAT_strtolower($temp_error), "can't open file") and strstr(AAT_strtolower($temp_error), ".myi") and strstr($temp_error, "145")){
  435. $deoperror = 1;
  436. adminlog("LOG0_ADMIN_DBERROR","Running sched_repair.inc to repair table.");
  437. include ("scheduler/sched_repair.inc");
  438. }
  439. if (!$silent)
  440. {
  441. echo "<font color=\"red\">- failed to complete database operation in $served_page on line " .($served_line-1). ". Error code follows:\n";
  442. echo "<hr>\n";
  443. echo $temp_error . "<br><br>Original SQL: " . $db->sql;
  444. echo "<hr>\n";
  445. echo "</font><br>\n";
  446. }
  447. }
  448. }
  449. function template_display($templatename, $templatefile)
  450. {
  451. global $template_object;
  452. if(is_file($gameroot.$templatename.$templatefile)){
  453. $template_object->display($templatename.$templatefile);
  454. }
  455. else
  456. {
  457. $template_object->display("master_template/" . $templatefile);
  458. }
  459. }
  460. function playerlog($sid, $log_type, $data = '')
  461. {
  462. global $db, $db_prefix;
  463. // write log_entry to the player's log - identified by player's player_id - sid.
  464. if ($sid != '' && !empty($log_type))
  465. {
  466. $stamp = date("Y-m-d H:i:s");
  467. $debug_query = $db->Execute("INSERT INTO {$db_prefix}logs (player_id, type, time, data) VALUES($sid, '$log_type', '$stamp', " . $db->qstr($data) . ")");
  468. db_op_result($debug_query,__LINE__,__FILE__);
  469. }
  470. }
  471. function adminlog($log_type, $data = '')
  472. {
  473. global $db, $db_prefix;
  474. // Failures should be silent, since its the admin log.
  475. $silent = 1;
  476. // write log_entry to the admin log
  477. if (!empty($log_type))
  478. {
  479. $stamp = date("Y-m-d H:i:s");
  480. $debug_query = $db->Execute("INSERT INTO {$db_prefix}admin_log (type, time, data) VALUES('$log_type', '$stamp', " . $db->qstr($data) . ")");
  481. db_op_result($debug_query,__LINE__,__FILE__);
  482. }
  483. }
  484. ?>