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-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2005-02-08 21:56:12 +0000 (Tue, 08 Feb 2005) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 730 $ |
---|
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 | function get_sync_iptc_data($file) |
---|
31 | { |
---|
32 | global $conf; |
---|
33 | |
---|
34 | $map = $conf['use_iptc_mapping']; |
---|
35 | $datefields = array('date_creation', 'date_available'); |
---|
36 | |
---|
37 | $iptc = get_iptc_data($file, $map); |
---|
38 | |
---|
39 | foreach ($iptc as $pwg_key => $value) |
---|
40 | { |
---|
41 | if (in_array($pwg_key, $datefields)) |
---|
42 | { |
---|
43 | if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches)) |
---|
44 | { |
---|
45 | $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | if (isset($iptc['keywords'])) |
---|
51 | { |
---|
52 | // keywords separator is the comma, nothing else. Allowed characters in |
---|
53 | // keywords : [A-Za-z0-9], "-" and "_". All other characters will be |
---|
54 | // considered as separators |
---|
55 | $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']); |
---|
56 | $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']); |
---|
57 | } |
---|
58 | |
---|
59 | return $iptc; |
---|
60 | } |
---|
61 | |
---|
62 | function update_metadata($files) |
---|
63 | { |
---|
64 | global $conf; |
---|
65 | |
---|
66 | if (!defined('CURRENT_DATE')) |
---|
67 | { |
---|
68 | define('CURRENT_DATE', date('Y-m-d')); |
---|
69 | } |
---|
70 | |
---|
71 | $datas = array(); |
---|
72 | |
---|
73 | foreach ($files as $id => $file) |
---|
74 | { |
---|
75 | $data = array(); |
---|
76 | $data['id'] = $id; |
---|
77 | $data['filesize'] = floor(filesize($file)/1024); |
---|
78 | |
---|
79 | if ($image_size = @getimagesize($file)) |
---|
80 | { |
---|
81 | $data['width'] = $image_size[0]; |
---|
82 | $data['height'] = $image_size[1]; |
---|
83 | } |
---|
84 | |
---|
85 | if ($conf['use_exif']) |
---|
86 | { |
---|
87 | if ($exif = @read_exif_data($file)) |
---|
88 | { |
---|
89 | if (isset($exif['DateTime'])) |
---|
90 | { |
---|
91 | preg_match('/^(\d{4}).(\d{2}).(\d{2})/',$exif['DateTime'],$matches); |
---|
92 | $data['date_creation'] = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | if ($conf['use_iptc']) |
---|
98 | { |
---|
99 | $iptc = get_sync_iptc_data($file); |
---|
100 | if (count($iptc) > 0) |
---|
101 | { |
---|
102 | foreach (array_keys($iptc) as $key) |
---|
103 | { |
---|
104 | $data[$key] = addslashes($iptc[$key]); |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | $data['date_metadata_update'] = CURRENT_DATE; |
---|
110 | |
---|
111 | array_push($datas, $data); |
---|
112 | } |
---|
113 | |
---|
114 | if (count($datas) > 0) |
---|
115 | { |
---|
116 | $update_fields = array('filesize','width','height','date_metadata_update'); |
---|
117 | if ($conf['use_exif']) |
---|
118 | { |
---|
119 | array_push($update_fields, 'date_creation'); |
---|
120 | } |
---|
121 | if ($conf['use_iptc']) |
---|
122 | { |
---|
123 | $update_fields = array_merge($update_fields, |
---|
124 | array_keys($conf['use_iptc_mapping'])); |
---|
125 | } |
---|
126 | |
---|
127 | $fields = array('primary' => array('id'), |
---|
128 | 'update' => array_unique($update_fields)); |
---|
129 | mass_updates(IMAGES_TABLE, $fields, $datas); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * returns an array associating element id (images.id) with its complete |
---|
135 | * path in the filesystem |
---|
136 | * |
---|
137 | * @param int id_uppercat |
---|
138 | * @param boolean recursive ? |
---|
139 | * @param boolean only newly added files ? |
---|
140 | * @return array |
---|
141 | */ |
---|
142 | function get_filelist($category_id = '', $recursive = false, $only_new = false) |
---|
143 | { |
---|
144 | // filling $cat_ids : all categories required |
---|
145 | $cat_ids = array(); |
---|
146 | |
---|
147 | $query = ' |
---|
148 | SELECT id |
---|
149 | FROM '.CATEGORIES_TABLE.' |
---|
150 | WHERE site_id = 1 |
---|
151 | AND dir IS NOT NULL'; |
---|
152 | if (is_numeric($category_id)) |
---|
153 | { |
---|
154 | if ($recursive) |
---|
155 | { |
---|
156 | $query.= ' |
---|
157 | AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\' |
---|
158 | '; |
---|
159 | } |
---|
160 | else |
---|
161 | { |
---|
162 | $query.= ' |
---|
163 | AND id = '.$category_id.' |
---|
164 | '; |
---|
165 | } |
---|
166 | } |
---|
167 | $query.= ' |
---|
168 | ;'; |
---|
169 | $result = pwg_query($query); |
---|
170 | while ($row = mysql_fetch_array($result)) |
---|
171 | { |
---|
172 | array_push($cat_ids, $row['id']); |
---|
173 | } |
---|
174 | |
---|
175 | if (count($cat_ids) == 0) |
---|
176 | { |
---|
177 | return array(); |
---|
178 | } |
---|
179 | |
---|
180 | $files = array(); |
---|
181 | |
---|
182 | $query = ' |
---|
183 | SELECT id, path |
---|
184 | FROM '.IMAGES_TABLE.' |
---|
185 | WHERE storage_category_id IN ('.implode(',', $cat_ids).')'; |
---|
186 | if ($only_new) |
---|
187 | { |
---|
188 | $query.= ' |
---|
189 | AND date_metadata_update IS NULL |
---|
190 | '; |
---|
191 | } |
---|
192 | $query.= ' |
---|
193 | ;'; |
---|
194 | $result = pwg_query($query); |
---|
195 | while ($row = mysql_fetch_array($result)) |
---|
196 | { |
---|
197 | $files[$row['id']] = $row['path']; |
---|
198 | } |
---|
199 | |
---|
200 | return $files; |
---|
201 | } |
---|
202 | ?> |
---|