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