[1058] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[2297] | 3 | // | Piwigo - a PHP based picture gallery | |
---|
| 4 | // +-----------------------------------------------------------------------+ |
---|
[5196] | 5 | // | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | { |
---|
[2902] | 51 | $this->listing_url = $this->site_url.'listing.xml'; |
---|
[1107] | 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 | |
---|
[2902] | 68 | if ($xml_content = getXmlCode($this->listing_url)) |
---|
[1058] | 69 | { |
---|
| 70 | $this->site_dirs = array(); |
---|
| 71 | $this->site_files = array(); |
---|
| 72 | $info_xml_element = getChild($xml_content, 'informations'); |
---|
[1064] | 73 | if (getAttribute($info_xml_element , 'phpwg_version') != PHPWG_VERSION) |
---|
[1058] | 74 | { |
---|
[1064] | 75 | array_push( |
---|
| 76 | $errors, |
---|
| 77 | array( |
---|
[1107] | 78 | 'path' => $this->listing_url, |
---|
[1064] | 79 | 'type' => 'PWG-ERROR-VERSION' |
---|
| 80 | ) |
---|
| 81 | ); |
---|
[1107] | 82 | |
---|
[1058] | 83 | return false; |
---|
| 84 | } |
---|
[1064] | 85 | |
---|
[1883] | 86 | $additional_metadata = getAttribute($info_xml_element, 'metadata'); |
---|
[1107] | 87 | |
---|
[1883] | 88 | if ($additional_metadata != '') |
---|
| 89 | { |
---|
| 90 | $this->metadata_attributes = array_merge( |
---|
| 91 | $this->metadata_attributes, |
---|
| 92 | explode(',', $additional_metadata) |
---|
| 93 | ); |
---|
| 94 | } |
---|
| 95 | |
---|
[1058] | 96 | $this->build_structure($xml_content, '', 0); |
---|
[1107] | 97 | |
---|
[1058] | 98 | return true; |
---|
| 99 | } |
---|
| 100 | else |
---|
| 101 | { |
---|
[1064] | 102 | array_push( |
---|
| 103 | $errors, |
---|
| 104 | array( |
---|
[1107] | 105 | 'path' => $this->listing_url, |
---|
[1064] | 106 | 'type' => 'PWG-ERROR-NOLISTING' |
---|
| 107 | ) |
---|
| 108 | ); |
---|
| 109 | |
---|
[1058] | 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; |
---|
[1064] | 121 | if ($full_dir != $basedir |
---|
| 122 | and strpos($full_dir, $basedir) === 0 |
---|
[1058] | 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; |
---|
[1064] | 143 | if (strpos($full_dir, $path) === 0) |
---|
[1058] | 144 | { |
---|
[1064] | 145 | foreach ($files as $file) |
---|
[1058] | 146 | { |
---|
[1064] | 147 | $data = $this->get_element_attributes( |
---|
| 148 | $file, |
---|
| 149 | $this->insert_attributes |
---|
| 150 | ); |
---|
[1058] | 151 | $elements[$file] = $data; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | return $elements; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | // returns the name of the attributes that are supported for |
---|
[1204] | 160 | // files update/synchronization |
---|
[1058] | 161 | function get_update_attributes() |
---|
| 162 | { |
---|
| 163 | return $this->update_attributes; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | function get_element_update_attributes($file) |
---|
| 167 | { |
---|
[1064] | 168 | return $this->get_element_attributes( |
---|
| 169 | $file, |
---|
| 170 | $this->update_attributes |
---|
| 171 | ); |
---|
[1058] | 172 | } |
---|
| 173 | |
---|
[1204] | 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 |
---|
[1883] | 182 | function get_element_metadata($file, $has_high = false) |
---|
[1204] | 183 | { |
---|
| 184 | return $this->get_element_attributes( |
---|
| 185 | $file, |
---|
| 186 | $this->metadata_attributes |
---|
| 187 | ); |
---|
| 188 | } |
---|
| 189 | |
---|
[1058] | 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]; |
---|
[1064] | 200 | if (!isset($xml_element)) |
---|
[1058] | 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 | { |
---|
[1064] | 234 | $xml_elements = getChildren( |
---|
| 235 | getChild($xml_content, 'root'), |
---|
| 236 | 'element' |
---|
| 237 | ); |
---|
[1058] | 238 | foreach ($xml_elements as $xml_element) |
---|
| 239 | { |
---|
| 240 | $path = getAttribute($xml_element, 'path'); |
---|
| 241 | $this->site_files[$path] = $xml_element; |
---|
[1064] | 242 | array_push($this->site_dirs[$basedir], $path); |
---|
[1058] | 243 | } |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | } |
---|
| 248 | |
---|
[1903] | 249 | ?> |
---|