aboutsummaryrefslogtreecommitdiffstats
path: root/tools/create_listing_file.php
blob: 53dc65a4bddc00de6d5d6ec60c356d8fabf6dbce (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch        : BSF (Best So Far)
// | file          : $RCSfile$
// | last update   : $Date$
// | last modifier : $Author$
// | revision      : $Revision$
// +-----------------------------------------------------------------------+
// | 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.                                                                  |
// +-----------------------------------------------------------------------+

// +-----------------------------------------------------------------------+
// |                              parameters                               |
// +-----------------------------------------------------------------------+

// prefix for thumbnails in "thumbnail" sub directories
$conf['prefix_thumbnail'] = 'TN-';

// $conf['file_ext'] lists all extensions (case insensitive) allowed for
// your PhpWebGallery installation
$conf['file_ext'] = array('jpg','JPG','png','PNG','gif','GIF','mpg','zip',
                          'avi','mp3','ogg');

// $conf['picture_ext'] must be a subset of $conf['file_ext']
$conf['picture_ext'] = array('jpg','JPG','png','PNG','gif','GIF');

// $conf['version'] is used to verify the compatibility of the generated
// listing.xml file and the PhpWebGallery version you're running
$conf['version'] = 'BSF';

// $conf['use_exif'] set to true if you want to use Exif Date as "creation
// date" for the element, otherwise, set to false
$conf['use_exif'] = true;

// $conf['use_iptc'] set to true if you want to use IPTC informations of the
// element according to get_sync_iptc_data function mapping, otherwise, set
// to false
$conf['use_iptc'] = false;

// use_iptc_mapping : in which IPTC fields will PhpWebGallery find image
// information ? This setting is used during metadata synchronisation. It
// associates a phpwebgallery_images column name to a IPTC key
$conf['use_iptc_mapping'] = array(
  'keywords'        => '2#025',
  'date_creation'   => '2#055',
  'author'          => '2#122',
  'name'            => '2#005',
  'comment'         => '2#120'
  );

// +-----------------------------------------------------------------------+
// |                               functions                               |
// +-----------------------------------------------------------------------+

/**
 * returns informations from IPTC metadata, mapping is done at the beginning
 * of the function
 *
 * @param string $filename
 * @return array
 */
function get_iptc_data($filename, $map)
{
  $result = array();
  
  // Read IPTC data
  $iptc = array();
  
  $imginfo = array();
  getimagesize($filename, $imginfo);
  
  if (isset($imginfo['APP13']))
  {
    $iptc = iptcparse($imginfo['APP13']);
    if (is_array($iptc))
    {
      $rmap = array_flip($map);
      foreach (array_keys($rmap) as $iptc_key)
      {
        if (isset($iptc[$iptc_key][0]))
        {
          if ($iptc_key == '2#025')
          {
            $value = implode(',',
                             array_map('clean_iptc_value',$iptc[$iptc_key]));
          }
          else
          {
            $value = clean_iptc_value($iptc[$iptc_key][0]);
          }

          foreach (array_keys($map, $iptc_key) as $pwg_key)
          {
            $result[$pwg_key] = $value;
          }
        }
      }
    }
  }
  return $result;
}

/**
 * return a cleaned IPTC value
 *
 * @param string value
 * @return string
 */
function clean_iptc_value($value)
{
  // strip leading zeros (weird Kodak Scanner software)
  while ($value[0] == chr(0))
  {
    $value = substr($value, 1);
  }
  // remove binary nulls
  $value = str_replace(chr(0x00), ' ', $value);
  
  return htmlentities($value);
}

function get_sync_iptc_data($file)
{
  global $conf;

  $map = $conf['use_iptc_mapping'];
  $datefields = array('date_creation', 'date_available');
  
  $iptc = get_iptc_data($file, $map);

  foreach ($iptc as $pwg_key => $value)
  {
    if (in_array($pwg_key, $datefields))
    {
      if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
      {
        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
      }
    }
  }

  if (isset($iptc['keywords']))
  {
    // keywords separator is the comma, nothing else. Allowed characters in
    // keywords : [A-Za-z0-9], "-" and "_". All other characters will be
    // considered as separators
    $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']);
    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
  }

  return $iptc;
}

/**
 * returns a float value coresponding to the number of seconds since the
 * unix epoch (1st January 1970) and the microseconds are precised :
 * e.g. 1052343429.89276600
 *
 * @return float
 */
function get_moment()
{
  $t1 = explode(' ', microtime());
  $t2 = explode('.', $t1[0]);
  $t2 = $t1[1].'.'.$t2[1];
  return $t2;
}

/**
 * returns the number of seconds (with 3 decimals precision) between the
 * start time and the end time given.
 *
 * @param float start
 * @param float end
 * @return void
 */
function get_elapsed_time($start, $end)
{
  return number_format($end - $start, 3, '.', ' ').' s';
}

/**
 * returns an array with all picture files according to $conf['file_ext']
 *
 * @param string $dir
 * @return array
 */
function get_pwg_files($dir)
{
  global $conf;

  $pictures = array();
  if ($opendir = opendir($dir))
  {
    while ($file = readdir($opendir))
    {
      if (in_array(get_extension($file), $conf['file_ext']))
      {
        array_push($pictures, $file);
        if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
        {
          echo 'PWG-WARNING-2: "'.$file.'" : ';
          echo 'The name of the file should be composed of ';
          echo 'letters, figures, "-", "_" or "." ONLY';
          echo "\n";
        }
      }
    }
  }
  return $pictures;
}

/**
 * returns an array with all thumbnails according to $conf['picture_ext']
 * and $conf['prefix_thumbnail']
 *
 * @param string $dir
 * @return array
 */
function get_thumb_files($dir)
{
  global $conf;

  $prefix_length = strlen($conf['prefix_thumbnail']);
  
  $thumbnails = array();
  if ($opendir = @opendir($dir.'/thumbnail'))
  {
    while ($file = readdir($opendir))
    {
      if (in_array(get_extension($file), $conf['picture_ext'])
          and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'])
      {
        array_push($thumbnails, $file);
      }
    }
  }
  return $thumbnails;
}

/**
 * returns an array with representative picture files of a directory
 * according to $conf['picture_ext']
 *
 * @param string $dir
 * @return array
 */
function get_representative_files($dir)
{
  global $conf;

  $pictures = array();
  if ($opendir = @opendir($dir.'/pwg_representative'))
  {
    while ($file = readdir($opendir))
    {
      if (in_array(get_extension($file), $conf['picture_ext']))
      {
        array_push($pictures, $file);
      }
    }
  }
  return $pictures;
}

/**
 * search in $basedir the sub-directories and calls get_pictures
 *
 * @return void
 */
function get_dirs($basedir, $indent, $level)
{
  $fs_dirs = array();
  $dirs = "";

  if ($opendir = opendir($basedir))
  {
    while ($file = readdir($opendir))
    {
      if ($file != '.'
          and $file != '..'
          and $file != 'thumbnail'
          and $file != 'pwg_high'
          and $file != 'pwg_representative'
          and is_dir ($basedir.'/'.$file))
      {
        array_push($fs_dirs, $file);
        if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
        {
          echo 'PWG-WARNING-1: "'.$file.'" : ';
          echo 'The name of the directory should be composed of ';
          echo 'letters, figures, "-", "_" or "." ONLY';
          echo "\n";
        }
      }
    }
  }
  // write of the dirs
  foreach ($fs_dirs as $fs_dir)
  {
    $dirs.= "\n".$indent.'<dir'.$level.' name="'.$fs_dir.'">';
    $dirs.= get_pictures($basedir.'/'.$fs_dir, $indent.'  ');
    $dirs.= get_dirs($basedir.'/'.$fs_dir, $indent.'  ', $level + 1);
    $dirs.= "\n".$indent.'</dir'.$level.'>';
  }
  return $dirs;		
}

// get_extension returns the part of the string after the last "."
function get_extension($filename)
{
  return substr(strrchr($filename, '.'), 1, strlen ($filename));
}

// get_filename_wo_extension returns the part of the string before the last
// ".".
// get_filename_wo_extension('test.tar.gz') -> 'test.tar'
function get_filename_wo_extension($filename)
{
  return substr($filename, 0, strrpos($filename, '.'));
}

function get_pictures($dir, $indent)
{
  global $conf, $page;
  
  // fs means FileSystem : $fs_files contains files in the filesystem found
  // in $dir that can be managed by PhpWebGallery (see get_pwg_files
  // function), $fs_thumbnails contains thumbnails, $fs_representatives
  // contains potentially representative pictures for non picture files
  $fs_files = get_pwg_files($dir);
  $fs_thumbnails = get_thumb_files($dir);
  $fs_representatives = get_representative_files($dir);

  $elements = array();

  $print_dir = preg_replace('/^\.\//', '', $dir);
  $print_dir = preg_replace('/\/*$/', '/', $print_dir);
  
  foreach ($fs_files as $fs_file)
  {
    $element = array();
    $element['file'] = $fs_file;
    $element['path'] = $page['url'].$print_dir.$fs_file;
    $element['filesize'] = floor(filesize($dir.'/'.$fs_file) / 1024);
    
    $file_wo_ext = get_filename_wo_extension($fs_file);

    foreach ($conf['picture_ext'] as $ext)
    {
      $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
      if (!in_array($test, $fs_thumbnails))
      {
        continue;
      }
      else
      {
        $element['tn_ext'] = $ext;
        break;
      }
    }

    // 2 cases : the element is a picture or not. Indeed, for a picture
    // thumbnail is mandatory and for non picture element, thumbnail and
    // representative is optionnal
    if (in_array(get_extension($fs_file), $conf['picture_ext']))
    {
      // if we found a thumnbnail corresponding to our picture...
      if (isset($element['tn_ext']))
      {
        if ($image_size = @getimagesize($dir.'/'.$fs_file))
        {
          $element['width'] = $image_size[0];
          $element['height'] = $image_size[1];
        }

        if ($conf['use_exif'])
        {
          if ($exif = @read_exif_data($dir.'/'.$fs_file))
          {
            if (isset($exif['DateTime']))
            {
              preg_match('/^(\d{4}):(\d{2}):(\d{2})/'
                         ,$exif['DateTime']
                         ,$matches);
              $element['date_creation'] =
                $matches[1].'-'.$matches[2].'-'.$matches[3];
            }
          }
        }

        if ($conf['use_iptc'])
        {
          $iptc = get_sync_iptc_data($dir.'/'.$fs_file);
          if (count($iptc) > 0)
          {
            foreach (array_keys($iptc) as $key)
            {
              $element[$key] = addslashes($iptc[$key]);
            }
          }
        }
        
        array_push($elements, $element);
      }
      else
      {
        echo 'PWG-ERROR-1: The thumbnail is missing for '.$dir.'/'.$fs_file;
        echo '-> '.$dir.'/thumbnail/';
        echo $conf['prefix_thumbnail'].$file_wo_ext.'.xxx';
        echo ' ("xxx" can be : ';
        echo implode(', ', $conf['picture_ext']);
        echo ')'."\n";
      }
    }
    else
    {
      foreach ($conf['picture_ext'] as $ext)
      {
        $candidate = $file_wo_ext.'.'.$ext;
        if (!in_array($candidate, $fs_representatives))
        {
          continue;
        }
        else
        {
          $element['representative_ext'] = $ext;
          break;
        }
      }
      
      array_push($elements, $element);
    }
  }

  $xml = "\n".$indent.'<root>';
  $attributes = array('file','tn_ext','representative_ext','filesize',
                      'width','height','date_creation','author','keywords',
                      'name','comment','path');
  foreach ($elements as $element)
  {
    $xml.= "\n".$indent.'  ';
    $xml.= '<element';
    foreach ($attributes as $attribute)
    {
      if (isset($element{$attribute}))
      {
        $xml.= ' '.$attribute.'="'.$element{$attribute}.'"';
      }
    }
    $xml.= ' />';
  }
  $xml.= "\n".$indent.'</root>';

  return $xml;
}

// +-----------------------------------------------------------------------+
// |                                script                                 |
// +-----------------------------------------------------------------------+
if (isset($_GET['action']))
{
  $page['action'] = $_GET['action'];
}
else
{
  $page['action'] = 'generate';
}
echo '<pre>';
switch ($page['action'])
{
  case 'generate' :
  {
    $start = get_moment();
    
    $listing = '<informations';
    $listing.= ' generation_date="'.date('Y-m-d').'"';
    $listing.= ' phpwg_version="'.$conf{'version'}.'"';
    
    $end = strrpos($_SERVER['PHP_SELF'], '/') + 1;
    $local_folder = substr($_SERVER['PHP_SELF'], 0, $end);
    $page['url'] = 'http://'.$_SERVER['HTTP_HOST'].$local_folder;
    
    $listing.= ' url="'.$page['url'].'"';
    $listing.= '/>'."\n";
    
    $listing.= get_dirs('.', '', 0);
    
    if ($fp = @fopen("./listing.xml","w"))
    {
      fwrite($fp, $listing);
      fclose($fp);
      echo 'PWG-INFO-1: listing.xml created in ';
      echo get_elapsed_time($start, get_moment());
      echo "\n";
    }
    else
    {
      echo "PWG-ERROR-2: I can't write the file listing.xml"."\n";
    }
    break;
  }
  case 'test' :
  {
    if (isset($_GET['version']))
    {
      if ($_GET['version'] != $conf['version'])
      {
        echo 'PWG-ERROR-4: PhpWebGallery versions differs'."\n";
      }
      else
      {
        echo 'PWG-INFO-2: test successful'."\n";
      }
    }
    else
    {
      echo 'PWG-INFO-2: test successful'."\n";
    }
    break;
  }
  case 'clean' :
  {
    if( @unlink('./listing.xml'))
    {
      echo 'PWG-INFO-3 : listing.xml file deleted'."\n";
    }
    else
    {
      echo 'PWG-ERROR-3 : listing.xml does not exist'."\n";
    }
    break;
  }
}
echo '</pre>';
?>