diff --git a/search.php b/search.php index f0d51e8..5a40cf0 100644 --- a/search.php +++ b/search.php @@ -1,41 +1,31 @@ <?php -$query = htmlspecialchars(trim($_REQUEST["q"])); -echo $query; +$query = htmlspecialchars(trim($_REQUEST["q"] ?? '')); +echo $query ?: 'Search' . ' - Binternet'; ?> - Binternet - -
-

Binternet

- strlen($query) || strlen($query) > 64) { - header("Location: ./"); - die(); - } - - echo "value=\"$query\""; - ?> - > - -
+ +
+

Binternet

+ 64) { + header("Location: ./"); + exit(); + } + echo "value=\"" . htmlspecialchars($query) . "\""; + ?> + > +
[ "query" => $query, ], ]; - if ($bookmark != null) { + + if ($bookmark !== null) { $data_param_obj["options"]["bookmarks"] = [$bookmark]; } $data_param = urlencode(json_encode($data_param_obj)); - $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"; - } - + $finalurl = $bookmark === null ? "$url?data=$data_param" : $url; + $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"); } + return $ch; }; +// Function to perform the search and display results $search = function ($query, $bookmark) use ($prepare_search_curl_obj) { $ch = $prepare_search_curl_obj($query, $bookmark); $response = curl_exec($ch); $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 ""; + echo "
"; + + if ($data && isset($data->resource_response->data->results)) { + foreach ($data->resource_response->data->results as $result) { + $image = $result->images->orig; + $url = $image->url; + $images[] = $url; + 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) { +// Pagination link for the next page +if ($result->bookmark !== null) { $query_encoded = urlencode($query); $bookmark_encoded = urlencode($result->bookmark); $csrftoken_encoded = $csrftoken ? urlencode($csrftoken) : ""; @@ -158,6 +122,4 @@ if ($result->bookmark != null) { } include "misc/footer.php"; - - ?>