qa-blob.php 2.22 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Response to blob requests, outputting blob from the database


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/


Scott committed
23
// Ensure no PHP errors are shown in the blob response
Scott committed
24

Scott committed
25
@ini_set('display_errors', 0);
Scott committed
26

Scott committed
27 28 29 30 31
function qa_blob_db_fail_handler()
{
	header('HTTP/1.1 500 Internal Server Error');
	qa_exit('error');
}
Scott committed
32 33


Scott committed
34
// Load the Q2A base file which sets up a bunch of crucial stuff
Scott committed
35

36
$qa_autoconnect = false;
Scott committed
37
require 'qa-base.php';
Scott committed
38

Scott committed
39
qa_report_process_stage('init_blob');
Scott committed
40 41


Scott committed
42
// Output the blob in question
Scott committed
43

Scott committed
44
require_once QA_INCLUDE_DIR . 'app/blobs.php';
Scott committed
45

Scott committed
46
qa_db_connect('qa_blob_db_fail_handler');
47
qa_initialize_postdb_plugins();
Scott committed
48

Scott committed
49
$blob = qa_read_blob(qa_get('qa_blobid'));
Scott committed
50

51
if (isset($blob) && isset($blob['content'])) {
Scott committed
52 53
	// allows browsers and proxies to cache the blob (30 days)
	header('Cache-Control: max-age=2592000, public');
Scott committed
54

Scott committed
55
	$disposition = 'inline';
Scott committed
56

Scott committed
57 58 59 60 61
	switch ($blob['format']) {
		case 'jpeg':
		case 'jpg':
			header('Content-Type: image/jpeg');
			break;
Scott committed
62

Scott committed
63 64 65
		case 'gif':
			header('Content-Type: image/gif');
			break;
Scott committed
66

Scott committed
67 68 69
		case 'png':
			header('Content-Type: image/png');
			break;
Scott committed
70

Scott committed
71 72 73
		case 'pdf':
			header('Content-Type: application/pdf');
			break;
Scott committed
74

Scott committed
75 76 77
		case 'swf':
			header('Content-Type: application/x-shockwave-flash');
			break;
Scott committed
78

Scott committed
79 80
		default:
			header('Content-Type: application/octet-stream');
Scott committed
81
			$disposition = 'attachment';
Scott committed
82 83
			break;
	}
Scott committed
84

Scott committed
85 86
	// for compatibility with HTTP headers and all browsers
	$filename = preg_replace('/[^A-Za-z0-9 \\._-]+/', '', $blob['filename']);
Scott committed
87
	header('Content-Disposition: ' . $disposition . '; filename="' . $filename . '"');
Scott committed
88

Scott committed
89
	echo $blob['content'];
Scott committed
90

Scott committed
91 92 93
} else {
	header('HTTP/1.0 404 Not Found');
}
Scott committed
94

Scott committed
95
qa_db_disconnect();