search.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php require "misc/header.php"; ?>
  2. <title>
  3. <?php
  4. $query = htmlspecialchars(trim($_REQUEST["q"]));
  5. echo $query;
  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. $query_encoded = urlencode($query);
  14. if (1 > strlen($query) || strlen($query) > 64) {
  15. header("Location: ./");
  16. die();
  17. }
  18. echo "value=\"$query\"";
  19. ?>
  20. >
  21. <!-- <div></div> -->
  22. </form>
  23. <?php
  24. $query = $_GET["q"];
  25. $bookmark = null;
  26. if (array_key_exists("bookmark", $_GET)) {
  27. $bookmark = urldecode($_GET["bookmark"]);
  28. }
  29. $csrftoken = null;
  30. if (array_key_exists("csrftoken", $_GET)) {
  31. $csrftoken = $_GET["csrftoken"];
  32. }
  33. $url = "https://www.pinterest.com/resource/BaseSearchResource/get/";
  34. class SearchResult
  35. {
  36. public $images;
  37. public $bookmark;
  38. }
  39. $header_function = function ($ch, $rawheader) {
  40. global $csrftoken;
  41. $len = strlen($rawheader);
  42. $header = explode(":", $rawheader, 2);
  43. if (count($header) != 2) {
  44. return $len;
  45. }
  46. // we are only interested in set-cookie header
  47. if (trim($header[0]) != "set-cookie") {
  48. return $len;
  49. }
  50. $cookie = explode(";", trim($header[1]), 2);
  51. $cookie = explode("=", $cookie[0], 2);
  52. switch ($cookie[0]) {
  53. case "csrftoken":
  54. $csrftoken = $cookie[1];
  55. }
  56. return $len;
  57. };
  58. $prepare_search_curl_obj = function ($query, $bookmark) use (
  59. $url,
  60. $header_function,
  61. $csrftoken
  62. ) {
  63. $data_param_obj = [
  64. "options" => [
  65. "query" => $query,
  66. ],
  67. ];
  68. if ($bookmark != null) {
  69. $data_param_obj["options"]["bookmarks"] = [$bookmark];
  70. }
  71. $data_param = urlencode(json_encode($data_param_obj));
  72. $headers = [];
  73. if ($csrftoken != null) {
  74. $headers[] = "x-csrftoken: $csrftoken";
  75. $headers[] = "cookie: csrftoken=$csrftoken";
  76. }
  77. $finalurl = $url;
  78. if ($bookmark == null) {
  79. $finalurl = "$url?data=$data_param";
  80. }
  81. $ch = curl_init($finalurl);
  82. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  83. curl_setopt($ch, CURLOPT_HEADERFUNCTION, $header_function);
  84. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  85. if ($bookmark != null) {
  86. curl_setopt($ch, CURLOPT_POST, true);
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data_param");
  88. }
  89. return $ch;
  90. };
  91. $search = function ($query, $bookmark) use ($prepare_search_curl_obj) {
  92. $ch = $prepare_search_curl_obj($query, $bookmark);
  93. $response = curl_exec($ch);
  94. $data = json_decode($response);
  95. $images = [];
  96. echo "<div class=img-container>";
  97. if (
  98. $data &&
  99. property_exists($data, "resource_response") &&
  100. property_exists($data->{"resource_response"}, "data") &&
  101. property_exists($data->{"resource_response"}->{"data"}, "results")
  102. ) {
  103. foreach (
  104. $data->{"resource_response"}->{"data"}->{"results"}
  105. as $result
  106. ) {
  107. $image = $result->{"images"}->{"orig"};
  108. $url = $image->{"url"};
  109. array_push($images, $url);
  110. echo "<a class=img-result href='/image_proxy.php?url=", $url, "'>";
  111. echo "<img loading='lazy' src='/image_proxy.php?url=",
  112. $url,
  113. "'></a>";
  114. }
  115. } else {
  116. echo "<p>No results found.</p>";
  117. }
  118. echo "</div>";
  119. $result = new SearchResult();
  120. $result->images = $images;
  121. if (
  122. $data &&
  123. property_exists($data, "resource_response") &&
  124. property_exists($data->{"resource_response"}, "bookmark")
  125. ) {
  126. $result->bookmark = $data->{"resource_response"}->{"bookmark"};
  127. }
  128. return $result;
  129. };
  130. $result = $search($query, $bookmark);
  131. if ($result->bookmark != null) {
  132. $query_encoded = urlencode($query);
  133. $bookmark_encoded = urlencode($result->bookmark);
  134. $csrftoken_encoded = $csrftoken ? urlencode($csrftoken) : "";
  135. 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>";
  136. }
  137. include "misc/footer.php";
  138. ?>