0
|
1 #!/usr/bin/php
|
|
2 <?php
|
|
3 //
|
|
4 // Yet Another Image Gallery
|
|
5 // Commandline tool for creating / updating gallery
|
|
6 // (C) Copyright 2015 Tecnic Software productions (TNSP)
|
|
7 //
|
|
8 require_once "mgallery.inc.php";
|
|
9
|
|
10 //
|
|
11 // Array for specifying what will be copied and converted
|
|
12 // from the image file's EXIF information tag(s).
|
|
13 //
|
|
14 $galExifConversions = array(
|
|
15 array(MG_STR, "caption" , "ImageDescription"),
|
|
16 array(MG_STR, "copyright" , "Copyright"),
|
|
17 array(MG_STR, "model" , "Model"),
|
|
18 array(MG_INT, "width" , array("COMPUTED", "Width")),
|
|
19 array(MG_INT, "height" , array("COMPUTED", "Height")),
|
|
20 array(MG_DVA, "fnumber" , "FNumber"),
|
|
21 array(MG_DVA, "exposure" , "ExposureTime"),
|
|
22 array(MG_INT, "iso" , "ISOSpeedRatings"),
|
|
23 array(MG_DVA, "focallength" , "FocalLength"),
|
|
24 array(MG_STR, "datetime" , "DateTimeOriginal"),
|
|
25 array(MG_STR, "datetime" , "DateTimeDigitized"),
|
|
26 array(MG_INT, "filesize" , "FileSize"),
|
|
27 );
|
|
28
|
|
29 define("GCMD_UPDATE" , 1);
|
|
30 define("GCMD_RESCAN" , 2);
|
|
31 define("GCMD_CLEAN" , 3);
|
|
32
|
|
33 define("GCLEAN_CACHES" , 0x01);
|
|
34 define("GCLEAN_THUMBNAILS", 0x02);
|
|
35 define("GCLEAN_ALL" , 0x0f);
|
|
36
|
|
37
|
|
38 define("GUPD_MED_IMAGE" , 0x01);
|
|
39 define("GUPD_TN_IMAGE" , 0x02);
|
|
40 define("GUPD_IMAGES" , 0x0f);
|
|
41 define("GUPD_EXIF_INFO" , 0x10);
|
|
42 define("GUPD_CAPTION" , 0x20);
|
|
43
|
|
44
|
|
45 //
|
|
46 // Convert and scale image file function, for generating
|
|
47 // the intermediate size images and thumbnails. Uses the
|
|
48 // PHP ImageMagick bindings.
|
|
49 //
|
|
50 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb)
|
|
51 {
|
|
52 // Create conversion entity
|
|
53 $img = new Imagick($inFilename);
|
|
54 if ($img === FALSE)
|
|
55 return mgError("ImageMagick could not digest the file '".$inFilename."'.\n");
|
|
56
|
|
57 if ($outDim !== FALSE)
|
|
58 {
|
|
59 // Get dimensions, setup background
|
|
60 $dim = $img->getImageGeometry();
|
|
61 //$img->setImageBackgroundColor(imagick::COLOR_BLACK);
|
|
62 $img->setGravity(imagick::GRAVITY_CENTER);
|
|
63
|
|
64
|
|
65 if ($dim["width"] < $dim["height"])
|
|
66 {
|
|
67 $stmp = $outDim[0];
|
|
68 $outDim[0] = $outDim[1];
|
|
69 $outDim[1] = $stmp;
|
|
70 }
|
|
71
|
|
72 if ($dim["width"] != $outDim[0] || $dim["height"] != $outDim[1])
|
|
73 {
|
|
74 $outDim[1] = ($dim["height"] * $outDim[0]) / $dim["width"];
|
|
75 }
|
|
76
|
|
77 // Act based on image size vs. desired size and $thumb mode
|
|
78 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1])
|
|
79 {
|
|
80 // Image is larger
|
|
81 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1);
|
|
82 $img->setImageExtent($outDim[0], $outDim[1]);
|
|
83 $img->normalizeImage();
|
|
84 $img->unsharpMaskImage(0, 0.5, 1, 0.05);
|
|
85 }
|
|
86 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1])
|
|
87 {
|
|
88 // Image is smaller than requested dimension(s)?
|
|
89 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_QUADRATIC, 1);
|
|
90 $img->setImageExtent($outDim[0], $outDim[1]);
|
|
91 $img->unsharpMaskImage(0, 0.5, 1, 0.05);
|
|
92 }
|
|
93 }
|
|
94
|
|
95 $img->setFormat($outFormat);
|
|
96 $img->setCompressionQuality($outQuality);
|
|
97
|
|
98 $img->stripImage();
|
|
99 $img->writeImage($outFilename);
|
|
100 $img->removeImage();
|
|
101 return TRUE;
|
|
102 }
|
|
103
|
|
104
|
|
105 //
|
|
106 // Converts one value (mainly from EXIF tag information)
|
|
107 // by doing explicing type casting and special conversions.
|
|
108 //
|
|
109 function mgConvertExifData($val, $vtype)
|
|
110 {
|
|
111 switch ($vtype)
|
|
112 {
|
|
113 case MG_STR: return (string) $val;
|
|
114 case MG_INT: return intval($val);
|
|
115 case MG_BOOL: return intval($val);
|
|
116 case MG_DVA:
|
|
117 if (sscanf($val, "%d/%d", $v1, $v2) == 2)
|
|
118 {
|
|
119 if ($v1 < $v2)
|
|
120 return $val;
|
|
121 else
|
|
122 return sprintf("%1.1f", $v1 / $v2);
|
|
123 }
|
|
124 else
|
|
125 return $val;
|
|
126
|
|
127 default:
|
|
128 return $val;
|
|
129 }
|
|
130 }
|
|
131
|
|
132
|
|
133 //
|
|
134 // Conditionally copies one "field" from an associated array/hash to another.
|
|
135 // If destination is already SET, nothing will be done. If source does
|
|
136 // not exist (e.g. one or more of the keys do not exist in source array),
|
|
137 // a default value will be used, if provided.
|
|
138 // Source may have multi-depth keys, destination has one key.
|
|
139 //
|
|
140 function mgCopyEntryData(&$dst, $src, $vtype, $dkey, $skeys, $default = NULL)
|
|
141 {
|
|
142 // Is destination already set?
|
|
143 if (isset($dst[$dkey]))
|
|
144 return FALSE;
|
|
145
|
|
146 // If input key is not array, change it into one
|
|
147 if (!is_array($skeys))
|
|
148 $skeys = array($skeys);
|
|
149
|
|
150 // Traverse input array by using consequent keys
|
|
151 $tmp = &$src;
|
|
152 foreach ($skeys as $skey)
|
|
153 {
|
|
154 if (!array_key_exists($skey, $tmp))
|
|
155 {
|
|
156 // Key didn't exist, try for default
|
|
157 if ($default !== NULL)
|
|
158 $dst[$dkey] = $default;
|
|
159 return FALSE;
|
|
160 }
|
|
161 else
|
|
162 $tmp = &$tmp[$skey];
|
|
163 }
|
|
164
|
|
165 // Optionally convert the input value
|
|
166 $dst[$dkey] = mgConvertExifData($tmp, $vtype);
|
|
167 return TRUE;
|
|
168 }
|
|
169
|
|
170
|
|
171 //
|
|
172 // Attempt to get gallery album data from various sources.
|
|
173 //
|
|
174 function mgGetAlbumData($galBasePath, $galPath)
|
|
175 {
|
|
176 // Check path permissions
|
|
177 $galData = array();
|
|
178 if (is_readable($galPath))
|
|
179 {
|
|
180 // First, try to read gallery/album info file
|
|
181 $filename = mgGetPath($galPath, "info_file");
|
|
182 if ($filename !== FALSE && file_exists($filename))
|
|
183 {
|
|
184 mgDebug("Reading INFOFILE: ".$filename."\n");
|
|
185 if (($galData = parse_ini_file($filename, FALSE)) === FALSE)
|
|
186 $galData = array();
|
|
187 }
|
|
188
|
|
189 // Read header file, if any, and we don't have "header" field set yet
|
|
190 $filename = mgGetPath($galPath, "header_file");
|
|
191 if ($filename !== FALSE && file_exists($filename) &&
|
|
192 !isset($galData["header"]))
|
|
193 {
|
|
194 mgDebug("Reading HEADERFILE: ".$filename."\n");
|
|
195 $galData["header"] = file_get_contents($filename);
|
|
196 }
|
|
197
|
|
198 // Check for alternate key/values for album title/caption
|
|
199 if (isset($galData["title"]) && !isset($galData["caption"]))
|
|
200 {
|
|
201 $galData["caption"] = $galData["title"];
|
|
202 unset($galData["title"]);
|
|
203 }
|
|
204 }
|
|
205 else
|
|
206 $galData["hide"] = TRUE;
|
|
207
|
|
208 // If caption is not set, use last path component for
|
|
209 // a fallback value in case we don't discover proper title
|
|
210 // from other sources we can't check here yet.
|
|
211 $path = explode("/", $galPath);
|
|
212 $galData["fallback_caption"] = ucfirst(str_replace("_", " ", end($path)));
|
|
213
|
|
214 // Last, store the current gallery path
|
|
215 $len = strlen($galBasePath);
|
|
216 if ($len < strlen($galPath) && substr($galPath, 0, $len) == $galBasePath)
|
|
217 $galData["path"] = substr($galPath, $len);
|
|
218 else
|
|
219 $galData["path"] = "";
|
|
220
|
|
221 return $galData;
|
|
222 }
|
|
223
|
|
224
|
|
225 function mgReadCaptionsFile($galBasePath, $galPath)
|
|
226 {
|
|
227 $captions = array();
|
|
228 $filename = mgGetPath($galPath, "captions_file");
|
|
229 if ($filename === FALSE || ($fp = @fopen($filename, "rb")) === FALSE)
|
|
230 return $captions;
|
|
231
|
|
232 mgDebug("Reading CAPTIONS: ".$filename."\n");
|
|
233
|
|
234 // Read and parse data
|
|
235 while (!feof($fp))
|
|
236 {
|
|
237 $str = trim(fgets($fp));
|
|
238 // Ignore comments and empty lines
|
|
239 if ($str != "#" && $str != "")
|
|
240 {
|
|
241 if (preg_match("/^(#?)\s*(\S+?)\s+(.+)$/", $str, $m))
|
|
242 $captions[$m[2]] = array("caption" => $m[3], "hide" => ($m[1] == "#"), "used" => FALSE);
|
|
243 else
|
|
244 if (preg_match("/^(#?)\s*(\S+?)$/", $str, $m))
|
|
245 $captions[$m[2]] = array("hide" => ($m[1] == "#"), "used" => FALSE);
|
|
246 }
|
|
247 }
|
|
248
|
|
249 fclose($fp);
|
|
250 return $captions;
|
|
251 }
|
|
252
|
|
253
|
|
254 function mgMakeDir($path, $perm)
|
|
255 {
|
|
256 if (!file_exists($path))
|
|
257 {
|
|
258 if (mkdir($path, $perm, TRUE) === false)
|
|
259 return mgError("Could not create directory '".$path."'\n");
|
|
260 }
|
|
261 return TRUE;
|
|
262 }
|
|
263
|
|
264
|
|
265 function mgYesNoPrompt($msg, $default = FALSE)
|
|
266 {
|
|
267 echo $msg." [".($default ? "Y/n" : "y/N")."]? ";
|
|
268 $sprompt = strtolower(trim(fgets(STDIN)));
|
|
269
|
|
270 if ($default)
|
|
271 return ($sprompt == "n");
|
|
272 else
|
|
273 return ($sprompt == "y");
|
|
274 }
|
|
275
|
|
276
|
|
277 function mgDelete($path, $recurse)
|
|
278 {
|
|
279 global $flagDoDelete;
|
|
280 if (is_dir($path))
|
|
281 {
|
|
282 if (($dirHandle = @opendir($path)) === FALSE)
|
|
283 return mgError("Could not read directory '".$path."'.\n");
|
|
284
|
|
285 while (($dirFile = @readdir($dirHandle)) !== FALSE)
|
|
286 {
|
|
287 if ($dirFile != "." && $dirFile != "..")
|
|
288 mgDelete($path."/".$dirFile, $recurse);
|
|
289 }
|
|
290 closedir($dirHandle);
|
|
291
|
|
292 echo " - ".$path." [DIR]\n";
|
|
293 if ($flagDoDelete)
|
|
294 rmdir($path);
|
|
295 }
|
|
296 else
|
|
297 {
|
|
298 if (!$recurse)
|
|
299 echo " - ".$path."\n";
|
|
300 if ($flagDoDelete)
|
|
301 unlink($path);
|
|
302 }
|
|
303 }
|
|
304
|
|
305
|
|
306 function mgCheckQuit($now = FALSE)
|
|
307 {
|
|
308 global $flagQuit;
|
|
309
|
|
310 // Dispatch pending signals
|
|
311 pcntl_signal_dispatch();
|
|
312
|
|
313 // Check result
|
|
314 if ($now && $flagQuit)
|
|
315 mgFatal("Quitting.\n");
|
|
316
|
|
317 return $flagQuit;
|
|
318 }
|
|
319
|
|
320
|
|
321 function mgNeedUpdate($entry, $field, $cvalue)
|
|
322 {
|
|
323 if (!array_key_exists($field, $entry))
|
|
324 return TRUE;
|
|
325
|
|
326 return ($entry[$field] < $cvalue);
|
|
327 }
|
|
328
|
|
329
|
|
330 function mgHandleDirectory($mode, $basepath, $path, $parentData, $parentEntry, $writeMode, $startAt)
|
|
331 {
|
|
332 global $galExifConversions, $galTNPath, $galCleanFlags;
|
|
333
|
|
334 // Get cache file path
|
|
335 if (($cacheFilename = mgGetPath($path, "cache_file")) === FALSE)
|
|
336 return mgError("Cache filename / path not set.\n");
|
|
337
|
|
338 mgCheckQuit(TRUE);
|
|
339
|
|
340 // Read directory contents
|
|
341 $entries = array();
|
|
342 if (($dirHandle = @opendir($path)) === FALSE)
|
|
343 return mgError("Could not read directory '".$path."'.\n");
|
|
344
|
|
345 while (($dirFile = @readdir($dirHandle)) !== FALSE)
|
|
346 {
|
|
347 $realFile = $path."/".$dirFile;
|
|
348 if (is_dir($realFile))
|
|
349 {
|
|
350 if ($dirFile[0] != "." && $dirFile != $galTNPath)
|
|
351 $entries[$dirFile] = array("type" => 1, "base" => $dirFile, "ext" => "", "mtime" => filemtime($realFile));
|
|
352 }
|
|
353 else
|
|
354 if (preg_match("/^(\S+)(".mgGetSetting("format_exts").")$/i", $dirFile, $dirMatch))
|
|
355 $entries[$dirFile] = array("type" => 0, "base" => $dirMatch[1], "ext" => $dirMatch[2], "mtime" => filemtime($realFile), "hide" => false);
|
|
356 }
|
|
357 closedir($dirHandle);
|
|
358
|
|
359 mgCheckQuit();
|
|
360
|
|
361 // Cleanup mode
|
|
362 if ($mode == GCMD_CLEAN)
|
|
363 {
|
|
364 $gallery = array();
|
|
365
|
|
366 if ($writeMode)
|
|
367 {
|
|
368 if ($galCleanFlags & GCLEAN_CACHES)
|
|
369 mgDelete($cacheFilename, FALSE);
|
|
370
|
|
371 if ($galCleanFlags & GCLEAN_THUMBNAILS)
|
|
372 mgDelete($path."/".$galTNPath, TRUE);
|
|
373 }
|
|
374 }
|
|
375 else
|
|
376 // Update modes
|
|
377 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN)
|
|
378 {
|
|
379 // Load current cache file, if it exists
|
|
380 $galEntries = array();
|
|
381 $cacheTime = -1;
|
|
382 if ($mode == GCMD_UPDATE && file_exists($cacheFilename))
|
|
383 {
|
|
384 $cacheTime = filemtime($cacheFilename);
|
|
385 @include $cacheFilename;
|
|
386 }
|
|
387
|
|
388 // Read caption data
|
|
389 $captions = mgReadCaptionsFile($basepath, $path);
|
|
390 $gallery = mgGetAlbumData($basepath, $path);
|
|
391 if ($parentData !== NULL && $parentEntry !== NULL)
|
|
392 {
|
|
393 $gallery["parent"] = $parentData;
|
|
394 mgCopyEntryData($gallery, $parentEntry, MG_STR, "caption", "caption");
|
|
395 }
|
|
396
|
|
397 // Start actual processing
|
|
398 $nentries = count($entries);
|
|
399 $nentry = 0;
|
|
400 echo $path." .. ";
|
|
401 foreach ($entries as $ename => &$edata)
|
|
402 {
|
|
403 printf("\r%s (%1.1f%%) ..", $path, ($nentry * 100.0) / $nentries);
|
|
404
|
|
405 $nentry++;
|
|
406 $efilename = $path."/".$ename;
|
|
407
|
|
408 if (array_key_exists($ename, $galEntries))
|
|
409 $galEntry = &$galEntries[$ename];
|
|
410 else
|
|
411 $galEntry = array();
|
|
412
|
|
413 mgCheckQuit(FALSE);
|
|
414
|
|
415 // Update with captions file data, if any
|
|
416 if (array_key_exists($ename, $captions))
|
|
417 {
|
|
418 foreach ($captions[$ename] as $ckey => $cval)
|
|
419 $edata[$ckey] = $cval;
|
|
420 }
|
|
421
|
|
422 // Handle entry based on type
|
|
423 if ($edata["type"] == 0)
|
|
424 {
|
|
425 $updFlags = 0;
|
|
426 $tnPath = $path."/".$galTNPath;
|
|
427 $medFilename = $tnPath."/".$edata["base"].mgGetSetting("med_suffix").$edata["ext"];
|
|
428 $tnFilename = $tnPath."/".$ename;
|
|
429 $capFilename = $path."/".$edata["base"].".txt";
|
|
430
|
|
431 // Check what we need to update ..
|
|
432 if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"])
|
|
433 $updFlags |= GUPD_MED_IMAGE;
|
|
434
|
|
435 if (!file_exists($tnFilename) || filemtime($tnFilename) < $edata["mtime"])
|
|
436 $updFlags |= GUPD_TN_IMAGE;
|
|
437
|
|
438 if (mgNeedUpdate($galEntry, "mtime", $edata["mtime"]))
|
|
439 $updFlags |= GUPD_EXIF_INFO;
|
|
440
|
|
441 if (file_exists($capFilename) &&
|
|
442 mgNeedUpdate($galEntry, "mtime", filemtime($capFilename)))
|
|
443 $updFlags |= GUPD_CAPTION;
|
|
444
|
|
445 // Check for EXIF info
|
|
446 if (($updFlags & GUPD_EXIF_INFO) &&
|
|
447 ($exif = @exif_read_data($efilename)) !== FALSE)
|
|
448 {
|
|
449 echo "%";
|
|
450 foreach ($galExifConversions as $conv)
|
|
451 mgCopyEntryData($edata, $exif, $conv[0], $conv[1], $conv[2]);
|
|
452 }
|
|
453 else
|
|
454 {
|
|
455 // Copy old data that is not yet in new
|
|
456 echo "*";
|
|
457 foreach ($galEntry as $okey => $odata)
|
|
458 {
|
|
459 if (!array_key_exists($okey, $edata))
|
|
460 $edata[$okey] = $odata;
|
|
461 }
|
|
462 }
|
|
463
|
|
464 // Generate thumbnails, etc.
|
|
465 if ($updFlags & GUPD_IMAGES)
|
|
466 {
|
|
467 mgMakeDir($tnPath, 0755);
|
|
468
|
|
469 if ($updFlags & GUPD_MED_IMAGE)
|
|
470 {
|
|
471 echo "1";
|
|
472 mgConvertImage($efilename, $medFilename,
|
|
473 array(mgGetSetting("med_width"), mgGetSetting("med_height")),
|
|
474 "JPEG", mgGetSetting("med_quality"), TRUE);
|
|
475 }
|
|
476
|
|
477 if ($updFlags & GUPD_TN_IMAGE)
|
|
478 {
|
|
479 echo "2";
|
|
480 mgConvertImage($efilename, $tnFilename,
|
|
481 array(mgGetSetting("tn_width"), mgGetSetting("tn_height")),
|
|
482 "JPEG", mgGetSetting("tn_quality"), TRUE);
|
|
483 }
|
|
484 }
|
|
485
|
|
486 // Check for .txt caption file
|
|
487 if ($updFlags & GUPD_CAPTION)
|
|
488 {
|
|
489 echo "?";
|
|
490 if (($tmpData = @file_get_contents($capFilename)) !== FALSE)
|
|
491 $edata["caption"] = $tmpData;
|
|
492 }
|
|
493 }
|
|
494 else
|
|
495 if ($edata["type"] == 1)
|
|
496 {
|
|
497 $tmp = mgGetAlbumData($basepath, $efilename);
|
|
498 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "caption");
|
|
499 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "fallback_caption");
|
|
500 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "title");
|
|
501 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide", "hide", FALSE);
|
|
502 }
|
|
503 }
|
|
504
|
|
505 echo "\r".$path." ..... DONE\n";
|
|
506
|
|
507 mgCheckQuit(TRUE);
|
|
508
|
|
509 // Store gallery cache for this directory
|
|
510 if ($writeMode)
|
|
511 {
|
|
512 $images = array();
|
|
513 $albums = array();
|
|
514 foreach ($entries as $ename => &$edata)
|
|
515 {
|
|
516 if ($edata["hide"])
|
|
517 continue;
|
|
518
|
|
519 unset($edata["hide"]);
|
|
520 if ($edata["type"] == 0)
|
|
521 $images[$ename] = &$edata;
|
|
522 else
|
|
523 $albums[$ename] = &$edata;
|
|
524 }
|
|
525 ksort($images);
|
|
526 ksort($albums);
|
|
527
|
|
528 $str =
|
|
529 "<?\n".
|
|
530 "\$galData = ".var_export($gallery, TRUE).";\n".
|
|
531 "\$galAlbumsIndex = ".var_export(array_keys($albums), TRUE).";\n".
|
|
532 "\$galImagesIndex = ".var_export(array_keys($images), TRUE).";\n".
|
|
533 "\$galEntries = ".var_export($entries, TRUE).";\n".
|
|
534 "?>";
|
|
535
|
|
536 if (@file_put_contents($cacheFilename, $str, LOCK_EX) === FALSE)
|
|
537 return mgError("Error writing '".$cacheFilename."'\n");
|
|
538 }
|
|
539 }
|
|
540 else
|
|
541 mgFatal("Invalid work mode '".$mode."'.\n");
|
|
542
|
|
543 mgCheckQuit(TRUE);
|
|
544
|
|
545 // Recurse to subdirectories
|
|
546 foreach ($entries as $ename => $edata)
|
|
547 if ($edata["type"] == 1)
|
|
548 {
|
|
549 $epath = $path."/".$ename."/";
|
|
550 $newWriteMode = ($writeMode === FALSE && $epath == $startAt) || $writeMode;
|
|
551
|
|
552 if (!mgHandleDirectory($mode, $basepath, $epath, $gallery, $edata, $newWriteMode, $startAt))
|
|
553 return FALSE;
|
|
554 }
|
|
555
|
|
556 mgCheckQuit(TRUE);
|
|
557
|
|
558 return TRUE;
|
|
559 }
|
|
560
|
|
561
|
|
562 function mgSigHandler($signo)
|
|
563 {
|
|
564 global $flagQuit;
|
|
565 switch ($signo)
|
|
566 {
|
|
567 case SIGTERM:
|
|
568 mgFatal("Received SIGTERM.\n");
|
|
569 break;
|
|
570
|
|
571 case SIGQUIT:
|
|
572 case SIGINT:
|
|
573 $flagQuit = TRUE;
|
|
574 break;
|
|
575
|
|
576 }
|
|
577 }
|
|
578
|
|
579
|
|
580 function mgProcessGalleries($cmd, $path)
|
|
581 {
|
|
582 global $galTNPath;
|
|
583
|
|
584 // Fetch the settings we need
|
|
585 if (mgReadSettings() === FALSE)
|
|
586 die("MGallery not configured.\n");
|
|
587
|
|
588 // Check validity of some settings
|
|
589 $galPath = mgGetSetting("base_path");
|
|
590 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path"));
|
|
591 if ($galTNPath != mgGetSetting("tn_path"))
|
|
592 mgError("Invalid tn_path, using '".$galTNPath."'.\n");
|
|
593
|
|
594 if (strpos($galTNPath, "/") !== FALSE || $galTNPath == "")
|
|
595 mgFatal("Invalid tn_path '".$galTNPath."'.\n");
|
|
596
|
|
597 $parentData = $parentEntry = NULL;
|
|
598 $writeMode = TRUE;
|
|
599 $startAt = NULL;
|
|
600
|
|
601 // Check for path argument
|
|
602 if ($path !== FALSE)
|
|
603 {
|
|
604 // Check the given path, needs to be "under" the gallery path
|
|
605 $cmp = mgCleanPath(TRUE, mgRealPath($galPath))."/";
|
|
606 $tmp = mgCleanPath(TRUE, mgRealPath($path))."/";
|
|
607 if (substr($tmp, 0, strlen($cmp)) != $cmp)
|
|
608 mgFatal("Path '".$path."' ('".$tmp."') does not reside under '".$galPath."' ('".$cmp."')!\n");
|
|
609
|
|
610 // Check if we need to bootstrap
|
|
611 if ($cmp != $tmp)
|
|
612 {
|
|
613 $bpath = mgCleanPath(TRUE, mgRealPath($tmp."../"));
|
|
614
|
|
615 if ($cmd != GCMD_CLEAN)
|
|
616 {
|
|
617 $cacheFile = mgGetPath($bpath, "cache_file");
|
|
618 if (!file_exists($cacheFile))
|
|
619 mgFatal("Can't start working from '".$path."', parent '".$cacheFile."' does not exist!\n");
|
|
620
|
|
621 @include($cacheFile);
|
|
622 if (!isset($galEntries) || !isset($galData))
|
|
623 mgFatal("Cache file '".$cacheFile."' is broken or stale.\n");
|
|
624 }
|
|
625
|
|
626 $writeMode = FALSE;
|
|
627 $startAt = $tmp;
|
|
628 $path = $bpath;
|
|
629
|
|
630 echo "Starting: '".$startAt."' inside '".$path."'.\n";
|
|
631 }
|
|
632 else
|
|
633 $path = $tmp;
|
|
634 }
|
|
635 else
|
|
636 $path = $galPath;
|
|
637
|
|
638 // Start working
|
|
639 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n";
|
|
640 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt);
|
|
641 }
|
|
642
|
|
643
|
|
644 function mgShowCopyright()
|
|
645 {
|
|
646 global $mgProgVersion, $mgProgCopyright,
|
|
647 $mgProgInfo, $mgProgEmail;
|
|
648
|
|
649 echo
|
|
650 "MGTool ".$mgProgVersion." - MGallery management tool\n".
|
|
651 $mgProgInfo." ".$mgProgEmail."\n".
|
|
652 "(C) Copyright ".$mgProgCopyright."\n";
|
|
653 }
|
|
654
|
|
655
|
|
656 function mgShowHelp()
|
|
657 {
|
|
658 global $argv;
|
|
659 echo
|
|
660 "Usage: ".basename($argv[0])." <command> [arguments]\n".
|
|
661 "\n".
|
|
662 " --help - Show this help.\n".
|
|
663 " --version - Show version information.\n".
|
|
664 "\n".
|
|
665 " update [path]\n".
|
|
666 " Update directories under <path> or all gallery dirs.\n".
|
|
667 " Conditionally scans dirs for new or changed images and\n".
|
|
668 " updates cache files as needed.\n".
|
|
669 "\n".
|
|
670 " rescan [path]\n".
|
|
671 " Like 'update', but forces all cache files to be regenerated\n".
|
|
672 " and EXIF/caption/etc information to be re-scanned even\n".
|
|
673 " if the file timestamps do not justify re-scanning.\n".
|
|
674 "\n".
|
|
675 " clean <all|caches|thumbnails> [path]\n".
|
|
676 " Delete all generated files or selectively cache files or\n".
|
|
677 " everything inside thumbnail directories.\n".
|
|
678 "\n".
|
|
679 " config\n".
|
|
680 " Display configuration values.\n".
|
|
681 "\n";
|
|
682 }
|
|
683
|
|
684
|
|
685 //
|
|
686 // Main code starts
|
|
687 //
|
|
688 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"]))
|
|
689 {
|
|
690 header("Status: 404 Not Found");
|
|
691 die();
|
|
692 }
|
|
693
|
|
694 pcntl_signal(SIGTERM, "mgSigHandler");
|
|
695 pcntl_signal(SIGHUP, "mgSigHandler");
|
|
696 pcntl_signal(SIGQUIT, "mgSigHandler");
|
|
697 pcntl_signal(SIGINT, "mgSigHandler");
|
|
698
|
|
699
|
|
700 $cmd = mgCArgLC(1);
|
|
701 switch ($cmd)
|
|
702 {
|
|
703 case "--version":
|
|
704 case "version":
|
|
705 case "ver":
|
|
706 mgShowCopyright();
|
|
707 break;
|
|
708
|
|
709 case FALSE:
|
|
710 // No arguments
|
|
711 mgError("Nothing to do. Showing help:\n");
|
|
712
|
|
713 case "--help":
|
|
714 case "help":
|
|
715 if ($cmd !== FALSE)
|
|
716 mgShowCopyright();
|
|
717 mgShowHelp();
|
|
718 break;
|
|
719
|
|
720 case "update": case "up": case "upd": case "upda":
|
|
721 mgProcessGalleries(GCMD_UPDATE, mgCArg(2));
|
|
722 break;
|
|
723
|
|
724 case "rescan": case "re": case "res":
|
|
725 mgProcessGalleries(GCMD_RESCAN, mgCArg(2));
|
|
726 break;
|
|
727
|
|
728 case "clean": case "cl": case "cle":
|
|
729 $cmode = mgCArgLC(2, 2);
|
|
730 switch ($cmode)
|
|
731 {
|
|
732 case "al": $galCleanFlags = GCLEAN_ALL; break;
|
|
733 case "ca": $galCleanFlags = GCLEAN_CACHES; break;
|
|
734 case "th": $galCleanFlags = GCLEAN_THUMBNAILS; break;
|
|
735 case FALSE:
|
|
736 mgFatal("Cleaning requires a mode argument.\n");
|
|
737
|
|
738 default:
|
|
739 mgFatal("Invalid clean mode '".mgCArg(2)."'.\n");
|
|
740 }
|
|
741 $flagDoDelete = FALSE;
|
|
742 mgProcessGalleries(GCMD_CLEAN, mgCArg(3));
|
|
743 echo "--\n";
|
|
744 if (mgYesNoPrompt("Really delete the above files and directories?"))
|
|
745 {
|
|
746 echo "OKAY.\n";
|
|
747 }
|
|
748 break;
|
|
749
|
|
750 case "config":
|
|
751 if (mgReadSettings() === FALSE)
|
|
752 die("MGallery not configured.\n");
|
|
753
|
|
754 foreach ($mgDefaults as $key => $dval)
|
|
755 {
|
|
756 $sval = mgGetSetting($key);
|
|
757 printf("%-20s = %s%s\n",
|
|
758 $key,
|
|
759 mgGetDValStr($dval[0], $sval),
|
|
760 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval[0], $dval[1]).")" : "");
|
|
761 }
|
|
762 break;
|
|
763
|
|
764 case "dump":
|
|
765 if (mgReadSettings() === FALSE)
|
|
766 die("MGallery not configured.\n");
|
|
767
|
|
768 foreach ($mgDefaults as $key => $dval)
|
|
769 {
|
|
770 $sval = mgGetSetting($key);
|
|
771 printf("%-20s = %s\n", $key, mgGetDValStr($dval[0], $sval));
|
|
772 }
|
|
773 break;
|
|
774
|
|
775 default:
|
|
776 mgError("Unknown option/command '".$cmd."'.\n");
|
|
777 break;
|
|
778 }
|
|
779
|
|
780 ?> |