123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- if (!defined('SMF'))
- die('No direct access...');
- function showAvatar()
- {
- global $smcFunc, $modSettings, $maintenance;
-
- if(empty($_GET['attach']) || (string)$_GET['attach'] != (string)(int)$_GET['attach'])
- die;
-
- if(!empty($maintenance) && $maintenance == 2)
- die;
-
- if(!empty($modSettings['enableCompressedOutput']) && !headers_sent() && ob_get_length() == 0)
- {
- if(@ini_get('zlib.output_compression') == '1' || @ini_get('output_handler') == 'ob_gzhandler')
- $modSettings['enableCompressedOutput'] = 0;
- else
- ob_start('ob_gzhandler');
- }
- if(empty($modSettings['enableCompressedOutput']))
- {
- ob_start();
- header('Content-Encoding: none');
- }
-
- $id_attach = (int) $_GET['attach'];
-
- if(($cache = cache_get_data('avatar_lookup_id-'. $id_attach)) != null)
- $file = $cache;
-
- else
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_folder, filename AS real_filename, file_hash, fileext, id_attach, attachment_type, mime_type, approved, id_member
- FROM {db_prefix}attachments
- WHERE id_attach = {int:id_attach}
- AND id_member > {int:blank_id_member}
- LIMIT 1',
- array(
- 'id_attach' => $id_attach,
- 'blank_id_member' => 0,
- )
- );
- $file = $smcFunc['db_fetch_assoc']($request);
-
- if ($file['attachment_type'] != 3)
- $smcFunc['db_query']('attach_download_increase', '
- UPDATE LOW_PRIORITY {db_prefix}attachments
- SET downloads = downloads + 1
- WHERE id_attach = {int:id_attach}',
- array(
- 'id_attach' => $id_attach,
- )
- );
- $file['filename'] = getAttachmentFilename($file['real_filename'], $id_attach, $file['id_folder'], false, $file['file_hash']);
-
- $file['etag'] = '"'. function_exists('md5_file') ? md5_file($file['filename']) : md5(file_get_contents($file['filename'])). '"';
-
- cache_put_data('avatar_lookup_id-'. $id_attach, $file, mt_rand(850, 900));
- }
-
- if(!file_exists($file['filename']))
- {
- header('HTTP/1.0 404 File Not Found');
- die('404 File Not Found');
- }
-
- if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']))
- {
- list($modified_since) = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
- if (strtotime($modified_since) >= filemtime($file['filename']))
- {
- ob_end_clean();
-
- header('HTTP/1.1 304 Not Modified');
- exit;
- }
- }
- header('Pragma: ');
- header('Expires: '. gmdate('D, d M Y H:i:s', time() + 31536000). ' GMT');
- header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($file['filename'])). ' GMT');
- header('Accept-Ranges: bytes');
- header('Connection: close');
- header('ETag: '. $file['etag']);
- header('Content-Type: '. $file['mime_type']);
-
- if (filesize($file['filename']) > 4194304)
- {
-
- while (@ob_get_level() > 0)
- @ob_end_clean();
- $fp = fopen($file['filename'], 'rb');
- while (!feof($fp))
- {
- print fread($fp, 8192);
- flush();
- }
- fclose($fp);
- }
-
- elseif (@readfile($file['filename']) === null)
- print file_get_contents($file['filename']);
- die();
- }
|