Esempio n. 1
0
 private function folder_iterator($recurring, $dir_name, $include_dot_files, $search_pattern = '', $search_prevent_file = '', $search_prevent_override = '')
 {
     //--
     $recurring = (bool) $recurring;
     $dir_name = (string) $dir_name;
     $include_dot_files = (bool) $include_dot_files;
     $search_pattern = (string) $search_pattern;
     $search_prevent_file = (string) $search_prevent_file;
     $search_prevent_override = (string) $search_prevent_override;
     //--
     if ((string) $dir_name == '') {
         Smart::log_warning('LibFileSys // ReadsFolderRecurring // Dir Name is Empty !');
         return;
         // this function does not return anything, but just stop here in this case
     }
     //end if
     //-- fix invalid path (must end with /)
     $dir_name = SmartFileSysUtils::add_dir_last_slash($dir_name);
     //-- protection
     SmartFileSysUtils::raise_error_if_unsafe_path($dir_name);
     //--
     @clearstatcache();
     //--
     $this->pattern_search_str = $search_pattern;
     $this->search_prevent_file = $search_prevent_file;
     $this->search_prevent_override = $search_prevent_override;
     //--
     if (SmartFileSystem::file_or_link_exists($dir_name) and !is_file($dir_name)) {
         // can be dir or link
         //list
         //--
         if ($handle = opendir($dir_name)) {
             //---------------------------------------
             while (false !== ($file = readdir($handle))) {
                 //--
                 if ((string) $file != '.' and (string) $file != '..') {
                     //--
                     if ($include_dot_files or !$include_dot_files and substr($file, 0, 1) != '.') {
                         //--
                         SmartFileSysUtils::raise_error_if_unsafe_path($dir_name . $file);
                         //-- params to see if counted or added to pattern matches
                         $tmp_allow_addition = 1;
                         $tmp_add_pattern = 0;
                         //-- this is for #private folders, will prevent searching in folders containing for example this file: .private-folder but can be overriden by the $search_prevent_override option exluding a particular path like folder/private/user1
                         if (strlen($search_prevent_file) > 0 and is_file($dir_name . $search_prevent_file)) {
                             if (strlen($search_prevent_override) <= 0 or strlen($search_prevent_override) > 0 and !is_file($dir_name . $search_prevent_override)) {
                                 $tmp_allow_addition = 0;
                             }
                             //end if
                         }
                         //end if
                         //-- this is a search pattern (search pattern does not apply to folders !!) ; if no empty will populate the pattern matches array with all files and folders matching ; to include all, use * or a particular search for the rest like myfile1
                         if ((string) $search_pattern == '' or is_dir($dir_name . $file)) {
                             if ($tmp_allow_addition) {
                                 if ($this->list_files_and_dirs) {
                                     $tmp_add_pattern = 1;
                                 }
                                 //end if
                             }
                             //end if
                         } else {
                             if ($this->limit_search_files <= 0 or Smart::array_size($this->pattern_file_matches) < $this->limit_search_files) {
                                 if ((string) $search_pattern == '*' or (string) $search_pattern == '[image]' and (substr($file, -4, 4) == '.png' or substr($file, -4, 4) == '.gif' or substr($file, -4, 4) == '.jpg' or substr($file, -5, 5) == '.jpeg') or (string) $search_pattern != '*' and (string) $search_pattern != '[image]' and stripos($file, $search_pattern) !== false) {
                                     if ($tmp_allow_addition) {
                                         if ($this->list_files_and_dirs) {
                                             $tmp_add_pattern = 1;
                                         }
                                         //end if
                                     }
                                     //end if
                                 } else {
                                     $tmp_allow_addition = 0;
                                 }
                                 //end if else
                             }
                             //end if
                         }
                         //end if
                         //--
                         if ($this->limit_search_files > 0) {
                             // the dir should not be taken in count here
                             if ($this->num_files + $this->num_links >= $this->limit_search_files) {
                                 break;
                             }
                             //end if
                         }
                         //end if
                         //--
                         if (!is_link($dir_name . $file)) {
                             //--
                             if (is_dir($dir_name . $file)) {
                                 //-- dir
                                 if ($tmp_allow_addition) {
                                     //--
                                     $tmp_fsize = Smart::format_number_int(@filesize($dir_name . $file), '+');
                                     //--
                                     $this->num_dirs++;
                                     $this->num_size += $tmp_fsize;
                                     $this->num_dirs_size += $tmp_fsize;
                                     //--
                                     $tmp_fsize = 0;
                                     //--
                                     if ($tmp_add_pattern) {
                                         if ($recurring) {
                                             // if recurring, add the full path
                                             $this->pattern_dir_matches[$dir_name . $file] = @filemtime($dir_name . $file);
                                         } else {
                                             // if not recurring, add just base path, without dirname prefix
                                             $this->pattern_dir_matches[$file] = @filemtime($dir_name . $file);
                                         }
                                         //end if else
                                     }
                                     //end if
                                     //--
                                 }
                                 //end if
                                 //--
                                 if ($recurring) {
                                     //-- we go search inside even if this folder name may not match the search pattern, it is a folder, except if dissalow addition from above
                                     $this->folder_iterator($recurring, SmartFileSysUtils::add_dir_last_slash($dir_name . $file), $include_dot_files, $search_pattern, $search_prevent_file, $search_prevent_override);
                                     //--
                                 }
                                 //end if
                                 //--
                             } else {
                                 //-- file
                                 if ($tmp_allow_addition) {
                                     //--
                                     $tmp_fsize = Smart::format_number_int(@filesize($dir_name . $file), '+');
                                     //--
                                     $this->num_files++;
                                     $this->num_size += $tmp_fsize;
                                     $this->num_files_size += $tmp_fsize;
                                     //--
                                     $tmp_fsize = 0;
                                     //--
                                     if ($tmp_add_pattern) {
                                         if ($recurring) {
                                             // if recurring, add the full path
                                             $this->pattern_file_matches[$dir_name . $file] = @filemtime($dir_name . $file);
                                         } else {
                                             // if not recurring, add just base path, without dirname prefix
                                             $this->pattern_file_matches[$file] = @filemtime($dir_name . $file);
                                         }
                                         //end if else
                                     }
                                     //end if
                                     //--
                                 }
                                 //end if
                                 //--
                             }
                             //end else
                             //--
                         } else {
                             //-- link
                             if ($tmp_allow_addition) {
                                 //--
                                 $link_result = SmartFileSystem::link_get_origin($dir_name . $file);
                                 //--
                                 if (empty($link_result) or (string) $link_result == '' or !SmartFileSystem::file_or_link_exists($link_result)) {
                                     //--
                                     // case of readlink error ..., not includding broken links, they are useless
                                     //--
                                 } else {
                                     //--
                                     $tmp_size_arr = array();
                                     $tmp_fsize = 0;
                                     //$tmp_size_arr = (array) @lstat($dir_name.$file);
                                     //$tmp_fsize = Smart::format_number_int($tmp_size_arr[7],'+'); // $tmp_size_arr[7] -> size, but may break compare if on a different file system or in distributed storage on various OS
                                     //--
                                     $this->num_links++;
                                     //--
                                     if (file_exists($dir_name . $file)) {
                                         // here file_exists must be tested because if broken link not stat on it (filemtime) to avoid log un-necessary errors
                                         //-- bugfix: not if broken link
                                         $this->num_size += $tmp_fsize;
                                         if ($tmp_add_pattern) {
                                             if (is_dir($dir_name . $file)) {
                                                 $this->num_dirs++;
                                                 $this->num_dirs_size += $tmp_fsize;
                                                 if ($recurring) {
                                                     // if recurring, add the full path
                                                     $this->pattern_dir_matches[$dir_name . $file] = @filemtime($dir_name . $file);
                                                 } else {
                                                     // if not recurring, add just base path, without dirname prefix
                                                     $this->pattern_dir_matches[$file] = @filemtime($dir_name . $file);
                                                 }
                                                 //end if else
                                             } else {
                                                 $this->num_files++;
                                                 $this->num_files_size += $tmp_fsize;
                                                 if ($recurring) {
                                                     // if recurring, add the full path
                                                     $this->pattern_file_matches[$dir_name . $file] = @filemtime($dir_name . $file);
                                                 } else {
                                                     // if not recurring, add just base path, without dirname prefix
                                                     $this->pattern_file_matches[$file] = @filemtime($dir_name . $file);
                                                 }
                                                 //end if else
                                             }
                                             //end if else
                                         }
                                         //end if
                                         //--
                                     }
                                     //end if
                                     //--
                                     $tmp_fsize = 0;
                                     $tmp_size_arr = array();
                                     //--
                                 }
                                 //end if else
                                 //--
                             }
                             //end if
                             //--
                         }
                         //end if else
                         //--
                     }
                     //end if
                     //--
                 }
                 //end if(. ..)
                 //--
             }
             //end while
             //---------------------------------------
             @closedir($handle);
             //---------------------------------------
         } else {
             //---------------------------------------
             $this->errors_arr[] = $dir_name;
             //---------------------------------------
         }
         //end else
         //--
     } else {
         //---------------------------------------
         // nothing ...
         //---------------------------------------
     }
     //end if else
     //--
 }