LibreY

- privacy respecting meta search engine
git clone git://git.acid.vegas/LibreY.git
Log | Files | Refs | Archive | README | LICENSE

text.php (5427B)

      1 <?php
      2     class TextSearch extends EngineRequest {
      3         public function __construct($opts, $mh) {
      4             $this->query = $opts->query;
      5             $this->page = $opts->page;
      6             $this->opts = $opts;
      7 
      8             $this->engine = $opts->preferred_engines["text"] ?? "google";
      9 
     10             $query_parts = explode(" ", $this->query);
     11             $last_word_query = end($query_parts);
     12             if (substr($this->query, 0, 1) == "!" || substr($last_word_query, 0, 1) == "!")
     13                 check_ddg_bang($this->query, $opts);
     14 
     15             if ($this->engine == "google") {
     16                 
     17                 require "engines/text/google.php";
     18                 $this->engine_request = new GoogleRequest($opts, $mh);
     19             }
     20 
     21             if ($this->engine == "duckduckgo") {
     22                 require "engines/text/duckduckgo.php";
     23                 $this->engine_request = new DuckDuckGoRequest($opts, $mh);
     24             }
     25 
     26             if (has_cooldown($this->engine, $this->opts->cooldowns) && !has_cached_results($this->engine_request->url)) {
     27                 // TODO dont add it in the first place
     28                 curl_multi_remove_handle($mh, $this->engine_request->ch);
     29                 $this->engine_request = null;
     30                 return;
     31             }
     32 
     33 
     34             require "engines/special/special.php";
     35             $this->special_request = get_special_search_request($opts, $mh);
     36         }
     37 
     38         public function parse_results($response) {
     39             if (!isset($this->engine_request))
     40                 return array();
     41 
     42             $results = $this->engine_request->get_results();
     43 
     44             if (empty($results)) {
     45                 set_cooldown($this->engine, ($opts->request_cooldown ?? "1") * 60, $this->opts->cooldowns);
     46             } else {
     47                 if ($this->special_request) {
     48                     $special_result = $this->special_request->get_results();
     49 
     50                     if ($special_result)
     51                         $results = array_merge(array($special_result), $results);
     52                 }
     53             }
     54 
     55             return $results;
     56         }
     57 
     58         public static function print_results($results, $opts)  {
     59 
     60             if (empty($results)) {
     61                 echo "<div class=\"text-result-container\"><p>An error occured fetching results</p></div>";
     62                 return;
     63             }
     64 
     65             if (array_key_exists("error", $results)) {
     66                 echo "<div class=\"text-result-container\"><p>" . $results["error"]["message"] . "</p></div>";
     67                 return;
     68             }
     69 
     70             $special = $results[0];
     71 
     72             if (array_key_exists("did_you_mean", $special)) {
     73                 $didyoumean = $special["did_you_mean"];
     74                 $new_url = "/search.php?q="  . urlencode($didyoumean);
     75                 echo "<p class=\"did-you-mean\">Did you mean ";
     76                 echo "<a href=\"$new_url\">$didyoumean</a>";
     77                 echo "?</p>";
     78             }
     79 
     80             if (array_key_exists("special_response", $special)) {
     81                 $response = $special["special_response"]["response"];
     82                 $source = $special["special_response"]["source"];
     83 
     84                 echo "<p class=\"special-result-container\">";
     85                 if (array_key_exists("image", $special["special_response"])) {
     86                     $image_url = $special["special_response"]["image"];
     87                     echo "<img src=\"image_proxy.php?url=$image_url\">";
     88                 }
     89                 echo $response;
     90                 if ($source) {
     91                     $source = check_for_privacy_frontend($source, $opts);
     92                     echo "<a href=\"$source\" target=\"_blank\">$source</a>";
     93                 }
     94                 echo "</p>";
     95             }
     96 
     97             echo "<div class=\"text-result-container\">";
     98 
     99             foreach($results as $result) {
    100                 if (!array_key_exists("title", $result))
    101                     continue;
    102 
    103                 $title = $result["title"];
    104                 $url = $result["url"];
    105                 $url = check_for_privacy_frontend($url, $opts);
    106 
    107                 $base_url = get_base_url($url);
    108                 $description = $result["description"];
    109 
    110                 echo "<div class=\"text-result-wrapper\">";
    111                 echo "<a href=\"$url\">";
    112                 echo "$base_url";
    113                 echo "<h2>$title</h2>";
    114                 echo "</a>";
    115                 echo "<span>$description</span>";
    116                 echo "</div>";
    117             }
    118 
    119             echo "</div>";
    120         }
    121     }
    122 
    123     function check_ddg_bang($query, $opts) {
    124 
    125         $bangs_json = file_get_contents("static/misc/ddg_bang.json");
    126         $bangs = json_decode($bangs_json, true);
    127 
    128         if (substr($query, 0, 1) == "!")
    129             $search_word = substr(explode(" ", $query)[0], 1);
    130         else
    131             $search_word = substr(end(explode(" ", $query)), 1);
    132         
    133         $bang_url = null;
    134 
    135         foreach($bangs as $bang) {
    136             if ($bang["t"] == $search_word) {
    137                 $bang_url = $bang["u"];
    138                 break;
    139             }
    140         }
    141 
    142         if ($bang_url) {
    143             $bang_query_array = explode("!" . $search_word, $query);
    144             $bang_query = trim(implode("", $bang_query_array));
    145 
    146             $request_url = str_replace("{{{s}}}", str_replace('%26quot%3B','%22', urlencode($bang_query)), $bang_url);
    147 
    148             header("Location: " . $request_url);
    149             die();
    150         }
    151     }
    152 
    153 ?>