-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patha.php
executable file
·131 lines (100 loc) · 3.63 KB
/
a.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
<?php
require __DIR__ . '/bootstrap.php';
date_default_timezone_set('UTC');
ini_set('max_execution_time', 600);
ini_set('display_errors', 1);
error_reporting(1);
set_time_limit(600);
$root = dirname(__FILE__);
@include $root . '/storage/configuration/user_setup.php';
require_once $root . '/app/koken/Shutter/Shutter.php';
require_once $root . '/app/koken/Utils/KokenAPI.php';
require $root . '/app/application/libraries/phpzip/autoload.php';
if (!defined('LOOPBACK_HOST_HEADER')) {
define('LOOPBACK_HOST_HEADER', false);
}
Shutter::enable();
Shutter::hook('image.boot');
if (isset($_GET['path'])) {
$path = $_GET['path'];
} else if (isset($_SERVER['QUERY_STRING'])) {
$path = urldecode($_SERVER['QUERY_STRING']);
} else if (isset($_SERVER['PATH_INFO'])) {
$path = $_SERVER['PATH_INFO'];
} else if (isset($_SERVER['REQUEST_URI'])) {
$path = preg_replace('/.*\/a.php/', '', $_SERVER['REQUEST_URI']);
}
$ds = DIRECTORY_SEPARATOR;
// File exists, but rewites not supported
if (file_exists($root . '/storage/cache/albums' . $path)) {
$file = $root . '/storage/cache/albums' . $path;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
$disabled_functions = explode(',', ini_get('disable_functions'));
if (is_callable('readfile') && !in_array('readfile', $disabled_functions)) {
readfile($file);
exit;
} else {
die(file_get_contents($file));
}
}
preg_match('/^\/([0-9]{3}\/[0-9]{3})\/[0-9]+\/.+$/i', $path, $matches);
if (empty($matches)) {
// Bad request
header('HTTP/1.1 403 Forbidden');
exit;
}
$id = (int)str_replace('/', '', $matches[1]);
$KokenAPI = new KokenAPI;
$result = $KokenAPI->get('/albums/' . $id . '/content/limit:500');
if (!isset($result['album']) || $result['album']['album_type'] === 'set') {
header('HTTP/1.1 403 Forbidden');
exit;
}
$visibility_values = array('public', 'unlisted', 'private');
$album_visibility = array_search($result['album']['visibility']['raw'], $visibility_values);
$ts = $result['album']['modified_on']['timestamp'];
$album_cache_key = 'albums/' . $matches[1];
$cache_key = $album_cache_key . '/' . $ts . '/' . basename($path);
Shutter::clear_cache($album_cache_key);
$zip = new \PHPZip\Zip\File\Zip();
// Create an empty file
Shutter::write_cache($cache_key, '');
$zip->setZipFile($root . '/storage/cache/' . $cache_key);
foreach ($result['content'] as $content) {
$md = $content['max_download']['raw'];
$visibility = array_search($content['visibility']['raw'], $visibility_values);
if ($md !== 'none' && $visibility <= $album_visibility) {
if ($md === 'original') {
$url = $root . $content['original']['relative_url'];
} else {
$url = $content['presets'][$md]['url'];
$relative_url = preg_replace('/.+?(?=\/storage\/cache\/images\/)/', $root, $url);
if (file_exists($relative_url)) {
$url = $relative_url;
}
}
$file = false;
for ($i = 1; $i <= 10; $i++) {
$file = file_get_contents($url);
if ($file) {
break;
} else {
sleep(1);
}
}
if ($file) {
$zip->addFile($file, $content['filename']);
}
}
}
if ($zip->getArchiveSize() !== 0) {
$zip->sendZip(basename($path));
} else {
Shutter::clear_cache($album_cache_key);
}