Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class search{
- private $timeout = 172800; #seconds to keep queries cached
- public function run($query){
- global $mysql;
- global $secure;
- $timeout = (TIME-$this->timeout);
- $query = $this->clearWhiteSpace($this->eraseCommonWords(strtolower($query)));
- $cond = $this->create($query);
- $checkquery = $mysql->fetch_one("search_results","*","query='{$query}'");
- if($checkquery['timestamp']>$timeout){
- $secure->redirect(SITEURL.'/search/'.$checkquery['hash']);
- }else{
- if($checkquery>0){ #see if the query even exists in the db
- $searchHash = $checkquery['hash'];
- $timestamp = TIMENOW;
- $mysql->query("UPDATE ".DBPREFIX."search_queries SET count = (count+1),timestamp ='{$timestamp}' WHERE hash='{$searchHash}' LIMIT 1");
- }else{ #if not added it
- $searchHash = $this->createHash($secure->user_ip);
- $array = array(
- "hash"=>$searchHash,
- "query"=>$query,
- "timestamp"=>TIMENOW,
- "count"=>1
- );
- }
- $this->addResults($searchHash,$query,$cond); #add OR and AND results to database
- $this->addToLog($query,$secure->user_ip);
- $secure->redirect(SITEURL.'/search/'.$checkquery['hash']);
- }
- }
- private function addResults($searchHash,$query,$cond){
- global $mysql;
- $sql['and'] = $mysql->select("tutorials","*","approved=1 and ({$cond['and']})","ORDER BY rating_score DESC");
- foreach($sql['and'] as $and){
- $array_and = array(
- "hash"=>$searchHash,
- "query"=>$query,
- "timestamp"=>TIMENOW,
- "tutorial_id"=>$and['id'],
- "tutorial_url"=>$and['url'],
- "tutorial_tags"=>$and['tags'],
- "tutorial_title"=>$and['title'],
- "tutorial_submitter"=>$and['submitter'],
- "tutorial_timestamp_approved"=>$and['timestamp_approved'],
- "tutorial_rating_total"=>$and['tutorial_rating_total'],
- "tutorial_rating_count"=>$and['tutorial_rating_count'],
- "tutorial_rating_avg"=>$and['tutorial_rating_avg'],
- "tutorial_rating_score"=>$and['tutorial_rating_score']
- );
- $insert_and = $mysql->insert("search_results",$array_and);
- $and_array[]="{$and['title']}+{$and['id']}";
- }
- $sql['or'] = $mysql->select("tutorials","*","approved=1 and ({$cond['or']})","ORDER BY rating_score DESC");
- foreach($sql['or'] as $or){
- if(in_array("{$or['title']}+{$or['id']}",$and_array)) continue;
- $array_or = array(
- "hash"=>$searchHash,
- "query"=>$query,
- "timestamp"=>TIMENOW,
- "tutorial_id"=>$or['id'],
- "tutorial_url"=>$or['url'],
- "tutorial_tags"=>$or['tags'],
- "tutorial_title"=>$or['title'],
- "tutorial_submitter"=>$or['submitter'],
- "tutorial_timestamp_approved"=>$or['timestamp_approved'],
- "tutorial_rating_total"=>$or['tutorial_rating_total'],
- "tutorial_rating_count"=>$or['tutorial_rating_count'],
- "tutorial_rating_avg"=>$or['tutorial_rating_avg'],
- "tutorial_rating_score"=>$or['tutorial_rating_score']
- );
- $insert_or = $mysql->insert("search_results",$array_or);
- }
- }
- private function createHash($ip){
- global $helper;
- return md5(TIMENOW.$ip.$helper->randomString(10));
- }
- public function create($query){
- $query = str_replace(',',' ',$query);
- $query = str_replace('-',' ',$query);
- $andquery ='';
- $orquery ='';
- #$query = $this->clearWhiteSpace($this->eraseCommonWords(strtolower($query)));
- $qarray = explode(" ",$query);
- foreach($qarray as $q){
- if(strlen($q)>=2){
- $andquery .=" (tags like '%{$q}%' or title like '%{$q}%' or url like '%{$q}%') and";
- $orquery .=" (tags like '%{$q}%' or title like '%{$q}%' or url like '%{$q}%') or";
- }
- }
- $return['and'] = substr($andquery, 0, -3);
- $return['or'] = substr($orquery, 0, -2);
- return $return;
- }
- private function clearWhiteSpace($s){
- $str = trim($s);
- $ret_str='';
- for($i=0;$i < strlen($str);$i++){
- if(substr($str, $i, 1) != " "){
- $ret_str .= trim(substr($str, $i, 1));
- }else{
- while(substr($str,$i,1) == " "){
- $i++;
- }
- $ret_str.= " ";
- $i--; // ***
- }
- }
- return $ret_str;
- }
- private function eraseCommonWords($query){
- $words = array(
- "I","a","about","an","are","and","as","at","be","by","com","de",
- "en","for","from","how","in","is","it","la","of","on","or","that","the",
- "this","to","was","what", "when","where","who","will","with","und","the",
- "www","tutorial","tutorials"
- );
- foreach($words as $word){
- $query = str_replace(" ".$word." ",null,$query);
- }
- return $query;
- }
- public function addToLog($query,$ip){
- global $mysql;
- $array_query = array(
- "query"=>$query,
- "timestamp"=>TIMENOW,
- "ip"=>$ip
- );
- $insert = $mysql->insert("search_log",$array_query);
- }
- }
Add Comment
Please, Sign In to add comment