-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimg-server.php
More file actions
92 lines (70 loc) · 2.8 KB
/
Copy pathimg-server.php
File metadata and controls
92 lines (70 loc) · 2.8 KB
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
<?php
/*
Handles requests for FULL IMAGES, checks permission and redirects read to /img/ folder - without hash.
## /data/imgserver/...hash.png
RewriteCond %{REQUEST_URI} /data/imgserver/([^.]+)(\.[^.]+)?\.(jpg|jpeg|png|gif)$
RewriteRule .* img-server.php [NE,L]
(The thumbs are handled separatly, see .htaccess)
*/
// absolute filesystem path to this web root
define('WWW_DIR', dirname(__FILE__));
// absolute filesystem path to the application root
define('APP_DIR', WWW_DIR . '/app');
// absolute filesystem path to the libraries
define('LIBS_DIR', WWW_DIR . '/libs');
// Load Nette Framework or autoloader generated by Composer
require LIBS_DIR . '/autoload.php';
// Configure application
$configurator = new Nette\Config\Configurator;
// Enable RobotLoader - this will load all classes automatically
$configurator->setTempDirectory(APP_DIR . '/temp');
$configurator->createRobotLoader()
->addDirectory(APP_DIR)
->addDirectory(LIBS_DIR)
->register();
// Create Dependency Injection container from config.neon file
$configurator->addConfig(APP_DIR . '/config.neon');
if(file_exists(APP_DIR . '/config-server.neon'))
$configurator->addConfig(APP_DIR . '/config-server.neon');
$container = $configurator->createContainer();
//error_reporting(E_ALL & ~E_NOTICE);
//------------------ do tricks: ------------
$img = basename($container->httpRequest->url->path);
preg_match('~^(\d+)-(\d+)-([0-9a-z]+).png$~', $img, $m);
$allowed = (\Casopisy\Obsah::getFilesSecretHash($m[1], $m[2], "") == $m[3]) OR $container->user->isInRole('admin');
//check permission
if (file_exists("data/img/$m[1]-$m[2].png") AND $allowed) {
$file = "data/img/$m[1]-$m[2].png";
$etag = md5($file.filemtime($file));
//allow http caching - http://stackoverflow.com/questions/1971721/how-to-use-http-cache-headers-with-php
$tsstring = gmdate('D, d M Y H:i:s ', filemtime($file)) . 'GMT';
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false;
if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) &&
($if_modified_since && $if_modified_since == $tsstring))
{
header('HTTP/1.1 304 Not Modified');
exit();
}
else
{
header("Last-Modified: $tsstring");
header("ETag: \"{$etag}\"");
header("Pragma: cache");
header_remove("Expires");
header_remove("Cache-control");
}
}
else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
$file = $container->parameters['staticDir'] . '/images/chyba404.png';
}
header("Content-Type: image/png");
readfile($file);
/* HELPER
$files = \Nette\Utils\Finder::findFiles("*.png")->from('data/img/');
foreach ($files as $file => $info) {
preg_match('~^(\d+)-(\d+)~', $info->getFilename(), $m);
rename($file, "data/img/$m[0].png");
}
*/