Guest User

Untitled

a guest
Jun 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. <?php
  2. class search{
  3.  
  4. private $timeout = 172800; #seconds to keep queries cached
  5.  
  6. public function run($query){
  7. global $mysql;
  8. global $secure;
  9.  
  10. $timeout = (TIME-$this->timeout);
  11. $query = $this->clearWhiteSpace($this->eraseCommonWords(strtolower($query)));
  12. $cond = $this->create($query);
  13.  
  14. $checkquery = $mysql->fetch_one("search_results","*","query='{$query}'");
  15.  
  16. if($checkquery['timestamp']>$timeout){
  17. $secure->redirect(SITEURL.'/search/'.$checkquery['hash']);
  18. }else{
  19. if($checkquery>0){ #see if the query even exists in the db
  20. $searchHash = $checkquery['hash'];
  21. $timestamp = TIMENOW;
  22. $mysql->query("UPDATE ".DBPREFIX."search_queries SET count = (count+1),timestamp ='{$timestamp}' WHERE hash='{$searchHash}' LIMIT 1");
  23. }else{ #if not added it
  24. $searchHash = $this->createHash($secure->user_ip);
  25. $array = array(
  26. "hash"=>$searchHash,
  27. "query"=>$query,
  28. "timestamp"=>TIMENOW,
  29. "count"=>1
  30. );
  31. }
  32.  
  33. $this->addResults($searchHash,$query,$cond); #add OR and AND results to database
  34. $this->addToLog($query,$secure->user_ip);
  35. $secure->redirect(SITEURL.'/search/'.$checkquery['hash']);
  36. }
  37.  
  38. }
  39.  
  40. private function addResults($searchHash,$query,$cond){
  41. global $mysql;
  42.  
  43. $sql['and'] = $mysql->select("tutorials","*","approved=1 and ({$cond['and']})","ORDER BY rating_score DESC");
  44.  
  45. foreach($sql['and'] as $and){
  46.  
  47. $array_and = array(
  48. "hash"=>$searchHash,
  49. "query"=>$query,
  50. "timestamp"=>TIMENOW,
  51. "tutorial_id"=>$and['id'],
  52. "tutorial_url"=>$and['url'],
  53. "tutorial_tags"=>$and['tags'],
  54. "tutorial_title"=>$and['title'],
  55. "tutorial_submitter"=>$and['submitter'],
  56. "tutorial_timestamp_approved"=>$and['timestamp_approved'],
  57. "tutorial_rating_total"=>$and['tutorial_rating_total'],
  58. "tutorial_rating_count"=>$and['tutorial_rating_count'],
  59. "tutorial_rating_avg"=>$and['tutorial_rating_avg'],
  60. "tutorial_rating_score"=>$and['tutorial_rating_score']
  61. );
  62.  
  63. $insert_and = $mysql->insert("search_results",$array_and);
  64. $and_array[]="{$and['title']}+{$and['id']}";
  65. }
  66.  
  67. $sql['or'] = $mysql->select("tutorials","*","approved=1 and ({$cond['or']})","ORDER BY rating_score DESC");
  68.  
  69. foreach($sql['or'] as $or){
  70.  
  71. if(in_array("{$or['title']}+{$or['id']}",$and_array)) continue;
  72.  
  73. $array_or = array(
  74. "hash"=>$searchHash,
  75. "query"=>$query,
  76. "timestamp"=>TIMENOW,
  77. "tutorial_id"=>$or['id'],
  78. "tutorial_url"=>$or['url'],
  79. "tutorial_tags"=>$or['tags'],
  80. "tutorial_title"=>$or['title'],
  81. "tutorial_submitter"=>$or['submitter'],
  82. "tutorial_timestamp_approved"=>$or['timestamp_approved'],
  83. "tutorial_rating_total"=>$or['tutorial_rating_total'],
  84. "tutorial_rating_count"=>$or['tutorial_rating_count'],
  85. "tutorial_rating_avg"=>$or['tutorial_rating_avg'],
  86. "tutorial_rating_score"=>$or['tutorial_rating_score']
  87. );
  88.  
  89. $insert_or = $mysql->insert("search_results",$array_or);
  90.  
  91. }
  92.  
  93. }
  94.  
  95. private function createHash($ip){
  96. global $helper;
  97. return md5(TIMENOW.$ip.$helper->randomString(10));
  98. }
  99.  
  100. public function create($query){
  101.  
  102. $query = str_replace(',',' ',$query);
  103. $query = str_replace('-',' ',$query);
  104. $andquery ='';
  105. $orquery ='';
  106. #$query = $this->clearWhiteSpace($this->eraseCommonWords(strtolower($query)));
  107. $qarray = explode(" ",$query);
  108. foreach($qarray as $q){
  109. if(strlen($q)>=2){
  110. $andquery .=" (tags like '%{$q}%' or title like '%{$q}%' or url like '%{$q}%') and";
  111. $orquery .=" (tags like '%{$q}%' or title like '%{$q}%' or url like '%{$q}%') or";
  112. }
  113. }
  114. $return['and'] = substr($andquery, 0, -3);
  115. $return['or'] = substr($orquery, 0, -2);
  116. return $return;
  117.  
  118. }
  119.  
  120. private function clearWhiteSpace($s){
  121. $str = trim($s);
  122. $ret_str='';
  123. for($i=0;$i < strlen($str);$i++){
  124. if(substr($str, $i, 1) != " "){
  125. $ret_str .= trim(substr($str, $i, 1));
  126. }else{
  127. while(substr($str,$i,1) == " "){
  128. $i++;
  129. }
  130. $ret_str.= " ";
  131. $i--; // ***
  132. }
  133. }
  134. return $ret_str;
  135. }
  136. private function eraseCommonWords($query){
  137. $words = array(
  138. "I","a","about","an","are","and","as","at","be","by","com","de",
  139. "en","for","from","how","in","is","it","la","of","on","or","that","the",
  140. "this","to","was","what", "when","where","who","will","with","und","the",
  141. "www","tutorial","tutorials"
  142. );
  143. foreach($words as $word){
  144. $query = str_replace(" ".$word." ",null,$query);
  145. }
  146. return $query;
  147. }
  148.  
  149. public function addToLog($query,$ip){
  150. global $mysql;
  151. $array_query = array(
  152. "query"=>$query,
  153. "timestamp"=>TIMENOW,
  154. "ip"=>$ip
  155. );
  156. $insert = $mysql->insert("search_log",$array_query);
  157. }
  158. }
Add Comment
Please, Sign In to add comment