wiby.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. class wiby{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("wiby");
  6. }
  7. public function getfilters($page){
  8. if($page != "web"){
  9. return [];
  10. }
  11. return [
  12. "nsfw" => [
  13. "display" => "NSFW",
  14. "option" => [
  15. "yes" => "Yes",
  16. "no" => "No"
  17. ]
  18. ],
  19. "date" => [
  20. "display" => "Time posted",
  21. "option" => [
  22. "any" => "Any time",
  23. "day" => "Past day",
  24. "week" => "Past week",
  25. "month" => "Past month",
  26. "year" => "Past year",
  27. ]
  28. ]
  29. ];
  30. }
  31. private function get($proxy, $url, $get = [], $nsfw){
  32. $curlproc = curl_init();
  33. if($get !== []){
  34. $get = http_build_query($get);
  35. $url .= "?" . $get;
  36. }
  37. print_r([$proxy, $url]);
  38. curl_setopt($curlproc, CURLOPT_URL, $url);
  39. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  40. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  41. ["User-Agent: " . config::USER_AGENT,
  42. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  43. "Accept-Language: en-US,en;q=0.5",
  44. "Accept-Encoding: gzip",
  45. "Cookie: ws={$nsfw}",
  46. "DNT: 1",
  47. "Connection: keep-alive",
  48. "Upgrade-Insecure-Requests: 1",
  49. "Sec-Fetch-Dest: document",
  50. "Sec-Fetch-Mode: navigate",
  51. "Sec-Fetch-Site: none",
  52. "Sec-Fetch-User: ?1"]
  53. );
  54. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  55. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  56. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  57. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  58. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  59. $this->backend->assign_proxy($curlproc, $proxy);
  60. $data = curl_exec($curlproc);
  61. if(curl_errno($curlproc)){
  62. throw new Exception(curl_error($curlproc));
  63. }
  64. curl_close($curlproc);
  65. return $data;
  66. }
  67. public function web($get){
  68. if($get["npt"]){
  69. [$q, $proxy] = $this->backend->get($get["npt"], "web");
  70. $q = json_decode($q, true);
  71. $nsfw = $q["nsfw"];
  72. unset($q["nsfw"]);
  73. }else{
  74. $search = $get["s"];
  75. if(strlen($search) === 0){
  76. throw new Exception("Search term is empty!");
  77. }
  78. $proxy = $this->backend->get_ip();
  79. $date = $get["date"];
  80. $nsfw = $get["nsfw"] == "yes" ? "0" : "1";
  81. $search =
  82. str_replace(
  83. [
  84. "!g",
  85. "!gi",
  86. "!gv",
  87. "!gm",
  88. "!b",
  89. "!bi",
  90. "!bv",
  91. "!bm",
  92. "!td",
  93. "!tw",
  94. "!tm",
  95. "!ty",
  96. "&g",
  97. "&gi",
  98. "&gv",
  99. "&gm",
  100. "&b",
  101. "&bi",
  102. "&bv",
  103. "&bm",
  104. "&td",
  105. "&tw",
  106. "&tm",
  107. "&ty",
  108. ],
  109. "",
  110. $search
  111. );
  112. switch($date){
  113. case "day": $search = "!td " . $search; break;
  114. case "week": $search = "!tw " . $search; break;
  115. case "month": $search = "!tm " . $search; break;
  116. case "year": $search = "!ty " . $search; break;
  117. }
  118. $q = [
  119. "q" => $search
  120. ];
  121. }
  122. try{
  123. $html = $this->get(
  124. $proxy,
  125. "https://wiby.me/",
  126. $q,
  127. $nsfw
  128. );
  129. }catch(Exception $error){
  130. throw new Exception("Failed to fetch search page");
  131. }
  132. preg_match(
  133. '/<p class="pin"><blockquote>(?:<\/p>)?<br><a class="more" href="\/\?q=[^"]+&p=([0-9]+)">Find more\.\.\.<\/a><\/blockquote>/',
  134. $html,
  135. $nextpage
  136. );
  137. if(count($nextpage) === 0){
  138. $nextpage = null;
  139. }else{
  140. $nextpage =
  141. $this->backend->store(
  142. json_encode([
  143. "q" => $q["q"],
  144. "p" => (int)$nextpage[1],
  145. "nsfw" => $nsfw
  146. ]),
  147. "web",
  148. $proxy
  149. );
  150. }
  151. $out = [
  152. "status" => "ok",
  153. "spelling" => [
  154. "type" => "no_correction",
  155. "using" => null,
  156. "correction" => null
  157. ],
  158. "npt" => $nextpage,
  159. "answer" => [],
  160. "web" => [],
  161. "image" => [],
  162. "video" => [],
  163. "news" => [],
  164. "related" => []
  165. ];
  166. preg_match_all(
  167. '/<blockquote>[\s]*<a .* href="(.*)">(.*)<\/a>.*<p>(.*)<\/p>[\s]*<\/blockquote>/Ui',
  168. $html,
  169. $links
  170. );
  171. for($i=0; $i<count($links[0]); $i++){
  172. $out["web"][] = [
  173. "title" => $this->unescapehtml(trim($links[2][$i])),
  174. "description" => $this->unescapehtml(trim(strip_tags($links[3][$i]))),
  175. "url" => trim($links[1][$i]),
  176. "date" => null,
  177. "type" => "web",
  178. "thumb" => [
  179. "url" => null,
  180. "ratio" => null
  181. ],
  182. "sublink" => [],
  183. "table" => []
  184. ];
  185. }
  186. return $out;
  187. }
  188. private function unescapehtml($str){
  189. return html_entity_decode(
  190. str_replace(
  191. [
  192. "<br>",
  193. "<br/>",
  194. "</br>",
  195. "<BR>",
  196. "<BR/>",
  197. "</BR>",
  198. ],
  199. "\n",
  200. $str
  201. ),
  202. ENT_QUOTES | ENT_XML1, 'UTF-8'
  203. );
  204. }
  205. }