| 1 | <?php |
|---|
| 2 | // +-----------------------------------------------------------------------+ |
|---|
| 3 | // | PhpWebGallery - a PHP based picture gallery | |
|---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
|---|
| 5 | // | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net | |
|---|
| 6 | // +-----------------------------------------------------------------------+ |
|---|
| 7 | // | branch : BSF (Best So Far) |
|---|
| 8 | // | file : $Id$ |
|---|
| 9 | // | last update : $Date$ |
|---|
| 10 | // | last modifier : $Author$ |
|---|
| 11 | // | revision : $Revision$ |
|---|
| 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 | |
|---|
| 28 | include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php'); |
|---|
| 29 | |
|---|
| 30 | $page['datefields'] = array('date_creation', 'date_available'); |
|---|
| 31 | |
|---|
| 32 | function get_sync_iptc_data($file) |
|---|
| 33 | { |
|---|
| 34 | global $conf, $page; |
|---|
| 35 | |
|---|
| 36 | $map = $conf['use_iptc_mapping']; |
|---|
| 37 | |
|---|
| 38 | $iptc = get_iptc_data($file, $map); |
|---|
| 39 | |
|---|
| 40 | foreach ($iptc as $pwg_key => $value) |
|---|
| 41 | { |
|---|
| 42 | if (in_array($pwg_key, $page['datefields'])) |
|---|
| 43 | { |
|---|
| 44 | if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches)) |
|---|
| 45 | { |
|---|
| 46 | $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | if (isset($iptc['keywords'])) |
|---|
| 52 | { |
|---|
| 53 | // official keywords separator is the comma |
|---|
| 54 | $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']); |
|---|
| 55 | $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']); |
|---|
| 56 | |
|---|
| 57 | $iptc['keywords'] = implode( |
|---|
| 58 | ',', |
|---|
| 59 | array_unique( |
|---|
| 60 | explode( |
|---|
| 61 | ',', |
|---|
| 62 | $iptc['keywords'] |
|---|
| 63 | ) |
|---|
| 64 | ) |
|---|
| 65 | ); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | foreach ($iptc as $pwg_key => $value) |
|---|
| 69 | { |
|---|
| 70 | $iptc[$pwg_key] = addslashes($iptc[$pwg_key]); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | return $iptc; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 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 | } |
|---|
| 91 | $exif[$pwg_key] = addslashes($exif[$pwg_key]); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | return $exif; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | function update_metadata($files) |
|---|
| 98 | { |
|---|
| 99 | global $conf; |
|---|
| 100 | |
|---|
| 101 | if (!defined('CURRENT_DATE')) |
|---|
| 102 | { |
|---|
| 103 | define('CURRENT_DATE', date('Y-m-d')); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | $datas = array(); |
|---|
| 107 | $tags_of = array(); |
|---|
| 108 | |
|---|
| 109 | foreach ($files as $id => $file) |
|---|
| 110 | { |
|---|
| 111 | $data = array(); |
|---|
| 112 | $data['id'] = $id; |
|---|
| 113 | $data['filesize'] = floor(filesize($file)/1024); |
|---|
| 114 | |
|---|
| 115 | if ($image_size = @getimagesize($file)) |
|---|
| 116 | { |
|---|
| 117 | $data['width'] = $image_size[0]; |
|---|
| 118 | $data['height'] = $image_size[1]; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | if ($conf['use_exif']) |
|---|
| 122 | { |
|---|
| 123 | $exif = get_sync_exif_data($file); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | if ($conf['use_iptc']) |
|---|
| 127 | { |
|---|
| 128 | $iptc = get_sync_iptc_data($file); |
|---|
| 129 | if (count($iptc) > 0) |
|---|
| 130 | { |
|---|
| 131 | foreach (array_keys($iptc) as $key) |
|---|
| 132 | { |
|---|
| 133 | if ($key == 'keywords' or $key == 'tags') |
|---|
| 134 | { |
|---|
| 135 | if (!isset($tags_of[$id])) |
|---|
| 136 | { |
|---|
| 137 | $tags_of[$id] = array(); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | foreach (explode(',', $iptc[$key]) as $tag_name) |
|---|
| 141 | { |
|---|
| 142 | array_push( |
|---|
| 143 | $tags_of[$id], |
|---|
| 144 | tag_id_from_tag_name($tag_name) |
|---|
| 145 | ); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | $data['date_metadata_update'] = CURRENT_DATE; |
|---|
| 153 | |
|---|
| 154 | array_push($datas, $data); |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | if (count($datas) > 0) |
|---|
| 158 | { |
|---|
| 159 | $update_fields = |
|---|
| 160 | array( |
|---|
| 161 | 'filesize', |
|---|
| 162 | 'width', |
|---|
| 163 | 'height', |
|---|
| 164 | 'date_metadata_update' |
|---|
| 165 | ); |
|---|
| 166 | |
|---|
| 167 | if ($conf['use_exif']) |
|---|
| 168 | { |
|---|
| 169 | $update_fields = |
|---|
| 170 | array_merge( |
|---|
| 171 | $update_fields, |
|---|
| 172 | array_keys($conf['use_exif_mapping']) |
|---|
| 173 | ); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | if ($conf['use_iptc']) |
|---|
| 177 | { |
|---|
| 178 | $update_fields = |
|---|
| 179 | array_merge( |
|---|
| 180 | $update_fields, |
|---|
| 181 | array_diff( |
|---|
| 182 | array_keys($conf['use_iptc_mapping']), |
|---|
| 183 | array('tags', 'keywords') |
|---|
| 184 | ) |
|---|
| 185 | ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | mass_updates( |
|---|
| 189 | IMAGES_TABLE, |
|---|
| 190 | array( |
|---|
| 191 | 'primary' => array('id'), |
|---|
| 192 | 'update' => array_unique($update_fields) |
|---|
| 193 | ), |
|---|
| 194 | $datas |
|---|
| 195 | ); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | set_tags_of($tags_of); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /** |
|---|
| 202 | * returns an array associating element id (images.id) with its complete |
|---|
| 203 | * path in the filesystem |
|---|
| 204 | * |
|---|
| 205 | * @param int id_uppercat |
|---|
| 206 | * @param int site_id |
|---|
| 207 | * @param boolean recursive ? |
|---|
| 208 | * @param boolean only newly added files ? |
|---|
| 209 | * @return array |
|---|
| 210 | */ |
|---|
| 211 | function get_filelist($category_id = '', $site_id=1, $recursive = false, |
|---|
| 212 | $only_new = false) |
|---|
| 213 | { |
|---|
| 214 | // filling $cat_ids : all categories required |
|---|
| 215 | $cat_ids = array(); |
|---|
| 216 | |
|---|
| 217 | $query = ' |
|---|
| 218 | SELECT id |
|---|
| 219 | FROM '.CATEGORIES_TABLE.' |
|---|
| 220 | WHERE site_id = '.$site_id.' |
|---|
| 221 | AND dir IS NOT NULL'; |
|---|
| 222 | if (is_numeric($category_id)) |
|---|
| 223 | { |
|---|
| 224 | if ($recursive) |
|---|
| 225 | { |
|---|
| 226 | $query.= ' |
|---|
| 227 | AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\' |
|---|
| 228 | '; |
|---|
| 229 | } |
|---|
| 230 | else |
|---|
| 231 | { |
|---|
| 232 | $query.= ' |
|---|
| 233 | AND id = '.$category_id.' |
|---|
| 234 | '; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | $query.= ' |
|---|
| 238 | ;'; |
|---|
| 239 | $result = pwg_query($query); |
|---|
| 240 | while ($row = mysql_fetch_array($result)) |
|---|
| 241 | { |
|---|
| 242 | array_push($cat_ids, $row['id']); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | if (count($cat_ids) == 0) |
|---|
| 246 | { |
|---|
| 247 | return array(); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | $files = array(); |
|---|
| 251 | |
|---|
| 252 | $query = ' |
|---|
| 253 | SELECT id, path |
|---|
| 254 | FROM '.IMAGES_TABLE.' |
|---|
| 255 | WHERE storage_category_id IN ('.implode(',', $cat_ids).')'; |
|---|
| 256 | if ($only_new) |
|---|
| 257 | { |
|---|
| 258 | $query.= ' |
|---|
| 259 | AND date_metadata_update IS NULL |
|---|
| 260 | '; |
|---|
| 261 | } |
|---|
| 262 | $query.= ' |
|---|
| 263 | ;'; |
|---|
| 264 | $result = pwg_query($query); |
|---|
| 265 | while ($row = mysql_fetch_array($result)) |
|---|
| 266 | { |
|---|
| 267 | $files[$row['id']] = $row['path']; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | return $files; |
|---|
| 271 | } |
|---|
| 272 | ?> |
|---|