source: trunk/admin/site_reader_local.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 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// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | branch        : BSF (Best So Far)
29// | file          : $Id: site_reader_local.php 2297 2008-04-04 22:57:23Z plg $
30// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
31// | last modifier : $Author: plg $
32// | revision      : $Revision: 2297 $
33// +-----------------------------------------------------------------------+
34// | This program is free software; you can redistribute it and/or modify  |
35// | it under the terms of the GNU General Public License as published by  |
36// | the Free Software Foundation                                          |
37// |                                                                       |
38// | This program is distributed in the hope that it will be useful, but   |
39// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
40// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
41// | General Public License for more details.                              |
42// |                                                                       |
43// | You should have received a copy of the GNU General Public License     |
44// | along with this program; if not, write to the Free Software           |
45// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
46// | USA.                                                                  |
47// +-----------------------------------------------------------------------+
48
49// provides data for site synchronization from the local file system
50class LocalSiteReader
51{
52
53var $site_url;
54
55function LocalSiteReader($url)
56{
57  $this->site_url = $url;
58}
59
60/**
61 * Is this local site ok ?
62 *
63 * @return true on success, false otherwise
64 */
65function open()
66{
67  global $errors;
68
69  if (!is_dir($this->site_url))
70  {
71    array_push(
72      $errors,
73      array(
74        'path' => $this->site_url,
75        'type' => 'PWG-ERROR-NO-FS'
76        )
77      );
78
79    return false;
80  }
81
82  return true;
83}
84
85// retrieve file system sub-directories fulldirs
86function get_full_directories($basedir)
87{
88  $fs_fulldirs = get_fs_directories($basedir);
89  return $fs_fulldirs;
90}
91
92/**
93 * Returns an array with all file system files according to $conf['file_ext']
94 * and $conf['picture_ext']
95 * @param string $path recurse in this directory
96 * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... )
97 */
98function get_elements($path)
99{
100  global $conf;
101  if (!isset($conf['flip_file_ext']))
102  {
103    $conf['flip_file_ext'] = array_flip($conf['file_ext']);
104  }
105
106  $subdirs = array();
107  $fs = array();
108  if (is_dir($path) && $contents = opendir($path) )
109  {
110    while (($node = readdir($contents)) !== false)
111    {
112      if (is_file($path.'/'.$node))
113      {
114        $extension = get_extension($node);
115        $filename_wo_ext = get_filename_wo_extension($node);
116
117        if ( isset($conf['flip_file_ext'][$extension]) )
118        {
119          $tn_ext = $this->get_tn_ext($path, $filename_wo_ext);
120          $fs[ $path.'/'.$node ] = array(
121            'tn_ext' => $tn_ext,
122            );
123        }
124      }
125      elseif (is_dir($path.'/'.$node)
126               and $node != '.'
127               and $node != '..'
128               and $node != 'pwg_high'
129               and $node != 'pwg_representative'
130               and $node != 'thumbnail' )
131      {
132        array_push($subdirs, $node);
133      }
134    } //end while readdir
135    closedir($contents);
136
137    foreach ($subdirs as $subdir)
138    {
139      $tmp_fs = $this->get_elements($path.'/'.$subdir);
140      $fs = array_merge($fs, $tmp_fs);
141    }
142  } //end if is_dir
143  return $fs;
144}
145
146// returns the name of the attributes that are supported for
147// files update/synchronization
148function get_update_attributes()
149{
150  return array('tn_ext', 'has_high', 'representative_ext');
151}
152
153function get_element_update_attributes($file)
154{
155  global $conf;
156  $data = array();
157
158  $filename = basename($file);
159  $dirname = dirname($file);
160  $filename_wo_ext = get_filename_wo_extension($filename);
161  $extension = get_extension($filename);
162
163  $data['tn_ext'] = $this->get_tn_ext($dirname, $filename_wo_ext);
164  $data['has_high'] = $this->get_has_high($dirname, $filename);
165
166  if ( !isset($conf['flip_picture_ext'][$extension]) )
167  {
168    $data['representative_ext'] = $this->get_representative_ext(
169        $dirname, $filename_wo_ext
170      );
171  }
172  return $data;
173}
174
175// returns the name of the attributes that are supported for
176// metadata update/synchronization according to configuration
177function get_metadata_attributes()
178{
179  global $conf;
180
181  $update_fields = array('filesize', 'width', 'height', 'high_filesize');
182
183  if ($conf['use_exif'])
184  {
185    $update_fields =
186      array_merge(
187        $update_fields,
188        array_keys($conf['use_exif_mapping'])
189        );
190  }
191
192  if ($conf['use_iptc'])
193  {
194    $update_fields =
195      array_merge(
196        $update_fields,
197        array_keys($conf['use_iptc_mapping'])
198        );
199  }
200
201  return $update_fields;
202}
203
204// returns a hash of attributes (metadata+filesize+width,...) for file
205function get_element_metadata($file, $has_high = false)
206{
207  global $conf;
208  if (!is_file($file))
209  {
210    return null;
211  }
212
213  $data = array();
214
215  $data['filesize'] = floor(filesize($file)/1024);
216
217  if ($image_size = @getimagesize($file))
218  {
219    $data['width'] = $image_size[0];
220    $data['height'] = $image_size[1];
221  }
222
223  if ($has_high)
224  {
225    $high_file = dirname($file).'/pwg_high/'.basename($file);
226   
227    $data['high_filesize'] = floor(filesize($high_file)/1024);
228  }
229 
230  if ($conf['use_exif'])
231  {
232    $data = array_merge($data, get_sync_exif_data($file) );
233  }
234
235  if ($conf['use_iptc'])
236  {
237    $data = array_merge($data, get_sync_iptc_data($file) );
238  }
239
240  return $data;
241}
242
243
244//-------------------------------------------------- private functions --------
245function get_representative_ext($path, $filename_wo_ext)
246{
247  global $conf;
248  $base_test = $path.'/pwg_representative/'.$filename_wo_ext.'.';
249  foreach ($conf['picture_ext'] as $ext)
250  {
251    $test = $base_test.$ext;
252    if (is_file($test))
253    {
254      return $ext;
255    }
256  }
257  return null;
258}
259
260function get_tn_ext($path, $filename_wo_ext)
261{
262  global $conf;
263
264  $base_test =
265    $path.'/thumbnail/'.$conf['prefix_thumbnail'].$filename_wo_ext.'.';
266
267  foreach ($conf['picture_ext'] as $ext)
268  {
269    $test = $base_test.$ext;
270    if (is_file($test))
271    {
272      return $ext;
273    }
274  }
275
276  return null;
277}
278
279function get_has_high($path, $filename)
280{
281  if (is_file($path.'/pwg_high/'.$filename))
282  {
283    return 'true';
284  }
285
286  return null;
287}
288
289}
290?>
Note: See TracBrowser for help on using the repository browser.