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