LibreY

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

google.php (2932B)

      1 <?php
      2     class GoogleRequest extends EngineRequest {
      3         public function get_request_url() {
      4 
      5             $query_encoded = str_replace("%22", "\"", urlencode($this->query));
      6             $results = array();
      7 
      8             $domain = $this->opts->google_domain;
      9             $results_language = $this->opts->language;
     10             $number_of_results = $this->opts->number_of_results;
     11 
     12             $url = "https://www.google.$domain/search?q=$query_encoded&nfpr=1&start=$this->page";
     13 
     14             if (3 > strlen($results_language) && 0 < strlen($results_language)) {
     15                 $url .= "&lr=lang_$results_language";
     16                 $url .= "&hl=$results_language";
     17             }
     18 
     19             if (3 > strlen($number_of_results) && 0 < strlen($number_of_results))
     20                 $url .= "&num=$number_of_results";
     21 
     22             if (isset($_COOKIE["safe_search"]))
     23                 $url .= "&safe=medium";
     24 
     25             return $url;
     26         }
     27 
     28 
     29         public function parse_results($response) {
     30             $results = array();
     31             $xpath = get_xpath($response);
     32 
     33             if (!$xpath)
     34                 return $results;
     35 
     36             $didyoumean = $xpath->query(".//a[@class='gL9Hy']")[0];
     37 
     38             if (!is_null($didyoumean))
     39                 array_push($results, array(
     40                     "did_you_mean" => $didyoumean->textContent
     41                 ));
     42 
     43             foreach($xpath->query("//div[@id='search']//div[contains(@class, 'g')]") as $result) {
     44                 $url = $xpath->evaluate(".//div[@class='yuRUbf']//a/@href", $result)[0];
     45 
     46                 if ($url == null)
     47                     continue;
     48 
     49                     if (!empty($results) && array_key_exists("url", $results) && end($results)["url"] == $url->textContent)
     50                         continue;
     51 
     52                 $url = $url->textContent;
     53 
     54                 $title = $xpath->evaluate(".//h3", $result)[0];
     55                 $description = $xpath->evaluate(".//div[contains(@class, 'VwiC3b')]", $result)[0];
     56 
     57                 array_push($results,
     58                     array (
     59                         "title" => htmlspecialchars($title->textContent),
     60                         "url" =>  htmlspecialchars($url),
     61                         // base_url is to be removed in the future, see #47
     62                         "base_url" => htmlspecialchars(get_base_url($url)),
     63                         "description" =>  $description == null ?
     64                                           TEXTS["result_no_description"] :
     65                                           htmlspecialchars($description->textContent)
     66                     )
     67                 );
     68             }
     69 
     70             if (empty($results) && !str_contains($response, "Our systems have detected unusual traffic from your computer network.")) {
     71                 $results["error"] = array(
     72                     "message" => TEXTS["failure_empty"]
     73                 );
     74             }
     75 
     76             return $results;
     77         }
     78     }
     79 ?>