search.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "x-pinterest-pws-handler: www/search/[scope].js"
  53. ];
  54. if ($csrftoken !== null) {
  55. $headers[] = "x-csrftoken: $csrftoken";
  56. $headers[] = "cookie: csrftoken=$csrftoken";
  57. }
  58. $finalurl = $bookmark === null ? "$url?data=$data_param" : $url;
  59. $ch = curl_init($finalurl);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  61. curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
  62. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  63. if ($bookmark !== null) {
  64. curl_setopt($ch, CURLOPT_POST, true);
  65. curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
  66. }
  67. return $ch;
  68. };
  69. // Function to perform the search and display results
  70. $search = function ($query, $bookmark) use ($prepare_search_curl_obj) {
  71. $ch = $prepare_search_curl_obj($query, $bookmark);
  72. $response = curl_exec($ch);
  73. $data = json_decode($response);
  74. $images = [];
  75. echo "<div class='img-container'>";
  76. if ($data && isset($data->resource_response->data->results)) {
  77. foreach ($data->resource_response->data->results as $result) {
  78. $image = $result->images->orig;
  79. $url = $image->url;
  80. $images[] = $url;
  81. echo "<a class='img-result' href='/image_proxy.php?url=" . htmlspecialchars($url) . "'>";
  82. echo "<img loading='lazy' src='/image_proxy.php?url=" . htmlspecialchars($url) . "'></a>";
  83. }
  84. } else {
  85. echo "<p>No results found.</p>";
  86. }
  87. echo "</div>";
  88. $result = new SearchResult();
  89. $result->images = $images;
  90. if (isset($data->resource_response->bookmark)) {
  91. $result->bookmark = $data->resource_response->bookmark;
  92. }
  93. return $result;
  94. };
  95. $result = $search($query, $bookmark);
  96. // Pagination link for the next page
  97. if ($result->bookmark !== null) {
  98. $query_encoded = urlencode($query);
  99. $bookmark_encoded = urlencode($result->bookmark);
  100. $csrftoken_encoded = $csrftoken ? urlencode($csrftoken) : "";
  101. 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>";
  102. }
  103. include "misc/footer.php";
  104. ?>