Browse Source

Update search.php

Fixes urlencode() receiving null as a parameter. Add check to ensure that $data is not null.
Danii Saahir 11 months ago
parent
commit
012d22cbb0
1 changed files with 61 additions and 42 deletions
  1. 61 42
      search.php

+ 61 - 42
search.php

@@ -1,8 +1,8 @@
 <?php require "misc/header.php"; ?>
 <?php require "misc/header.php"; ?>
 <title>
 <title>
 <?php
 <?php
-  $query = htmlspecialchars(trim($_REQUEST["q"]));
+$query = htmlspecialchars(trim($_REQUEST["q"]));
-  echo $query;
+echo $query;
 ?> - Binternet</title>
 ?> - Binternet</title>
 </head>
 </head>
     <body>
     <body>
@@ -10,23 +10,21 @@
             <h1><a class="no-decoration accent" href="./">Binternet</a></h1>
             <h1><a class="no-decoration accent" href="./">Binternet</a></h1>
             <input type="text" name="q" placeholder="Search Image"
             <input type="text" name="q" placeholder="Search Image"
                 <?php
                 <?php
-                    $query_encoded = urlencode($query);
+                $query_encoded = urlencode($query);
 
 
-                    if (1 > strlen($query) || strlen($query) > 64)
+                if (1 > strlen($query) || strlen($query) > 64) {
-                    {
+                    header("Location: ./");
-                        header("Location: ./");
+                    die();
-                        die();
+                }
-                    }
 
 
-                    echo "value=\"$query\"";
+                echo "value=\"$query\"";
                 ?>
                 ?>
             >
             >
             <!-- <div></div> -->
             <!-- <div></div> -->
         </form>
         </form>
 
 
 <?php
 <?php
-
+$query = $_GET["q"];
-$query = $_GET['q'];
 
 
 $bookmark = null;
 $bookmark = null;
 if (array_key_exists("bookmark", $_GET)) {
 if (array_key_exists("bookmark", $_GET)) {
@@ -46,24 +44,24 @@ class SearchResult
     public $bookmark;
     public $bookmark;
 }
 }
 
 
-$header_function = function($ch, $rawheader)
+$header_function = function ($ch, $rawheader) {
-{
     global $csrftoken;
     global $csrftoken;
     $len = strlen($rawheader);
     $len = strlen($rawheader);
 
 
     $header = explode(":", $rawheader, 2);
     $header = explode(":", $rawheader, 2);
-    if (count($header) != 2)
+    if (count($header) != 2) {
         return $len;
         return $len;
+    }
 
 
     // we are only interested in set-cookie header
     // we are only interested in set-cookie header
-    if (trim($header[0]) != "set-cookie")
+    if (trim($header[0]) != "set-cookie") {
         return $len;
         return $len;
+    }
 
 
     $cookie = explode(";", trim($header[1]), 2);
     $cookie = explode(";", trim($header[1]), 2);
     $cookie = explode("=", $cookie[0], 2);
     $cookie = explode("=", $cookie[0], 2);
 
 
-    switch ($cookie[0])
+    switch ($cookie[0]) {
-    {
         case "csrftoken":
         case "csrftoken":
             $csrftoken = $cookie[1];
             $csrftoken = $cookie[1];
     }
     }
@@ -71,74 +69,95 @@ $header_function = function($ch, $rawheader)
     return $len;
     return $len;
 };
 };
 
 
