source: trunk/tools/create_listing_file.php @ 758

Last change on this file since 758 was 758, checked in by plg, 19 years ago
  • new way of managing the application version
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-03-25 22:47:18 +0000 (Fri, 25 Mar 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 758 $
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// |                              parameters                               |
30// +-----------------------------------------------------------------------+
31
32// prefix for thumbnails in "thumbnail" sub directories
33$conf['prefix_thumbnail'] = 'TN-';
34
35// $conf['file_ext'] lists all extensions (case insensitive) allowed for
36// your PhpWebGallery installation
37$conf['file_ext'] = array('jpg','JPG','png','PNG','gif','GIF','mpg','zip',
38                          'avi','mp3','ogg');
39
40// $conf['picture_ext'] must be a subset of $conf['file_ext']
41$conf['picture_ext'] = array('jpg','JPG','png','PNG','gif','GIF');
42
43// $conf['version'] is used to verify the compatibility of the generated
44// listing.xml file and the PhpWebGallery version you're running
45$conf['version'] = '%PWGVERSION%';
46
47// $conf['use_exif'] set to true if you want to use Exif Date as "creation
48// date" for the element, otherwise, set to false
49$conf['use_exif'] = true;
50
51// $conf['use_iptc'] set to true if you want to use IPTC informations of the
52// element according to get_sync_iptc_data function mapping, otherwise, set
53// to false
54$conf['use_iptc'] = false;
55
56// use_iptc_mapping : in which IPTC fields will PhpWebGallery find image
57// information ? This setting is used during metadata synchronisation. It
58// associates a phpwebgallery_images column name to a IPTC key
59$conf['use_iptc_mapping'] = array(
60  'keywords'        => '2#025',
61  'date_creation'   => '2#055',
62  'author'          => '2#122',
63  'name'            => '2#005',
64  'comment'         => '2#120'
65  );
66
67// +-----------------------------------------------------------------------+
68// |                               functions                               |
69// +-----------------------------------------------------------------------+
70
71/**
72 * returns informations from IPTC metadata, mapping is done at the beginning
73 * of the function
74 *
75 * @param string $filename
76 * @return array
77 */
78function get_iptc_data($filename, $map)
79{
80  $result = array();
81 
82  // Read IPTC data
83  $iptc = array();
84 
85  $imginfo = array();
86  getimagesize($filename, $imginfo);
87 
88  if (isset($imginfo['APP13']))
89  {
90    $iptc = iptcparse($imginfo['APP13']);
91    if (is_array($iptc))
92    {
93      $rmap = array_flip($map);
94      foreach (array_keys($rmap) as $iptc_key)
95      {
96        if (isset($iptc[$iptc_key][0]))
97        {
98          if ($iptc_key == '2#025')
99          {
100            $value = implode(',',
101                             array_map('clean_iptc_value',$iptc[$iptc_key]));
102          }
103          else
104          {
105            $value = clean_iptc_value($iptc[$iptc_key][0]);
106          }
107
108          foreach (array_keys($map, $iptc_key) as $pwg_key)
109          {
110            $result[$pwg_key] = $value;
111          }
112        }
113      }
114    }
115  }
116  return $result;
117}
118
119/**
120 * return a cleaned IPTC value
121 *
122 * @param string value
123 * @return string
124 */
125function clean_iptc_value($value)
126{
127  // strip leading zeros (weird Kodak Scanner software)
128  while ($value[0] == chr(0))
129  {
130    $value = substr($value, 1);
131  }
132  // remove binary nulls
133  $value = str_replace(chr(0x00), ' ', $value);
134 
135  return htmlentities($value);
136}
137
138function get_sync_iptc_data($file)
139{
140  global $conf;
141
142  $map = $conf['use_iptc_mapping'];
143  $datefields = array('date_creation', 'date_available');
144 
145  $iptc = get_iptc_data($file, $map);
146
147  foreach ($iptc as $pwg_key => $value)
148  {
149    if (in_array($pwg_key, $datefields))
150    {
151      if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
152      {
153        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
154      }
155    }
156  }
157
158  if (isset($iptc['keywords']))
159  {
160    // keywords separator is the comma, nothing else. Allowed characters in
161    // keywords : [A-Za-z0-9], "-" and "_". All other characters will be
162    // considered as separators
163    $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']);
164    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
165  }
166
167  return $iptc;
168}
169
170/**
171 * returns a float value coresponding to the number of seconds since the
172 * unix epoch (1st January 1970) and the microseconds are precised :
173 * e.g. 1052343429.89276600
174 *
175 * @return float
176 */
177function get_moment()
178{
179  $t1 = explode(' ', microtime());
180  $t2 = explode('.', $t1[0]);
181  $t2 = $t1[1].'.'.$t2[1];
182  return $t2;
183}
184
185/**
186 * returns the number of seconds (with 3 decimals precision) between the
187 * start time and the end time given.
188 *
189 * @param float start
190 * @param float end
191 * @return void
192 */
193function get_elapsed_time($start, $end)
194{
195  return number_format($end - $start, 3, '.', ' ').' s';
196}
197
198/**
199 * returns an array with all picture files according to $conf['file_ext']
200 *
201 * @param string $dir
202 * @return array
203 */
204function get_pwg_files($dir)
205{
206  global $conf;
207
208  $pictures = array();
209  if ($opendir = opendir($dir))
210  {
211    while ($file = readdir($opendir))
212    {
213      if (in_array(get_extension($file), $conf['file_ext']))
214      {
215        array_push($pictures, $file);
216        if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
217        {
218          echo 'PWG-WARNING-2: "'.$file.'" : ';
219          echo 'The name of the file should be composed of ';
220          echo 'letters, figures, "-", "_" or "." ONLY';
221          echo "\n";
222        }
223      }
224    }
225  }
226  return $pictures;
227}
228
229/**
230 * returns an array with all thumbnails according to $conf['picture_ext']
231 * and $conf['prefix_thumbnail']
232 *
233 * @param string $dir
234 * @return array
235 */
236function get_thumb_files($dir)
237{
238  global $conf;
239
240  $prefix_length = strlen($conf['prefix_thumbnail']);
241 
242  $thumbnails = array();
243  if ($opendir = @opendir($dir.'/thumbnail'))
244  {
245    while ($file = readdir($opendir))
246    {
247      if (in_array(get_extension($file), $conf['picture_ext'])
248          and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'])
249      {
250        array_push($thumbnails, $file);
251      }
252    }
253  }
254  return $thumbnails;
255}
256
257/**
258 * returns an array with representative picture files of a directory
259 * according to $conf['picture_ext']
260 *
261 * @param string $dir
262 * @return array
263 */
264function get_representative_files($dir)
265{
266  global $conf;
267
268  $pictures = array();
269  if ($opendir = @opendir($dir.'/pwg_representative'))
270  {
271    while ($file = readdir($opendir))
272    {
273      if (in_array(get_extension($file), $conf['picture_ext']))
274      {
275        array_push($pictures, $file);
276      }
277    }
278  }
279  return $pictures;
280}
281
282/**
283 * search in $basedir the sub-directories and calls get_pictures
284 *
285 * @return void
286 */
287function get_dirs($basedir, $indent, $level)
288{
289  $fs_dirs = array();
290  $dirs = "";
291
292  if ($opendir = opendir($basedir))
293  {
294    while ($file = readdir($opendir))
295    {
296      if ($file != '.'
297          and $file != '..'
298          and $file != 'thumbnail'
299          and $file != 'pwg_high'
300          and $file != 'pwg_representative'
301          and is_dir ($basedir.'/'.$file))
302      {
303        array_push($fs_dirs, $file);
304        if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
305        {
306          echo 'PWG-WARNING-1: "'.$file.'" : ';
307          echo 'The name of the directory should be composed of ';
308          echo 'letters, figures, "-", "_" or "." ONLY';
309          echo "\n";
310        }
311      }
312    }
313  }
314  // write of the dirs
315  foreach ($fs_dirs as $fs_dir)
316  {
317    $dirs.= "\n".$indent.'<dir'.$level.' name="'.$fs_dir.'">';
318    $dirs.= get_pictures($basedir.'/'.$fs_dir, $indent.'  ');
319    $dirs.= get_dirs($basedir.'/'.$fs_dir, $indent.'  ', $level + 1);
320    $dirs.= "\n".$indent.'</dir'.$level.'>';
321  }
322  return $dirs;         
323}
324
325// get_extension returns the part of the string after the last "."
326function get_extension($filename)
327{
328  return substr(strrchr($filename, '.'), 1, strlen ($filename));
329}
330
331// get_filename_wo_extension returns the part of the string before the last
332// ".".
333// get_filename_wo_extension('test.tar.gz') -> 'test.tar'
334function get_filename_wo_extension($filename)
335{
336  return substr($filename, 0, strrpos($filename, '.'));
337}
338
339function get_pictures($dir, $indent)
340{
341  global $conf, $page;
342 
343  // fs means FileSystem : $fs_files contains files in the filesystem found
344  // in $dir that can be managed by PhpWebGallery (see get_pwg_files
345  // function), $fs_thumbnails contains thumbnails, $fs_representatives
346  // contains potentially representative pictures for non picture files
347  $fs_files = get_pwg_files($dir);
348  $fs_thumbnails = get_thumb_files($dir);
349  $fs_representatives = get_representative_files($dir);
350
351  $elements = array();
352
353  $print_dir = preg_replace('/^\.\//', '', $dir);
354  $print_dir = preg_replace('/\/*$/', '/', $print_dir);
355 
356  foreach ($fs_files as $fs_file)
357  {
358    $element = array();
359    $element['file'] = $fs_file;
360    $element['path'] = $page['url'].$print_dir.$fs_file;
361    $element['filesize'] = floor(filesize($dir.'/'.$fs_file) / 1024);
362   
363    $file_wo_ext = get_filename_wo_extension($fs_file);
364
365    foreach ($conf['picture_ext'] as $ext)
366    {
367      $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
368      if (!in_array($test, $fs_thumbnails))
369      {
370        continue;
371      }
372      else
373      {
374        $element['tn_ext'] = $ext;
375        break;
376      }
377    }
378
379    // 2 cases : the element is a picture or not. Indeed, for a picture
380    // thumbnail is mandatory and for non picture element, thumbnail and
381    // representative is optionnal
382    if (in_array(get_extension($fs_file), $conf['picture_ext']))
383    {
384      // if we found a thumnbnail corresponding to our picture...
385      if (isset($element['tn_ext']))
386      {
387        if ($image_size = @getimagesize($dir.'/'.$fs_file))
388        {
389          $element['width'] = $image_size[0];
390          $element['height'] = $image_size[1];
391        }
392
393        if ($conf['use_exif'])
394        {
395          if ($exif = @read_exif_data($dir.'/'.$fs_file))
396          {
397            if (isset($exif['DateTime']))
398            {
399              preg_match('/^(\d{4}):(\d{2}):(\d{2})/'
400                         ,$exif['DateTime']
401                         ,$matches);
402              $element['date_creation'] =
403                $matches[1].'-'.$matches[2].'-'.$matches[3];
404            }
405          }
406        }
407
408        if ($conf['use_iptc'])
409        {
410          $iptc = get_sync_iptc_data($dir.'/'.$fs_file);
411          if (count($iptc) > 0)
412          {
413            foreach (array_keys($iptc) as $key)
414            {
415              $element[$key] = addslashes($iptc[$key]);
416            }
417          }
418        }
419       
420        array_push($elements, $element);
421      }
422      else
423      {
424        echo 'PWG-ERROR-1: The thumbnail is missing for '.$dir.'/'.$fs_file;
425        echo '-> '.$dir.'/thumbnail/';
426        echo $conf['prefix_thumbnail'].$file_wo_ext.'.xxx';
427        echo ' ("xxx" can be : ';
428        echo implode(', ', $conf['picture_ext']);
429        echo ')'."\n";
430      }
431    }
432    else
433    {
434      foreach ($conf['picture_ext'] as $ext)
435      {
436        $candidate = $file_wo_ext.'.'.$ext;
437        if (!in_array($candidate, $fs_representatives))
438        {
439          continue;
440        }
441        else
442        {
443          $element['representative_ext'] = $ext;
444          break;
445        }
446      }
447     
448      array_push($elements, $element);
449    }
450  }
451
452  $xml = "\n".$indent.'<root>';
453  $attributes = array('file','tn_ext','representative_ext','filesize',
454                      'width','height','date_creation','author','keywords',
455                      'name','comment','path');
456  foreach ($elements as $element)
457  {
458    $xml.= "\n".$indent.'  ';
459    $xml.= '<element';
460    foreach ($attributes as $attribute)
461    {
462      if (isset($element{$attribute}))
463      {
464        $xml.= ' '.$attribute.'="'.$element{$attribute}.'"';
465      }
466    }
467    $xml.= ' />';
468  }
469  $xml.= "\n".$indent.'</root>';
470
471  return $xml;
472}
473
474// +-----------------------------------------------------------------------+
475// |                                script                                 |
476// +-----------------------------------------------------------------------+
477if (isset($_GET['action']))
478{
479  $page['action'] = $_GET['action'];
480}
481else
482{
483  $page['action'] = 'generate';
484}
485echo '<pre>';
486switch ($page['action'])
487{
488  case 'generate' :
489  {
490    $start = get_moment();
491   
492    $listing = '<informations';
493    $listing.= ' generation_date="'.date('Y-m-d').'"';
494    $listing.= ' phpwg_version="'.$conf{'version'}.'"';
495   
496    $end = strrpos($_SERVER['PHP_SELF'], '/') + 1;
497    $local_folder = substr($_SERVER['PHP_SELF'], 0, $end);
498    $page['url'] = 'http://'.$_SERVER['HTTP_HOST'].$local_folder;
499   
500    $listing.= ' url="'.$page['url'].'"';
501    $listing.= '/>'."\n";
502   
503    $listing.= get_dirs('.', '', 0);
504   
505    if ($fp = @fopen("./listing.xml","w"))
506    {
507      fwrite($fp, $listing);
508      fclose($fp);
509      echo 'PWG-INFO-1: listing.xml created in ';
510      echo get_elapsed_time($start, get_moment());
511      echo "\n";
512    }
513    else
514    {
515      echo "PWG-ERROR-2: I can't write the file listing.xml"."\n";
516    }
517    break;
518  }
519  case 'test' :
520  {
521    if (isset($_GET['version']))
522    {
523      if ($_GET['version'] != $conf['version'])
524      {
525        echo 'PWG-ERROR-4: PhpWebGallery versions differs'."\n";
526      }
527      else
528      {
529        echo 'PWG-INFO-2: test successful'."\n";
530      }
531    }
532    else
533    {
534      echo 'PWG-INFO-2: test successful'."\n";
535    }
536    break;
537  }
538  case 'clean' :
539  {
540    if( @unlink('./listing.xml'))
541    {
542      echo 'PWG-INFO-3 : listing.xml file deleted'."\n";
543    }
544    else
545    {
546      echo 'PWG-ERROR-3 : listing.xml does not exist'."\n";
547    }
548    break;
549  }
550}
551echo '</pre>';
552?>
Note: See TracBrowser for help on using the repository browser.