source: tags/build-Alligator02/tools/create_listing_file.php @ 6299

Last change on this file since 6299 was 1775, checked in by laurent_duretz, 17 years ago

Issue 0000475 : Synchroniser plus de 7000 photos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 43.3 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: 2007-02-01 22:35:22 +0000 (Thu, 01 Feb 2007) $
10// | last modifier : $Author: laurent_duretz $
11// | revision      : $Revision: 1775 $
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// |                                User configuration                     |
30// +-----------------------------------------------------------------------+
31
32// Srcipt version
33$conf['version'] = 'Alligator';
34
35// prefix for thumbnails in "thumbnail" sub directories
36$conf['prefix_thumbnail'] = 'TN-';
37
38// $conf['file_ext'] lists all extensions (case insensitive) allowed for your PhpWebGallery installation
39$conf['file_ext'] = array('jpg','JPG','png','PNG','gif','GIF','mpg','zip', 'avi','mp3','ogg');
40
41// $conf['picture_ext'] must be a subset of $conf['file_ext']
42$conf['picture_ext'] = array('jpg','JPG','png','PNG','gif','GIF');
43
44// URL of main gallery
45$conf['gallery'] = 'http://';
46
47// max excution time before refresh in seconds
48$conf['max_execution_time'] = (5*ini_get('max_execution_time'))/6; // 25 seconds with default PHP configuration
49
50// refresh delay is seconds
51$conf['refresh_delay'] = 0;
52
53// $conf['file_ext'] lists all extensions (case insensitive) allowed for your PhpWebGallery installation
54$conf['file_ext'] = array('jpg','JPG','jpeg','JPEG','png','PNG','gif','GIF','mpg','zip', 'avi','mp3','ogg');
55
56// $conf['use_exif'] set to true if you want to use Exif information
57$conf['use_exif'] = true;
58
59// use_exif_mapping: same behaviour as use_iptc_mapping
60$conf['use_exif_mapping'] = array(
61  'date_creation' => 'DateTimeOriginal'
62  );
63
64// $conf['use_iptc'] set to true if you want to use IPTC informations of the
65// element according to get_sync_iptc_data function mapping, otherwise, set
66// to false
67$conf['use_iptc'] = false;
68
69// use_iptc_mapping : in which IPTC fields will PhpWebGallery find image
70// information ? This setting is used during metadata synchronisation. It
71// associates a phpwebgallery_images column name to a IPTC key
72$conf['use_iptc_mapping'] = array(
73  'keywords'        => '2#025',
74  'date_creation'   => '2#055',
75  'author'          => '2#122',
76  'name'            => '2#005',
77  'comment'         => '2#120');
78
79// index.php content for command 'protect'
80$conf['protect_content'] = '<?php header("Location: '.$conf['gallery'].'") ?>';
81
82// directories names
83$conf['thumbs'] = 'thumbnail'; // thumbnails
84$conf['high'] = 'pwg_high'; // high resolution
85$conf['represent'] = 'pwg_representative'; // non pictures representative files
86
87// +-----------------------------------------------------------------------+
88// |                                Advanced script configuration          |
89// +-----------------------------------------------------------------------+
90
91// url of icon directory in yoga template
92$pwg_conf['icon_dir'] = $conf['gallery'].'/template/yoga/icon/';
93
94// list of actions managed by this script
95$pwg_conf['scan_action'] = array('clean', 'test', 'generate', 'protect');
96
97// url of this script
98$pwg_conf['this_url'] = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
99
100// list of reserved directory names
101$pwg_conf['reserved_directory_names'] = array($conf['thumbs'], $conf['high'], $conf['represent'], ".", "..", ".svn");
102
103// content of index.php generated in protect action
104$pwg_conf['protect_content'] = '<?php header("Location: '.$conf['gallery'].'") ?>';
105
106// backup of PHP safe_mode INI parameter (used for time limitation)
107$pwg_conf['safe_mode'] = (ini_get('safe_mode') == '1') ? true : false;
108
109// Error level management
110// true  : show warnings
111// false : hide warnings
112$pwg_conf['warning']['protect'] = true;
113
114// +-----------------------------------------------------------------------+
115// |                               functions                               |
116// +-----------------------------------------------------------------------+
117
118/**
119 * write line in log file
120 *
121 * @param string line
122 * @return string
123 */
124function pwg_log($line)
125{
126  $log_file = fopen(__FILE__.'.log', 'a');
127  fwrite($log_file, $line);
128  fclose($log_file);
129}
130
131/**
132 * returns xml </dirX> lines
133 *
134 * @param integer $dir_start
135 * @param integer $dir_number
136 * @return string
137 */
138function pwg_close_level($dir_start, $dir_number)
139{
140  //~ pwg_log('>>>>> pwg_close_level($dir_start = '.var_export($dir_start, TRUE).', $dir_number = '.var_export($dir_number, TRUE).') >>>>>'."\n");
141 
142  $lines ='';
143  do
144  {
145    $lines .= str_repeat(' ', 2*$dir_start).'</dir'.$dir_start.">\n";
146    $dir_number--;
147    $dir_start--;
148  }
149  while(($dir_number > 0) && ($dir_start >= 0));
150 
151  //~ pwg_log('<<<<< pwg_close_level returns '.var_export($lines, TRUE).' <<<<<'."\n");
152  return $lines;
153}
154
155/**
156 * return a cleaned IPTC value
157 *
158 * @param string value
159 * @return string
160 */
161function pwg_clean_iptc_value($value)
162{
163  //~ pwg_log('>>>>> pwg_clean_iptc_value ($value = '.var_export($value, TRUE).') >>>>>'."\n");
164 
165  // strip leading zeros (weird Kodak Scanner software)
166  while (isset($value[0]) and $value[0] == chr(0))
167  {
168    $value = substr($value, 1);
169  }
170  // remove binary nulls
171  $value = str_replace(chr(0x00), ' ', $value);
172
173  //~ pwg_log('<<<<< pwg_clean_iptc_value() returns '.var_export($value, TRUE).' <<<<<'."\n");
174  return $value;
175}
176
177/**
178 * returns informations from IPTC metadata, mapping is done at the beginning
179 * of the function
180 *
181 * @param string $filename
182 * @param string $map
183 * @return array
184 */
185function pwg_get_iptc_data($filename, $map)
186{
187  //~ pwg_log('>>>>> pwg_get_iptc_data ($filename = '.var_export($filename, TRUE).', $map = '.var_export($map, TRUE).') >>>>>'."\n");
188 
189  $result = array();
190
191  // Read IPTC data
192  $iptc = array();
193
194  $imginfo = array();
195  getimagesize($filename, $imginfo);
196
197  if (isset($imginfo['APP13']))
198  {
199    $iptc = iptcparse($imginfo['APP13']);
200    if (is_array($iptc))
201    {
202      $rmap = array_flip($map);
203      foreach (array_keys($rmap) as $iptc_key)
204      {
205        if (isset($iptc[$iptc_key][0]))
206        {
207          if ($iptc_key == '2#025')
208          {
209            $value = implode(',', array_map('pwg_clean_iptc_value', $iptc[$iptc_key]));
210          }
211          else
212          {
213            $value = pwg_clean_iptc_value($iptc[$iptc_key][0]);
214          }
215
216          foreach (array_keys($map, $iptc_key) as $pwg_key)
217          {
218            $result[$pwg_key] = $value;
219          }
220        }
221      }
222    }
223  }
224 
225  //~ pwg_log('<<<<< pwg_get_iptc_data() returns '.var_export($result, TRUE).' <<<<<'."\n");
226  return $result;
227}
228
229/**
230 * returns informations from IPTC metadata
231 *
232 * @param string $file
233 * @return array iptc
234 */
235function pwg_get_sync_iptc_data($file)
236{
237  //~ pwg_log('>>>>> pwg_get_sync_iptc_data ($file = '.var_export($file, TRUE).') >>>>>'."\n");
238
239  global $conf;
240
241  $map = $conf['use_iptc_mapping'];
242  $datefields = array('date_creation', 'date_available');
243
244  $iptc = pwg_get_iptc_data($file, $map);
245
246  foreach ($iptc as $pwg_key => $value)
247  {
248    if (in_array($pwg_key, $datefields))
249    {
250      if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
251      {
252        $value = $matches[1].'-'.$matches[2].'-'.$matches[3];
253      }
254    }
255    if ($pwg_key == 'keywords')
256    {
257      // official keywords separator is the comma
258      $value = preg_replace('/[.;]/', ',', $value);
259      $value = preg_replace('/^,+|,+$/', '', $value);
260    }
261    $iptc[$pwg_key] = htmlentities($value);
262  }
263
264  $iptc['keywords'] = implode(',', array_unique(explode(',', $iptc['keywords'])));
265
266  //~ pwg_log('<<<<< pwg_get_sync_iptc_data() returns '.var_export($iptc, TRUE).' <<<<<'."\n");
267  return $iptc;
268}
269
270/**
271 * return extension of the representative file
272 *
273 * @param string $file_dir
274 * @param string $file_short
275 * @return string
276 */
277function pwg_get_representative_ext($file_dir, $file_short)
278{
279  //~ pwg_log('>>>>> pwg_get_representative_ext($file_dir = '.var_export($file_dir, TRUE).', $file_short = '.var_export($file_short, TRUE).') >>>>>'."\n");
280 
281  global $conf;
282 
283  $rep_ext = '';
284  foreach ($conf['picture_ext'] as $ext)
285  {
286    if (file_exists($file_dir.'/'.$conf['represent'].'/'.$file_short.'.'.$ext))
287    {
288      $rep_ext = $ext;
289      break;
290    }
291  }
292 
293  //~ pwg_log('<<<<< pwg_get_representative_ext() returns '.var_export($rep_ext, TRUE).' <<<<<'."\n");
294  return $rep_ext; 
295}
296
297/**
298 * return 'true' if high resolution picture exists else ''
299 *
300 * @param string $file_dir
301 * @param string $file_base
302 * @return boolean
303 */
304function pwg_get_high($file_dir, $file_base)
305{
306  //~ pwg_log('>>>>> pwg_get_high($file = '.var_export($file_dir, TRUE).', $line = '.var_export($file_base, TRUE).') >>>>>'."\n");
307 
308  global $conf;
309 
310  $high = false;
311  if (file_exists($file_dir.'/'.$conf['high'].'/'.$file_base))
312  {
313    $high = true;
314  }
315 
316  //~ pwg_log('<<<<< pwg_get_high() returns '.var_export($high, TRUE).' <<<<<'."\n");
317  return $high; 
318}
319
320/**
321 * return filename without extension
322 *
323 * @param string $filename
324 * @return string
325 */
326function pwg_get_filename_wo_extension($filename)
327{
328  //~ pwg_log('>>>>> _get_filename_wo_extension($filename = '.var_export($filename, TRUE).') >>>>>'."\n");
329 
330  $short_name = substr($filename, 0, strrpos($filename, '.'));
331 
332  //~ pwg_log('<<<<< _get_filename_wo_extension() returns '.var_export($short_name, TRUE).' <<<<<'."\n");
333  return $short_name;
334}
335
336/**
337 * return extension of the thumbnail
338 *
339 * @param string $file_dir
340 * @param string $file_short
341 * @return string
342 */
343function pwg_get_thumbnail_ext($file_dir, $file_short)
344{
345  //~ pwg_log('>>>>> pwg_get_thumbnail_ext($file_dir = '.var_export($file_dir, TRUE).', $file_short = '.var_export($file_short, TRUE).') >>>>>'."\n");
346 
347  global $conf;
348 
349  $thumb_ext = '';
350  foreach ($conf['picture_ext'] as $ext)
351  {
352    if (file_exists($file_dir.'/'.$conf['thumbs'].'/'.$conf['prefix_thumbnail'].$file_short.'.'.$ext))
353    {
354      $thumb_ext = $ext;
355      break;
356    }
357  }
358 
359  //~ pwg_log('<<<<< pwg_get_thumbnail_ext() returns '.var_export($thumb_ext, TRUE).' <<<<<'."\n");
360  return $thumb_ext; 
361}
362
363/**
364 * completes xml line <element .../> and returns error log
365 *
366 * @param string $file
367 * @param string &$line
368 * @return string
369 */
370function pwg_scan_file($file_full, &$line)
371{
372  //~ pwg_log('>>>>> pwg_scan_file($file = '.var_export($file_full, TRUE).', $line = '.var_export($line, TRUE).') >>>>>'."\n");
373 
374  global $conf, $pwg_conf;
375 
376  $error_log ='';
377 
378  $file_base  = basename($file_full);
379  $file_short = pwg_get_filename_wo_extension($file_base);
380  $file_ext   = pwg_get_file_extension($file_base);
381  $file_dir   = dirname($file_full);
382
383  $element['file'] = $file_base;
384  $element['path'] = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']).substr($file_dir, 1).'/'.$file_base;
385 
386  if (in_array($file_ext, $conf['picture_ext']))
387  {
388    // Here we scan a picture : thumbnail is mandatory, high is optionnal, representative is not scanned
389    $element['tn_ext'] = pwg_get_thumbnail_ext($file_dir, $file_short);
390    if ($element['tn_ext'] != '')
391    {
392      // picture has a thumbnail, get image width, heigth, size in Mo
393      $element['filesize'] = floor(filesize($file_full) / 1024);
394      if ($image_size = getimagesize($file_full))
395      {
396        $element['width'] = $image_size[0];
397        $element['height'] = $image_size[1];
398      }
399     
400      // get high resolution
401      if (pwg_get_high($file_dir, $file_base))
402      {
403        $element['has_high'] = 'true';
404      }
405     
406      // get EXIF meta datas
407      if ($conf['use_exif'])
408      {
409        // Verify activation of exif module
410        if (extension_loaded('exif'))
411        {
412          if ($exif = read_exif_data($file_full))
413          {
414            foreach ($conf['use_exif_mapping'] as $pwg_key => $exif_key )
415            {
416              if (isset($exif[$exif_key]))
417              {
418                if ( in_array($pwg_key, array('date_creation','date_available') ) )
419                {
420                  if (preg_match('/^(\d{4}):(\d{2}):(\d{2})/', $exif[$exif_key], $matches))
421                    {
422                      $element[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
423                    }
424                }
425                else
426                {
427                  $element[$pwg_key] = $exif[$exif_key];
428                }
429              }
430            }
431          }
432        }
433      }
434     
435      // get IPTC meta datas
436      if ($conf['use_iptc'])
437      {
438        $iptc = pwg_get_sync_iptc_data($file_full);
439        if (count($iptc) > 0)
440        {
441          foreach (array_keys($iptc) as $key)
442          {
443            $element[$key] = addslashes($iptc[$key]);
444          }
445        }
446      }
447     
448    }
449    else
450    {
451      $error_log .= '          <code class="failure">Failure -</code> Thumbnail is missing for <code>'.$file_dir.'/'.$file_base.'</code>';
452      $error_log .= ' <img src="'.$pwg_conf['icon_dir'].'add_tag.png" title="'.$file_dir.'/thumbnail/'.$conf['prefix_thumbnail'].$file_short;
453      $error_log .= '.xxx ('.implode(', ', $conf['picture_ext']).')" /><br />'."\n";
454    }
455  }
456  else
457  {
458    // Here we scan a non picture file : thumbnail and high are unused, representative is optionnal
459    $ext = pwg_get_representative_ext($file_dir, $file_short);
460    if ($ext != '')
461    {
462      $element['representative_ext'] = $ext;
463    }
464  }
465 
466  if (strlen($error_log) == 0)
467  {
468    $line = pwg_get_indent('element').'<element ';
469    foreach($element as $key => $value)
470    {
471      $line .= $key.'="'.$value.'" ';
472    }
473    $line .= '/>'."\n";
474  }
475 
476  //~ pwg_log('<<<<< pwg_scan_file() returns '.var_export($error_log, TRUE).' <<<<<'."\n");
477  return $error_log;
478}
479
480/**
481 * returns current level in tree
482 *
483 * @return integer
484 */
485function pwg_get_level($dir)
486{
487  //~ pwg_log('>>>>> pwg_get_level($dir = '.var_export($dir, TRUE).') >>>>>'."\n");
488 
489  $level = substr_count($dir, '/') - 1; // -1 because of ./ at the beginning of path
490 
491  //~ pwg_log('<<<<< pwg_get_level() returns '.var_export($level, TRUE).' <<<<<'."\n");
492  return $level;
493}
494
495/**
496 * returns indentation of element
497 *
498 * @param string $element_type : 'root', 'element', 'dir'
499 * @return string
500 */
501function pwg_get_indent($element_type)
502{
503  //~ pwg_log('>>>>> pwg_get_indent($element_type = '.var_export($element_type, TRUE).') >>>>>'."\n");
504 
505  $level = substr_count($_SESSION['scan_list_fold'][0], '/') - 1; // because of ./ at the beginning
506  switch($element_type)
507  {
508    case 'dir' :
509    {
510      $indent = str_repeat(' ', 2*pwg_get_level($_SESSION['scan_list_fold'][0]));
511      break;
512    }
513    case 'root' :
514    {
515      $indent = str_repeat(' ', 2*pwg_get_level($_SESSION['scan_list_fold'][0])+2);
516      break;
517    }
518    case 'element' :
519    {
520      $indent = str_repeat(' ', 2*pwg_get_level($_SESSION['scan_list_fold'][0])+4);
521      break;
522    }
523    default :
524    {
525      $indent = '';
526      break;
527    }
528  }
529 
530  //~ pwg_log('<<<<< pwg_get_indent() returns '.var_export(strlen($indent), TRUE).' spaces <<<<<'."\n");
531  return $indent;
532}
533
534/**
535 * create index.php in directory and reserved sub_directories, return logs
536 *
537 * @param string dir
538 * @return string
539 */
540function pwg_protect_directories($directory)
541{
542  global $conf, $pwg_conf;
543 
544  //~ pwg_log('>>>>> pwg_protect_directories($directory = '.var_export($directory, true).') >>>>>'."\n");
545  $error_log = '';
546  $dirlist = array($directory, $directory.'/'.$conf['thumbs'], $directory.'/'.$conf['high'], $directory.'/'.$conf['represent']);
547 
548  foreach ($dirlist as $dir)
549  {
550    if (file_exists($dir))
551    {
552      if (!file_exists($dir.'/index.php'))
553      {
554        $file = @fopen($dir.'/index.php', 'w');
555        if ($file != false)
556        {
557          fwrite($file, $pwg_conf['protect_content']); // the return code should be verified
558          $error_log .= '          <code class="success">Success -</code> index.php created in directory <a href="'.$dir.'">'.$dir."</a><br />\n";
559          $_SESSION['scan_cnt_fold']++;
560        }
561        else
562        {
563          $error_log .= '          <code class="failure">Failure -</code> Can not create index.php in directory <code>'.$dir."</code><br />\n";
564        }
565      }
566      else
567      {
568        if ($pwg_conf['warning']['protect'])
569        {
570          $error_log .= '          <code class="warning">Warning -</code> index.php already exists in directory <a href="'.$dir.'">'.$dir."</a><br />\n";
571          $_SESSION['scan_cnt_fold']++;
572        }
573      }
574    }
575  }
576 
577  //~ pwg_log('<<<<< pwg_protect_directories() returns '.var_export($error_log, true).' <<<<<'."\n");
578  return $error_log;
579}
580
581/**
582 * returns file extension (.xxx)
583 *
584 * @param string $file
585 * @return string
586 */
587function pwg_get_file_extension($file)
588{
589  //~ pwg_log('>>>>> pwg_get_file_extension($file = '.var_export($file, true).') >>>>>'."\n");
590 
591  $ext = substr(strrchr($file, '.'), 1, strlen ($file));
592 
593  //~ pwg_log('<<<<< pwg_get_file_extension() returns '.var_export($ext, true).' <<<<<'."\n");
594  return $ext;
595}
596
597/**
598 * completes directory list of supported files and returns error logs
599 *
600 * @param string $directory
601 * @return string
602 */
603function pwg_get_file_list($directory)
604{
605  //~ pwg_log('>>>>> pwg_get_file_list($directory = '.var_export($directory, true).') >>>>>'."\n");
606 
607  global $conf, $pwg_conf;
608
609  $errorLog = '';
610  $dir = opendir($directory);
611  while (($file = readdir($dir)) !== false)
612  {
613    switch (filetype($directory."/".$file))
614    {
615      case 'file' :
616      {
617        if (in_array(pwg_get_file_extension($file), $conf['file_ext']))
618        {
619          // The file pointed is a regular file with a supported extension
620          array_push($_SESSION['scan_list_file'], $directory.'/'.$file);
621          //~ pwg_log('--->> Push in $_SESSION[scan_list_file] value "'.$directory.'/'.$file.'"'."\n");
622          if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
623          {
624            $errorLog .= '          <code class="failure">Failure -</code> Invalid file name for <code>'.$file.'</code> in <code>'.$directory.'</code>';
625            $errorLog .= ' <img src="'.$pwg_conf['icon_dir'].'add_tag.png"';
626            $errorLog .= ' title="Name should be composed of letters, figures, -, _ or . ONLY" /><br />'."\n";
627          }
628        }
629        break; // End of filetype FILE
630      }
631      case 'dir' :
632      {
633        if(!in_array($file, $pwg_conf['reserved_directory_names']))
634        {
635          // The file pointed is a directory but neither system directory nor reserved by PWG
636          array_push($_SESSION['scan_list_fold'], $directory.'/'.$file);
637          //~ pwg_log('--->> Push in $_SESSION[scan_list_fold] value "'.$directory.'/'.$file.'"'."\n");
638          if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
639          {
640            $errorLog .= '          <code class="failure">Failure -</code> Invalid directory name for <code>'.$directory.'/'.$file.'</code>';
641            $errorLog .= ' <img src="'.$pwg_conf['icon_dir'].'add_tag.png"';
642            $errorLog .= ' title="Name should be composed of letters, figures, -, _ or . ONLY" /><br />'."\n";
643          }
644        }
645        break; // End of filetype DIR
646      }
647      case 'fifo' :
648      case 'char' :
649      case 'block' :
650      case 'link' :
651      case 'unknown':
652      default :
653      {
654        // PWG does not manage these cases
655        break;
656      }
657    }
658  }
659  closedir($dir);
660 
661  //~ pwg_log('<<<<< pwg_get_file_list() returns '.var_export($errorLog, true).' <<<<<'."\n");
662
663  return $errorLog;
664}
665
666/**
667 * returns a float value coresponding to the number of seconds since the
668 * unix epoch (1st January 1970) and the microseconds are precised :
669 * e.g. 1052343429.89276600
670 *
671 * @return float
672 */
673function pwg_get_moment()
674{
675  //~ pwg_log('>>>>> pwg_get_moment() >>>>>'."\n");
676 
677  $t1 = explode(' ', microtime());
678  $t2 = explode('.', $t1[0]);
679  $t2 = $t1[1].'.'.$t2[1];
680
681  //~ pwg_log('<<<<< pwg_get_moment() returns '.var_export($t2, true).' <<<<<'."\n");
682  return $t2;
683}
684
685/**
686 * return true if HTTP_REFERER and PHP_SELF are similar
687 *
688 * return boolean
689 */
690function pwg_referer_is_me()
691{
692  //~ pwg_log('>>>>> pwg_referer_is_me() >>>>>'."\n");
693 
694  $response = false;
695  $server = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'];
696  $caller = $_SERVER['HTTP_REFERER'];
697
698  if (strcasecmp($server, $caller) == 0) {
699    $response = true;
700  }
701
702  //~ pwg_log('<<<<< pwg_referer_is_me() returns '.var_export($response, true).' <<<<<'."\n");
703  return $response;
704}
705
706// +-----------------------------------------------------------------------+
707// |                                pwg_<ACTION>_<STEP> FUNCTIONS          |
708// +-----------------------------------------------------------------------+
709
710function pwg_test_start()
711{
712  //~ pwg_log('>>>>> pwg_test_start() >>>>>'."\n");
713
714  global $g_message, $conf;
715 
716  if (isset($_REQUEST['version']))
717  {
718    if ($_REQUEST['version'] != $conf['version'])
719    {
720      $g_message = '0';
721    }
722    else
723    {
724      $g_message = '1';
725    }
726  }
727  else
728  {
729    $g_message  = '1';
730  }
731  $_SESSION['scan_step'] = 'exit';
732 
733  //~ pwg_log('<<<<< pwg_test_start() <<<<<'."\n");
734}
735
736function pwg_test_exit()
737{
738  //~ pwg_log('>>>>> pwg_test_exit() >>>>>'."\n");
739
740  global $g_header, $g_message, $g_footer, $conf, $pwg_conf;
741 
742  if (pwg_referer_is_me())
743  {
744    $g_header  = ' : <span class="success">Test</span>'."\n";
745    $g_message = '        This script is tagged : <code class="failure">'.$conf['version'].'</code>'."\n";
746    $g_footer  = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>'."\n";
747  }
748  else
749  {
750    // compare version in GET parameter with $conf['version']
751    if ($g_message == '1')
752    {
753      exit('<pre>PWG-INFO-2: test successful</pre>');
754    }
755    else
756    {
757      exit('<pre>PWG-ERROR-4: PhpWebGallery versions differs</pre>');
758    }
759  }
760 
761  //~ pwg_log('<<<<< pwg_test_exit() <<<<<'."\n");
762}
763
764function pwg_clean_start()
765{
766  //~ pwg_log('>>>>> pwg_clean_start() >>>>>'."\n");
767 
768  global $g_message;
769 
770  if(@unlink('./listing.xml'))
771  {
772    $g_message = '1';
773  }
774  else
775  {
776    $g_message = '0';
777  }
778
779  $_SESSION['scan_step'] = 'exit';
780 
781  //~ pwg_log('<<<<< pwg_clean_start() <<<<<'."\n");
782}
783
784function pwg_clean_exit()
785{
786  //~ pwg_log('>>>>> pwg_clean_exit() >>>>>'."\n");
787
788  global $g_header, $g_message, $g_footer, $conf, $pwg_conf;
789 
790  if(pwg_referer_is_me())
791  {
792    $g_header = ' : <span class="success">Clean</span>';
793    if ($g_message == '1')
794    {
795      $g_message = '        <code class="success">Success -</code> <code>listing.xml</code> file deleted'."\n";
796    }
797    else
798    {
799      $g_message = '        <code class="failure">Failure -</code> <code>listing.xml</code> does not exist or is read only'."\n";
800    }
801    $g_footer = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>';
802  }
803  else
804  {
805    if ($g_message == '1')
806    {
807      exit('<pre>PWG-INFO-3 : listing.xml file deleted</pre>');
808    }
809    else
810    {
811      exit('<pre>PWG-ERROR-3 : listing.xml does not exist</pre>');
812    }
813  }
814 
815  //~ pwg_log('<<<<< pwg_clean_exit() <<<<<'."\n");
816}
817
818function pwg_protect_start()
819{
820  //~ pwg_log('>>>>> pwg_protect_start() >>>>>'."\n");
821 
822  $_SESSION['scan_logs'] = pwg_get_file_list('.');
823  sort($_SESSION['scan_list_fold']);
824 
825  // Erase first file list because root directory does not contain images.
826  $_SESSION['scan_list_file'] = array();
827 
828  // What are we doing at next step
829  if(count($_SESSION['scan_list_fold']) > 0)
830  {
831    $_SESSION['scan_step'] = 'list';
832  }
833  else
834  {
835    $_SESSION['scan_step'] = 'stop';
836  }
837 
838  //~ pwg_log('<<<<< pwg_protect_start() <<<<<'."\n");
839}
840
841function pwg_protect_list()
842{
843  //~ pwg_log('>>>>> pwg_protect_list() >>>>>'."\n");
844
845  // Get list of files and directories
846  $_SESSION['scan_logs'] .= pwg_get_file_list($_SESSION['scan_list_fold'][0]);
847  sort($_SESSION['scan_list_fold']);
848 
849  // Delete unused file list
850  $_SESSION['scan_list_file'] = array();
851 
852  // Position next step
853  $_SESSION['scan_step'] = 'scan';
854 
855  //~ pwg_log('<<<<< pwg_protect_list() <<<<<'."\n");
856}
857
858function pwg_protect_scan()
859{
860  //~ pwg_log('>>>>> pwg_protect_scan() >>>>>'."\n");
861 
862  $_SESSION['scan_logs'] .= pwg_protect_directories($_SESSION['scan_list_fold'][0]);
863 
864  if (isset($_SESSION['scan_list_fold'][1]))
865  {
866    array_shift($_SESSION['scan_list_fold']);
867    $_SESSION['scan_step'] = 'list';
868  }
869  else
870  {
871    $_SESSION['scan_step'] = 'stop';
872  }
873 
874  //~ pwg_log('<<<<< pwg_protect_scan() <<<<<'."\n");
875}
876
877function pwg_protect_stop()
878{
879  //~ pwg_log('>>>>> pwg_protect_stop() >>>>>'."\n");
880 
881  global $g_header, $g_message, $g_footer, $pwg_conf;
882 
883  $time_elapsed = number_format(pwg_get_moment() - $_SESSION['scan_time'], 3, '.', ' ');
884
885  $g_header   = ' : <span class="success">Protect</span>';
886  $g_message  = '        <div>'."\n".$_SESSION['scan_logs'].'        </div>'."\n";
887  $g_message .= '        <div><code class="success">'.$_SESSION['scan_cnt_fold'].'</code> directories protected</div>'."\n";
888  $g_message .= '        <div style="{text-align: right;}">Gallery protected in : <code>'.$time_elapsed.' s</code></div>';
889  $g_footer   = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>';
890 
891  // What are we doing at next step
892  $_SESSION['scan_step'] = 'exit';
893 
894  //~ pwg_log('<<<<< pwg_protect_stop() <<<<<'."\n");
895}
896
897function pwg_generate_start()
898{
899  //~ pwg_log('>>>>> pwg_generate_start() >>>>>'."\n");
900  //~ pwg_log("GENARATE start >>>\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE start >>>\n");
901
902  global $g_listing, $conf;
903 
904  // Flush line <informations>
905  $xml_header_url = 'http://'.dirname($_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']).'/';
906  $xml_header_date = date('Y-m-d');
907  $xml_header_version = htmlentities($conf['version']);
908 
909  $attrs = array();
910  if ($conf['use_iptc'])
911  {
912    $attrs = array_merge($attrs, array_keys($conf['use_iptc_mapping']) );
913  }
914  if ($conf['use_exif'])
915  {
916    $attrs = array_merge($attrs, array_keys($conf['use_exif_mapping']) );
917  }
918  $xml_header_metadata = implode(',',array_unique($attrs));
919 
920  $xml_header = '<informations';
921  $xml_header .= ' generation_date="'.$xml_header_date.'"';
922  $xml_header .= ' phpwg_version="'.$xml_header_version.'"';
923  $xml_header .= ' metadata="'.$xml_header_metadata.'"';
924  $xml_header .= ' url="'.$xml_header_url.'"';
925  $xml_header .= '>'."\n";
926 
927  fwrite($g_listing, $xml_header);
928 
929  // Initialization of directory and file lists
930  $_SESSION['scan_list_fold'] = array();
931  $_SESSION['scan_list_file'] = array();
932  $_SESSION['scan_logs'] = pwg_get_file_list('.');
933  sort($_SESSION['scan_list_fold']);
934         
935  // Erase first file list because root directory does not contain images.
936  $_SESSION['scan_list_file'] = array();
937           
938  // What are we doing at next step
939  if(count($_SESSION['scan_list_fold']) > 0)
940  {
941    $_SESSION['scan_step'] = 'list';
942  }
943  else
944  {
945    $_SESSION['scan_step'] = 'stop';
946  }
947 
948  //~ pwg_log("GENARATE start <<<\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE start <<<\n");
949  //~ pwg_log('<<<<< pwg_generate_start() <<<<<'."\n");
950}
951
952function pwg_generate_list()
953{
954  //~ pwg_log('>>>>> pwg_generate_list() >>>>>'."\n");
955  //~ pwg_log("GENARATE list >>>\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE list >>>\n");
956 
957  global $g_listing;
958 
959  // Flush line <dirX name=""> in xml file
960  $dirname = basename($_SESSION['scan_list_fold'][0]);
961  $line = pwg_get_indent('dir').'<dir'.pwg_get_level($_SESSION['scan_list_fold'][0]).' name="'.$dirname.'">'."\n";
962  fwrite($g_listing, $line);
963 
964  // Get list of files and directories
965  $_SESSION['scan_logs'] .= pwg_get_file_list($_SESSION['scan_list_fold'][0]);
966  sort($_SESSION['scan_list_fold']); // Mandatory to keep the tree order
967  sort($_SESSION['scan_list_file']); // Easier to read when sorted
968 
969  // Flush line <root>
970  $line = pwg_get_indent('root').'<root>'."\n";
971  fwrite($g_listing, $line);
972 
973  // What are we doing at next step
974  $_SESSION['scan_step'] = 'scan';
975 
976  //~ pwg_log("GENARATE list <<<\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE list <<<\n");
977  //~ pwg_log('<<<<< pwg_generate_list() <<<<<'."\n");
978}
979
980function pwg_generate_scan()
981{
982  //~ pwg_log('>>>>> pwg_generate_scan() >>>>>'."\n");
983  //~ pwg_log("GENARATE scan >>>\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE scan >>>\n");
984 
985  global $g_listing;
986 
987  while (pwg_continue() and count($_SESSION['scan_list_file']) > 0)
988  {
989    $line = '';
990    $_SESSION['scan_logs'] .= pwg_scan_file($_SESSION['scan_list_file'][0], $line);
991    if (strlen($line) > 0)
992    {
993      fwrite($g_listing, $line);
994    }
995    //~ pwg_log('---<< Pull of $_SESSION[scan_list_file] value "'.$_SESSION['scan_list_file'][0].'"'."\n");
996    array_shift($_SESSION['scan_list_file']);
997    $_SESSION['scan_cnt_file']++;
998  }
999         
1000  if (count($_SESSION['scan_list_file']) <= 0)
1001  {
1002    // Flush line </root>
1003    $line = pwg_get_indent('root').'</root>'."\n";
1004    fwrite($g_listing, $line);
1005   
1006    // How many directories to close
1007    $current_level = pwg_get_level($_SESSION['scan_list_fold'][0]);
1008    if (isset($_SESSION['scan_list_fold'][1]))
1009    {
1010      //~ pwg_log('---<< Pull of $_SESSION[scan_list_fold] value "'.$_SESSION['scan_list_fold'][0].'"'."\n");
1011      array_shift($_SESSION['scan_list_fold']);
1012      $_SESSION['scan_cnt_fold']++;
1013      $next_level = pwg_get_level($_SESSION['scan_list_fold'][0]);
1014      $_SESSION['scan_step'] = 'list';
1015    }
1016    else
1017    {
1018      $next_level = -1;
1019      $_SESSION['scan_step'] = 'stop';
1020    }
1021   
1022    if ($current_level == $next_level)
1023    {
1024      fwrite($g_listing, pwg_close_level($current_level, 1));
1025    }
1026    else
1027    {
1028      if (($current_level > $next_level))
1029      {
1030        fwrite($g_listing, pwg_close_level($current_level, $current_level-$next_level+1));
1031      } // Nothing to do if current_level < next_level
1032    }
1033  }
1034 
1035  //~ pwg_log("GENERATE scan <<<\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE scan <<<\n");
1036  //~ pwg_log('<<<<< pwg_generate_scan() <<<<<'."\n");
1037}
1038
1039function pwg_generate_stop()
1040{
1041  //~ pwg_log('>>>>> pwg_generate_stop() >>>>>'."\n");
1042  //~ pwg_log("GENARATE stop >>>\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE stop >>>\n");
1043 
1044  global $pwg_conf, $g_listing, $g_header, $g_message, $g_footer;
1045 
1046  // Flush line </informations>
1047  fwrite($g_listing, '</informations>'."\n");
1048 
1049  // backup error log before cleaning session
1050  $time_elapsed = number_format(pwg_get_moment() - $_SESSION['scan_time'], 3, '.', ' ');
1051 
1052  $g_header   = ' : <span class="success">Generate</span>';
1053  $g_message  = '        <div>'."\n".$_SESSION['scan_logs'].'        </div>'."\n";
1054  $g_message .= '        <div><code class="success">'.$_SESSION['scan_cnt_fold'].'</code> directories parsed<br />'."\n";
1055  $g_message .= '        <code class="success">'.$_SESSION['scan_cnt_file'].'</code> files scanned</div>'."\n";
1056  $g_message .= '        <div>View <a href="listing.xml">listing.xml</a></div>'."\n";
1057  $g_message .= '        <div style="{text-align: right;}">Listing generated in : <code>'.$time_elapsed.' s</code></div>';
1058  $g_footer   = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>';
1059     
1060  // What are we doing at next step
1061  $_SESSION['scan_step'] = 'exit';
1062 
1063  //~ pwg_log("GENARATE stop <<<\n".var_export($_SESSION['scan_list_fold'], true)."\n".var_export($_SESSION['scan_list_file'], true)."\nGENERATE stop <<<\n");
1064  //~ pwg_log('<<<<< pwg_generate_stop() <<<<<'."\n");
1065}
1066
1067// +-----------------------------------------------------------------------+
1068// |                                ALWAYS CALLED FUNCTIONS                |
1069// +-----------------------------------------------------------------------+
1070
1071/**
1072 * This function check step and time ellapsed to determine end of loop
1073 *
1074 * @return bool
1075 */
1076function pwg_continue()
1077{
1078  //~ pwg_log('>>>>> pwg_continue() >>>>>'."\n");
1079 
1080  global $conf, $pwg_conf, $g_refresh, $g_header, $g_message, $g_footer, $start_time;
1081 
1082  if (!isset($_SESSION['scan_step']) or $_SESSION['scan_step'] == 'exit')
1083  {
1084    // evident end of process
1085    $return = false;
1086  }
1087  else
1088  {
1089    if ($pwg_conf['safe_mode'])
1090    {
1091      // can not reset the time
1092      $time_elapsed = pwg_get_moment() - $start_time;
1093      if ($time_elapsed < $conf['max_execution_time'])
1094      {
1095        $return = true;
1096      }
1097      else
1098      {
1099        $start_time = $_SESSION['scan_time'];
1100        $formated_time = number_format(pwg_get_moment() - $start_time, 3, '.', ' ');
1101
1102        $g_refresh = '<meta http-equiv="Refresh" content="'.$conf['refresh_delay'].'">'."\n";
1103        $g_header  = ' : <span class="success">'.ucfirst($_SESSION['scan_action']).'</span>';
1104        $g_message = '';
1105        if ($_SESSION['scan_cnt_fold'] != 0)
1106        {
1107          $g_message .= '<code class="success">'.$_SESSION['scan_cnt_fold'].'</code> directories scanned<br />'."\n";
1108        }
1109        if ($_SESSION['scan_cnt_file'] != 0)
1110        {
1111          $g_message .= '<code class="success">'.$_SESSION['scan_cnt_file'].'</code> files scanned<br />'."\n";
1112        }
1113        $nb = count($_SESSION['scan_list_fold']);
1114        if ($nb > 0)
1115        {
1116          $g_message .= '<code class="warning">'.$nb.'</code> directories to scan<br />'."\n";
1117        }
1118        $nb = count($_SESSION['scan_list_file']);
1119        if ($nb > 0)
1120        {
1121          $g_message .= '<code class="warning">'.$nb.'</code> files to scan<br />'."\n";
1122        }
1123        $g_message .= '        <div style="{text-align: right;}">Time elapsed : <code>'.$formated_time.' s</code></div>';
1124        $g_footer  = '<a href="'.$pwg_conf['this_url'].'?action='.$_SESSION['scan_action'].'" title="Continue"><img src="'.$pwg_conf['icon_dir'].'right.png" /></a>'."\n";
1125       
1126        $return = false;
1127      }
1128    }
1129    else
1130    {
1131      // reset the time
1132      set_time_limit(intval(ini_get('max_execution_time')));
1133      $return = true;
1134    }
1135  }
1136  //~ pwg_log('<<<<< pwg_continue() returns '.var_export($return, true).' <<<<<'."\n");
1137 
1138  return $return;
1139}
1140
1141/**
1142 * This function :
1143 * -> Verify the script call
1144 * -> Lock the script
1145 * -> Open listing.xml if action is 'generate'
1146 * -> Initialize output and session variables
1147 *
1148 * @return nothing
1149 */
1150function pwg_init()
1151{
1152  //~ pwg_log('>>>>> pwg_init() >>>>>'."\n");
1153
1154  global $g_message, $g_listing, $g_footer, $conf, $pwg_conf, $start_time;
1155 
1156  // Lock other script sessions, this lock will be remove during 'exit' step
1157  if (!isset($_SESSION['scan_step']))
1158  {
1159    $fp = @fopen(__FILE__.'.lock', 'x+'); // return false if __FILE__.lock exists or if cannot create
1160    if ($fp == false)
1161    {
1162      $g_header   = $_SESSION['scan_action'];
1163      $g_message  = '        <code class="failure">Failure -</code> Another script is running';
1164      $g_message .= ' <img src="'.$pwg_conf['icon_dir'].'add_tag.png" title="Delete file '.__FILE__.'.lock and retry" />';
1165      $g_message .= "\n";
1166      $g_footer   = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>'."\n";
1167      $_SESSION['scan_step'] = 'exit';
1168      //~ pwg_log('<<<<< pwg_init() failure <<<<<'."\n");
1169      return;
1170    }
1171    else
1172    {
1173      fwrite($fp, session_id()); // Writing session_id to trace lock
1174      fclose($fp);
1175      $_SESSION['scan_step'] = 'init';
1176    }
1177  }
1178 
1179  // Verify and backup parameter action. This backup will be removed during step 'exit'
1180  if (isset($_REQUEST['action']))
1181  {
1182    if (in_array($_REQUEST['action'], $pwg_conf['scan_action']))
1183    {
1184      if (isset($_SESSION['scan_action']))
1185      {
1186        if ($_SESSION['scan_action'] != $_REQUEST['action'])
1187        {
1188          // Fatal error
1189          $g_message  = '        <code class="failure">Failure -</code> Parameter <code>action</code> differs between url and session';
1190          $g_message .= "\n";
1191          $g_footer   = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>'."\n";
1192          $_SESSION['scan_step'] = 'exit';
1193          //~ pwg_log('<<<<< pwg_init() failure <<<<<'."\n");
1194          return;
1195        }
1196      }
1197      else
1198      {
1199        $_SESSION['scan_action'] = $_REQUEST['action'];
1200      }
1201    }
1202    else
1203    {
1204      // Fatal error
1205      $g_message  = '        <code class="failure">Failure -</code> Problem with <code>action</code> parameter';
1206      $g_message .= ' <img src="'.$pwg_conf['icon_dir'].'add_tag.png" title="empty, '.implode(', ', $pwg_conf['scan_action']).'" />';
1207      $g_message .= "\n";
1208      $g_footer   = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>'."\n";
1209      $_SESSION['scan_step'] = 'exit';
1210      //~ pwg_log('<<<<< pwg_init() failure <<<<<'."\n");
1211      return;
1212    }
1213  }
1214  else
1215  {
1216    // Here we are on welcome page
1217    $g_message  = '        <ul>'."\n";
1218    $g_message .= '          <li><a href="'.$pwg_conf['this_url'].'?action=test" title="Display/Compare script version">Test</a></li>'."\n";
1219    $g_message .= '          <li><a href="'.$pwg_conf['this_url'].'?action=clean" title="Delete listing.xml if exists">Clean</a></li>'."\n";
1220    $g_message .= '          <li><a href="'.$pwg_conf['this_url'].'?action=generate" title="Scan all images from this directory and write informations in listing.xml">Listing</a></li>'."\n";
1221    $g_message .= '          <li><a href="'.$pwg_conf['this_url'].'?action=protect" title="Protect all directories from this point with index.php">Protect</a></li>'."\n";
1222    $g_message .= '        </ul>'."\n";
1223    $g_footer   = '<a href="'.$conf['gallery'].'/admin.php?page=site_manager" title="Main gallery :: site manager">';
1224    $g_footer  .= '<img src="'.$pwg_conf['icon_dir'].'home.png" /></a>'."\n";
1225    $_SESSION['scan_step'] = 'exit';
1226  }
1227 
1228  // Open listing.xml
1229  if ($_SESSION['scan_action'] == 'generate')
1230  {
1231    $mode = ($_SESSION['scan_step'] == 'init') ? 'w' : 'a'; // Erase old listing.xml at the beginning of generation (mode w)
1232    $g_listing = @fopen('listing.xml', $mode);
1233    if ($g_listing === false)
1234    {
1235      $g_header   = $_SESSION['scan_action'];
1236      $g_message  = '        <code class="failure">Failure -</code> Can not write file <code>listing.xml</code>';
1237      $g_message .= "\n";
1238      $g_footer   = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>'."\n";
1239      $_SESSION['scan_step'] = 'exit';
1240      //~ pwg_log('<<<<< pwg_init() failure <<<<<'."\n");
1241      return;
1242    }
1243  }
1244 
1245  // Initializing session counters. This counters will be completely unset during step 'exit'
1246  if ($_SESSION['scan_step'] == 'init')
1247  {
1248    $_SESSION['scan_list_file'] = array();
1249    $_SESSION['scan_list_fold'] = array();
1250    $_SESSION['scan_cnt_file'] = 0;
1251    $_SESSION['scan_cnt_fold'] = 0;
1252    $_SESSION['scan_time'] = $start_time;
1253    $_SESSION['scan_step'] = 'start';
1254    $_SESSION['scan_logs'] = '';
1255  }
1256 
1257  //~ pwg_log('<<<<< pwg_init() success <<<<<'."\n");
1258}
1259
1260/**
1261 * This function :
1262 * -> Close listing.xml if action is 'generate'
1263 * -> Unlock the script
1264 * -> Erase session variables
1265 *
1266 * @return nothing
1267 */
1268function pwg_exit()
1269{
1270  //~ pwg_log('>>>>> pwg_exit() >>>>>'."\n");
1271 
1272  global $g_listing;
1273 
1274  // Close XML file
1275  if ($_SESSION['scan_action'] == 'generate' and $g_listing != false)
1276  {
1277    fclose($g_listing);
1278  }
1279 
1280  // Unlock script
1281  unlink(__FILE__.'.lock');
1282 
1283  // Erase session counters
1284  unset($_SESSION['scan_list_file']);
1285  unset($_SESSION['scan_list_fold']);
1286  unset($_SESSION['scan_cnt_file']);
1287  unset($_SESSION['scan_cnt_fold']);
1288  unset($_SESSION['scan_time']);
1289  unset($_SESSION['scan_step']);
1290  $local_action = $_SESSION['scan_action'];
1291  unset($_SESSION['scan_action']);
1292  unset($_SESSION['scan_logs']);
1293
1294
1295  // Call specific action post process
1296  if (is_callable('pwg_'.$local_action.'_exit'))
1297  {
1298    call_user_func('pwg_'.$local_action.'_exit');
1299  }
1300 
1301  //~ pwg_log('<<<<< pwg_exit() <<<<<'."\n");
1302}
1303
1304// +-----------------------------------------------------------------------+
1305// |                                script                                 |
1306// +-----------------------------------------------------------------------+
1307session_start();
1308
1309$start_time = pwg_get_moment();
1310
1311// Initializing message for web page
1312$g_refresh = '';
1313$g_header  = '';
1314$g_message = '';
1315$g_footer  = '';
1316$g_listing = '';
1317
1318pwg_init();
1319
1320while(pwg_continue())
1321{
1322  if (is_callable('pwg_'.$_SESSION['scan_action'].'_'.$_SESSION['scan_step']))
1323  {
1324    call_user_func('pwg_'.$_SESSION['scan_action'].'_'.$_SESSION['scan_step']); // Run the step : start, list, scan, stop are available
1325  }
1326  else
1327  {
1328    $g_header   = $_SESSION['scan_action'];
1329    $g_message  = '        <code class="failure">Failure -</code> INTERNAL STEP ERROR : <code>pwg_'.$_SESSION['scan_action'].'_'.$_SESSION['scan_step'].'()</code> undefined';
1330    $g_message .= "\n";
1331    $g_footer   = '<a href="'.$pwg_conf['this_url'].'" title="Main menu"><img src="'.$pwg_conf['icon_dir'].'up.png" /></a>'."\n";
1332    $_SESSION['scan_step'] = 'exit';
1333  }
1334}
1335
1336if ($_SESSION['scan_step'] == 'exit')
1337{
1338  pwg_exit();
1339}
1340
1341?>
1342<html>
1343  <head>
1344  <?php echo $g_refresh; ?>
1345  <title>Manage distant gallery</title>
1346  </head>
1347    <style type="text/css">
1348      code {font-weight: bold}
1349      img {border-style: none; vertical-align: middle}
1350      ul {list-style-image: url(<?php echo $pwg_conf['icon_dir']; ?>add_tag.png)}     
1351      .success {color: green}
1352      .warning {color: orange}
1353      .failure {color: red}
1354      .header {text-align: center; font-variant: small-caps; font-weight: bold;}
1355      .p {color: #F93;}
1356      .w {color: #ccc;}
1357      .g {color: #69C;}
1358      .pwg {text-decoration: none; border-bottom-style: dotted; border-bottom-width: 1px;}
1359      .content {width: 75%; position: absolute; top: 10%; left: 12%;}
1360      .footer {text-align: right;}
1361      .pwg_block {float: left;}
1362    </style>
1363  <body>
1364    <div class="content">
1365      <fieldset class="header">
1366        <span class="p">Php</span>
1367        <span class="w">Web</span>
1368        <span class="g">Gallery</span>
1369        &nbsp;distant site<? echo $g_header; ?>
1370      </fieldset>
1371      <fieldset>
1372<?php echo $g_message; ?>
1373      </fieldset>
1374      <fieldset class="footer">
1375        <div class="pwg_block">
1376          Powered by <a href="http://www.phpwebgallery.net" class="pwg"><span class="p">Php</span><span class="w">Web</span><span class="g">Gallery</span></a>
1377        </div>
1378        <?php echo $g_footer; ?>
1379      </fieldset>
1380    </div>
1381  </body>
1382</html>
Note: See TracBrowser for help on using the repository browser.