*/ class feedSearch { private $out; private $caseSensitiveSearch = false; /** * Entry point for the application. * * @return void */ public function run($caseSensitiveSearch = false) { $this->caseSensitiveSearch = $caseSensitiveSearch; switch ($this->fetchRunCode()) { case "search": $this->out = $this->run_search(); break; default: $this->out = $this->run_main(); $this->wrapOut(); break; } $this->printOut(); } /** * Renders the page elements. * * @return string */ private function run_main() { $out = "
URL
Tag Name
Search keyword
"; return $out; } /** * Renders the search results. * * @return string */ private function run_search() { $out = ""; $search_url = $GLOBALS["_POST"]["search_url"]; $search_tagName = $GLOBALS["_POST"]["search_tagName"]; $search_keyword = $GLOBALS["_POST"]["search_keyword"]; if ($search_url == "" || $search_tagName == "" || $search_keyword == "") { return "All fields are mandatory for the search."; } if (!$this->checkSearchURL($search_url)) { return "You can only access files on the www."; } $fileData = $this->fetchFileContent($search_url); if ($fileData == "") { return "The requested page does not exist or is unavailable."; } $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); $xmlElements = Array(); xml_parse_into_struct($parser, $fileData, $xmlElements); foreach ($xmlElements as $key => $value) { if ( $value["type"] = "complete" // only elements with values // tag match && ($this->caseSensitiveSearch ? strcmp($value["tag"], $search_tagName) : strcasecmp($value["tag"], $search_tagName)) == 0 // keyword match && ($this->caseSensitiveSearch ? ereg($search_keyword, $value["value"]) : eregi($search_keyword, $value["value"])) ) { $out .= "
  • ".$value["value"]."
  • "; } } if ($out == "") { $out = "No match."; } else { $out = ""; } return $out; } /** * Checks if the url is on http. * * @return bool */ private function checkSearchURL($url) { if (substr($url, 0, 7) != "http://") { return false; } return true; } /** * Returns the content of the file/page. * * @return string */ private function fetchFileContent($filePath) { $file = @ fopen($filePath, "r"); if (!$file) { return ""; } $fileData = ""; while ($readData = fread($file, 4096)) { $fileData .= $readData; } return $fileData; } /** * Returns the application run state. * * @return string */ private function fetchRunCode() { if (isset($GLOBALS["_POST"]["search_url"])) { return "search"; } else { return "main"; } } /** * Wraps the output in HTML tags. * * @return void */ private function wrapOut() { $this->out = "".chr(10). "".chr(10). "".chr(10). "".$this->docTitle."".chr(10). "".chr(10). "".chr(10). $this->out. "".chr(10). ""; } private function printOut() { echo $this->out; } } $theApp = new feedSearch(); $theApp->run(); // for case sensitive searching use run(1) ?>