PageRenderTime 87ms CodeModel.GetById 49ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions.php

https://bitbucket.org/capi/ogspy
PHP | 1456 lines | 962 code | 171 blank | 323 comment | 316 complexity | e40a25a03a088d24959edb13887a9bdc MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * OGSpy Global functions
  4. * @package OGSpy
  5. * @subpackage Common
  6. * @author Kyser
  7. * @copyright Copyright &copy; 2012, http://www.ogsteam.fr/
  8. * @version 3.1.1 ($Rev: 7690 $)
  9. * @modified $Date: 2012-08-19 21:49:20 +0200 (Sun, 19 Aug 2012) $
  10. * @link $HeadURL: http://svn.ogsteam.fr/trunk/ogspy/includes/functions.php $
  11. * $Id: functions.php 7690 2012-08-19 19:49:20Z darknoon $
  12. */
  13. if (!defined('IN_SPYOGAME')) {
  14. die("Hacking attempt");
  15. }
  16. /**
  17. * URL Redirection
  18. * @param string $url target URL
  19. */
  20. function redirection($url)
  21. {
  22. if (headers_sent()) {
  23. die('<meta http-equiv="refresh" content="0; URL=' . $url . '">');
  24. } else {
  25. header("Location: " . $url);
  26. exit();
  27. }
  28. }
  29. /**
  30. * Check if the folder is writable or not
  31. * @param string $path The file or the folder to check
  32. * @return boolean true if the file or the folder is writable
  33. * @link http://fr.php.net/manual/fr/function.is-writable.php#68598
  34. */
  35. function is__writable($path)
  36. {
  37. if ($path{strlen($path) - 1} == '/'){
  38. return is__writable($path . uniqid(mt_rand()) . '.tmp');
  39. }elseif (@ereg('.tmp', $path)) {
  40. if (!($f = @fopen($path, 'w+')))
  41. return false;
  42. fclose($f);
  43. unlink($path);
  44. return true;
  45. } else{
  46. die("return 0; // Or return error - invalid path...<br>" . getcwd() . "<br>$path");
  47. }
  48. }
  49. /**
  50. * Write a text or a table in a file
  51. * @param string $file Filename
  52. * @param string $mode File Opening Mode
  53. * @param string|Array $text String or table to write
  54. * @return boolean false if failed
  55. */
  56. function write_file($file, $mode, $text)
  57. {
  58. if ($fp = fopen($file, $mode)) {
  59. if (is_array($text)) {
  60. foreach ($text as $t) {
  61. fwrite($fp, rtrim($t));
  62. fwrite($fp, "\r\n");
  63. }
  64. } else {
  65. fwrite($fp, $text);
  66. fwrite($fp, "\r\n");
  67. }
  68. fclose($fp);
  69. return true;
  70. } else
  71. return false;
  72. }
  73. /**
  74. * Write a text or a table in a gz compressed file
  75. * @param string $file Filename
  76. * @param string $mode File Opening Mode
  77. * @param string|Array $text String or table to write
  78. * @return boolean false if failed
  79. */
  80. function write_file_gz($file, $mode, $text)
  81. {
  82. if ($fp = gzopen($file . ".gz", $mode)) {
  83. if (is_array($text)) {
  84. foreach ($text as $t) {
  85. gzwrite($fp, rtrim($t));
  86. gzwrite($fp, "\r\n");
  87. }
  88. } else {
  89. gzwrite($fp, $text);
  90. gzwrite($fp, "\r\n");
  91. }
  92. gzclose($fp);
  93. return true;
  94. } else
  95. return false;
  96. }
  97. /**
  98. * Convert an IP in Hex Format
  99. * @param string $ip format xxx.xxx.xxx.xxx in IPv4 and xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx in IPv6
  100. * @return string IP in hex : HHHHHHHH for IPv4 and HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH for IPv6
  101. */
  102. function encode_ip ($ip)
  103. {
  104. $d = explode('.', $ip);
  105. if (count($d) == 4) return sprintf('%02x%02x%02x%02x', $d[0], $d[1], $d[2], $d[3]);
  106. $d = explode(':', preg_replace('/(^:)|(:$)/', '', $ip));
  107. $res = '';
  108. foreach ($d as $x)
  109. $res .= sprintf('%0'. ($x == '' ? (9 - count($d)) * 4 : 4) .'s', $x);
  110. return $res;
  111. }
  112. /**
  113. * Convert an IP in Hex format to an IPv4 or IPv6 format
  114. * @param string $int_ip IP encoded
  115. * @return string $ip format xxx.xxx.xxx.xxx in IPv4 and xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx in IPv6
  116. */
  117. function decode_ip($int_ip)
  118. {
  119. if (strlen($int_ip) == 32) {
  120. $int_ip = substr(chunk_split($int_ip, 4, ':'), 0, 39);
  121. $int_ip = ':'. implode(':', array_map("hexhex", explode(':',$int_ip))) .':';
  122. preg_match_all("/(:0)+/", $int_ip, $zeros);
  123. if (count($zeros[0]) > 0) {
  124. $match = '';
  125. foreach($zeros[0] as $zero)
  126. if (strlen($zero) > strlen($match))
  127. $match = $zero;
  128. $int_ip = preg_replace('/'. $match .'/', ':', $int_ip, 1);
  129. }
  130. return preg_replace('/(^:([^:]))|(([^:]):$)/', '$2$4', $int_ip);
  131. }
  132. $hexipbang = explode('.', chunk_split($int_ip, 2, '.'));
  133. return hexdec($hexipbang[0]). '.' . hexdec($hexipbang[1]) . '.' . hexdec($hexipbang[2]) . '.' . hexdec($hexipbang[3]);
  134. }
  135. /**
  136. * Converts a hex value to another hew value (depnding of the current php version on the server)
  137. * @param string $value The initial hexvalue
  138. * @return string the new hew value
  139. */
  140. function hexhex($value) {
  141. return dechex(hexdec($value));
  142. };
  143. /**
  144. * Generates a random password with 6 chars
  145. * @return string $password The generated password
  146. */
  147. function password_generator()
  148. {
  149. $string = "abBDEFcdefghijkmnPQRSTUVWXYpqrst23456789";
  150. srand((double)microtime() * 1000000);
  151. $password = '';
  152. for ($i = 0; $i < 6; $i++) {
  153. $password .= $string[rand() % strlen($string)];
  154. }
  155. return $password;
  156. }
  157. /**
  158. * Initialisation of the cache for all Mod settings
  159. *
  160. * Generates a file which contains all configurations for different installed OGSpy Modules
  161. */
  162. function init_mod_cache()
  163. {
  164. global $cache_mod, $server_config;
  165. // Load cached config
  166. $filename = 'cache/cache_mod.php';
  167. if (file_exists($filename)) {
  168. include $filename;
  169. // regeneration si besoin
  170. if ((filemtime($filename) + $server_config['mod_cache']) < time()) {
  171. generate_mod_cache();
  172. }
  173. } else {
  174. generate_mod_cache();
  175. if (file_exists($filename)) {
  176. include $filename; // on reinjecte le fichier s'il existe'
  177. }
  178. }
  179. }
  180. /**
  181. * Initialisation of the cache for all Server settings
  182. *
  183. * Generates a file which contains all configurations for the OGSpy Server
  184. */
  185. function init_serverconfig()
  186. {
  187. global $server_config;
  188. // Load cached config
  189. $filename = 'cache/cache_config.php';
  190. if (file_exists($filename)) {
  191. include $filename;
  192. // regeneration si besoin
  193. if ((filemtime($filename) + $server_config['config_cache']) < time()) {
  194. generate_config_cache();
  195. }
  196. } else {
  197. generate_config_cache();
  198. if (file_exists($filename)) {
  199. include $filename; // on reinjecte le fichier s'il existe'
  200. }
  201. }
  202. }
  203. /**
  204. * Updates in the database all configurations displayed in the display administration Page.
  205. * @todo Query: update . TABLE_CONFIG . set config_value = . $pub_enable_portee_missil . where config_name = \'portee_missil\'
  206. * @todo Query: "update " . TABLE_CONFIG . " set config_value = " . $pub_galaxy_by_line_stat . " where config_name = 'galaxy_by_line_stat'"
  207. * @todo Query: "update " . TABLE_CONFIG . " set config_value = " . $pub_system_by_line_stat . " where config_name = 'system_by_line_stat'"
  208. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $pub_open_user ."' where config_name = 'open_user'"
  209. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $pub_open_admin . "' where config_name = 'open_admin'"
  210. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_enable_stat_view ." where config_name = 'enable_stat_view'"
  211. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_enable_members_view ." where config_name = 'enable_members_view'"
  212. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_nb_colonnes_ally) ."' where config_name = 'nb_colonnes_ally'"
  213. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($color_ally) . "' where config_name = 'color_ally'"
  214. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_galaxy_by_line_ally ." where config_name = 'galaxy_by_line_ally'"
  215. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_system_by_line_ally ." where config_name = 'system_by_line_ally'"
  216. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $pub_enable_register_view ."' where config_name = 'enable_register_view'"
  217. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_register_alliance) ."' where config_name = 'register_alliance'"
  218. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_register_forum) ."' where config_name = 'register_forum'"
  219. */
  220. function set_server_view()
  221. {
  222. global $db, $user_data;
  223. global $pub_enable_portee_missil, $pub_enable_members_view, $pub_enable_stat_view,
  224. $pub_galaxy_by_line_stat, $pub_system_by_line_stat, $pub_galaxy_by_line_ally, $pub_system_by_line_ally,
  225. $pub_nb_colonnes_ally, $pub_color_ally, $pub_enable_register_view, $pub_register_alliance,
  226. $pub_register_forum, $pub_open_user, $pub_open_admin;
  227. if (!check_var($pub_enable_members_view, "Num") || !check_var($pub_enable_stat_view,
  228. "Num") || !check_var($pub_galaxy_by_line_stat, "Num") || !check_var($pub_system_by_line_stat,
  229. "Num") || !check_var($pub_galaxy_by_line_ally, "Num") || !check_var($pub_system_by_line_ally,
  230. "Num")) {
  231. redirection("index.php?action=message&id_message=errordata&info");
  232. }
  233. if ($user_data["user_admin"] != 1 && $user_data["user_coadmin"] != 1) {
  234. redirection("planetindex.php?action=message&id_message=forbidden&info");
  235. }
  236. if (!isset($pub_galaxy_by_line_stat) || !isset($pub_system_by_line_stat) || !
  237. isset($pub_galaxy_by_line_ally) || !isset($pub_system_by_line_ally)) {
  238. redirection("index.php?action=message&id_message=setting_server_view_failed&info");
  239. }
  240. if (is_null($pub_enable_portee_missil))
  241. $pub_enable_portee_missil = 0;
  242. if (is_null($pub_enable_stat_view))
  243. $pub_enable_stat_view = 0;
  244. if (is_null($pub_enable_members_view))
  245. $pub_enable_members_view = 0;
  246. $break = false;
  247. if (!is_numeric($pub_galaxy_by_line_stat))
  248. $break = true;
  249. if (!is_numeric($pub_system_by_line_stat))
  250. $break = true;
  251. if ($pub_enable_stat_view != 0 && $pub_enable_stat_view != 1)
  252. $break = true;
  253. if ($pub_enable_members_view != 0 && $pub_enable_members_view != 1)
  254. $break = true;
  255. if (!is_numeric($pub_galaxy_by_line_ally))
  256. $break = true;
  257. if (!is_numeric($pub_system_by_line_ally))
  258. $break = true;
  259. if ($pub_nb_colonnes_ally == 0 || $pub_nb_colonnes_ally > 9 || !is_numeric($pub_nb_colonnes_ally))
  260. $break = true;
  261. if ($pub_enable_register_view != 0 && $pub_enable_register_view != 1)
  262. $break = true;
  263. if ($break) {
  264. redirection("index.php?action=message&id_message=setting_server_view_failed&info");
  265. }
  266. //
  267. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_enable_portee_missil .
  268. " where config_name = 'portee_missil'";
  269. $db->sql_query($request);
  270. //
  271. if ($pub_galaxy_by_line_stat < 1)
  272. $pub_galaxy_by_line_stat = 1;
  273. if ($pub_galaxy_by_line_stat > 100)
  274. $pub_galaxy_by_line_stat = 100;
  275. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_galaxy_by_line_stat .
  276. " where config_name = 'galaxy_by_line_stat'";
  277. $db->sql_query($request);
  278. //
  279. if ($pub_system_by_line_stat < 1)
  280. $pub_system_by_line_stat = 1;
  281. if ($pub_system_by_line_stat > 100)
  282. $pub_system_by_line_stat = 100;
  283. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_system_by_line_stat .
  284. " where config_name = 'system_by_line_stat'";
  285. $db->sql_query($request);
  286. //
  287. $request = "update " . TABLE_CONFIG . " set config_value = '" . $pub_open_user .
  288. "' where config_name = 'open_user'";
  289. $db->sql_query($request);
  290. //
  291. $request = "update " . TABLE_CONFIG . " set config_value = '" . $pub_open_admin .
  292. "' where config_name = 'open_admin'";
  293. $db->sql_query($request);
  294. //
  295. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_enable_stat_view .
  296. " where config_name = 'enable_stat_view'";
  297. $db->sql_query($request);
  298. //
  299. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_enable_members_view .
  300. " where config_name = 'enable_members_view'";
  301. $db->sql_query($request);
  302. //
  303. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  304. sql_escape_string($pub_nb_colonnes_ally) .
  305. "' where config_name = 'nb_colonnes_ally'";
  306. $db->sql_query($request);
  307. $array = $pub_color_ally; //die(var_dump($pub_color_ally));
  308. $color_ally = implode("_", $array);
  309. //
  310. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  311. sql_escape_string($color_ally) . "' where config_name = 'color_ally'";
  312. $db->sql_query($request);
  313. //
  314. if ($pub_galaxy_by_line_ally < 1)
  315. $pub_galaxy_by_line_ally = 1;
  316. if ($pub_galaxy_by_line_ally > 100)
  317. $pub_galaxy_by_line_ally = 100;
  318. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_galaxy_by_line_ally .
  319. " where config_name = 'galaxy_by_line_ally'";
  320. $db->sql_query($request);
  321. //
  322. if ($pub_system_by_line_ally < 1)
  323. $pub_system_by_line_ally = 1;
  324. if ($pub_system_by_line_ally > 100)
  325. $pub_system_by_line_ally = 100;
  326. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_system_by_line_ally .
  327. " where config_name = 'system_by_line_ally'";
  328. $db->sql_query($request);
  329. //
  330. $request = "update " . TABLE_CONFIG . " set config_value = '" . $pub_enable_register_view .
  331. "' where config_name = 'enable_register_view'";
  332. $db->sql_query($request);
  333. //
  334. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  335. sql_escape_string($pub_register_alliance) .
  336. "' where config_name = 'register_alliance'";
  337. $db->sql_query($request);
  338. //
  339. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  340. sql_escape_string($pub_register_forum) .
  341. "' where config_name = 'register_forum'";
  342. $db->sql_query($request);
  343. // mise a jour des caches avec les modifs
  344. generate_config_cache();
  345. log_("set_server_view");
  346. redirection("index.php?action=administration&subaction=affichage");
  347. }
  348. /**
  349. * Updates in the database all configurations displayed in the parameters administration Page.
  350. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_server_active ." where config_name = 'server_active'";
  351. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_debug_log ." where config_name = 'debug_log'";
  352. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_block_ratio ." where config_name = 'block_ratio'";
  353. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_log_phperror ." where config_name = 'log_phperror'";
  354. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_max_favorites ." where config_name = 'max_favorites'";
  355. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_max_favorites_spy ." where config_name = 'max_favorites_spy'";
  356. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_ratio_limit ." where config_name = 'ratio_limit'";
  357. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_max_spyreport ." where config_name = 'max_spyreport'";
  358. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_max_battlereport ." where config_name = 'max_battlereport'";
  359. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_session_time ." where config_name = 'session_time'";
  360. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_max_keeplog ." where config_name = 'max_keeplog'";
  361. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_default_skin) . "' where config_name = 'default_skin'";
  362. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_reason) . "' where config_name = 'reason'";
  363. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_ally_protection) ."' where config_name = 'ally_protection'";
  364. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_url_forum) . "' where config_name = 'url_forum'";
  365. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_max_keeprank ." where config_name = 'max_keeprank'";
  366. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_keeprank_criterion) ."' where config_name = 'keeprank_criterion'";
  367. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_max_keepspyreport ." where config_name = 'max_keepspyreport'";
  368. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_servername) . "' where config_name = 'servername'";
  369. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $db->sql_escape_string($pub_allied) . "' where config_name = 'allied'";
  370. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_disable_ip_check ." where config_name = 'disable_ip_check'";
  371. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_num_of_galaxies ." where config_name = 'num_of_galaxies'";
  372. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_num_of_systems ." where config_name = 'num_of_systems'";
  373. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $pub_ddr ."' where config_name = 'ddr'";
  374. * @todo Query : "update " . TABLE_CONFIG . " set config_value = '" . $pub_astro_strict ."' where config_name = 'astro_strict'";
  375. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_speed_uni ." where config_name = 'speed_uni'";
  376. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_mod_cache ." where config_name = 'mod_cache'";
  377. * @todo Query : "update " . TABLE_CONFIG . " set config_value = " . $pub_config_cache ." where config_name = 'config_cache'";
  378. */
  379. function set_serverconfig()
  380. {
  381. global $db, $user_data, $server_config;
  382. global $pub_max_battlereport, $pub_max_favorites, $pub_max_favorites_spy, $pub_max_spyreport,
  383. $pub_server_active, $pub_session_time, $pub_max_keeplog, $pub_default_skin, $pub_debug_log,
  384. $pub_reason, $pub_ally_protection, $pub_url_forum, $pub_max_keeprank, $pub_keeprank_criterion,
  385. $pub_max_keepspyreport, $pub_servername, $pub_allied, $pub_disable_ip_check, $pub_num_of_galaxies,
  386. $pub_num_of_systems, $pub_log_phperror, $pub_block_ratio, $pub_ratio_limit, $pub_speed_uni,
  387. $pub_ddr, $pub_astro_strict, $pub_config_cache, $pub_mod_cache;
  388. if (!isset($pub_num_of_galaxies))
  389. $pub_num_of_galaxies = intval($server_config['num_of_galaxies']);
  390. if (!isset($pub_num_of_systems))
  391. $pub_num_of_systems = intval($server_config['num_of_systems']);
  392. if (!check_var($pub_max_battlereport, "Num") || !check_var($pub_max_favorites,
  393. "Num") || !check_var($pub_max_favorites_spy, "Num") || !check_var($pub_ratio_limit,
  394. "Special", "#^[\w\s,\.\-]+$#") || !check_var($pub_max_spyreport, "Num") || !
  395. check_var($pub_server_active, "Num") || !check_var($pub_session_time, "Num") ||
  396. !check_var($pub_max_keeplog, "Num") || !check_var($pub_default_skin, "URL") || !
  397. check_var($pub_debug_log, "Num") || !check_var($pub_block_ratio, "Num") || !
  398. check_var(stripslashes($pub_reason), "Text") || !check_var($pub_ally_protection,
  399. "Special", "#^[\w\s,\.\-]+$#") || !check_var($pub_url_forum, "URL") || !
  400. check_var($pub_max_keeprank, "Num") || !check_var($pub_keeprank_criterion,
  401. "Char") || !check_var($pub_max_keepspyreport, "Num") || !check_var(stripslashes
  402. ($pub_servername), "Text") || !check_var($pub_allied, "Special", "#^[\w\s,\.\-]+$#") ||
  403. !check_var($pub_disable_ip_check, "Num") || !check_var($pub_num_of_galaxies,
  404. "Galaxies") || !check_var($pub_num_of_systems, "Galaxies") || !check_var($pub_config_cache,
  405. "Num") || !check_var($pub_mod_cache, "Num")) {
  406. redirection("index.php?action=message&id_message=errordata&info");
  407. }
  408. if ($user_data["user_admin"] != 1 && $user_data["user_coadmin"] != 1) {
  409. redirection("planetindex.php?action=message&id_message=forbidden&info");
  410. }
  411. if (!isset($pub_max_battlereport) || !isset($pub_max_favorites) || !isset($pub_max_favorites_spy) ||
  412. !isset($pub_ratio_limit) || !isset($pub_max_spyreport) || !isset($pub_session_time) ||
  413. !isset($pub_max_keeplog) || !isset($pub_default_skin) || !isset($pub_reason) ||
  414. !isset($pub_ally_protection) || !isset($pub_url_forum) || !isset($pub_max_keeprank) ||
  415. !isset($pub_keeprank_criterion) || !isset($pub_max_keepspyreport) || !isset($pub_servername) ||
  416. !isset($pub_allied) || !isset($pub_mod_cache) || !isset($pub_config_cache)) {
  417. redirection("index.php?action=message&id_message=setting_serverconfig_failed&info");
  418. }
  419. if (is_null($pub_server_active))
  420. $pub_server_active = 0;
  421. if (is_null($pub_disable_ip_check))
  422. $pub_disable_ip_check = 0;
  423. if (is_null($pub_log_phperror))
  424. $pub_log_phperror = 0;
  425. if (is_null($pub_debug_log))
  426. $pub_debug_log = 0;
  427. if (is_null($pub_block_ratio))
  428. $pub_block_ratio = 0;
  429. $break = false;
  430. if ($pub_server_active != 0 && $pub_server_active != 1)
  431. $break = true;
  432. if ($pub_debug_log != 0 && $pub_debug_log != 1)
  433. $break = true;
  434. if ($pub_block_ratio != 0 && $pub_block_ratio != 1)
  435. $break = true;
  436. if (!is_numeric($pub_max_favorites))
  437. $break = true;
  438. if (!is_numeric($pub_max_favorites_spy))
  439. $break = true;
  440. if (!is_numeric($pub_ratio_limit))
  441. $break = true;
  442. if (!is_numeric($pub_max_spyreport))
  443. $break = true;
  444. if (!is_numeric($pub_max_battlereport))
  445. $break = true;
  446. if (!is_numeric($pub_session_time))
  447. $break = true;
  448. if (!is_numeric($pub_max_keeplog))
  449. $break = true;
  450. if ($pub_disable_ip_check != 0 && $pub_disable_ip_check != 1)
  451. $break = true;
  452. if ($pub_log_phperror != 0 && $pub_log_phperror != 1)
  453. $break = true;
  454. if ($break) {
  455. redirection("index.php?action=message&id_message=setting_serverconfig_failed&info");
  456. }
  457. if (($pub_num_of_galaxies != intval($server_config['num_of_galaxies'])) || ($pub_num_of_systems !=
  458. intval($server_config['num_of_systems']))) {
  459. resize_db($pub_num_of_galaxies, $pub_num_of_systems);
  460. }
  461. //
  462. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_server_active .
  463. " where config_name = 'server_active'";
  464. $db->sql_query($request);
  465. //
  466. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_debug_log .
  467. " where config_name = 'debug_log'";
  468. $db->sql_query($request);
  469. //
  470. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_block_ratio .
  471. " where config_name = 'block_ratio'";
  472. $db->sql_query($request);
  473. //
  474. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_log_phperror .
  475. " where config_name = 'log_phperror'";
  476. $db->sql_query($request);
  477. //
  478. $pub_max_favorites = intval($pub_max_favorites);
  479. if ($pub_max_favorites < 0)
  480. $pub_max_favorites = 0;
  481. if ($pub_max_favorites > 99)
  482. $pub_max_favorites = 99;
  483. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_max_favorites .
  484. " where config_name = 'max_favorites'";
  485. $db->sql_query($request);
  486. //
  487. $pub_max_favorites_spy = intval($pub_max_favorites_spy);
  488. if ($pub_max_favorites_spy < 0)
  489. $pub_max_favorites_spy = 0;
  490. if ($pub_max_favorites_spy > 99)
  491. $pub_max_favorites_spy = 99;
  492. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_max_favorites_spy .
  493. " where config_name = 'max_favorites_spy'";
  494. $db->sql_query($request);
  495. //
  496. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_ratio_limit .
  497. " where config_name = 'ratio_limit'";
  498. $db->sql_query($request);
  499. //
  500. $pub_max_spyreport = intval($pub_max_spyreport);
  501. if ($pub_max_spyreport < 1)
  502. $pub_max_spyreport = 1;
  503. if ($pub_max_spyreport > 10)
  504. $pub_max_spyreport = 10;
  505. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_max_spyreport .
  506. " where config_name = 'max_spyreport'";
  507. $db->sql_query($request);
  508. //
  509. $pub_max_battlereport = intval($pub_max_battlereport);
  510. if ($pub_max_battlereport < 0)
  511. $pub_max_battlereport = 0;
  512. if ($pub_max_battlereport > 99)
  513. $pub_max_battlereport = 99;
  514. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_max_battlereport .
  515. " where config_name = 'max_battlereport'";
  516. $db->sql_query($request);
  517. //
  518. $pub_session_time = intval($pub_session_time);
  519. if ($pub_session_time < 5 && $pub_session_time != 0)
  520. $pub_session_time = 5;
  521. if ($pub_session_time > 180)
  522. $pub_session_time = 180;
  523. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_session_time .
  524. " where config_name = 'session_time'";
  525. $db->sql_query($request);
  526. //
  527. $pub_max_keeplog = intval($pub_max_keeplog);
  528. if ($pub_max_keeplog < 0)
  529. $pub_max_keeplog = 0;
  530. if ($pub_max_keeplog > 365)
  531. $pub_max_keeplog = 365;
  532. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_max_keeplog .
  533. " where config_name = 'max_keeplog'";
  534. $db->sql_query($request);
  535. //
  536. if (substr($pub_default_skin, strlen($pub_default_skin) - 1) != "/")
  537. $pub_default_skin .= "/";
  538. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  539. sql_escape_string($pub_default_skin) . "' where config_name = 'default_skin'";
  540. $db->sql_query($request);
  541. //
  542. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  543. sql_escape_string($pub_reason) . "' where config_name = 'reason'";
  544. $db->sql_query($request);
  545. //
  546. if (substr($pub_ally_protection, strlen($pub_ally_protection) - 1) == ",")
  547. $pub_ally_protection = substr($pub_ally_protection, 0, strlen($pub_ally_protection) -
  548. 1);
  549. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  550. sql_escape_string($pub_ally_protection) .
  551. "' where config_name = 'ally_protection'";
  552. $db->sql_query($request);
  553. //
  554. if ($pub_url_forum != "" && !preg_match("#^http://#", $pub_url_forum))
  555. $pub_url_forum = "http://" . $pub_url_forum;
  556. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  557. sql_escape_string($pub_url_forum) . "' where config_name = 'url_forum'";
  558. $db->sql_query($request);
  559. //
  560. $pub_max_keeprank = intval($pub_max_keeprank);
  561. if ($pub_max_keeprank < 1)
  562. $pub_max_keeprank = 1;
  563. if ($pub_max_keeprank > 50)
  564. $pub_max_keeprank = 50;
  565. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_max_keeprank .
  566. " where config_name = 'max_keeprank'";
  567. $db->sql_query($request);
  568. //
  569. if ($pub_keeprank_criterion != "quantity" && $pub_keeprank_criterion != "day")
  570. $pub_keeprank_criterion = "quantity";
  571. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  572. sql_escape_string($pub_keeprank_criterion) .
  573. "' where config_name = 'keeprank_criterion'";
  574. $db->sql_query($request);
  575. //
  576. $pub_max_keepspyreport = intval($pub_max_keepspyreport);
  577. if ($pub_max_keepspyreport < 1)
  578. $pub_max_keepspyreport = 1;
  579. if ($pub_max_keepspyreport > 90)
  580. $pub_max_keepspyreport = 90;
  581. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_max_keepspyreport .
  582. " where config_name = 'max_keepspyreport'";
  583. $db->sql_query($request);
  584. //
  585. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  586. sql_escape_string($pub_servername) . "' where config_name = 'servername'";
  587. $db->sql_query($request);
  588. //
  589. if (substr($pub_allied, strlen($pub_allied) - 1) == ",")
  590. $pub_allied = substr($pub_allied, 0, strlen($pub_allied) - 1);
  591. $request = "update " . TABLE_CONFIG . " set config_value = '" . $db->
  592. sql_escape_string($pub_allied) . "' where config_name = 'allied'";
  593. $db->sql_query($request);
  594. //
  595. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_disable_ip_check .
  596. " where config_name = 'disable_ip_check'";
  597. $db->sql_query($request);
  598. //
  599. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_num_of_galaxies .
  600. " where config_name = 'num_of_galaxies'";
  601. $db->sql_query($request);
  602. //
  603. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_num_of_systems .
  604. " where config_name = 'num_of_systems'";
  605. $db->sql_query($request);
  606. //
  607. if (!isset($pub_ddr) || !is_numeric($pub_ddr))
  608. $pub_ddr = 0;
  609. $request = "update " . TABLE_CONFIG . " set config_value = '" . $pub_ddr .
  610. "' where config_name = 'ddr'";
  611. $db->sql_query($request);
  612. //
  613. if (!isset($pub_astro_strict) || !is_numeric($pub_astro_strict))
  614. $pub_ddr = 0;
  615. $request = "update " . TABLE_CONFIG . " set config_value = '" . $pub_astro_strict .
  616. "' where config_name = 'astro_strict'";
  617. $db->sql_query($request);
  618. //
  619. if (!is_numeric($pub_speed_uni) || $pub_speed_uni < 1)
  620. $pub_speed_uni = 1;
  621. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_speed_uni .
  622. " where config_name = 'speed_uni'";
  623. $db->sql_query($request);
  624. //
  625. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_mod_cache .
  626. " where config_name = 'mod_cache'";
  627. $db->sql_query($request);
  628. //
  629. $request = "update " . TABLE_CONFIG . " set config_value = " . $pub_config_cache .
  630. " where config_name = 'config_cache'";
  631. $db->sql_query($request);
  632. // mise a jour des caches avec les mofids
  633. generate_config_cache();
  634. log_("set_serverconfig");
  635. redirection("index.php?action=administration&subaction=parameter");
  636. }
  637. /**
  638. * Returns the Status of the Database used size.
  639. * @return Array [Server], et [Total]
  640. * @todo : Query : "SHOW TABLE STATUS"
  641. */
  642. function db_size_info()
  643. {
  644. global $db;
  645. global $table_prefix;
  646. $dbSizeServer = 0;
  647. $dbSizeTotal = 0;
  648. $request = "SHOW TABLE STATUS";
  649. $result = $db->sql_query($request);
  650. while ($row = $db->sql_fetch_assoc($result)) {
  651. $dbSizeTotal += $row['Data_length'] + $row['Index_length'];
  652. if (preg_match("#^" . $table_prefix . ".*$#", $row['Name'])) {
  653. $dbSizeServer += $row['Data_length'] + $row['Index_length'];
  654. }
  655. }
  656. $bytes = array('Octets', 'Ko', 'Mo', 'Go', 'To');
  657. if ($dbSizeServer < 1024)
  658. $dbSizeServer = 1;
  659. for ($i = 0; $dbSizeServer > 1024; $i++)
  660. $dbSizeServer /= 1024;
  661. $dbSize_info["Server"] = round($dbSizeServer, 2) . " " . $bytes[$i];
  662. if ($dbSizeTotal < 1024)
  663. $dbSizeTotal = 1;
  664. for ($i = 0; $dbSizeTotal > 1024; $i++)
  665. $dbSizeTotal /= 1024;
  666. $dbSize_info["Total"] = round($dbSizeTotal, 2) . " " . $bytes[$i];
  667. return $dbSize_info;
  668. }
  669. /**
  670. * Function to Optimize all tables of the OGSpy Database
  671. * @param boolean $maintenance_action true if no url redirection is requested,false to redirect to another page
  672. * @todo : Query : "SHOW TABLES"
  673. */
  674. function db_optimize($maintenance_action = false)
  675. {
  676. global $db;
  677. $dbSize_before = db_size_info();
  678. $dbSize_before = $dbSize_before["Total"];
  679. $request = 'SHOW TABLES';
  680. $res = $db->sql_query($request);
  681. while (list($table) = $db->sql_fetch_row($res)) {
  682. $request = 'OPTIMIZE TABLE ' . $table;
  683. $db->sql_query($request);
  684. }
  685. // 09-07-2012 : Commenté car cette table n'est plus utilisée
  686. //$request = 'TRUNCATE ' . TABLE_UNIVERSE_TEMPORARY;
  687. //$db->sql_query($request);
  688. $dbSize_after = db_size_info();
  689. $dbSize_after = $dbSize_after["Total"];
  690. if (!$maintenance_action) {
  691. redirection("index.php?action=message&id_message=db_optimize&info=" . $dbSize_before .
  692. "¤" . $dbSize_after);
  693. }
  694. }
  695. /**
  696. * Adapt the database to fit on the number of galaxies and solar systems
  697. * @param int $new_num_of_galaxies Galaxy total
  698. * @param int $new_num_of_systems Solar Systems total
  699. * @return null
  700. * @todo : Query : sql_query("DELETE FROM " . TABLE_UNIVERSE . " WHERE galaxy > $new_num_of_galaxies");
  701. * @todo : Query : sql_query("UPDATE " . TABLE_USER . " SET user_galaxy=1 WHERE user_galaxy > $new_num_of_galaxies");
  702. * @todo : Query : sql_query("DELETE FROM " . TABLE_USER_FAVORITE . " WHERE galaxy > $new_num_of_galaxies");
  703. * @todo : Query : sql_query("DELETE FROM " . TABLE_UNIVERSE . " WHERE system > $new_num_of_systems");
  704. * @todo : Query : sql_query("UPDATE " . TABLE_USER . " SET user_system=1 WHERE user_system > $new_num_of_systems");
  705. * @todo : Query : sql_query("DELETE FROM " . TABLE_USER_FAVORITE . " WHERE system > $new_num_of_systems");
  706. * @todo : Query : "ALTER TABLE `" . TABLE_UNIVERSE . "` CHANGE `galaxy` `galaxy` ENUM("; -> Voir Fonction
  707. * @todo : Query : "ALTER TABLE `" . TABLE_USER ." CHANGE `user_galaxy` `user_galaxy` -> Voir fonction
  708. * @todo : Query : $request = "ALTER TABLE `" . TABLE_USER_FAVORITE ."` CHANGE `galaxy` `galaxy` ENUM(" -> Voir fonction
  709. * @todo : Query : "REPLACE INTO " . TABLE_CONFIG ." (config_name, config_value) VALUES ('num_of_galaxies','$new_num_of_galaxies')";
  710. * @todo : Query : $requests = "REPLACE INTO " . TABLE_CONFIG ." (config_name, config_value) VALUES ('num_of_systems','$new_num_of_systems')";
  711. */
  712. function resize_db($new_num_of_galaxies, $new_num_of_systems)
  713. {
  714. global $db, $db_host, $db_user, $db_password, $db_database, $table_prefix, $server_config;
  715. // si on reduit on doit supprimez toutes les entrées qui font reference au systemes ou galaxies que l'on va enlever
  716. if ($new_num_of_galaxies < intval($server_config['num_of_galaxies'])) {
  717. $db->sql_query("DELETE FROM " . TABLE_UNIVERSE . " WHERE galaxy > $new_num_of_galaxies");
  718. $db->sql_query("UPDATE " . TABLE_USER . " SET user_galaxy=1 WHERE user_galaxy > $new_num_of_galaxies");
  719. $db->sql_query("DELETE FROM " . TABLE_USER_FAVORITE . " WHERE galaxy > $new_num_of_galaxies");
  720. }
  721. if ($new_num_of_systems < intval($server_config['num_of_systems'])) {
  722. $db->sql_query("DELETE FROM " . TABLE_UNIVERSE . " WHERE system > $new_num_of_systems");
  723. $db->sql_query("UPDATE " . TABLE_USER . " SET user_system=1 WHERE user_system > $new_num_of_systems");
  724. $db->sql_query("DELETE FROM " . TABLE_USER_FAVORITE . " WHERE system > $new_num_of_systems");
  725. }
  726. $request = "ALTER TABLE `" . TABLE_UNIVERSE . "` CHANGE `galaxy` `galaxy` ENUM(";
  727. for ($i = 1; $i < $new_num_of_galaxies; $i++)
  728. $request .= "'$i' , ";
  729. $request .= "'$new_num_of_galaxies') NOT NULL DEFAULT '1'";
  730. $db->sql_query($request);
  731. $request = "ALTER TABLE `" . TABLE_USER .
  732. "` CHANGE `user_galaxy` `user_galaxy` ENUM(";
  733. for ($i = 1; $i < $new_num_of_galaxies; $i++)
  734. $request .= "'$i' , ";
  735. $request .= "'$new_num_of_galaxies') NOT NULL DEFAULT '1'";
  736. $db->sql_query($request);
  737. $request = "ALTER TABLE `" . TABLE_USER_FAVORITE .
  738. "` CHANGE `galaxy` `galaxy` ENUM(";
  739. for ($i = 1; $i < $new_num_of_galaxies; $i++)
  740. $request .= "'$i' , ";
  741. $request .= "'$new_num_of_galaxies') NOT NULL DEFAULT '1'";
  742. $db->sql_query($request);
  743. $server_config['num_of_galaxies'] = "$new_num_of_galaxies";
  744. $server_config['num_of_systems'] = "$new_num_of_systems";
  745. $requests = "REPLACE INTO " . TABLE_CONFIG .
  746. " (config_name, config_value) VALUES ('num_of_galaxies','$new_num_of_galaxies')";
  747. $db->sql_query($request);
  748. $requests = "REPLACE INTO " . TABLE_CONFIG .
  749. " (config_name, config_value) VALUES ('num_of_systems','$new_num_of_systems')";
  750. $db->sql_query($request);
  751. log_("set_db_size");
  752. }
  753. /**
  754. * File Log size on the Server
  755. * @return Array tableau [type] and [size]
  756. */
  757. function log_size_info()
  758. {
  759. $logSize = 0;
  760. $res = opendir(PATH_LOG);
  761. $directory = array();
  762. //Récupération de la liste des fichiers présents dans les répertoires répertoriés
  763. while ($file = readdir($res)) {
  764. if ($file != "." && $file != "..") {
  765. if (is_dir(PATH_LOG . $file)) {
  766. $directory[] = PATH_LOG . $file;
  767. }
  768. }
  769. }
  770. closedir($res);
  771. foreach ($directory as $v) {
  772. $res = opendir($v);
  773. $directory = array();
  774. //Récupération de la liste des fichiers présents dans les répertoires répertoriés
  775. while ($file = readdir($res)) {
  776. if ($file != "." && $file != "..") {
  777. $logSize += @filesize($v . "/" . $file);
  778. }
  779. }
  780. closedir($res);
  781. }
  782. $bytes = array('Octets', 'Ko', 'Mo', 'Go', 'To');
  783. if ($logSize < 1024)
  784. $logSize = 1;
  785. for ($i = 0; $logSize > 1024; $i++)
  786. $logSize /= 1024;
  787. $log_size_info['size'] = round($logSize, 2);
  788. $log_size_info['type'] = $bytes[$i];
  789. return $log_size_info;
  790. }
  791. /**
  792. * Checks the availability of a log File
  793. * @param int $date Requested Date
  794. * @return boolean true if the log file exists
  795. * @internal To be improved...
  796. */
  797. function log_check_exist($date)
  798. {
  799. if (!isset($date))
  800. redirection("index.php?action=message&id_message=errorfatal&info");
  801. require_once ('library/zip.lib.php');
  802. $typelog = array("sql", "log", "txt");
  803. $root = PATH_LOG;
  804. $path = opendir("$root");
  805. //Récupération de la liste des répertoires correspondant ŕ cette date
  806. while ($file = readdir($path)) {
  807. if ($file != "." && $file != "..") {
  808. if (is_dir($root . $file) && preg_match("/^" . $date . "/", $file))
  809. $directories[] = $file;
  810. }
  811. }
  812. closedir($path);
  813. if (!isset($directories)) {
  814. return false;
  815. }
  816. foreach ($directories as $d) {
  817. $path = opendir($root . $d);
  818. while ($file = readdir($path)) {
  819. if ($file != "." && $file != "..") {
  820. $extension = substr($file, (strrpos($file, ".") + 1));
  821. if (in_array($extension, $typelog)) {
  822. $files[] = $d . "/" . $file;
  823. }
  824. }
  825. }
  826. closedir($path);
  827. }
  828. if (!isset($files)) {
  829. return false;
  830. }
  831. return true;
  832. }
  833. /**
  834. * Sends a Compressed archive to the browser for a specific date
  835. * @global array $user_data
  836. */
  837. function log_extractor()
  838. {
  839. global $pub_date, $user_data;
  840. if ($user_data["user_admin"] != 1 && $user_data["user_coadmin"] != 1) {
  841. redirection("index.php?action=message&id_message=forbidden&info");
  842. }
  843. if (!isset($pub_date))
  844. redirection("index.php?action=message&id_message=errorfatal&info");
  845. require_once ('library/zip.lib.php');
  846. $typelog = array("sql", "log", "txt");
  847. $root = PATH_LOG;
  848. $path = opendir("$root");
  849. //Récupération de la liste des répertoires correspondant ŕ cette date
  850. while ($file = readdir($path)) {
  851. if ($file != "." && $file != "..") {
  852. if (is_dir($root . $file) && preg_match("/^" . $pub_date . "/", $file))
  853. $directories[] = $file;
  854. }
  855. }
  856. closedir($path);
  857. if (!isset($directories)) {
  858. redirection("index.php?action=message&id_message=log_missing&info");
  859. }
  860. foreach ($directories as $d) {
  861. $path = opendir($root . $d);
  862. while ($file = readdir($path)) {
  863. if ($file != "." && $file != "..") {
  864. $extension = substr($file, (strrpos($file, ".") + 1));
  865. if (in_array($extension, $typelog)) {
  866. $files[] = $d . "/" . $file;
  867. }
  868. }
  869. }
  870. closedir($path);
  871. }
  872. if (!isset($files)) {
  873. redirection("index.php?action=message&id_message=log_missing&info");
  874. }
  875. // création d'un objet 'zipfile'
  876. $zip = new zipfile();
  877. foreach ($files as $filename) {
  878. // contenu du fichier
  879. $fp = fopen($root . $filename, 'r');
  880. $content = @fread($fp, @filesize($root . $filename));
  881. fclose($fp);
  882. // ajout du fichier dans cet objet
  883. $zip->addfile($content, $filename);
  884. // production de l'archive Zip
  885. $archive = $zip->file();
  886. }
  887. // entętes HTTP
  888. header('Content-Type: application/x-zip');
  889. // force le téléchargement
  890. header('Content-Disposition: inline; filename=log_' . $pub_date . '.zip');
  891. // envoi du fichier au navigateur
  892. echo $archive;
  893. }
  894. /**
  895. * Deletes a specified Log File
  896. *
  897. */
  898. function log_remove()
  899. {
  900. global $pub_date, $user_data, $pub_directory;
  901. if ($user_data["user_admin"] != 1 && $user_data["user_coadmin"] != 1)
  902. redirection("index.php?action=message&id_message=forbidden&info");
  903. if ($pub_directory == true) {
  904. @unlink("journal/" . $pub_date . "/log_" . $pub_date . ".log");
  905. @unlink("journal/" . $pub_date . "/index.htm");
  906. if (rmdir("journal/" . $pub_date)) {
  907. redirection("index.php?action=message&id_message=log_remove&info");
  908. } else {
  909. redirection("index.php?action=message&id_message=log_missing&info");
  910. }
  911. } else {
  912. if (unlink("journal/" . $pub_date . "/log_" . $pub_date . ".log")) {
  913. redirection("index.php?action=message&id_message=log_remove&info");
  914. } else {
  915. redirection("index.php?action=message&id_message=log_missing&info");
  916. }
  917. }
  918. }
  919. /**
  920. * Log file cleaning according the the Server configuration
  921. */
  922. function log_purge()
  923. {
  924. global $server_config;
  925. $time = $server_config["max_keeplog"];
  926. $limit = time() - (60 * 60 * 24 * $time);
  927. $limit = intval(date("ymd", $limit));
  928. $root = PATH_LOG;
  929. $path = opendir("$root");
  930. while ($file = readdir($path)) {
  931. if ($file != "." && $file != "..") {
  932. if (is_dir($root . $file) && intval($file) < $limit && @ereg("[0-9]{6}", $file)) {
  933. $directories[] = $file;
  934. }
  935. }
  936. }
  937. closedir($path);
  938. if (!isset($directories)) {
  939. return;
  940. }
  941. $files = array();
  942. foreach ($directories as $d) {
  943. $path = opendir($root . $d);
  944. while ($file = readdir($path)) {
  945. if ($file != "." && $file != "..") {
  946. $extension = substr($file, (strrpos($file, ".") + 1));
  947. unlink($root . $d . "/" . $file);
  948. }
  949. }
  950. closedir($path);
  951. rmdir($root . $d);
  952. }
  953. }
  954. /**
  955. * Formats a number.
  956. * @param int $number The value to be converted
  957. * @param int $decimal Sets the number of decimal points.
  958. * @return string The number with the new formatting
  959. */
  960. function formate_number($number, $decimal = 0)
  961. {
  962. return number_format($number, $decimal, ",", " ");
  963. }
  964. /**
  965. * Server Maintenance (Cleaning of Galaxy, Spy reports and Logs)
  966. */
  967. function maintenance_action()
  968. {
  969. global $db, $server_config;
  970. $time = mktime(0, 0, 0);
  971. if (isset($server_config["last_maintenance_action"]) && $time > $server_config["last_maintenance_action"]) {
  972. galaxy_purge_ranking();
  973. log_purge();
  974. galaxy_purge_spy();
  975. $request = "update " . TABLE_CONFIG . " set config_value = '" . $time . "' where config_name = 'last_maintenance_action'";
  976. $db->sql_query($request);
  977. }
  978. }
  979. /**
  980. * Security Function : Variable Verification according the type(Pseudo, Password, string, number,...)
  981. * @param string $value Value of the data to check
  982. * @param string $type_check Type of the value (Pseudo_Groupname, Pseudo_ingame, Password, Text, CharNum, Char, Num, Galaxies, URL, Special)
  983. * @param string $mask Can be used to specify a Regex for the check when the type is set as Special
  984. * @param boolean $auth_null Workarround linked to the authentification
  985. * @return boolean true if the value is ok or empty and false if the checking has failed.
  986. */
  987. function check_var($value, $type_check, $mask = "", $auth_null = true)
  988. {
  989. if ($auth_null && $value == "") {
  990. return true;
  991. }
  992. switch ($type_check) {
  993. //Pseudo des membres
  994. case "Pseudo_Groupname":
  995. if (!preg_match("#^[\w\s\-]{3,15}$#", $value)) {
  996. log_("check_var", array("Pseudo_Groupname", $value));
  997. return false;
  998. }
  999. break;
  1000. //Pseudo ingame
  1001. case "Pseudo_ingame": // caracteres autorises entre 3 et 20 ( interdit au 05/11/11 = > &"'()# `/,;+ )
  1002. if (!preg_match("#^[\w@äŕçéčęëďîöôűü\^\{\}\[\]\.\*\-_~%§]{3,20}$#", $value)) {
  1003. log_("check_var", array("Text", $value));
  1004. return false;
  1005. }
  1006. break;
  1007. //Mot de passe des membres
  1008. case "Password":
  1009. if (!preg_match("#^[\w\s\-]{6,15}$#", $value)) {
  1010. return false;
  1011. }
  1012. break;
  1013. //Chaîne de caractčres avec espace
  1014. case "Text":
  1015. if (!preg_match("#^[\w'äŕçéčęëďîöôűü\s\.\*\-]+$#", $value)) {
  1016. log_("check_var", array("Text", $value));
  1017. return false;
  1018. }
  1019. break;
  1020. //Chaîne de caractčres et chiffre
  1021. case "CharNum":
  1022. if (!preg_match("#^[\w\.\*\-\#]+$#", $value)) {
  1023. log_("check_var", array("CharNum", $value));
  1024. return false;
  1025. }
  1026. break;
  1027. //Caractčres
  1028. case "Char":
  1029. if (!preg_match("#^[[:alpha:]_\.\*\-]+$#", $value)) {
  1030. log_("check_var", array("Char", $value));
  1031. return false;
  1032. }
  1033. break;
  1034. //Chiffres
  1035. case "Num":
  1036. if (!preg_match("#^[[:digit:]]+$#", $value)) {
  1037. log_("check_var", array("Num", $value));
  1038. return false;
  1039. }
  1040. break;
  1041. //Galaxies
  1042. case "Galaxies":
  1043. if ($value < 1 || $value > 999) {
  1044. log_("check_var", array("Galaxy or system", $value));
  1045. return false;
  1046. }
  1047. break;
  1048. //Adresse internet
  1049. case "URL":
  1050. if (!preg_match("#^(((?:http?)://)?(?(2)(www\.)?|(www\.){1})?[-a-z0-9~_]{2,}(\.[-a-z0-9~._]{2,})?[-a-z0-9~_\/&\?=.]{2,})$#i",
  1051. $value)) {
  1052. log_("check_var", array("URL", $value));
  1053. return false;
  1054. }
  1055. break;
  1056. //Plančte, Joueur et alliance
  1057. case "Galaxy":
  1058. // if (!preg_match("#^[\w\s\.\*\-]+$#", $value)) {
  1059. // log_("check_var", array("Galaxy", $value));
  1060. // return false;
  1061. // }
  1062. break;
  1063. //Rapport d'espionnage
  1064. case "Spyreport":
  1065. // if (!preg_match("#^[\w\s\[\]\:\-'%\.\*]+$#", $value)) {
  1066. // log_("check_var", array("Spyreport", $value));
  1067. // return false;
  1068. // }
  1069. break;
  1070. //Masque paramétrable
  1071. case "Special":
  1072. if (!preg_match($mask, $value)) {
  1073. log_("check_var", array("Special", $value));
  1074. return false;
  1075. }
  1076. break;
  1077. default:
  1078. return false;
  1079. }
  1080. return true;
  1081. }
  1082. /**
  1083. * Resets the User for imported datas.
  1084. * @param boolean $maintenance_action If true the function does not redirect the user to the raz_ration Page
  1085. */
  1086. function admin_raz_ratio($maintenance_action = false)
  1087. {
  1088. global $db, $user_data;
  1089. if ($user_data["user_admin"] != 1 && $user_data["user_coadmin"] != 1 && $user_data["management_user"] !=
  1090. 1) {
  1091. die("Acces interdit");
  1092. }
  1093. $request = "UPDATE " . TABLE_USER . " set search='0'";
  1094. $db->sql_query($request);
  1095. if (!$maintenance_action) {
  1096. redirection("index.php?action=message&id_message=raz_ratio&info");
  1097. }
  1098. }
  1099. /**
  1100. * Microtime Value formatted for benchmark functions
  1101. * @return int Current microtime
  1102. */
  1103. function benchmark()
  1104. {
  1105. $mtime = microtime();
  1106. $mtime = explode(" ", $mtime);
  1107. $mtime = $mtime[1] + $mtime[0];
  1108. return $mtime;
  1109. }
  1110. /**
  1111. * Security : HTTP GET Data verifications
  1112. * @param string $secvalue The value to be checked
  1113. * @return boolean true if the verification is ok
  1114. */
  1115. function check_getvalue($secvalue)
  1116. {
  1117. if (!is_array($secvalue)) {
  1118. if ((preg_match("/<[^>]*script*\"?[^>]*>/i", $secvalue)) || (preg_match("/<[^>]*object*\"?[^>]*>/i",
  1119. $secvalue)) || (preg_match("/<[^>]*iframe*\"?[^>]*>/i", $secvalue)) || (preg_match
  1120. ("/<[^>]*applet*\"?[^>]*>/i", $secvalue)) || (preg_match("/<[^>]*meta*\"?[^>]*>/i",
  1121. $secvalue)) || (preg_match("/<[^>]*style*\"?[^>]*>/i", $secvalue)) || (preg_match
  1122. ("/<[^>]*form*\"?[^>]*>/i", $secvalue)) || (preg_match("/<[^>]*img*\"?[^>]*>/i",
  1123. $secvalue)) || (preg_match("/\([^>]*\"?[^)]*\)/i", $secvalue)) || (preg_match("/\"/i",
  1124. $secvalue))) {
  1125. return false;
  1126. }
  1127. } else {
  1128. foreach ($secvalue as $subsecvalue) {
  1129. if (!check_getvalue($subsecvalue))
  1130. return false;
  1131. }
  1132. }
  1133. return true;
  1134. }
  1135. /**
  1136. * Security : HTTP POST Data verifications
  1137. * @param string $secvalue The value to be checked
  1138. * @return boolean true if the verification is ok
  1139. */
  1140. function check_postvalue($secvalue)
  1141. {
  1142. if (!is_array($secvalue)) {
  1143. if ((preg_match("/<[^>]*script*\"?[^>]*>/", $secvalue)) || (preg_match("/<[^>]*style*\"?[^>]*>/",
  1144. $secvalue))) {
  1145. return false;
  1146. }
  1147. } else {
  1148. foreach ($secvalue as $subsecvalue) {
  1149. if (!check_postvalue($subsecvalue))
  1150. return false;
  1151. }
  1152. }
  1153. return true;
  1154. }
  1155. //\\ fonctions utilisable pour les mods //\\
  1156. /**
  1157. * Funtion to install a…

Large files files are truncated, but you can click here to view the full file