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