Browse Source

added source url to image results, removed content security header because it caused issues

hnhx 2 years ago
parent
commit
14a70acf2a
4 changed files with 36 additions and 21 deletions
  1. 10 2
      api.php
  2. 1 1
      config.php.example
  3. 25 17
      engines/google/image.php
  4. 0 1
      misc/header.php

+ 10 - 2
api.php

@@ -4,7 +4,15 @@
 
     if (!isset($_REQUEST["q"]))
     {
-        echo "API usage: <a href=\"https://github.com/hnhx/librex/#api\">https://github.com/hnhx/librex/</a>";
+        echo "<p>Example API request: <a href=\"./api.php?q=gentoo&p=2&type=0\">./api.php?q=gentoo&p=2&type=0</a></p>
+        <br/>
+        <p>\"q\" is the keyword</p>
+        <p>\"p\" is the result page (the first page is 0)</p>
+        <p>\"type\" is the search type (0=text, 1=image, 2=video, 3=torrent)</p>
+        <br/>
+        <p>The results are going to be in JSON format.</p>
+        <p>The API supports both POST and GET requests.</p>";
+
         die();
     }
 
@@ -44,6 +52,6 @@
             break;
     }
 
-    header('Content-Type: application/json');
+    header("Content-Type: application/json");
     echo json_encode($results, JSON_PRETTY_PRINT);
 ?>

+ 1 - 1
config.php.example

@@ -39,7 +39,7 @@
             // CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
             CURLOPT_RETURNTRANSFER => true,
             CURLOPT_ENCODING => "",
-            CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36",
+            CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36",
             CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
             CURLOPT_CUSTOMREQUEST => "GET",
             CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,

+ 25 - 17
engines/google/image.php

@@ -8,21 +8,33 @@
         $xpath = get_xpath($response);
 
         $mh = curl_multi_init();
-        $chs = $alts = $results = array();
+        $chs = $results = array();
 
-        foreach($xpath->query("//img[@data-src]") as $image)
+        foreach($xpath->query("//div[@class='isv-r PNCib MSM1fd BUooTd']") as $result)
         {       
-                $alt = $image->getAttribute("alt");
-                $src = $image->getAttribute("data-src");
+                $image = $xpath->evaluate(".//img[@data-src]", $result)[0];
+                $url = $xpath->evaluate(".//a/@href", $result)[0]->textContent;
 
-                if (!empty($alt)) 
+                if (!empty($image))
                 {
-                    $ch = curl_init($src);
-                    curl_setopt_array($ch, $config->curl_settings);
-                    array_push($chs, $ch);
-                    curl_multi_add_handle($mh, $ch);
+                    $alt = $image->getAttribute("alt");
+                    $src = $image->getAttribute("data-src");
 
-                    array_push($alts, $alt);
+                    if (!empty($alt)) 
+                    {
+                        $ch = curl_init($src);
+                        curl_setopt_array($ch, $config->curl_settings);
+                        array_push($chs, $ch);
+                        curl_multi_add_handle($mh, $ch);
+
+                        array_push($results, 
+                            array (
+                                "base64" => "",
+                                "alt" => htmlspecialchars($alt),
+                                "url" => htmlspecialchars($url)
+                            )
+                        );
+                    }
                 }
         }
 
@@ -34,12 +46,7 @@
         for ($i=0; count($chs)>$i; $i++)
         {
             $img_base64 = base64_encode(curl_multi_getcontent($chs[$i]));
-            array_push($results, 
-                array (
-                    "base64" => $img_base64,
-                    "alt" => htmlspecialchars($alts[$i])
-                )
-            );
+            $results[$i]["base64"] = $img_base64;
         }
 
         return $results;
@@ -53,8 +60,9 @@
             {
                 $src = $result["base64"];
                 $alt = $result["alt"];
+                $url = $result["url"];
 
-                echo "<a title=\"$alt\" href=\"data:image/jpeg;base64,$src\" target=\"_blank\">";
+                echo "<a title=\"$alt\" href=\"$url\" target=\"_blank\">";
                 echo "<img src=\"data:image/jpeg;base64,$src\" height=\"200\">";
                 echo "</a>";
             }

+ 0 - 1
misc/header.php

@@ -1,7 +1,6 @@
 <!DOCTYPE html >
 <html lang="en">
     <head>
-        <?php header("Content-Security-Policy: sandbox allow-forms allow-top-navigation; default-src 'self'; img-src 'self' data:;"); ?>
         <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
         <meta charset="UTF-8"/>
         <meta name="description" content="A privacy respecting meta search engine."/>