source: branches/2.1/admin/site_reader_local.php @ 6276

Last change on this file since 6276 was 6276, checked in by plg, 14 years ago

merge r6265 from trunk to branch 2.1

Correct text alignement in .infos, .errors
30px => 53px

File size: 6.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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// provides data for site synchronization from the local file system
25class LocalSiteReader
26{
27
28var $site_url;
29
30function LocalSiteReader($url)
31{
32  $this->site_url = $url;
33}
34
35/**
36 * Is this local site ok ?
37 *
38 * @return true on success, false otherwise
39 */
40function open()
41{
42  global $errors;
43
44  if (!is_dir($this->site_url))
45  {
46    array_push(
47      $errors,
48      array(
49        'path' => $this->site_url,
50        'type' => 'PWG-ERROR-NO-FS'
51        )
52      );
53
54    return false;
55  }
56
57  return true;
58}
59
60// retrieve file system sub-directories fulldirs
61function get_full_directories($basedir)
62{
63  $fs_fulldirs = get_fs_directories($basedir);
64  return $fs_fulldirs;
65}
66
67/**
68 * Returns an array with all file system files according to $conf['file_ext']
69 * and $conf['picture_ext']
70 * @param string $path recurse in this directory
71 * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... )
72 */
73function get_elements($path)
74{
75  global $conf;
76  if (!isset($conf['flip_file_ext']))
77  {
78    $conf['flip_file_ext'] = array_flip($conf['file_ext']);
79  }
80
81  $subdirs = array();
82  $fs = array();
83  if (is_dir($path) && $contents = opendir($path) )
84  {
85    while (($node = readdir($contents)) !== false)
86    {
87      if (is_file($path.'/'.$node))
88      {
89        $extension = get_extension($node);
90        $filename_wo_ext = get_filename_wo_extension($node);
91
92        if ( isset($conf['flip_file_ext'][$extension]) )
93        {
94          $tn_ext = $this->get_tn_ext($path, $filename_wo_ext);
95          $fs[ $path.'/'.$node ] = array(
96            'tn_ext' => $tn_ext,
97            );
98        }
99      }
100      elseif (is_dir($path.'/'.$node)
101               and $node != '.'
102               and $node != '..'
103               and $node != 'pwg_high'
104               and $node != 'pwg_representative'
105               and $node != 'thumbnail' )
106      {
107        array_push($subdirs, $node);
108      }
109    } //end while readdir
110    closedir($contents);
111
112    foreach ($subdirs as $subdir)
113    {
114      $tmp_fs = $this->get_elements($path.'/'.$subdir);
115      $fs = array_merge($fs, $tmp_fs);
116    }
117    ksort($fs);
118  } //end if is_dir
119  return $fs;
120}
121
122// returns the name of the attributes that are supported for
123// files update/synchronization
124function get_update_attributes()
125{
126  return array('tn_ext', 'has_high', 'representative_ext');
127}
128
129function get_element_update_attributes($file)
130{
131  global $conf;
132  $data = array();
133
134  $filename = basename($file);
135  $dirname = dirname($file);
136  $filename_wo_ext = get_filename_wo_extension($filename);
137  $extension = get_extension($filename);
138
139  $data['tn_ext'] = $this->get_tn_ext($dirname, $filename_wo_ext);
140  $data['has_high'] = $this->get_has_high($dirname, $filename);
141
142  if ( !isset($conf['flip_picture_ext'][$extension]) )
143  {
144    $data['representative_ext'] = $this->get_representative_ext(
145        $dirname, $filename_wo_ext
146      );
147  }
148  return $data;
149}
150
151// returns the name of the attributes that are supported for
152// metadata update/synchronization according to configuration
153function get_metadata_attributes()
154{
155  global $conf;
156
157  $update_fields = array('filesize', 'width', 'height', 'high_filesize');
158
159  if ($conf['use_exif'])
160  {
161    $update_fields =
162      array_merge(
163        $update_fields,
164        array_keys($conf['use_exif_mapping'])
165        );
166  }
167
168  if ($conf['use_iptc'])
169  {
170    $update_fields =
171      array_merge(
172        $update_fields,
173        array_keys($conf['use_iptc_mapping'])
174        );
175  }
176
177  return $update_fields;
178}
179
180// returns a hash of attributes (metadata+filesize+width,...) for file
181function get_element_metadata($file, $has_high = false)
182{
183  global $conf;
184  if (!is_file($file))
185  {
186    return null;
187  }
188
189  $data = array();
190
191  $data['filesize'] = floor(filesize($file)/1024);
192
193  if ($image_size = @getimagesize($file))
194  {
195    $data['width'] = $image_size[0];
196    $data['height'] = $image_size[1];
197  }
198
199  if ($has_high)
200  {
201    $high_file = dirname($file).'/pwg_high/'.basename($file);
202
203    $data['high_filesize'] = floor(filesize($high_file)/1024);
204  }
205
206  if ($conf['use_exif'])
207  {
208    $exif = get_sync_exif_data($file);
209    if (count($exif) == 0 and isset($data['high_filesize']))
210    {
211      $exif = get_sync_exif_data($high_file);
212    }
213    $data = array_merge($data, $exif);
214  }
215
216  if ($conf['use_iptc'])
217  {
218    $iptc = get_sync_iptc_data($file);
219    if (count($iptc) == 0 and isset($data['high_filesize']))
220    {
221      $iptc = get_sync_iptc_data($high_file);
222    }
223    $data = array_merge($data, $iptc);
224  }
225
226  return $data;
227}
228
229
230//-------------------------------------------------- private functions --------
231function get_representative_ext($path, $filename_wo_ext)
232{
233  global $conf;
234  $base_test = $path.'/pwg_representative/'.$filename_wo_ext.'.';
235  foreach ($conf['picture_ext'] as $ext)
236  {
237    $test = $base_test.$ext;
238    if (is_file($test))
239    {
240      return $ext;
241    }
242  }
243  return null;
244}
245
246function get_tn_ext($path, $filename_wo_ext)
247{
248  global $conf;
249
250  $base_test =
251    $path.'/thumbnail/'.$conf['prefix_thumbnail'].$filename_wo_ext.'.';
252
253  foreach ($conf['picture_ext'] as $ext)
254  {
255    $test = $base_test.$ext;
256    if (is_file($test))
257    {
258      return $ext;
259    }
260  }
261
262  return null;
263}
264
265function get_has_high($path, $filename)
266{
267  if (is_file($path.'/pwg_high/'.$filename))
268  {
269    return 'true';
270  }
271
272  return null;
273}
274
275}
276?>
Note: See TracBrowser for help on using the repository browser.