source: trunk/admin/create_listing_file.php @ 362

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

header global refactoring

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                        create_listing_file.php                        |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-02-11 23:20:38 +0000 (Wed, 11 Feb 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 362 $
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$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']
40 *
41 * @param string $dir
42 * @return array
43 */
44function get_picture_files( $dir )
45{
46  global $conf;
47
48  $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 );
56      }
57    }
58  }
59  return $pictures;
60}
61
62/**
63 * returns an array with all thumbnails according to $conf['picture_ext']
64 * and $conf['prefix_thumbnail']
65 *
66 * @param string $dir
67 * @return array
68 */
69function get_thumb_files( $dir )
70{
71  global $conf;
72
73  $prefix_length = strlen( $conf['prefix_thumbnail'] );
74 
75  $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 );
84      }
85    }
86  }
87  return $thumbnails;
88}
89
90// get_dirs retourne un tableau contenant tous les sous-répertoires d'un
91// répertoire
92function get_dirs( $basedir, $indent, $level )
93{
94  $fs_dirs = array();
95  $dirs = "";
96
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.'" : ';
110          echo 'The name of the directory should be composed of ';
111          echo 'letters, figures, "-", "_" or "." ONLY';
112          echo '</span><br />';
113        }
114      }
115    }
116  }
117  // write of the dirs
118  foreach ( $fs_dirs as $fs_dir ) {
119    $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 );
122    $dirs.= "\n".$indent.'</dir'.$level.'>';
123  }
124  return $dirs;         
125}
126
127// get_extension returns the part of the string after the last "."
128function get_extension( $filename )
129{
130  return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) );
131}
132
133// get_filename_wo_extension returns the part of the string before the last
134// ".".
135// get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar'
136function get_filename_wo_extension( $filename )
137{
138  return substr( $filename, 0, strrpos( $filename, '.' ) );
139}
140
141function get_pictures( $dir, $indent )
142{
143  global $conf;
144 
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 ) {
156      $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.= ' />';
173     
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      }
181    }
182    else
183    {
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
200if ( $fp = @fopen("./listing.xml","w") )
201{
202  fwrite( $fp, $listing );
203  fclose( $fp );
204  echo "listing.xml created";
205}
206else
207{
208  echo "I can't write the file listing.xml";
209}
210?>
Note: See TracBrowser for help on using the repository browser.