-$prepare_search_curl_obj = function($query, $bookmark) use ($url, $header_function, $csrftoken)
+$prepare_search_curl_obj = function ($query, $bookmark) use (
-{
+    $url,
-    $data_param_obj = array(
+    $header_function,
-        "options"=>array(
+    $csrftoken
-            "query"=>$query
+) {
-        )
+    $data_param_obj = [
-    );
+        "options" => [
-    if ($bookmark != null)
+            "query" => $query,
-        $data_param_obj["options"]["bookmarks"] = array($bookmark);
+        ],
+    ];
+    if ($bookmark != null) {
+        $data_param_obj["options"]["bookmarks"] = [$bookmark];
+    }
 
 
     $data_param = urlencode(json_encode($data_param_obj));
     $data_param = urlencode(json_encode($data_param_obj));
 
 
-    $headers = array();
+    $headers = [];
-    if ($csrftoken != null)
+    if ($csrftoken != null) {
-    {
         $headers[] = "x-csrftoken: $csrftoken";
         $headers[] = "x-csrftoken: $csrftoken";
         $headers[] = "cookie: csrftoken=$csrftoken";
         $headers[] = "cookie: csrftoken=$csrftoken";
     }
     }
 
 
     $finalurl = $url;
     $finalurl = $url;
-    if ($bookmark == null)
+    if ($bookmark == null) {
         $finalurl = "$url?data=$data_param";
         $finalurl = "$url?data=$data_param";
+    }
 
 
     $ch = curl_init($finalurl);
     $ch = curl_init($finalurl);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
     curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
-    if ($bookmark != null)
+    if ($bookmark != null) {
-    {
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
         curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
     }
     }
     return $ch;
     return $ch;
 };
 };
 
 
-$search = function($query, $bookmark) use($prepare_search_curl_obj)
+$search = function ($query, $bookmark) use ($prepare_search_curl_obj) {
-{
     $ch = $prepare_search_curl_obj($query, $bookmark);
     $ch = $prepare_search_curl_obj($query, $bookmark);
     $response = curl_exec($ch);
     $response = curl_exec($ch);
     $data = json_decode($response);
     $data = json_decode($response);
-    $images = array();
+    $images = [];
     echo "<div class=img-container>";
     echo "<div class=img-container>";
-        foreach ($data->{"resource_response"}->{"data"}->{"results"} as $result)
+    if (
-        {
+        $data &&
+        property_exists($data, "resource_response") &&
+        property_exists($data->{"resource_response"}, "data") &&
+        property_exists($data->{"resource_response"}->{"data"}, "results")
+    ) {
+        foreach (
+            $data->{"resource_response"}->{"data"}->{"results"}
+            as $result
+        ) {
             $image = $result->{"images"}->{"orig"};
             $image = $result->{"images"}->{"orig"};
             $url = $image->{"url"};
             $url = $image->{"url"};
             array_push($images, $url);
             array_push($images, $url);
             echo "<a class=img-result href='/image_proxy.php?url=", $url, "'>";
             echo "<a class=img-result href='/image_proxy.php?url=", $url, "'>";
-            echo "<img loading='lazy' src='/image_proxy.php?url=", $url, "'></a>";
+            echo "<img loading='lazy' src='/image_proxy.php?url=",
+                $url,
+                "'></a>";
         }
         }
+    } else {
+        echo "<p>No results found.</p>";
+    }
     echo "</div>";
     echo "</div>";
     $result = new SearchResult();
     $result = new SearchResult();
     $result->images = $images;
     $result->images = $images;
-    if (property_exists($data->{"resource_response"}, "bookmark"))
+    if (
+        $data &&
+        property_exists($data, "resource_response") &&
+        property_exists($data->{"resource_response"}, "bookmark")
+    ) {
         $result->bookmark = $data->{"resource_response"}->{"bookmark"};
         $result->bookmark = $data->{"resource_response"}->{"bookmark"};
+    }
     return $result;
     return $result;
 };
 };
 
 
 $result = $search($query, $bookmark);
 $result = $search($query, $bookmark);
 
 
-if ($result->bookmark != null)
+if ($result->bookmark != null) {
-{
     $query_encoded = urlencode($query);
     $query_encoded = urlencode($query);
     $bookmark_encoded = urlencode($result->bookmark);
     $bookmark_encoded = urlencode($result->bookmark);
-    $csrftoken_encoded = urlencode($csrftoken);
+    $csrftoken_encoded = $csrftoken ? urlencode($csrftoken) : "";
+
     echo "<h2 style=\"text-align: center;\"><a href=\"/search.php?q=$query_encoded&bookmark=$bookmark_encoded&csrftoken=$csrftoken_encoded\">Next page</a></h2><br><br><br>";
     echo "<h2 style=\"text-align: center;\"><a href=\"/search.php?q=$query_encoded&bookmark=$bookmark_encoded&csrftoken=$csrftoken_encoded\">Next page</a></h2><br><br><br>";
 }
 }
 
 
 include "misc/footer.php";
 include "misc/footer.php";
 
 
+
 ?>
 ?>