* @version 1.0.3 (July 06, 2005) * @package AutoIndex */ class Search extends DirectoryListDetailed { /** * @var array List of matched filenames */ private $matches; /** * @return string The HTML text that makes up the search box */ public static function search_box() { global $words, $subdir; $search = (isset($_GET['search']) ? Url::html_output($_GET['search']) : ''); $mode = (isset($_GET['search_mode']) ? self::clean_mode($_GET['search_mode']) : 'f'); $modes = array('files' => 'f', 'folders' => 'd', 'both' => 'fd'); $out = '
' . '

' . '

'; return $out; } /** * @param string $filename * @param string $string * @return bool True if string matches filename */ private static function match(&$filename, &$string) { if (preg_match_all('/(?<=")[^"]+(?=")|[^ "]+/', $string, $matches)) { foreach ($matches[0] as $w) { if (stripos($filename, $w) !== false) { return true; } } } return false; } /** * Merges $obj into $this. * * @param Search $obj */ private function merge(Search $obj) { $this -> total_folders += $obj -> __get('total_folders'); $this -> total_files += $obj -> __get('total_files'); $this -> total_downloads += $obj -> __get('total_downloads'); $this -> total_size -> add_size($obj -> __get('total_size')); $this -> matches = array_merge($this -> matches, $obj -> __get('contents')); } /** * Returns a string with all characters except 'd' and 'f' stripped. * Either 'd' 'f' 'df' will be returned, defaults to 'f' * * @param string $mode * @return string */ private static function clean_mode($mode) { $str = ''; if (stripos($mode, 'f') !== false) { $str .= 'f'; } if (stripos($mode, 'd') !== false) { $str .= 'd'; } else if ($str == '') { $str = 'f'; } return $str; } /** * @param string $query String to search for * @param string $dir The folder to search (recursive) * @param string $mode Should be f (files), d (directories), or fd (both) */ public function __construct($query, $dir, $mode) { if (strlen($query) < 2 || strlen($query) > 20) { throw new ExceptionDisplay('Search query is either too long or too short.'); } $mode = self::clean_mode($mode); $dir = Item::make_sure_slash($dir); DirectoryList::__construct($dir); $this -> matches = array(); $this -> total_size = new Size(0); $this -> total_downloads = $this -> total_folders = $this -> total_files = 0; foreach ($this as $item) { if ($item == '..') { continue; } if (@is_dir($dir . $item)) { if (stripos($mode, 'd') !== false && self::match($item, $query)) { $temp = new DirItem($dir, $item); $this -> matches[] = $temp; if ($temp -> __get('size') -> __get('bytes') !== false) { $this -> total_size -> add_size($temp -> __get('size')); } $this -> total_folders++; } $sub_search = new Search($query, $dir . $item, $mode); $this -> merge($sub_search); } else if (stripos($mode, 'f') !== false && self::match($item, $query)) { $temp = new FileItem($dir, $item); $this -> matches[] = $temp; $this -> total_size -> add_size($temp -> __get('size')); $this -> total_downloads += $temp -> __get('downloads'); $this -> total_files++; } } global $words, $config, $subdir; $link = ' ' . Url::html_output($dir) . ' '; $this -> path_nav = $words -> __get('search results for') . $link . $words -> __get('and its subdirectories'); $this -> contents = $this -> matches; unset($this -> matches); } } ?>