1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | /** |
---|
25 | * @package functions\admin\metadata |
---|
26 | */ |
---|
27 | |
---|
28 | |
---|
29 | include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php'); |
---|
30 | |
---|
31 | |
---|
32 | /** |
---|
33 | * Returns IPTC metadata to sync from a file, depending on IPTC mapping. |
---|
34 | * @toto : clean code (factorize foreach) |
---|
35 | * |
---|
36 | * @param string $file |
---|
37 | * @return array |
---|
38 | */ |
---|
39 | function get_sync_iptc_data($file) |
---|
40 | { |
---|
41 | global $conf; |
---|
42 | |
---|
43 | $map = $conf['use_iptc_mapping']; |
---|
44 | |
---|
45 | $iptc = get_iptc_data($file, $map); |
---|
46 | |
---|
47 | foreach ($iptc as $pwg_key => $value) |
---|
48 | { |
---|
49 | if (in_array($pwg_key, array('date_creation', 'date_available'))) |
---|
50 | { |
---|
51 | if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches)) |
---|
52 | { |
---|
53 | $year = $matches[1]; |
---|
54 | $month = $matches[2]; |
---|
55 | $day = $matches[3]; |
---|
56 | |
---|
57 | if (!checkdate($month, $day, $year)) |
---|
58 | { |
---|
59 | // we suppose the year is correct |
---|
60 | $month = 1; |
---|
61 | $day = 1; |
---|
62 | } |
---|
63 | |
---|
64 | $iptc[$pwg_key] = $year.'-'.$month.'-'.$day; |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | if (isset($iptc['keywords'])) |
---|
70 | { |
---|
71 | // official keywords separator is the comma |
---|
72 | $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']); |
---|
73 | $iptc['keywords'] = preg_replace('/,+/', ',', $iptc['keywords']); |
---|
74 | $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']); |
---|
75 | |
---|
76 | $iptc['keywords'] = implode( |
---|
77 | ',', |
---|
78 | array_unique( |
---|
79 | explode( |
---|
80 | ',', |
---|
81 | $iptc['keywords'] |
---|
82 | ) |
---|
83 | ) |
---|
84 | ); |
---|
85 | } |
---|
86 | |
---|
87 | foreach ($iptc as $pwg_key => $value) |
---|
88 | { |
---|
89 | $iptc[$pwg_key] = addslashes($iptc[$pwg_key]); |
---|
90 | } |
---|
91 | |
---|
92 | return $iptc; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Returns EXIF metadata to sync from a file, depending on EXIF mapping. |
---|
97 | * |
---|
98 | * @param string $file |
---|
99 | * @return array |
---|
100 | */ |
---|
101 | function get_sync_exif_data($file) |
---|
102 | { |
---|
103 | global $conf; |
---|
104 | |
---|
105 | $exif = get_exif_data($file, $conf['use_exif_mapping']); |
---|
106 | |
---|
107 | foreach ($exif as $pwg_key => $value) |
---|
108 | { |
---|
109 | if (in_array($pwg_key, array('date_creation', 'date_available'))) |
---|
110 | { |
---|
111 | if (preg_match('/^(\d{4}).(\d{2}).(\d{2}) (\d{2}).(\d{2}).(\d{2})/', $value, $matches)) |
---|
112 | { |
---|
113 | $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3].' '.$matches[4].':'.$matches[5].':'.$matches[6]; |
---|
114 | } |
---|
115 | elseif (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches)) |
---|
116 | { |
---|
117 | $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
118 | } |
---|
119 | else |
---|
120 | { |
---|
121 | unset($exif[$pwg_key]); |
---|
122 | continue; |
---|
123 | } |
---|
124 | } |
---|
125 | $exif[$pwg_key] = addslashes($exif[$pwg_key]); |
---|
126 | } |
---|
127 | |
---|
128 | return $exif; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Get all potential file metadata fields, including IPTC and EXIF. |
---|
133 | * |
---|
134 | * @return string[] |
---|
135 | */ |
---|
136 | function get_sync_metadata_attributes() |
---|
137 | { |
---|
138 | global $conf; |
---|
139 | |
---|
140 | $update_fields = array('filesize', 'width', 'height'); |
---|
141 | |
---|
142 | if ($conf['use_exif']) |
---|
143 | { |
---|
144 | $update_fields = |
---|
145 | array_merge( |
---|
146 | $update_fields, |
---|
147 | array_keys($conf['use_exif_mapping']), |
---|
148 | array('latitude', 'longitude') |
---|
149 | ); |
---|
150 | } |
---|
151 | |
---|
152 | if ($conf['use_iptc']) |
---|
153 | { |
---|
154 | $update_fields = |
---|
155 | array_merge( |
---|
156 | $update_fields, |
---|
157 | array_keys($conf['use_iptc_mapping']) |
---|
158 | ); |
---|
159 | } |
---|
160 | |
---|
161 | return array_unique($update_fields); |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Get all metadata of a file. |
---|
166 | * |
---|
167 | * @param array $infos - (path[, representative_ext]) |
---|
168 | * @return array - includes data provided in $infos |
---|
169 | */ |
---|
170 | function get_sync_metadata($infos) |
---|
171 | { |
---|
172 | global $conf; |
---|
173 | $file = PHPWG_ROOT_PATH.$infos['path']; |
---|
174 | $fs = @filesize($file); |
---|
175 | |
---|
176 | if ($fs===false) |
---|
177 | { |
---|
178 | return false; |
---|
179 | } |
---|
180 | |
---|
181 | $infos['filesize'] = floor($fs/1024); |
---|
182 | |
---|
183 | if (isset($infos['representative_ext'])) |
---|
184 | { |
---|
185 | $file = original_to_representative($file, $infos['representative_ext']); |
---|
186 | } |
---|
187 | |
---|
188 | if ($image_size = @getimagesize($file)) |
---|
189 | { |
---|
190 | $infos['width'] = $image_size[0]; |
---|
191 | $infos['height'] = $image_size[1]; |
---|
192 | } |
---|
193 | |
---|
194 | if ($conf['use_exif']) |
---|
195 | { |
---|
196 | $exif = get_sync_exif_data($file); |
---|
197 | $infos = array_merge($infos, $exif); |
---|
198 | } |
---|
199 | |
---|
200 | if ($conf['use_iptc']) |
---|
201 | { |
---|
202 | $iptc = get_sync_iptc_data($file); |
---|
203 | $infos = array_merge($infos, $iptc); |
---|
204 | } |
---|
205 | |
---|
206 | return $infos; |
---|
207 | } |
---|
208 | |
---|
209 | /** |
---|
210 | * Sync all metadata of a list of images. |
---|
211 | * Metadata are fetched from original files and saved in database. |
---|
212 | * |
---|
213 | * @param int[] $ids |
---|
214 | */ |
---|
215 | function sync_metadata($ids) |
---|
216 | { |
---|
217 | global $conf; |
---|
218 | |
---|
219 | if (!defined('CURRENT_DATE')) |
---|
220 | { |
---|
221 | define('CURRENT_DATE', date('Y-m-d')); |
---|
222 | } |
---|
223 | |
---|
224 | $datas = array(); |
---|
225 | $tags_of = array(); |
---|
226 | |
---|
227 | $query = ' |
---|
228 | SELECT id, path, representative_ext |
---|
229 | FROM '.IMAGES_TABLE.' |
---|
230 | WHERE id IN ( |
---|
231 | '.wordwrap(implode(', ', $ids), 160, "\n").' |
---|
232 | ) |
---|
233 | ;'; |
---|
234 | |
---|
235 | $result = pwg_query($query); |
---|
236 | while ($data = pwg_db_fetch_assoc($result)) |
---|
237 | { |
---|
238 | $data = get_sync_metadata($data); |
---|
239 | if ($data === false) |
---|
240 | { |
---|
241 | continue; |
---|
242 | } |
---|
243 | |
---|
244 | $id = $data['id']; |
---|
245 | foreach (array('keywords', 'tags') as $key) |
---|
246 | { |
---|
247 | if (isset($data[$key])) |
---|
248 | { |
---|
249 | if (!isset($tags_of[$id])) |
---|
250 | { |
---|
251 | $tags_of[$id] = array(); |
---|
252 | } |
---|
253 | |
---|
254 | foreach (explode(',', $data[$key]) as $tag_name) |
---|
255 | { |
---|
256 | $tags_of[$id][] = tag_id_from_tag_name($tag_name); |
---|
257 | } |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
262 | |
---|
263 | $datas[] = $data; |
---|
264 | } |
---|
265 | |
---|
266 | if (count($datas) > 0) |
---|
267 | { |
---|
268 | $update_fields = get_sync_metadata_attributes(); |
---|
269 | $update_fields[] = 'date_metadata_update'; |
---|
270 | |
---|
271 | $update_fields = array_diff( |
---|
272 | $update_fields, |
---|
273 | array('tags', 'keywords') |
---|
274 | ); |
---|
275 | |
---|
276 | mass_updates( |
---|
277 | IMAGES_TABLE, |
---|
278 | array( |
---|
279 | 'primary' => array('id'), |
---|
280 | 'update' => $update_fields |
---|
281 | ), |
---|
282 | $datas, |
---|
283 | MASS_UPDATES_SKIP_EMPTY |
---|
284 | ); |
---|
285 | } |
---|
286 | |
---|
287 | set_tags_of($tags_of); |
---|
288 | } |
---|
289 | |
---|
290 | /** |
---|
291 | * Returns an array associating element id (images.id) with its complete |
---|
292 | * path in the filesystem |
---|
293 | * |
---|
294 | * @param int $category_id |
---|
295 | * @param int $site_id |
---|
296 | * @param boolean $recursive |
---|
297 | * @param boolean $only_new |
---|
298 | * @return array |
---|
299 | */ |
---|
300 | function get_filelist($category_id = '', $site_id=1, $recursive = false, |
---|
301 | $only_new = false) |
---|
302 | { |
---|
303 | // filling $cat_ids : all categories required |
---|
304 | $cat_ids = array(); |
---|
305 | |
---|
306 | $query = ' |
---|
307 | SELECT id |
---|
308 | FROM '.CATEGORIES_TABLE.' |
---|
309 | WHERE site_id = '.$site_id.' |
---|
310 | AND dir IS NOT NULL'; |
---|
311 | if (is_numeric($category_id)) |
---|
312 | { |
---|
313 | if ($recursive) |
---|
314 | { |
---|
315 | $query.= ' |
---|
316 | AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\' |
---|
317 | '; |
---|
318 | } |
---|
319 | else |
---|
320 | { |
---|
321 | $query.= ' |
---|
322 | AND id = '.$category_id.' |
---|
323 | '; |
---|
324 | } |
---|
325 | } |
---|
326 | $query.= ' |
---|
327 | ;'; |
---|
328 | $result = pwg_query($query); |
---|
329 | while ($row = pwg_db_fetch_assoc($result)) |
---|
330 | { |
---|
331 | $cat_ids[] = $row['id']; |
---|
332 | } |
---|
333 | |
---|
334 | if (count($cat_ids) == 0) |
---|
335 | { |
---|
336 | return array(); |
---|
337 | } |
---|
338 | |
---|
339 | $query = ' |
---|
340 | SELECT id, path, representative_ext |
---|
341 | FROM '.IMAGES_TABLE.' |
---|
342 | WHERE storage_category_id IN ('.implode(',', $cat_ids).')'; |
---|
343 | if ($only_new) |
---|
344 | { |
---|
345 | $query.= ' |
---|
346 | AND date_metadata_update IS NULL |
---|
347 | '; |
---|
348 | } |
---|
349 | $query.= ' |
---|
350 | ;'; |
---|
351 | return hash_from_query($query, 'id'); |
---|
352 | } |
---|
353 | |
---|
354 | ?> |
---|