Changeset 526 for trunk/admin


Ignore:
Timestamp:
Sep 18, 2004, 6:09:47 PM (20 years ago)
Author:
z0rglub
Message:
  • new version of create_file_listing.php for future 1.4 branch
  • new feature : remote site management (generate listing, update, clean, delete)
  • on category page, no display of filesize if info not available in database
Location:
trunk/admin
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/create_listing_file.php

    r362 r526  
    2525// | USA.                                                                  |
    2626// +-----------------------------------------------------------------------+
     27
     28// +-----------------------------------------------------------------------+
     29// |                              parameters                               |
     30// +-----------------------------------------------------------------------+
     31
     32// prefix for thumbnails in "thumbnail" sub directories
    2733$conf['prefix_thumbnail'] = 'TN-';
    28 $conf['picture_ext'] = array ( 'jpg', 'gif', 'png', 'JPG', 'GIF', 'PNG' );
    29 
    30 $listing = '';
    31 
    32 $end = strrpos( $_SERVER['PHP_SELF'], '/' ) + 1;
    33 $local_folder = substr( $_SERVER['PHP_SELF'], 0, $end );
    34 $url = 'http://'.$_SERVER['HTTP_HOST'].$local_folder;
    35 
    36 $listing.= '<url>'.$url.'</url>';
    37 
    38 /**
    39  * returns an array with all picture files according to $conf['picture_ext']
     34
     35// $conf['file_ext'] lists all extensions (case insensitive) allowed for
     36// your PhpWebGallery installation
     37$conf['file_ext'] = array('jpg','JPG','png','PNG','gif','GIF','mpg','zip',
     38                          'avi','mp3','ogg');
     39
     40// $conf['picture_ext'] must bea subset of $conf['file_ext']
     41$conf['picture_ext'] = array('jpg','JPG','png','PNG','gif','GIF');
     42
     43// $conf['version'] is used to verify the compatibility of the generated
     44// listing.xml file and the PhpWebGallery version you're running
     45$conf['version'] = 'BSF';
     46
     47// +-----------------------------------------------------------------------+
     48// |                               functions                               |
     49// +-----------------------------------------------------------------------+
     50
     51/**
     52 * returns a float value coresponding to the number of seconds since the
     53 * unix epoch (1st January 1970) and the microseconds are precised :
     54 * e.g. 1052343429.89276600
     55 *
     56 * @return float
     57 */
     58function get_moment()
     59{
     60  $t1 = explode(' ', microtime());
     61  $t2 = explode('.', $t1[0]);
     62  $t2 = $t1[1].'.'.$t2[1];
     63  return $t2;
     64}
     65
     66/**
     67 * returns the number of seconds (with 3 decimals precision) between the
     68 * start time and the end time given.
     69 *
     70 * @param float start
     71 * @param float end
     72 * @return void
     73 */
     74function get_elapsed_time($start, $end)
     75{
     76  return number_format($end - $start, 3, '.', ' ').' s';
     77}
     78
     79/**
     80 * returns an array with all picture files according to $conf['file_ext']
    4081 *
    4182 * @param string $dir
    4283 * @return array
    4384 */
    44 function get_picture_files( $dir )
     85function get_pwg_files($dir)
    4586{
    4687  global $conf;
    4788
    4889  $pictures = array();
    49   if ( $opendir = opendir( $dir ) )
    50   {
    51     while ( $file = readdir( $opendir ) )
    52     {
    53       if ( in_array( get_extension( $file ), $conf['picture_ext'] ) )
    54       {
    55         array_push( $pictures, $file );
     90  if ($opendir = opendir($dir))
     91  {
     92    while ($file = readdir($opendir))
     93    {
     94      if (in_array(get_extension($file), $conf['file_ext']))
     95      {
     96        array_push($pictures, $file);
     97        if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
     98        {
     99          echo 'PWG-WARNING-2: "'.$file.'" : ';
     100          echo 'The name of the file should be composed of ';
     101          echo 'letters, figures, "-", "_" or "." ONLY';
     102          echo "\n";
     103        }
    56104      }
    57105    }
     
    67115 * @return array
    68116 */
    69 function get_thumb_files( $dir )
     117function get_thumb_files($dir)
    70118{
    71119  global $conf;
    72120
    73   $prefix_length = strlen( $conf['prefix_thumbnail'] );
     121  $prefix_length = strlen($conf['prefix_thumbnail']);
    74122 
    75123  $thumbnails = array();
    76   if ( $opendir = @opendir( $dir ) )
    77   {
    78     while ( $file = readdir( $opendir ) )
    79     {
    80       if ( in_array( get_extension( $file ), $conf['picture_ext'] )
    81            and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'] )
    82       {
    83         array_push( $thumbnails, $file );
     124  if ($opendir = @opendir($dir.'/thumbnail'))
     125  {
     126    while ($file = readdir($opendir))
     127    {
     128      if (in_array(get_extension($file), $conf['picture_ext'])
     129          and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'])
     130      {
     131        array_push($thumbnails, $file);
    84132      }
    85133    }
     
    88136}
    89137
    90 // get_dirs retourne un tableau contenant tous les sous-répertoires d'un
    91 // répertoire
    92 function get_dirs( $basedir, $indent, $level )
     138/**
     139 * returns an array with representative picture files of a directory
     140 * according to $conf['picture_ext']
     141 *
     142 * @param string $dir
     143 * @return array
     144 */
     145function get_representative_files($dir)
     146{
     147  global $conf;
     148
     149  $pictures = array();
     150  if ($opendir = @opendir($dir.'/representative'))
     151  {
     152    while ($file = readdir($opendir))
     153    {
     154      if (in_array(get_extension($file), $conf['picture_ext']))
     155      {
     156        array_push($pictures, $file);
     157      }
     158    }
     159  }
     160  return $pictures;
     161}
     162
     163/**
     164 * search in $basedir the sub-directories and calls get_pictures
     165 *
     166 * @return void
     167 */
     168function get_dirs($basedir, $indent, $level)
    93169{
    94170  $fs_dirs = array();
    95171  $dirs = "";
    96172
    97   if ( $opendir = opendir( $basedir ) )
    98   {
    99     while ( $file = readdir( $opendir ) )
    100     {
    101       if ( $file != '.'
    102            and $file != '..'
    103            and is_dir ( $basedir.'/'.$file )
    104            and $file != 'thumbnail' )
    105       {
    106         array_push( $fs_dirs, $file );
    107         if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
    108         {
    109           echo '<span style="color:red;">"'.$file.'" : ';
     173  if ($opendir = opendir($basedir))
     174  {
     175    while ($file = readdir($opendir))
     176    {
     177      if ($file != '.'
     178          and $file != '..'
     179          and is_dir ($basedir.'/'.$file)
     180          and $file != 'thumbnail')
     181      {
     182        array_push($fs_dirs, $file);
     183        if (!preg_match('/^[a-zA-Z0-9-_.]+$/', $file))
     184        {
     185          echo 'PWG-WARNING-1: "'.$file.'" : ';
    110186          echo 'The name of the directory should be composed of ';
    111187          echo 'letters, figures, "-", "_" or "." ONLY';
    112           echo '</span><br />';
     188          echo "\n";
    113189        }
    114190      }
     
    116192  }
    117193  // write of the dirs
    118   foreach ( $fs_dirs as $fs_dir ) {
     194  foreach ($fs_dirs as $fs_dir)
     195  {
    119196    $dirs.= "\n".$indent.'<dir'.$level.' name="'.$fs_dir.'">';
    120     $dirs.= get_pictures( $basedir.'/'.$fs_dir, $indent.'  ' );
    121     $dirs.= get_dirs( $basedir.'/'.$fs_dir, $indent.'  ', $level + 1 );
     197    $dirs.= get_pictures($basedir.'/'.$fs_dir, $indent.'  ');
     198    $dirs.= get_dirs($basedir.'/'.$fs_dir, $indent.'  ', $level + 1);
    122199    $dirs.= "\n".$indent.'</dir'.$level.'>';
    123200  }
     
    126203
    127204// get_extension returns the part of the string after the last "."
    128 function get_extension( $filename )
    129 {
    130   return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) );
     205function get_extension($filename)
     206{
     207  return substr(strrchr($filename, '.'), 1, strlen ($filename));
    131208}
    132209
    133210// get_filename_wo_extension returns the part of the string before the last
    134211// ".".
    135 // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar'
    136 function get_filename_wo_extension( $filename )
    137 {
    138   return substr( $filename, 0, strrpos( $filename, '.' ) );
    139 }
    140 
    141 function get_pictures( $dir, $indent )
     212// get_filename_wo_extension('test.tar.gz') -> 'test.tar'
     213function get_filename_wo_extension($filename)
     214{
     215  return substr($filename, 0, strrpos($filename, '.'));
     216}
     217
     218function get_pictures($dir, $indent)
    142219{
    143220  global $conf;
    144221 
    145   // fs means filesystem : $fs_pictures contains pictures in the filesystem
    146   // found in $dir, $fs_thumbnails contains thumbnails...
    147   $fs_pictures   = get_picture_files( $dir );
    148   $fs_thumbnails = get_thumb_files( $dir.'/thumbnail' );
    149 
    150   $root = "\n".$indent.'<root>';
    151 
    152   foreach ( $fs_pictures as $fs_picture ) {
    153     $file_wo_ext = get_filename_wo_extension( $fs_picture );
    154     $tn_ext = '';
    155     foreach ( $conf['picture_ext'] as $ext ) {
     222  // fs means FileSystem : $fs_files contains files in the filesystem found
     223  // in $dir that can be managed by PhpWebGallery (see get_pwg_files
     224  // function), $fs_thumbnails contains thumbnails, $fs_representatives
     225  // contains potentially representative pictures for non picture files
     226  $fs_files = get_pwg_files($dir);
     227  $fs_thumbnails = get_thumb_files($dir);
     228  $fs_representatives = get_representative_files($dir);
     229
     230  $elements = array();
     231 
     232  foreach ($fs_files as $fs_file)
     233  {
     234    $element = array();
     235    $element['file'] = $fs_file;
     236    $element['filesize'] = floor(filesize($dir.'/'.$fs_file) / 1024);
     237   
     238    $file_wo_ext = get_filename_wo_extension($fs_file);
     239
     240    foreach ($conf['picture_ext'] as $ext)
     241    {
    156242      $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
    157       if ( !in_array( $test, $fs_thumbnails ) ) continue;
    158       else { $tn_ext = $ext; break; }
    159     }
    160     // if we found a thumnbnail corresponding to our picture...
    161     if ( $tn_ext != '' )
    162     {
    163       list( $width,$height ) = @getimagesize( $dir.'/'.$fs_picture );
    164 
    165       $root.= "\n".$indent.'  ';
    166       $root.= '<picture';
    167       $root.= ' file="'.    $fs_picture.'"';
    168       $root.= ' tn_ext="'.  $tn_ext.'"';
    169       $root.= ' filesize="'.floor(filesize($dir.'/'.$fs_picture)/1024).'"';
    170       $root.= ' width="'.   $width.'"';
    171       $root.= ' height="'.  $height.'"';
    172       $root.= ' />';
     243      if (!in_array($test, $fs_thumbnails))
     244      {
     245        continue;
     246      }
     247      else
     248      {
     249        $element['tn_ext'] = $ext;
     250        break;
     251      }
     252    }
     253
     254    // 2 cases : the element is a picture or not. Indeed, for a picture
     255    // thumbnail is mandatory and for non picture element, thumbnail and
     256    // representative is optionnal
     257    if (in_array(get_extension($fs_file), $conf['picture_ext']))
     258    {
     259      // if we found a thumnbnail corresponding to our picture...
     260      if (isset($element['tn_ext']))
     261      {
     262        if ($image_size = @getimagesize($dir.'/'.$fs_file))
     263        {
     264          $element['width'] = $image_size[0];
     265          $element['height'] = $image_size[1];
     266        }
     267       
     268        array_push($elements, $element);
     269      }
     270      else
     271      {
     272        echo 'PWG-ERROR-1: The thumbnail is missing for '.$dir.'/'.$fs_file;
     273        echo '-> '.$dir.'/thumbnail/';
     274        echo $conf['prefix_thumbnail'].$file_wo_ext.'.xxx';
     275        echo ' ("xxx" can be : ';
     276        echo implode(', ', $conf['picture_ext']);
     277        echo ')'."\n";
     278      }
     279    }
     280    else
     281    {
     282      foreach ($conf['picture_ext'] as $ext)
     283      {
     284        $candidate = $file_wo_ext.'.'.$ext;
     285        if (!in_array($candidate, $fs_representatives))
     286        {
     287          continue;
     288        }
     289        else
     290        {
     291          $element['representative_ext'] = $ext;
     292          break;
     293        }
     294      }
    173295     
    174       if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $fs_picture ) )
    175       {
    176         echo '<span style="color:red;">"'.$fs_picture.'" : ';
    177         echo 'The name of the picture should be composed of ';
    178         echo 'letters, figures, "-", "_" or "." ONLY';
    179         echo '</span><br />';
    180       }
     296      array_push($elements, $element);
     297    }
     298  }
     299
     300  $xml = "\n".$indent.'<root>';
     301  $attributes = array('file','tn_ext','representative_ext','filesize',
     302                      'width','height');
     303  foreach ($elements as $element)
     304  {
     305    $xml.= "\n".$indent.'  ';
     306    $xml.= '<element';
     307    foreach ($attributes as $attribute)
     308    {
     309      if (isset($element{$attribute}))
     310      {
     311        $xml.= ' '.$attribute.'="'.$element{$attribute}.'"';
     312      }
     313    }
     314    $xml.= ' />';
     315  }
     316  $xml.= "\n".$indent.'</root>';
     317
     318  return $xml;
     319}
     320
     321// +-----------------------------------------------------------------------+
     322// |                                script                                 |
     323// +-----------------------------------------------------------------------+
     324if (isset($_GET['action']))
     325{
     326  $page['action'] = $_GET['action'];
     327}
     328else
     329{
     330  $page['action'] = 'generate';
     331}
     332echo '<pre>';
     333switch ($page['action'])
     334{
     335  case 'generate' :
     336  {
     337    $start = get_moment();
     338   
     339    $listing = '<informations';
     340    $listing.= ' generation_date="'.date('Y-m-d').'"';
     341    $listing.= ' phpwg_version="'.$conf{'version'}.'"';
     342   
     343    $end = strrpos($_SERVER['PHP_SELF'], '/') + 1;
     344    $local_folder = substr($_SERVER['PHP_SELF'], 0, $end);
     345    $url = 'http://'.$_SERVER['HTTP_HOST'].$local_folder;
     346   
     347    $listing.= ' url="'.$url.'"';
     348    $listing.= '/>'."\n";
     349   
     350    $listing.= get_dirs('.', '', 0);
     351   
     352    if ($fp = @fopen("./listing.xml","w"))
     353    {
     354      fwrite($fp, $listing);
     355      fclose($fp);
     356      echo 'PWG-INFO-1: listing.xml created in ';
     357      echo get_elapsed_time($start, get_moment());
     358      echo "\n";
    181359    }
    182360    else
    183361    {
    184       echo 'The thumbnail is missing for '.$dir.'/'.$fs_picture;
    185       echo '-> '.$dir.'/thumbnail/';
    186       echo $conf['prefix_thumbnail'].$file_wo_ext.'.xxx';
    187       echo ' ("xxx" can be : ';
    188       echo implode( ', ', $conf['picture_ext'] );
    189       echo ')<br />';
    190     }
    191   }
    192 
    193   $root.= "\n".$indent.'</root>';
    194 
    195   return $root;
    196 }
    197 
    198 $listing.= get_dirs( '.', '', 0 );
    199 
    200 if ( $fp = @fopen("./listing.xml","w") )
    201 {
    202   fwrite( $fp, $listing );
    203   fclose( $fp );
    204   echo "listing.xml created";
    205 }
    206 else
    207 {
    208   echo "I can't write the file listing.xml";
    209 }
     362      echo "PWG-ERROR-2: I can't write the file listing.xml"."\n";
     363    }
     364    break;
     365  }
     366  case 'test' :
     367  {
     368    if (isset($_GET['version']))
     369    {
     370      if ($_GET['version'] != $conf['version'])
     371      {
     372        echo 'PWG-ERROR-4: PhpWebGallery versions differs'."\n";
     373      }
     374      else
     375      {
     376        echo 'PWG-INFO-2: test successful'."\n";
     377      }
     378    }
     379    else
     380    {
     381      echo 'PWG-INFO-2: test successful'."\n";
     382    }
     383    break;
     384  }
     385  case 'clean' :
     386  {
     387    if( @unlink('./listing.xml'))
     388    {
     389      echo 'PWG-INFO-3 : listing.xml file deleted'."\n";
     390    }
     391    else
     392    {
     393      echo 'PWG-ERROR-3 : listing.xml does not exist'."\n";
     394    }
     395    break;
     396  }
     397}
     398echo '</pre>';
    210399?>
Note: See TracChangeset for help on using the changeset viewer.