source: trunk/admin/site_reader_remote.php @ 2585

Last change on this file since 2585 was 2299, checked in by plg, 17 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
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// +-----------------------------------------------------------------------+
23
24
25// provides data for site synchronization from a remote listing.xml
26class RemoteSiteReader
27{
28
29var $site_url;
30var $listing_url;
31var $site_dirs;
32var $site_files;
33var $insert_attributes;
34var $metadata_attributes;
35
36function 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 */
64function open()
65{
66  global $errors;
67
68  if (@fopen($this->listing_url, 'r'))
69  {
70    $this->site_dirs = array();
71    $this->site_files = array();
72    $xml_content = getXmlCode($this->listing_url);
73    $info_xml_element = getChild($xml_content, 'informations');
74    if (getAttribute($info_xml_element , 'phpwg_version') != PHPWG_VERSION)
75    {
76      array_push(
77        $errors,
78        array(
79          'path' => $this->listing_url,
80          'type' => 'PWG-ERROR-VERSION'
81          )
82        );
83
84      return false;
85    }
86
87    $additional_metadata = getAttribute($info_xml_element, 'metadata');
88
89    if ($additional_metadata != '')
90    {
91      $this->metadata_attributes = array_merge(
92        $this->metadata_attributes,
93        explode(',', $additional_metadata)
94        );
95    }
96   
97    $this->build_structure($xml_content, '', 0);
98
99    return true;
100  }
101  else
102  {
103    array_push(
104      $errors,
105      array(
106        'path' => $this->listing_url,
107        'type' => 'PWG-ERROR-NOLISTING'
108        )
109      );
110
111    return false;
112  }
113}
114
115// retrieve xml sub-directories fulldirs
116function 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;
122    if ($full_dir != $basedir
123        and strpos($full_dir, $basedir) === 0
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 */
138function get_elements($path)
139{
140  $elements = array();
141  foreach ( $this->site_dirs as $dir=>$files)
142  {
143    $full_dir = $this->site_url . $dir;
144    if (strpos($full_dir, $path) === 0)
145    {
146      foreach ($files as $file)
147      {
148        $data = $this->get_element_attributes(
149          $file,
150          $this->insert_attributes
151          );
152        $elements[$file] = $data;
153      }
154    }
155  }
156
157  return $elements;
158}
159
160// returns the name of the attributes that are supported for
161// files update/synchronization
162function get_update_attributes()
163{
164  return $this->update_attributes;
165}
166
167function get_element_update_attributes($file)
168{
169  return $this->get_element_attributes(
170    $file,
171    $this->update_attributes
172    );
173}
174
175// returns the name of the attributes that are supported for
176// metadata update/synchronization according to listing.xml
177function get_metadata_attributes()
178{
179  return $this->metadata_attributes;
180}
181
182// returns a hash of attributes (metadata+width,...) for file
183function get_element_metadata($file, $has_high = false)
184{
185  return $this->get_element_attributes(
186    $file,
187    $this->metadata_attributes
188    );
189}
190
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*/
198function get_element_attributes($file, $attributes)
199{
200  $xml_element = $this->site_files[$file];
201  if (!isset($xml_element))
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
218function 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  {
235    $xml_elements = getChildren(
236      getChild($xml_content, 'root'),
237      'element'
238      );
239    foreach ($xml_elements as $xml_element)
240    {
241      $path = getAttribute($xml_element, 'path');
242      $this->site_files[$path] = $xml_element;
243      array_push($this->site_dirs[$basedir], $path);
244    }
245  }
246}
247
248}
249
250?>
Note: See TracBrowser for help on using the repository browser.