ghostery.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. class ghostery{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("ghostery");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. public function getfilters($page){
  10. if($page != "web"){
  11. return [];
  12. }
  13. return [
  14. "country" => [
  15. "display" => "Country",
  16. "option" => [
  17. "any" => "All regions",
  18. "AR" => "Argentina",
  19. "AU" => "Australia",
  20. "AT" => "Austria",
  21. "BE" => "Belgium",
  22. "BR" => "Brazil",
  23. "CA" => "Canada",
  24. "CL" => "Chile",
  25. "DK" => "Denmark",
  26. "FI" => "Finland",
  27. "FR" => "France",
  28. "DE" => "Germany",
  29. "HK" => "Hong Kong",
  30. "IN" => "India",
  31. "ID" => "Indonesia",
  32. "IT" => "Italy",
  33. "JP" => "Japan",
  34. "KR" => "Korea",
  35. "MY" => "Malaysia",
  36. "MX" => "Mexico",
  37. "NL" => "Netherlands",
  38. "NZ" => "New Zealand",
  39. "NO" => "Norway",
  40. "CN" => "People's Republic of China",
  41. "PL" => "Poland",
  42. "PT" => "Portugal",
  43. "PH" => "Republic of the Philippines",
  44. "RU" => "Russia",
  45. "SA" => "Saudi Arabia",
  46. "ZA" => "South Africa",
  47. "ES" => "Spain",
  48. "SE" => "Sweden",
  49. "CH" => "Switzerland",
  50. "TW" => "Taiwan",
  51. "TR" => "Turkey",
  52. "GB" => "United Kingdom",
  53. "US" => "United States"
  54. ]
  55. ]
  56. ];
  57. }
  58. private function get($proxy, $url, $get = [], $country){
  59. $curlproc = curl_init();
  60. if($get !== []){
  61. $get = http_build_query($get);
  62. $url .= "?" . $get;
  63. }
  64. curl_setopt($curlproc, CURLOPT_URL, $url);
  65. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  66. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  67. ["User-Agent: " . config::USER_AGENT,
  68. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  69. "Accept-Language: en-US,en;q=0.5",
  70. "Accept-Encoding: gzip",
  71. "Referer: https://ghosterysearch.com",
  72. "DNT: 1",
  73. "Sec-GPC: 1",
  74. "Connection: keep-alive",
  75. "Cookie: ctry=" . ($country == "any" ? "--" : $country) . "; noads=true",
  76. "Upgrade-Insecure-Requests: 1",
  77. "Sec-Fetch-Dest: document",
  78. "Sec-Fetch-Mode: navigate",
  79. "Sec-Fetch-Site: same-origin",
  80. "Sec-Fetch-User: ?1",
  81. "Priority: u=0, i"]
  82. );
  83. // http2 bypass
  84. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  85. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  86. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  87. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  88. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  89. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  90. $this->backend->assign_proxy($curlproc, $proxy);
  91. $data = curl_exec($curlproc);
  92. if(curl_errno($curlproc)){
  93. throw new Exception(curl_error($curlproc));
  94. }
  95. curl_close($curlproc);
  96. return $data;
  97. }
  98. public function web($get){
  99. if($get["npt"]){
  100. [$query, $proxy] = $this->backend->get($get["npt"], "web");
  101. parse_str($query, $query);
  102. // country
  103. $country = $query["c"];
  104. unset($query["c"]);
  105. $query = http_build_query($query);
  106. $html =
  107. $this->get(
  108. $proxy,
  109. "https://ghosterysearch.com/search?" . $query,
  110. [],
  111. $country
  112. );
  113. }else{
  114. $proxy = $this->backend->get_ip();
  115. $html =
  116. $this->get(
  117. $proxy,
  118. "https://ghosterysearch.com/search",
  119. [
  120. "q" => $get["s"]
  121. ],
  122. $get["country"]
  123. );
  124. }
  125. $out = [
  126. "status" => "ok",
  127. "spelling" => [
  128. "type" => "no_correction",
  129. "using" => null,
  130. "correction" => null
  131. ],
  132. "npt" => null,
  133. "answer" => [],
  134. "web" => [],
  135. "image" => [],
  136. "video" => [],
  137. "news" => [],
  138. "related" => []
  139. ];
  140. $this->fuckhtml->load($html);
  141. $results_wrapper =
  142. $this->fuckhtml
  143. ->getElementsByClassName(
  144. "results",
  145. "section"
  146. );
  147. if(count($results_wrapper) === 0){
  148. throw new Exception("Failed to grep result section");
  149. }
  150. $this->fuckhtml->load($results_wrapper[0]);
  151. // get search results
  152. $results =
  153. $this->fuckhtml
  154. ->getElementsByClassName(
  155. "result",
  156. "li"
  157. );
  158. if(count($results) === 0){
  159. return $out;
  160. }
  161. foreach($results as $result){
  162. $this->fuckhtml->load($result);
  163. $a =
  164. $this->fuckhtml
  165. ->getElementsByClassName(
  166. "url",
  167. "a"
  168. );
  169. if(count($a) === 0){
  170. continue;
  171. }
  172. $a = $a[0];
  173. $out["web"][] = [
  174. "title" =>
  175. $this->titledots(
  176. $this->fuckhtml
  177. ->getTextContent(
  178. $this->fuckhtml
  179. ->getElementsByTagName(
  180. "h2"
  181. )[0]
  182. )
  183. ),
  184. "description" =>
  185. $this->titledots(
  186. $this->fuckhtml
  187. ->getTextContent(
  188. $this->fuckhtml
  189. ->getElementsByTagName(
  190. "p"
  191. )[0]
  192. )
  193. ),
  194. "url" =>
  195. $this->fuckhtml
  196. ->getTextContent(
  197. $a
  198. ["attributes"]
  199. ["href"]
  200. ),
  201. "date" => null,
  202. "type" => "web",
  203. "thumb" => [
  204. "url" => null,
  205. "ratio" => null
  206. ],
  207. "sublink" => [],
  208. "table" => []
  209. ];
  210. }
  211. $this->fuckhtml->load($html);
  212. // get pagination token
  213. $pagination_wrapper =
  214. $this->fuckhtml
  215. ->getElementsByClassName(
  216. "pagination",
  217. "div"
  218. );
  219. if(count($pagination_wrapper) !== 0){
  220. // found next page!
  221. $this->fuckhtml->load($pagination_wrapper[0]);
  222. $a =
  223. $this->fuckhtml
  224. ->getElementsByTagName(
  225. "a"
  226. );
  227. if(count($a) !== 0){
  228. $q =
  229. parse_url(
  230. $this->fuckhtml
  231. ->getTextContent(
  232. $a[count($a) - 1]
  233. ["attributes"]
  234. ["href"]
  235. ),
  236. PHP_URL_QUERY
  237. );
  238. $out["npt"] =
  239. $this->backend
  240. ->store(
  241. $q . "&c=" . $get["country"],
  242. "web",
  243. $proxy
  244. );
  245. }
  246. }
  247. return $out;
  248. }
  249. private function titledots($title){
  250. return trim($title, " .\t\n\r\0\x0B…");
  251. }
  252. }