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