startpage.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. <?php
  2. class startpage{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("startpage");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. public function getfilters($page){
  10. switch($page){
  11. case "web":
  12. return [
  13. "country" => [
  14. "display" => "Country",
  15. "option" => [
  16. "any" => "All Regions",
  17. "es_AR" => "Argentina",
  18. "en_AU" => "Australia",
  19. "de_AT" => "Austria",
  20. "ru_BY" => "Belarus",
  21. "fr_BE" => "Belgium (FR)",
  22. "nl_BE" => "Belgium (NL)",
  23. "bg_BG" => "Bulgaria",
  24. "en_CA" => "Canada (EN)",
  25. "fr_CA" => "Canada (FR)",
  26. "es_CL" => "Chile",
  27. "es_CO" => "Colombia",
  28. "cs_CZ" => "Czech Republic",
  29. "da_DK" => "Denmark",
  30. "ar_EG" => "Egypt",
  31. "et_EE" => "Estonia",
  32. "fi_FI" => "Finland",
  33. "fr_FR" => "France",
  34. "de_DE" => "Germany",
  35. "el_GR" => "Greece",
  36. "hu_HU" => "Hungary",
  37. "hi_IN" => "India (HI)",
  38. "en_IN" => "India (EN)",
  39. "id_ID" => "Indonesia (ID)",
  40. "en_ID" => "Indonesia (EN)",
  41. "en_IE" => "Ireland",
  42. "it_IT" => "Italy",
  43. "ja_JP" => "Japan",
  44. "ko_KR" => "Korea",
  45. "ms_MY" => "Malaysia (MS)",
  46. "en_MY" => "Malaysia (EN)",
  47. "es_MX" => "Mexico",
  48. "nl_NL" => "Netherlands",
  49. "en_NZ" => "New Zealand",
  50. "no_NO" => "Norway",
  51. "es_PE" => "Peru",
  52. "fil_PH" => "Philippines (FIL)",
  53. "en_PH" => "Philippines (EN)",
  54. "pl_PL" => "Poland",
  55. "pt_PT" => "Portugal",
  56. "ro_RO" => "Romania",
  57. "ru_RU" => "Russia",
  58. "ms_SG" => "Singapore (MS)",
  59. "en_SG" => "Singapore (EN)",
  60. "es_ES" => "Spain (ES)",
  61. "ca_ES" => "Spain (CA)",
  62. "sv_SE" => "Sweden",
  63. "de_CH" => "Switzerland (DE)",
  64. "fr_CH" => "Switzerland (FR)",
  65. "it_CH" => "Switzerland (IT)",
  66. "tr_TR" => "Turkey",
  67. "uk_UA" => "Ukraine",
  68. "en_US" => "US (EN)",
  69. "es_US" => "US (ES)",
  70. "es_UY" => "Uruguay",
  71. "es_VE" => "Venezuela",
  72. "vi_VN" => "Vietnam (VI)",
  73. "en_VN" => "Vietnam (EN)",
  74. "en_ZA" => "South Africa"
  75. ]
  76. ],
  77. "nsfw" => [ // qadf
  78. "display" => "NSFW",
  79. "option" => [
  80. "yes" => "Yes", // qadf=none
  81. "no" => "No" // qadf=heavy
  82. ]
  83. ],
  84. "time" => [ // with_date
  85. "display" => "Time posted",
  86. "option" => [
  87. "any" => "Any time",
  88. "d" => "Past 24 hours",
  89. "w" => "Past week",
  90. "m" => "Past month",
  91. "y" => "Past year",
  92. ]
  93. ],
  94. "extendedsearch" => [
  95. // undefined display, so it wont show in frontend
  96. "option" => [
  97. "yes" => "Yes",
  98. "no" => "No"
  99. ]
  100. ]
  101. ];
  102. break;
  103. case "images":
  104. return [
  105. "nsfw" => [ // qadf
  106. "display" => "NSFW",
  107. "option" => [
  108. "yes" => "Yes", // qadf=none
  109. "no" => "No" // qadf=heavy
  110. ]
  111. ],
  112. "size" => [ // flimgsize
  113. "display" => "Size",
  114. "option" => [
  115. "any" => "Any size",
  116. "Small" => "Small",
  117. "Medium" => "Medium",
  118. "Large" => "Large",
  119. "Wallpaper" => "Wallpaper",
  120. // from here, image-size-select, var prefix = isz:lt,islt:
  121. "qsvgs" => "Larger than 400x300",
  122. "vga" => "Larger than 640x480",
  123. "svga" => "Larger than 800x600",
  124. "xga" => "Larger than 1024x768",
  125. "qsvgs" => "Larger than 400x300",
  126. "2mp" => "Larger than 2 MP (1600x1200)",
  127. "4mp" => "Larger than 4 MP (2272x1704)",
  128. "6mp" => "Larger than 6 MP (2816x2112)",
  129. "8mp" => "Larger than 8 MP (3264x2448)",
  130. "10mp" => "Larger than 10 MP (3648x2736)",
  131. "12mp" => "Larger than 12 MP (4096x3072)",
  132. "15mp" => "Larger than 15 MP (4480x3360)",
  133. "20mp" => "Larger than 20 MP (5120x3840)",
  134. "40mp" => "Larger than 40 MP (7216x5412)",
  135. "70mp" => "Larger than 70 MP (9600x7200)"
  136. ]
  137. ],
  138. "color" => [ // flimgcolor
  139. "display" => "Color",
  140. "option" => [
  141. "any" => "Any color",
  142. // from here, var prefix = ic:
  143. "color" => "Color only",
  144. "bnw" => "Black & white", // set to "gray"
  145. // from here, var prefix = ic:specific,isc:
  146. "red" => "Red",
  147. "orange" => "Orange",
  148. "yellow" => "Yellow",
  149. "green" => "Green",
  150. "teal" => "Teal",
  151. "blue" => "Blue",
  152. "purple" => "Purple",
  153. "pink" => "Pink",
  154. "white" => "White",
  155. "gray" => "Gray",
  156. "black" => "Black",
  157. "brown" => "Brown"
  158. ]
  159. ],
  160. "type" => [ // flimgtype
  161. "display" => "Type",
  162. "option" => [
  163. "any" => "Any type",
  164. "AnimatedGif" => "Animated GIF",
  165. "Clipart" => "Clip Art",
  166. "Line" => "Line Drawing",
  167. "Photo" => "Photograph",
  168. "Transparent" => "Transparent Background"
  169. ]
  170. ],
  171. "license" => [ // flimglicense
  172. "display" => "License",
  173. "option" => [
  174. "any" => "Any license",
  175. "p" => "Public domain",
  176. "s" => "Free to share",
  177. "sc" => "Free to share commercially",
  178. "m" => "Free to modify",
  179. "mc" => "Free to modify commercially"
  180. ]
  181. ]
  182. ];
  183. break;
  184. case "videos":
  185. return [
  186. "nsfw" => [ // qadf
  187. "display" => "NSFW",
  188. "option" => [
  189. "yes" => "Yes", // qadf=none
  190. "no" => "No" // qadf=heavy
  191. ]
  192. ],
  193. "sort" => [
  194. "display" => "Sort by",
  195. "option" => [
  196. "relevance" => "Most relevant",
  197. "popular" => "Most popular",
  198. "recent" => "Most recent"
  199. ]
  200. ],
  201. "duration" => [ // with_duration
  202. "display" => "Duration",
  203. "option" => [
  204. "any" => "Any duration",
  205. "short" => "Short",
  206. "medium" => "Medium",
  207. "long" => "Long"
  208. ]
  209. ]
  210. ];
  211. break;
  212. case "news":
  213. return [
  214. "nsfw" => [ // qadf
  215. "display" => "NSFW",
  216. "option" => [
  217. "yes" => "Yes", // qadf=none
  218. "no" => "No" // qadf=heavy
  219. ]
  220. ],
  221. "time" => [ // with_date
  222. "display" => "Time posted",
  223. "option" => [
  224. "any" => "Any time",
  225. "d" => "Past 24 hours",
  226. "w" => "Past week",
  227. "m" => "Past month"
  228. ]
  229. ]
  230. ];
  231. break;
  232. //preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEazerbaijaniN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius; Domain=startpage.com; Expires=Mon, 28 Oct 2024 20:21:58 GMT; Secure; Path=/
  233. //preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius; Domain=startpage.com; Expires=Mon, 28 Oct 2024 20:22:52 GMT; Secure; Path=/
  234. }
  235. }
  236. private function get($proxy, $url, $get = [], $post = false, $is_xhr = false){
  237. $curlproc = curl_init();
  238. if($post === true){
  239. curl_setopt($curlproc, CURLOPT_POST, true);
  240. curl_setopt($curlproc, CURLOPT_POSTFIELDS, $get);
  241. }elseif($get !== []){
  242. $get = http_build_query($get);
  243. $url .= "?" . $get;
  244. }
  245. curl_setopt($curlproc, CURLOPT_URL, $url);
  246. // http2 bypass
  247. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  248. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  249. if($is_xhr === true){
  250. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  251. ["User-Agent: " . config::USER_AGENT,
  252. "Accept: application/json",
  253. "Accept-Language: en-US,en;q=0.5",
  254. "Accept-Encoding: gzip",
  255. "Referer: https://www.startpage.com/",
  256. "Content-Type: application/json",
  257. "Content-Length: " . strlen($get),
  258. "Origin: https://www.startpage.com/",
  259. "DNT: 1",
  260. "Connection: keep-alive",
  261. "Cookie: preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius",
  262. "Sec-Fetch-Dest: empty",
  263. "Sec-Fetch-Mode: cors",
  264. "Sec-Fetch-Site: same-origin",
  265. "TE: trailers"]
  266. );
  267. }elseif($post === true){
  268. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  269. ["User-Agent: " . config::USER_AGENT,
  270. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  271. "Accept-Language: en-US,en;q=0.5",
  272. "Accept-Encoding: gzip",
  273. "Referer: https://www.startpage.com/",
  274. "Content-Type: application/x-www-form-urlencoded",
  275. "Content-Length: " . strlen($get),
  276. "DNT: 1",
  277. "Connection: keep-alive",
  278. "Cookie: preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius",
  279. "Upgrade-Insecure-Requests: 1",
  280. "Sec-Fetch-Dest: document",
  281. "Sec-Fetch-Mode: navigate",
  282. "Sec-Fetch-Site: none",
  283. "Sec-Fetch-User: ?1",
  284. "Priority: u=0, i",
  285. "TE: trailers"]
  286. );
  287. }else{
  288. curl_setopt($curlproc, CURLOPT_HTTPHEADER,
  289. ["User-Agent: " . config::USER_AGENT,
  290. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  291. "Accept-Language: en-US,en;q=0.5",
  292. "Accept-Encoding: gzip",
  293. "DNT: 1",
  294. "Connection: keep-alive",
  295. "Cookie: preferences=date_timeEEEworldN1Ndisable_family_filterEEE1N1Ndisable_open_in_new_windowEEE0N1Nenable_post_methodEEE1N1Nenable_proxy_safety_suggestEEE0N1Nenable_stay_controlEEE0N1Ninstant_answersEEE1N1Nlang_homepageEEEs%2Fdevice%2FenN1NlanguageEEEenglishN1Nlanguage_uiEEEenglishN1Nnum_of_resultsEEE20N1Nsearch_results_regionEEEallN1NsuggestionsEEE1N1Nwt_unitEEEcelsius",
  296. "Sec-Fetch-Dest: document",
  297. "Sec-Fetch-Mode: navigate",
  298. "Sec-Fetch-Site: none",
  299. "Sec-Fetch-User: ?1",
  300. "Priority: u=0, i",
  301. "TE: trailers"]
  302. );
  303. }
  304. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  305. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  306. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  307. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  308. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  309. $this->backend->assign_proxy($curlproc, $proxy);
  310. $data = curl_exec($curlproc);
  311. if(curl_errno($curlproc)){
  312. throw new Exception(curl_error($curlproc));
  313. }
  314. curl_close($curlproc);
  315. return $data;
  316. }
  317. public function web($get){
  318. if($get["npt"]){
  319. [$post, $proxy] = $this->backend->get($get["npt"], "web");
  320. try{
  321. $html = $this->get(
  322. $proxy,
  323. "https://www.startpage.com/sp/search",
  324. $post,
  325. true
  326. );
  327. }catch(Exception $error){
  328. throw new Exception("Failed to fetch search page");
  329. }
  330. $get_instant_answer = false;
  331. }else{
  332. $proxy = $this->backend->get_ip();
  333. $params = [
  334. "query" => $get["s"],
  335. "cat" => "web",
  336. "pl" => "opensearch"
  337. ];
  338. if($get["nsfw"] == "no"){
  339. $params["qadf"] = "heavy";
  340. $get_instant_answer = false;
  341. }else{
  342. $get_instant_answer = true;
  343. }
  344. if($get["country"] !== "any"){
  345. $params["qsr"] = $get["country"];
  346. }
  347. if($get["time"] !== "any"){
  348. $params["with_date"] = $get["time"];
  349. }
  350. try{
  351. $html = $this->get(
  352. $proxy,
  353. "https://www.startpage.com/sp/search",
  354. $params
  355. );
  356. }catch(Exception $error){
  357. throw new Exception("Failed to fetch search page");
  358. }
  359. //$html = file_get_contents("scraper/startpage.html");
  360. }
  361. if(
  362. preg_match(
  363. '/React\.createElement\(UIStartpage\.AppSerpWeb, ?(.+)\),$/m',
  364. $html,
  365. $matches
  366. ) === 0
  367. ){
  368. throw new Exception("Failed to grep JSON object");
  369. }
  370. $json = json_decode($matches[1], true);
  371. if($json === null){
  372. throw new Exception("Failed to decode JSON");
  373. }
  374. //print_r($json);
  375. $out = [
  376. "status" => "ok",
  377. "spelling" => [
  378. "type" => "no_correction",
  379. "using" => null,
  380. "correction" => null
  381. ],
  382. "npt" => null,
  383. "answer" => [],
  384. "web" => [],
  385. "image" => [],
  386. "video" => [],
  387. "news" => [],
  388. "related" => []
  389. ];
  390. // get npt
  391. $out["npt"] = $this->parse_npt($json, "web", $proxy);
  392. foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
  393. if(!isset($category["display_type"])){
  394. continue;
  395. }
  396. switch($category["display_type"]){
  397. case "web-google":
  398. foreach($category["results"] as $result){
  399. $sublinks = [];
  400. foreach($result["siteLinks"] as $sublink){
  401. $sublinks[] = [
  402. "title" => $sublink["title"],
  403. "description" => null,
  404. "url" => $sublink["clickUrl"]
  405. ];
  406. }
  407. $description =
  408. explode(
  409. "...",
  410. $this->titledots(
  411. html_entity_decode(
  412. $this->fuckhtml
  413. ->getTextContent(
  414. $result["description"]
  415. )
  416. )
  417. ),
  418. 2
  419. );
  420. $date = strtotime(trim($description[0]));
  421. if(
  422. $date === false ||
  423. count($description) !== 2 ||
  424. strlen($description[0]) > 14
  425. ){
  426. // no date found
  427. $description =
  428. implode(
  429. " ... ",
  430. $description
  431. );
  432. $date = null;
  433. }else{
  434. // date found
  435. $description = ltrim($description[1]);
  436. }
  437. $out["web"][] = [
  438. "title" =>
  439. $this->titledots(
  440. html_entity_decode(
  441. $this->fuckhtml
  442. ->getTextContent(
  443. $result["title"]
  444. )
  445. )
  446. ),
  447. "description" => $description,
  448. "url" => $result["clickUrl"],
  449. "date" => $date,
  450. "type" => "web",
  451. "thumb" => [
  452. "url" => null,
  453. "ratio" => null
  454. ],
  455. "sublink" => $sublinks,
  456. "table" => []
  457. ];
  458. }
  459. break;
  460. case "images-qi-top":
  461. foreach($category["results"] as $result){
  462. $out["image"][] = [
  463. "title" =>
  464. $this->titledots(
  465. html_entity_decode(
  466. $this->fuckhtml
  467. ->getTextContent(
  468. $result["title"]
  469. )
  470. )
  471. ),
  472. "source" => [
  473. [
  474. "url" => $result["rawImageUrl"],
  475. "width" => (int)$result["width"],
  476. "height" => (int)$result["height"]
  477. ],
  478. [
  479. "url" => $this->unshitimage($result["mdThumbnailUrl"]),
  480. "width" => (int)$result["mdThumbnailWidth"],
  481. "height" => (int)$result["mdThumbnailHeight"]
  482. ]
  483. ],
  484. "url" =>
  485. $result["altClickUrl"]
  486. ];
  487. }
  488. break;
  489. case "spellsuggest-google":
  490. $out["spelling"] =
  491. [
  492. "type" => "including",
  493. "using" => $json["render"]["query"],
  494. "correction" => $category["results"][0]["query"]
  495. ];
  496. break;
  497. case "dictionary-qi":
  498. foreach($category["results"] as $result){
  499. $answer = [
  500. "title" => $result["word"],
  501. "description" => [],
  502. "url" => null,
  503. "thumb" => null,
  504. "table" => [],
  505. "sublink" => []
  506. ];
  507. foreach($result["lexical_categories"] as $lexic_type => $definitions){
  508. $answer["description"][] = [
  509. "type" => "title",
  510. "value" => $lexic_type
  511. ];
  512. $i = 0;
  513. foreach($definitions as $definition){
  514. $text_definition = trim($definition["definition"]);
  515. $text_example = trim($definition["example"]);
  516. $text_synonyms = implode(", ", $definition["synonyms"]);
  517. if($text_definition != ""){
  518. $i++;
  519. $c = count($answer["description"]) - 1;
  520. if(
  521. $c !== 0 &&
  522. $answer["description"][$c]["type"] == "text"
  523. ){
  524. $answer["description"][$c]["value"] .=
  525. "\n\n" . $i . ". " . $text_definition;
  526. }else{
  527. $answer["description"][] = [
  528. "type" => "text",
  529. "value" => $i . ". " . $text_definition
  530. ];
  531. }
  532. }
  533. if($text_example != ""){
  534. $answer["description"][] = [
  535. "type" => "quote",
  536. "value" => $text_example
  537. ];
  538. }
  539. if($text_synonyms != ""){
  540. $answer["description"][] = [
  541. "type" => "text",
  542. "value" => "Synonyms: " . $text_synonyms
  543. ];
  544. }
  545. }
  546. }
  547. $out["answer"][] = $answer;
  548. }
  549. break;
  550. }
  551. }
  552. // parse instant answers
  553. if(
  554. $get["extendedsearch"] == "yes" &&
  555. $get_instant_answer === true
  556. ){
  557. // https://www.startpage.com/sp/qi?qimsn=ex&sxap=%2Fv1%2Fquery&sc=BqZ3inqrAgF701&sr=1
  558. try{
  559. $post = [
  560. "se" => "n0vze2y9dqwy",
  561. "q" => $json["render"]["query"],
  562. "results" => [], // populate
  563. "enableKnowledgePanel" => true,
  564. "enableMediaThumbBar" => false,
  565. "enableSearchSuggestions" => false,
  566. "enableTripadvisorProperties" => [],
  567. "enableTripadvisorPlaces" => [],
  568. "enableTripadvisorPlacesForLocations" => [],
  569. "enableWebProducts" => false,
  570. "tripadvisorPartnerId" => null,
  571. "tripadvisorMapColorMode" => "light",
  572. "tripadvisorDisablesKnowledgePanel" => false,
  573. "instantAnswers" => [
  574. "smartAnswers",
  575. "youtube",
  576. "tripadvisor"
  577. ],
  578. "iaType" => null,
  579. "forceEnhancedKnowledgePanel" => false,
  580. "shoppingOnly" => false,
  581. "allowAdultProducts" => true,
  582. "lang" => "en",
  583. "browserLang" => "en-US",
  584. "browserTimezone" => "America/New_York",
  585. "market" => null,
  586. "userLocation" => null,
  587. "userDate" => date("Y-m-d"),
  588. "userAgentType" => "unknown"
  589. ];
  590. foreach($out["web"] as $result){
  591. $post["results"][] = [
  592. "url" => $result["url"],
  593. "title" => $result["title"]
  594. ];
  595. }
  596. $post = json_encode($post, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE);
  597. $additional_data =
  598. $this->get(
  599. $proxy,
  600. "https://www.startpage.com/sp/qi?qimsn=ex&sxap=%2Fv1%2Fquery&sc=" . $json["render"]["callback_sc"] . "&sr=1",
  601. $post,
  602. true,
  603. true
  604. );
  605. $additional_data = json_decode($additional_data, true);
  606. if($additional_data === null){
  607. throw new Exception("Failed to decode JSON"); // just break out, dont fail completely
  608. }
  609. if(!isset($additional_data["knowledgePanel"])){
  610. throw new Exception("Response has missing data (knowledgePanel)");
  611. }
  612. $additional_data = $additional_data["knowledgePanel"];
  613. $answer = [
  614. "title" => $additional_data["meta"]["title"],
  615. "description" => [
  616. [
  617. "type" => "quote",
  618. "value" => $additional_data["meta"]["description"]
  619. ]
  620. ],
  621. "url" => $additional_data["meta"]["origWikiUrl"],
  622. "thumb" => $additional_data["meta"]["image"],
  623. "table" => [],
  624. "sublink" => []
  625. ];
  626. // parse html for instant answer
  627. $this->fuckhtml->load($additional_data["html"]);
  628. $div =
  629. $this->fuckhtml
  630. ->getElementsByTagName(
  631. "div"
  632. );
  633. // get description
  634. $description =
  635. $this->fuckhtml
  636. ->getElementsByClassName(
  637. "sx-kp-short-extract sx-kp-short-extract-complete",
  638. $div
  639. );
  640. if(count($description) !== 0){
  641. $answer["description"][] = [
  642. "type" => "text",
  643. "value" =>
  644. html_entity_decode(
  645. $this->fuckhtml
  646. ->getTextContent(
  647. $description[0]
  648. )
  649. )
  650. ];
  651. }
  652. // get socials
  653. $socials =
  654. $this->fuckhtml
  655. ->getElementsByClassName(
  656. "sx-wiki-social-link",
  657. "a"
  658. );
  659. foreach($socials as $social){
  660. $title =
  661. $this->fuckhtml
  662. ->getTextContent(
  663. $social["attributes"]["title"]
  664. );
  665. $url =
  666. $this->fuckhtml
  667. ->getTextContent(
  668. $social["attributes"]["href"]
  669. );
  670. switch($title){
  671. case "Official Website":
  672. $title = "Website";
  673. break;
  674. }
  675. $answer["sublink"][$title] = $url;
  676. }
  677. // get videos
  678. $videos =
  679. $this->fuckhtml
  680. ->getElementsByClassName(
  681. "sx-kp-video-grid-item",
  682. $div
  683. );
  684. foreach($videos as $video){
  685. $this->fuckhtml->load($video);
  686. $as =
  687. $this->fuckhtml
  688. ->getElementsByTagName(
  689. "a"
  690. );
  691. if(count($as) === 0){
  692. // ?? invalid
  693. continue;
  694. }
  695. $image =
  696. $this->fuckhtml
  697. ->getElementsByAttributeName(
  698. "data-sx-src",
  699. "img"
  700. );
  701. if(count($image) !== 0){
  702. $thumb = [
  703. "ratio" => "16:9",
  704. "url" =>
  705. $this->fuckhtml
  706. ->getTextContent(
  707. $image[0]["attributes"]["data-sx-src"]
  708. )
  709. ];
  710. }else{
  711. $thumb = [
  712. "ratio" => null,
  713. "url" => null
  714. ];
  715. }
  716. $out["video"][] = [
  717. "title" =>
  718. $this->fuckhtml
  719. ->getTextContent(
  720. $as[0]["attributes"]["title"]
  721. ),
  722. "description" => null,
  723. "date" => null,
  724. "duration" => null,
  725. "views" => null,
  726. "thumb" => $thumb,
  727. "url" =>
  728. $this->fuckhtml
  729. ->getTextContent(
  730. $as[0]["attributes"]["href"]
  731. )
  732. ];
  733. }
  734. // reset
  735. $this->fuckhtml->load($additional_data["html"]);
  736. // get table elements
  737. $table =
  738. $this->fuckhtml
  739. ->getElementsByClassName(
  740. "sx-infobox",
  741. "table"
  742. );
  743. if(count($table) !== 0){
  744. $trs =
  745. $this->fuckhtml
  746. ->getElementsByTagName(
  747. "tr"
  748. );
  749. foreach($trs as $tr){
  750. $this->fuckhtml->load($tr);
  751. // ok so startpage devs cant fucking code a table
  752. // td = content
  753. // th (AAAHH) = title
  754. $tds =
  755. $this->fuckhtml
  756. ->getElementsByTagName(
  757. "td"
  758. );
  759. $ths =
  760. $this->fuckhtml
  761. ->getElementsByTagName(
  762. "th"
  763. );
  764. if(
  765. count($ths) === 1 &&
  766. count($tds) === 1
  767. ){
  768. $title =
  769. $this->fuckhtml
  770. ->getTextContent(
  771. $ths[0]
  772. );
  773. $description = [];
  774. $this->fuckhtml->load($tds[0]);
  775. $lis =
  776. $this->fuckhtml
  777. ->getElementsByTagName(
  778. "li"
  779. );
  780. if(count($lis) !== 0){
  781. foreach($lis as $li){
  782. $description[] =
  783. $this->fuckhtml
  784. ->getTextContent(
  785. $li
  786. );
  787. }
  788. $description = implode(", ", $description);
  789. }else{
  790. $description =
  791. $this->fuckhtml
  792. ->getTextContent(
  793. $tds[0]
  794. );
  795. }
  796. $answer["table"][$title] = $description;
  797. }
  798. }
  799. }
  800. $out["answer"][] = $answer;
  801. }catch(Exception $error){
  802. // do nothing
  803. //echo "error!";
  804. }
  805. }
  806. return $out;
  807. }
  808. public function image($get){
  809. if($get["npt"]){
  810. [$post, $proxy] = $this->backend->get($get["npt"], "images");
  811. try{
  812. $html = $this->get(
  813. $proxy,
  814. "https://www.startpage.com/sp/search",
  815. $post,
  816. true
  817. );
  818. }catch(Exception $error){
  819. throw new Exception("Failed to fetch search page");
  820. }
  821. }else{
  822. $search = $get["s"];
  823. if(strlen($search) === 0){
  824. throw new Exception("Search term is empty!");
  825. }
  826. try{
  827. $proxy = $this->backend->get_ip();
  828. $params = [
  829. "query" => $get["s"],
  830. "cat" => "images",
  831. "pl" => "opensearch"
  832. ];
  833. if($get["nsfw"] == "no"){
  834. $params["qadf"] = "heavy";
  835. }
  836. if($get["size"] != "any"){
  837. if(
  838. $get["size"] == "Small" ||
  839. $get["size"] == "Medium" ||
  840. $get["size"] == "Large" ||
  841. $get["size"] == "Wallpaper"
  842. ){
  843. $params["flimgsize"] = $get["size"];
  844. }else{
  845. $params["image-size-select"] = "isz:lt,islt:" . $get["size"];
  846. }
  847. }
  848. if($get["color"] != "any"){
  849. if($get["color"] == "color"){
  850. $params["flimgcolor"] = "ic:color";
  851. }elseif($get["color"] == "bnw"){
  852. $params["flimgcolor"] = "ic:gray";
  853. }else{
  854. $params["flimgcolor"] = "ic:specific,isc:" . $get["color"];
  855. }
  856. }
  857. if($get["type"] != "any"){
  858. $params["flimgtype"] = $get["type"];
  859. }
  860. if($get["license"] != "any"){
  861. $params["flimglicense"] = $get["license"];
  862. }
  863. try{
  864. $html = $this->get(
  865. $proxy,
  866. "https://www.startpage.com/sp/search",
  867. $params
  868. );
  869. }catch(Exception $error){
  870. throw new Exception("Failed to fetch search page");
  871. }
  872. //$html = file_get_contents("scraper/startpage.html");
  873. }catch(Exception $error){
  874. throw new Exception("Failed to fetch search page");
  875. }
  876. }
  877. $out = [
  878. "status" => "ok",
  879. "npt" => null,
  880. "image" => []
  881. ];
  882. if(
  883. preg_match(
  884. '/React\.createElement\(UIStartpage\.AppSerpImages, ?(.+)\),$/m',
  885. $html,
  886. $matches
  887. ) === 0
  888. ){
  889. throw new Exception("Failed to grep JSON object");
  890. }
  891. $json = json_decode($matches[1], true);
  892. if($json === null){
  893. throw new Exception("Failed to decode JSON object");
  894. }
  895. // get npt
  896. $out["npt"] = $this->parse_npt($json, "images", $proxy);
  897. // get images
  898. foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
  899. if($category["display_type"] != "images-bing"){
  900. // ignore ads and !! suggestions !! @todo
  901. continue;
  902. }
  903. foreach($category["results"] as $image){
  904. $out["image"][] = [
  905. "title" => $this->titledots($image["title"]),
  906. "source" => [
  907. [
  908. "url" => $this->unshitimage($image["clickUrl"]),
  909. "width" => (int)$image["width"],
  910. "height" => (int)$image["height"]
  911. ],
  912. [
  913. "url" => $this->unshitimage($image["thumbnailUrl"]),
  914. "width" => (int)$image["thumbnailWidth"],
  915. "height" => (int)$image["thumbnailHeight"]
  916. ]
  917. ],
  918. "url" => $image["altClickUrl"]
  919. ];
  920. }
  921. }
  922. return $out;
  923. }
  924. public function video($get){
  925. if($get["npt"]){
  926. [$post, $proxy] = $this->backend->get($get["npt"], "videos");
  927. try{
  928. $html = $this->get(
  929. $proxy,
  930. "https://www.startpage.com/sp/search",
  931. $post,
  932. true
  933. );
  934. }catch(Exception $error){
  935. throw new Exception("Failed to fetch search page");
  936. }
  937. }else{
  938. $search = $get["s"];
  939. if(strlen($search) === 0){
  940. throw new Exception("Search term is empty!");
  941. }
  942. try{
  943. $proxy = $this->backend->get_ip();
  944. $params = [
  945. "query" => $get["s"],
  946. "cat" => "video",
  947. "pl" => "opensearch"
  948. ];
  949. if($get["nsfw"] == "no"){
  950. $params["qadf"] = "heavy";
  951. }
  952. if($get["sort"] != "relevance"){
  953. $params["sort_by"] = $get["sort"];
  954. }
  955. if($get["duration"] != "any"){
  956. $params["with_duration"] = $get["duration"];
  957. }
  958. try{
  959. $html = $this->get(
  960. $proxy,
  961. "https://www.startpage.com/sp/search",
  962. $params
  963. );
  964. }catch(Exception $error){
  965. throw new Exception("Failed to fetch search page");
  966. }
  967. //$html = file_get_contents("scraper/startpage.html");
  968. }catch(Exception $error){
  969. throw new Exception("Failed to fetch search page");
  970. }
  971. }
  972. if(
  973. preg_match(
  974. '/React\.createElement\(UIStartpage\.AppSerpVideos, ?(.+)\),$/m',
  975. $html,
  976. $matches
  977. ) === 0
  978. ){
  979. throw new Exception("Failed to get JSON object");
  980. }
  981. $json = json_decode($matches[1], true);
  982. if($json === null){
  983. throw new Exception("Failed to decode JSON object");
  984. }
  985. $out = [
  986. "status" => "ok",
  987. "npt" => null,
  988. "video" => [],
  989. "author" => [],
  990. "livestream" => [],
  991. "playlist" => [],
  992. "reel" => []
  993. ];
  994. // get npt
  995. $out["npt"] = $this->parse_npt($json, "video", $proxy);
  996. // get results
  997. foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
  998. if($category["display_type"] == "video-youtube"){
  999. foreach($category["results"] as $video){
  1000. if(
  1001. isset($video["thumbnailUrl"]) &&
  1002. $video["thumbnailUrl"] !== null
  1003. ){
  1004. $thumb = [
  1005. "ratio" => "16:9",
  1006. "url" => $this->unshitimage($video["thumbnailUrl"])
  1007. ];
  1008. }else{
  1009. $thumb = [
  1010. "ratio" => null,
  1011. "url" => null
  1012. ];
  1013. }
  1014. $out["video"][] = [
  1015. "title" => $video["title"],
  1016. "description" => $this->limitstrlen($video["description"]),
  1017. "author" => [
  1018. "name" => $video["channelTitle"],
  1019. "url" => null,
  1020. "avatar" => null
  1021. ],
  1022. "date" => strtotime($video["publishDate"]),
  1023. "duration" => $this->hms2int($video["duration"]),
  1024. "views" => (int)$video["viewCount"],
  1025. "thumb" => $thumb,
  1026. "url" => $video["clickUrl"]
  1027. ];
  1028. }
  1029. }
  1030. }
  1031. return $out;
  1032. }
  1033. public function news($get){
  1034. if($get["npt"]){
  1035. [$post, $proxy] = $this->backend->get($get["npt"], "news");
  1036. try{
  1037. $html = $this->get(
  1038. $proxy,
  1039. "https://www.startpage.com/sp/search",
  1040. $post,
  1041. true
  1042. );
  1043. }catch(Exception $error){
  1044. throw new Exception("Failed to fetch search page");
  1045. }
  1046. }else{
  1047. $search = $get["s"];
  1048. if(strlen($search) === 0){
  1049. throw new Exception("Search term is empty!");
  1050. }
  1051. try{
  1052. $proxy = $this->backend->get_ip();
  1053. $params = [
  1054. "query" => $get["s"],
  1055. "cat" => "news",
  1056. "pl" => "opensearch"
  1057. ];
  1058. if($get["nsfw"] == "no"){
  1059. $params["qadf"] = "heavy";
  1060. }
  1061. if($get["time"] != "any"){
  1062. $params["with_date"] = $get["time"];
  1063. }
  1064. try{
  1065. $html = $this->get(
  1066. $proxy,
  1067. "https://www.startpage.com/sp/search",
  1068. $params
  1069. );
  1070. }catch(Exception $error){
  1071. throw new Exception("Failed to fetch search page");
  1072. }
  1073. //$html = file_get_contents("scraper/startpage.html");
  1074. }catch(Exception $error){
  1075. throw new Exception("Failed to fetch search page");
  1076. }
  1077. }
  1078. if(
  1079. preg_match(
  1080. '/React\.createElement\(UIStartpage\.AppSerpNews, ?(.+)\),$/m',
  1081. $html,
  1082. $matches
  1083. ) === 0
  1084. ){
  1085. throw new Exception("Failed to get JSON object");
  1086. }
  1087. $json = json_decode($matches[1], true);
  1088. if($json === null){
  1089. throw new Exception("Failed to decode JSON object");
  1090. }
  1091. $out = [
  1092. "status" => "ok",
  1093. "npt" => null,
  1094. "news" => []
  1095. ];
  1096. // get npt
  1097. $out["npt"] = $this->parse_npt($json, "news", $proxy);
  1098. foreach($json["render"]["presenter"]["regions"]["mainline"] as $category){
  1099. if($category["display_type"] != "news-bing"){
  1100. // unsupported category
  1101. continue;
  1102. }
  1103. foreach($category["results"] as $news){
  1104. if(
  1105. isset($news["thumbnailUrl"]) &&
  1106. $news["thumbnailUrl"] !== null
  1107. ){
  1108. $thumb = [
  1109. "ratio" => "16:9",
  1110. "url" => $this->unshitimage($news["thumbnailUrl"])
  1111. ];
  1112. }else{
  1113. $thumb = [
  1114. "ratio" => null,
  1115. "url" => null
  1116. ];
  1117. }
  1118. $out["news"][] = [
  1119. "title" => $this->titledots($this->remove_penguins($news["title"])),
  1120. "author" => $news["source"],
  1121. "description" => $this->titledots($this->remove_penguins($news["description"])),
  1122. "date" => (int)substr((string)$news["date"], 0, -3),
  1123. "thumb" => $thumb,
  1124. "url" => $news["clickUrl"]
  1125. ];
  1126. }
  1127. }
  1128. return $out;
  1129. }
  1130. private function parse_npt($json, $pagetype, $proxy){
  1131. foreach($json["render"]["presenter"]["pagination"]["pages"] as $page){
  1132. if($page["name"] == "Next"){
  1133. parse_str(
  1134. explode(
  1135. "?",
  1136. $page["url"],
  1137. 2
  1138. )[1],
  1139. $str
  1140. );
  1141. return
  1142. $this->backend->store(
  1143. http_build_query(
  1144. [
  1145. "lui" => "english",
  1146. "language" => "english",
  1147. "query" => $str["q"],
  1148. "cat" => $pagetype,
  1149. "sc" => $str["sc"],
  1150. "t" => "device",
  1151. "segment" => "startpage.udog",
  1152. "page" => $str["page"]
  1153. ]
  1154. ),
  1155. $pagetype,
  1156. $proxy
  1157. );
  1158. break;
  1159. }
  1160. }
  1161. return null;
  1162. }
  1163. private function unshitimage($url){
  1164. $query = parse_url($url, PHP_URL_QUERY);
  1165. parse_str($query, $query);
  1166. if(isset($query["piurl"])){
  1167. if(strpos($query["piurl"], "gstatic.com/")){
  1168. return
  1169. explode(
  1170. "&",
  1171. $query["piurl"],
  1172. 2
  1173. )[0];
  1174. }
  1175. if(
  1176. strpos($query["piurl"], "bing.net/") ||
  1177. strpos($query["piurl"], "bing.com/")
  1178. ){
  1179. return
  1180. explode(
  1181. "&",
  1182. $query["piurl"],
  1183. 2
  1184. )[0];
  1185. }
  1186. return $query["piurl"];
  1187. }
  1188. return $url;
  1189. }
  1190. private function limitstrlen($text){
  1191. return
  1192. explode(
  1193. "\n",
  1194. wordwrap(
  1195. str_replace(
  1196. ["\n\r", "\r\n", "\n", "\r"],
  1197. " ",
  1198. $text
  1199. ),
  1200. 300,
  1201. "\n"
  1202. ),
  1203. 2
  1204. )[0];
  1205. }
  1206. private function titledots($title){
  1207. return trim($title, " .\t\n\r\0\x0B…");
  1208. }
  1209. private function hms2int($time){
  1210. $parts = explode(":", $time, 3);
  1211. $time = 0;
  1212. if(count($parts) === 3){
  1213. // hours
  1214. $time = $time + ((int)$parts[0] * 3600);
  1215. array_shift($parts);
  1216. }
  1217. if(count($parts) === 2){
  1218. // minutes
  1219. $time = $time + ((int)$parts[0] * 60);
  1220. array_shift($parts);
  1221. }
  1222. // seconds
  1223. $time = $time + (int)$parts[0];
  1224. return $time;
  1225. }
  1226. private function remove_penguins($text){
  1227. return str_replace(
  1228. ["", ""],
  1229. "",
  1230. $text
  1231. );
  1232. }
  1233. }