bingcache-todo-fix.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // https://www.bing.com/search?q=url%3Ahttps%3A%2F%2Flolcat.ca
  3. // https://cc.bingj.com/cache.aspx?q=url%3ahttps%3a%2f%2flolcat.ca&d=4769685974291356&mkt=en-CA&setlang=en-US&w=tEsWuE7HW3Z5AIPQMVkDH4WaotS4LrK-
  4. // <div class="b_attribution" u="0N|5119|4769685974291356|tEsWuE7HW3Z5AIPQMVkDH4WaotS4LrK-" tabindex="0">
  5. new bingcache();
  6. class bingcache{
  7. public function __construct(){
  8. if(
  9. !isset($_GET["s"]) ||
  10. $this->validate_url($_GET["s"]) === false
  11. ){
  12. var_dump($this->validate_url($_GET["s"]));
  13. $this->do404("Please provide a valid URL.");
  14. }
  15. $url = $_GET["s"];
  16. $curlproc = curl_init();
  17. curl_setopt(
  18. $curlproc,
  19. CURLOPT_URL,
  20. "https://www.bing.com/search?q=url%3A" .
  21. urlencode($url)
  22. );
  23. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  24. curl_setopt(
  25. $curlproc,
  26. CURLOPT_HTTPHEADER,
  27. ["User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0",
  28. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  29. "Accept-Language: en-US,en;q=0.5",
  30. "Accept-Encoding: gzip",
  31. "DNT: 1",
  32. "Connection: keep-alive",
  33. "Upgrade-Insecure-Requests: 1",
  34. "Sec-Fetch-Dest: document",
  35. "Sec-Fetch-Mode: navigate",
  36. "Sec-Fetch-Site: none",
  37. "Sec-Fetch-User: ?1"]
  38. );
  39. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  40. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  41. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  42. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 5);
  43. $data = curl_exec($curlproc);
  44. if(curl_errno($curlproc)){
  45. $this->do404("Failed to connect to bing servers. Please try again later.");
  46. }
  47. curl_close($curlproc);
  48. preg_match(
  49. '/<div class="b_attribution" u="(.*)" tabindex="0">/',
  50. $data,
  51. $keys
  52. );
  53. print_r($keys);
  54. if(count($keys) === 0){
  55. $this->do404("Bing has not archived this URL.");
  56. }
  57. $keys = explode("|", $keys[1]);
  58. $count = count($keys);
  59. //header("Location: https://cc.bingj.com/cache.aspx?d=" . $keys[$count - 2] . "&w=" . $keys[$count - 1]);
  60. echo("Location: https://cc.bingj.com/cache.aspx?d=" . $keys[$count - 2] . "&w=" . $keys[$count - 1]);
  61. }
  62. public function do404($text){
  63. include "lib/frontend.php";
  64. $frontend = new frontend();
  65. echo
  66. $frontend->load(
  67. "error.html",
  68. [
  69. "title" => "Shit",
  70. "text" => $text
  71. ]
  72. );
  73. die();
  74. }
  75. public function validate_url($url){
  76. $url_parts = parse_url($url);
  77. // check if required parts are there
  78. if(
  79. !isset($url_parts["scheme"]) ||
  80. !(
  81. $url_parts["scheme"] == "http" ||
  82. $url_parts["scheme"] == "https"
  83. ) ||
  84. !isset($url_parts["host"])
  85. ){
  86. return false;
  87. }
  88. if(
  89. // if its not an RFC-valid URL
  90. !filter_var($url, FILTER_VALIDATE_URL)
  91. ){
  92. return false;
  93. }
  94. $ip =
  95. str_replace(
  96. ["[", "]"], // handle ipv6
  97. "",
  98. $url_parts["host"]
  99. );
  100. // if its not an IP
  101. if(!filter_var($ip, FILTER_VALIDATE_IP)){
  102. // resolve domain's IP
  103. $ip = gethostbyname($url_parts["host"] . ".");
  104. }
  105. // check if its localhost
  106. return filter_var(
  107. $ip,
  108. FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
  109. );
  110. }
  111. }