Browse Source

Added suggestions and fixed drag and drop

Pablo Ferreiro 2 years ago
parent
commit
ba589f8361
3 changed files with 30 additions and 4 deletions
  1. 16 0
      engines/google/suggestions.php
  2. 4 4
      opensearch.xml.example
  3. 10 0
      suggestions.php

+ 16 - 0
engines/google/suggestions.php

@@ -0,0 +1,16 @@
+<?php
+    function get_suggestions_results($query) {
+        global $config;
+        $query_encoded = urlencode($query);
+        $url = "https://www.google.$config->google_domain/complete/search?q=$query_encoded&output=firefox&hl=$config->google_language";
+
+        // Make request
+        $ch = curl_init($url);
+        curl_setopt_array($ch, $config->curl_settings);
+        $result = curl_exec($ch);
+        return $result;
+    }
+
+    function print_suggestions_results($result) {
+        echo $result;
+    }

+ 4 - 4
opensearch.xml.example

@@ -4,11 +4,11 @@
   <Description>A meta search engine for Google.</Description>
   <InputEncoding>UTF-8</InputEncoding>
   <LongName>LibreX search</LongName>
-    <Url rel="results" type="text/html" method="post" template="http://localhost/search.php">
-        <Param name="q" value="{searchTerms}" />
-    </Url>
+  <Url rel="results" type="text/html" method="get" template="http://localhost/search.php?q={searchTerms}" />
+
+  <Url rel="suggestions" type="application/x-suggestions+json" method="get" template="http://localhost/suggestions.php?q={searchTerms}" />
 
   <Url type="application/opensearchdescription+xml"
       rel="self"
       template="/opensearch.xml?method=POST" />
-</OpenSearchDescription>
+</OpenSearchDescription>

+ 10 - 0
suggestions.php

@@ -0,0 +1,10 @@
+<?php
+    $config = require "config.php";
+    require "engines/google/suggestions.php";
+
+    header('Content-Type: application/x-suggestions+xml');
+    header('Content-Disposition: attachment; filename="suggestions.json"');
+
+    $q = isset($_REQUEST['q']) ? htmlspecialchars(trim($_REQUEST["q"])) : '';
+    $result = get_suggestions_results($q);
+    echo print_suggestions_results($result, $q);