[486] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[1717] | 5 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[486] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[1717] | 8 | // | file : $Id: functions_metadata.php 1883 2007-03-09 16:28:49Z plg $ |
---|
[486] | 9 | // | last update : $Date: 2007-03-09 16:28:49 +0000 (Fri, 09 Mar 2007) $ |
---|
| 10 | // | last modifier : $Author: plg $ |
---|
| 11 | // | revision : $Revision: 1883 $ |
---|
| 12 | // +-----------------------------------------------------------------------+ |
---|
| 13 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 14 | // | it under the terms of the GNU General Public License as published by | |
---|
| 15 | // | the Free Software Foundation | |
---|
| 16 | // | | |
---|
| 17 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 20 | // | General Public License for more details. | |
---|
| 21 | // | | |
---|
| 22 | // | You should have received a copy of the GNU General Public License | |
---|
| 23 | // | along with this program; if not, write to the Free Software | |
---|
| 24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 25 | // | USA. | |
---|
| 26 | // +-----------------------------------------------------------------------+ |
---|
| 27 | |
---|
[493] | 28 | include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php'); |
---|
| 29 | |
---|
[858] | 30 | $page['datefields'] = array('date_creation', 'date_available'); |
---|
| 31 | |
---|
[493] | 32 | function get_sync_iptc_data($file) |
---|
[486] | 33 | { |
---|
[858] | 34 | global $conf, $page; |
---|
[600] | 35 | |
---|
| 36 | $map = $conf['use_iptc_mapping']; |
---|
[486] | 37 | |
---|
[493] | 38 | $iptc = get_iptc_data($file, $map); |
---|
| 39 | |
---|
| 40 | foreach ($iptc as $pwg_key => $value) |
---|
[486] | 41 | { |
---|
[858] | 42 | if (in_array($pwg_key, $page['datefields'])) |
---|
[486] | 43 | { |
---|
[614] | 44 | if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches)) |
---|
[486] | 45 | { |
---|
[493] | 46 | $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
[486] | 47 | } |
---|
| 48 | } |
---|
| 49 | } |
---|
[493] | 50 | |
---|
[562] | 51 | if (isset($iptc['keywords'])) |
---|
| 52 | { |
---|
[1127] | 53 | // official keywords separator is the comma |
---|
| 54 | $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']); |
---|
[562] | 55 | $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']); |
---|
[1223] | 56 | |
---|
| 57 | $iptc['keywords'] = implode( |
---|
| 58 | ',', |
---|
| 59 | array_unique( |
---|
| 60 | explode( |
---|
| 61 | ',', |
---|
| 62 | $iptc['keywords'] |
---|
| 63 | ) |
---|
| 64 | ) |
---|
| 65 | ); |
---|
[562] | 66 | } |
---|
| 67 | |
---|
[1717] | 68 | foreach ($iptc as $pwg_key => $value) |
---|
| 69 | { |
---|
| 70 | $iptc[$pwg_key] = addslashes($iptc[$pwg_key]); |
---|
| 71 | } |
---|
| 72 | |
---|
[493] | 73 | return $iptc; |
---|
[486] | 74 | } |
---|
| 75 | |
---|
[858] | 76 | function get_sync_exif_data($file) |
---|
| 77 | { |
---|
| 78 | global $conf, $page; |
---|
| 79 | |
---|
| 80 | $exif = get_exif_data($file, $conf['use_exif_mapping']); |
---|
| 81 | |
---|
| 82 | foreach ($exif as $pwg_key => $value) |
---|
| 83 | { |
---|
| 84 | if (in_array($pwg_key, $page['datefields'])) |
---|
| 85 | { |
---|
| 86 | if (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches)) |
---|
| 87 | { |
---|
| 88 | $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
| 89 | } |
---|
| 90 | } |
---|
[1717] | 91 | $exif[$pwg_key] = addslashes($exif[$pwg_key]); |
---|
[858] | 92 | } |
---|
| 93 | |
---|
| 94 | return $exif; |
---|
| 95 | } |
---|
| 96 | |
---|
[486] | 97 | function update_metadata($files) |
---|
| 98 | { |
---|
| 99 | global $conf; |
---|
[582] | 100 | |
---|
| 101 | if (!defined('CURRENT_DATE')) |
---|
| 102 | { |
---|
[624] | 103 | define('CURRENT_DATE', date('Y-m-d')); |
---|
[582] | 104 | } |
---|
| 105 | |
---|
[625] | 106 | $datas = array(); |
---|
[1119] | 107 | $tags_of = array(); |
---|
[1883] | 108 | $has_high_images = array(); |
---|
[486] | 109 | |
---|
[1883] | 110 | $image_ids = array(); |
---|
[486] | 111 | foreach ($files as $id => $file) |
---|
| 112 | { |
---|
[1883] | 113 | array_push($image_ids, $id); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | $query = ' |
---|
| 117 | SELECT id |
---|
| 118 | FROM '.IMAGES_TABLE.' |
---|
| 119 | WHERE has_high = \'true\' |
---|
| 120 | AND id IN ( |
---|
| 121 | '.wordwrap(implode(', ', $image_ids), 80, "\n").' |
---|
| 122 | ) |
---|
| 123 | ;'; |
---|
| 124 | |
---|
| 125 | $result = pwg_query($query); |
---|
| 126 | while ($row = mysql_fetch_array($result)) |
---|
| 127 | { |
---|
| 128 | array_push($has_high_images, $row['id']); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | foreach ($files as $id => $file) |
---|
| 132 | { |
---|
[625] | 133 | $data = array(); |
---|
| 134 | $data['id'] = $id; |
---|
| 135 | $data['filesize'] = floor(filesize($file)/1024); |
---|
[486] | 136 | |
---|
| 137 | if ($image_size = @getimagesize($file)) |
---|
| 138 | { |
---|
[625] | 139 | $data['width'] = $image_size[0]; |
---|
| 140 | $data['height'] = $image_size[1]; |
---|
[486] | 141 | } |
---|
[1883] | 142 | |
---|
| 143 | if (in_array($id, $has_high_images)) |
---|
| 144 | { |
---|
| 145 | $high_file = dirname($file).'/pwg_high/'.basename($file); |
---|
| 146 | |
---|
| 147 | $data['high_filesize'] = floor(filesize($high_file)/1024); |
---|
| 148 | } |
---|
[486] | 149 | |
---|
| 150 | if ($conf['use_exif']) |
---|
| 151 | { |
---|
[858] | 152 | $exif = get_sync_exif_data($file); |
---|
[486] | 153 | } |
---|
| 154 | |
---|
| 155 | if ($conf['use_iptc']) |
---|
| 156 | { |
---|
[493] | 157 | $iptc = get_sync_iptc_data($file); |
---|
| 158 | if (count($iptc) > 0) |
---|
[486] | 159 | { |
---|
[493] | 160 | foreach (array_keys($iptc) as $key) |
---|
| 161 | { |
---|
[1119] | 162 | if ($key == 'keywords' or $key == 'tags') |
---|
| 163 | { |
---|
| 164 | if (!isset($tags_of[$id])) |
---|
| 165 | { |
---|
| 166 | $tags_of[$id] = array(); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | foreach (explode(',', $iptc[$key]) as $tag_name) |
---|
| 170 | { |
---|
| 171 | array_push( |
---|
| 172 | $tags_of[$id], |
---|
| 173 | tag_id_from_tag_name($tag_name) |
---|
| 174 | ); |
---|
| 175 | } |
---|
| 176 | } |
---|
[493] | 177 | } |
---|
[486] | 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
[625] | 181 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
[486] | 182 | |
---|
[625] | 183 | array_push($datas, $data); |
---|
[486] | 184 | } |
---|
| 185 | |
---|
[625] | 186 | if (count($datas) > 0) |
---|
[486] | 187 | { |
---|
[903] | 188 | $update_fields = |
---|
| 189 | array( |
---|
| 190 | 'filesize', |
---|
| 191 | 'width', |
---|
| 192 | 'height', |
---|
[1883] | 193 | 'high_filesize', |
---|
[903] | 194 | 'date_metadata_update' |
---|
| 195 | ); |
---|
| 196 | |
---|
[702] | 197 | if ($conf['use_exif']) |
---|
| 198 | { |
---|
[903] | 199 | $update_fields = |
---|
| 200 | array_merge( |
---|
| 201 | $update_fields, |
---|
| 202 | array_keys($conf['use_exif_mapping']) |
---|
| 203 | ); |
---|
[702] | 204 | } |
---|
[903] | 205 | |
---|
[702] | 206 | if ($conf['use_iptc']) |
---|
| 207 | { |
---|
[903] | 208 | $update_fields = |
---|
| 209 | array_merge( |
---|
| 210 | $update_fields, |
---|
[1119] | 211 | array_diff( |
---|
| 212 | array_keys($conf['use_iptc_mapping']), |
---|
| 213 | array('tags', 'keywords') |
---|
| 214 | ) |
---|
[903] | 215 | ); |
---|
[702] | 216 | } |
---|
[858] | 217 | |
---|
[1127] | 218 | mass_updates( |
---|
| 219 | IMAGES_TABLE, |
---|
[903] | 220 | array( |
---|
| 221 | 'primary' => array('id'), |
---|
| 222 | 'update' => array_unique($update_fields) |
---|
[1127] | 223 | ), |
---|
| 224 | $datas |
---|
| 225 | ); |
---|
[486] | 226 | } |
---|
[1119] | 227 | |
---|
[1127] | 228 | set_tags_of($tags_of); |
---|
[486] | 229 | } |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | * returns an array associating element id (images.id) with its complete |
---|
| 233 | * path in the filesystem |
---|
| 234 | * |
---|
| 235 | * @param int id_uppercat |
---|
[1029] | 236 | * @param int site_id |
---|
[486] | 237 | * @param boolean recursive ? |
---|
| 238 | * @param boolean only newly added files ? |
---|
| 239 | * @return array |
---|
| 240 | */ |
---|
[1029] | 241 | function get_filelist($category_id = '', $site_id=1, $recursive = false, |
---|
| 242 | $only_new = false) |
---|
[486] | 243 | { |
---|
[657] | 244 | // filling $cat_ids : all categories required |
---|
| 245 | $cat_ids = array(); |
---|
[486] | 246 | |
---|
| 247 | $query = ' |
---|
[657] | 248 | SELECT id |
---|
[582] | 249 | FROM '.CATEGORIES_TABLE.' |
---|
[1029] | 250 | WHERE site_id = '.$site_id.' |
---|
[582] | 251 | AND dir IS NOT NULL'; |
---|
[486] | 252 | if (is_numeric($category_id)) |
---|
| 253 | { |
---|
| 254 | if ($recursive) |
---|
| 255 | { |
---|
| 256 | $query.= ' |
---|
[582] | 257 | AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\' |
---|
[486] | 258 | '; |
---|
| 259 | } |
---|
| 260 | else |
---|
| 261 | { |
---|
| 262 | $query.= ' |
---|
[582] | 263 | AND id = '.$category_id.' |
---|
[486] | 264 | '; |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | $query.= ' |
---|
| 268 | ;'; |
---|
[587] | 269 | $result = pwg_query($query); |
---|
[486] | 270 | while ($row = mysql_fetch_array($result)) |
---|
| 271 | { |
---|
[657] | 272 | array_push($cat_ids, $row['id']); |
---|
[486] | 273 | } |
---|
| 274 | |
---|
[657] | 275 | if (count($cat_ids) == 0) |
---|
[590] | 276 | { |
---|
| 277 | return array(); |
---|
| 278 | } |
---|
| 279 | |
---|
[657] | 280 | $files = array(); |
---|
[486] | 281 | |
---|
| 282 | $query = ' |
---|
[657] | 283 | SELECT id, path |
---|
[486] | 284 | FROM '.IMAGES_TABLE.' |
---|
[1121] | 285 | WHERE storage_category_id IN ('.implode(',', $cat_ids).')'; |
---|
[486] | 286 | if ($only_new) |
---|
| 287 | { |
---|
| 288 | $query.= ' |
---|
| 289 | AND date_metadata_update IS NULL |
---|
| 290 | '; |
---|
| 291 | } |
---|
| 292 | $query.= ' |
---|
| 293 | ;'; |
---|
[587] | 294 | $result = pwg_query($query); |
---|
[486] | 295 | while ($row = mysql_fetch_array($result)) |
---|
| 296 | { |
---|
[657] | 297 | $files[$row['id']] = $row['path']; |
---|
[486] | 298 | } |
---|
| 299 | |
---|
| 300 | return $files; |
---|
| 301 | } |
---|
| 302 | ?> |
---|