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