1 | <?php |
---|
2 | /*************************************************************************** |
---|
3 | * create_listing_file.php * |
---|
4 | * ------------------- * |
---|
5 | * application : PhpWebGallery 1.3 <http://phpwebgallery.net> * |
---|
6 | * author : Pierrick LE GALL <pierrick@z0rglub.com> * |
---|
7 | * * |
---|
8 | * $Id: create_listing_file.php 284 2004-01-17 18:13:49Z z0rglub $ |
---|
9 | * * |
---|
10 | ***************************************************************************/ |
---|
11 | |
---|
12 | $conf['prefix_thumbnail'] = 'TN-'; |
---|
13 | $conf['picture_ext'] = array ( 'jpg', 'gif', 'png', 'JPG', 'GIF', 'PNG' ); |
---|
14 | |
---|
15 | $listing = ''; |
---|
16 | |
---|
17 | $end = strrpos( $_SERVER['PHP_SELF'], '/' ) + 1; |
---|
18 | $local_folder = substr( $_SERVER['PHP_SELF'], 0, $end ); |
---|
19 | $url = 'http://'.$_SERVER['HTTP_HOST'].$local_folder; |
---|
20 | |
---|
21 | $listing.= '<url>'.$url.'</url>'; |
---|
22 | |
---|
23 | /** |
---|
24 | * returns an array with all picture files according to $conf['picture_ext'] |
---|
25 | * |
---|
26 | * @param string $dir |
---|
27 | * @return array |
---|
28 | */ |
---|
29 | function get_picture_files( $dir ) |
---|
30 | { |
---|
31 | global $conf; |
---|
32 | |
---|
33 | $pictures = array(); |
---|
34 | if ( $opendir = opendir( $dir ) ) |
---|
35 | { |
---|
36 | while ( $file = readdir( $opendir ) ) |
---|
37 | { |
---|
38 | if ( in_array( get_extension( $file ), $conf['picture_ext'] ) ) |
---|
39 | { |
---|
40 | array_push( $pictures, $file ); |
---|
41 | } |
---|
42 | } |
---|
43 | } |
---|
44 | return $pictures; |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * returns an array with all thumbnails according to $conf['picture_ext'] |
---|
49 | * and $conf['prefix_thumbnail'] |
---|
50 | * |
---|
51 | * @param string $dir |
---|
52 | * @return array |
---|
53 | */ |
---|
54 | function get_thumb_files( $dir ) |
---|
55 | { |
---|
56 | global $conf; |
---|
57 | |
---|
58 | $prefix_length = strlen( $conf['prefix_thumbnail'] ); |
---|
59 | |
---|
60 | $thumbnails = array(); |
---|
61 | if ( $opendir = @opendir( $dir ) ) |
---|
62 | { |
---|
63 | while ( $file = readdir( $opendir ) ) |
---|
64 | { |
---|
65 | if ( in_array( get_extension( $file ), $conf['picture_ext'] ) |
---|
66 | and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'] ) |
---|
67 | { |
---|
68 | array_push( $thumbnails, $file ); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | return $thumbnails; |
---|
73 | } |
---|
74 | |
---|
75 | // get_dirs retourne un tableau contenant tous les sous-répertoires d'un |
---|
76 | // répertoire |
---|
77 | function get_dirs( $basedir, $indent, $level ) |
---|
78 | { |
---|
79 | $fs_dirs = array(); |
---|
80 | $dirs = ""; |
---|
81 | |
---|
82 | if ( $opendir = opendir( $basedir ) ) |
---|
83 | { |
---|
84 | while ( $file = readdir( $opendir ) ) |
---|
85 | { |
---|
86 | if ( $file != '.' |
---|
87 | and $file != '..' |
---|
88 | and is_dir ( $basedir.'/'.$file ) |
---|
89 | and $file != 'thumbnail' ) |
---|
90 | { |
---|
91 | array_push( $fs_dirs, $file ); |
---|
92 | if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) ) |
---|
93 | { |
---|
94 | echo '<span style="color:red;">"'.$file.'" : '; |
---|
95 | echo 'The name of the directory should be composed of '; |
---|
96 | echo 'letters, figures, "-", "_" or "." ONLY'; |
---|
97 | echo '</span><br />'; |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | // write of the dirs |
---|
103 | foreach ( $fs_dirs as $fs_dir ) { |
---|
104 | $dirs.= "\n".$indent.'<dir'.$level.' name="'.$fs_dir.'">'; |
---|
105 | $dirs.= get_pictures( $basedir.'/'.$fs_dir, $indent.' ' ); |
---|
106 | $dirs.= get_dirs( $basedir.'/'.$fs_dir, $indent.' ', $level + 1 ); |
---|
107 | $dirs.= "\n".$indent.'</dir'.$level.'>'; |
---|
108 | } |
---|
109 | return $dirs; |
---|
110 | } |
---|
111 | |
---|
112 | // get_extension returns the part of the string after the last "." |
---|
113 | function get_extension( $filename ) |
---|
114 | { |
---|
115 | return substr( strrchr( $filename, '.' ), 1, strlen ( $filename ) ); |
---|
116 | } |
---|
117 | |
---|
118 | // get_filename_wo_extension returns the part of the string before the last |
---|
119 | // ".". |
---|
120 | // get_filename_wo_extension( 'test.tar.gz' ) -> 'test.tar' |
---|
121 | function get_filename_wo_extension( $filename ) |
---|
122 | { |
---|
123 | return substr( $filename, 0, strrpos( $filename, '.' ) ); |
---|
124 | } |
---|
125 | |
---|
126 | function get_pictures( $dir, $indent ) |
---|
127 | { |
---|
128 | global $conf; |
---|
129 | |
---|
130 | // fs means filesystem : $fs_pictures contains pictures in the filesystem |
---|
131 | // found in $dir, $fs_thumbnails contains thumbnails... |
---|
132 | $fs_pictures = get_picture_files( $dir ); |
---|
133 | $fs_thumbnails = get_thumb_files( $dir.'/thumbnail' ); |
---|
134 | |
---|
135 | $root = "\n".$indent.'<root>'; |
---|
136 | |
---|
137 | foreach ( $fs_pictures as $fs_picture ) { |
---|
138 | $file_wo_ext = get_filename_wo_extension( $fs_picture ); |
---|
139 | $tn_ext = ''; |
---|
140 | foreach ( $conf['picture_ext'] as $ext ) { |
---|
141 | $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext; |
---|
142 | if ( !in_array( $test, $fs_thumbnails ) ) continue; |
---|
143 | else { $tn_ext = $ext; break; } |
---|
144 | } |
---|
145 | // if we found a thumnbnail corresponding to our picture... |
---|
146 | if ( $tn_ext != '' ) |
---|
147 | { |
---|
148 | list( $width,$height ) = @getimagesize( $dir.'/'.$fs_picture ); |
---|
149 | |
---|
150 | $root.= "\n".$indent.' '; |
---|
151 | $root.= '<picture'; |
---|
152 | $root.= ' file="'. $fs_picture.'"'; |
---|
153 | $root.= ' tn_ext="'. $tn_ext.'"'; |
---|
154 | $root.= ' filesize="'.floor(filesize($dir.'/'.$fs_picture)/1024).'"'; |
---|
155 | $root.= ' width="'. $width.'"'; |
---|
156 | $root.= ' height="'. $height.'"'; |
---|
157 | $root.= ' />'; |
---|
158 | |
---|
159 | if ( !preg_match( '/^[a-zA-Z0-9-_.]+$/', $fs_picture ) ) |
---|
160 | { |
---|
161 | echo '<span style="color:red;">"'.$fs_picture.'" : '; |
---|
162 | echo 'The name of the picture should be composed of '; |
---|
163 | echo 'letters, figures, "-", "_" or "." ONLY'; |
---|
164 | echo '</span><br />'; |
---|
165 | } |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | echo 'The thumbnail is missing for '.$dir.'/'.$fs_picture; |
---|
170 | echo '-> '.$dir.'/thumbnail/'; |
---|
171 | echo $conf['prefix_thumbnail'].$file_wo_ext.'.xxx'; |
---|
172 | echo ' ("xxx" can be : '; |
---|
173 | echo implode( ', ', $conf['picture_ext'] ); |
---|
174 | echo ')<br />'; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | $root.= "\n".$indent.'</root>'; |
---|
179 | |
---|
180 | return $root; |
---|
181 | } |
---|
182 | |
---|
183 | $listing.= get_dirs( '.', '', 0 ); |
---|
184 | |
---|
185 | if ( $fp = @fopen("./listing.xml","w") ) |
---|
186 | { |
---|
187 | fwrite( $fp, $listing ); |
---|
188 | fclose( $fp ); |
---|
189 | echo "listing.xml created"; |
---|
190 | } |
---|
191 | else |
---|
192 | { |
---|
193 | echo "I can't write the file listing.xml"; |
---|
194 | } |
---|
195 | ?> |
---|