image.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. function get_image_results($query, $page)
  3. {
  4. global $config;
  5. // Randomly choose a instance
  6. $results = array();
  7. $num = array_rand($config->instances);
  8. $instance = $config->instances[$num];
  9. $url = "https://$instance/api.php";
  10. // API POST field
  11. $post_string = "q=$query&p=$page&t=1";
  12. // alternative frontends
  13. // fuck the cookie abuse in API
  14. if (!isset($_COOKIE["disable_frontends"]))
  15. {
  16. $frontends = array(
  17. "invidious"=>null,
  18. "rimgo"=>null,
  19. "scribe"=>null,
  20. "gothub"=>null,
  21. "librarian"=>null,
  22. "nitter"=>null,
  23. "libreddit"=>null,
  24. "proxitok"=>null,
  25. "wikiless"=>null,
  26. "quetre"=>null,
  27. "libremdb"=>null,
  28. "breezewiki"=>null,
  29. "anonymousoverflow"=>null,
  30. "suds"=>null,
  31. "biblioreads"=>null
  32. );
  33. foreach ($frontends as $i => $value) {
  34. if (isset($_COOKIE[$i]))
  35. $frontends[$i] = $_COOKIE[$i];
  36. else if (!empty($config->frontends[$i]["instance_url"]))
  37. $frontends[$i] = $config->frontends[$i]["instance_url"];
  38. }
  39. $cookies=array_merge($cookies, $frontends);
  40. }
  41. else
  42. $cookies=array_merge($cookies, array("disable_frontends"=>$_COOKIE["disable_frontends"]));
  43. $api_ch = curl_init($url);
  44. curl_setopt_array($api_ch, $config->curl_settings);
  45. curl_setopt($api_ch, CURLOPT_CUSTOMREQUEST, "POST");
  46. curl_setopt($api_ch, CURLOPT_COOKIE, http_build_query($cookies, '', ';'));
  47. curl_setopt($api_ch, CURLOPT_POSTFIELDS, $post_string);
  48. $results = json_decode(curl_exec($api_ch),true);
  49. if($results == null) $results = "$instance is broken";
  50. return $results;
  51. }
  52. function print_image_results($results)
  53. {
  54. echo "<div class=\"image-result-container\">";
  55. foreach($results as $result)
  56. {
  57. $thumbnail = urlencode($result["thumbnail"]);
  58. $alt = $result["alt"];
  59. $url = $result["url"];
  60. echo "<a title=\"$alt\" href=\"$url\" target=\"_blank\">";
  61. echo "<img src=\"image_proxy.php?url=$thumbnail\">";
  62. echo "</a>";
  63. }
  64. echo "</div>";
  65. }
  66. ?>