source: trunk/admin/site_reader_local.php @ 12640

Last change on this file since 12640 was 10157, checked in by mistic100, 13 years ago

bug:1791 display hd dimensions instead of websize dimensions

  • Property svn:eol-style set to LF
File size: 6.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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', 'high_width', 'high_height');
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    $data['high_filesize'] = floor(filesize($high_file)/1024);
203   
204    if ($high_size = @getimagesize($high_file))
205    {
206      $data['high_width'] = $high_size[0];
207      $data['high_height'] = $high_size[1];
208    }
209  }
210
211  if ($conf['use_exif'])
212  {
213    $exif = get_sync_exif_data($file);
214    if (count($exif) == 0 and isset($data['high_filesize']))
215    {
216      $exif = get_sync_exif_data($high_file);
217    }
218    $data = array_merge($data, $exif);
219  }
220
221  if ($conf['use_iptc'])
222  {
223    $iptc = get_sync_iptc_data($file);
224    if (count($iptc) == 0 and isset($data['high_filesize']))
225    {
226      $iptc = get_sync_iptc_data($high_file);
227    }
228    $data = array_merge($data, $iptc);
229  }
230
231  return $data;
232}
233
234
235//-------------------------------------------------- private functions --------
236function get_representative_ext($path, $filename_wo_ext)
237{
238  global $conf;
239  $base_test = $path.'/pwg_representative/'.$filename_wo_ext.'.';
240  foreach ($conf['picture_ext'] as $ext)
241  {
242    $test = $base_test.$ext;
243    if (is_file($test))
244    {
245      return $ext;
246    }
247  }
248  return null;
249}
250
251function get_tn_ext($path, $filename_wo_ext)
252{
253  global $conf;
254
255  $base_test =
256    $path.'/thumbnail/'.$conf['prefix_thumbnail'].$filename_wo_ext.'.';
257
258  foreach ($conf['picture_ext'] as $ext)
259  {
260    $test = $base_test.$ext;
261    if (is_file($test))
262    {
263      return $ext;
264    }
265  }
266
267  return null;
268}
269
270function get_has_high($path, $filename)
271{
272  if (is_file($path.'/pwg_high/'.$filename))
273  {
274    return 'true';
275  }
276
277  return null;
278}
279
280}
281?>
Note: See TracBrowser for help on using the repository browser.