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

/forum/download/file.php

https://github.com/GreyTeardrop/socionicasys-forum
PHP | 737 lines | 547 code | 98 blank | 92 comment | 143 complexity | e01f42760aeb5e313a2783303650f34d MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id$
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. define('IN_PHPBB', true);
  14. $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../';
  15. $phpEx = substr(strrchr(__FILE__, '.'), 1);
  16. // Thank you sun.
  17. if (isset($_SERVER['CONTENT_TYPE']))
  18. {
  19. if ($_SERVER['CONTENT_TYPE'] === 'application/x-java-archive')
  20. {
  21. exit;
  22. }
  23. }
  24. else if (isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Java') !== false)
  25. {
  26. exit;
  27. }
  28. if (isset($_GET['avatar']))
  29. {
  30. require($phpbb_root_path . 'includes/startup.' . $phpEx);
  31. require($phpbb_root_path . 'config.' . $phpEx);
  32. if (!defined('PHPBB_INSTALLED') || empty($dbms) || empty($acm_type))
  33. {
  34. exit;
  35. }
  36. require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
  37. require($phpbb_root_path . 'includes/cache.' . $phpEx);
  38. require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
  39. require($phpbb_root_path . 'includes/constants.' . $phpEx);
  40. require($phpbb_root_path . 'includes/automatic_dst.' . $phpEx);
  41. require($phpbb_root_path . 'includes/functions.' . $phpEx);
  42. $db = new $sql_db();
  43. $cache = new cache();
  44. // Connect to DB
  45. if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
  46. {
  47. exit;
  48. }
  49. unset($dbpasswd);
  50. // worst-case default
  51. $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
  52. $config = $cache->obtain_config();
  53. $filename = request_var('avatar', '');
  54. $avatar_group = false;
  55. $exit = false;
  56. if (isset($filename[0]) && $filename[0] === 'g')
  57. {
  58. $avatar_group = true;
  59. $filename = substr($filename, 1);
  60. }
  61. // '==' is not a bug - . as the first char is as bad as no dot at all
  62. if (strpos($filename, '.') == false)
  63. {
  64. send_status_line(403, 'Forbidden');
  65. $exit = true;
  66. }
  67. if (!$exit)
  68. {
  69. $ext = substr(strrchr($filename, '.'), 1);
  70. $stamp = (int) substr(stristr($filename, '_'), 1);
  71. $filename = (int) $filename;
  72. $exit = set_modified_headers($stamp, $browser);
  73. }
  74. if (!$exit && !in_array($ext, array('png', 'gif', 'jpg', 'jpeg')))
  75. {
  76. // no way such an avatar could exist. They are not following the rules, stop the show.
  77. send_status_line(403, 'Forbidden');
  78. $exit = true;
  79. }
  80. if (!$exit)
  81. {
  82. if (!$filename)
  83. {
  84. // no way such an avatar could exist. They are not following the rules, stop the show.
  85. send_status_line(403, 'Forbidden');
  86. }
  87. else
  88. {
  89. send_avatar_to_browser(($avatar_group ? 'g' : '') . $filename . '.' . $ext, $browser);
  90. }
  91. }
  92. file_gc();
  93. }
  94. // implicit else: we are not in avatar mode
  95. include($phpbb_root_path . 'common.' . $phpEx);
  96. $download_id = request_var('id', 0);
  97. $mode = request_var('mode', '');
  98. $thumbnail = request_var('t', false);
  99. // Start session management, do not update session page.
  100. $user->session_begin(false);
  101. $auth->acl($user->data);
  102. $user->setup('viewtopic');
  103. if (!$download_id)
  104. {
  105. send_status_line(404, 'Not Found');
  106. trigger_error('NO_ATTACHMENT_SELECTED');
  107. }
  108. if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
  109. {
  110. send_status_line(404, 'Not Found');
  111. trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
  112. }
  113. $sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id, filetime
  114. FROM ' . ATTACHMENTS_TABLE . "
  115. WHERE attach_id = $download_id";
  116. $result = $db->sql_query_limit($sql, 1);
  117. $attachment = $db->sql_fetchrow($result);
  118. $db->sql_freeresult($result);
  119. if (!$attachment)
  120. {
  121. send_status_line(404, 'Not Found');
  122. trigger_error('ERROR_NO_ATTACHMENT');
  123. }
  124. if ((!$attachment['in_message'] && !$config['allow_attachments']) || ($attachment['in_message'] && !$config['allow_pm_attach']))
  125. {
  126. send_status_line(404, 'Not Found');
  127. trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
  128. }
  129. $row = array();
  130. if ($attachment['is_orphan'])
  131. {
  132. // We allow admins having attachment permissions to see orphan attachments...
  133. $own_attachment = ($auth->acl_get('a_attach') || $attachment['poster_id'] == $user->data['user_id']) ? true : false;
  134. if (!$own_attachment || ($attachment['in_message'] && !$auth->acl_get('u_pm_download')) || (!$attachment['in_message'] && !$auth->acl_get('u_download')))
  135. {
  136. send_status_line(404, 'Not Found');
  137. trigger_error('ERROR_NO_ATTACHMENT');
  138. }
  139. // Obtain all extensions...
  140. $extensions = $cache->obtain_attach_extensions(true);
  141. }
  142. else
  143. {
  144. if (!$attachment['in_message'])
  145. {
  146. //
  147. $sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
  148. FROM ' . POSTS_TABLE . ' p, ' . FORUMS_TABLE . ' f
  149. WHERE p.post_id = ' . $attachment['post_msg_id'] . '
  150. AND p.forum_id = f.forum_id';
  151. $result = $db->sql_query_limit($sql, 1);
  152. $row = $db->sql_fetchrow($result);
  153. $db->sql_freeresult($result);
  154. // Global announcement?
  155. $f_download = (!$row) ? $auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']);
  156. if ($auth->acl_get('u_download') && $f_download)
  157. {
  158. if ($row && $row['forum_password'])
  159. {
  160. // Do something else ... ?
  161. login_forum_box($row);
  162. }
  163. }
  164. else
  165. {
  166. send_status_line(403, 'Forbidden');
  167. trigger_error('SORRY_AUTH_VIEW_ATTACH');
  168. }
  169. }
  170. else
  171. {
  172. $row['forum_id'] = false;
  173. if (!$auth->acl_get('u_pm_download'))
  174. {
  175. send_status_line(403, 'Forbidden');
  176. trigger_error('SORRY_AUTH_VIEW_ATTACH');
  177. }
  178. // Check if the attachment is within the users scope...
  179. $sql = 'SELECT user_id, author_id
  180. FROM ' . PRIVMSGS_TO_TABLE . '
  181. WHERE msg_id = ' . $attachment['post_msg_id'];
  182. $result = $db->sql_query($sql);
  183. $allowed = false;
  184. while ($user_row = $db->sql_fetchrow($result))
  185. {
  186. if ($user->data['user_id'] == $user_row['user_id'] || $user->data['user_id'] == $user_row['author_id'])
  187. {
  188. $allowed = true;
  189. break;
  190. }
  191. }
  192. $db->sql_freeresult($result);
  193. if (!$allowed)
  194. {
  195. send_status_line(403, 'Forbidden');
  196. trigger_error('ERROR_NO_ATTACHMENT');
  197. }
  198. }
  199. // disallowed?
  200. $extensions = array();
  201. if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
  202. {
  203. send_status_line(404, 'Forbidden');
  204. trigger_error(sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
  205. }
  206. }
  207. if (!download_allowed())
  208. {
  209. send_status_line(403, 'Forbidden');
  210. trigger_error($user->lang['LINKAGE_FORBIDDEN']);
  211. }
  212. $download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
  213. // www.phpBB-SEO.com SEO TOOLKIT BEGIN -> Zero dupe
  214. // Added , attach_comment
  215. // Fetching filename here to prevent sniffing of filename
  216. $sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype, filetime, attach_comment
  217. FROM ' . ATTACHMENTS_TABLE . "
  218. WHERE attach_id = $download_id";
  219. // www.phpBB-SEO.com SEO TOOLKIT END -> Zero dupe
  220. $result = $db->sql_query_limit($sql, 1);
  221. $attachment = $db->sql_fetchrow($result);
  222. $db->sql_freeresult($result);
  223. if (!$attachment)
  224. {
  225. send_status_line(404, 'Not Found');
  226. trigger_error('ERROR_NO_ATTACHMENT');
  227. }
  228. $attachment['physical_filename'] = utf8_basename($attachment['physical_filename']);
  229. $display_cat = $extensions[$attachment['extension']]['display_cat'];
  230. if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
  231. {
  232. $display_cat = ATTACHMENT_CATEGORY_NONE;
  233. }
  234. if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
  235. {
  236. $display_cat = ATTACHMENT_CATEGORY_NONE;
  237. }
  238. // www.phpBB-SEO.com SEO TOOLKIT BEGIN -> Zero dupe
  239. if (!empty($phpbb_seo->seo_opt['rewrite_files'])) {
  240. if (empty($phpbb_seo->seo_url['file'][$download_id])) {
  241. $comment = bbcode_nl2br(censor_text($attachment['attach_comment']));
  242. $comment_clean = preg_replace('`<[^>]*>`Ui', ' ', $comment);
  243. $_display_cat = ($thumbnail && $display_cat != ATTACHMENT_CATEGORY_NONE) ? ATTACHMENT_CATEGORY_THUMB : $display_cat;
  244. if (($_pos = utf8_strpos($comment, '<br')) !== false) {
  245. $comment_url = strip_tags(utf8_substr($comment, 0, $_pos));
  246. } else {
  247. $comment_url = $comment_clean;
  248. }
  249. $comment_url = utf8_strlen($comment_url) > 60 ? utf8_substr($comment_url, 0, 60) : $comment_url;
  250. $phpbb_seo->seo_url['file'][$download_id] = $phpbb_seo->format_url($comment_url, $phpbb_seo->seo_static['file'][$display_cat]);
  251. }
  252. $phpbb_seo->seo_opt['zero_dupe']['redir_def'] = array(
  253. 'id' => array('val' => $download_id, 'keep' => true),
  254. 'mode' => array('val' => $mode, 'keep' => (boolean) ($mode == 'view')),
  255. 't' => array('val' => $thumbnail, 'keep' => $thumbnail),
  256. );
  257. $phpbb_seo->seo_chk_dupe('', '', $phpbb_root_path . 'download/');
  258. }
  259. // www.phpBB-SEO.com SEO TOOLKIT END -> Zero dupe
  260. if ($thumbnail)
  261. {
  262. $attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
  263. }
  264. else if (($display_cat == ATTACHMENT_CATEGORY_NONE/* || $display_cat == ATTACHMENT_CATEGORY_IMAGE*/) && !$attachment['is_orphan'])
  265. {
  266. // Update download count
  267. $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
  268. SET download_count = download_count + 1
  269. WHERE attach_id = ' . $attachment['attach_id'];
  270. $db->sql_query($sql);
  271. }
  272. if ($display_cat == ATTACHMENT_CATEGORY_IMAGE && $mode === 'view' && (strpos($attachment['mimetype'], 'image') === 0) && ((strpos(strtolower($user->browser), 'msie') !== false) && (strpos(strtolower($user->browser), 'msie 8.0') === false)))
  273. {
  274. wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
  275. file_gc();
  276. }
  277. else
  278. {
  279. // Determine the 'presenting'-method
  280. if ($download_mode == PHYSICAL_LINK)
  281. {
  282. // This presenting method should no longer be used
  283. if (!@is_dir($phpbb_root_path . $config['upload_path']))
  284. {
  285. send_status_line(500, 'Internal Server Error');
  286. trigger_error($user->lang['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
  287. }
  288. redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
  289. file_gc();
  290. }
  291. else
  292. {
  293. send_file_to_browser($attachment, $config['upload_path'], $display_cat);
  294. file_gc();
  295. }
  296. }
  297. /**
  298. * A simplified function to deliver avatars
  299. * The argument needs to be checked before calling this function.
  300. */
  301. function send_avatar_to_browser($file, $browser)
  302. {
  303. global $config, $phpbb_root_path;
  304. $prefix = $config['avatar_salt'] . '_';
  305. $image_dir = $config['avatar_path'];
  306. // Adjust image_dir path (no trailing slash)
  307. if (substr($image_dir, -1, 1) == '/' || substr($image_dir, -1, 1) == '\\')
  308. {
  309. $image_dir = substr($image_dir, 0, -1) . '/';
  310. }
  311. $image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir);
  312. if ($image_dir && ($image_dir[0] == '/' || $image_dir[0] == '\\'))
  313. {
  314. $image_dir = '';
  315. }
  316. $file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file;
  317. if ((@file_exists($file_path) && @is_readable($file_path)) && !headers_sent())
  318. {
  319. header('Pragma: public');
  320. $image_data = @getimagesize($file_path);
  321. header('Content-Type: ' . image_type_to_mime_type($image_data[2]));
  322. if (strpos(strtolower($browser), 'msie') !== false && strpos(strtolower($browser), 'msie 8.0') === false)
  323. {
  324. header('Content-Disposition: attachment; ' . header_filename($file));
  325. if (strpos(strtolower($browser), 'msie 6.0') !== false)
  326. {
  327. header('Expires: -1');
  328. }
  329. else
  330. {
  331. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
  332. }
  333. }
  334. else
  335. {
  336. header('Content-Disposition: inline; ' . header_filename($file));
  337. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
  338. }
  339. $size = @filesize($file_path);
  340. if ($size)
  341. {
  342. header("Content-Length: $size");
  343. }
  344. if (@readfile($file_path) == false)
  345. {
  346. $fp = @fopen($file_path, 'rb');
  347. if ($fp !== false)
  348. {
  349. while (!feof($fp))
  350. {
  351. echo fread($fp, 8192);
  352. }
  353. fclose($fp);
  354. }
  355. }
  356. flush();
  357. }
  358. else
  359. {
  360. send_status_line(404, 'Not Found');
  361. }
  362. }
  363. /**
  364. * Wraps an url into a simple html page. Used to display attachments in IE.
  365. * this is a workaround for now; might be moved to template system later
  366. * direct any complaints to 1 Microsoft Way, Redmond
  367. */
  368. function wrap_img_in_html($src, $title)
  369. {
  370. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">';
  371. echo '<html>';
  372. echo '<head>';
  373. echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
  374. echo '<title>' . $title . '</title>';
  375. echo '</head>';
  376. echo '<body>';
  377. echo '<div>';
  378. echo '<img src="' . $src . '" alt="' . $title . '" />';
  379. echo '</div>';
  380. echo '</body>';
  381. echo '</html>';
  382. }
  383. /**
  384. * Send file to browser
  385. */
  386. function send_file_to_browser($attachment, $upload_dir, $category)
  387. {
  388. global $user, $db, $config, $phpbb_root_path;
  389. $filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
  390. if (!@file_exists($filename))
  391. {
  392. send_status_line(404, 'Not Found');
  393. trigger_error($user->lang['ERROR_NO_ATTACHMENT'] . '<br /><br />' . sprintf($user->lang['FILE_NOT_FOUND_404'], $filename));
  394. }
  395. // Correct the mime type - we force application/octetstream for all files, except images
  396. // Please do not change this, it is a security precaution
  397. if ($category != ATTACHMENT_CATEGORY_IMAGE || strpos($attachment['mimetype'], 'image') !== 0)
  398. {
  399. $attachment['mimetype'] = (strpos(strtolower($user->browser), 'msie') !== false || strpos(strtolower($user->browser), 'opera') !== false) ? 'application/octetstream' : 'application/octet-stream';
  400. }
  401. if (@ob_get_length())
  402. {
  403. @ob_end_clean();
  404. }
  405. // Now send the File Contents to the Browser
  406. $size = @filesize($filename);
  407. // To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
  408. // Check if headers already sent or not able to get the file contents.
  409. if (headers_sent() || !@file_exists($filename) || !@is_readable($filename))
  410. {
  411. // PHP track_errors setting On?
  412. if (!empty($php_errormsg))
  413. {
  414. send_status_line(500, 'Internal Server Error');
  415. trigger_error($user->lang['UNABLE_TO_DELIVER_FILE'] . '<br />' . sprintf($user->lang['TRACKED_PHP_ERROR'], $php_errormsg));
  416. }
  417. send_status_line(500, 'Internal Server Error');
  418. trigger_error('UNABLE_TO_DELIVER_FILE');
  419. }
  420. // Now the tricky part... let's dance
  421. header('Pragma: public');
  422. /**
  423. * Commented out X-Sendfile support. To not expose the physical filename within the header if xsendfile is absent we need to look into methods of checking it's status.
  424. *
  425. * Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
  426. * lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
  427. *
  428. * Not really ideal, but should work fine...
  429. * <code>
  430. * if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
  431. * {
  432. * header('X-Sendfile: ' . $filename);
  433. * }
  434. * </code>
  435. */
  436. // Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
  437. $is_ie8 = (strpos(strtolower($user->browser), 'msie 8.0') !== false);
  438. header('Content-Type: ' . $attachment['mimetype']);
  439. if ($is_ie8)
  440. {
  441. header('X-Content-Type-Options: nosniff');
  442. }
  443. if ($category == ATTACHMENT_CATEGORY_FLASH && request_var('view', 0) === 1)
  444. {
  445. // We use content-disposition: inline for flash files and view=1 to let it correctly play with flash player 10 - any other disposition will fail to play inline
  446. header('Content-Disposition: inline');
  447. }
  448. else
  449. {
  450. if (empty($user->browser) || (!$is_ie8 && (strpos(strtolower($user->browser), 'msie') !== false)))
  451. {
  452. header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
  453. if (empty($user->browser) || (strpos(strtolower($user->browser), 'msie 6.0') !== false))
  454. {
  455. header('expires: -1');
  456. }
  457. }
  458. else
  459. {
  460. header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ? 'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
  461. if ($is_ie8 && (strpos($attachment['mimetype'], 'image') !== 0))
  462. {
  463. header('X-Download-Options: noopen');
  464. }
  465. }
  466. }
  467. if ($size)
  468. {
  469. header("Content-Length: $size");
  470. }
  471. // Close the db connection before sending the file
  472. $db->sql_close();
  473. if (!set_modified_headers($attachment['filetime'], $user->browser))
  474. {
  475. // Try to deliver in chunks
  476. @set_time_limit(0);
  477. $fp = @fopen($filename, 'rb');
  478. if ($fp !== false)
  479. {
  480. while (!feof($fp))
  481. {
  482. echo fread($fp, 8192);
  483. }
  484. fclose($fp);
  485. }
  486. else
  487. {
  488. @readfile($filename);
  489. }
  490. flush();
  491. }
  492. file_gc();
  493. }
  494. /**
  495. * Get a browser friendly UTF-8 encoded filename
  496. */
  497. function header_filename($file)
  498. {
  499. $user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
  500. // There be dragons here.
  501. // Not many follows the RFC...
  502. if (strpos($user_agent, 'MSIE') !== false || strpos($user_agent, 'Safari') !== false || strpos($user_agent, 'Konqueror') !== false)
  503. {
  504. return "filename=" . rawurlencode($file);
  505. }
  506. // follow the RFC for extended filename for the rest
  507. return "filename*=UTF-8''" . rawurlencode($file);
  508. }
  509. /**
  510. * Check if downloading item is allowed
  511. */
  512. function download_allowed()
  513. {
  514. global $config, $user, $db;
  515. if (!$config['secure_downloads'])
  516. {
  517. return true;
  518. }
  519. $url = (!empty($_SERVER['HTTP_REFERER'])) ? trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
  520. if (!$url)
  521. {
  522. return ($config['secure_allow_empty_referer']) ? true : false;
  523. }
  524. // Split URL into domain and script part
  525. $url = @parse_url($url);
  526. if ($url === false)
  527. {
  528. return ($config['secure_allow_empty_referer']) ? true : false;
  529. }
  530. $hostname = $url['host'];
  531. unset($url);
  532. $allowed = ($config['secure_allow_deny']) ? false : true;
  533. $iplist = array();
  534. if (($ip_ary = @gethostbynamel($hostname)) !== false)
  535. {
  536. foreach ($ip_ary as $ip)
  537. {
  538. if ($ip)
  539. {
  540. $iplist[] = $ip;
  541. }
  542. }
  543. }
  544. // Check for own server...
  545. $server_name = $user->host;
  546. // Forcing server vars is the only way to specify/override the protocol
  547. if ($config['force_server_vars'] || !$server_name)
  548. {
  549. $server_name = $config['server_name'];
  550. }
  551. if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
  552. {
  553. $allowed = true;
  554. }
  555. // Get IP's and Hostnames
  556. if (!$allowed)
  557. {
  558. $sql = 'SELECT site_ip, site_hostname, ip_exclude
  559. FROM ' . SITELIST_TABLE;
  560. $result = $db->sql_query($sql);
  561. while ($row = $db->sql_fetchrow($result))
  562. {
  563. $site_ip = trim($row['site_ip']);
  564. $site_hostname = trim($row['site_hostname']);
  565. if ($site_ip)
  566. {
  567. foreach ($iplist as $ip)
  568. {
  569. if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
  570. {
  571. if ($row['ip_exclude'])
  572. {
  573. $allowed = ($config['secure_allow_deny']) ? false : true;
  574. break 2;
  575. }
  576. else
  577. {
  578. $allowed = ($config['secure_allow_deny']) ? true : false;
  579. }
  580. }
  581. }
  582. }
  583. if ($site_hostname)
  584. {
  585. if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
  586. {
  587. if ($row['ip_exclude'])
  588. {
  589. $allowed = ($config['secure_allow_deny']) ? false : true;
  590. break;
  591. }
  592. else
  593. {
  594. $allowed = ($config['secure_allow_deny']) ? true : false;
  595. }
  596. }
  597. }
  598. }
  599. $db->sql_freeresult($result);
  600. }
  601. return $allowed;
  602. }
  603. /**
  604. * Check if the browser has the file already and set the appropriate headers-
  605. * @returns false if a resend is in order.
  606. */
  607. function set_modified_headers($stamp, $browser)
  608. {
  609. // let's see if we have to send the file at all
  610. $last_load = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime(trim($_SERVER['HTTP_IF_MODIFIED_SINCE'])) : false;
  611. if ((strpos(strtolower($browser), 'msie 6.0') === false) && (strpos(strtolower($browser), 'msie 8.0') === false))
  612. {
  613. if ($last_load !== false && $last_load >= $stamp)
  614. {
  615. send_status_line(304, 'Not Modified');
  616. // seems that we need those too ... browsers
  617. header('Pragma: public');
  618. header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 31536000));
  619. return true;
  620. }
  621. else
  622. {
  623. header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stamp) . ' GMT');
  624. }
  625. }
  626. return false;
  627. }
  628. function file_gc()
  629. {
  630. global $cache, $db;
  631. if (!empty($cache))
  632. {
  633. $cache->unload();
  634. }
  635. $db->sql_close();
  636. exit;
  637. }
  638. ?>