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