[486] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[675] | 5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[486] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[486] | 8 | // | file : $RCSfile$ |
---|
| 9 | // | last update : $Date: 2005-09-14 21:57:05 +0000 (Wed, 14 Sep 2005) $ |
---|
| 10 | // | last modifier : $Author: plg $ |
---|
| 11 | // | revision : $Revision: 862 $ |
---|
| 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 | { |
---|
| 53 | // keywords separator is the comma, nothing else. Allowed characters in |
---|
| 54 | // keywords : [A-Za-z0-9], "-" and "_". All other characters will be |
---|
| 55 | // considered as separators |
---|
| 56 | $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']); |
---|
| 57 | $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']); |
---|
| 58 | } |
---|
| 59 | |
---|
[493] | 60 | return $iptc; |
---|
[486] | 61 | } |
---|
| 62 | |
---|
[858] | 63 | function get_sync_exif_data($file) |
---|
| 64 | { |
---|
| 65 | global $conf, $page; |
---|
| 66 | |
---|
| 67 | $exif = get_exif_data($file, $conf['use_exif_mapping']); |
---|
| 68 | |
---|
| 69 | foreach ($exif as $pwg_key => $value) |
---|
| 70 | { |
---|
| 71 | if (in_array($pwg_key, $page['datefields'])) |
---|
| 72 | { |
---|
| 73 | if (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches)) |
---|
| 74 | { |
---|
| 75 | $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | return $exif; |
---|
| 81 | } |
---|
| 82 | |
---|
[486] | 83 | function update_metadata($files) |
---|
| 84 | { |
---|
| 85 | global $conf; |
---|
[582] | 86 | |
---|
| 87 | if (!defined('CURRENT_DATE')) |
---|
| 88 | { |
---|
[624] | 89 | define('CURRENT_DATE', date('Y-m-d')); |
---|
[582] | 90 | } |
---|
| 91 | |
---|
[625] | 92 | $datas = array(); |
---|
[486] | 93 | |
---|
| 94 | foreach ($files as $id => $file) |
---|
| 95 | { |
---|
[625] | 96 | $data = array(); |
---|
| 97 | $data['id'] = $id; |
---|
| 98 | $data['filesize'] = floor(filesize($file)/1024); |
---|
[486] | 99 | |
---|
| 100 | if ($image_size = @getimagesize($file)) |
---|
| 101 | { |
---|
[625] | 102 | $data['width'] = $image_size[0]; |
---|
| 103 | $data['height'] = $image_size[1]; |
---|
[486] | 104 | } |
---|
| 105 | |
---|
| 106 | if ($conf['use_exif']) |
---|
| 107 | { |
---|
[858] | 108 | $exif = get_sync_exif_data($file); |
---|
| 109 | |
---|
| 110 | if (count($exif) > 0) |
---|
[738] | 111 | { |
---|
[858] | 112 | foreach (array_keys($exif) as $key) |
---|
[486] | 113 | { |
---|
[858] | 114 | $data[$key] = addslashes($exif[$key]); |
---|
[486] | 115 | } |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | if ($conf['use_iptc']) |
---|
| 120 | { |
---|
[493] | 121 | $iptc = get_sync_iptc_data($file); |
---|
| 122 | if (count($iptc) > 0) |
---|
[486] | 123 | { |
---|
[493] | 124 | foreach (array_keys($iptc) as $key) |
---|
| 125 | { |
---|
[730] | 126 | $data[$key] = addslashes($iptc[$key]); |
---|
[493] | 127 | } |
---|
[486] | 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
[625] | 131 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
[486] | 132 | |
---|
[625] | 133 | array_push($datas, $data); |
---|
[486] | 134 | } |
---|
| 135 | |
---|
[625] | 136 | if (count($datas) > 0) |
---|
[486] | 137 | { |
---|
[702] | 138 | $update_fields = array('filesize','width','height','date_metadata_update'); |
---|
| 139 | if ($conf['use_exif']) |
---|
| 140 | { |
---|
| 141 | array_push($update_fields, 'date_creation'); |
---|
| 142 | } |
---|
| 143 | if ($conf['use_iptc']) |
---|
| 144 | { |
---|
| 145 | $update_fields = array_merge($update_fields, |
---|
| 146 | array_keys($conf['use_iptc_mapping'])); |
---|
| 147 | } |
---|
[858] | 148 | |
---|
[625] | 149 | $fields = array('primary' => array('id'), |
---|
[702] | 150 | 'update' => array_unique($update_fields)); |
---|
[625] | 151 | mass_updates(IMAGES_TABLE, $fields, $datas); |
---|
[486] | 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * returns an array associating element id (images.id) with its complete |
---|
| 157 | * path in the filesystem |
---|
| 158 | * |
---|
| 159 | * @param int id_uppercat |
---|
| 160 | * @param boolean recursive ? |
---|
| 161 | * @param boolean only newly added files ? |
---|
| 162 | * @return array |
---|
| 163 | */ |
---|
| 164 | function get_filelist($category_id = '', $recursive = false, $only_new = false) |
---|
| 165 | { |
---|
[657] | 166 | // filling $cat_ids : all categories required |
---|
| 167 | $cat_ids = array(); |
---|
[486] | 168 | |
---|
| 169 | $query = ' |
---|
[657] | 170 | SELECT id |
---|
[582] | 171 | FROM '.CATEGORIES_TABLE.' |
---|
| 172 | WHERE site_id = 1 |
---|
| 173 | AND dir IS NOT NULL'; |
---|
[486] | 174 | if (is_numeric($category_id)) |
---|
| 175 | { |
---|
| 176 | if ($recursive) |
---|
| 177 | { |
---|
| 178 | $query.= ' |
---|
[582] | 179 | AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\' |
---|
[486] | 180 | '; |
---|
| 181 | } |
---|
| 182 | else |
---|
| 183 | { |
---|
| 184 | $query.= ' |
---|
[582] | 185 | AND id = '.$category_id.' |
---|
[486] | 186 | '; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | $query.= ' |
---|
| 190 | ;'; |
---|
[587] | 191 | $result = pwg_query($query); |
---|
[486] | 192 | while ($row = mysql_fetch_array($result)) |
---|
| 193 | { |
---|
[657] | 194 | array_push($cat_ids, $row['id']); |
---|
[486] | 195 | } |
---|
| 196 | |
---|
[657] | 197 | if (count($cat_ids) == 0) |
---|
[590] | 198 | { |
---|
| 199 | return array(); |
---|
| 200 | } |
---|
| 201 | |
---|
[657] | 202 | $files = array(); |
---|
[486] | 203 | |
---|
| 204 | $query = ' |
---|
[657] | 205 | SELECT id, path |
---|
[486] | 206 | FROM '.IMAGES_TABLE.' |
---|
[657] | 207 | WHERE storage_category_id IN ('.implode(',', $cat_ids).')'; |
---|
[486] | 208 | if ($only_new) |
---|
| 209 | { |
---|
| 210 | $query.= ' |
---|
| 211 | AND date_metadata_update IS NULL |
---|
| 212 | '; |
---|
| 213 | } |
---|
| 214 | $query.= ' |
---|
| 215 | ;'; |
---|
[587] | 216 | $result = pwg_query($query); |
---|
[486] | 217 | while ($row = mysql_fetch_array($result)) |
---|
| 218 | { |
---|
[657] | 219 | $files[$row['id']] = $row['path']; |
---|
[486] | 220 | } |
---|
| 221 | |
---|
| 222 | return $files; |
---|
| 223 | } |
---|
| 224 | ?> |
---|