forked from vladamatena/gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
476 lines (390 loc) · 12.2 KB
/
api.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
include "config.defaults.php";
include "config.php";
date_default_timezone_set('UTC');
$function = "undefined";
if(isset($_GET['fn']))
$function = $_GET['fn'];
// Check user is logged in
if($function != "login-challenge" && $function != "login-response")
validate() or exit();
// Execute requested function
switch($function) {
case "ls":
if(isset($_GET['folder']))
ls($_GET['folder'], $_GET['subdirs'] == "true");
else
ls("", $_GET['subdirs'] == "true");
break;
case "thumb":
get("small", $_GET['img']);
break;
case "web":
get("web", $_GET['img']);
break;
case "img":
get("src", $_GET['img']);
break;
case "video":
get("video", $_GET['img']);
break;
case "info":
info($_GET['img']);
break;
case "update":
update();
break;
case "session":
echo("logged");
break;
case "login-challenge":
challenge();
break;
case "login-response":
login($_GET['response']);
break;
case "logout":
logout();
break;
case "config":
config();
break;
case "speedtest":
speedtest($_GET['size']);
break;
default:
apiError("Bad command: $function");
}
function validate() {
GLOBAL $gallery;
// If not password protected return true
if(strlen($gallery['password']) == 0)
return true;
$sql = connect();
// get ticket from cookie
if(!isset($_COOKIE['ticket']))
return false;
$ticket = $sql->escape_string($_COOKIE['ticket']);
// validate ticket
$result = $sql->query("SELECT * FROM ticket WHERE ticket='$ticket'") or die(my_error());
if($result->num_rows == 1)
{
// update time
$sql->query("UPDATE ticket SET time=CURRENT_TIMESTAMP WHERE ticket='$ticket'") or die(my_error());
return true;
}
return false;
}
function challenge() {
$sql = connect();
// gen challenge data
$challenge = $sql->escape_string(rand());
$address = $_SERVER['REMOTE_ADDR'];
// wipe old challenges for address
$sql->query("DELETE FROM challenge WHERE address='$address'");
// store challenge to database
$sql->query("INSERT INTO challenge ( challenge, address ) VALUES ( '$challenge', '$address' )");
echo $challenge;
}
function login($response) {
GLOBAL $gallery;
$sql = connect();
$address = $_SERVER['REMOTE_ADDR'];
// get salt from database
$salt = "";
$result = $sql->query("SELECT challenge FROM challenge WHERE address='$address'");
if(($result) && ($result->num_rows > 0)) {
$challenge = $result->fetch_assoc();
$salt = $challenge['challenge'];
} else return false;
// remove all challenges for address
$sql->query("DELETE FROM challenge WHERE address='$address'");
$plaintext = $salt . $gallery['password'];
$cyphertext = hash("sha512", $plaintext);
// check response and add ticket
if($response == $cyphertext) {
// prepare ticket
$ticket = rand();
$user = 0;
// add ticket to database and cookies
$sql->query("INSERT INTO ticket ( ticket, user, address ) VALUES ( '$ticket', '$user', '$address' )") or die(my_error());
setcookie('ticket', $ticket, time()+60*60*24*365*10);
return true;
} else {
return false;
}
}
function logout() {
if(!isset($_COOKIE['ticket'])) {
return;
} else {
$sql = connect();
$ticket = $sql->escape_string($_COOKIE['ticket']);
setcookie('ticket','');
$sql->query("DELETE FROM ticket WHERE ticket='$ticket'");
}
}
function connect() {
GLOBAL $gallery;
return new mysqli("p:" . $gallery['sql_host'], $gallery['sql_user'], $gallery['sql_pass'], $gallery['sql_db']);
}
function apiError($message) {
echo "API error: $message";
exit();
}
function isFileImage($name) {
$is_jpg = preg_match('/\.[jJ][pP][eE]?[gG]$/', $name);
$is_gif = preg_match('/\.[gG][iI][fF]$/', $name);
$is_png = preg_match('/\.[pP][nN][gG]$/', $name);
return $is_jpg || $is_gif || $is_png;
}
function isFileVideo($name) {
$is_mp4 = preg_match('/\.[mM][pP][4]$/', $name);
$is_mkv = preg_match('/\.[mM][kK][vV]$/', $name);
$is_webm = preg_match('/\.[wW][eE][bB][mM]$/', $name);
return $is_mp4 || $is_mkv || $is_webm;
}
function listFolder($folder, $list_subdirs) {
GLOBAL $gallery;
// Check for ".." in folder name
if(preg_match('/\.\./', $folder))
apiError("Invalid chars in folder name: $folder");
// Get folder content
$path = $gallery['root'] . "/$folder";
$items = array();
if(file_exists($path) && $handle = opendir($path)) {
while(false !== ($entry = readdir($handle))) {
if($entry != "." && $entry != ".." && !preg_match('/^\..*/', $entry)) {
// Add new item
$item = array();
$item['name'] = $entry;
if(is_dir("$path/$entry")) {
$item['type'] = "directory";
if($list_subdirs){
$item['subdirs'] = listFolder($entry, false);
}
}
else if(isFileImage($entry))
$item['type'] = "image";
else if(isFileVideo($entry))
$item['type'] = "video";
else continue;
if($folder == "")
$item['path'] = $entry;
else
$item['path'] = "$folder/$entry";
$items[] = $item;
}
}
closedir($handle);
}
return $items;
}
function ls($folder, $subdirs) {
header('Content-type: application/json');
echo(json_encode(listFolder($folder, $subdirs)));
}
function api_makeSmall($image) {
GLOBAL $gallery;
$src = $gallery['root'] . "/" . $image;
$hash = hash("sha1", "small/" . $image);
$hashDir = $gallery['scaled'] . "/" . substr($hash, 0, 2);
// Ensure hashdir
if(!is_dir($hashDir))
mkdir($hashDir);
$small = $hashDir . "/" . $hash;
// Ensure image
if(!is_file($small) || filemtime($src) > filemtime($small) || filesize($small) == 0) {
if(isFileImage($src))
system('convert -thumbnail 128x128 "' . $src . '" "' . $small . '"');
if(isFileVideo($src))
system('ffmpegthumbnailer -s 128 -c jpg -i"' . $src . '" -o "' . $small . '"');
touch($small);
}
return $small;
}
function api_makeWeb($image) {
GLOBAL $gallery;
$src = $gallery['root'] . "/" . $image;
$hash = hash("sha1", "web/" . $image);
$hashDir = $gallery['scaled'] . "/" . substr($hash, 0, 2);
// Ensure hashdir
if(!is_dir($hashDir))
mkdir($hashDir);
$web = $hashDir . "/" . $hash;
// Ensure image
if(!is_file($web) || filemtime($src) > filemtime($web) || filesize($web) == 0) {
if(isFileImage($src)) {
system('convert "' . $src . '" -resize "1024x768>" -compress JPEG -quality 80 "' . $web . '"');
touch($web);
}
}
return $web;
}
function get($size, $image) {
GLOBAL $gallery;
$src = $gallery['root'] . "/" . $image;
switch($size) {
case "video":
case "src":
$img = $src;
break;
case "small":
$img = api_makeSmall($image);
break;
case "web":
$img = api_makeWeb($image);
break;
default:
apiError("Image size: " . $size . " not uspported");
}
/* if($size == "video")
header("Content-type: video/mp4");
else
header("Content-type: image/jpg"); */
header("Cache-Control: public");
header("Cache-Control: max-age=86400");
sendFile($img);
}
function info($image) {
GLOBAL $gallery;
$src = $gallery['root'] . "/" . $image;
// Grather image informations
$exif = exif_read_data($src, 'EXIF');
$info = array();
if(isset($exif['DateTimeOriginal'])) $info['date'] = $exif['DateTimeOriginal'];
if(isset($exif['ExifImageWidth']) && $exif['ExifImageLength']) $info['dimensions'] = $exif['ExifImageWidth'] . "x" . $exif['ExifImageLength'];
$info['size'] = formatBytes(filesize($src));
if(isset($exif['Make']) && isset($exif['Model'])) $info['model'] = $exif['Make'] . "(" . $exif['Model'] . ")";
if(isset($exif['ExposureTime'])) $info['exposure'] = $exif['ExposureTime'];
if(isset($exif['FNumber'])) $info['fnumber'] = $exif['FNumber'];
if(isset($exif['ISOSpeedRatings'])) $info['iso'] = $exif['ISOSpeedRatings'];
if(isset($exif['ImageDescription']) && strlen($exif['ImageDescription'] > 0)) $info['description'] = $exif['ImageDescription'];
if(isset($exif['Artist'])) $info['artist'] = $exif['Artist'];
header('Content-type: application/json');
header("Cache-Control: public");
header("Cache-Control: max-age=3600");
echo(json_encode($info));
}
function config() {
GLOBAL $gallery_client;
header('Content-type: application/json');
echo(json_encode($gallery_client));
}
function speedtest($size) {
for($i = 0; $i < $size; $i++) {
echo(chr(mt_rand(0, 255)));
}
}
function update() {
if (ob_get_level() == 0)
ob_start();
header('Content-Type: text/html; charset=utf-8');
echo "Generating scaled images...</br>";
function updateDir($path) {
$items = listFolder($path, false);
echo "Processing directory: $path</br>";
echo str_pad('',4096)."\n";
ob_flush();
flush();
foreach($items as $item) {
if($item['type'] == "directory") {
if($path == "")
updateDir($item['name']);
else
updateDir($path . "/" . $item['name']);
} else {
echo "Processing file: " . $path . "/" . $item['name'] . "...";
echo str_pad('',4096)."\n";
ob_flush();
flush();
api_makeSmall($path . "/" . $item['name']);
api_makeWeb($path . "/" . $item['name']);
echo "done</br>";
echo str_pad('',4096)."\n";
ob_flush();
flush();
}
}
}
updateDir("");
ob_end_flush();
}
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
function sendFile($file) {
$fp = @fopen($file, 'rb');
if(!file_exists($file) | !@fp) {
header("HTTP/1.0 404 Not Found");
exit();
}
header('Content-Type: ' . mime_content_type($file));
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
$size = filesize($file); // File size
$length = $size; // Content length
$start = 0; // Start byte
$end = $size - 1; // End byte
header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {
$c_start = $start;
$c_end = $end;
// Extract the range string
list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
// Make sure the client hasn't sent us a multibyte range
if (strpos($range, ',') !== false) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
// If the range starts with an '-' we start from the beginning
// If not, we forward the file pointer
// And make sure to get the end byte if spesified
if ($range{0} == '-') {
// The n-number of the last bytes is requested
$c_start = $size - substr($range, 1);
} else {
$range = explode('-', $range);
$c_start = $range[0];
$c_end = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
}
/* Check the range and make sure it's treated according to the specs.
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
*/
// End bytes can not be larger than $end.
$c_end = ($c_end > $end) ? $end : $c_end;
// Validate the requested range and return an error if it's not correct.
if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes $start-$end/$size");
exit;
}
$start = $c_start;
$end = $c_end;
$length = $end - $start + 1; // Calculate new content length
fseek($fp, $start);
header('HTTP/1.1 206 Partial Content');
}
// Notify the client the byte range we'll be outputting
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: $length");
// Start buffered download
$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {
if ($p + $buffer > $end) {
// In case we're only outputtin a chunk, make sure we don't
// read past the length
$buffer = $end - $p + 1;
}
set_time_limit(0); // Reset time limit for big files
echo fread($fp, $buffer);
flush(); // Free up memory. Otherwise large files will trigger PHP's memory limit.
}
fclose($fp);
}
?>