1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
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 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | |
---|
25 | // provides data for site synchronization from a remote listing.xml |
---|
26 | class RemoteSiteReader |
---|
27 | { |
---|
28 | |
---|
29 | var $site_url; |
---|
30 | var $listing_url; |
---|
31 | var $site_dirs; |
---|
32 | var $site_files; |
---|
33 | var $insert_attributes; |
---|
34 | var $metadata_attributes; |
---|
35 | |
---|
36 | function RemoteSiteReader($url, $listing_url) |
---|
37 | { |
---|
38 | $this->site_url = $url; |
---|
39 | $this->insert_attributes = array( |
---|
40 | 'tn_ext', |
---|
41 | ); |
---|
42 | $this->update_attributes = array( |
---|
43 | 'tn_ext', 'representative_ext', 'has_high', |
---|
44 | ); |
---|
45 | $this->metadata_attributes = array( |
---|
46 | 'filesize', 'width', 'height', 'high_filesize' |
---|
47 | ); |
---|
48 | |
---|
49 | if (!isset($listing_url)) |
---|
50 | { |
---|
51 | $this->listing_url = $this->site_url.'listing.xml'; |
---|
52 | } |
---|
53 | else |
---|
54 | { |
---|
55 | $this->listing_url = $listing_url; |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Is this remote site ok ? |
---|
61 | * |
---|
62 | * @return true on success, false otherwise |
---|
63 | */ |
---|
64 | function open() |
---|
65 | { |
---|
66 | global $errors; |
---|
67 | |
---|
68 | if ($xml_content = getXmlCode($this->listing_url)) |
---|
69 | { |
---|
70 | $this->site_dirs = array(); |
---|
71 | $this->site_files = array(); |
---|
72 | $info_xml_element = getChild($xml_content, 'informations'); |
---|
73 | if (getAttribute($info_xml_element , 'phpwg_version') != PHPWG_VERSION) |
---|
74 | { |
---|
75 | array_push( |
---|
76 | $errors, |
---|
77 | array( |
---|
78 | 'path' => $this->listing_url, |
---|
79 | 'type' => 'PWG-ERROR-VERSION' |
---|
80 | ) |
---|
81 | ); |
---|
82 | |
---|
83 | return false; |
---|
84 | } |
---|
85 | |
---|
86 | $additional_metadata = getAttribute($info_xml_element, 'metadata'); |
---|
87 | |
---|
88 | if ($additional_metadata != '') |
---|
89 | { |
---|
90 | $this->metadata_attributes = array_merge( |
---|
91 | $this->metadata_attributes, |
---|
92 | explode(',', $additional_metadata) |
---|
93 | ); |
---|
94 | } |
---|
95 | |
---|
96 | $this->build_structure($xml_content, '', 0); |
---|
97 | |
---|
98 | return true; |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | array_push( |
---|
103 | $errors, |
---|
104 | array( |
---|
105 | 'path' => $this->listing_url, |
---|
106 | 'type' => 'PWG-ERROR-NOLISTING' |
---|
107 | ) |
---|
108 | ); |
---|
109 | |
---|
110 | return false; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | // retrieve xml sub-directories fulldirs |
---|
115 | function get_full_directories($basedir) |
---|
116 | { |
---|
117 | $dirs = array(); |
---|
118 | foreach ( array_keys($this->site_dirs) as $dir) |
---|
119 | { |
---|
120 | $full_dir = $this->site_url . $dir; |
---|
121 | if ($full_dir != $basedir |
---|
122 | and strpos($full_dir, $basedir) === 0 |
---|
123 | ) |
---|
124 | { |
---|
125 | array_push($dirs, $full_dir); |
---|
126 | } |
---|
127 | } |
---|
128 | return $dirs; |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Returns a hash with all elements (images and files) inside the full $path |
---|
133 | * according to listing.xml |
---|
134 | * @param string $path recurse in this directory only |
---|
135 | * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... ) |
---|
136 | */ |
---|
137 | function get_elements($path) |
---|
138 | { |
---|
139 | $elements = array(); |
---|
140 | foreach ( $this->site_dirs as $dir=>$files) |
---|
141 | { |
---|
142 | $full_dir = $this->site_url . $dir; |
---|
143 | if (strpos($full_dir, $path) === 0) |
---|
144 | { |
---|
145 | foreach ($files as $file) |
---|
146 | { |
---|
147 | $data = $this->get_element_attributes( |
---|
148 | $file, |
---|
149 | $this->insert_attributes |
---|
150 | ); |
---|
151 | $elements[$file] = $data; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | return $elements; |
---|
157 | } |
---|
158 | |
---|
159 | // returns the name of the attributes that are supported for |
---|
160 | // files update/synchronization |
---|
161 | function get_update_attributes() |
---|
162 | { |
---|
163 | return $this->update_attributes; |
---|
164 | } |
---|
165 | |
---|
166 | function get_element_update_attributes($file) |
---|
167 | { |
---|
168 | return $this->get_element_attributes( |
---|
169 | $file, |
---|
170 | $this->update_attributes |
---|
171 | ); |
---|
172 | } |
---|
173 | |
---|
174 | // returns the name of the attributes that are supported for |
---|
175 | // metadata update/synchronization according to listing.xml |
---|
176 | function get_metadata_attributes() |
---|
177 | { |
---|
178 | return $this->metadata_attributes; |
---|
179 | } |
---|
180 | |
---|
181 | // returns a hash of attributes (metadata+width,...) for file |
---|
182 | function get_element_metadata($file, $has_high = false) |
---|
183 | { |
---|
184 | return $this->get_element_attributes( |
---|
185 | $file, |
---|
186 | $this->metadata_attributes |
---|
187 | ); |
---|
188 | } |
---|
189 | |
---|
190 | //-------------------------------------------------- private functions -------- |
---|
191 | /** |
---|
192 | * Returns a hash of image/file attributes |
---|
193 | * @param string $file fully qualified file name |
---|
194 | * @param array $attributes specifies which attributes to retrieve |
---|
195 | * returned |
---|
196 | */ |
---|
197 | function get_element_attributes($file, $attributes) |
---|
198 | { |
---|
199 | $xml_element = $this->site_files[$file]; |
---|
200 | if (!isset($xml_element)) |
---|
201 | { |
---|
202 | return null; |
---|
203 | } |
---|
204 | $data = array(); |
---|
205 | foreach($attributes as $att) |
---|
206 | { |
---|
207 | if (getAttribute($xml_element, $att) != '') |
---|
208 | { |
---|
209 | $val = getAttribute($xml_element, $att); |
---|
210 | $data[$att] = addslashes($val); |
---|
211 | } |
---|
212 | } |
---|
213 | return $data; |
---|
214 | } |
---|
215 | |
---|
216 | // recursively parse the xml_content for later usage |
---|
217 | function build_structure($xml_content, $basedir, $level) |
---|
218 | { |
---|
219 | $temp_dirs = getChildren($xml_content, 'dir'.$level); |
---|
220 | foreach ($temp_dirs as $temp_dir) |
---|
221 | { |
---|
222 | $dir_name = $basedir; |
---|
223 | if ($dir_name != '' ) |
---|
224 | { |
---|
225 | $dir_name .= '/'; |
---|
226 | } |
---|
227 | $dir_name .= getAttribute($temp_dir, 'name'); |
---|
228 | $this->site_dirs[ $dir_name ] = array(); |
---|
229 | $this->build_structure($temp_dir, $dir_name, $level+1); |
---|
230 | } |
---|
231 | |
---|
232 | if ($basedir != '') |
---|
233 | { |
---|
234 | $xml_elements = getChildren( |
---|
235 | getChild($xml_content, 'root'), |
---|
236 | 'element' |
---|
237 | ); |
---|
238 | foreach ($xml_elements as $xml_element) |
---|
239 | { |
---|
240 | $path = getAttribute($xml_element, 'path'); |
---|
241 | $this->site_files[$path] = $xml_element; |
---|
242 | array_push($this->site_dirs[$basedir], $path); |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | } |
---|
248 | |
---|
249 | ?> |
---|