search.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php require "misc/header.php"; ?>
  2. <title>
  3. <?php
  4. $query = htmlspecialchars(trim($_REQUEST["q"] ?? ''));
  5. echo $query ?: 'Search' . ' - Binternet';
  6. ?> - Binternet</title>
  7. </head>
  8. <body>
  9. <form class="search-container" method="get" autocomplete="off">
  10. <h1><a class="no-decoration accent" href="./">Binternet</a></h1>
  11. <input type="text" name="q" placeholder="Search Image"
  12. <?php
  13. // Validate query length
  14. if (strlen($query) < 1 || strlen($query) > 64) {
  15. header("Location: ./");
  16. exit();
  17. }
  18. echo "value=\"" . htmlspecialchars($query) . "\"";
  19. ?>
  20. >
  21. </form>
  22. <?php
  23. // Fetching query and optional parameters
  24. $bookmark = $_GET["bookmark"] ?? null;
  25. $csrftoken = $_GET["csrftoken"] ?? null;
  26. // Pinterest API endpoint
  27. $url = "https://www.pinterest.com/resource/BaseSearchResource/get/";
  28. class SearchResult
  29. {
  30. public $images;
  31. public $bookmark;
  32. }
  33. // Header function to capture CSRF token from response
  34. $header_function = function ($ch, $rawheader) use (&$csrftoken) {
  35. if (preg_match('/^set-cookie:\s*csrftoken=([^;]*)/', $rawheader, $matches)) {
  36. $csrftoken = $matches[1];
  37. }
  38. return strlen($rawheader);
  39. };
  40. // Prepare CURL object for search request
  41. $prepare_search_curl_obj = function ($query, $bookmark) use ($url, $header_function, $csrftoken) {
  42. $data_param_obj = [
  43. "options" => [
  44. "query" => $query,
  45. ],
  46. ];
  47. if ($bookmark !== null) {
  48. $data_param_obj["options"]["bookmarks"] = [$bookmark];
  49. }
  50. $data_param = urlencode(json_encode($data_param_obj));
  51. $headers = [];
  52. if ($csrftoken !== null) {
  53. $headers[] = "x-csrftoken: $csrftoken";
  54. $headers[] = "cookie: csrftoken=$csrftoken";
  55. }
  56. $finalurl = $bookmark === null ? "$url?data=$data_param" : $url;
  57. $ch = curl_init($finalurl);
  58. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  59. curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
  60. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  61. if ($bookmark !== null) {
  62. curl_setopt($ch, CURLOPT_POST, true);
  63. curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
  64. }
  65. return $ch;
  66. };
  67. // Function to perform the search and display results
  68. $search = function ($query, $bookmark) use ($prepare_search_curl_obj) {
  69. $ch = $prepare_search_curl_obj($query, $bookmark);
  70. $response = curl_exec($ch);
  71. $data = json_decode($response);
  72. $images = [];
  73. echo "<div class='img-container'>";
  74. if ($data && isset($data->resource_response->data->results)) {
  75. foreach ($data->resource_response->data->results as $result) {
  76. $image = $result->images->orig;
  77. $url = $image->url;
  78. $images[] = $url;
  79. echo "<a class='img-result' href='/image_proxy.php?url=" . htmlspecialchars($url) . "'>";
  80. echo "<img loading='lazy' src='/image_proxy.php?url=" . htmlspecialchars($url) . "'></a>";
  81. }
  82. } else {
  83. echo "<p>No results found.</p>";
  84. }
  85. echo "</div>";
  86. $result = new SearchResult();
  87. $result->images = $images;
  88. if (isset($data->resource_response->bookmark)) {
  89. $result->bookmark = $data->resource_response->bookmark;
  90. }
  91. return $result;
  92. };
  93. $result = $search($query, $bookmark);
  94. // Pagination link for the next page
  95. if ($result->bookmark !== null) {
  96. $query_encoded = urlencode($query);
  97. $bookmark_encoded = urlencode($result->bookmark);
  98. $csrftoken_encoded = $csrftoken ? urlencode($csrftoken) : "";
  99. 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>";
  100. }
  101. include "misc/footer.php";
  102. ?>