-
Notifications
You must be signed in to change notification settings - Fork 7
/
getPhoto.php
169 lines (136 loc) · 3.78 KB
/
getPhoto.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
<?php
namespace Lychee;
# must be one of the following value:
# * auto (automatically choose the best option)
# * php (use php readfile() function)
# * nginx (use nginx xsendfile header)
# * apache (use apache xsendfile header)
$sendFileMethod = 'php';
$uploadsPath = __DIR__ . '/../../uploads';
# ----------------- End of configuration -------------------------
# -----------------------------------------------------------------
$uploadsPath = realpath($uploadsPath);
if(empty($_GET['type']) || empty($_GET['url']))
{
exit('Error: Unable to find your photo.');
}
$type = $_GET['type'];
$url = $_GET['url'];
# Usual startup (taken from php/index.php file)
use Lychee\Modules\Album;
use Lychee\Modules\Config;
use Lychee\Modules\Settings;
use Lychee\Modules\Database;
require(__DIR__ . '/../../php/define.php');
require(__DIR__ . '/../../php/autoload.php');
session_start();
date_default_timezone_set('UTC');
if (Config::exists()===false) {
exit('Error: no config file.');
}
$isAdmin = false;
if ((isset($_SESSION['login'])&&$_SESSION['login']===true)&&
(isset($_SESSION['identifier'])&&$_SESSION['identifier']===Settings::get()['identifier'])) {
$isAdmin = true;
}
# end of startup
$database = Database::get();
# return the photo if the current user has acces to it or false otherwise (taken from php/module/photo.php)
function getPhoto($database, $type, $photoUrl, $isAdmin)
{
$retinaSuffix = '@2x';
$urlParts = explode('.', $photoUrl);
$dbUrl = $photoUrl;
# If the filename ends in $retinaSuffix, remove it for the database query
if (substr_compare($urlParts[0], $retinaSuffix, strlen($urlParts[0])-strlen($retinaSuffix), strlen($retinaSuffix)) === 0) {
$dbUrl = substr($urlParts[0], 0, -strlen($retinaSuffix)) . '.' . $urlParts[1];
}
# Get photo
if($type == 'thumb')
{
$query = Database::prepare(
$database,
"SELECT * FROM ? WHERE thumbUrl = '?' LIMIT 1",
array(
LYCHEE_TABLE_PHOTOS,
$dbUrl
)
);
}
else {
$query = Database::prepare(
$database,
"SELECT * FROM ? WHERE url = '?' LIMIT 1",
array(
LYCHEE_TABLE_PHOTOS,
$dbUrl
)
);
}
$photos = Database::execute($database, $query, __METHOD__, __LINE__);
$photo = $photos->fetch_object();
if ($photo === null) {
http_response_code(404);
exit('Photo not found');
}
# Check if public
if ($isAdmin=== true||$photo->public==='1') {
# Photo public
return $photo;
} else {
# Check if album public
$album = new Album($photo->album);
$agP = $album->getPublic();
if ($agP===true) return $photo;
}
# Photo private
return false;
}
# get the photo
$photo = getPhoto($database, $type, $url, $isAdmin);
# Check the permission
if($photo === false)
{
exit('Error: You do not have access to this photo.');
}
# Check the type of the request
if( ! in_array($type, array('big', 'import', 'medium', 'small', 'thumb')))
{
exit('Error: type not recognized');
}
# check if the file exists
$filepath = $uploadsPath . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $url;
if( ! file_exists($filepath))
{
Log::error($database, 'plugin::webroot', __LINE__, 'The file does not exists: '.$filepath);
exit('Error: the file does not exists (see the log).');
}
# set the content-type
header('Content-type: '.$photo->type);
# send the file with the appropriate methode
if($sendFileMethod == 'auto')
{
$sendFileMethod = 'php';
if(strstr($_SERVER["SERVER_SOFTWARE"], 'nginx'))
{
$sendFileMethod = 'nginx';
}
elseif(strstr($_SERVER["SERVER_SOFTWARE"], 'Apache'))
{
$sendFileMethod = 'apache';
}
}
switch($sendFileMethod)
{
default:
case 'php':
readfile(__DIR__ . '/../../uploads/'.$type.'/'.$url);
break;
case 'nginx':
$fileurl = '/protected-uploads/' . $type . '/' . $url;
header('X-Accel-Redirect: ' . $fileurl);
break;
case 'apache':
header('X-Sendfile: '. $filepath);
break;
}