search.php 3.9 KB

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