Mercurial > hg > mgallery
annotate mgtool.php @ 11:0b097770061d
Improve content hiding functionality.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 11 Aug 2015 12:24:54 +0300 |
parents | d63d2f0307b0 |
children | 805f93962cf9 |
rev | line source |
---|---|
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 | |
1
a07447c02b13
Use Lanczos filter for scaling instead of quadratic. Better quality and
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
81 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 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)? | |
1
a07447c02b13
Use Lanczos filter for scaling instead of quadratic. Better quality and
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
89 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 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 { | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
241 if (preg_match("/^([#%]?)\s*(\S+?)\s+(.+)$/", $str, $m)) |
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
242 $captions[$m[2]] = array("caption" => $m[3], "hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE); |
0 | 243 else |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
244 if (preg_match("/^([#%]?)\s*(\S+?)$/", $str, $m)) |
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
245 $captions[$m[2]] = array("hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE); |
0 | 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 | |
6
405a02586fc2
Fix handling of image files that have spaces in filename (regexp used \S,
Matti Hamalainen <ccr@tnsp.org>
parents:
1
diff
changeset
|
354 if (preg_match("/^([^\/]+)(".mgGetSetting("format_exts").")$/i", $dirFile, $dirMatch)) |
0 | 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 { | |
10 | 497 // Set some of the album data here |
0 | 498 $tmp = mgGetAlbumData($basepath, $efilename); |
499 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "caption"); | |
500 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "fallback_caption"); | |
501 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "title"); | |
502 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide", "hide", FALSE); | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
503 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide_contents", "hide_contents", FALSE); |
0 | 504 } |
505 } | |
506 | |
507 echo "\r".$path." ..... DONE\n"; | |
508 | |
509 mgCheckQuit(TRUE); | |
510 | |
511 // Store gallery cache for this directory | |
512 if ($writeMode) | |
513 { | |
514 $images = array(); | |
515 $albums = array(); | |
516 foreach ($entries as $ename => &$edata) | |
517 { | |
518 if ($edata["hide"]) | |
519 continue; | |
520 | |
521 unset($edata["hide"]); | |
522 if ($edata["type"] == 0) | |
523 $images[$ename] = &$edata; | |
524 else | |
525 $albums[$ename] = &$edata; | |
526 } | |
527 ksort($images); | |
528 ksort($albums); | |
529 | |
530 $str = | |
531 "<?\n". | |
532 "\$galData = ".var_export($gallery, TRUE).";\n". | |
533 "\$galAlbumsIndex = ".var_export(array_keys($albums), TRUE).";\n". | |
534 "\$galImagesIndex = ".var_export(array_keys($images), TRUE).";\n". | |
535 "\$galEntries = ".var_export($entries, TRUE).";\n". | |
536 "?>"; | |
537 | |
538 if (@file_put_contents($cacheFilename, $str, LOCK_EX) === FALSE) | |
539 return mgError("Error writing '".$cacheFilename."'\n"); | |
540 } | |
541 } | |
542 else | |
543 mgFatal("Invalid work mode '".$mode."'.\n"); | |
544 | |
545 mgCheckQuit(TRUE); | |
546 | |
547 // Recurse to subdirectories | |
9
232086da1670
Fix generating of sub-album/dirs names without gallery.info to specify
Matti Hamalainen <ccr@tnsp.org>
parents:
6
diff
changeset
|
548 foreach ($entries as $ename => &$edata) |
0 | 549 if ($edata["type"] == 1) |
550 { | |
551 $epath = $path."/".$ename."/"; | |
552 $newWriteMode = ($writeMode === FALSE && $epath == $startAt) || $writeMode; | |
553 | |
554 if (!mgHandleDirectory($mode, $basepath, $epath, $gallery, $edata, $newWriteMode, $startAt)) | |
555 return FALSE; | |
556 } | |
557 | |
558 mgCheckQuit(TRUE); | |
559 | |
560 return TRUE; | |
561 } | |
562 | |
563 | |
564 function mgSigHandler($signo) | |
565 { | |
566 global $flagQuit; | |
567 switch ($signo) | |
568 { | |
569 case SIGTERM: | |
570 mgFatal("Received SIGTERM.\n"); | |
571 break; | |
572 | |
573 case SIGQUIT: | |
574 case SIGINT: | |
575 $flagQuit = TRUE; | |
576 break; | |
577 | |
578 } | |
579 } | |
580 | |
581 | |
582 function mgProcessGalleries($cmd, $path) | |
583 { | |
584 global $galTNPath; | |
585 | |
586 // Fetch the settings we need | |
587 if (mgReadSettings() === FALSE) | |
588 die("MGallery not configured.\n"); | |
589 | |
590 // Check validity of some settings | |
591 $galPath = mgGetSetting("base_path"); | |
592 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); | |
593 if ($galTNPath != mgGetSetting("tn_path")) | |
594 mgError("Invalid tn_path, using '".$galTNPath."'.\n"); | |
595 | |
596 if (strpos($galTNPath, "/") !== FALSE || $galTNPath == "") | |
597 mgFatal("Invalid tn_path '".$galTNPath."'.\n"); | |
598 | |
599 $parentData = $parentEntry = NULL; | |
600 $writeMode = TRUE; | |
601 $startAt = NULL; | |
602 | |
603 // Check for path argument | |
604 if ($path !== FALSE) | |
605 { | |
606 // Check the given path, needs to be "under" the gallery path | |
607 $cmp = mgCleanPath(TRUE, mgRealPath($galPath))."/"; | |
608 $tmp = mgCleanPath(TRUE, mgRealPath($path))."/"; | |
609 if (substr($tmp, 0, strlen($cmp)) != $cmp) | |
610 mgFatal("Path '".$path."' ('".$tmp."') does not reside under '".$galPath."' ('".$cmp."')!\n"); | |
611 | |
612 // Check if we need to bootstrap | |
613 if ($cmp != $tmp) | |
614 { | |
615 $bpath = mgCleanPath(TRUE, mgRealPath($tmp."../")); | |
616 | |
617 if ($cmd != GCMD_CLEAN) | |
618 { | |
619 $cacheFile = mgGetPath($bpath, "cache_file"); | |
620 if (!file_exists($cacheFile)) | |
621 mgFatal("Can't start working from '".$path."', parent '".$cacheFile."' does not exist!\n"); | |
622 | |
623 @include($cacheFile); | |
624 if (!isset($galEntries) || !isset($galData)) | |
625 mgFatal("Cache file '".$cacheFile."' is broken or stale.\n"); | |
626 } | |
627 | |
628 $writeMode = FALSE; | |
629 $startAt = $tmp; | |
630 $path = $bpath; | |
631 | |
632 echo "Starting: '".$startAt."' inside '".$path."'.\n"; | |
633 } | |
634 else | |
635 $path = $tmp; | |
636 } | |
637 else | |
638 $path = $galPath; | |
639 | |
640 // Start working | |
641 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; | |
642 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); | |
643 } | |
644 | |
645 | |
646 function mgShowCopyright() | |
647 { | |
648 global $mgProgVersion, $mgProgCopyright, | |
649 $mgProgInfo, $mgProgEmail; | |
650 | |
651 echo | |
652 "MGTool ".$mgProgVersion." - MGallery management tool\n". | |
653 $mgProgInfo." ".$mgProgEmail."\n". | |
654 "(C) Copyright ".$mgProgCopyright."\n"; | |
655 } | |
656 | |
657 | |
658 function mgShowHelp() | |
659 { | |
660 global $argv; | |
661 echo | |
662 "Usage: ".basename($argv[0])." <command> [arguments]\n". | |
663 "\n". | |
664 " --help - Show this help.\n". | |
665 " --version - Show version information.\n". | |
666 "\n". | |
667 " update [path]\n". | |
668 " Update directories under <path> or all gallery dirs.\n". | |
669 " Conditionally scans dirs for new or changed images and\n". | |
670 " updates cache files as needed.\n". | |
671 "\n". | |
672 " rescan [path]\n". | |
673 " Like 'update', but forces all cache files to be regenerated\n". | |
674 " and EXIF/caption/etc information to be re-scanned even\n". | |
675 " if the file timestamps do not justify re-scanning.\n". | |
676 "\n". | |
677 " clean <all|caches|thumbnails> [path]\n". | |
678 " Delete all generated files or selectively cache files or\n". | |
679 " everything inside thumbnail directories.\n". | |
680 "\n". | |
681 " config\n". | |
682 " Display configuration values.\n". | |
683 "\n"; | |
684 } | |
685 | |
686 | |
687 // | |
688 // Main code starts | |
689 // | |
690 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"])) | |
691 { | |
692 header("Status: 404 Not Found"); | |
693 die(); | |
694 } | |
695 | |
696 pcntl_signal(SIGTERM, "mgSigHandler"); | |
697 pcntl_signal(SIGHUP, "mgSigHandler"); | |
698 pcntl_signal(SIGQUIT, "mgSigHandler"); | |
699 pcntl_signal(SIGINT, "mgSigHandler"); | |
700 | |
701 | |
702 $cmd = mgCArgLC(1); | |
703 switch ($cmd) | |
704 { | |
705 case "--version": | |
706 case "version": | |
707 case "ver": | |
708 mgShowCopyright(); | |
709 break; | |
710 | |
711 case FALSE: | |
712 // No arguments | |
713 mgError("Nothing to do. Showing help:\n"); | |
714 | |
715 case "--help": | |
716 case "help": | |
717 if ($cmd !== FALSE) | |
718 mgShowCopyright(); | |
719 mgShowHelp(); | |
720 break; | |
721 | |
722 case "update": case "up": case "upd": case "upda": | |
723 mgProcessGalleries(GCMD_UPDATE, mgCArg(2)); | |
724 break; | |
725 | |
726 case "rescan": case "re": case "res": | |
727 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); | |
728 break; | |
729 | |
730 case "clean": case "cl": case "cle": | |
731 $cmode = mgCArgLC(2, 2); | |
732 switch ($cmode) | |
733 { | |
734 case "al": $galCleanFlags = GCLEAN_ALL; break; | |
735 case "ca": $galCleanFlags = GCLEAN_CACHES; break; | |
736 case "th": $galCleanFlags = GCLEAN_THUMBNAILS; break; | |
737 case FALSE: | |
738 mgFatal("Cleaning requires a mode argument.\n"); | |
739 | |
740 default: | |
741 mgFatal("Invalid clean mode '".mgCArg(2)."'.\n"); | |
742 } | |
743 $flagDoDelete = FALSE; | |
744 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); | |
745 echo "--\n"; | |
746 if (mgYesNoPrompt("Really delete the above files and directories?")) | |
747 { | |
748 echo "OKAY.\n"; | |
749 } | |
750 break; | |
751 | |
752 case "config": | |
753 if (mgReadSettings() === FALSE) | |
754 die("MGallery not configured.\n"); | |
755 | |
756 foreach ($mgDefaults as $key => $dval) | |
757 { | |
758 $sval = mgGetSetting($key); | |
759 printf("%-20s = %s%s\n", | |
760 $key, | |
761 mgGetDValStr($dval[0], $sval), | |
762 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval[0], $dval[1]).")" : ""); | |
763 } | |
764 break; | |
765 | |
766 case "dump": | |
767 if (mgReadSettings() === FALSE) | |
768 die("MGallery not configured.\n"); | |
769 | |
770 foreach ($mgDefaults as $key => $dval) | |
771 { | |
772 $sval = mgGetSetting($key); | |
773 printf("%-20s = %s\n", $key, mgGetDValStr($dval[0], $sval)); | |
774 } | |
775 break; | |
776 | |
777 default: | |
778 mgError("Unknown option/command '".$cmd."'.\n"); | |
779 break; | |
780 } | |
781 | |
782 ?> |