source: trunk/tools/create_listing_file.php @ 564

Last change on this file since 564 was 564, checked in by z0rglub, 20 years ago

admin/create_listing_file.php moved to tools/create_listing_file.php

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