source: trunk/admin/site_reader_remote.php @ 1058

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

remake of Remote sites and synchronize: final integration and old code cleanup

fix: xml getAttribute always decodes html entities and added encodeAttribute
function

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