Mercurial > hg > mgallery
annotate mgtool.php @ 258:c5a5899f4869
Minor stylesheet adjustments.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 12 Dec 2018 17:26:32 +0200 |
parents | 67a807666405 |
children | 32c13f594e39 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/php |
2 <?php | |
3 // | |
4 // Yet Another Image Gallery | |
69
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
5 // -- Commandline tool for creating / updating gallery |
7777887bc515
Update header/copyright blurbs.
Matti Hamalainen <ccr@tnsp.org>
parents:
64
diff
changeset
|
6 // Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
161 | 7 // (C) Copyright 2015-2018 Tecnic Software productions (TNSP) |
0 | 8 // |
9 require_once "mgallery.inc.php"; | |
10 | |
11 // | |
12 // Array for specifying what will be copied and converted | |
13 // from the image file's EXIF information tag(s). | |
14 // | |
45 | 15 $galExifConversions = |
16 [ | |
124
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
17 [ MG_STR, "caption" , "ImageDescription" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
18 [ MG_STR, "copyright" , "Copyright" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
19 [ MG_STR, "model" , "Model" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
20 [ MG_INT, "width" , ["COMPUTED", "Width"] ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
21 [ MG_INT, "height" , ["COMPUTED", "Height"] ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
22 [ MG_DVA, "fnumber" , "FNumber" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
23 [ MG_DVA, "exposure" , "ExposureTime" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
24 [ MG_INT, "iso" , "ISOSpeedRatings" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
25 [ MG_STR, "lensmodel" , "UndefinedTag:0xA434" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
26 [ MG_DVA, "focallength" , "FocalLength" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
27 [ MG_DATE, "datetime" , "DateTimeOriginal" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
28 [ MG_DATE, "datetime" , "DateTimeDigitized" ], |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
29 [ MG_INT, "filesize" , "FileSize" ], |
45 | 30 ]; |
0 | 31 |
32 define("GCMD_UPDATE" , 1); | |
33 define("GCMD_RESCAN" , 2); | |
34 define("GCMD_CLEAN" , 3); | |
35 | |
36 define("GCLEAN_CACHES" , 0x01); | |
251
19dab8b0c340
Remove clean command suboptions 'thumbnails' and 'mediums' and replace them with 'images'.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
37 define("GCLEAN_IMAGES" , 0x02); |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
38 define("GCLEAN_TRASH" , 0x04); |
0 | 39 define("GCLEAN_ALL" , 0x0f); |
40 | |
41 define("GUPD_MED_IMAGE" , 0x01); | |
42 define("GUPD_TN_IMAGE" , 0x02); | |
43 define("GUPD_IMAGES" , 0x0f); | |
44 define("GUPD_EXIF_INFO" , 0x10); | |
45 define("GUPD_CAPTION" , 0x20); | |
46 | |
47 | |
48 // | |
49 // Convert and scale image file function, for generating | |
50 // the intermediate size images and thumbnails. Uses the | |
51 // PHP ImageMagick bindings. | |
52 // | |
53 function mgConvertImage($inFilename, $outFilename, $outDim, $outFormat, $outQuality, $thumb) | |
54 { | |
55 // Create conversion entity | |
53
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
56 try |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
57 { |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
58 $img = new Imagick($inFilename); |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
59 } |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
60 catch (Exception $e) |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
61 { |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
62 return mgError("ImageMagick exception for file '".$inFilename."':\n".$e->getMessage()."\n"); |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
63 } |
4c0a08b0ef65
Handle ImageMagick exceptions for loading input images.
Matti Hamalainen <ccr@tnsp.org>
parents:
52
diff
changeset
|
64 |
0 | 65 if ($img === FALSE) |
66 return mgError("ImageMagick could not digest the file '".$inFilename."'.\n"); | |
67 | |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
68 $profiles = $img->getImageProfiles("icc", true); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
69 $img->setImageDepth(16); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
70 $img->transformImageColorspace(Imagick::COLORSPACE_SRGB); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
71 $img->setImageColorspace(Imagick::COLORSPACE_SRGB); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
72 |
0 | 73 if ($outDim !== FALSE) |
74 { | |
75 // Get dimensions, setup background | |
76 $dim = $img->getImageGeometry(); | |
77 //$img->setImageBackgroundColor(imagick::COLOR_BLACK); | |
78 $img->setGravity(imagick::GRAVITY_CENTER); | |
79 | |
80 | |
81 if ($dim["width"] < $dim["height"]) | |
82 { | |
83 $stmp = $outDim[0]; | |
84 $outDim[0] = $outDim[1]; | |
85 $outDim[1] = $stmp; | |
86 } | |
87 | |
88 if ($dim["width"] != $outDim[0] || $dim["height"] != $outDim[1]) | |
89 { | |
90 $outDim[1] = ($dim["height"] * $outDim[0]) / $dim["width"]; | |
91 } | |
92 | |
93 // Act based on image size vs. desired size and $thumb mode | |
94 if ($thumb || $dim["width"] > $outDim[0] || $dim["height"] > $outDim[1]) | |
95 { | |
96 // 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
|
97 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 98 $img->setImageExtent($outDim[0], $outDim[1]); |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
99 // $img->normalizeImage(); |
0 | 100 $img->unsharpMaskImage(0, 0.5, 1, 0.05); |
101 } | |
102 if ($dim["width"] < $outDim[0] || $dim["height"] < $outDim[1]) | |
103 { | |
104 // 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
|
105 $img->resizeImage($outDim[0], $outDim[1], Imagick::FILTER_LANCZOS, 1); |
0 | 106 $img->setImageExtent($outDim[0], $outDim[1]); |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
107 // $img->normalizeImage(); |
0 | 108 $img->unsharpMaskImage(0, 0.5, 1, 0.05); |
109 } | |
110 } | |
111 | |
250
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
112 $img->setImageDepth(8); |
257
67a807666405
Minor fix to mgtool format conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
256
diff
changeset
|
113 $tfmt = strtolower($outFormat); |
67a807666405
Minor fix to mgtool format conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
256
diff
changeset
|
114 switch ($tfmt) |
250
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
115 { |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
116 case "jpeg": |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
117 $img->setFormat("JPEG"); |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
118 $img->setImageCompression(Imagick::COMPRESSION_JPEG); |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
119 break; |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
120 |
250
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
121 case "webp": |
257
67a807666405
Minor fix to mgtool format conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
256
diff
changeset
|
122 $img->setFormat("WEBP"); |
250
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
123 $img->setOption('webp:method', '6'); |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
124 break; |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
125 |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
126 default: |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
127 return mgError("Unsupported MGallery med/tn format '".$tfmt."'.\n"); |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
128 } |
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
129 |
55
b1fa4fd593d1
Fix thumbnail/medium image JPEG quality handling to actually work. :S
Matti Hamalainen <ccr@tnsp.org>
parents:
54
diff
changeset
|
130 $img->setImageCompressionQuality($outQuality); |
80 | 131 |
51
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
132 $img->stripImage(); |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
133 if (!empty($profiles)) |
549541f3e9c0
Possibly improve medium and thumbnail image generation by controlling Imagick components better.
Matti Hamalainen <ccr@tnsp.org>
parents:
46
diff
changeset
|
134 $img->profileImage("icc", $profiles["icc"]); |
0 | 135 |
136 $img->writeImage($outFilename); | |
137 $img->removeImage(); | |
138 return TRUE; | |
139 } | |
140 | |
141 | |
142 // | |
143 // Converts one value (mainly from EXIF tag information) | |
52 | 144 // by doing explicit type casting and special conversions. |
0 | 145 // |
146 function mgConvertExifData($val, $vtype) | |
147 { | |
148 switch ($vtype) | |
149 { | |
46
e58292065b01
Let arrays through mgConvertExifData() when the type is MG_STR without
Matti Hamalainen <ccr@tnsp.org>
parents:
45
diff
changeset
|
150 case MG_STR: return is_array($val) ? $val : (string) $val; |
0 | 151 case MG_INT: return intval($val); |
152 case MG_BOOL: return intval($val); | |
153 case MG_DVA: | |
223
438277861f51
Implement better interpretation of DVA values (exposure times, for example),
Matti Hamalainen <ccr@tnsp.org>
parents:
189
diff
changeset
|
154 if (sscanf($val, "%d/%d", $v1, $v2) == 2 && $v2 != 0 && $v1 != 0) |
0 | 155 { |
156 if ($v1 < $v2) | |
249
abcd21b01829
Use floating point formatting for certain DVA values.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
157 return sprintf("1/%1.1f", $v2 / $v1); |
0 | 158 else |
159 return sprintf("%1.1f", $v1 / $v2); | |
160 } | |
161 else | |
162 return $val; | |
163 | |
124
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
164 case MG_DATE: |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
165 return date_timestamp_get(date_create_from_format("Y:m:d H:i:s", $val)); |
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
166 |
0 | 167 default: |
168 return $val; | |
169 } | |
170 } | |
171 | |
172 | |
173 // | |
174 // Conditionally copies one "field" from an associated array/hash to another. | |
175 // If destination is already SET, nothing will be done. If source does | |
176 // not exist (e.g. one or more of the keys do not exist in source array), | |
177 // a default value will be used, if provided. | |
178 // Source may have multi-depth keys, destination has one key. | |
179 // | |
180 function mgCopyEntryData(&$dst, $src, $vtype, $dkey, $skeys, $default = NULL) | |
181 { | |
182 // Is destination already set? | |
183 if (isset($dst[$dkey])) | |
184 return FALSE; | |
185 | |
186 // If input key is not array, change it into one | |
187 if (!is_array($skeys)) | |
188 $skeys = array($skeys); | |
189 | |
190 // Traverse input array by using consequent keys | |
191 $tmp = &$src; | |
192 foreach ($skeys as $skey) | |
193 { | |
194 if (!array_key_exists($skey, $tmp)) | |
195 { | |
196 // Key didn't exist, try for default | |
197 if ($default !== NULL) | |
198 $dst[$dkey] = $default; | |
199 return FALSE; | |
200 } | |
201 else | |
202 $tmp = &$tmp[$skey]; | |
203 } | |
204 | |
205 // Optionally convert the input value | |
206 $dst[$dkey] = mgConvertExifData($tmp, $vtype); | |
207 return TRUE; | |
208 } | |
209 | |
210 | |
211 // | |
212 // Attempt to get gallery album data from various sources. | |
213 // | |
214 function mgGetAlbumData($galBasePath, $galPath) | |
215 { | |
216 // Check path permissions | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
217 $galData = []; |
0 | 218 if (is_readable($galPath)) |
219 { | |
220 // First, try to read gallery/album info file | |
221 $filename = mgGetPath($galPath, "info_file"); | |
222 if ($filename !== FALSE && file_exists($filename)) | |
223 { | |
224 mgDebug("Reading INFOFILE: ".$filename."\n"); | |
225 if (($galData = parse_ini_file($filename, FALSE)) === FALSE) | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
226 $galData = []; |
0 | 227 } |
228 | |
229 // Read header file, if any, and we don't have "header" field set yet | |
230 $filename = mgGetPath($galPath, "header_file"); | |
231 if ($filename !== FALSE && file_exists($filename) && | |
232 !isset($galData["header"])) | |
233 { | |
234 mgDebug("Reading HEADERFILE: ".$filename."\n"); | |
235 $galData["header"] = file_get_contents($filename); | |
236 } | |
237 | |
238 // Check for alternate key/values for album title/caption | |
239 if (isset($galData["title"]) && !isset($galData["caption"])) | |
240 { | |
241 $galData["caption"] = $galData["title"]; | |
242 unset($galData["title"]); | |
243 } | |
244 } | |
245 else | |
246 $galData["hide"] = TRUE; | |
247 | |
248 // If caption is not set, use last path component for | |
249 // a fallback value in case we don't discover proper title | |
250 // from other sources we can't check here yet. | |
251 $path = explode("/", $galPath); | |
252 $galData["fallback_caption"] = ucfirst(str_replace("_", " ", end($path))); | |
253 | |
254 // Last, store the current gallery path | |
255 $len = strlen($galBasePath); | |
256 if ($len < strlen($galPath) && substr($galPath, 0, $len) == $galBasePath) | |
257 $galData["path"] = substr($galPath, $len); | |
258 else | |
259 $galData["path"] = ""; | |
260 | |
261 return $galData; | |
262 } | |
263 | |
264 | |
165 | 265 // |
266 // Parse a simple image captions file, with format of one entry per line. | |
267 // Lines starting with "# " are comments (note the whitespace), empty lines ignored. | |
268 // First continuous non-whitespace text is file/dirname, followed | |
269 // by caption text, which is separate with whitespace from the filename. | |
270 // Filenames starting with # will be made hidden entries. | |
271 // Filenames starging with % will hide contents. | |
272 // | |
0 | 273 function mgReadCaptionsFile($galBasePath, $galPath) |
274 { | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
275 $captions = []; |
0 | 276 $filename = mgGetPath($galPath, "captions_file"); |
277 if ($filename === FALSE || ($fp = @fopen($filename, "rb")) === FALSE) | |
278 return $captions; | |
279 | |
280 mgDebug("Reading CAPTIONS: ".$filename."\n"); | |
281 | |
282 // Read and parse data | |
283 while (!feof($fp)) | |
284 { | |
285 $str = trim(fgets($fp)); | |
286 // Ignore comments and empty lines | |
287 if ($str != "#" && $str != "") | |
288 { | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
289 if (preg_match("/^([#%]?)\s*(\S+?)\s+(.+)$/", $str, $m)) |
45 | 290 $captions[$m[2]] = ["caption" => $m[3], "hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE]; |
0 | 291 else |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
292 if (preg_match("/^([#%]?)\s*(\S+?)$/", $str, $m)) |
45 | 293 $captions[$m[2]] = ["hide" => ($m[1] == "#"), "hide_contents" => ($m[1] == "%"), "used" => FALSE]; |
0 | 294 } |
295 } | |
296 | |
297 fclose($fp); | |
298 return $captions; | |
299 } | |
300 | |
301 | |
165 | 302 // |
303 // Create directory with specified permissions | |
304 // | |
0 | 305 function mgMakeDir($path, $perm) |
306 { | |
307 if (!file_exists($path)) | |
308 { | |
309 if (mkdir($path, $perm, TRUE) === false) | |
310 return mgError("Could not create directory '".$path."'\n"); | |
311 } | |
312 return TRUE; | |
313 } | |
314 | |
315 | |
165 | 316 // |
317 // Print a simple yes/no prompt with given message | |
318 // and default value. | |
319 // | |
0 | 320 function mgYesNoPrompt($msg, $default = FALSE) |
321 { | |
322 echo $msg." [".($default ? "Y/n" : "y/N")."]? "; | |
323 $sprompt = strtolower(trim(fgets(STDIN))); | |
324 | |
166
d8387b48b08d
Improve yes/no prompt check.
Matti Hamalainen <ccr@tnsp.org>
parents:
165
diff
changeset
|
325 if ($sprompt == "") |
d8387b48b08d
Improve yes/no prompt check.
Matti Hamalainen <ccr@tnsp.org>
parents:
165
diff
changeset
|
326 return $default; |
0 | 327 else |
165 | 328 return $sprompt[0] == 'y'; |
0 | 329 } |
330 | |
331 | |
165 | 332 // |
182 | 333 // Delete given directory OR file, recursively |
165 | 334 // |
183 | 335 function mgDelete($path, $showFiles) |
0 | 336 { |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
337 global $flagDoDelete, $countDoDelete; |
0 | 338 if (is_dir($path)) |
339 { | |
340 if (($dirHandle = @opendir($path)) === FALSE) | |
341 return mgError("Could not read directory '".$path."'.\n"); | |
342 | |
343 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
344 { | |
345 if ($dirFile != "." && $dirFile != "..") | |
183 | 346 mgDelete($path."/".$dirFile, $showFiles); |
0 | 347 } |
183 | 348 |
0 | 349 closedir($dirHandle); |
350 | |
351 echo " - ".$path." [DIR]\n"; | |
62
3f60a5f5b4b3
Actually fix checking for files to be deleted to work.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
352 $countDoDelete++; |
0 | 353 if ($flagDoDelete) |
354 rmdir($path); | |
355 } | |
356 else | |
54
624c50e1b52d
Fix 'mgtool clean' to not attempt deleting files that do not exist.
Matti Hamalainen <ccr@tnsp.org>
parents:
53
diff
changeset
|
357 if (file_exists($path)) |
0 | 358 { |
183 | 359 if ($showFiles) |
0 | 360 echo " - ".$path."\n"; |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
361 |
62
3f60a5f5b4b3
Actually fix checking for files to be deleted to work.
Matti Hamalainen <ccr@tnsp.org>
parents:
61
diff
changeset
|
362 $countDoDelete++; |
0 | 363 if ($flagDoDelete) |
364 unlink($path); | |
365 } | |
366 } | |
367 | |
368 | |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
369 function mgDeleteConditional($path, &$noDelete) |
255
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
370 { |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
371 global $flagDoDelete, $countDoDelete; |
255
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
372 if (is_dir($path)) |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
373 { |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
374 if (($dirHandle = @opendir($path)) === FALSE) |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
375 return mgError("Could not read directory '".$path."'.\n"); |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
376 |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
377 while (($dirFile = @readdir($dirHandle)) !== FALSE) |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
378 { |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
379 if ($dirFile != "." && $dirFile != "..") |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
380 mgDeleteConditional($path."/".$dirFile, $noDelete); |
255
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
381 } |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
382 |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
383 closedir($dirHandle); |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
384 |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
385 if (!array_key_exists($path, $noDelete)) |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
386 { |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
387 $countDoDelete++; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
388 if ($flagDoDelete) |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
389 rmdir($path); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
390 else |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
391 echo "DEL DIR '".$path."'\n"; |
255
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
392 } |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
393 } |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
394 else |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
395 if (file_exists($path) && !array_key_exists($path, $noDelete)) |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
396 { |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
397 $countDoDelete++; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
398 if ($flagDoDelete) |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
399 unlink($path); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
400 else |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
401 echo "DEL FILE '".$path."'\n"; |
255
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
402 } |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
403 } |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
404 |
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
405 |
165 | 406 // |
407 // Check if we have received the quit signal | |
408 // and if yes, quit cleanly now. | |
409 // | |
0 | 410 function mgCheckQuit($now = FALSE) |
411 { | |
412 global $flagQuit; | |
413 | |
414 // Dispatch pending signals | |
415 pcntl_signal_dispatch(); | |
416 | |
417 // Check result | |
418 if ($now && $flagQuit) | |
419 mgFatal("Quitting.\n"); | |
420 | |
421 return $flagQuit; | |
422 } | |
423 | |
424 | |
425 function mgNeedUpdate($entry, $field, $cvalue) | |
426 { | |
427 if (!array_key_exists($field, $entry)) | |
428 return TRUE; | |
80 | 429 |
0 | 430 return ($entry[$field] < $cvalue); |
431 } | |
432 | |
433 | |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
434 function mgSortFunc($a, $b) |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
435 { |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
436 if (isset($a["datetime"]) && isset($b["datetime"])) |
124
eac2cf04261a
Change how date timestamps are stored and handled. This breaks cache file
Matti Hamalainen <ccr@tnsp.org>
parents:
114
diff
changeset
|
437 return $b["datetime"] - $a["datetime"]; |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
438 else |
108
417cdd9f8864
Use image file base name for sorting if timestamps are not available.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
439 if (isset($a["base"]) && isset($b["base"])) |
417cdd9f8864
Use image file base name for sorting if timestamps are not available.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
440 return strcmp($b["base"], $a["base"]); |
417cdd9f8864
Use image file base name for sorting if timestamps are not available.
Matti Hamalainen <ccr@tnsp.org>
parents:
80
diff
changeset
|
441 else |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
442 return 0; |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
443 } |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
444 |
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
445 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
446 function mgWriteGalleryCache($cacheFilename, &$gallery, &$entries, &$parentEntry) |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
447 { |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
448 // Store gallery cache for this directory |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
449 $images = []; |
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
450 $albums = []; |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
451 $output = []; |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
452 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
453 // If we are hiding contents of an album, generate no data |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
454 if (!$parentEntry["hide_contents"]) |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
455 { |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
456 foreach ($entries as $ename => &$edata) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
457 { |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
458 if ($edata["hide"]) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
459 continue; |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
460 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
461 unset($edata["hide"]); |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
462 if ($edata["type"] == 0) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
463 $images[$ename] = &$edata; |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
464 else |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
465 $albums[$ename] = &$edata; |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
466 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
467 $output[$ename] = &$edata; |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
468 } |
30
f2c21a8b9071
Sort images in a folder by their capture timestamp.
Matti Hamalainen <ccr@tnsp.org>
parents:
29
diff
changeset
|
469 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
470 uasort($images, "mgSortFunc"); |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
471 krsort($albums); |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
472 |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
473 // Choose gallery album image |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
474 if (count($images) > 0) |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
475 { |
79
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
476 if (isset($gallery["albumpic"]) && |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
477 isset($images[$gallery["albumpic"]])) |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
478 { |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
479 $parentEntry["image"] = $gallery["albumpic"]; |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
480 } |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
481 else |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
482 { |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
483 end($images); |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
484 $parentEntry["image"] = key($images); |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
485 } |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
486 } |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
487 else |
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
488 { |
79
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
489 foreach ($albums as $aid => &$adata) |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
490 if (isset($adata["image"])) |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
491 { |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
492 $parentEntry["image"] = &$adata; |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
493 break; |
8d8b361a76af
Allow setting "albumpic" in gallery.info for each album, e.g. forced album cover picture.
Matti Hamalainen <ccr@tnsp.org>
parents:
69
diff
changeset
|
494 } |
64
9a01e14b27ef
Implement hide_contents setting for galleries, which hides contents of an album.
Matti Hamalainen <ccr@tnsp.org>
parents:
63
diff
changeset
|
495 } |
35
985596db0f01
Implement recursive depth album covers.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
496 } |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
497 |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
498 $str = |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
499 "<?\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
500 "\$galData = ".var_export($gallery, TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
501 "\$galAlbumsIndex = ".var_export(array_keys($albums), TRUE).";\n". |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
502 "\$galImagesIndex = ".var_export(array_keys($images), TRUE).";\n". |
61
ee82f7c8d44c
Do not include hidden entries in the entry list.
Matti Hamalainen <ccr@tnsp.org>
parents:
60
diff
changeset
|
503 "\$galEntries = ".var_export($output, TRUE).";\n". |
15
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
504 "?>"; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
505 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
506 if (@file_put_contents($cacheFilename, $str, LOCK_EX) === FALSE) |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
507 return mgError("Error writing '".$cacheFilename."'\n"); |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
508 |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
509 return TRUE; |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
510 } |
805f93962cf9
Factorize gallery cache file writing to a separate function.
Matti Hamalainen <ccr@tnsp.org>
parents:
11
diff
changeset
|
511 |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
512 |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
513 function mgHandleDirectory($mode, $basepath, $path, &$parentData, &$parentEntry, $writeMode, $startAt) |
0 | 514 { |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
515 global $galExifConversions, $galTNPath, $galMedPath, $galCleanFlags; |
0 | 516 |
517 // Get cache file path | |
518 if (($cacheFilename = mgGetPath($path, "cache_file")) === FALSE) | |
519 return mgError("Cache filename / path not set.\n"); | |
520 | |
521 mgCheckQuit(TRUE); | |
522 | |
523 // Read directory contents | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
524 $entries = []; |
0 | 525 if (($dirHandle = @opendir($path)) === FALSE) |
526 return mgError("Could not read directory '".$path."'.\n"); | |
527 | |
528 while (($dirFile = @readdir($dirHandle)) !== FALSE) | |
529 { | |
530 $realFile = $path."/".$dirFile; | |
531 if (is_dir($realFile)) | |
532 { | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
533 if ($dirFile[0] != "." && $dirFile != $galTNPath && $dirFile != $galMedPath) |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
534 $entries[$dirFile] = ["type" => 1, "base" => $dirFile, "ext" => "", "mtime" => filemtime($realFile)]; |
0 | 535 } |
536 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
|
537 if (preg_match("/^([^\/]+)(".mgGetSetting("format_exts").")$/i", $dirFile, $dirMatch)) |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
538 $entries[$dirFile] = ["type" => 0, "base" => $dirMatch[1], "ext" => $dirMatch[2], "mtime" => filemtime($realFile), "hide" => false]; |
0 | 539 } |
540 closedir($dirHandle); | |
541 | |
542 mgCheckQuit(); | |
543 | |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
544 $tnPath = $path."/".$galTNPath; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
545 $medPath = $path."/".$galMedPath; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
546 $generatedFiles = []; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
547 $generatedFiles[$tnPath] = 1; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
548 $generatedFiles[$medPath] = 1; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
549 |
0 | 550 // Cleanup mode |
551 if ($mode == GCMD_CLEAN) | |
552 { | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
553 $gallery = []; |
0 | 554 |
555 if ($writeMode) | |
556 { | |
557 if ($galCleanFlags & GCLEAN_CACHES) | |
183 | 558 mgDelete($cacheFilename, TRUE); |
0 | 559 |
251
19dab8b0c340
Remove clean command suboptions 'thumbnails' and 'mediums' and replace them with 'images'.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
560 if ($galCleanFlags & GCLEAN_IMAGES) |
19dab8b0c340
Remove clean command suboptions 'thumbnails' and 'mediums' and replace them with 'images'.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
561 { |
183 | 562 mgDelete($path."/".$galTNPath, FALSE); |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
563 mgDelete($path."/".$galMedPath, FALSE); |
251
19dab8b0c340
Remove clean command suboptions 'thumbnails' and 'mediums' and replace them with 'images'.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
564 } |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
565 |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
566 if ($galCleanFlags & GCLEAN_TRASH) |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
567 { |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
568 foreach ($entries as $ename => &$edata) |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
569 { |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
570 $medFilename = $medPath."/".$ename.".".mgGetSetting("med_format"); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
571 $tnFilename = $tnPath."/".$ename.".".mgGetSetting("tn_format"); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
572 $generatedFiles[$medFilename] = 1; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
573 $generatedFiles[$tnFilename] = 1; |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
574 } |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
575 |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
576 // Delete any "trash" files from medium/thumbnail dirs |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
577 mgDeleteConditional($tnPath, $generatedFiles); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
578 mgDeleteConditional($medPath, $generatedFiles); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
579 } |
0 | 580 } |
581 } | |
582 else | |
583 // Update modes | |
584 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) | |
585 { | |
586 // Load current cache file, if it exists | |
60
ef9f17280372
Use modern PHP array definition in some places.
Matti Hamalainen <ccr@tnsp.org>
parents:
57
diff
changeset
|
587 $galEntries = []; |
0 | 588 $cacheTime = -1; |
589 if ($mode == GCMD_UPDATE && file_exists($cacheFilename)) | |
590 { | |
591 $cacheTime = filemtime($cacheFilename); | |
592 @include $cacheFilename; | |
593 } | |
594 | |
595 // Read caption data | |
596 $captions = mgReadCaptionsFile($basepath, $path); | |
597 $gallery = mgGetAlbumData($basepath, $path); | |
598 if ($parentData !== NULL && $parentEntry !== NULL) | |
599 { | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
600 $gallery["parent"] = &$parentData; |
0 | 601 mgCopyEntryData($gallery, $parentEntry, MG_STR, "caption", "caption"); |
602 } | |
603 | |
604 // Start actual processing | |
605 $nentries = count($entries); | |
606 $nentry = 0; | |
607 echo $path." .. "; | |
608 foreach ($entries as $ename => &$edata) | |
609 { | |
610 printf("\r%s (%1.1f%%) ..", $path, ($nentry * 100.0) / $nentries); | |
611 | |
612 $nentry++; | |
613 $efilename = $path."/".$ename; | |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
614 $medFilename = $medPath."/".$ename.".".mgGetSetting("med_format"); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
615 $tnFilename = $tnPath."/".$ename.".".mgGetSetting("tn_format"); |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
616 $capFilename = $path."/".$edata["base"].".txt"; |
0 | 617 |
618 if (array_key_exists($ename, $galEntries)) | |
619 $galEntry = &$galEntries[$ename]; | |
620 else | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
621 $galEntry = []; |
0 | 622 |
57
29bb365e3ee2
Be more responsive when processing images, check for signals after each
Matti Hamalainen <ccr@tnsp.org>
parents:
56
diff
changeset
|
623 mgCheckQuit(TRUE); |
0 | 624 |
625 // Update with captions file data, if any | |
626 if (array_key_exists($ename, $captions)) | |
627 { | |
628 foreach ($captions[$ename] as $ckey => $cval) | |
629 $edata[$ckey] = $cval; | |
630 } | |
631 | |
632 // Handle entry based on type | |
633 if ($edata["type"] == 0) | |
634 { | |
635 $updFlags = 0; | |
255
d0bcc6c7fc66
Automatically clean any unnecessary "trash" from thumbnail and medium image directories.
Matti Hamalainen <ccr@tnsp.org>
parents:
253
diff
changeset
|
636 |
0 | 637 // Check what we need to update .. |
638 if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"]) | |
639 $updFlags |= GUPD_MED_IMAGE; | |
640 | |
641 if (!file_exists($tnFilename) || filemtime($tnFilename) < $edata["mtime"]) | |
642 $updFlags |= GUPD_TN_IMAGE; | |
643 | |
644 if (mgNeedUpdate($galEntry, "mtime", $edata["mtime"])) | |
645 $updFlags |= GUPD_EXIF_INFO; | |
646 | |
647 if (file_exists($capFilename) && | |
648 mgNeedUpdate($galEntry, "mtime", filemtime($capFilename))) | |
649 $updFlags |= GUPD_CAPTION; | |
650 | |
651 // Check for EXIF info | |
652 if (($updFlags & GUPD_EXIF_INFO) && | |
653 ($exif = @exif_read_data($efilename)) !== FALSE) | |
654 { | |
655 echo "%"; | |
656 foreach ($galExifConversions as $conv) | |
657 mgCopyEntryData($edata, $exif, $conv[0], $conv[1], $conv[2]); | |
658 } | |
659 else | |
660 { | |
661 // Copy old data that is not yet in new | |
662 echo "*"; | |
663 foreach ($galEntry as $okey => $odata) | |
664 { | |
665 if (!array_key_exists($okey, $edata)) | |
666 $edata[$okey] = $odata; | |
667 } | |
668 } | |
669 | |
670 // Generate thumbnails, etc. | |
671 if ($updFlags & GUPD_IMAGES) | |
672 { | |
673 mgMakeDir($tnPath, 0755); | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
674 mgMakeDir($medPath, 0755); |
0 | 675 |
676 if ($updFlags & GUPD_MED_IMAGE) | |
677 { | |
678 echo "1"; | |
679 mgConvertImage($efilename, $medFilename, | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
680 [mgGetSetting("med_width"), mgGetSetting("med_height")], |
250
3af3b3f50cb0
Add settings for med_format and tn_format (currently allowed values are
Matti Hamalainen <ccr@tnsp.org>
parents:
249
diff
changeset
|
681 mgGetSetting("med_format"), mgGetSetting("med_quality"), TRUE); |
0 | 682 } |
683 | |
684 if ($updFlags & GUPD_TN_IMAGE) | |
685 { | |
686 echo "2"; | |
687 mgConvertImage($efilename, $tnFilename, | |
63
1c32ece9acde
Some residual array() => [] conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
62
diff
changeset
|
688 [mgGetSetting("tn_width"), mgGetSetting("tn_height")], |
253
5b41efcec87d
More work on webp/etc support implementation.
Matti Hamalainen <ccr@tnsp.org>
parents:
251
diff
changeset
|
689 mgGetSetting("tn_format"), mgGetSetting("tn_quality"), TRUE); |
0 | 690 } |
691 } | |
692 | |
693 // Check for .txt caption file | |
694 if ($updFlags & GUPD_CAPTION) | |
695 { | |
696 echo "?"; | |
697 if (($tmpData = @file_get_contents($capFilename)) !== FALSE) | |
698 $edata["caption"] = $tmpData; | |
699 } | |
700 } | |
701 else | |
702 if ($edata["type"] == 1) | |
703 { | |
10 | 704 // Set some of the album data here |
0 | 705 $tmp = mgGetAlbumData($basepath, $efilename); |
706 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "caption"); | |
707 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "fallback_caption"); | |
708 mgCopyEntryData($edata, $tmp, MG_STR, "caption", "title"); | |
709 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide", "hide", FALSE); | |
11
0b097770061d
Improve content hiding functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
10
diff
changeset
|
710 mgCopyEntryData($edata, $tmp, MG_BOOL, "hide_contents", "hide_contents", FALSE); |
0 | 711 } |
712 } | |
713 | |
714 echo "\r".$path." ..... DONE\n"; | |
715 | |
716 mgCheckQuit(TRUE); | |
717 } | |
718 else | |
719 mgFatal("Invalid work mode '".$mode."'.\n"); | |
720 | |
721 mgCheckQuit(TRUE); | |
722 | |
723 // 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
|
724 foreach ($entries as $ename => &$edata) |
0 | 725 if ($edata["type"] == 1) |
726 { | |
727 $epath = $path."/".$ename."/"; | |
728 $newWriteMode = ($writeMode === FALSE && $epath == $startAt) || $writeMode; | |
729 | |
730 if (!mgHandleDirectory($mode, $basepath, $epath, $gallery, $edata, $newWriteMode, $startAt)) | |
731 return FALSE; | |
732 } | |
733 | |
16
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
734 // Finish update modes |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
735 if ($mode == GCMD_UPDATE || $mode == GCMD_RESCAN) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
736 { |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
737 // Store gallery cache for this directory |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
738 if ($writeMode && !mgWriteGalleryCache($cacheFilename, $gallery, $entries, $parentEntry)) |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
739 return FALSE; |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
740 } |
3491ab93630e
Add sub-gallery thumbnails to gallery cache data.
Matti Hamalainen <ccr@tnsp.org>
parents:
15
diff
changeset
|
741 |
0 | 742 mgCheckQuit(TRUE); |
743 | |
744 return TRUE; | |
745 } | |
746 | |
747 | |
748 function mgSigHandler($signo) | |
749 { | |
750 global $flagQuit; | |
751 switch ($signo) | |
752 { | |
753 case SIGTERM: | |
754 mgFatal("Received SIGTERM.\n"); | |
755 break; | |
756 | |
757 case SIGQUIT: | |
758 case SIGINT: | |
759 $flagQuit = TRUE; | |
760 break; | |
761 | |
762 } | |
763 } | |
764 | |
765 | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
766 function mgCheckMPath($path, $id) |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
767 { |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
768 if ($path."/" != mgGetSetting($id)) |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
769 mgError("Invalid ".$id." '".mgGetSetting($id)."', using '".$path."'.\n"); |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
770 |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
771 if (strpos($path, "/") !== FALSE || $path == "") |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
772 mgFatal("Invalid ".$id." '".$path."'.\n"); |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
773 } |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
774 |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
775 |
0 | 776 function mgProcessGalleries($cmd, $path) |
777 { | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
778 global $galTNPath, $galMedPath; |
0 | 779 |
780 // Check validity of some settings | |
781 $galPath = mgGetSetting("base_path"); | |
782 $galTNPath = mgCleanPath(TRUE, mgGetSetting("tn_path")); | |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
783 $galMedPath = mgCleanPath(TRUE, mgGetSetting("med_path")); |
0 | 784 |
185
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
785 mgCheckMPath($galTNPath, "tn_path"); |
cb7019690a73
Separate medium image and thumbnail directories / handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
184
diff
changeset
|
786 mgCheckMPath($galMedPath, "med_path"); |
0 | 787 |
788 $parentData = $parentEntry = NULL; | |
789 $writeMode = TRUE; | |
790 $startAt = NULL; | |
791 | |
792 // Check for path argument | |
793 if ($path !== FALSE) | |
794 { | |
795 // Check the given path, needs to be "under" the gallery path | |
796 $cmp = mgCleanPath(TRUE, mgRealPath($galPath))."/"; | |
797 $tmp = mgCleanPath(TRUE, mgRealPath($path))."/"; | |
798 if (substr($tmp, 0, strlen($cmp)) != $cmp) | |
799 mgFatal("Path '".$path."' ('".$tmp."') does not reside under '".$galPath."' ('".$cmp."')!\n"); | |
800 | |
801 // Check if we need to bootstrap | |
802 if ($cmp != $tmp) | |
803 { | |
804 $bpath = mgCleanPath(TRUE, mgRealPath($tmp."../")); | |
805 | |
806 if ($cmd != GCMD_CLEAN) | |
807 { | |
808 $cacheFile = mgGetPath($bpath, "cache_file"); | |
809 if (!file_exists($cacheFile)) | |
810 mgFatal("Can't start working from '".$path."', parent '".$cacheFile."' does not exist!\n"); | |
811 | |
812 @include($cacheFile); | |
813 if (!isset($galEntries) || !isset($galData)) | |
814 mgFatal("Cache file '".$cacheFile."' is broken or stale.\n"); | |
815 } | |
816 | |
817 $writeMode = FALSE; | |
818 $startAt = $tmp; | |
819 $path = $bpath; | |
820 | |
821 echo "Starting: '".$startAt."' inside '".$path."'.\n"; | |
822 } | |
823 else | |
824 $path = $tmp; | |
825 } | |
826 else | |
827 $path = $galPath; | |
828 | |
829 // Start working | |
830 echo "Gallery path: '".$galPath."', starting at '".$path."' ...\n"; | |
831 mgHandleDirectory($cmd, $galPath, $path, $parentData, $parentEntry, $writeMode, $startAt); | |
832 } | |
833 | |
834 | |
835 function mgShowCopyright() | |
836 { | |
837 global $mgProgVersion, $mgProgCopyright, | |
838 $mgProgInfo, $mgProgEmail; | |
839 | |
840 echo | |
184 | 841 "MGTool ".$mgProgVersion." - MGallery management tool\n". |
842 $mgProgInfo." ".$mgProgEmail."\n". | |
843 "(C) Copyright ".$mgProgCopyright."\n"; | |
0 | 844 } |
845 | |
846 | |
847 function mgShowHelp() | |
848 { | |
849 global $argv; | |
850 echo | |
851 "Usage: ".basename($argv[0])." <command> [arguments]\n". | |
852 "\n". | |
853 " --help - Show this help.\n". | |
854 " --version - Show version information.\n". | |
855 "\n". | |
856 " update [path]\n". | |
857 " Update directories under <path> or all gallery dirs.\n". | |
858 " Conditionally scans dirs for new or changed images and\n". | |
859 " updates cache files as needed.\n". | |
860 "\n". | |
861 " rescan [path]\n". | |
862 " Like 'update', but forces all cache files to be regenerated\n". | |
863 " and EXIF/caption/etc information to be re-scanned even\n". | |
864 " if the file timestamps do not justify re-scanning.\n". | |
865 "\n". | |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
866 " clean <option> [path]\n". |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
867 " all - Delete all generated files\n". |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
868 " images - Delete generated images (thumbnails/mediums)\n". |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
869 " caches - Delete mgallery data cache files\n". |
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
870 " trash - Delete any 'unmanaged' files inside thumbnail/medium dirs\n". |
0 | 871 "\n". |
159
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
872 " config|dump\n". |
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
873 " Display configuration values (with extra information) or\n". |
b010c763a1fc
Document mgtool 'dump' command.
Matti Hamalainen <ccr@tnsp.org>
parents:
130
diff
changeset
|
874 " \"dump\" current configuration as is.\n". |
0 | 875 "\n"; |
876 } | |
877 | |
878 | |
879 // | |
880 // Main code starts | |
881 // | |
882 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"])) | |
883 { | |
884 header("Status: 404 Not Found"); | |
885 die(); | |
886 } | |
887 | |
888 pcntl_signal(SIGTERM, "mgSigHandler"); | |
889 pcntl_signal(SIGHUP, "mgSigHandler"); | |
890 pcntl_signal(SIGQUIT, "mgSigHandler"); | |
891 pcntl_signal(SIGINT, "mgSigHandler"); | |
892 | |
130
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
893 if (mgReadSettings() === FALSE) |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
894 die("MGallery is not configured, failed to find a configuration file.\n"); |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
895 |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
896 if (($pageTimeZone = mgGetSetting("timezone")) !== NULL) |
f825d644cf7a
Implement new config setting "timezone" for specifying the timezone used for date/timestamp interpretation.
Matti Hamalainen <ccr@tnsp.org>
parents:
124
diff
changeset
|
897 date_default_timezone_set($pageTimeZone); |
0 | 898 |
899 $cmd = mgCArgLC(1); | |
900 switch ($cmd) | |
901 { | |
902 case "--version": | |
903 case "version": | |
904 case "ver": | |
905 mgShowCopyright(); | |
906 break; | |
907 | |
908 case FALSE: | |
909 // No arguments | |
910 mgError("Nothing to do. Showing help:\n"); | |
911 | |
912 case "--help": | |
913 case "help": | |
914 if ($cmd !== FALSE) | |
915 mgShowCopyright(); | |
916 mgShowHelp(); | |
917 break; | |
918 | |
919 case "update": case "up": case "upd": case "upda": | |
920 mgProcessGalleries(GCMD_UPDATE, mgCArg(2)); | |
921 break; | |
922 | |
923 case "rescan": case "re": case "res": | |
924 mgProcessGalleries(GCMD_RESCAN, mgCArg(2)); | |
925 break; | |
926 | |
927 case "clean": case "cl": case "cle": | |
928 $cmode = mgCArgLC(2, 2); | |
929 switch ($cmode) | |
930 { | |
931 case "al": $galCleanFlags = GCLEAN_ALL; break; | |
932 case "ca": $galCleanFlags = GCLEAN_CACHES; break; | |
251
19dab8b0c340
Remove clean command suboptions 'thumbnails' and 'mediums' and replace them with 'images'.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
933 case "im": $galCleanFlags = GCLEAN_IMAGES; break; |
256
6ad17bc3d1a6
Add 'trash' option to 'clean' command to clean up any unmanaged files
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
934 case "tr": $galCleanFlags = GCLEAN_TRASH; break; |
0 | 935 case FALSE: |
936 mgFatal("Cleaning requires a mode argument.\n"); | |
937 | |
938 default: | |
939 mgFatal("Invalid clean mode '".mgCArg(2)."'.\n"); | |
940 } | |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
941 $countDoDelete = 0; |
0 | 942 $flagDoDelete = FALSE; |
943 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); | |
944 echo "--\n"; | |
56
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
945 if ($countDoDelete == 0) |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
946 { |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
947 echo "Nothing to clean!\n"; |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
948 } |
7384504d7b52
Do not prompt for deletion in cleanup if there are no files/directories to delete.
Matti Hamalainen <ccr@tnsp.org>
parents:
55
diff
changeset
|
949 else |
0 | 950 if (mgYesNoPrompt("Really delete the above files and directories?")) |
951 { | |
25
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
952 echo "Deleting ...\n"; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
953 $flagDoDelete = TRUE; |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
954 mgProcessGalleries(GCMD_CLEAN, mgCArg(3)); |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
955 } |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
956 else |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
957 { |
c698618e6f67
Actually implement deletion.
Matti Hamalainen <ccr@tnsp.org>
parents:
16
diff
changeset
|
958 echo "Okay, canceling operation.\n"; |
0 | 959 } |
960 break; | |
961 | |
962 case "config": | |
963 case "dump": | |
964 | |
965 foreach ($mgDefaults as $key => $dval) | |
966 { | |
967 $sval = mgGetSetting($key); | |
26 | 968 if ($cmd == "dump") |
969 { | |
189 | 970 printf("%-20s = %s\n", |
971 $key, | |
972 mgGetDValStr($dval, $sval)); | |
26 | 973 } |
974 else | |
975 { | |
976 printf("%-20s = %s%s\n", | |
977 $key, | |
114
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
978 mgGetDValStr($dval, $sval), |
c803235bd0f9
Implement flag config option type.
Matti Hamalainen <ccr@tnsp.org>
parents:
111
diff
changeset
|
979 ($dval[1] !== NULL && $sval !== $dval[1]) ? " (default: ".mgGetDValStr($dval, $dval[1]).")" : ""); |
26 | 980 } |
0 | 981 } |
982 break; | |
983 | |
984 default: | |
985 mgError("Unknown option/command '".$cmd."'.\n"); | |
986 break; | |
987 } | |
988 | |
989 ?> |