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