PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions_admin.php

http://github.com/MightyGorgon/icy_phoenix
PHP | 370 lines | 277 code | 43 blank | 50 comment | 69 complexity | 26cec6d8bf98379d412270a1091e855c MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Icy Phoenix
  5. * @version $Id$
  6. * @copyright (c) 2008 Icy Phoenix
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. *
  12. * @Icy Phoenix is based on phpBB
  13. * @copyright (c) 2008 phpBB Group
  14. *
  15. */
  16. if (!defined('IN_ICYPHOENIX'))
  17. {
  18. die('Hacking attempt');
  19. }
  20. /**
  21. * Function needed to fix config values before passing them to DB
  22. */
  23. function fix_config_values($config_name, $config_value)
  24. {
  25. global $config;
  26. if (in_array($config_name, array('header_table_text')))
  27. {
  28. $config_value = htmlspecialchars_decode($config_value, ENT_COMPAT);
  29. }
  30. if ($config_name == 'cookie_name')
  31. {
  32. $config_value = str_replace('.', '_', $config_value);
  33. }
  34. // Attempt to prevent a common mistake with this value,
  35. // http:// is the protocol and not part of the server name
  36. if ($config_name == 'server_name')
  37. {
  38. $config_value = str_replace('http://', '', $config_value);
  39. }
  40. if ($config_name == 'report_forum')
  41. {
  42. $config_value = str_replace('f', '', $config_value);
  43. }
  44. if ($config_name == 'bin_forum')
  45. {
  46. $config_value = str_replace('f', '', $config_value);
  47. }
  48. // Attempt to prevent a mistake with this value.
  49. if ($config_name == 'avatar_path')
  50. {
  51. $config_value = trim($config_value);
  52. if (strstr($config_value, "\0") || !is_dir(IP_ROOT_PATH . $config_value) || !is_writable(IP_ROOT_PATH . $config_value))
  53. {
  54. $config_value = $config['avatar_path'];
  55. }
  56. }
  57. // AJAX CHAT - BEGIN
  58. if ($config_name == 'ajax_chat_msgs_refresh')
  59. {
  60. // Just make sure ajax_chat_msgs_refresh is not below 1 second
  61. $config_value = ((int) $config_value < 1) ? 1 : $config_value;
  62. }
  63. if ($config_name == 'ajax_chat_session_refresh')
  64. {
  65. // Just make sure ajax_chat_session_refresh is not below 5 seconds
  66. $config_value = ((int) $config_value < 5) ? 5 : $config_value;
  67. }
  68. // AJAX CHAT - END
  69. return $config_value;
  70. }
  71. // Duplicate forum auth
  72. function duplicate_auth($source_id, $target_id)
  73. {
  74. global $db, $forum_auth_fields;
  75. $sql = "SELECT * FROM " . FORUMS_TABLE . "
  76. WHERE forum_id = " . (int) $source_id;
  77. $db->sql_return_on_error(true);
  78. $result = $db->sql_query($sql);
  79. $db->sql_return_on_error(false);
  80. if (!$result)
  81. {
  82. return false;
  83. }
  84. $row = $db->sql_fetchrow($result);
  85. $auth_sql = '';
  86. for ($i = 0; $i < sizeof($forum_auth_fields); $i++)
  87. {
  88. if ($i < (sizeof($forum_auth_fields) - 1))
  89. {
  90. $comma_append = ', ';
  91. }
  92. else
  93. {
  94. $comma_append = '';
  95. }
  96. $auth_sql .= $forum_auth_fields[$i] . ' = \'' . $row[$forum_auth_fields[$i]] . '\'' . $comma_append;
  97. }
  98. $sql = "UPDATE " . FORUMS_TABLE . "
  99. SET ". $auth_sql . "
  100. WHERE forum_id = " . (int) $target_id;
  101. $db->sql_return_on_error(true);
  102. $result = $db->sql_query($sql);
  103. $db->sql_return_on_error(false);
  104. if (!$result)
  105. {
  106. return false;
  107. }
  108. return true;
  109. }
  110. /**
  111. * Check IP addresses
  112. */
  113. function match_ips($ip_list_match)
  114. {
  115. $ip_list = array();
  116. $ip_list_temp = explode(',', $ip_list_match);
  117. for($i = 0; $i < sizeof($ip_list_temp); $i++)
  118. {
  119. if (preg_match('/^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[ ]*\-[ ]*([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/', trim($ip_list_temp[$i]), $ip_range_explode))
  120. {
  121. // Don't ask about all this, just don't ask ... !
  122. $ip_1_counter = $ip_range_explode[1];
  123. $ip_1_end = $ip_range_explode[5];
  124. while ($ip_1_counter <= $ip_1_end)
  125. {
  126. $ip_2_counter = ($ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[2] : 0;
  127. $ip_2_end = ($ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[6];
  128. if (($ip_2_counter == 0) && ($ip_2_end == 254))
  129. {
  130. $ip_2_counter = 255;
  131. $ip_2_fragment = 255;
  132. $ip_list[] = "$ip_1_counter.255.255.255";
  133. }
  134. while ($ip_2_counter <= $ip_2_end)
  135. {
  136. $ip_3_counter = (($ip_2_counter == $ip_range_explode[2]) && ($ip_1_counter == $ip_range_explode[1])) ? $ip_range_explode[3] : 0;
  137. $ip_3_end = (($ip_2_counter < $ip_2_end) || ($ip_1_counter < $ip_1_end)) ? 254 : $ip_range_explode[7];
  138. if (($ip_3_counter == 0) && ($ip_3_end == 254))
  139. {
  140. $ip_3_counter = 255;
  141. $ip_3_fragment = 255;
  142. $ip_list[] = "$ip_1_counter.$ip_2_counter.255.255";
  143. }
  144. while ($ip_3_counter <= $ip_3_end)
  145. {
  146. $ip_4_counter = (($ip_3_counter == $ip_range_explode[3]) && ($ip_2_counter == $ip_range_explode[2]) && ($ip_1_counter == $ip_range_explode[1])) ? $ip_range_explode[4] : 0;
  147. $ip_4_end = (($ip_3_counter < $ip_3_end) || ($ip_2_counter < $ip_2_end)) ? 254 : $ip_range_explode[8];
  148. if (($ip_4_counter == 0) && ($ip_4_end == 254))
  149. {
  150. $ip_4_counter = 255;
  151. $ip_4_fragment = 255;
  152. $ip_list[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.255";
  153. }
  154. while ($ip_4_counter <= $ip_4_end)
  155. {
  156. $ip_list[] = "$ip_1_counter.$ip_2_counter.$ip_3_counter.$ip_4_counter";
  157. $ip_4_counter++;
  158. }
  159. $ip_3_counter++;
  160. }
  161. $ip_2_counter++;
  162. }
  163. $ip_1_counter++;
  164. }
  165. }
  166. elseif (preg_match('/^([\w\-_]\.?){2,}$/is', trim($ip_list_temp[$i])))
  167. {
  168. $ip = gethostbynamel(trim($ip_list_temp[$i]));
  169. for($j = 0; $j < sizeof($ip); $j++)
  170. {
  171. if ( !empty($ip[$j]) )
  172. {
  173. $ip_list[] = $ip[$j];
  174. }
  175. }
  176. }
  177. elseif (preg_match('/^([0-9]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})$/', trim($ip_list_temp[$i])))
  178. {
  179. // Mighty Gorgon: we don't use this replacement any more...
  180. //$ip_list[] = str_replace('*', '255', trim($ip_list_temp[$i]));
  181. $ip_list[] = trim($ip_list_temp[$i]);
  182. }
  183. }
  184. return $ip_list;
  185. }
  186. /**
  187. * Check MEM Limit
  188. */
  189. function check_mem_limit()
  190. {
  191. $mem_limit = @ini_get('memory_limit');
  192. if (!empty($mem_limit))
  193. {
  194. $unit = strtolower(substr($mem_limit, -1, 1));
  195. $mem_limit = (int) $mem_limit;
  196. if ($unit == 'k')
  197. {
  198. $mem_limit = floor($mem_limit / 1024);
  199. }
  200. elseif ($unit == 'g')
  201. {
  202. $mem_limit *= 1024;
  203. }
  204. elseif (is_numeric($unit))
  205. {
  206. $mem_limit = floor((int) ($mem_limit . $unit) / 1048576);
  207. }
  208. $mem_limit = max(128, $mem_limit) . 'M';
  209. }
  210. else
  211. {
  212. $mem_limit = '128M';
  213. }
  214. return $mem_limit;
  215. }
  216. /**
  217. * Retrieve contents from remotely stored file
  218. */
  219. function get_remote_file($host, $directory, $filename, &$errstr, &$errno, $port = 80, $timeout = 10)
  220. {
  221. global $lang;
  222. if ($fsock = @fsockopen($host, $port, $errno, $errstr, $timeout))
  223. {
  224. @fputs($fsock, "GET $directory/$filename HTTP/1.1\r\n");
  225. @fputs($fsock, "HOST: $host\r\n");
  226. @fputs($fsock, "Connection: close\r\n\r\n");
  227. $file_info = '';
  228. $get_info = false;
  229. while (!@feof($fsock))
  230. {
  231. if ($get_info)
  232. {
  233. $file_info .= @fread($fsock, 1024);
  234. }
  235. else
  236. {
  237. $line = @fgets($fsock, 1024);
  238. if ($line == "\r\n")
  239. {
  240. $get_info = true;
  241. }
  242. elseif (stripos($line, '404 not found') !== false)
  243. {
  244. $errstr = $lang['FILE_NOT_FOUND'] . ': ' . $filename;
  245. return false;
  246. }
  247. }
  248. }
  249. @fclose($fsock);
  250. }
  251. else
  252. {
  253. if ($errstr)
  254. {
  255. $errstr = utf8_convert_message($errstr);
  256. return false;
  257. }
  258. else
  259. {
  260. $errstr = $lang['FSOCK_DISABLED'];
  261. return false;
  262. }
  263. }
  264. return $file_info;
  265. }
  266. /**
  267. * Obtains the latest version information
  268. *
  269. * @param bool $force_update Ignores cached data. Defaults to false.
  270. * @param bool $warn_fail Trigger a warning if obtaining the latest version information fails. Defaults to false.
  271. * @param int $ttl Cache version information for $ttl seconds. Defaults to 86400 (24 hours).
  272. *
  273. * @return string | false Version info on success, false on failure.
  274. */
  275. function obtain_latest_version_info($force_update = false, $warn_fail = false, $ttl = 86400)
  276. {
  277. global $cache;
  278. $info = $cache->get('versioncheck');
  279. if (($info === false) || $force_update)
  280. {
  281. $errstr = '';
  282. $errno = 0;
  283. $info = get_remote_file('www.icyphoenix.com', '/version', 'ip2x.txt', $errstr, $errno);
  284. if ($info === false)
  285. {
  286. $cache->destroy('versioncheck');
  287. if ($warn_fail)
  288. {
  289. trigger_error($errstr, E_USER_WARNING);
  290. }
  291. return false;
  292. }
  293. $cache->put('versioncheck', $info, $ttl);
  294. }
  295. return $info;
  296. }
  297. /**
  298. * Return language string value for storage
  299. */
  300. function prepare_lang_entry($text, $store = true)
  301. {
  302. $text = (STRIP) ? stripslashes($text) : $text;
  303. // Adjust for storage...
  304. if ($store)
  305. {
  306. $text = str_replace("'", "\\'", str_replace('\\', '\\\\', $text));
  307. }
  308. return $text;
  309. }
  310. /**
  311. * Check if dir exists and eventually creates it
  312. */
  313. function ip_mkdir($path)
  314. {
  315. $result = false;
  316. $result = @is_dir($path);
  317. if (empty($result))
  318. {
  319. $result = @mkdir($path, 0755, true);
  320. //@chmod($path, 0777);
  321. }
  322. return $result;
  323. }
  324. ?>