From 2bed56224fea6f3a823591c49d4fbc7f5a750099 Mon Sep 17 00:00:00 2001
From: Danii Saahir <158811885+danii-saahir@users.noreply.github.com>
Date: Fri, 17 May 2024 00:04:57 +0800
Subject: [PATCH] Update search.php
1. Sanitize input properly.
2. Streamlining the query and parameter handling.
3. Adding pagination with "Previous" and page number.
4. Handling potential cURL errors.
---
search.php | 173 +++++++++++++++++++----------------------------------
1 file changed, 60 insertions(+), 113 deletions(-)
diff --git a/search.php b/search.php
index f0d51e8..b2db009 100644
--- a/search.php
+++ b/search.php
@@ -1,163 +1,110 @@
-
+
- Binternet
+$query = htmlspecialchars(trim($_GET['q']??''), ENT_QUOTES, 'UTF-8');
+if (strlen($query) < 1 || strlen($query) > 64) {
+ header("Location: ./");
+ die();
+}
+?>
+
+ - Binternet
-
-
+
+
[
- "query" => $query,
- ],
- ];
- if ($bookmark != null) {
+$prepare_search_curl_obj = function ($query, $bookmark) use ($url, $header_function, $csrftoken) {
+ $data_param_obj = ["options" => ["query" => $query]];
+ if ($bookmark !== null) {
$data_param_obj["options"]["bookmarks"] = [$bookmark];
}
-
- $data_param = urlencode(json_encode($data_param_obj));
-
+ $data_param = json_encode($data_param_obj);
+ $finalurl = $url . "?data=" . urlencode($data_param);
$headers = [];
- if ($csrftoken != null) {
+ if ($csrftoken !== null) {
$headers[] = "x-csrftoken: $csrftoken";
$headers[] = "cookie: csrftoken=$csrftoken";
}
-
- $finalurl = $url;
- if ($bookmark == null) {
- $finalurl = "$url?data=$data_param";
- }
-
$ch = curl_init($finalurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- if ($bookmark != null) {
+ if ($bookmark !== null) {
curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
+ curl_setopt($ch, CURLOPT_POSTFIELDS, "data=" . urlencode($data_param));
}
return $ch;
};
-
$search = function ($query, $bookmark) use ($prepare_search_curl_obj) {
$ch = $prepare_search_curl_obj($query, $bookmark);
$response = curl_exec($ch);
+ if (curl_errno($ch)) {
+ echo "cURL error: " . curl_error($ch);
+ return new SearchResult();
+ }
$data = json_decode($response);
- $images = [];
- echo "";
- 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"};
- $url = $image->{"url"};
- array_push($images, $url);
- echo "
";
- echo "
";
+ $result = new SearchResult();
+ if ($data && isset($data->resource_response->data->results)) {
+ echo "
";
+ foreach ($data->resource_response->data->results as $item) {
+ $image_url = $item->images->orig->url??null;
+ if ($image_url) {
+ $result->images[] = $image_url;
+ echo "
";
+ echo " . ")
";
+ }
}
+ echo "
";
} else {
echo "
No results found.
";
}
- echo "
";
- $result = new SearchResult();
- $result->images = $images;
- if (
- $data &&
- property_exists($data, "resource_response") &&
- property_exists($data->{"resource_response"}, "bookmark")
- ) {
- $result->bookmark = $data->{"resource_response"}->{"bookmark"};
+ if (isset($data->resource_response->bookmark)) {
+ $result->bookmark = $data->resource_response->bookmark;
}
return $result;
};
-
$result = $search($query, $bookmark);
-
-if ($result->bookmark != null) {
+if ($result->bookmark !== null) {
$query_encoded = urlencode($query);
- $bookmark_encoded = urlencode($result->bookmark);
$csrftoken_encoded = $csrftoken ? urlencode($csrftoken) : "";
-
- echo "
";
+ $prev_page = $page > 1 ? $page - 1 : 1;
+ $next_page = $page + 1;
+ $bookmark_encoded = urlencode($result->bookmark);
+ echo "
";
}
-
include "misc/footer.php";
-
-
?>