Changeset 1775


Ignore:
Timestamp:
Feb 1, 2007, 11:35:22 PM (17 years ago)
Author:
laurent_duretz
Message:

Issue 0000475 : Synchroniser plus de 7000 photos

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/site_manager.php

    r1704 r1775  
    195195      break;
    196196    }
     197    case 'protect' :
     198    {
     199      $title = $galleries_url.' : '.l10n('remote_site_protect');
     200      $template->assign_vars(array('REMOTE_SITE_TITLE'=>$title));
     201      remote_output($galleries_url.'create_listing_file.php?action=protect');
     202      break;
     203    }
    197204    case 'delete' :
    198205    {
     
    266273       array(
    267274         'U_TEST' => $base_url.'test',
    268          'U_GENERATE' => $base_url.'generate',
    269          'U_CLEAN' => $base_url.'clean'
     275         'U_GENERATE' => $row['galleries_url'].'create_listing_file.php?action=generate',
     276         'U_CLEAN' => $base_url.'clean',
     277         'U_PROTECT' => $base_url.'protect'
    270278         )
    271279       );
  • trunk/language/en_UK.iso-8859-1/admin.lang.php

    r1760 r1775  
    344344$lang['registration_date'] = 'registration date';
    345345$lang['remote_site'] = 'Remote site';
     346$lang['remote_site_protect'] = 'protect';
     347$lang['remote_site_protect_hint'] = 'create index.php in all directories';
    346348$lang['remote_site_clean'] = 'clean';
    347349$lang['remote_site_clean_hint'] = 'remove remote listing.xml file';
  • trunk/language/fr_FR.iso-8859-1/admin.lang.php

    r1760 r1775  
    344344$lang['registration_date'] = 'date d\'enregistrement';
    345345$lang['remote_site'] = 'Site distant';
     346$lang['remote_site_protect'] = 'protéger';
     347$lang['remote_site_protect_hint'] = 'créer un fichier index.php dans chaque répertoire';
    346348$lang['remote_site_clean'] = 'nettoyer';
    347349$lang['remote_site_clean_hint'] = 'supprimer le fichier listing.xml distant';
  • trunk/template/yoga/admin/site_manager.tpl

    r1699 r1775  
    4848      <br>
    4949      [<a href="{sites.site.remote.U_TEST}" title="{lang:remote_site_test_hint}" {TAG_INPUT_ENABLED}>{lang:remote_site_test}</a>]
    50       [<a href="{sites.site.remote.U_GENERATE}" title="{lang:remote_site_generate_hint}" {TAG_INPUT_ENABLED}>{lang:remote_site_generate}</a>]
     50      [<a href="{sites.site.remote.U_GENERATE}" title="{lang:remote_site_generate_hint}" target="_blank" {TAG_INPUT_ENABLED}>{lang:remote_site_generate}</a>]
    5151      [<a href="{sites.site.remote.U_CLEAN}" title="{lang:remote_site_clean_hint}" {TAG_INPUT_ENABLED}>{lang:remote_site_clean}</a>]
     52      [<a href="{sites.site.remote.U_PROTECT}" title="{lang:remote_site_protect_hint}" {TAG_INPUT_ENABLED}>{lang:remote_site_protect}</a>]
    5253    <!-- END remote -->
    5354    <!-- BEGIN plugin_links -->
  • trunk/tools/create_listing_file.php

    r1635 r1775  
    2727
    2828// +-----------------------------------------------------------------------+
    29 // |                              parameters                               |
    30 // +-----------------------------------------------------------------------+
     29// |                                User configuration                     |
     30// +-----------------------------------------------------------------------+
     31
     32// Srcipt version
     33$conf['version'] = 'Alligator';
    3134
    3235// prefix for thumbnails in "thumbnail" sub directories
    3336$conf['prefix_thumbnail'] = 'TN-';
    3437
    35 // $conf['file_ext'] lists all extensions (case insensitive) allowed for
    36 // your PhpWebGallery installation
    37 $conf['file_ext'] = array('jpg','JPG','jpeg','JPEG',
    38                           'png','PNG','gif','GIF','mpg','zip',
    39                           'avi','mp3','ogg');
     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');
    4040
    4141// $conf['picture_ext'] must be a subset of $conf['file_ext']
    42 $conf['picture_ext'] = array('jpg','JPG','jpeg','JPEG',
    43                              'png','PNG','gif','GIF');
    44 
    45 // $conf['version'] is used to verify the compatibility of the generated
    46 // listing.xml file and the PhpWebGallery version you're running
    47 $conf['version'] = 'Alligator';
     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');
    4855
    4956// $conf['use_exif'] set to true if you want to use Exif information
     
    6875  'author'          => '2#122',
    6976  'name'            => '2#005',
    70   'comment'         => '2#120'
    71   );
     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;
    72113
    73114// +-----------------------------------------------------------------------+
    74115// |                               functions                               |
    75116// +-----------------------------------------------------------------------+
     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}
    76176
    77177/**
     
    80180 *
    81181 * @param string $filename
     182 * @param string $map
    82183 * @return array
    83184 */
    84 function get_iptc_data($filename, $map)
    85 {
     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 
    86189  $result = array();
    87190
     
    104207          if ($iptc_key == '2#025')
    105208          {
    106             $value = implode(',',
    107                              array_map('clean_iptc_value',$iptc[$iptc_key]));
     209            $value = implode(',', array_map('pwg_clean_iptc_value', $iptc[$iptc_key]));
    108210          }
    109211          else
    110212          {
    111             $value = clean_iptc_value($iptc[$iptc_key][0]);
     213            $value = pwg_clean_iptc_value($iptc[$iptc_key][0]);
    112214          }
    113215
     
    120222    }
    121223  }
     224 
     225  //~ pwg_log('<<<<< pwg_get_iptc_data() returns '.var_export($result, TRUE).' <<<<<'."\n");
    122226  return $result;
    123227}
    124228
    125229/**
    126  * return a cleaned IPTC value
    127  *
    128  * @param string value
    129  * @return string
    130  */
    131 function clean_iptc_value($value)
    132 {
    133   // strip leading zeros (weird Kodak Scanner software)
    134   while ($value[0] == chr(0))
    135   {
    136     $value = substr($value, 1);
    137   }
    138   // remove binary nulls
    139   $value = str_replace(chr(0x00), ' ', $value);
    140 
    141   return $value;
    142 }
    143 
    144 function get_sync_iptc_data($file)
    145 {
     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
    146239  global $conf;
    147240
     
    149242  $datefields = array('date_creation', 'date_available');
    150243
    151   $iptc = get_iptc_data($file, $map);
     244  $iptc = pwg_get_iptc_data($file, $map);
    152245
    153246  foreach ($iptc as $pwg_key => $value)
     
    169262  }
    170263
    171   $iptc['keywords'] = implode(
    172                ',',
    173                array_unique(
    174                  explode(
    175                    ',',
    176                    $iptc['keywords']
    177                    )
    178                  )
    179                );
     264  $iptc['keywords'] = implode(',', array_unique(explode(',', $iptc['keywords'])));
     265
     266  //~ pwg_log('<<<<< pwg_get_sync_iptc_data() returns '.var_export($iptc, TRUE).' <<<<<'."\n");
    180267  return $iptc;
    181268}
    182269
    183270/**
    184  * returns a float value coresponding to the number of seconds since the
    185  * unix epoch (1st January 1970) and the microseconds are precised :
    186  * e.g. 1052343429.89276600
    187  *
    188  * @return float
    189  */
    190 function get_moment()
    191 {
    192   $t1 = explode(' ', microtime());
    193   $t2 = explode('.', $t1[0]);
    194   $t2 = $t1[1].'.'.$t2[1];
    195   return $t2;
    196 }
    197 
    198 /**
    199  * returns the number of seconds (with 3 decimals precision) between the
    200  * start time and the end time given.
    201  *
    202  * @param float start
    203  * @param float end
    204  * @return void
    205  */
    206 function get_elapsed_time($start, $end)
    207 {
    208   return number_format($end - $start, 3, '.', ' ').' s';
    209 }
    210 
    211 /**
    212  * returns an array with all picture files according to $conf['file_ext']
    213  *
    214  * @param string $dir
    215  * @return array
    216  */
    217 function get_pwg_files($dir)
    218 {
     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 
    219281  global $conf;
    220 
    221   $pictures = array();
    222   if ($opendir = opendir($dir))
    223   {
    224     while ($file = readdir($opendir))
    225     {
    226       if (in_array(get_extension($file), $conf['file_ext']))
    227       {
    228         array_push($pictures, $file);
    229         if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
     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'))
    230411        {
    231           echo 'PWG-WARNING-2: "'.$file.'" : ';
    232           echo 'The name of the file should be composed of ';
    233           echo 'letters, figures, "-", "_" or "." ONLY';
    234           echo "\n";
    235         }
    236       }
    237     }
    238   }
    239   return $pictures;
    240 }
    241 
    242 /**
    243  * returns an array with all thumbnails according to $conf['picture_ext']
    244  * and $conf['prefix_thumbnail']
    245  *
    246  * @param string $dir
    247  * @return array
    248  */
    249 function get_thumb_files($dir)
    250 {
    251   global $conf;
    252 
    253   $prefix_length = strlen($conf['prefix_thumbnail']);
    254 
    255   $thumbnails = array();
    256   if (is_dir($dir.'/thumbnail'))
    257   {
    258     if ($opendir = opendir($dir.'/thumbnail'))
    259     {
    260       while ($file = readdir($opendir))
    261       {
    262         if (in_array(get_extension($file), $conf['picture_ext'])
    263             and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'])
    264         {
    265           array_push($thumbnails, $file);
    266         }
    267       }
    268     }
    269   }
    270   return $thumbnails;
    271 }
    272 
    273 /**
    274  * returns an array with representative picture files of a directory
    275  * according to $conf['picture_ext']
    276  *
    277  * @param string $dir
    278  * @return array
    279  */
    280 function get_representative_files($dir)
    281 {
    282   global $conf;
    283 
    284   $pictures = array();
    285   if (is_dir($dir.'/pwg_representative'))
    286   {
    287     if ($opendir = opendir($dir.'/pwg_representative'))
    288     {
    289       while ($file = readdir($opendir))
    290       {
    291         if (in_array(get_extension($file), $conf['picture_ext']))
    292         {
    293           array_push($pictures, $file);
    294         }
    295       }
    296     }
    297   }
    298   return $pictures;
    299 }
    300 
    301 /**
    302  * returns an array with high quality/resolution picture files of a directory
    303  * according to $conf['picture_ext']
    304  *
    305  * @param string $dir
    306  * @return array
    307  */
    308 function get_high_files($dir)
    309 {
    310   global $conf;
    311 
    312   $pictures = array();
    313   if (is_dir($dir.'/pwg_high'))
    314   {
    315     if ($opendir = opendir($dir.'/pwg_high'))
    316     {
    317       while ($file = readdir($opendir))
    318       {
    319         if (in_array(get_extension($file), $conf['picture_ext']))
    320         {
    321           array_push($pictures, $file);
    322         }
    323       }
    324     }
    325   }
    326   return $pictures;
    327 }
    328 
    329 /**
    330  * search in $basedir the sub-directories and calls get_pictures
    331  *
    332  * @return void
    333  */
    334 function get_dirs($basedir, $indent, $level)
    335 {
    336   $fs_dirs = array();
    337   $dirs = "";
    338   global $conf_safe_mode;
    339 
    340   // Refresh the max_execution_time to avoid timout error
    341   // By default time to scan a directory (without subdirs) is fixed to 30 seconds
    342   if (!$conf_safe_mode)
    343   {
    344     set_time_limit(30);
    345   }
    346 
    347   if ($opendir = opendir($basedir))
    348   {
    349     while ($file = readdir($opendir))
    350     {
    351       if ($file != '.'
    352           and $file != '..'
    353           and $file != '.svn'
    354           and $file != 'thumbnail'
    355           and $file != 'pwg_high'
    356           and $file != 'pwg_representative'
    357           and is_dir ($basedir.'/'.$file))
    358       {
    359         array_push($fs_dirs, $file);
    360         if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
    361         {
    362           echo 'PWG-WARNING-1: "'.$file.'" : ';
    363           echo 'The name of the directory should be composed of ';
    364           echo 'letters, figures, "-", "_" or "." ONLY';
    365           echo "\n";
    366         }
    367       }
    368     }
    369   }
    370   // write of the dirs
    371   foreach ($fs_dirs as $fs_dir)
    372   {
    373     $dirs.= "\n".$indent.'<dir'.$level.' name="'.$fs_dir.'">';
    374     $dirs.= get_pictures($basedir.'/'.$fs_dir, $indent.'  ');
    375     $dirs.= get_dirs($basedir.'/'.$fs_dir, $indent.'  ', $level + 1);
    376     $dirs.= "\n".$indent.'</dir'.$level.'>';
    377   }
    378   return $dirs;
    379 }
    380 
    381 // get_extension returns the part of the string after the last "."
    382 function get_extension($filename)
    383 {
    384   return substr(strrchr($filename, '.'), 1, strlen ($filename));
    385 }
    386 
    387 // get_filename_wo_extension returns the part of the string before the last
    388 // ".".
    389 // get_filename_wo_extension('test.tar.gz') -> 'test.tar'
    390 function get_filename_wo_extension($filename)
    391 {
    392   return substr($filename, 0, strrpos($filename, '.'));
    393 }
    394 
    395 function get_pictures($dir, $indent)
    396 {
    397   global $conf, $page;
    398 
    399   // fs means FileSystem : $fs_files contains files in the filesystem found
    400   // in $dir that can be managed by PhpWebGallery (see get_pwg_files
    401   // function), $fs_thumbnails contains thumbnails, $fs_representatives
    402   // contains potentially representative pictures for non picture files
    403   $fs_files = get_pwg_files($dir);
    404   $fs_thumbnails = get_thumb_files($dir);
    405   $fs_representatives = get_representative_files($dir);
    406   $fs_highs = get_high_files($dir);
    407 
    408   $elements = array();
    409 
    410   $print_dir = preg_replace('/^\.\//', '', $dir);
    411   $print_dir = preg_replace('/\/*$/', '/', $print_dir);
    412 
    413   foreach ($fs_files as $fs_file)
    414   {
    415     $element = array();
    416     $element['file'] = $fs_file;
    417     $element['path'] = $page['url'].$print_dir.$fs_file;
    418     $element['filesize'] = floor(filesize($dir.'/'.$fs_file) / 1024);
    419 
    420     $file_wo_ext = get_filename_wo_extension($fs_file);
    421 
    422     foreach ($conf['picture_ext'] as $ext)
    423     {
    424       $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
    425       if (!in_array($test, $fs_thumbnails))
    426       {
    427         continue;
    428       }
    429       else
    430       {
    431         $element['tn_ext'] = $ext;
    432         break;
    433       }
    434     }
    435 
    436     // 2 cases : the element is a picture or not. Indeed, for a picture
    437     // thumbnail is mandatory, high is optional and for non picture element,
    438     // thumbnail and representative is optionnal
    439     if (in_array(get_extension($fs_file), $conf['picture_ext']))
    440     {
    441       // if we found a thumnbnail corresponding to our picture...
    442       if (isset($element['tn_ext']))
    443       {
    444         if ($image_size = @getimagesize($dir.'/'.$fs_file))
    445         {
    446           $element['width'] = $image_size[0];
    447           $element['height'] = $image_size[1];
    448         }
    449 
    450         if ( in_array($fs_file, $fs_highs) )
    451         {
    452           $element['has_high'] = 'true';
    453         }
    454 
    455         if ($conf['use_exif'])
    456         {
    457           // Verify activation of exif module
    458           if (extension_loaded('exif'))
     412          if ($exif = read_exif_data($file_full))
    459413          {
    460             if ($exif = read_exif_data($dir.'/'.$fs_file))
     414            foreach ($conf['use_exif_mapping'] as $pwg_key => $exif_key )
    461415            {
    462               foreach ($conf['use_exif_mapping'] as $pwg_key => $exif_key )
     416              if (isset($exif[$exif_key]))
    463417              {
    464                 if (isset($exif[$exif_key]))
     418                if ( in_array($pwg_key, array('date_creation','date_available') ) )
    465419                {
    466                   if ( in_array($pwg_key, array('date_creation','date_available') ) )
    467                   {
    468                      if (preg_match('/^(\d{4}):(\d{2}):(\d{2})/'
    469                            ,$exif[$exif_key]
    470                            ,$matches))
    471                      {
    472                        $element[$pwg_key] =
    473                           $matches[1].'-'.$matches[2].'-'.$matches[3];
    474                      }
    475                   }
    476                   else
    477                   {
    478                     $element[$pwg_key] = $exif[$exif_key];
    479                   }
     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];
    480428                }
    481429              }
     
    483431          }
    484432        }
    485 
    486         if ($conf['use_iptc'])
     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)
    487440        {
    488           $iptc = get_sync_iptc_data($dir.'/'.$fs_file);
    489           if (count($iptc) > 0)
     441          foreach (array_keys($iptc) as $key)
    490442          {
    491             foreach (array_keys($iptc) as $key)
    492             {
    493               $element[$key] = $iptc[$key];
    494             }
     443            $element[$key] = addslashes($iptc[$key]);
    495444          }
    496445        }
    497 
    498         array_push($elements, $element);
    499       }
    500       else
    501       {
    502         echo 'PWG-ERROR-1: The thumbnail is missing for '.$dir.'/'.$fs_file;
    503         echo '-> '.$dir.'/thumbnail/';
    504         echo $conf['prefix_thumbnail'].$file_wo_ext.'.xxx';
    505         echo ' ("xxx" can be : ';
    506         echo implode(', ', $conf['picture_ext']);
    507         echo ')'."\n";
    508       }
     446      }
     447     
    509448    }
    510449    else
    511450    {
    512       foreach ($conf['picture_ext'] as $ext)
    513       {
    514         $candidate = $file_wo_ext.'.'.$ext;
    515         if (!in_array($candidate, $fs_representatives))
     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)
    516556        {
    517           continue;
     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']++;
    518560        }
    519561        else
    520562        {
    521           $element['representative_ext'] = $ext;
    522           break;
     563          $error_log .= '          <code class="failure">Failure -</code> Can not create index.php in directory <code>'.$dir."</code><br />\n";
    523564        }
    524565      }
    525 
    526       array_push($elements, $element);
    527     }
    528   }
    529 
    530   $xml = "\n".$indent.'<root>';
    531   $attributes = array('file','tn_ext','representative_ext','filesize',
    532                       'width','height','date_creation','author','keywords',
    533                       'name','comment','has_high', 'path');
    534   foreach ($elements as $element)
    535   {
    536     $xml.= "\n".$indent.'  ';
    537     $xml.= '<element';
    538     foreach ($attributes as $attribute)
    539     {
    540       if (isset($element{$attribute}))
    541       {
    542         $xml.= ' '.$attribute.'="'.$element{$attribute}.'"';
    543       }
    544     }
    545     $xml.= ' />';
    546   }
    547   $xml.= "\n".$indent.'</root>';
    548 
    549   return $xml;
     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");
    5501302}
    5511303
     
    5531305// |                                script                                 |
    5541306// +-----------------------------------------------------------------------+
    555 if (isset($_GET['action']))
    556 {
    557   $page['action'] = $_GET['action'];
    558 }
    559 else
    560 {
    561   $page['action'] = '';
    562 }
    563 
    564 // Looking at the safe_mode configuration for execution time
    565 $conf_safe_mode = TRUE;
    566 if (ini_get('safe_mode') == 0)
    567 {
    568   $conf_safe_mode = FALSE;
    569 }
    570 
    571 echo '<pre>';
    572 switch ($page['action'])
    573 {
    574   case 'generate' :
    575   {
    576     $start = get_moment();
    577 
    578     $listing = '<informations';
    579     $listing.= ' generation_date="'.date('Y-m-d').'"';
    580     $listing.= ' phpwg_version="'.htmlentities($conf{'version'}).'"';
    581 
    582     $attrs=array();
    583     if ($conf['use_iptc'])
    584     {
    585       $attrs = array_merge($attrs, array_keys($conf['use_iptc_mapping']) );
    586     }
    587     if ($conf['use_exif'])
    588     {
    589       $attrs = array_merge($attrs, array_keys($conf['use_exif_mapping']) );
    590     }
    591     $listing.= ' metadata="'.implode(',',array_unique($attrs)).'"';
    592 
    593     $end = strrpos($_SERVER['PHP_SELF'], '/') + 1;
    594     $local_folder = substr($_SERVER['PHP_SELF'], 0, $end);
    595     $page['url'] = 'http://'.$_SERVER['HTTP_HOST'].$local_folder;
    596 
    597     $listing.= ' url="'.$page['url'].'"';
    598     $listing.= '/>'."\n";
    599 
    600     $listing.= get_dirs('.', '', 0);
    601 
    602     if ($fp = @fopen("./listing.xml","w"))
    603     {
    604       fwrite($fp, $listing);
    605       fclose($fp);
    606       echo 'PWG-INFO-1: listing.xml created in ';
    607       echo get_elapsed_time($start, get_moment());
    608       echo "\n";
    609     }
    610     else
    611     {
    612       echo "PWG-ERROR-2: I can't write the file listing.xml"."\n";
    613     }
    614     break;
    615   }
    616   case 'test' :
    617   {
    618     if (isset($_GET['version']))
    619     {
    620       if ($_GET['version'] != $conf['version'])
    621       {
    622         echo 'PWG-ERROR-4: PhpWebGallery versions differs'."\n";
    623       }
    624       else
    625       {
    626         echo 'PWG-INFO-2: test successful'."\n";
    627       }
    628     }
    629     else
    630     {
    631       echo 'PWG-INFO-2: test successful'."\n";
    632     }
    633     break;
    634   }
    635   case 'clean' :
    636   {
    637     if( @unlink('./listing.xml'))
    638     {
    639       echo 'PWG-INFO-3 : listing.xml file deleted'."\n";
    640     }
    641     else
    642     {
    643       echo 'PWG-ERROR-3 : listing.xml does not exist'."\n";
    644     }
    645     break;
    646   }
    647   default :
    648   {
    649     // Menu de lancement pour la mise à jour manuel des sites distant
    650     echo '</pre>
    651 <ul>
    652   <li>
    653     <a href="create_listing_file.php?action=generate">Generate listing.xml</a>
    654   </li>
    655 
    656   <li>
    657     <a href="create_listing_file.php?action=test">Test</a>
    658   </li>
    659 
    660   <li>
    661     <a href="create_listing_file.php?action=clean">Clean</a>
    662   </li>
    663 </ul>
    664 <pre>';
    665   }
    666 }
    667 echo '</pre>';
     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
    6681341?>
     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 TracChangeset for help on using the changeset viewer.