Mercurial > hg > mgallery
annotate mgallery.inc.php @ 124:eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
backward/forward compatibility.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 31 Jul 2017 14:48:09 +0300 |
parents | 928852711534 |
children | 3ee66c432081 |
rev | line source |
---|---|
0 | 1 <?php |
2 // | |
3 // Yet Another Image Gallery | |
69
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
4 // -- Common functions and data include |
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
59
diff
changeset
|
5 // Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
111 | 6 // (C) Copyright 2015-2017 Tecnic Software productions (TNSP) |
0 | 7 // |
8 | |
121 | 9 $mgProgVersion = "v0.8.5"; |
0 | 10 $mgProgInfo = "Programmed by Matti 'ccr' Hamalainen"; |
11 $mgProgEmail = "<ccr@tnsp.org>"; | |
111 | 12 $mgProgCopyright = "2015-2017 Tecnic Software productions (TNSP)"; |
0 | 13 |
14 | |
15 // | |
106
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
16 // Navigation control defines |
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
17 // |
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
18 define("MGF_JAVASCRIPT" , 0x01); |
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
19 define("MGF_BREADCRUMBS" , 0x10); |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
20 define("MGF_CAPTIONS" , 0x20); |
106
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
21 |
a3b8cd4183e6
Change {album,image}_navigation setting to *_flags.
Matti Hamalainen <ccr@tnsp.org>
parents:
105
diff
changeset
|
22 // |
0 | 23 // Constants for different value types |
24 // | |
25 define("MG_STR", 1); | |
26 define("MG_INT", 2); | |
27 define("MG_DVA", 3); | |
28 define("MG_BOOL", 4); | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
29 define("MG_FLAGS", 5); |
124
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
121
diff
changeset
|
30 define("MG_DATE", 6); |
0 | 31 |
32 | |
33 define("yes", 1); | |
34 define("no", 0); | |
35 | |
36 | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
37 $mgGFlags = [ |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
38 "javascript" => MGF_JAVASCRIPT, |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
39 "breadcrumbs" => MGF_BREADCRUMBS, |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
40 "captions" => MGF_CAPTIONS, |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
41 ]; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
42 |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
43 |
0 | 44 function mgPathName($path) |
45 { | |
46 $tmp = mgCleanPathArray(TRUE, 0, func_num_args(), func_get_args()); | |
47 if (count($tmp) > 0) | |
48 return implode("/", array_splice($tmp, 0, -1))."/"; | |
49 else | |
50 return $path; | |
51 } | |
52 | |
53 // | |
54 // Configuration settings and their default values | |
55 // | |
56 $mgDefaults = array( | |
57 "base_path" => array(MG_STR, mgPathName(mgRealPath($_SERVER["SCRIPT_FILENAME"]))), | |
58 "base_url" => array(MG_STR, mgPathName($_SERVER["PHP_SELF"])), | |
59 "image_url" => array(MG_STR, mgPathName($_SERVER["PHP_SELF"])), | |
60 "mgallery_php" => array(MG_STR, "mgallery.php"), | |
61 "format_exts" => array(MG_STR, "\.jpg|\.png|\.gif|\.jpeg"), | |
62 "captions_file" => array(MG_STR, "captions.txt"), | |
63 "header_file" => array(MG_STR, "header.txt"), | |
64 "info_file" => array(MG_STR, "gallery.info"), | |
65 "cache_file" => array(MG_STR, ".mgallery.cache"), | |
66 | |
17
3f7bc4202df3
Add new configuration setting 'cover_images' to disable/enable sub-album
Matti Hamalainen <ccr@tnsp.org>
parents:
14
diff
changeset
|
67 "cover_images" => array(MG_BOOL, TRUE), |
19
59075f5e7a62
Add configuration setting to enable and disable use of HTML table elements.
Matti Hamalainen <ccr@tnsp.org>
parents:
17
diff
changeset
|
68 "use_tables" => array(MG_BOOL, TRUE), |
0 | 69 "clean_urls" => array(MG_BOOL, FALSE), |
70 | |
71 "title_prefix" => array(MG_STR, ""), | |
72 "title_sep" => array(MG_STR, " - "), | |
73 "page_info" => array(MG_STR, "<b>MGallery ".$mgProgVersion."</b> © Copyright ".$mgProgCopyright), | |
74 "css_select" => array(MG_BOOL, FALSE), | |
75 "css" => array(MG_STR, NULL), | |
107
71de97240799
Add support for urchin file.
Matti Hamalainen <ccr@tnsp.org>
parents:
106
diff
changeset
|
76 "urchin_file" => array(MG_STR, FALSE), |
0 | 77 "album_icon" => array(MG_STR, "album_sm.png"), |
78 | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
79 "global_flags" => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS | MGF_CAPTIONS, &$mgGFlags), |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
80 "image_flags" => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS, &$mgGFlags), |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
81 "album_flags" => array(MG_FLAGS, MGF_JAVASCRIPT | MGF_BREADCRUMBS, &$mgGFlags), |
0 | 82 "album_row_limit" => array(MG_INT, 5), |
83 | |
84 "med_suffix" => array(MG_STR, ".med"), | |
85 "tn_path" => array(MG_STR, "tn/"), | |
86 | |
87 "tn_width" => array(MG_INT, 140), // In pixels, applies as bounding box for w/h | |
88 "tn_height" => array(MG_INT, 100), | |
95
b8c6e3909972
Adjust the default thumb/mid image size and quality settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
89 "tn_quality" => array(MG_INT, 77), // JPEG quality percent |
0 | 90 |
95
b8c6e3909972
Adjust the default thumb/mid image size and quality settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
91 "med_width" => array(MG_INT, 960), |
b8c6e3909972
Adjust the default thumb/mid image size and quality settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
92 "med_height" => array(MG_INT, 640), |
b8c6e3909972
Adjust the default thumb/mid image size and quality settings.
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
93 "med_quality" => array(MG_INT, 88), |
0 | 94 ); |
95 | |
96 | |
97 function mgDebug($msg) | |
98 { | |
99 // echo "MGAL[debug]: ".$msg; | |
100 } | |
101 | |
102 | |
103 function mgFatal($msg) | |
104 { | |
105 die("MGAL[fatal]: ".$msg); | |
106 } | |
107 | |
108 | |
109 function mgError($msg) | |
110 { | |
111 echo "MGAL[error]: ".$msg; | |
112 return FALSE; | |
113 } | |
114 | |
115 | |
116 function mgCArg($index, $clip = FALSE) | |
117 { | |
118 global $argc, $argv; | |
119 if ($index < $argc) | |
120 { | |
121 $str = $argv[$index]; | |
122 return ($clip !== FALSE) ? substr($str, 0, $clip) : $str; | |
123 } | |
124 else | |
125 return FALSE; | |
126 } | |
127 | |
128 | |
129 function mgCArgLC($index, $clip = FALSE) | |
130 { | |
131 global $argc, $argv; | |
132 if ($index < $argc) | |
133 { | |
134 $str = strtolower($argv[$index]); | |
135 return ($clip !== FALSE) ? substr($str, 0, $clip) : $str; | |
136 } | |
137 else | |
138 return FALSE; | |
139 } | |
140 | |
141 | |
142 function mgGetSetting($key, $default = NULL) | |
143 { | |
144 global $mgSettings, $mgDefaults; | |
145 | |
146 if (!array_key_exists($key, $mgDefaults)) | |
147 mgFatal("Setting '".$key."' does not exist.\n"); | |
148 | |
149 if (array_key_exists($key, $mgSettings)) | |
150 $val = $mgSettings[$key]; | |
151 else | |
152 $val = $mgDefaults[$key][1]; | |
153 | |
154 if (!isset($val) || $val === NULL) | |
155 { | |
156 if ($default !== NULL) | |
157 $val = $default; | |
158 else | |
159 mgFatal("Setting '".$key."' is not set, but is required to be configured.\n"); | |
160 } | |
161 | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
162 switch ($mgDefaults[$key][0]) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
163 { |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
164 case MG_FLAGS: |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
165 if (is_string($val)) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
166 { |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
167 $flags = $mgDefaults[$key][2]; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
168 $cval = preg_split("/\s*[,|]\s*/", strtolower($val), -1, PREG_SPLIT_NO_EMPTY); |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
169 $nval = 0; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
170 foreach ($cval as $qval) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
171 { |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
172 if (array_key_exists($qval, $flags)) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
173 $nval |= $flags[$qval]; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
174 else |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
175 mgFatal("Invalid flag value for '".$key."': '".$qval."'."); |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
176 } |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
177 } |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
178 break; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
179 } |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
180 |
0 | 181 return $val; |
182 } | |
183 | |
184 | |
185 function mgGetPath($path, $key) | |
186 { | |
187 $val = mgGetSetting($key); | |
188 return ($val !== FALSE) ? $path."/".$val : FALSE; | |
189 } | |
190 | |
191 | |
192 function mgReadSettings($filename = "mgallery.cfg") | |
193 { | |
194 global $mgSettings, $mgDefaults; | |
195 | |
196 $spaths = array(); | |
197 $spaths[] = getcwd()."/"; | |
198 if (($tmp = getenv("HOME")) !== FALSE && strlen($tmp) > 0) | |
199 { | |
200 $spaths[] = $tmp."/.config/mgallery/"; | |
201 $spaths[] = $tmp."/."; | |
202 } | |
203 else | |
204 { | |
205 $data = posix_getpwuid(posix_getuid()); | |
206 if ($data !== FALSE && isset($data["dir"])) | |
207 { | |
208 $tmp = $data["dir"]; | |
209 $spaths[] = $tmp."/.config/mgallery/"; | |
210 $spaths[] = $tmp."/."; | |
211 } | |
212 } | |
213 $spaths[] = dirname(__FILE__)."/"; | |
214 | |
215 foreach (array_unique($spaths) as $path) | |
216 { | |
217 $file = $path.$filename; | |
218 mgDebug("Checking '".$file."' for configuration ..\n"); | |
219 if (file_exists($file) && | |
220 ($mgSettings = parse_ini_file($file, FALSE)) !== FALSE) | |
221 { | |
222 mgDebug("Found '".$file."' config.\n"); | |
223 | |
224 // Validate settings | |
225 $ok = TRUE; | |
226 foreach ($mgSettings as $setting => $val) | |
227 { | |
228 if (!array_key_exists($setting, $mgDefaults)) | |
229 { | |
230 mgError("Setting '".$setting."' does not exist.\n"); | |
231 $ok = FALSE; | |
232 } | |
233 } | |
234 return $ok; | |
235 } | |
236 } | |
237 | |
238 $mgSettings = array(); | |
239 return FALSE; | |
240 } | |
241 | |
242 | |
243 function mgRealPath($path) | |
244 { | |
245 return realpath($path); | |
246 } | |
247 | |
248 | |
249 function mgCleanPathArray($refs, $start, $argc, $argv) | |
250 { | |
251 $path = array(); | |
252 $first = TRUE; | |
253 for ($n = $start; $n < $argc; $n++) | |
254 { | |
255 foreach (explode("/", $argv[$n]) as $piece) | |
256 { | |
257 switch ($piece) | |
258 { | |
259 case ".": | |
260 case "": | |
261 if ($first) | |
262 $path[] = $piece; | |
263 break; | |
264 | |
265 case "..": | |
266 if ($refs && count($path) > 0) | |
267 array_pop($path); | |
268 break; | |
269 | |
270 default: | |
271 $path[] = $piece; | |
272 break; | |
273 } | |
274 $first = FALSE; | |
275 } | |
276 } | |
277 return $path; | |
278 } | |
279 | |
280 | |
281 function mgCleanPath($refs) | |
282 { | |
283 return implode("/", mgCleanPathArray($refs, 1, func_num_args(), func_get_args())); | |
284 } | |
285 | |
286 | |
113 | 287 function mgGetTrans($val, $useEntities = FALSE) |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
288 { |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
289 global $pageLang; |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
290 |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
291 if (is_array($val)) |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
292 $str = array_key_exists($pageLang, $val) ? $val[$pageLang] : reset($val); |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
293 else |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
294 $str = $val; |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
295 |
113 | 296 return $useEntities ? chentities($str) : $str; |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
297 } |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
298 |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
299 |
0 | 300 function mgGetArr($data, $skeys, $sfmt1 = "%1", $sfmt2 = "", $func = NULL) |
301 { | |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
302 global $pageLang; |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
303 |
0 | 304 if (!is_array($skeys)) |
305 $skeys = array($skeys); | |
306 | |
307 foreach ($skeys as $skey) | |
308 if (!array_key_exists($skey, $data)) | |
309 return $sfmt2; | |
310 | |
311 $str = $sfmt1; | |
312 for ($i = 1; $i <= sizeof($skeys); $i++) | |
313 { | |
314 $val = $data[$skeys[$i - 1]]; | |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
315 if (is_array($val)) |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
316 $vtmp = array_key_exists($pageLang, $val) ? $val[$pageLang] : reset($val); |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
317 else |
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
318 $vtmp = $val; |
0 | 319 |
320 if (is_callable($func)) | |
77
fcfd635d7abf
Oops, fix format string callback functions ..
Matti Hamalainen <ccr@tnsp.org>
parents:
70
diff
changeset
|
321 $vtmp = call_user_func($func, $vtmp); |
0 | 322 |
47
1e2b55775a84
Begin implementation of translated caption/title support.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
323 $str = str_replace("%".$i, $vtmp, $str); |
0 | 324 } |
325 | |
326 return $str; | |
327 } | |
328 | |
329 | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
330 function mgGetDValStr($mdef, $val) |
0 | 331 { |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
332 switch ($mdef[0]) |
0 | 333 { |
334 case MG_STR : return "\"".$val."\""; | |
335 case MG_BOOL : return $val ? "yes" : "no"; | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
336 case MG_FLAGS: |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
337 { |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
338 $mstr = []; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
339 foreach ($mdef[2] as $vkey => $vval) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
340 { |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
341 if ($val & $vval) |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
342 $mstr[] = $vkey; |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
343 } |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
344 return implode($mstr, " | "); |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
113
diff
changeset
|
345 } |
0 | 346 case MG_INT : |
347 default : return (string) $val; | |
348 } | |
349 } | |
350 | |
351 | |
352 ?> |