@@ -87,15 +87,17 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
8787 if (($ directory_depth < 1 || $ new_depth > 0 ) && is_dir ($ source_dir . $ file ))
8888 {
8989 $ filedata [$ file ] = directory_map ($ source_dir . $ file , $ new_depth , $ hidden );
90- } else
90+ }
91+ else
9192 {
9293 $ filedata [] = $ file ;
9394 }
9495 }
9596
9697 closedir ($ fp );
9798 return $ filedata ;
98- } catch (Exception $ fe )
99+ }
100+ catch (\Exception $ fe )
99101 {
100102 return [];
101103 }
@@ -122,25 +124,29 @@ function directory_map(string $source_dir, int $directory_depth = 0, bool $hidde
122124 */
123125 function write_file (string $ path , string $ data , string $ mode = 'wb ' ): bool
124126 {
125- if ( ! $ fp = @ fopen ( $ path , $ mode ))
127+ try
126128 {
127- return false ;
128- }
129+ $ fp = fopen ($ path , $ mode );
129130
130- flock ($ fp , LOCK_EX );
131+ flock ($ fp , LOCK_EX );
131132
132- for ($ result = $ written = 0 , $ length = strlen ($ data ); $ written < $ length ; $ written += $ result )
133- {
134- if (($ result = fwrite ($ fp , substr ($ data , $ written ))) === false )
133+ for ($ result = $ written = 0 , $ length = strlen ($ data ); $ written < $ length ; $ written += $ result )
135134 {
136- break ;
135+ if (($ result = fwrite ($ fp , substr ($ data , $ written ))) === false )
136+ {
137+ break ;
138+ }
137139 }
138- }
139140
140- flock ($ fp , LOCK_UN );
141- fclose ($ fp );
141+ flock ($ fp , LOCK_UN );
142+ fclose ($ fp );
142143
143- return is_int ($ result );
144+ return is_int ($ result );
145+ }
146+ catch (\Exception $ fe )
147+ {
148+ return false ;
149+ }
144150 }
145151
146152}
@@ -170,28 +176,33 @@ function delete_files(string $path, bool $delDir = false, bool $htdocs = false,
170176 // Trim the trailing slash
171177 $ path = rtrim ($ path , '/ \\' );
172178
173- if ( ! $ current_dir = @ opendir ( $ path ))
179+ try
174180 {
175- return false ;
176- }
181+ $ current_dir = opendir ($ path );
177182
178- while (false !== ($ filename = @readdir ($ current_dir )))
179- {
180- if ($ filename !== '. ' && $ filename !== '.. ' )
183+ while (false !== ($ filename = @readdir ($ current_dir )))
181184 {
182- if (is_dir ($ path . DIRECTORY_SEPARATOR . $ filename ) && $ filename [0 ] !== '. ' )
183- {
184- delete_files ($ path . DIRECTORY_SEPARATOR . $ filename , $ delDir , $ htdocs , $ _level + 1 );
185- } elseif ($ htdocs !== true || ! preg_match ('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i ' , $ filename ))
185+ if ($ filename !== '. ' && $ filename !== '.. ' )
186186 {
187- @unlink ($ path . DIRECTORY_SEPARATOR . $ filename );
187+ if (is_dir ($ path . DIRECTORY_SEPARATOR . $ filename ) && $ filename [0 ] !== '. ' )
188+ {
189+ delete_files ($ path . DIRECTORY_SEPARATOR . $ filename , $ delDir , $ htdocs , $ _level + 1 );
190+ }
191+ elseif ($ htdocs !== true || ! preg_match ('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i ' , $ filename ))
192+ {
193+ @unlink ($ path . DIRECTORY_SEPARATOR . $ filename );
194+ }
188195 }
189196 }
190- }
191197
192- closedir ($ current_dir );
198+ closedir ($ current_dir );
193199
194- return ($ delDir === true && $ _level > 0 ) ? @rmdir ($ path ) : true ;
200+ return ($ delDir === true && $ _level > 0 ) ? @rmdir ($ path ) : true ;
201+ }
202+ catch (\Exception $ fe )
203+ {
204+ return false ;
205+ }
195206 }
196207
197208}
@@ -217,8 +228,9 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
217228 {
218229 static $ filedata = [];
219230
220- if ( $ fp = @ opendir ( $ source_dir ))
231+ try
221232 {
233+ $ fp = opendir ($ source_dir );
222234 // reset the array and make sure $source_dir has a trailing slash on the initial call
223235 if ($ recursion === false )
224236 {
@@ -231,7 +243,8 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
231243 if (is_dir ($ source_dir . $ file ) && $ file [0 ] !== '. ' )
232244 {
233245 get_filenames ($ source_dir . $ file . DIRECTORY_SEPARATOR , $ include_path , true );
234- } elseif ($ file [0 ] !== '. ' )
246+ }
247+ elseif ($ file [0 ] !== '. ' )
235248 {
236249 $ filedata [] = ($ include_path === true ) ? $ source_dir . $ file : $ file ;
237250 }
@@ -240,8 +253,10 @@ function get_filenames(string $source_dir, bool $include_path = false, bool $rec
240253 closedir ($ fp );
241254 return $ filedata ;
242255 }
243-
244- return [];
256+ catch (\Exception $ fe )
257+ {
258+ return [];
259+ }
245260 }
246261
247262}
@@ -270,33 +285,38 @@ function get_dir_file_info(string $source_dir, bool $top_level_only = true, bool
270285 static $ filedata = [];
271286 $ relative_path = $ source_dir ;
272287
273- if ( $ fp = @ opendir ( $ source_dir ))
288+ try
274289 {
275- // reset the array and make sure $source_dir has a trailing slash on the initial call
276- if ($ recursion === false )
277- {
278- $ filedata = [];
279- $ source_dir = rtrim (realpath ($ source_dir ), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
280- }
281-
282- // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
283- while (false !== ($ file = readdir ($ fp )))
284- {
285- if (is_dir ($ source_dir . $ file ) && $ file [0 ] !== '. ' && $ top_level_only === false )
290+ $ fp = @opendir ($ source_dir ); {
291+ // reset the array and make sure $source_dir has a trailing slash on the initial call
292+ if ($ recursion === false )
286293 {
287- get_dir_file_info ($ source_dir . $ file . DIRECTORY_SEPARATOR , $ top_level_only , true );
288- } elseif ($ file [0 ] !== '. ' )
294+ $ filedata = [];
295+ $ source_dir = rtrim (realpath ($ source_dir ), DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
296+ }
297+
298+ // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
299+ while (false !== ($ file = readdir ($ fp )))
289300 {
290- $ filedata [$ file ] = get_file_info ($ source_dir . $ file );
291- $ filedata [$ file ]['relative_path ' ] = $ relative_path ;
301+ if (is_dir ($ source_dir . $ file ) && $ file [0 ] !== '. ' && $ top_level_only === false )
302+ {
303+ get_dir_file_info ($ source_dir . $ file . DIRECTORY_SEPARATOR , $ top_level_only , true );
304+ }
305+ elseif ($ file [0 ] !== '. ' )
306+ {
307+ $ filedata [$ file ] = get_file_info ($ source_dir . $ file );
308+ $ filedata [$ file ]['relative_path ' ] = $ relative_path ;
309+ }
292310 }
293- }
294311
295- closedir ($ fp );
296- return $ filedata ;
312+ closedir ($ fp );
313+ return $ filedata ;
314+ }
315+ }
316+ catch (\Exception $ fe )
317+ {
318+ return [];
297319 }
298-
299- return [];
300320 }
301321
302322}
@@ -385,25 +405,32 @@ function symbolic_permissions(int $perms): string
385405 if (($ perms & 0xC000 ) === 0xC000 )
386406 {
387407 $ symbolic = 's ' ; // Socket
388- } elseif (($ perms & 0xA000 ) === 0xA000 )
408+ }
409+ elseif (($ perms & 0xA000 ) === 0xA000 )
389410 {
390411 $ symbolic = 'l ' ; // Symbolic Link
391- } elseif (($ perms & 0x8000 ) === 0x8000 )
412+ }
413+ elseif (($ perms & 0x8000 ) === 0x8000 )
392414 {
393415 $ symbolic = '- ' ; // Regular
394- } elseif (($ perms & 0x6000 ) === 0x6000 )
416+ }
417+ elseif (($ perms & 0x6000 ) === 0x6000 )
395418 {
396419 $ symbolic = 'b ' ; // Block special
397- } elseif (($ perms & 0x4000 ) === 0x4000 )
420+ }
421+ elseif (($ perms & 0x4000 ) === 0x4000 )
398422 {
399423 $ symbolic = 'd ' ; // Directory
400- } elseif (($ perms & 0x2000 ) === 0x2000 )
424+ }
425+ elseif (($ perms & 0x2000 ) === 0x2000 )
401426 {
402427 $ symbolic = 'c ' ; // Character special
403- } elseif (($ perms & 0x1000 ) === 0x1000 )
428+ }
429+ elseif (($ perms & 0x1000 ) === 0x1000 )
404430 {
405431 $ symbolic = 'p ' ; // FIFO pipe
406- } else
432+ }
433+ else
407434 {
408435 $ symbolic = 'u ' ; // Unknown
409436 }
@@ -474,7 +501,8 @@ function set_realpath(string $path, bool $checkExistance = false): string
474501 if (realpath ($ path ) !== false )
475502 {
476503 $ path = realpath ($ path );
477- } elseif ($ checkExistance && ! is_dir ($ path ) && ! is_file ($ path ))
504+ }
505+ elseif ($ checkExistance && ! is_dir ($ path ) && ! is_file ($ path ))
478506 {
479507 throw new InvalidArgumentException ('Not a valid path: ' . $ path );
480508 }
0 commit comments