[486] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | // +-----------------------------------------------------------------------+ |
---|
[486] | 23 | |
---|
[493] | 24 | include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php'); |
---|
| 25 | |
---|
[858] | 26 | |
---|
[493] | 27 | function get_sync_iptc_data($file) |
---|
[486] | 28 | { |
---|
[2560] | 29 | global $conf; |
---|
[2521] | 30 | |
---|
[600] | 31 | $map = $conf['use_iptc_mapping']; |
---|
[2521] | 32 | |
---|
[493] | 33 | $iptc = get_iptc_data($file, $map); |
---|
| 34 | |
---|
| 35 | foreach ($iptc as $pwg_key => $value) |
---|
[486] | 36 | { |
---|
[2560] | 37 | if (in_array($pwg_key, array('date_creation', 'date_available'))) |
---|
[486] | 38 | { |
---|
[614] | 39 | if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches)) |
---|
[486] | 40 | { |
---|
[7507] | 41 | $year = $matches[1]; |
---|
| 42 | $month = $matches[2]; |
---|
| 43 | $day = $matches[3]; |
---|
| 44 | |
---|
| 45 | if (!checkdate($month, $day, $year)) |
---|
| 46 | { |
---|
| 47 | // we suppose the year is correct |
---|
| 48 | $month = 1; |
---|
| 49 | $day = 1; |
---|
| 50 | } |
---|
[12831] | 51 | |
---|
[7507] | 52 | $iptc[$pwg_key] = $year.'-'.$month.'-'.$day; |
---|
[486] | 53 | } |
---|
| 54 | } |
---|
| 55 | } |
---|
[493] | 56 | |
---|
[562] | 57 | if (isset($iptc['keywords'])) |
---|
| 58 | { |
---|
[1127] | 59 | // official keywords separator is the comma |
---|
| 60 | $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']); |
---|
[13081] | 61 | $iptc['keywords'] = preg_replace('/,+/', ',', $iptc['keywords']); |
---|
[562] | 62 | $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']); |
---|
[1223] | 63 | |
---|
| 64 | $iptc['keywords'] = implode( |
---|
| 65 | ',', |
---|
| 66 | array_unique( |
---|
| 67 | explode( |
---|
| 68 | ',', |
---|
| 69 | $iptc['keywords'] |
---|
| 70 | ) |
---|
| 71 | ) |
---|
| 72 | ); |
---|
[562] | 73 | } |
---|
| 74 | |
---|
[1717] | 75 | foreach ($iptc as $pwg_key => $value) |
---|
| 76 | { |
---|
| 77 | $iptc[$pwg_key] = addslashes($iptc[$pwg_key]); |
---|
| 78 | } |
---|
| 79 | |
---|
[493] | 80 | return $iptc; |
---|
[486] | 81 | } |
---|
| 82 | |
---|
[858] | 83 | function get_sync_exif_data($file) |
---|
| 84 | { |
---|
[2560] | 85 | global $conf; |
---|
[858] | 86 | |
---|
| 87 | $exif = get_exif_data($file, $conf['use_exif_mapping']); |
---|
| 88 | |
---|
| 89 | foreach ($exif as $pwg_key => $value) |
---|
| 90 | { |
---|
[2560] | 91 | if (in_array($pwg_key, array('date_creation', 'date_available'))) |
---|
[858] | 92 | { |
---|
[9073] | 93 | if (preg_match('/^(\d{4}).(\d{2}).(\d{2}) (\d{2}).(\d{2}).(\d{2})/', $value, $matches)) |
---|
[858] | 94 | { |
---|
[9073] | 95 | $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3].' '.$matches[4].':'.$matches[5].':'.$matches[6]; |
---|
| 96 | } |
---|
| 97 | elseif (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches)) |
---|
| 98 | { |
---|
[858] | 99 | $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
| 100 | } |
---|
[11748] | 101 | else |
---|
| 102 | { |
---|
| 103 | unset($exif[$pwg_key]); |
---|
| 104 | continue; |
---|
| 105 | } |
---|
[858] | 106 | } |
---|
[1717] | 107 | $exif[$pwg_key] = addslashes($exif[$pwg_key]); |
---|
[858] | 108 | } |
---|
| 109 | |
---|
| 110 | return $exif; |
---|
| 111 | } |
---|
| 112 | |
---|
[12831] | 113 | |
---|
| 114 | function get_sync_metadata_attributes() |
---|
[486] | 115 | { |
---|
| 116 | global $conf; |
---|
[582] | 117 | |
---|
[12831] | 118 | $update_fields = array('filesize', 'width', 'height'); |
---|
| 119 | |
---|
| 120 | if ($conf['use_exif']) |
---|
| 121 | { |
---|
| 122 | $update_fields = |
---|
| 123 | array_merge( |
---|
| 124 | $update_fields, |
---|
| 125 | array_keys($conf['use_exif_mapping']) |
---|
| 126 | ); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if ($conf['use_iptc']) |
---|
| 130 | { |
---|
| 131 | $update_fields = |
---|
| 132 | array_merge( |
---|
| 133 | $update_fields, |
---|
| 134 | array_keys($conf['use_iptc_mapping']) |
---|
| 135 | ); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | return array_unique($update_fields); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | function get_sync_metadata($infos) |
---|
| 142 | { |
---|
| 143 | global $conf; |
---|
| 144 | $file = PHPWG_ROOT_PATH.$infos['path']; |
---|
| 145 | $fs = @filesize($file); |
---|
| 146 | |
---|
| 147 | if ($fs===false) |
---|
| 148 | { |
---|
| 149 | return false; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | $infos['filesize'] = floor($fs/1024); |
---|
| 153 | |
---|
| 154 | if (isset($infos['representative_ext'])) |
---|
| 155 | { |
---|
| 156 | $file = original_to_representative($file, $infos['representative_ext']); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | if ($image_size = @getimagesize($file)) |
---|
| 160 | { |
---|
| 161 | $infos['width'] = $image_size[0]; |
---|
| 162 | $infos['height'] = $image_size[1]; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | if ($conf['use_exif']) |
---|
| 166 | { |
---|
| 167 | $exif = get_sync_exif_data($file); |
---|
| 168 | $infos = array_merge($infos, $exif); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | if ($conf['use_iptc']) |
---|
| 172 | { |
---|
| 173 | $iptc = get_sync_iptc_data($file); |
---|
| 174 | $infos = array_merge($infos, $iptc); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | return $infos; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | function sync_metadata($ids) |
---|
| 182 | { |
---|
| 183 | global $conf; |
---|
| 184 | |
---|
[582] | 185 | if (!defined('CURRENT_DATE')) |
---|
| 186 | { |
---|
[624] | 187 | define('CURRENT_DATE', date('Y-m-d')); |
---|
[582] | 188 | } |
---|
| 189 | |
---|
[625] | 190 | $datas = array(); |
---|
[1119] | 191 | $tags_of = array(); |
---|
[486] | 192 | |
---|
[1883] | 193 | $query = ' |
---|
[12831] | 194 | SELECT id, path, representative_ext |
---|
[1883] | 195 | FROM '.IMAGES_TABLE.' |
---|
[12831] | 196 | WHERE id IN ( |
---|
| 197 | '.wordwrap(implode(', ', $ids), 160, "\n").' |
---|
[1883] | 198 | ) |
---|
| 199 | ;'; |
---|
| 200 | |
---|
[12831] | 201 | $result = pwg_query($query); |
---|
| 202 | while ($data = pwg_db_fetch_assoc($result)) |
---|
[1883] | 203 | { |
---|
[12831] | 204 | $data = get_sync_metadata($data); |
---|
| 205 | if ($data === false) |
---|
[486] | 206 | { |
---|
[12831] | 207 | continue; |
---|
[486] | 208 | } |
---|
[1883] | 209 | |
---|
[12831] | 210 | $id = $data['id']; |
---|
| 211 | foreach (array('keywords', 'tags') as $key) |
---|
[1883] | 212 | { |
---|
[12831] | 213 | if (isset($data[$key])) |
---|
[4683] | 214 | { |
---|
[12831] | 215 | if (!isset($tags_of[$id])) |
---|
| 216 | { |
---|
| 217 | $tags_of[$id] = array(); |
---|
| 218 | } |
---|
[486] | 219 | |
---|
[12831] | 220 | foreach (explode(',', $data[$key]) as $tag_name) |
---|
[493] | 221 | { |
---|
[12831] | 222 | array_push( |
---|
| 223 | $tags_of[$id], |
---|
| 224 | tag_id_from_tag_name($tag_name) |
---|
| 225 | ); |
---|
[493] | 226 | } |
---|
[486] | 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
[625] | 230 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
[486] | 231 | |
---|
[625] | 232 | array_push($datas, $data); |
---|
[486] | 233 | } |
---|
[2521] | 234 | |
---|
[625] | 235 | if (count($datas) > 0) |
---|
[486] | 236 | { |
---|
[12831] | 237 | $update_fields = get_sync_metadata_attributes(); |
---|
| 238 | array_push($update_fields, 'date_metadata_update'); |
---|
[2521] | 239 | |
---|
[12831] | 240 | $update_fields = array_diff( |
---|
| 241 | $update_fields, |
---|
| 242 | array('tags', 'keywords') |
---|
| 243 | ); |
---|
[2521] | 244 | |
---|
[1127] | 245 | mass_updates( |
---|
| 246 | IMAGES_TABLE, |
---|
[903] | 247 | array( |
---|
| 248 | 'primary' => array('id'), |
---|
[12831] | 249 | 'update' => $update_fields |
---|
[1127] | 250 | ), |
---|
[2521] | 251 | $datas, |
---|
| 252 | MASS_UPDATES_SKIP_EMPTY |
---|
[1127] | 253 | ); |
---|
[486] | 254 | } |
---|
[1119] | 255 | |
---|
[1127] | 256 | set_tags_of($tags_of); |
---|
[486] | 257 | } |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | * returns an array associating element id (images.id) with its complete |
---|
| 261 | * path in the filesystem |
---|
| 262 | * |
---|
| 263 | * @param int id_uppercat |
---|
[1029] | 264 | * @param int site_id |
---|
[486] | 265 | * @param boolean recursive ? |
---|
| 266 | * @param boolean only newly added files ? |
---|
| 267 | * @return array |
---|
| 268 | */ |
---|
[2521] | 269 | function get_filelist($category_id = '', $site_id=1, $recursive = false, |
---|
[1029] | 270 | $only_new = false) |
---|
[486] | 271 | { |
---|
[657] | 272 | // filling $cat_ids : all categories required |
---|
| 273 | $cat_ids = array(); |
---|
[2521] | 274 | |
---|
[486] | 275 | $query = ' |
---|
[657] | 276 | SELECT id |
---|
[582] | 277 | FROM '.CATEGORIES_TABLE.' |
---|
[1029] | 278 | WHERE site_id = '.$site_id.' |
---|
[582] | 279 | AND dir IS NOT NULL'; |
---|
[486] | 280 | if (is_numeric($category_id)) |
---|
| 281 | { |
---|
| 282 | if ($recursive) |
---|
| 283 | { |
---|
| 284 | $query.= ' |
---|
[4367] | 285 | AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\' |
---|
[486] | 286 | '; |
---|
| 287 | } |
---|
| 288 | else |
---|
| 289 | { |
---|
| 290 | $query.= ' |
---|
[582] | 291 | AND id = '.$category_id.' |
---|
[486] | 292 | '; |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | $query.= ' |
---|
| 296 | ;'; |
---|
[587] | 297 | $result = pwg_query($query); |
---|
[4325] | 298 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[486] | 299 | { |
---|
[657] | 300 | array_push($cat_ids, $row['id']); |
---|
[486] | 301 | } |
---|
| 302 | |
---|
[657] | 303 | if (count($cat_ids) == 0) |
---|
[590] | 304 | { |
---|
| 305 | return array(); |
---|
| 306 | } |
---|
| 307 | |
---|
[486] | 308 | $query = ' |
---|
[12831] | 309 | SELECT id, path, representative_ext |
---|
[486] | 310 | FROM '.IMAGES_TABLE.' |
---|
[1121] | 311 | WHERE storage_category_id IN ('.implode(',', $cat_ids).')'; |
---|
[486] | 312 | if ($only_new) |
---|
| 313 | { |
---|
| 314 | $query.= ' |
---|
| 315 | AND date_metadata_update IS NULL |
---|
| 316 | '; |
---|
| 317 | } |
---|
| 318 | $query.= ' |
---|
| 319 | ;'; |
---|
[12831] | 320 | return hash_from_query($query, 'id'); |
---|
[486] | 321 | } |
---|
| 322 | ?> |
---|