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