-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathimg.php
42 lines (41 loc) · 1.59 KB
/
img.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
<?php
// img.php by pdq 2011 =)
error_reporting(0);
/* Locate images folder outside of webroot */
define('BITBUCKET_DIR', DIRECTORY_SEPARATOR.'var'.DIRECTORY_SEPARATOR.'bucket'); // /path/to/bitbucket
/* Sanity checking */
function valid_path($root, $input)
{
$fullpath = $root.$input;
$fullpath = realpath($fullpath);
$root = realpath($root);
$rl = strlen($root);
return ($root != substr($fullpath, 0, $rl)) ? NULL : $fullpath;
}
/* Process request */
if (isset($_SERVER['REQUEST_URI'])) {
$image = valid_path(BITBUCKET_DIR, substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME'])));
if (!((($pi = pathinfo($image)) && preg_match('#^(jpg|jpeg|gif|png)$#i', $pi['extension'])) && $image && is_file($image))) die('^_^');
$img['last_mod'] = filemtime($image);
$img['date_fmt'] = 'D, d M Y H:i:s T';
$img['lm_date'] = date($img['date_fmt'], $img['last_mod']);
$img['ex_date'] = date($img['date_fmt'], time() + (86400 * 7));
$img['stop'] = false;
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$img['since'] = explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'], 2);
$img['since'] = strtotime($img['since'][0]);
if ($img['since'] == $img['last_mod']) {
header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
$img['stop'] = true;
}
}
header('Expires: '.$img['ex_date']);
header('Cache-Control: private, max-age=604800');
if ($img['stop']) die();
header('Last-Modified: '.$img['lm_date']);
header('Content-type: image/'.$pi['extension']);
readfile($image);
exit();
}
// End of File
?>