comparison mgallery.inc.php @ 324:d598b2320878

Improvements and fixes to configuration handling.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 09 Apr 2020 13:09:01 +0300
parents 2f4e3e458714
children 782c1520984e
comparison
equal deleted inserted replaced
323:23625c53e62d 324:d598b2320878
65 65
66 "clean_urls" => [MG_BOOL, FALSE], 66 "clean_urls" => [MG_BOOL, FALSE],
67 "base_path" => [MG_STR, mgPathName(mgRealPath($_SERVER["SCRIPT_FILENAME"]))], 67 "base_path" => [MG_STR, mgPathName(mgRealPath($_SERVER["SCRIPT_FILENAME"]))],
68 "base_url" => [MG_STR, mgPathName($_SERVER["PHP_SELF"])], 68 "base_url" => [MG_STR, mgPathName($_SERVER["PHP_SELF"])],
69 "image_url" => [MG_STR, mgPathName($_SERVER["PHP_SELF"])], 69 "image_url" => [MG_STR, mgPathName($_SERVER["PHP_SELF"])],
70 "mgallery_php" => [MG_STR, "mgallery.php"], 70 "mgallery_php" => [MG_STR, ""],
71 "format_exts" => [MG_STR, "\.jpg|\.png|\.gif|\.jpeg|\.webp"], 71 "format_exts" => [MG_STR, "\.jpg|\.png|\.gif|\.jpeg|\.webp"],
72 "captions_file" => [MG_STR, "captions.txt"], 72 "captions_file" => [MG_STR, "captions.txt"],
73 "header_file" => [MG_STR, "header.txt"], 73 "header_file" => [MG_STR, "header.txt"],
74 "info_file" => [MG_STR, "mgallery.info"], 74 "info_file" => [MG_STR, "mgallery.info"],
75 "cache_file" => [MG_STR, ".mgallery.cache"], 75 "cache_file" => [MG_STR, ".mgallery.cache"],
76 76
77 "cover_images" => [MG_BOOL, TRUE], 77 "cover_images" => [MG_BOOL, TRUE],
78 "album_icon" => [MG_STR, "album_sm.png"], 78 "album_icon" => [MG_STR, NULL],
79 79
80 "title_prefix" => [MG_STR, ""], 80 "title_prefix" => [MG_STR, ""],
81 "title_sep" => [MG_STR, " - "], 81 "title_sep" => [MG_STR, " - "],
82 "page_info" => [MG_STR, "Powered by <b>MGallery ".$mgProgVersion."</b> &copy; Copyright ".$mgProgCopyright], 82 "page_info" => [MG_STR, "Powered by <b>MGallery ".$mgProgVersion."</b> &copy; Copyright ".$mgProgCopyright],
83 83
96 "tn_width" => [MG_INT, 140], // In pixels, applies as bounding box for w/h 96 "tn_width" => [MG_INT, 140], // In pixels, applies as bounding box for w/h
97 "tn_height" => [MG_INT, 100], 97 "tn_height" => [MG_INT, 100],
98 "tn_quality" => [MG_INT, 90], // JPEG/WEBP quality percent 98 "tn_quality" => [MG_INT, 90], // JPEG/WEBP quality percent
99 99
100 "med_format" => [MG_STR_LC, "jpeg"], 100 "med_format" => [MG_STR_LC, "jpeg"],
101 "med_width" => [MG_INT, 960], 101 "med_width" => [MG_INT, 1200],
102 "med_height" => [MG_INT, 640], 102 "med_height" => [MG_INT, 900],
103 "med_quality" => [MG_INT, 90], 103 "med_quality" => [MG_INT, 90],
104 104
105 "backend" => [MG_STR_LC, "php"], 105 "backend" => [MG_STR_LC, "php"],
106 "sql_db" => [MG_STR, NULL], 106 "sql_db" => [MG_STR, FALSE],
107 "sql_username" => [MG_STR, ""], 107 "sql_username" => [MG_STR, ""],
108 "sql_password" => [MG_STR, ""], 108 "sql_password" => [MG_STR, ""],
109 "sql_options" => [MG_STR_ARRAY, []], 109 "sql_options" => [MG_STR_ARRAY, []],
110 ]; 110 ];
111 111
232 $val = mgGetSetting($key); 232 $val = mgGetSetting($key);
233 return ($val !== FALSE) ? $path."/".$val : FALSE; 233 return ($val !== FALSE) ? $path."/".$val : FALSE;
234 } 234 }
235 235
236 236
237 function mgReadSettings(&$spaths) 237 function mgReadOneConfig(&$searchPaths, $pathList)
238 { 238 {
239 global $mgSettings, $mgDefaults; 239 global $mgSettings, $mgProgConfigFile;
240 240 $found = FALSE;
241
242 foreach (array_unique($pathList) as $path)
243 {
244 $filename = $path.$mgProgConfigFile;
245 $searchPaths[] = $filename;
246
247 if (!$found)
248 {
249 mgDebug("Checking '".$filename."' for configuration ..\n");
250
251 if (file_exists($filename) &&
252 ($data = parse_ini_file($filename, FALSE)) !== FALSE)
253 {
254 mgDebug("Found '".$filename."' config.\n");
255 foreach ($data as $dkey => &$dval)
256 $mgSettings[$dkey] = $dval;
257
258 $found = TRUE;
259 }
260 }
261 }
262
263 return $found;
264 }
265
266
267 function mgReadSettings(&$searchPaths)
268 {
269 global $mgSettings;
270 $mgSettings = [];
271 $searchPaths = [];
272
273 // System-wide
274 $ok = mgReadOneConfig($searchPaths, ["/etc/", "/usr/local/etc/"]);
275
276 // User-specific
241 $spaths = []; 277 $spaths = [];
242 $spaths[] = getcwd()."/";
243 if (($tmp = getenv("HOME")) !== FALSE && strlen($tmp) > 0) 278 if (($tmp = getenv("HOME")) !== FALSE && strlen($tmp) > 0)
244 { 279 {
245 $spaths[] = $tmp."/.config/mgallery/"; 280 $spaths[] = $tmp."/.config/mgallery/";
246 $spaths[] = $tmp."/."; 281 $spaths[] = $tmp."/.";
247 } 282 }
253 $tmp = $data["dir"]; 288 $tmp = $data["dir"];
254 $spaths[] = $tmp."/.config/mgallery/"; 289 $spaths[] = $tmp."/.config/mgallery/";
255 $spaths[] = $tmp."/."; 290 $spaths[] = $tmp."/.";
256 } 291 }
257 } 292 }
258 $spaths[] = dirname(__FILE__)."/"; 293 $ok |= mgReadOneConfig($searchPaths, $spaths);
259 294
260 $spaths = array_map(function ($path) 295 // Album/directory-local
261 { 296 $ok |= mgReadOneConfig($searchPaths, [ dirname(__FILE__)."/", getcwd()."/" ]);
262 global $mgProgConfigFile; 297
263 return $path.$mgProgConfigFile; 298 $searchPaths = array_unique($searchPaths);
264 }, $spaths); 299 return $ok;
265
266 foreach (array_unique($spaths) as $file)
267 {
268 mgDebug("Checking '".$file."' for configuration ..\n");
269 if (file_exists($file) &&
270 ($mgSettings = parse_ini_file($file, FALSE)) !== FALSE)
271 {
272 mgDebug("Found '".$file."' config.\n");
273 return TRUE;
274 }
275 }
276
277 $mgSettings = [];
278 return FALSE;
279 } 300 }
280 301
281 302
282 function mgRealPath($path) 303 function mgRealPath($path)
283 { 304 {