doSearch() protected method

Recursive files search
Author: Dmitry (dio) Levashov
protected doSearch ( string $path, string $q, array $mimes ) : array
$path string dir path
$q string search string
$mimes array
return array
 /**
  * Recursive files search
  *
  * @param  string  $path   dir path
  * @param  string  $q      search string
  * @param  array   $mimes
  * @return array
  * @author Dmitry (dio) Levashov
  * @author Naoki Sawada
  **/
 protected function doSearch($path, $q, $mimes)
 {
     if ($this->encoding) {
         // non UTF-8 has problem of glob() results
         return parent::doSearch($path, $q, $mimes);
     }
     static $escaper;
     if (!$escaper) {
         $escaper = array('[' => '\\[', ']' => '\\]', '*' => '\\*', '?' => '\\?');
     }
     $result = array();
     $path = strtr($path, $escaper);
     $_q = strtr($q, $escaper);
     $dirs = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
     $match = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*' . $_q . '*', GLOB_NOSORT);
     if ($match) {
         foreach ($match as $p) {
             $stat = $this->stat($p);
             if (!$stat) {
                 // invalid links
                 continue;
             }
             if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'], $mimes)) {
                 continue;
             }
             $name = $stat['name'];
             if (!$mimes || $stat['mime'] !== 'directory') {
                 $stat['path'] = $this->path($stat['hash']);
                 if ($this->URL && !isset($stat['url'])) {
                     $path = str_replace(DIRECTORY_SEPARATOR, '/', substr($p, strlen($this->root) + 1));
                     $stat['url'] = $this->URL . $path;
                 }
                 $result[] = $stat;
             }
         }
     }
     if ($dirs) {
         foreach ($dirs as $dir) {
             $stat = $this->stat($dir);
             if ($stat['read'] && !isset($stat['alias'])) {
                 @set_time_limit(30);
                 $result = array_merge($result, $this->doSearch($dir, $q, $mimes));
             }
         }
     }
     return $result;
 }
 /**
  * Recursive files search
  *
  * @param  string  $path   dir path
  * @param  string  $q      search string
  * @param  array   $mimes
  * @return array
  * @author Dmitry (dio) Levashov
  * @author Naoki Sawada
  **/
 protected function doSearch($path, $q, $mimes)
 {
     if ($this->encoding || !class_exists('FilesystemIterator', false)) {
         // non UTF-8 use elFinderVolumeDriver::doSearch()
         return parent::doSearch($path, $q, $mimes);
     }
     $match = array();
     try {
         $iterator = new RecursiveIteratorIterator(new RecursiveCallbackFilterIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::SKIP_DOTS | (defined('RecursiveDirectoryIterator::FOLLOW_SYMLINKS') && $this->options['followSymLinks'] ? RecursiveDirectoryIterator::FOLLOW_SYMLINKS : 0)), array($this, 'localFileSystemSearchIteratorFilter')), RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD);
         foreach ($iterator as $key => $node) {
             if ($node->isDir()) {
                 if ($this->stripos($node->getFilename(), $q) !== false) {
                     $match[] = $key;
                 }
             } else {
                 $match[] = $key;
             }
         }
     } catch (Exception $e) {
     }
     $result = array();
     if ($match) {
         foreach ($match as $p) {
             $stat = $this->stat($p);
             if (!$stat) {
                 // invalid links
                 continue;
             }
             if (!empty($stat['hidden']) || !$this->mimeAccepted($stat['mime'], $mimes)) {
                 continue;
             }
             $name = $stat['name'];
             if (!$mimes || $stat['mime'] !== 'directory') {
                 $stat['path'] = $this->path($stat['hash']);
                 if ($this->URL && !isset($stat['url'])) {
                     $path = str_replace(DIRECTORY_SEPARATOR, '/', substr($p, strlen($this->root) + 1));
                     $stat['url'] = $this->URL . $path;
                 }
                 $result[] = $stat;
             }
         }
     }
     return $result;
 }
Esempio n. 3
0
 /**
  * Recursive files search
  *
  * @param  string  $path   dir path
  * @param  string  $q      search string
  * @param  array   $mimes
  * @return array
  * @author Naoki Sawada
  **/
 protected function doSearch($path, $q, $mimes)
 {
     if ($path != $this->root) {
         return parent::doSearch($path, $q, $mimes);
     } else {
         $result = array();
         $q = '%' . mysql_real_escape_string($q) . '%';
         $sql = 'SELECT `file_id`, `mime`, `uid`, `gid`, `gids`, `perm`, `home_of` FROM ' . $this->tbf . ' WHERE `name` LIKE \'' . $q . '\'';
         $res = $this->query($sql);
         if ($res) {
             while ($stat = $this->db->fetchArray($res)) {
                 if ($stat['mime'] === 'directory' || !$this->mimeAccepted($stat['mime'], $mimes)) {
                     continue;
                 }
                 $this->setAuthByPerm($stat);
                 if (empty($stat['hidden'])) {
                     $result[] = $this->stat($stat['file_id']);
                 }
             }
         }
         return $result;
     }
 }