/**
 * recurse direcory
 *
 * This function recurses into a given base directory
 * and calls the supplied function for each file and directory
 *
 * @param   array ref $data The results of the search are stored here
 * @param   string    $base Where to start the search
 * @param   callback  $func Callback (function name or arayy with object,method)
 * @param   string    $dir  Current directory beyond $base
 * @param   int       $lvl  Recursion Level
 * @author  Andreas Gohr <*****@*****.**>
 */
function search(&$data, $base, $func, $opts, $dir = '', $lvl = 1)
{
    $dirs = array();
    $files = array();
    //read in directories and files
    $dh = @opendir($base . '/' . $dir);
    if (!$dh) {
        return;
    }
    while (($file = readdir($dh)) !== false) {
        if (preg_match('/^[\\._]/', $file)) {
            continue;
        }
        //skip hidden files and upper dirs
        if (is_dir($base . '/' . $dir . '/' . $file)) {
            $dirs[] = $dir . '/' . $file;
            continue;
        }
        $files[] = $dir . '/' . $file;
    }
    closedir($dh);
    sort($files);
    sort($dirs);
    //give directories to userfunction then recurse
    foreach ($dirs as $dir) {
        if (search_callback($func, $data, $base, $dir, 'd', $lvl, $opts)) {
            search($data, $base, $func, $opts, $dir, $lvl + 1);
        }
    }
    //now handle the files
    foreach ($files as $file) {
        search_callback($func, $data, $base, $file, 'f', $lvl, $opts);
    }
}
Exemple #2
0
 /**
  * recurse direcory
  *
  * This function recurses into a given base directory
  * and calls the supplied function for each file and directory
  *
  * @param   array ref $data The results of the search are stored here
  * @param   string    $base Where to start the search
  * @param   callback  $func Callback (function name or arayy with object,method)
  * @param   string    $dir  Current directory beyond $base
  * @param   int       $lvl  Recursion Level
  * @author  Andreas Gohr <*****@*****.**>
  * modified by Samuele Tognini <*****@*****.**>
  */
 function _search(&$data, $base, $func, $opts, $dir = '', $lvl = 1)
 {
     $dirs = array();
     $files = array();
     $files_tmp = array();
     $dirs_tmp = array();
     //read in directories and files
     $dh = @opendir($base . '/' . $dir);
     if (!$dh) {
         return;
     }
     while (($file = readdir($dh)) !== false) {
         //skip hidden files and upper dirs
         if (preg_match('/^[\\._]/', $file)) {
             continue;
         }
         if (is_dir($base . '/' . $dir . '/' . $file)) {
             $dirs[] = $dir . '/' . $file;
             continue;
         }
         $files[] = $dir . '/' . $file;
     }
     closedir($dh);
     //Sort dirs
     if ($this->nsort) {
         foreach ($dirs as $dir) {
             search_callback($func, $dirs_tmp, $base, $dir, 'd', $lvl, $opts);
         }
         usort($dirs_tmp, array($this, "_cmp"));
         foreach ($dirs_tmp as $dir) {
             $data[] = $dir;
             if ($dir['return']) {
                 $this->_search($data, $base, $func, $opts, $dir['file'], $lvl + 1);
             }
         }
     } else {
         sort($dirs);
         foreach ($dirs as $dir) {
             if (search_callback($func, $data, $base, $dir, 'd', $lvl, $opts)) {
                 $this->_search($data, $base, $func, $opts, $dir, $lvl + 1);
             }
         }
     }
     //Sort files
     foreach ($files as $file) {
         search_callback($func, $files_tmp, $base, $file, 'f', $lvl, $opts);
     }
     usort($files_tmp, array($this, "_cmp"));
     if (empty($dirs) && empty($files_tmp)) {
         $v = end($data);
         if (!$v['hns']) {
             array_pop($data);
         }
     } else {
         $data = array_merge($data, $files_tmp);
     }
     return true;
 }