source: trunk/admin/site_reader_remote.php @ 1064

Last change on this file since 1064 was 1064, checked in by plg, 18 years ago

new feature: source/destination links between categories. Will we keep this
feature? Code is complicated and very few people will understand how it
works...

modification: #images.storage_category_id replaced by
#image_category.is_storage

improvement: many code refactoring to improve readibility

improvement: virtual category creation code was moved to a dedicated
function in order to be called from admin/cat_list.php and
admin/cat_modify.php (create a new destination category)

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