-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
298 lines (258 loc) · 8.92 KB
/
functions.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
<?php
//
// SPDX-License-Identifier: BSD-3-Clause
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !! !!
// !! NOTE - this file does not need to be edited; see setup.php !!
// !! !!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
//////////////////////////////////////////////////////////////////////////////
// Security
//
// Two functions contain filesystem calls (search for "FS" in this
// file):
//
// directory_data()
// cache_test()
//
// function path_security_check(string $victim, string $test)
//
// the resolved path of $victim must be below $test on the filesystem
// check only if victim exists; otherwise realpath cannot resolve path
//
function path_security_check($victim, $test) {
if (!realpath($victim) ||
preg_match("/^" . rtrim('/', $test) . ".*/", rtrim('/', realpath($victim)))) {
return true;
}
die("path security check failed: $victim - $test");
} // function path_security_check
// function parse_resolution(string $res)
//
// converts a string dimension (800x600, 800X600, 800 x 600, 800 X 600)
// to a two-element array
//
function parse_resolution($res) {
//return(explode('x', ereg_replace('[^0-9x]', '', strtolower($res))));
return(explode('x', preg_replace('/[^0-9x]/', '', strtolower($res))));
} // function parse_resolution
// function cache_test(string $url, int $x, int $y) {
//
// creates the file's cache path and tests for existance in the cache
// returns:
// array(
// is_cached,
// name,
// path,
// cache_url
// )
//
function cache_test($url, $x, $y) {
global $CFG_cache_enable, $CFG_path_cache, $CFG_url_cache;
// cache paths and URL references to images must be URL and filesystem safe
// pure urlencoding would require double-urlencoding image URLs -- confusing
// instead replace %2f (/) with ! and % with $ (!, $ are URL safe) for readability and consistency between two versions
//
preg_match("/(.*)(\.[A-Za-z0-9]+)$/", $url, $matches);
$result = array();
$result['is_cached'] = false;
$result['name'] = str_replace('%', '$', str_replace('%2F', '!', urlencode($matches[1]))) . '_' . $x . 'x' . $y . $matches[2];
$result['path'] = $CFG_path_cache . '/' . $result['name'];
$result['cache_url'] = $CFG_url_cache . '/' . $result['name'];
path_security_check($result['path'], $CFG_path_cache);
if ($CFG_cache_enable && is_file($result['path']) && is_readable($result['path'])) { // FS READ
$result['is_cached'] = true;
}
return $result;
} // function cache_test
// function directory_data(string $path, string $url_path)
//
// walks the specified directory and returns an array containing image file
// and directory details:
//
// array(
// files => array(
// name,
// index,
// path,
// thumb_url,
// image_url,
// view_url,
// raw_url
// ),
// directories => array(
// name,
// index,
// list_url
// )
// )
//
// note: only files with extensions matching $CFG_image_valid are included
// '.' and '..' are not referenced in the directory array
//
function directory_data($path, $url_path) {
global $CFG_image_valid, $CFG_url_album, $CFG_thumb_width, $CFG_thumb_height, $CFG_image_width, $CFG_image_height, $CFG_path_images, $CFG_cache_outside_docroot;
//compensate for switching away from eregi()
$CFG_image_valid_i = array();
foreach ($CFG_image_valid as $e) {
$CFG_image_valid_i[] = $e;
$CFG_image_valid_i[] = strtoupper($e);
}
// put CFG_image_valid array into eregi() form
$valid_extensions = '(.' . implode('|.', $CFG_image_valid_i) . ')$';
path_security_check($path, $CFG_path_images);
// load raw directory first, sort, and reprocess
//
$files_raw = array();
$dirs_raw = array();
if ($h_dir = opendir($path)) { // FS READ
while (false !== ($filename = readdir($h_dir))) { // FS READ
if ($filename != '.' && $filename != '..') {
// set complete url
//
if ($url_path == '') {
$url = $filename;
} else {
$url = "$url_path/$filename";
}
path_security_check("$path/$filename", $CFG_path_images);
if (is_readable("$path/$filename") && // FS READ
is_file("$path/$filename") && // FS READ
preg_match("/{$valid_extensions}/", $filename)) {
$files_raw[] = array('name' => $filename,
'url' => $url);
} else if (is_readable("$path/$filename") &&
is_dir("$path/$filename") &&
substr($filename, 0, 1) != '_') { // FS READ
$dirs_raw[] = array('name' => $filename,
'url' => $url);
} // if ... else is_file or is_dir
} // if
} // while
closedir($h_dir); // FS READ
} // if opendir
// sort directory arrays by filename
//
function cmp($a, $b) {
return strcasecmp($a['name'], $b['name']);
} // function cmp
@usort($dirs_raw, 'cmp');
@usort($files_raw, 'cmp');
// reprocess arrays
//
$files = array();
$dirs = array();
$file_count = 0;
$dir_count = 0;
foreach ($files_raw as $k => $v) {
// set thumbnail cached vs. not
//
$thumb = cache_test($v['url'], $CFG_thumb_width, $CFG_thumb_height); // FS FUNCTION
$image = cache_test($v['url'], $CFG_image_width, $CFG_image_height); // FS FUNCTION
if ($CFG_cache_outside_docroot || !$thumb['is_cached']) {
$thumb_url = build_url('image', $CFG_thumb_width . 'x' . $CFG_thumb_height, $v['url']);
} else {
$thumb_url = $thumb['cache_url'];
}
if ($CFG_cache_outside_docroot || !$image['is_cached']) {
$image_url = build_url('image', $CFG_image_width . 'x' . $CFG_image_height, $v['url']);
} else {
$image_url = $image['cache_url'];
}
path_security_check("$path/$v[name]", $CFG_path_images);
$files[] = array('name' => $v['name'],
'index' => $file_count,
'path' => "$path/$v[name]",
'thumb_url' => $thumb_url,
'image_url' => $image_url,
'view_url' => build_url('view', $file_count, $v['url']),
'raw_url' => build_url('image', '0', $v['url'])); // 0 index for raw image
$file_count++;
}
foreach ($dirs_raw as $k => $v) {
$dirs[] = array('name' => $v['name'],
'index' => $dir_count,
'list_url' => build_url('list', '0', $v['url']));
$dir_count++;
}
return(array('files' => $files, 'directories' => $dirs));
} // function directory_data
// function path_list(string $path)
//
// return list of path parts and URLs in an array:
//
// array(
// url,
// name
// )
//
function path_list($path) {
global $CFG_url_album, $CFG_album_name;
$image_subdir_parts = array();
if ($path != '') {
$image_subdir_parts = explode('/', $path);
}
$path_list[] = array('url' => $CFG_url_album,
'name' => $CFG_album_name);
for ($i = 0; $i < count($image_subdir_parts); $i++) {
$k = key($image_subdir_parts);
$v = current($image_subdir_parts);
$path_list[] = array('url' => build_url('list', '0', implode('/', array_slice($image_subdir_parts, 0, $i + 1))),
'name' => $image_subdir_parts[$i]);
} // for
return $path_list;
} // function path_data
// function debug(string $type[, string $message])
//
// sets an entry in global DEBUG_MESSAGES
//
function debug($type, $message = '') {
global $DEBUG_MESSAGES;
if ($message == '') {
$message = $type;
$type = 'debug';
} // if
if (is_array($message) || is_object($message)) {
ob_start();
var_dump($message);
$message = ob_get_contents();
ob_end_clean();
} // if
$DEBUG_MESSAGES[] = "[$type]: $message";
} // function debug
// return a URL string based on view, index, path components and CFG vars
//
function build_url($view, $index, $path) {
global $CFG_variable_mode, $CFG_url_album;
if ($CFG_variable_mode == 'path') {
return("$CFG_url_album/$view/$index/" . str_replace('%2F', '/', urlencode($path)));
} else {
return("$CFG_url_album?v=$view&i=$index&p=" . str_replace('%2F', '/', urlencode($path)));
}
} // function build_url
// function resize($x, $y)
// calculates resized image based on image x1,y1 and bounding box x2,y2
// three modes: constant X, constant Y, full bounding box
// returns array(x, y)
//
function calculate_resize($x1, $y1, $x2, $y2) {
global $CFG_resize_mode;
switch ($CFG_resize_mode) {
case 'X':
(int)$resize_x = $x2;
(int)$resize_y = round(($y1 * $x2)/$x1);
break;
case 'Y':
(int)$resize_x = round(($x1 * $y2)/$y1);
(int)$resize_y = $y2;
break;
default:
(int)$resize_x = ($x1 <= $y1) ? round(($x1 * $y2)/$y1) : $x2;
(int)$resize_y = ($x1 > $y1) ? round(($y1 * $x2)/$x1) : $y2;
break;
}
return array($resize_x, $resize_y);
} // calculate_resize
?>