aboutsummaryrefslogtreecommitdiffstats
path: root/BSF/admin/site_reader_local.php
blob: 6d6d252ee8a7ea1dab93e4fb0a15e348b8c6ef9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based picture gallery                                  |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

// provides data for site synchronization from the local file system
class LocalSiteReader
{

var $site_url;

function LocalSiteReader($url)
{
  $this->site_url = $url;
}

/**
 * Is this local site ok ?
 *
 * @return true on success, false otherwise
 */
function open()
{
  global $errors;

  if (!is_dir($this->site_url))
  {
    array_push(
      $errors,
      array(
        'path' => $this->site_url,
        'type' => 'PWG-ERROR-NO-FS'
        )
      );

    return false;
  }

  return true;
}

// retrieve file system sub-directories fulldirs
function get_full_directories($basedir)
{
  $fs_fulldirs = get_fs_directories($basedir);
  return $fs_fulldirs;
}

/**
 * Returns an array with all file system files according to $conf['file_ext']
 * and $conf['picture_ext']
 * @param string $path recurse in this directory
 * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... )
 */
function get_elements($path)
{
  global $conf;
  if (!isset($conf['flip_file_ext']))
  {
    $conf['flip_file_ext'] = array_flip($conf['file_ext']);
  }

  $subdirs = array();
  $fs = array();
  if (is_dir($path) && $contents = opendir($path) )
  {
    while (($node = readdir($contents)) !== false)
    {
      if (is_file($path.'/'.$node))
      {
        $extension = get_extension($node);
        $filename_wo_ext = get_filename_wo_extension($node);

        if ( isset($conf['flip_file_ext'][$extension]) )
        {
          $tn_ext = $this->get_tn_ext($path, $filename_wo_ext);
          $fs[ $path.'/'.$node ] = array(
            'tn_ext' => $tn_ext,
            );
        }
      }
      elseif (is_dir($path.'/'.$node)
               and $node != '.'
               and $node != '..'
               and $node != 'pwg_high'
               and $node != 'pwg_representative'
               and $node != 'thumbnail' )
      {
        array_push($subdirs, $node);
      }
    } //end while readdir
    closedir($contents);

    foreach ($subdirs as $subdir)
    {
      $tmp_fs = $this->get_elements($path.'/'.$subdir);
      $fs = array_merge($fs, $tmp_fs);
    }
  } //end if is_dir
  return $fs;
}

// returns the name of the attributes that are supported for
// files update/synchronization
function get_update_attributes()
{
  return array('tn_ext', 'has_high', 'representative_ext');
}

function get_element_update_attributes($file)
{
  global $conf;
  $data = array();

  $filename = basename($file);
  $dirname = dirname($file);
  $filename_wo_ext = get_filename_wo_extension($filename);
  $extension = get_extension($filename);

  $data['tn_ext'] = $this->get_tn_ext($dirname, $filename_wo_ext);
  $data['has_high'] = $this->get_has_high($dirname, $filename);

  if ( !isset($conf['flip_picture_ext'][$extension]) )
  {
    $data['representative_ext'] = $this->get_representative_ext(
        $dirname, $filename_wo_ext
      );
  }
  return $data;
}

// returns the name of the attributes that are supported for
// metadata update/synchronization according to configuration
function get_metadata_attributes()
{
  global $conf;

  $update_fields = array('filesize', 'width', 'height', 'high_filesize');

  if ($conf['use_exif'])
  {
    $update_fields =
      array_merge(
        $update_fields,
        array_keys($conf['use_exif_mapping'])
        );
  }

  if ($conf['use_iptc'])
  {
    $update_fields =
      array_merge(
        $update_fields,
        array_keys($conf['use_iptc_mapping'])
        );
  }

  return $update_fields;
}

// returns a hash of attributes (metadata+filesize+width,...) for file
function get_element_metadata($file, $has_high = false)
{
  global $conf;
  if (!is_file($file))
  {
    return null;
  }

  $data = array();

  $data['filesize'] = floor(filesize($file)/1024);

  if ($image_size = @getimagesize($file))
  {
    $data['width'] = $image_size[0];
    $data['height'] = $image_size[1];
  }

  if ($has_high)
  {
    $high_file = dirname($file).'/pwg_high/'.basename($file);
    
    $data['high_filesize'] = floor(filesize($high_file)/1024);
  }
  
  if ($conf['use_exif'])
  {
    $data = array_merge($data, get_sync_exif_data($file) );
  }

  if ($conf['use_iptc'])
  {
    $data = array_merge($data, get_sync_iptc_data($file) );
  }

  return $data;
}


//-------------------------------------------------- private functions --------
function get_representative_ext($path, $filename_wo_ext)
{
  global $conf;
  $base_test = $path.'/pwg_representative/'.$filename_wo_ext.'.';
  foreach ($conf['picture_ext'] as $ext)
  {
    $test = $base_test.$ext;
    if (is_file($test))
    {
      return $ext;
    }
  }
  return null;
}

function get_tn_ext($path, $filename_wo_ext)
{
  global $conf;

  $base_test =
    $path.'/thumbnail/'.$conf['prefix_thumbnail'].$filename_wo_ext.'.';

  foreach ($conf['picture_ext'] as $ext)
  {
    $test = $base_test.$ext;
    if (is_file($test))
    {
      return $ext;
    }
  }

  return null;
}

function get_has_high($path, $filename)
{
  if (is_file($path.'/pwg_high/'.$filename))
  {
    return 'true';
  }

  return null;
}

}
?>