source: trunk/admin/site_reader_remote.php @ 1204

Last change on this file since 1204 was 1204, checked in by rvelices, 18 years ago

merge r1203 from branch-1_6 into trunk

bug 335: tn_ext, has_high and representative_ext are updated during file
synchronization and not metadata synchronization

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
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-04-18 23:46:53 +0000 (Tue, 18 Apr 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1204 $
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
30class RemoteSiteReader
31{
32
33var $site_url;
34var $listing_url;
35var $site_dirs;
36var $site_files;
37var $insert_attributes;
38var $metadata_attributes;
39
40function 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'
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 */
68function 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    $this->metadata_attributes = array_merge(
92      $this->metadata_attributes,
93      explode(',', getAttribute($info_xml_element, 'metadata'))
94      );
95
96    $this->build_structure($xml_content, '', 0);
97
98    return true;
99  }
100  else
101  {
102    array_push(
103      $errors,
104      array(
105        'path' => $this->listing_url,
106        'type' => 'PWG-ERROR-NOLISTING'
107        )
108      );
109
110    return false;
111  }
112}
113
114// retrieve xml sub-directories fulldirs
115function 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;
121    if ($full_dir != $basedir
122        and strpos($full_dir, $basedir) === 0
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 */
137function get_elements($path)
138{
139  $elements = array();
140  foreach ( $this->site_dirs as $dir=>$files)
141  {
142    $full_dir = $this->site_url . $dir;
143    if (strpos($full_dir, $path) === 0)
144    {
145      foreach ($files as $file)
146      {
147        $data = $this->get_element_attributes(
148          $file,
149          $this->insert_attributes
150          );
151        $elements[$file] = $data;
152      }
153    }
154  }
155
156  return $elements;
157}
158
159// returns the name of the attributes that are supported for
160// files update/synchronization
161function get_update_attributes()
162{
163  return $this->update_attributes;
164}
165
166function get_element_update_attributes($file)
167{
168  return $this->get_element_attributes(
169    $file,
170    $this->update_attributes
171    );
172}
173
174// returns the name of the attributes that are supported for
175// metadata update/synchronization according to listing.xml
176function get_metadata_attributes()
177{
178  return $this->metadata_attributes;
179}
180
181// returns a hash of attributes (metadata+width,...) for file
182function get_element_metadata($file)
183{
184  return $this->get_element_attributes(
185    $file,
186    $this->metadata_attributes
187    );
188}
189
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*/
197function get_element_attributes($file, $attributes)
198{
199  $xml_element = $this->site_files[$file];
200  if (!isset($xml_element))
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
217function 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  {
234    $xml_elements = getChildren(
235      getChild($xml_content, 'root'),
236      'element'
237      );
238    foreach ($xml_elements as $xml_element)
239    {
240      $path = getAttribute($xml_element, 'path');
241      $this->site_files[$path] = $xml_element;
242      array_push($this->site_dirs[$basedir], $path);
243    }
244  }
245}
246
247}
248
249?>
Note: See TracBrowser for help on using the repository browser.