[eluser]skunkbad[/eluser]
Code:
class Search extends Controller {
private $keyword = null;
private $error = null;
private $local = 0;
private $min_chars = 3;
private $max_chars = 30;
private $boundary = '<!-- SEARCH BOUNDARY -->';
private $message_A= "Your search results for:";
private $message_B = "Invalid Search <span style='font-style:italic; font-size:80%;'>(too short or too long)</span>";
private $message_C = "The value that you are searching for is either less than 3 characters, or more than 30 characters. Please try to search again with a search value between 3 and 30 characters in length.";
private $message_D = "No Search Value";
private $message_E = "The search program can't search for an empty value. Try typing something in to the search form before pressing the \"Search\" button.";
private $message_F = "There were no instances of ";
private $message_G = " found on any of the pages of this site";
private $no_title = "Untitled";
private $limit_extracts = "10";
private $contents;
public function index()
{
$this->keyword = $this->input->post('keyword');
$this->searchHeader();
$this->searchFiles();
// insert page specific data into template
$data = array(
'title' => 'Brian\'s Web Design - Search',
'robots' => 'noindex,nofollow',
'style_sheet_names' => array(
'css/style.css',
'css/page_specific/search/screen.css'
),
'media_targets' => array(
'screen',
'screen'
),
'javascripts' => array(
'js/target_blank.js'
),
'slogan' => 'Great Web Design - Quality You Can Trust',
'content' => $this->load->view('search', array('search_results' => $this->contents) , TRUE)
);
$this->load->view('template_content', $data );
}
private function searchHeader() {
$this->searchError();
if ($this->error!=1&&isset;($this->keyword)){
$this->contents = "<h2>$this->message_A <i>".htmlentities(stripslashes($this->keyword))."</i></h2>\n";
}
}
private function searchError() {
if (isset($this->keyword)){
if(strlen($this->keyword)<$this->min_chars||strlen($this->keyword)>$this->max_chars) {
$this->contents = "<h2>$this->message_B</h2>";
$this->contents .= "<p>$this->message_C</p>";
$this->error = 1;
}
}else{
$this->contents = "<h2>$this->message_D</h2>";
$this->contents .= "<p>$this->message_E</p>";
$this->error = 1;
}
}
private function searchFiles() {
if (isset($this->keyword)&&$this->error!=1){
$pageList = file(site_url('sitemap/txt', 'http'), FILE_IGNORE_NEW_LINES);
foreach($pageList as $page){
$fgcdata = file_get_contents($page);
$chopped = explode($this->boundary,$fgcdata);
static $count_hits = null;
if (count($chopped) >= 3){
$text = null;
for($i=0;$i<=count($chopped)-1;$i++){
if ($i%2){
$text .= $chopped[$i];
}
}
$text = strip_tags($text);
$keyword_html = htmlentities($this->keyword);
$do=stristr($text, $this->keyword)||stristr($text, $keyword_html);
if($do) {
$count_hits++;
if(preg_match_all("=<title[^>]*>(.*)</title>=siU", $fgcdata, $title)) {
if(!$title[1][0])
$link_title=$no_title;
else
$link_title=$title[1][0];
}
else {
$link_title=$no_title;
}
($page == $pageList[0]) ? $page = base_url() : $page = $page;
$this->contents .= "<p class=\"itemP\">\n\t<a >$count_hits. $link_title</a>\n\t<br />\n\t";
$this->keyword = preg_quote($this->keyword);
$this->keyword = str_replace("/","\/","$this->keyword");
$keyword_html = preg_quote($keyword_html);
$keyword_html = str_replace("/","\/","$keyword_html");
$this->contents .= "<span class=\"extract\">";
if(preg_match_all("/((\s\S*){0,3})($this->keyword|$keyword_html)((\s?\S*){0,3})/i", $text, $match, PREG_SET_ORDER)); {
if(!$this->limit_extracts)
$number=count($match);
else
$number=$this->limit_extracts;
for ($h=0;$h<$number;$h++) {
if (!empty($match[$h][3]))
$this->contents .= sprintf("<i><b>..</b> %s<b>%s</b>%s <b>..</b></i>", $match[$h][1], $match[$h][3], $match[$h][4]);
}
}
$this->contents .= "</span></p>\n";
flush();
}
}
}
if ($count_hits == null){
$this->contents .= "<p>$this->message_F<b>" . htmlentities($this->keyword) . "</b>$this->message_G.</p>";
}
}
}
}