-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdfproxy.php
90 lines (71 loc) · 2.18 KB
/
pdfproxy.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
<?php
error_reporting(E_ALL);
/*
Based on https://github.com/andrieslouw/imagesweserv, also borrows from
http://stackoverflow.com/questions/16847015/php-stream-remote-pdf-to-client-browser
Make remote PDF's cachable and accessible by pdf.js
*/
function download_file($path,$fname){
$options = array(
CURLOPT_FILE => fopen($fname, 'w'),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_URL => $path,
CURLOPT_FAILONERROR => true, // HTTP code > 400 will throw curl error
CURLOPT_TIMEOUT => 60,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; ImageFetcher/5.6; +http://images.weserv.nl/)',
);
//print_r($options);
$ch = curl_init();
curl_setopt_array($ch, $options);
$return = curl_exec($ch);
if ($return === false){
$error = curl_error($ch);
$errno = curl_errno($ch);
curl_close($ch);
unlink($fname);
$error_code = substr($error,0,3);
if($errno == 6){
header('HTTP/1.1 410 Gone');
header('X-Robots-Tag: none');
header('X-Gone-Reason: Hostname not in DNS or blocked by policy');
echo 'Error 410: Server could parse the ?url= that you were looking for "' . $path . '", because the hostname of the origin is unresolvable (DNS) or blocked by policy.';
echo 'Error: $error';
die;
}
if(in_array($error_code,array('400','403','404','500','502'))){
trigger_error('cURL Request error: '.$error.' URL: '.$path,E_USER_WARNING);
}
return array(false,$error);
}else{
curl_close($ch);
return array(true,NULL);
}
}
$url = '';
if (isset($_GET['url']))
{
$url = $_GET['url'];
}
if ($url != '')
{
// fetch PDF
$path = $url;
$path = str_replace(' ','%20',$path);
$fname = tempnam('/tmp', 'imo_');
$curl_result = download_file($path,$fname);
if($curl_result[0] === false){
header("HTTP/1.0 404 Not Found");
echo 'Error 404: Server could parse the ?url= that you were looking for, error it got: '.$curl_result[1];
die;
}
header('Expires: '.gmdate("D, d M Y H:i:s", (time()+2678400)).' GMT'); //31 days
header('Cache-Control: max-age=2678400'); //31 days
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($fname));
ob_start();
readfile($fname);
ob_end_flush();
}
?>