6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 define('IN_PHPBB', true);
15 $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH
: './../';
16 $phpEx = substr(strrchr(__FILE__
, '.'), 1);
18 if (isset($_GET['avatar']))
20 require($phpbb_root_path . 'config.' . $phpEx);
22 if (!defined('PHPBB_INSTALLED') ||
empty($dbms) ||
empty($acm_type))
27 require($phpbb_root_path . 'includes/acm/acm_' . $acm_type . '.' . $phpEx);
28 require($phpbb_root_path . 'includes/cache.' . $phpEx);
29 require($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
30 require($phpbb_root_path . 'includes/constants.' . $phpEx);
36 if (!@$db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, false))
43 $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ?
htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0';
45 $config = cache
::obtain_config();
46 $filename = $_GET['avatar'];
47 $avatar_group = false;
48 if ($filename[0] === 'g')
51 $filename = substr($filename, 1);
54 // '==' is not a bug - . as the first char is as bad as no dot at all
55 if (strpos($filename, '.') == false)
57 header('HTTP/1.0 403 forbidden');
66 $ext = substr(strrchr($filename, '.'), 1);
67 $stamp = (int) substr(stristr($filename, '_'), 1);
68 $filename = (int) $filename;
70 // let's see if we have to send the file at all
71 $last_load = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
strtotime(trim($_SERVER['HTTP_IF_MODIFIED_SINCE'])) : false;
72 if (strpos(strtolower($browser), 'msie 6.0') === false)
74 if ($last_load !== false && $last_load <= $stamp)
76 if (@php_sapi_name
() === 'CGI')
78 header('Status: 304 Not Modified', true, 304);
82 header('HTTP/1.0 304 Not Modified', true, 304);
84 // seems that we need those too ... browsers
85 header('Pragma: public');
86 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() +
31536000));
91 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $stamp) . ' GMT');
95 if (!in_array($ext, array('png', 'gif', 'jpg', 'jpeg')))
97 // no way such an avatar could exist. They are not following the rules, stop the show.
98 header("HTTP/1.0 403 forbidden");
109 // no way such an avatar could exist. They are not following the rules, stop the show.
110 header("HTTP/1.0 403 forbidden");
119 send_avatar_to_browser(($avatar_group ?
'g' : '') . $filename . '.' . $ext, $browser);
129 // implicit else: we are not in avatar mode
130 include($phpbb_root_path . 'common.' . $phpEx);
132 $download_id = request_var('id', 0);
133 $mode = request_var('mode', '');
134 $thumbnail = request_var('t', false);
136 // Start session management, do not update session page.
137 $user->session_begin(false);
138 $auth->acl($user->data
);
139 $user->setup('viewtopic');
143 trigger_error('NO_ATTACHMENT_SELECTED');
146 if (!$config['allow_attachments'] && !$config['allow_pm_attach'])
148 trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
151 $sql = 'SELECT attach_id, in_message, post_msg_id, extension, is_orphan, poster_id
152 FROM ' . ATTACHMENTS_TABLE
. "
153 WHERE attach_id = $download_id";
154 $result = $db->sql_query_limit($sql, 1);
155 $attachment = $db->sql_fetchrow($result);
156 $db->sql_freeresult($result);
160 trigger_error('ERROR_NO_ATTACHMENT');
163 if ((!$attachment['in_message'] && !$config['allow_attachments']) ||
($attachment['in_message'] && !$config['allow_pm_attach']))
165 trigger_error('ATTACHMENT_FUNCTIONALITY_DISABLED');
170 if ($attachment['is_orphan'])
172 // We allow admins having attachment permissions to see orphan attachments...
173 $own_attachment = ($auth->acl_get('a_attach') ||
$attachment['poster_id'] == $user->data
['user_id']) ?
true : false;
175 if (!$own_attachment ||
($attachment['in_message'] && !$auth->acl_get('u_pm_download')) ||
(!$attachment['in_message'] && !$auth->acl_get('u_download')))
177 trigger_error('ERROR_NO_ATTACHMENT');
180 // Obtain all extensions...
181 $extensions = cache
::obtain_attach_extensions(true);
185 if (!$attachment['in_message'])
188 $sql = 'SELECT p.forum_id, f.forum_password, f.parent_id
189 FROM ' . POSTS_TABLE
. ' p, ' . FORUMS_TABLE
. ' f
190 WHERE p.post_id = ' . $attachment['post_msg_id'] . '
191 AND p.forum_id = f.forum_id';
192 $result = $db->sql_query_limit($sql, 1);
193 $row = $db->sql_fetchrow($result);
194 $db->sql_freeresult($result);
196 // Global announcement?
197 $f_download = (!$row) ?
$auth->acl_getf_global('f_download') : $auth->acl_get('f_download', $row['forum_id']);
199 if ($auth->acl_get('u_download') && $f_download)
201 if ($row && $row['forum_password'])
203 // Do something else ... ?
204 login_forum_box($row);
209 trigger_error('SORRY_AUTH_VIEW_ATTACH');
214 $row['forum_id'] = false;
215 if (!$auth->acl_get('u_pm_download'))
217 header('HTTP/1.0 403 forbidden');
218 trigger_error('SORRY_AUTH_VIEW_ATTACH');
221 // Check if the attachment is within the users scope...
222 $sql = 'SELECT user_id, author_id
223 FROM ' . PRIVMSGS_TO_TABLE
. '
224 WHERE msg_id = ' . $attachment['post_msg_id'];
225 $result = $db->sql_query($sql);
228 while ($user_row = $db->sql_fetchrow($result))
230 if ($user->data
['user_id'] == $user_row['user_id'] ||
$user->data
['user_id'] == $user_row['author_id'])
236 $db->sql_freeresult($result);
240 header('HTTP/1.0 403 forbidden');
241 trigger_error('ERROR_NO_ATTACHMENT');
246 $extensions = array();
247 if (!extension_allowed($row['forum_id'], $attachment['extension'], $extensions))
249 trigger_error(sprintf($user->lang
['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension']));
253 if (!download_allowed())
255 header('HTTP/1.0 403 forbidden');
256 trigger_error($user->lang
['LINKAGE_FORBIDDEN']);
259 $download_mode = (int) $extensions[$attachment['extension']]['download_mode'];
261 // Fetching filename here to prevent sniffing of filename
262 $sql = 'SELECT attach_id, is_orphan, in_message, post_msg_id, extension, physical_filename, real_filename, mimetype
263 FROM ' . ATTACHMENTS_TABLE
. "
264 WHERE attach_id = $download_id";
265 $result = $db->sql_query_limit($sql, 1);
266 $attachment = $db->sql_fetchrow($result);
267 $db->sql_freeresult($result);
271 trigger_error('ERROR_NO_ATTACHMENT');
274 $attachment['physical_filename'] = basename($attachment['physical_filename']);
275 $display_cat = $extensions[$attachment['extension']]['display_cat'];
277 if (($display_cat == ATTACHMENT_CATEGORY_IMAGE ||
$display_cat == ATTACHMENT_CATEGORY_THUMB
) && !$user->optionget('viewimg'))
279 $display_cat = ATTACHMENT_CATEGORY_NONE
;
282 if ($display_cat == ATTACHMENT_CATEGORY_FLASH
&& !$user->optionget('viewflash'))
284 $display_cat = ATTACHMENT_CATEGORY_NONE
;
289 $attachment['physical_filename'] = 'thumb_' . $attachment['physical_filename'];
291 else if (($display_cat == ATTACHMENT_CATEGORY_NONE ||
$display_cat == ATTACHMENT_CATEGORY_IMAGE
) && !$attachment['is_orphan'])
293 // Update download count
294 $sql = 'UPDATE ' . ATTACHMENTS_TABLE
. '
295 SET download_count = download_count + 1
296 WHERE attach_id = ' . $attachment['attach_id'];
297 $db->sql_query($sql);
300 if ($display_cat == ATTACHMENT_CATEGORY_IMAGE
&& $mode === 'view' && (strpos($attachment['mimetype'], 'image') === 0) && strpos(strtolower($user->browser
), 'msie') !== false)
302 wrap_img_in_html(append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'id=' . $attachment['attach_id']), $attachment['real_filename']);
306 // Determine the 'presenting'-method
307 if ($download_mode == PHYSICAL_LINK
)
309 // This presenting method should no longer be used
310 if (!@is_dir
($phpbb_root_path . $config['upload_path']))
312 trigger_error($user->lang
['PHYSICAL_DOWNLOAD_NOT_POSSIBLE']);
315 redirect($phpbb_root_path . $config['upload_path'] . '/' . $attachment['physical_filename']);
320 send_file_to_browser($attachment, $config['upload_path'], $display_cat);
327 * A simplified function to deliver avatars
328 * The argument needs to be checked before calling this function.
330 function send_avatar_to_browser($file, $browser)
332 global $config, $phpbb_root_path;
334 $prefix = $config['avatar_salt'] . '_';
335 $image_dir = $config['avatar_path'];
337 // Adjust image_dir path (no trailing slash)
338 if (substr($image_dir, -1, 1) == '/' ||
substr($image_dir, -1, 1) == '\\')
340 $image_dir = substr($image_dir, 0, -1) . '/';
342 $image_dir = str_replace(array('../', '..\\', './', '.\\'), '', $image_dir);
344 if ($image_dir && ($image_dir[0] == '/' ||
$image_dir[0] == '\\'))
348 $file_path = $phpbb_root_path . $image_dir . '/' . $prefix . $file;
350 if ((@file_exists
($file_path) && @is_readable
($file_path)) && !headers_sent())
352 header('Pragma: public');
354 $image_data = @getimagesize
($file_path);
355 header('Content-Type: ' . image_type_to_mime_type($image_data[2]));
357 if (strpos(strtolower($browser), 'msie') !== false)
359 header('Content-Disposition: attachment; ' . header_filename($file));
361 if (strpos(strtolower($browser), 'msie 6.0') !== false)
363 header('Expires: -1');
367 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() +
31536000));
372 header('Content-Disposition: inline; ' . header_filename($file));
373 header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() +
31536000));
376 $size = @filesize
($file_path);
379 header("Content-Length: $size");
382 if (@readfile
($file_path) === false)
384 $fp = @fopen
($file_path, 'rb');
390 echo fread($fp, 8192);
400 header('HTTP/1.0 404 not found');
405 * Wraps an url into a simple html page. Used to display attachments in IE.
406 * this is a workaround for now; might be moved to template system later
407 * direct any complaints to 1 Microsoft Way, Redmond
409 function wrap_img_in_html($src, $title)
411 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Strict.dtd">';
414 echo '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
415 echo '<title>' . $title . '</title>';
419 echo '<img src="' . $src . '" alt="' . $title . '" />';
426 * Send file to browser
428 function send_file_to_browser($attachment, $upload_dir, $category)
430 global $user, $db, $config, $phpbb_root_path;
432 $filename = $phpbb_root_path . $upload_dir . '/' . $attachment['physical_filename'];
434 if (!@file_exists
($filename))
436 trigger_error($user->lang
['ERROR_NO_ATTACHMENT'] . '<br /><br />' . sprintf($user->lang
['FILE_NOT_FOUND_404'], $filename));
439 // Correct the mime type - we force application/octetstream for all files, except images
440 // Please do not change this, it is a security precaution
441 if ($category != ATTACHMENT_CATEGORY_IMAGE ||
strpos($attachment['mimetype'], 'image') !== 0)
443 $attachment['mimetype'] = (strpos(strtolower($user->browser
), 'msie') !== false ||
strpos(strtolower($user->browser
), 'opera') !== false) ?
'application/octetstream' : 'application/octet-stream';
446 if (@ob_get_length
())
451 // Now send the File Contents to the Browser
452 $size = @filesize
($filename);
454 // To correctly display further errors we need to make sure we are using the correct headers for both (unsetting content-length may not work)
456 // Check if headers already sent or not able to get the file contents.
457 if (headers_sent() ||
!@file_exists
($filename) ||
!@is_readable
($filename))
459 // PHP track_errors setting On?
460 if (!empty($php_errormsg))
462 trigger_error($user->lang
['UNABLE_TO_DELIVER_FILE'] . '<br />' . sprintf($user->lang
['TRACKED_PHP_ERROR'], $php_errormsg));
465 trigger_error('UNABLE_TO_DELIVER_FILE');
468 // Now the tricky part... let's dance
469 header('Pragma: public');
472 * 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.
474 * Try X-Sendfile since it is much more server friendly - only works if the path is *not* outside of the root path...
475 * lighttpd has core support for it. An apache2 module is available at http://celebnamer.celebworld.ws/stuff/mod_xsendfile/
477 * Not really ideal, but should work fine...
479 * if (strpos($upload_dir, '/') !== 0 && strpos($upload_dir, '../') === false)
481 * header('X-Sendfile: ' . $filename);
486 // Send out the Headers. Do not set Content-Disposition to inline please, it is a security measure for users using the Internet Explorer.
487 header('Content-Type: ' . $attachment['mimetype']);
489 if (empty($user->browser
) ||
(strpos(strtolower($user->browser
), 'msie') !== false))
491 header('Content-Disposition: attachment; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
492 if (empty($user->browser
) ||
(strpos(strtolower($user->browser
), 'msie 6.0') !== false))
494 header('expires: -1');
499 header('Content-Disposition: ' . ((strpos($attachment['mimetype'], 'image') === 0) ?
'inline' : 'attachment') . '; ' . header_filename(htmlspecialchars_decode($attachment['real_filename'])));
504 header("Content-Length: $size");
507 // Try to deliver in chunks
510 $fp = @fopen
($filename, 'rb');
516 echo fread($fp, 8192);
522 @readfile
($filename);
530 * Get a browser friendly UTF-8 encoded filename
532 function header_filename($file)
534 $user_agent = (!empty($_SERVER['HTTP_USER_AGENT'])) ?
htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : '';
536 // There be dragons here.
537 // Not many follows the RFC...
538 if (strpos($user_agent, 'MSIE') !== false ||
strpos($user_agent, 'Safari') !== false ||
strpos($user_agent, 'Konqueror') !== false)
540 return "filename=" . rawurlencode($file);
543 // follow the RFC for extended filename for the rest
544 return "filename*=UTF-8''" . rawurlencode($file);
548 * Check if downloading item is allowed
550 function download_allowed()
552 global $config, $user, $db;
554 if (!$config['secure_downloads'])
559 $url = (!empty($_SERVER['HTTP_REFERER'])) ?
trim($_SERVER['HTTP_REFERER']) : trim(getenv('HTTP_REFERER'));
563 return ($config['secure_allow_empty_referer']) ?
true : false;
566 // Split URL into domain and script part
567 $url = @parse_url
($url);
571 return ($config['secure_allow_empty_referer']) ?
true : false;
574 $hostname = $url['host'];
577 $allowed = ($config['secure_allow_deny']) ?
false : true;
580 if (($ip_ary = @gethostbynamel
($hostname)) !== false)
582 foreach ($ip_ary as $ip)
591 // Check for own server...
592 $server_name = $user->host
;
594 // Forcing server vars is the only way to specify/override the protocol
595 if ($config['force_server_vars'] ||
!$server_name)
597 $server_name = $config['server_name'];
600 if (preg_match('#^.*?' . preg_quote($server_name, '#') . '.*?$#i', $hostname))
605 // Get IP's and Hostnames
608 $sql = 'SELECT site_ip, site_hostname, ip_exclude
609 FROM ' . SITELIST_TABLE
;
610 $result = $db->sql_query($sql);
612 while ($row = $db->sql_fetchrow($result))
614 $site_ip = trim($row['site_ip']);
615 $site_hostname = trim($row['site_hostname']);
619 foreach ($iplist as $ip)
621 if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_ip, '#')) . '$#i', $ip))
623 if ($row['ip_exclude'])
625 $allowed = ($config['secure_allow_deny']) ?
false : true;
630 $allowed = ($config['secure_allow_deny']) ?
true : false;
638 if (preg_match('#^' . str_replace('\*', '.*?', preg_quote($site_hostname, '#')) . '$#i', $hostname))
640 if ($row['ip_exclude'])
642 $allowed = ($config['secure_allow_deny']) ?
false : true;
647 $allowed = ($config['secure_allow_deny']) ?
true : false;
652 $db->sql_freeresult($result);