-
Notifications
You must be signed in to change notification settings - Fork 1
/
attachment.php
527 lines (471 loc) · 15.7 KB
/
attachment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 4.2.0 Patch Level 3 - Licence Number VBFBED0615
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2012 vBulletin Solutions Inc. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
@ini_set('zlib.output_compression', 'Off');
@set_time_limit(0);
if (@ini_get('output_handler') == 'ob_gzhandler' AND @ob_get_length() !== false)
{ // if output_handler = ob_gzhandler, turn it off and remove the header sent by PHP
@ob_end_clean();
header('Content-Encoding:');
}
// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'attachment');
define('CSRF_PROTECTION', true);
define('NOHEADER', 1);
define('NOZIP', 1);
define('NOCOOKIES', 1);
define('NOPMPOPUP', 1);
define('NONOTICES', 1);
// attachment.php/$attachmentid/file.mp3 -- for podcast and confused clients that determine file type in <enclosure> by the url extension <iTunes, I'm looking in your direction>
if (!$_REQUEST['attachmentid'])
{
$url_info = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
if ($url_info != '')
{
preg_match('#attachment\.php/(\d+)/#si', $url_info, $matches);
$_REQUEST['attachmentid'] = intval($matches[1]);
}
}
if (empty($_REQUEST['attachmentid']))
{
// return not found header
$sapi_name = php_sapi_name();
if ($sapi_name == 'cgi' OR $sapi_name == 'cgi-fcgi')
{
header('Status: 404 Not Found');
}
else
{
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
}
exit;
}
if ($_REQUEST['stc'] == 1) // we were called as <img src=> from showthread.php
{
define('NOSHUTDOWNFUNC', 1);
}
// Immediately send back the 304 Not Modified header if this image is cached, don't load global.php
// 3.5.x allows overwriting of attachments so we add the dateline to attachment links to avoid caching
if (!isset($_SERVER['HTTP_RANGE']) AND (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) OR !empty($_SERVER['HTTP_IF_NONE_MATCH'])))
{
$sapi_name = php_sapi_name();
if ($sapi_name == 'cgi' OR $sapi_name == 'cgi-fcgi')
{
header('Status: 304 Not Modified');
}
else
{
header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
}
// remove the content-type and X-Powered headers to emulate a 304 Not Modified response as close as possible
header('Content-Type:');
header('X-Powered-By:');
if (!empty($_REQUEST['attachmentid']))
{
header('ETag: "' . intval($_REQUEST['attachmentid']) . '"');
}
exit;
}
// if $_POST['ajax'] is set, we need to set a $_REQUEST['do'] so we can precache the lightbox template
if (!empty($_POST['ajax']) AND isset($_POST['uniqueid']))
{
$_REQUEST['do'] = 'lightbox';
}
// #################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();
// get special data templates from the datastore
$specialtemplates = array();
// pre-cache templates used by all actions
$globaltemplates = array();
// pre-cache templates used by specific actions
$actiontemplates = array('lightbox' => array('lightbox'));
/*
The following headers are usually handled internally but we do our own thing
with attachments, the cache-control is to stop caches keeping private attachments
and the Vary header is to deal with the fact the filename encoding changes.
*/
header('Cache-Control: private');
header('Vary: User-Agent');
// ########################## REQUIRE BACK-END ############################
require_once('./global.php');
require_once(DIR . '/packages/vbattach/attach.php');
// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################
$vbulletin->input->clean_array_gpc('r', array(
'attachmentid' => TYPE_UINT,
'thumb' => TYPE_BOOL,
'cid' => TYPE_UINT,
));
$vbulletin->input->clean_array_gpc('p', array(
'ajax' => TYPE_BOOL,
'uniqueid' => TYPE_UINT
));
if (!($attach =& vB_Attachment_Display_Single_Library::fetch_library($vbulletin, $vbulletin->GPC['cid'], $vbulletin->GPC['thumb'], $vbulletin->GPC['attachmentid'])))
{
eval(standard_error(fetch_error('invalidid', $vbphrase['attachment'], $vbulletin->options['contactuslink'])));
}
$result = $attach->verify_attachment();
if ($result === false)
{
eval(standard_error(fetch_error('invalidid', $vbphrase['attachment'], $vbulletin->options['contactuslink'])));
}
else if ($result === 0)
{
header('Content-type: image/gif');
readfile(DIR . '/' . $vbulletin->options['cleargifurl']);
exit;
}
else if ($result === -1)
{
print_no_permission();
}
$attachmentinfo = $attach->fetch_attachmentinfo();
// this convoluted mess sets the $threadinfo/$foruminfo arrays for the session.inthread and session.inforum values
if ($browsinginfo = $attach->fetch_browsinginfo())
{
foreach ($browsinginfo AS $arrayname => $values)
{
$$arrayname = array();
foreach ($values AS $index => $value)
{
$$arrayname[$$index] = $value;
}
}
}
// handle lightbox requests
if ($_REQUEST['do'] == 'lightbox')
{
$vbulletin->input->clean_array_gpc('r', array(
'width' => TYPE_UINT,
'height' => TYPE_UINT,
'first' => TYPE_BOOL,
'last' => TYPE_BOOL,
'current' => TYPE_UINT,
'total' => TYPE_UINT
));
$width = $vbulletin->GPC['width'];
$height = $vbulletin->GPC['height'];
$first = $vbulletin->GPC['first'];
$last = $vbulletin->GPC['last'];
$current = $vbulletin->GPC['current'];
$total = $vbulletin->GPC['total'];
require_once(DIR . '/includes/class_xml.php');
$xml = new vB_AJAX_XML_Builder($vbulletin, 'text/xml');
if (in_array(strtolower($attachmentinfo['extension']), array('jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp')))
{
$uniqueid = $vbulletin->GPC['uniqueid'];
$imagelink = 'attachment.php?' . $vbulletin->session->vars['sessionurl'] . 'attachmentid=' . $attachmentinfo['attachmentid'] . '&d=' . $attachmentinfo['dateline'];
$attachmentinfo['date_string'] = vbdate($vbulletin->options['dateformat'], $attachmentinfo['dateline']);
$attachmentinfo['time_string'] = vbdate($vbulletin->options['timeformat'], $attachmentinfo['dateline']);
$attachmentinfo['filename'] = fetch_censored_text(htmlspecialchars_uni($attachmentinfo['filename'], false));
$show['newwindow'] = ($attachmentinfo['newwindow'] ? true : false);
($hook = vBulletinHook::fetch_hook('attachment_lightbox')) ? eval($hook) : false;
$templater = vB_Template::create('lightbox');
$templater->register('attachmentinfo', $attachmentinfo);
$templater->register('current', $current);
$templater->register('first', $first);
$templater->register('height', $height);
$templater->register('imagelink', $imagelink);
$templater->register('last', $last);
$templater->register('total', $total);
$templater->register('uniqueid', $uniqueid);
$templater->register('width', $width);
$html = $templater->render(true);
$xml->add_group('img');
$xml->add_tag('html', process_replacement_vars($html));
$xml->add_tag('link', $imagelink);
$xml->add_tag('name', $attachmentinfo['filename']);
$xml->add_tag('date', $attachmentinfo['date_string']);
$xml->add_tag('time', $attachmentinfo['time_string']);
$xml->close_group();
}
else
{
$xml->add_group('errormessage');
$xml->add_tag('error', 'notimage');
$xml->add_tag('extension', $attachmentinfo['extension']);
$xml->close_group();
}
$xml->print_xml();
}
if ($attachmentinfo['extension'])
{
$extension = strtolower($attachmentinfo['extension']);
}
else
{
$extension = strtolower(file_extension($attachmentinfo['filename']));
}
if ($vbulletin->options['attachfile'])
{
require_once(DIR . '/includes/functions_file.php');
if ($vbulletin->GPC['thumb'])
{
$attachpath = fetch_attachment_path($attachmentinfo['uploader'], $attachmentinfo['filedataid'], true);
}
else
{
$attachpath = fetch_attachment_path($attachmentinfo['uploader'], $attachmentinfo['filedataid']);
}
if ($permissions['adminpermissions'] & $vbulletin->bf_ugp_adminpermissions['cancontrolpanel'])
{
if (!($fp = fopen($attachpath, 'rb')))
{
exit;
}
}
else if (!($fp = @fopen($attachpath, 'rb')))
{
$filedata = vb_base64_decode('R0lGODlhAQABAIAAAMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
$filesize = strlen($filedata);
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1
header('Pragma: no-cache'); // HTTP/1.0
header("Content-disposition: inline; filename=clear.gif");
header('Content-transfer-encoding: binary');
header("Content-Length: $filesize");
header('Content-type: image/gif');
echo $filedata;
exit;
}
}
$startbyte = 0;
$lastbyte = $attachmentinfo['filesize'] - 1;
if (isset($_SERVER['HTTP_RANGE']))
{
preg_match('#^bytes=(-?([0-9]+))(-([0-9]*))?$#', $_SERVER['HTTP_RANGE'], $matches);
if (intval($matches[1]) < 0)
{ // its negative so we want to take this value from last byte
$startbyte = $attachmentinfo['filesize'] - $matches[2];
}
else
{
$startbyte = intval($matches[2]);
if ($matches[4])
{
$lastbyte = $matches[4];
}
}
if ($startbyte < 0 OR $startbyte >= $attachmentinfo['filesize'])
{
if (SAPI_NAME == 'cgi' OR SAPI_NAME == 'cgi-fcgi')
{
header('Status: 416 Requested Range Not Satisfiable');
}
else
{
header($_SERVER['SERVER_PROTOCOL'] . ' 416 Requested Range Not Satisfiable');
}
header('Accept-Ranges: bytes');
header('Content-Range: bytes */'. $attachmentinfo['filesize']);
exit;
}
}
// send jpeg header for PDF, BMP, TIF, TIFF, and PSD thumbnails as they are jpegs
if ($vbulletin->GPC['thumb'] AND in_array($extension, array('bmp', 'tif', 'tiff', 'psd', 'pdf')))
{
$attachmentinfo['filename'] = preg_replace('#.(bmp|tiff?|psd|pdf)$#i', '.jpg', $attachmentinfo['filename']);
$mimetype = array('Content-type: image/jpeg');
}
else
{
$mimetype = unserialize($attachmentinfo['mimetype']);
}
($hook = vBulletinHook::fetch_hook('attachment_process_start')) ? eval($hook) : false;
header('Pragma:'); // VBIV-8269
header('Cache-control: max-age=31536000, private');
header('Expires: ' . gmdate("D, d M Y H:i:s", TIMENOW + 31536000) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $attachmentinfo['dateline']) . ' GMT');
header('ETag: "' . $attachmentinfo['attachmentid'] . '"');
header('Accept-Ranges: bytes');
// look for entities in the file name, and if found try to convert
// the filename to UTF-8
$filename = $attachmentinfo['filename'];
if (preg_match('~&#([0-9]+);~', $filename))
{
if (function_exists('iconv'))
{
$filename_conv = @iconv(vB_Template_Runtime::fetchStyleVar('charset'), 'UTF-8//IGNORE', $filename);
if ($filename_conv !== false)
{
$filename = $filename_conv;
}
}
$filename = preg_replace(
'~&#([0-9]+);~e',
"convert_int_to_utf8('\\1')",
$filename
);
$filename_charset = 'utf-8';
}
else
{
$filename_charset = vB_Template_Runtime::fetchStyleVar('charset');
}
$filename = preg_replace('#[\r\n]#', '', $filename);
// Opera and IE have not a clue about this, mozilla puts on incorrect extensions.
if (is_browser('mozilla'))
{
$filename = "filename*=" . $filename_charset . "''" . rawurlencode($filename);
}
else
{
// other browsers seem to want names in UTF-8
if ($filename_charset != 'utf-8' AND function_exists('iconv'))
{
$filename_conv = iconv($filename_charset, 'UTF-8//IGNORE', $filename);
if ($filename_conv !== false)
{
$filename = $filename_conv;
}
}
if (is_browser('opera') OR is_browser('konqueror') OR is_browser('safari'))
{
// Opera / Konqueror does not support encoded file names
$filename = 'filename="' . str_replace('"', '', $filename) . '"';
}
else
{
// encode the filename to stay within spec
$filename = 'filename="' . rawurlencode($filename) . '"';
}
}
if (in_array($extension, array('jpg', 'jpe', 'jpeg', 'gif', 'png')))
{
header("Content-disposition: inline; $filename");
header('Content-transfer-encoding: binary');
}
else
{
// force files to be downloaded because of a possible XSS issue in IE
header("Content-disposition: attachment; $filename");
}
if ($startbyte != 0 OR $lastbyte != ($attachmentinfo['filesize'] - 1))
{
if (SAPI_NAME == 'cgi' OR SAPI_NAME == 'cgi-fcgi')
{
header('Status: 206 Partial Content');
}
else
{
header($_SERVER['SERVER_PROTOCOL'] . ' 206 Partial Content');
}
header('Content-Range: bytes '. $startbyte .'-'. $lastbyte .'/'. $attachmentinfo['filesize']);
}
header('Content-Length: ' . (($lastbyte + 1) - $startbyte));
if (is_array($mimetype))
{
foreach ($mimetype AS $header)
{
if (!empty($header))
{
header($header);
}
}
}
else
{
header('Content-type: unknown/unknown');
}
// This is new in IE8 and tells the browser not to try and guess
header('X-Content-Type-Options: nosniff');
// prevent flash from ever considering this to be a cross domain file
header('X-Permitted-Cross-Domain-Policies: none');
($hook = vBulletinHook::fetch_hook('attachment_display')) ? eval($hook) : false;
// update views counter
if (!$vbulletin->GPC['thumb'] AND connection_status() == 0 AND $lastbyte == ($attachmentinfo['filesize'] - 1))
{
if ($vbulletin->options['attachmentviewslive'])
{
// doing it as they happen; not using a DM to avoid overhead
$db->query_write("
UPDATE " . TABLE_PREFIX . "attachment SET
counter = counter + 1
WHERE attachmentid = $attachmentinfo[attachmentid]
");
}
else
{
// or doing it once an hour
$query = "INSERT INTO " . TABLE_PREFIX . "attachmentviews (attachmentid)
VALUES ($attachmentinfo[attachmentid])
";
defined('NOSHUTDOWNFUNC') ? $db->query_write($query) : $db->shutdown_query($query);
}
}
if ($vbulletin->options['attachfile'])
{
if (defined('NOSHUTDOWNFUNC'))
{
if ($_GET['stc'] == 1)
{
$db->close();
}
else
{
exec_shut_down();
}
}
if ($startbyte > 0)
{
fseek($fp, $startbyte);
}
while (connection_status() == 0 AND $startbyte <= $lastbyte)
{ // You can limit bandwidth by decreasing the values in the read size call, they must be equal.
$size = $lastbyte - $startbyte;
$readsize = ($size > 1048576) ? 1048576 : $size + 1;
echo @fread($fp, $readsize);
$startbyte += $readsize;
flush();
}
@fclose($fp);
}
else
{
// start grabbing the filedata in batches of 2mb
while (connection_status() == 0 AND $startbyte <= $lastbyte)
{
$size = $lastbyte - $startbyte;
$readsize = ($size > 2097152) ? 2097152 : $size + 1;
$attachmentinfo = $db->query_first_slave("
SELECT filedataid, SUBSTRING(" . ((!empty($vbulletin->GPC['thumb']) ? 'thumbnail' : 'filedata')) . ", $startbyte + 1, $readsize) AS filedata
FROM " . TABLE_PREFIX . "filedata
WHERE filedataid = $attachmentinfo[filedataid]
");
echo $attachmentinfo['filedata'];
$startbyte += $readsize;
flush();
}
if (defined('NOSHUTDOWNFUNC'))
{
if ($_GET['stc'] == 1)
{
$db->close();
}
else
{
exec_shut_down();
}
}
}
($hook = vBulletinHook::fetch_hook('attachment_complete')) ? eval($hook) : false;
/*======================================================================*\
|| ####################################################################
|| # Downloaded: 16:29, Sun Dec 9th 2012
|| # CVS: $RCSfile$ - $Revision: 62621 $
|| ####################################################################
\*======================================================================*/
?>