ddg.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  1. <?php
  2. class ddg{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("ddg");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. /*
  10. curl functions
  11. */
  12. private const req_web = 0;
  13. private const req_xhr = 1;
  14. private function get($proxy, $url, $get = [], $reqtype = self::req_web){
  15. $curlproc = curl_init();
  16. if($get !== []){
  17. $get = http_build_query($get);
  18. $url .= "?" . $get;
  19. }
  20. curl_setopt($curlproc, CURLOPT_URL, $url);
  21. // http2 bypass
  22. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  23. switch($reqtype){
  24. case self::req_web:
  25. $headers =
  26. ["User-Agent: " . config::USER_AGENT,
  27. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  28. "Accept-Language: en-US,en;q=0.5",
  29. "Accept-Encoding: gzip",
  30. "DNT: 1",
  31. "Sec-GPC: 1",
  32. "Connection: keep-alive",
  33. "Upgrade-Insecure-Requests: 1",
  34. "Sec-Fetch-Dest: document",
  35. "Sec-Fetch-Mode: navigate",
  36. "Sec-Fetch-Site: same-origin",
  37. "Sec-Fetch-User: ?1",
  38. "Priority: u=0, i",
  39. "TE: trailers"];
  40. break;
  41. case self::req_xhr:
  42. $headers =
  43. ["User-Agent: " . config::USER_AGENT,
  44. "Accept: */*",
  45. "Accept-Language: en-US,en;q=0.5",
  46. "Accept-Encoding: gzip",
  47. "Referer: https://duckduckgo.com/",
  48. "DNT: 1",
  49. "Sec-GPC: 1",
  50. "Connection: keep-alive",
  51. "Sec-Fetch-Dest: script",
  52. "Sec-Fetch-Mode: no-cors",
  53. "Sec-Fetch-Site: same-site",
  54. "Priority: u=1"];
  55. break;
  56. }
  57. $this->backend->assign_proxy($curlproc, $proxy);
  58. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  59. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  60. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  61. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  62. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  63. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  64. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  65. $data = curl_exec($curlproc);
  66. if(curl_errno($curlproc)){
  67. throw new Exception(curl_error($curlproc));
  68. }
  69. curl_close($curlproc);
  70. return $data;
  71. }
  72. public function getfilters($pagetype){
  73. $base = [
  74. "country" => [
  75. "display" => "Country",
  76. "option" => [
  77. "us-en" => "US (English)",
  78. "ar-es" => "Argentina",
  79. "au-en" => "Australia",
  80. "at-de" => "Austria",
  81. "be-fr" => "Belgium (fr)",
  82. "be-nl" => "Belgium (nl)",
  83. "br-pt" => "Brazil",
  84. "bg-bg" => "Bulgaria",
  85. "ca-en" => "Canada (en)",
  86. "ca-fr" => "Canada (fr)",
  87. "ct-ca" => "Catalonia",
  88. "cl-es" => "Chile",
  89. "cn-zh" => "China",
  90. "co-es" => "Colombia",
  91. "hr-hr" => "Croatia",
  92. "cz-cs" => "Czech Republic",
  93. "dk-da" => "Denmark",
  94. "ee-et" => "Estonia",
  95. "fi-fi" => "Finland",
  96. "fr-fr" => "France",
  97. "de-de" => "Germany",
  98. "gr-el" => "Greece",
  99. "hk-tzh" => "Hong Kong",
  100. "hu-hu" => "Hungary",
  101. "in-en" => "India (en)",
  102. "id-en" => "Indonesia (en)",
  103. "ie-en" => "Ireland",
  104. "il-en" => "Israel (en)",
  105. "it-it" => "Italy",
  106. "jp-jp" => "Japan",
  107. "kr-kr" => "Korea",
  108. "lv-lv" => "Latvia",
  109. "lt-lt" => "Lithuania",
  110. "my-en" => "Malaysia (en)",
  111. "mx-es" => "Mexico",
  112. "nl-nl" => "Netherlands",
  113. "nz-en" => "New Zealand",
  114. "no-no" => "Norway",
  115. "pk-en" => "Pakistan (en)",
  116. "pe-es" => "Peru",
  117. "ph-en" => "Philippines (en)",
  118. "pl-pl" => "Poland",
  119. "pt-pt" => "Portugal",
  120. "ro-ro" => "Romania",
  121. "ru-ru" => "Russia",
  122. "xa-ar" => "Saudi Arabia",
  123. "sg-en" => "Singapore",
  124. "sk-sk" => "Slovakia",
  125. "sl-sl" => "Slovenia",
  126. "za-en" => "South Africa",
  127. "es-ca" => "Spain (ca)",
  128. "es-es" => "Spain (es)",
  129. "se-sv" => "Sweden",
  130. "ch-de" => "Switzerland (de)",
  131. "ch-fr" => "Switzerland (fr)",
  132. "tw-tzh" => "Taiwan",
  133. "th-en" => "Thailand (en)",
  134. "tr-tr" => "Turkey",
  135. "us-es" => "US (Spanish)",
  136. "ua-uk" => "Ukraine",
  137. "uk-en" => "United Kingdom",
  138. "vn-en" => "Vietnam (en)"
  139. ]
  140. ]
  141. ];
  142. switch($pagetype){
  143. case "web":
  144. $base["country"]["option"] =
  145. array_merge(["any" => "All Regions"], $base["country"]["option"]);
  146. return array_merge($base,
  147. [
  148. "nsfw" => [
  149. "display" => "NSFW",
  150. "option" => [
  151. "yes" => "Yes",
  152. "maybe" => "Maybe",
  153. "no" => "No"
  154. ]
  155. ],
  156. "newer" => [
  157. "display" => "Newer than",
  158. "option" => "_DATE"
  159. ],
  160. "older" => [
  161. "display" => "Older than",
  162. "option" => "_DATE"
  163. ],
  164. "extendedsearch" => [
  165. // undefined display
  166. "option" => [
  167. "yes" => "Yes",
  168. "no" => "No",
  169. ]
  170. ]
  171. ]
  172. );
  173. break;
  174. case "images":
  175. return array_merge($base,
  176. [
  177. "nsfw" => [
  178. "display" => "NSFW",
  179. "option" => [
  180. "yes" => "Yes",
  181. "no" => "No"
  182. ]
  183. ],
  184. "date" => [
  185. "display" => "Time posted",
  186. "option" => [
  187. "any" => "Any time",
  188. "Day" => "Past day",
  189. "Week" => "Past week",
  190. "Month" => "Past month"
  191. ]
  192. ],
  193. "size" => [
  194. "display" => "Size",
  195. "option" => [
  196. "any" => "Any size",
  197. "Small" => "Small",
  198. "Medium" => "Medium",
  199. "Large" => "Large",
  200. "Wallpaper" => "Wallpaper"
  201. ]
  202. ],
  203. "color" => [
  204. "display" => "Colors",
  205. "option" => [
  206. "any" => "All colors",
  207. "Monochrome" => "Black and white",
  208. "Red" => "Red",
  209. "Orange" => "Orange",
  210. "Yellow" => "Yellow",
  211. "Green" => "Green",
  212. "Blue" => "Blue",
  213. "Purple" => "Purple",
  214. "Pink" => "Pink",
  215. "Brown" => "Brown",
  216. "Black" => "Black",
  217. "Gray" => "Gray",
  218. "Teal" => "Teal",
  219. "White" => "White"
  220. ]
  221. ],
  222. "type" => [
  223. "display" => "Type",
  224. "option" => [
  225. "any" => "All types",
  226. "photo" => "Photograph",
  227. "clipart" => "Clipart",
  228. "gif" => "Animated GIF",
  229. "transparent" => "Transparent"
  230. ]
  231. ],
  232. "layout" => [
  233. "display" => "Layout",
  234. "option" => [
  235. "any" => "All layouts",
  236. "Square" => "Square",
  237. "Tall" => "Tall",
  238. "Wide" => "Wide"
  239. ]
  240. ],
  241. "license" => [
  242. "display" => "License",
  243. "option" => [
  244. "any" => "All licenses",
  245. "Any" => "All Creative Commons",
  246. "Public" => "Public domain",
  247. "Share" => "Free to Share and Use",
  248. "ShareCommercially" => "Free to Share and Use Commercially",
  249. "Modify" => "Free to Modify, Share, and Use",
  250. "ModifyCommercially" => "Free to Modify, Share, and Use Commercially"
  251. ]
  252. ]
  253. ]
  254. );
  255. break;
  256. case "videos":
  257. return array_merge($base,
  258. [
  259. "nsfw" => [
  260. "display" => "NSFW",
  261. "option" => [
  262. "yes" => "Yes",
  263. "maybe" => "Maybe",
  264. "no" => "No"
  265. ]
  266. ],
  267. "date" => [
  268. "display" => "Time fetched",
  269. "option" => [
  270. "any" => "Any time",
  271. "d" => "Past day",
  272. "w" => "Past week",
  273. "m" => "Past month"
  274. ]
  275. ],
  276. "resolution" => [ //videoDefinition
  277. "display" => "Resolution",
  278. "option" => [
  279. "any" => "Any resolution",
  280. "high" => "High definition",
  281. "standard" => "Standard definition"
  282. ]
  283. ],
  284. "duration" => [ // videoDuration
  285. "display" => "Duration",
  286. "option" => [
  287. "any" => "Any duration",
  288. "short" => "Short (>5min)",
  289. "medium" => "Medium (5-20min)",
  290. "long" => "Long (<20min)"
  291. ]
  292. ],
  293. "license" => [
  294. "display" => "License",
  295. "option" => [
  296. "any" => "Any license",
  297. "creativeCommon" => "Creative Commons",
  298. "youtube" => "YouTube Standard"
  299. ]
  300. ]
  301. ]
  302. );
  303. break;
  304. case "news":
  305. return array_merge($base,
  306. [
  307. "nsfw" => [
  308. "display" => "NSFW",
  309. "option" => [
  310. "yes" => "Yes",
  311. "maybe" => "Maybe",
  312. "no" => "No"
  313. ]
  314. ],
  315. "date" => [
  316. "display" => "Time posted",
  317. "option" => [
  318. "any" => "Any time",
  319. "d" => "Past day",
  320. "w" => "Past week",
  321. "m" => "Past month"
  322. ]
  323. ]
  324. ]
  325. );
  326. break;
  327. }
  328. }
  329. public function web($get){
  330. if($get["npt"]){
  331. [$raw_data, $proxy] = $this->backend->get($get["npt"], "web");
  332. $raw_data = explode(",", $raw_data, 2);
  333. if($raw_data[0] == "0"){
  334. return $this->web_html($get, [$raw_data[1], $proxy]);
  335. }
  336. return $this->web_full($get, [$raw_data[1], $proxy]);
  337. }else{
  338. // we have $get["s"]
  339. if(strpos($get["s"], "\"") !== false){
  340. return $this->web_html($get);
  341. }
  342. // no quotes sent, do full web search
  343. return $this->web_full($get);
  344. }
  345. }
  346. public function web_html($get, $npt = null){
  347. $out = [
  348. "status" => "ok",
  349. "spelling" => [
  350. "type" => "no_correction",
  351. "using" => null,
  352. "correction" => null
  353. ],
  354. "npt" => null,
  355. "answer" => [],
  356. "web" => [],
  357. "image" => [],
  358. "video" => [],
  359. "news" => [],
  360. "related" => []
  361. ];
  362. if($npt !== null){
  363. [$get_filters, $proxy] = $npt;
  364. $get_filters = json_decode($get_filters, true);
  365. }else{
  366. if(strlen($get["s"]) === 0){
  367. throw new Exception("Search term is empty!");
  368. }
  369. $proxy = $this->backend->get_ip();
  370. // generate filters
  371. $get_filters = [
  372. "q" => $get["s"]
  373. ];
  374. if($get["country"] == "any"){
  375. $get_filters["kl"] = "wt-wt";
  376. }else{
  377. $get_filters["kl"] = $get["country"];
  378. }
  379. switch($get["nsfw"]){
  380. case "yes": $get_filters["kp"] = "-2"; break;
  381. case "maybe": $get_filters["kp"] = "-1"; break;
  382. case "no": $get_filters["kp"] = "1"; break;
  383. }
  384. $df = true;
  385. if($get["newer"] === false){
  386. if($get["older"] !== false){
  387. $start = 36000;
  388. $end = $get["older"];
  389. }else{
  390. $df = false;
  391. }
  392. }else{
  393. $start = $get["newer"];
  394. if($get["older"] !== false){
  395. $end = $get["older"];
  396. }else{
  397. $end = time();
  398. }
  399. }
  400. if($df === true){
  401. $get_filters["df"] = date("Y-m-d", $start) . ".." . date("Y-m-d", $end);
  402. }
  403. }
  404. //
  405. // Get HTML
  406. //
  407. try{
  408. $html = $this->get(
  409. $proxy,
  410. "https://html.duckduckgo.com/html/",
  411. $get_filters
  412. );
  413. }catch(Exception $e){
  414. throw new Exception("Failed to fetch search page");
  415. }
  416. //$html = file_get_contents("scraper/ddg.html");
  417. $this->fuckhtml->load($html);
  418. //
  419. // Get next page token
  420. //
  421. $forms =
  422. $this->fuckhtml
  423. ->getElementsByTagName(
  424. "form"
  425. );
  426. foreach(array_reverse($forms) as $form){
  427. $this->fuckhtml->load($form);
  428. $input_probe =
  429. $this->fuckhtml
  430. ->getElementsByClassName(
  431. "btn--alt",
  432. "input"
  433. );
  434. if(count($input_probe) !== 0){
  435. // found next page!
  436. $inputs =
  437. $this->fuckhtml
  438. ->getElementsByAttributeValue(
  439. "type",
  440. "hidden",
  441. "input"
  442. );
  443. $query = [];
  444. foreach($inputs as $q){
  445. $query[
  446. $this->fuckhtml
  447. ->getTextContent(
  448. $q["attributes"]["name"]
  449. )
  450. ] =
  451. $this->fuckhtml
  452. ->getTextContent(
  453. $q["attributes"]["value"]
  454. );
  455. }
  456. $out["npt"] =
  457. $this->backend->store(
  458. "0," . json_encode($query),
  459. "web",
  460. $proxy
  461. );
  462. break;
  463. }
  464. }
  465. // reset
  466. $this->fuckhtml->load($html);
  467. //
  468. // parse wikipedia answer
  469. //
  470. $wiki_wrapper =
  471. $this->fuckhtml
  472. ->getElementsByClassName(
  473. "zci-wrapper",
  474. "div"
  475. );
  476. if(count($wiki_wrapper) !== 0){
  477. $this->fuckhtml->load($wiki_wrapper[0]);
  478. $a =
  479. $this->fuckhtml
  480. ->getElementsByTagName(
  481. "a"
  482. );
  483. if(count($a) !== 0){
  484. $link =
  485. $this->unshiturl(
  486. $this->fuckhtml
  487. ->getTextContent(
  488. $a[0]["attributes"]["href"]
  489. )
  490. );
  491. }else{
  492. $link = null;
  493. }
  494. $title =
  495. $this->fuckhtml
  496. ->getElementsByTagName(
  497. "h1"
  498. );
  499. if(count($title) !== 0){
  500. $title =
  501. $this->fuckhtml
  502. ->getTextContent(
  503. $title[0]
  504. );
  505. }else{
  506. $title = null;
  507. }
  508. $description =
  509. $this->fuckhtml
  510. ->getElementById(
  511. "zero_click_abstract",
  512. "div"
  513. );
  514. if($description !== false){
  515. $this->fuckhtml->load($description);
  516. $thumb =
  517. $this->fuckhtml
  518. ->getElementsByTagName(
  519. "img"
  520. );
  521. if(count($thumb) !== 0){
  522. $thumb =
  523. $this->fuckhtml
  524. ->getTextContent(
  525. $thumb[0]["attributes"]["src"]
  526. );
  527. }else{
  528. $thumb = null;
  529. }
  530. $as =
  531. $this->fuckhtml
  532. ->getElementsByTagName(
  533. "a"
  534. );
  535. foreach($as as $a){
  536. $description["innerHTML"] =
  537. str_replace(
  538. $a["outerHTML"],
  539. "",
  540. $description["innerHTML"]
  541. );
  542. }
  543. $description =
  544. $this->fuckhtml
  545. ->getTextContent(
  546. $description
  547. );
  548. $out["answer"][] = [
  549. "title" => $title,
  550. "description" => [
  551. [
  552. "type" => "text",
  553. "value" => $description
  554. ]
  555. ],
  556. "url" => $link,
  557. "thumb" => $thumb,
  558. "table" => [],
  559. "sublink" => []
  560. ];
  561. }
  562. // reset
  563. $this->fuckhtml->load($html);
  564. }
  565. //
  566. // Get results
  567. //
  568. $results =
  569. $this->fuckhtml
  570. ->getElementsByClassName(
  571. "result",
  572. "div"
  573. );
  574. foreach($results as $result){
  575. $this->fuckhtml->load($result);
  576. $title =
  577. $this->fuckhtml
  578. ->getElementsByTagName(
  579. "h2"
  580. );
  581. if(count($title) === 0){
  582. // should not happen
  583. continue;
  584. }
  585. $title =
  586. $this->fuckhtml
  587. ->getTextContent(
  588. $title[0]
  589. );
  590. $description_obj =
  591. $this->fuckhtml
  592. ->getElementsByClassName(
  593. "result__snippet",
  594. "a"
  595. );
  596. if(count($description_obj) === 0){
  597. $description = null;
  598. }else{
  599. $description =
  600. $this->titledots(
  601. $this->fuckhtml
  602. ->getTextContent(
  603. $description_obj[0]
  604. )
  605. );
  606. }
  607. $url =
  608. $this->fuckhtml
  609. ->getTextContent(
  610. $description_obj[0]["attributes"]["href"]
  611. );
  612. $out["web"][] = [
  613. "title" => $this->titledots($title),
  614. "description" => $description,
  615. "url" => $this->unshiturl($url),
  616. "date" => null,
  617. "type" => "web",
  618. "thumb" => [
  619. "ratio" => null,
  620. "url" => null
  621. ],
  622. "sublink" => [],
  623. "table" => []
  624. ];
  625. }
  626. return $out;
  627. }
  628. public function web_full($get, $npt = null){
  629. $out = [
  630. "status" => "ok",
  631. "spelling" => [
  632. "type" => "no_correction",
  633. "using" => null,
  634. "correction" => null
  635. ],
  636. "npt" => null,
  637. "answer" => [],
  638. "web" => [],
  639. "image" => [],
  640. "video" => [],
  641. "news" => [],
  642. "related" => []
  643. ];
  644. if($npt !== null){
  645. [$js_link, $proxy] = $npt;
  646. $js_link = "https://links.duckduckgo.com" . $js_link;
  647. $html = "";
  648. $get["extendedsearch"] = "no";
  649. }else{
  650. if(strlen($get["s"]) === 0){
  651. throw new Exception("Search term is empty!");
  652. }
  653. $proxy = $this->backend->get_ip();
  654. // generate filters
  655. $get_filters = [
  656. "q" => $get["s"]
  657. ];
  658. if($get["country"] == "any"){
  659. $get_filters["kl"] = "wt-wt";
  660. }else{
  661. $get_filters["kl"] = $get["country"];
  662. }
  663. switch($get["nsfw"]){
  664. case "yes": $get_filters["kp"] = "-2"; break;
  665. case "maybe": $get_filters["kp"] = "-1"; break;
  666. case "no": $get_filters["kp"] = "1"; break;
  667. }
  668. $df = true;
  669. if($get["newer"] === false){
  670. if($get["older"] !== false){
  671. $start = 36000;
  672. $end = $get["older"];
  673. }else{
  674. $df = false;
  675. }
  676. }else{
  677. $start = $get["newer"];
  678. if($get["older"] !== false){
  679. $end = $get["older"];
  680. }else{
  681. $end = time();
  682. }
  683. }
  684. if($df === true){
  685. $get_filters["df"] = date("Y-m-d", $start) . ".." . date("Y-m-d", $end);
  686. }
  687. //
  688. // Get HTML
  689. //
  690. try{
  691. $html = $this->get(
  692. $proxy,
  693. "https://duckduckgo.com/",
  694. $get_filters
  695. );
  696. }catch(Exception $e){
  697. throw new Exception("Failed to fetch search page");
  698. }
  699. $this->fuckhtml->load($html);
  700. $script =
  701. $this->fuckhtml
  702. ->getElementById(
  703. "deep_preload_link",
  704. "link"
  705. );
  706. if(
  707. $script === null ||
  708. !isset($script["attributes"]["href"])
  709. ){
  710. throw new Exception("Failed to grep d.js");
  711. }
  712. $js_link =
  713. $this->fuckhtml
  714. ->getTextContent(
  715. $script["attributes"]["href"]
  716. );
  717. }
  718. //
  719. // Get d.js
  720. //
  721. try{
  722. $js = $this->get(
  723. $proxy,
  724. $js_link,
  725. [],
  726. ddg::req_xhr
  727. );
  728. }catch(Exception $e){
  729. throw new Exception("Failed to fetch d.js");
  730. }
  731. //$js = file_get_contents("scraper/fuck.js");
  732. //echo htmlspecialchars($js);
  733. $js_tmp =
  734. preg_split(
  735. '/DDG\.pageLayout\.load\(\s*\'d\'\s*,\s*/',
  736. $js,
  737. 2
  738. );
  739. if(count($js_tmp) <= 1){
  740. //
  741. // Detect javascript challenge
  742. //
  743. if(
  744. preg_match(
  745. '/DDG\.deep\.initialize\(\'([^\']+)\'\ *\+ *jsa/i',
  746. $js,
  747. $challenge_url
  748. )
  749. ){
  750. throw new Exception("DuckDuckGo returned a JSA challenge");
  751. // get JSA initial token
  752. if(
  753. !preg_match(
  754. '/let jsa *= *([0-9]+)/',
  755. $js,
  756. $jsa
  757. )
  758. ){
  759. $jsa = 0;
  760. }else{
  761. $jsa = (int)$jsa[1];
  762. }
  763. // get function bodies
  764. preg_match_all(
  765. '/let *([A-Za-z0-9]+) *= *function\(.*\) *{(.*)};/sU',
  766. $js,
  767. $functions
  768. );
  769. $parsed_functions = [];
  770. for($i=0; $i<count($functions[0]); $i++){
  771. $functions[2][$i] = trim($functions[2][$i]);
  772. if(
  773. preg_match(
  774. '/return num *\* *([0-9]+)/i',
  775. $functions[2][$i],
  776. $num
  777. )
  778. ){
  779. $parsed_functions[$functions[1][$i]] = [
  780. "type" => "multiplication",
  781. "num" => (int)$num[1]
  782. ];
  783. continue;
  784. }
  785. if(
  786. preg_match(
  787. '/innerHTML *= *`([^`]+)`/i',
  788. $functions[2][$i],
  789. $challenge
  790. )
  791. ){
  792. $challenge[1] =
  793. preg_replace(
  794. '/<\/(br)>/',
  795. '<$1>',
  796. $challenge[1]
  797. );
  798. $parsed_functions[$functions[1][$i]] = [
  799. "type" => "challenge",
  800. "text" => $challenge[1]
  801. ];
  802. }
  803. }
  804. // get function call order
  805. preg_match_all(
  806. '/jsa *= *([A-Za-z0-9]+)\(jsa\)/i',
  807. $js,
  808. $call_order
  809. );
  810. foreach($call_order[1] as $order){
  811. if(!isset($parsed_functions[$order])){
  812. throw new Exception("JS challenge solve failure: DuckDuckGo called an unknown function");
  813. }
  814. if($parsed_functions[$order]["type"] == "multiplication"){
  815. $jsa = $jsa * $parsed_functions[$order]["num"];
  816. continue;
  817. }
  818. if($parsed_functions[$order]["type"] == "challenge"){
  819. // @TODO get parsed length
  820. //$parsed_functions[$order]["text"]
  821. $jsa = $jsa + strlen($parsed_functions[$order]["text"]);
  822. }
  823. }
  824. try{
  825. $js = $this->get(
  826. $proxy,
  827. "https://links.duckduckgo.com" . $challenge_url[1] . $jsa,
  828. [],
  829. ddg::req_xhr
  830. );
  831. }catch(Exception $error){
  832. throw new Exception("Failed to get challenged d.js");
  833. }
  834. }
  835. //
  836. // Detect JavaScript anomaly failure thingy
  837. //
  838. if(
  839. preg_match(
  840. '/DDG.deep.anomalyDetectionBlock\({/',
  841. $js
  842. )
  843. ){
  844. throw new Exception("DuckDuckGo detected an anomaly in the Javascript challenge response");
  845. }
  846. throw new Exception("Failed to grep pageLayout(d)");
  847. }
  848. $json =
  849. json_decode(
  850. $this->fuckhtml
  851. ->extract_json(
  852. $js_tmp[1]
  853. ),
  854. true
  855. );
  856. if($json === null){
  857. throw new Exception("Failed to decode JSON");
  858. }
  859. //
  860. // Get search results + NPT token
  861. //
  862. foreach($json as $item){
  863. if(isset($item["c"])){
  864. if(
  865. !isset($item["s"]) &&
  866. isset($item["t"]) &&
  867. (
  868. $item["t"] == "DEEP_ERROR_NO_RESULTS" ||
  869. $item["t"] == "DEEP_SIMPLE_NO_RESULTS"
  870. )
  871. ){
  872. return $out;
  873. }
  874. $table = [];
  875. // get youtube video information
  876. if(isset($item["video"]["thumbnail_url_template"])){
  877. $thumb =
  878. [
  879. "ratio" => "16:9",
  880. "url" => $this->bingimg($item["video"]["thumbnail_url_template"])
  881. ];
  882. }else{
  883. $thumb =
  884. [
  885. "ratio" => null,
  886. "url" => null
  887. ];
  888. }
  889. // get table items
  890. if(isset($item["rf"])){
  891. foreach($item["rf"] as $hint){
  892. if(
  893. !isset($hint["label"]["text"]) ||
  894. !isset($hint["items"][0]["text"])
  895. ){
  896. continue;
  897. }
  898. $text = [];
  899. foreach($hint["items"] as $text_part){
  900. $text[] = $text_part["text"];
  901. }
  902. $text = implode(", ", $text);
  903. if(is_numeric($text)){
  904. $text = number_format((string)$text);
  905. }
  906. $table[$hint["label"]["text"]] = $text;
  907. }
  908. }
  909. // get ratings
  910. if(isset($item["ar"])){
  911. foreach($item["ar"] as $rating){
  912. if(
  913. isset($rating["aggregateRating"]["bestRating"]) &&
  914. isset($rating["aggregateRating"]["ratingValue"])
  915. ){
  916. $text = $rating["aggregateRating"]["ratingValue"] . "/" . $rating["aggregateRating"]["bestRating"];
  917. if(isset($rating["aggregateRating"]["reviewCount"])){
  918. $text .= " (" . number_format($rating["aggregateRating"]["reviewCount"]) . " votes)";
  919. }
  920. $table["Rating"] = $text;
  921. }
  922. }
  923. }
  924. // get sublinks
  925. $sublinks = [];
  926. if(isset($item["l"])){
  927. foreach($item["l"] as $sublink){
  928. $sublinks[] = [
  929. "title" => $this->titledots($sublink["text"]),
  930. "description" => $this->titledots($sublink["snippet"]),
  931. "url" => $sublink["targetUrl"],
  932. "date" => null
  933. ];
  934. }
  935. }
  936. $title =
  937. $this->titledots(
  938. $this->fuckhtml
  939. ->getTextContent(
  940. $item["t"]
  941. )
  942. );
  943. if(
  944. $title == "EOF" &&
  945. strpos(
  946. $item["c"],
  947. "google"
  948. )
  949. ){
  950. continue;
  951. }
  952. // parse search result
  953. $out["web"][] = [
  954. "title" =>
  955. $this->titledots(
  956. $this->fuckhtml
  957. ->getTextContent(
  958. $item["t"]
  959. )
  960. ),
  961. "description" =>
  962. isset($item["a"]) ?
  963. $this->titledots(
  964. $this->fuckhtml
  965. ->getTextContent(
  966. $item["a"]
  967. )
  968. ) : null,
  969. "url" => $this->unshiturl($item["c"]),
  970. "date" =>
  971. isset($item["e"]) ?
  972. strtotime($item["e"]) : null,
  973. "type" => "web",
  974. "thumb" => $thumb,
  975. "sublink" => $sublinks,
  976. "table" => $table
  977. ];
  978. continue;
  979. }
  980. if(isset($item["n"])){
  981. // get NPT
  982. $out["npt"] =
  983. $this->backend->store(
  984. "1," . $item["n"],
  985. "web",
  986. $proxy
  987. );
  988. continue;
  989. }
  990. }
  991. //
  992. // Get spelling
  993. //
  994. $js_tmp =
  995. preg_split(
  996. '/DDG\.page\.showMessage\(\s*\'spelling\'\s*,\s*/',
  997. $js,
  998. 2
  999. );
  1000. if(count($js_tmp) > 1){
  1001. $json =
  1002. json_decode(
  1003. $this->fuckhtml
  1004. ->extract_json(
  1005. $js_tmp[1]
  1006. ),
  1007. true
  1008. );
  1009. if($json !== null){
  1010. // parse spelling
  1011. // qc=2: including
  1012. switch((int)$json["qc"]){
  1013. case 2:
  1014. $type = "including";
  1015. break;
  1016. default:
  1017. $type = "not_many";
  1018. break;
  1019. }
  1020. $out["spelling"] = [
  1021. "type" => $type,
  1022. "using" =>
  1023. $this->fuckhtml
  1024. ->getTextContent(
  1025. $json["suggestion"]
  1026. ),
  1027. "correction" => html_entity_decode($json["recourseText"])
  1028. ];
  1029. }
  1030. }
  1031. //
  1032. // Get images
  1033. //
  1034. $js_tmp =
  1035. preg_split(
  1036. '/DDG\.duckbar\.load\(\s*\'images\'\s*,\s*/',
  1037. $js,
  1038. 2
  1039. );
  1040. if(count($js_tmp) > 1){
  1041. $json =
  1042. json_decode(
  1043. $this->fuckhtml
  1044. ->extract_json(
  1045. $js_tmp[1]
  1046. ),
  1047. true
  1048. );
  1049. if($json !== null){
  1050. foreach($json["results"] as $image){
  1051. $ratio = $this->bingratio((int)$image["width"], (int)$image["height"]);
  1052. $out["image"][] = [
  1053. "title" => $image["title"],
  1054. "source" => [
  1055. [
  1056. "url" => $image["image"],
  1057. "width" => (int)$image["width"],
  1058. "height" => (int)$image["height"]
  1059. ],
  1060. [
  1061. "url" => $this->bingimg($image["thumbnail"]),
  1062. "width" => $ratio[0],
  1063. "height" => $ratio[1]
  1064. ]
  1065. ],
  1066. "url" => $this->unshiturl($image["url"])
  1067. ];
  1068. }
  1069. }
  1070. }
  1071. //
  1072. // Get videos
  1073. //
  1074. $js_tmp =
  1075. preg_split(
  1076. '/DDG\.duckbar\.load\(\s*\'videos\'\s*,\s*/',
  1077. $js,
  1078. 2
  1079. );
  1080. if(count($js_tmp) > 1){
  1081. $json =
  1082. json_decode(
  1083. $this->fuckhtml
  1084. ->extract_json(
  1085. $js_tmp[1]
  1086. ),
  1087. true
  1088. );
  1089. if($json !== null){
  1090. foreach($json["results"] as $video){
  1091. $thumb = [
  1092. "ratio" => null,
  1093. "url" => null
  1094. ];
  1095. foreach(["large", "medium", "small"] as $contender){
  1096. if(isset($video["images"][$contender])){
  1097. $thumb = [
  1098. "ratio" => "16:9",
  1099. "url" => $this->bingimg($video["images"][$contender])
  1100. ];
  1101. break;
  1102. }
  1103. }
  1104. $out["video"][] = [
  1105. "title" => $this->titledots($video["title"]),
  1106. "description" =>
  1107. $video["description"] != "" ?
  1108. $this->titledots($video["description"]) : null,
  1109. "date" =>
  1110. isset($video["published"]) ?
  1111. strtotime($video["published"]) : null,
  1112. "duration" =>
  1113. $video["duration"] != "" ?
  1114. $this->hms2int($video["duration"]) : null,
  1115. "views" =>
  1116. isset($video["statistics"]["viewCount"]) ?
  1117. (int)$video["statistics"]["viewCount"] : null,
  1118. "thumb" => $thumb,
  1119. "url" => $this->unshiturl($video["content"])
  1120. ];
  1121. }
  1122. }
  1123. }
  1124. //
  1125. // Get news
  1126. //
  1127. $js_tmp =
  1128. preg_split(
  1129. '/DDG\.duckbar\.load\(\s*\'news\'\s*,\s*/',
  1130. $js,
  1131. 2
  1132. );
  1133. if(count($js_tmp) > 1){
  1134. $json =
  1135. json_decode(
  1136. $this->fuckhtml
  1137. ->extract_json(
  1138. $js_tmp[1]
  1139. ),
  1140. true
  1141. );
  1142. if($json !== null){
  1143. foreach($json["results"] as $news){
  1144. if(isset($news["image"])){
  1145. $thumb = [
  1146. "ratio" => "16:9",
  1147. "url" => $news["image"]
  1148. ];
  1149. }else{
  1150. $thumb = [
  1151. "ratio" => null,
  1152. "url" => null
  1153. ];
  1154. }
  1155. $out["news"][] = [
  1156. "title" => $news["title"],
  1157. "description" =>
  1158. $this->fuckhtml
  1159. ->getTextContent(
  1160. $news["excerpt"]
  1161. ),
  1162. "date" => (int)$news["date"],
  1163. "thumb" => $thumb,
  1164. "url" => $news["url"]
  1165. ];
  1166. }
  1167. }
  1168. }
  1169. //
  1170. // Get related searches
  1171. //
  1172. $js_tmp =
  1173. preg_split(
  1174. '/DDG\.duckbar\.loadModule\(\s*\'related_searches\'\s*,\s*/',
  1175. $js,
  1176. 2
  1177. );
  1178. if(count($js_tmp) > 1){
  1179. $json =
  1180. json_decode(
  1181. $this->fuckhtml
  1182. ->extract_json(
  1183. $js_tmp[1]
  1184. ),
  1185. true
  1186. );
  1187. if($json !== null){
  1188. foreach($json["results"] as $related){
  1189. $out["related"][] = $related["text"];
  1190. }
  1191. }
  1192. }
  1193. //
  1194. // Get instant answers
  1195. //
  1196. $js_tmp =
  1197. preg_split(
  1198. '/DDG\.duckbar\.add\(\s*/',
  1199. $html . $js,
  1200. 2
  1201. );
  1202. if(count($js_tmp) > 1){
  1203. $json =
  1204. json_decode(
  1205. $this->fuckhtml
  1206. ->extract_json(
  1207. $js_tmp[1]
  1208. ),
  1209. true
  1210. );
  1211. if($json !== null){
  1212. $json = $json["data"];
  1213. $table = [];
  1214. $sublinks = [];
  1215. $description = [];
  1216. // get official website
  1217. if(
  1218. isset($json["OfficialWebsite"]) &&
  1219. $json["OfficialWebsite"] !== null
  1220. ){
  1221. $sublinks["Website"] = $json["OfficialWebsite"];
  1222. }
  1223. // get sublinks & table elements
  1224. if(isset($json["Infobox"]["content"])){
  1225. foreach($json["Infobox"]["content"] as $info){
  1226. if($info["data_type"] == "string"){
  1227. // add table element
  1228. $table[$info["label"]] = $info["value"];
  1229. continue;
  1230. }
  1231. if($info["data_type"] == "wd_description"){
  1232. $description[] = [
  1233. "type" => "quote",
  1234. "value" => $info["value"]
  1235. ];
  1236. continue;
  1237. }
  1238. // add sublink
  1239. switch($info["data_type"]){
  1240. case "official_site":
  1241. case "official_website":
  1242. $type = "Website";
  1243. break;
  1244. case "wikipedia": $type = "Wikipedia"; break;
  1245. case "itunes": $type = "iTunes"; break;
  1246. case "amazon": $type = "Amazon"; break;
  1247. case "imdb_title_id":
  1248. case "imdb_id":
  1249. case "imdb_name_id":
  1250. $type = "IMDb";
  1251. $delim = substr($info["value"], 0, 2);
  1252. if($delim == "nm"){
  1253. $prefix = "https://www.imdb.com/name/";
  1254. }elseif($delim == "tt"){
  1255. $prefix = "https://www.imdb.com/title/";
  1256. }elseif($delim == "co"){
  1257. $prefix = "https://www.imdb.com/search/title/?companies=";
  1258. }else{
  1259. $prefix = "https://www.imdb.com/title/";
  1260. }
  1261. break;
  1262. case "imdb_name_id": $prefix = "https://www.imdb.com/name/"; $type = "IMDb"; break;
  1263. case "twitter_profile": $prefix = "https://twitter.com/"; $type = "Twitter"; break;
  1264. case "instagram_profile": $prefix = "https://instagram.com/"; $type = "Instagram"; break;
  1265. case "facebook_profile": $prefix = "https://facebook.com/"; $type = "Facebook"; break;
  1266. case "spotify_artist_id": $prefix = "https://open.spotify.com/artist/"; $type = "Spotify"; break;
  1267. case "itunes_artist_id": $prefix = "https://music.apple.com/us/artist/"; $type = "iTunes"; break;
  1268. case "rotten_tomatoes": $prefix = "https://rottentomatoes.com/"; $type = "Rotten Tomatoes"; break;
  1269. case "youtube_channel": $prefix = "https://youtube.com/channel/"; $type = "YouTube"; break;
  1270. case "soundcloud_id": $prefix = "https://soundcloud.com/"; $type = "SoundCloud"; break;
  1271. default:
  1272. $prefix = null;
  1273. $type = false;
  1274. }
  1275. if($type !== false){
  1276. if($prefix === null){
  1277. $sublinks[$type] = $info["value"];
  1278. }else{
  1279. $sublinks[$type] = $prefix . $info["value"];
  1280. }
  1281. }
  1282. }
  1283. }
  1284. if(isset($json["Abstract"])){
  1285. $description = $this->parse_rich_text($json["Abstract"]);
  1286. }
  1287. if(
  1288. !isset($json["Image"]) ||
  1289. $json["Image"] == "" ||
  1290. $json["Image"] === null ||
  1291. $json["Image"] == "https://duckduckgo.com/i/"
  1292. ){
  1293. $image = null;
  1294. }else{
  1295. if(
  1296. preg_match(
  1297. '/^https?:\/\//',
  1298. $json["Image"]
  1299. )
  1300. ){
  1301. $image = $json["Image"];
  1302. }else{
  1303. $image = "https://duckduckgo.com" . $json["Image"];
  1304. }
  1305. }
  1306. $out["answer"][] = [
  1307. "title" => $json["Heading"],
  1308. "description" => $description,
  1309. "url" => $json["AbstractURL"],
  1310. "thumb" => $image,
  1311. "table" => $table,
  1312. "sublink" => $sublinks
  1313. ];
  1314. }
  1315. }
  1316. if($get["extendedsearch"] == "no"){
  1317. return $out;
  1318. }
  1319. //
  1320. // Parse additional data endpoints
  1321. //
  1322. //nrj('/js/spice/dictionary/definition/create', null, null, null, null, 'dictionary_definition');
  1323. preg_match_all(
  1324. '/nrj\(\s*\'([^\']+)\'/',
  1325. $js,
  1326. $nrj
  1327. );
  1328. if(isset($nrj[1])){
  1329. foreach($nrj[1] as $potential_endpoint){
  1330. //
  1331. // Probe for wordnik definition
  1332. //
  1333. preg_match(
  1334. '/\/js\/spice\/dictionary\/definition\/([^\/]+)/',
  1335. $potential_endpoint,
  1336. $word
  1337. );
  1338. if(isset($word[1])){
  1339. $word = $word[1];
  1340. // found wordnik definition & word
  1341. try{
  1342. $nik =
  1343. $this->get(
  1344. $proxy,
  1345. "https://duckduckgo.com/js/spice/dictionary/definition/" . $word,
  1346. [],
  1347. ddg::req_xhr
  1348. );
  1349. }catch(Exception $e){
  1350. // fail gracefully
  1351. return $out;
  1352. }
  1353. // remove javascript
  1354. $js_tmp =
  1355. preg_split(
  1356. '/ddg_spice_dictionary_definition\(\s*/',
  1357. $nik,
  1358. 2
  1359. );
  1360. if(count($js_tmp) > 1){
  1361. $nik =
  1362. json_decode(
  1363. $this->fuckhtml
  1364. ->extract_json(
  1365. $js_tmp[1]
  1366. ),
  1367. true
  1368. );
  1369. }
  1370. if($nik === null){
  1371. return $out;
  1372. }
  1373. $answer_cat = [];
  1374. $answer = [];
  1375. foreach($nik as $snippet){
  1376. if(!isset($snippet["partOfSpeech"])){ continue; }
  1377. $push = [];
  1378. // add text snippet
  1379. if(isset($snippet["text"])){
  1380. $push[] = [
  1381. "type" => "text",
  1382. "value" =>
  1383. $this->fuckhtml
  1384. ->getTextContent(
  1385. $snippet["text"]
  1386. )
  1387. ];
  1388. }
  1389. // add example uses
  1390. if(isset($snippet["exampleUses"])){
  1391. foreach($snippet["exampleUses"] as $example){
  1392. $push[] = [
  1393. "type" => "quote",
  1394. "value" => "\"" .
  1395. $this->fuckhtml
  1396. ->getTextContent(
  1397. $example["text"]
  1398. ) . "\""
  1399. ];
  1400. }
  1401. }
  1402. // add citations
  1403. if(isset($snippet["citations"])){
  1404. foreach($snippet["citations"] as $citation){
  1405. if(!isset($citation["cite"])){ continue; }
  1406. $text =
  1407. $this->fuckhtml
  1408. ->getTextContent(
  1409. $citation["cite"]
  1410. );
  1411. if(isset($citation["source"])){
  1412. $text .=
  1413. " - " .
  1414. $this->fuckhtml
  1415. ->getTextContent(
  1416. $citation["source"]
  1417. );
  1418. }
  1419. $push[] = [
  1420. "type" => "quote",
  1421. "value" => $text
  1422. ];
  1423. }
  1424. }
  1425. // add related words
  1426. if(isset($snippet["relatedWords"])){
  1427. $relations = [];
  1428. foreach($snippet["relatedWords"] as $related){
  1429. $words = [];
  1430. foreach($related["words"] as $wrd){
  1431. $words[] =
  1432. $this->fuckhtml
  1433. ->getTextContent(
  1434. $wrd
  1435. );
  1436. }
  1437. if(
  1438. count($words) !== 0 &&
  1439. isset($related["relationshipType"])
  1440. ){
  1441. $relations[ucfirst($related["relationshipType"]) . "s"] =
  1442. implode(", ", $words);
  1443. }
  1444. }
  1445. foreach($relations as $relation_title => $relation_content){
  1446. $push[] = [
  1447. "type" => "quote",
  1448. "value" => $relation_title . ": " . $relation_content
  1449. ];
  1450. }
  1451. }
  1452. if(count($push) !== 0){
  1453. // push data to answer_cat
  1454. if(!isset($answer_cat[$snippet["partOfSpeech"]])){
  1455. $answer_cat[$snippet["partOfSpeech"]] = [];
  1456. }
  1457. $answer_cat[$snippet["partOfSpeech"]] =
  1458. array_merge(
  1459. $answer_cat[$snippet["partOfSpeech"]],
  1460. $push
  1461. );
  1462. }
  1463. }
  1464. foreach($answer_cat as $answer_title => $answer_content){
  1465. $i = 0;
  1466. $answer[] = [
  1467. "type" => "title",
  1468. "value" => $answer_title
  1469. ];
  1470. $old_type = $answer[count($answer) - 1]["type"];
  1471. foreach($answer_content as $ans){
  1472. if(
  1473. $ans["type"] == "text" &&
  1474. $old_type == "text"
  1475. ){
  1476. $i++;
  1477. $c = count($answer) - 1;
  1478. // append text to existing textfield
  1479. $answer[$c] = [
  1480. "type" => "text",
  1481. "value" => $answer[$c]["value"] . "\n" . $i . ". " . $ans["value"]
  1482. ];
  1483. }elseif($ans["type"] == "text"){
  1484. $i++;
  1485. $answer[] = [
  1486. "type" => "text",
  1487. "value" => $i . ". " . $ans["value"]
  1488. ];
  1489. }else{
  1490. // append normally
  1491. $answer[] = $ans;
  1492. }
  1493. $old_type = $ans["type"];
  1494. }
  1495. }
  1496. // yeah.. sometimes duckduckgo doesnt give us a definition back
  1497. if(count($answer) !== 0){
  1498. $out["answer"][] = [
  1499. "title" => ucfirst($word),
  1500. "description" => $answer,
  1501. "url" => "https://www.wordnik.com/words/" . $word,
  1502. "thumb" => null,
  1503. "table" => [],
  1504. "sublink" => []
  1505. ];
  1506. }
  1507. }
  1508. //
  1509. // Parse stackoverflow answer
  1510. //
  1511. if(
  1512. preg_match(
  1513. '/^\/a\.js.*src_id=stack_overflow/',
  1514. $potential_endpoint
  1515. )
  1516. ){
  1517. // found stackoverflow answer
  1518. try{
  1519. $json =
  1520. $this->get(
  1521. $proxy,
  1522. "https://duckduckgo.com" . $potential_endpoint,
  1523. [],
  1524. ddg::req_xhr
  1525. );
  1526. }catch(Exception $e){
  1527. // fail gracefully
  1528. return $out;
  1529. }
  1530. $json = explode("DDG.duckbar.add_array(", $json, 2);
  1531. if(count($json) === 2){
  1532. $json =
  1533. json_decode(
  1534. $this->fuckhtml
  1535. ->extract_json(
  1536. $json[1]
  1537. ),
  1538. true
  1539. );
  1540. if(
  1541. $json !== null &&
  1542. isset($json[0]["data"])
  1543. ){
  1544. $json = $json[0]["data"];
  1545. foreach($json as $answer){
  1546. if(isset($answer["Heading"])){
  1547. $title = $answer["Heading"];
  1548. }elseif(isset($answer["title"])){
  1549. $title = $answer["title"];
  1550. }else{
  1551. $title = null;
  1552. }
  1553. if(
  1554. $title !== null &&
  1555. isset($answer["Abstract"])
  1556. ){
  1557. $description = $this->parse_rich_text($answer["Abstract"]);
  1558. $out["answer"][] = [
  1559. "title" => $title,
  1560. "description" => $description,
  1561. "url" => $answer["AbstractURL"],
  1562. "thumb" => null,
  1563. "table" => [],
  1564. "sublink" => []
  1565. ];
  1566. }
  1567. }
  1568. }
  1569. }
  1570. }
  1571. }
  1572. }
  1573. return $out;
  1574. }
  1575. public function image($get){
  1576. if($get["npt"]){
  1577. [$js_link, $proxy] = $this->backend->get($get["npt"], "images");
  1578. }else{
  1579. if(strlen($get["s"]) === 0){
  1580. throw new Exception("Search term is empty!");
  1581. }
  1582. $proxy = $this->backend->get_ip();
  1583. $filters = [];
  1584. if($get["date"] != "any"){ $filters[] = "time:{$get["date"]}"; }
  1585. if($get["size"] != "any"){ $filters[] = "size:{$get["size"]}"; }
  1586. if($get["color"] != "any"){ $filters[] = "color:{$get["color"]}"; }
  1587. if($get["type"] != "any"){ $filters[] = "type:{$get["type"]}"; }
  1588. if($get["layout"] != "any"){ $filters[] = "layout:{$get["layout"]}"; }
  1589. if($get["license"] != "any"){ $filters[] = "license:{$get["license"]}"; }
  1590. $filters = implode(",", $filters);
  1591. $get_filters = [
  1592. "q" => $get["s"],
  1593. "iax" => "images",
  1594. "ia" => "images"
  1595. ];
  1596. if($filters != ""){
  1597. $get_filters["iaf"] = $filters;
  1598. }
  1599. $nsfw = $get["nsfw"] == "yes" ? "-1" : "1";
  1600. $get_filters["kp"] = $nsfw;
  1601. try{
  1602. $html = $this->get(
  1603. $proxy,
  1604. "https://duckduckgo.com",
  1605. $get_filters,
  1606. ddg::req_web
  1607. );
  1608. }catch(Exception $err){
  1609. throw new Exception("Failed to fetch search page");
  1610. }
  1611. preg_match(
  1612. '/vqd="([0-9-]+)"/',
  1613. $html,
  1614. $vqd
  1615. );
  1616. if(!isset($vqd[1])){
  1617. throw new Exception("Failed to grep VQD token");
  1618. }
  1619. $js_link =
  1620. "i.js?" .
  1621. http_build_query([
  1622. "l" => $get["country"],
  1623. "o" => "json",
  1624. "q" => $get["s"],
  1625. "vqd" => $vqd[1],
  1626. "f" => $filters,
  1627. "p" => $nsfw
  1628. ]);
  1629. }
  1630. try{
  1631. $json =
  1632. $this->get(
  1633. $proxy,
  1634. "https://duckduckgo.com/" . $js_link,
  1635. [],
  1636. ddg::req_xhr
  1637. );
  1638. }catch(Exception $error){
  1639. throw new Exception("Failed to get i.js");
  1640. }
  1641. $json = json_decode($json, true);
  1642. if($json === null){
  1643. throw new Exception("Failed to decode JSON");
  1644. }
  1645. $out = [
  1646. "status" => "ok",
  1647. "npt" => null,
  1648. "image" => []
  1649. ];
  1650. if(!isset($json["results"])){
  1651. return $out;
  1652. }
  1653. // get npt
  1654. if(
  1655. isset($json["next"]) &&
  1656. $json["next"] !== null
  1657. ){
  1658. $vqd = null;
  1659. if(isset($vqd[1])){
  1660. $vqd = $vqd[1];
  1661. }else{
  1662. $vqd = array_values($json["vqd"]);
  1663. if(count($vqd) > 0){
  1664. $vqd = $vqd[0];
  1665. }
  1666. }
  1667. if($vqd !== null){
  1668. $out["npt"] =
  1669. $this->backend->store(
  1670. $json["next"] . "&vqd=" . $vqd,
  1671. "images",
  1672. $proxy
  1673. );
  1674. }
  1675. }
  1676. // get images
  1677. foreach($json["results"] as $image){
  1678. $ratio =
  1679. $this->bingratio(
  1680. (int)$image["width"],
  1681. (int)$image["height"]
  1682. );
  1683. $out["image"][] = [
  1684. "title" => $this->titledots($image["title"]),
  1685. "source" => [
  1686. [
  1687. "url" => $image["image"],
  1688. "width" => (int)$image["width"],
  1689. "height" => (int)$image["height"]
  1690. ],
  1691. [
  1692. "url" => $this->bingimg($image["thumbnail"]),
  1693. "width" => $ratio[0],
  1694. "height" => $ratio[1]
  1695. ]
  1696. ],
  1697. "url" => $this->unshiturl($image["url"])
  1698. ];
  1699. }
  1700. return $out;
  1701. }
  1702. public function video($get){
  1703. if($get["npt"]){
  1704. [$js_link, $proxy] = $this->backend->get($get["npt"], "videos");
  1705. }else{
  1706. if(strlen($get["s"]) === 0){
  1707. throw new Exception("Search term is empty!");
  1708. }
  1709. $proxy = $this->backend->get_ip();
  1710. $get_filters = [
  1711. "q" => $get["s"],
  1712. "iax" => "videos",
  1713. "ia" => "videos"
  1714. ];
  1715. switch($get["nsfw"]){
  1716. case "yes": $nsfw = "-2"; break;
  1717. case "maybe": $nsfw = "-1"; break;
  1718. case "no": $nsfw = "1"; break;
  1719. }
  1720. $filters = [];
  1721. if($get["date"] != "any"){ $filters[] = "publishedAfter:{$date}"; }
  1722. if($get["resolution"] != "any"){ $filters[] = "videoDefinition:{$resolution}"; }
  1723. if($get["duration"] != "any"){ $filters[] = "videoDuration:{$duration}"; }
  1724. if($get["license"] != "any"){ $filters[] = "videoLicense:{$license}"; }
  1725. $filters = implode(",", $filters);
  1726. if($filters != ""){
  1727. $get_filters["iaf"] = $filters;
  1728. }
  1729. try{
  1730. $html =
  1731. $this->get(
  1732. $proxy,
  1733. "https://duckduckgo.com/",
  1734. $get_filters,
  1735. ddg::req_web
  1736. );
  1737. }catch(Exception $error){
  1738. throw new Exception("Failed to fetch search page");
  1739. }
  1740. preg_match(
  1741. '/vqd="([0-9-]+)"/',
  1742. $html,
  1743. $vqd
  1744. );
  1745. if(!isset($vqd[1])){
  1746. throw new Exception("Failed to grep VQD token");
  1747. }
  1748. $js_link =
  1749. "v.js?" .
  1750. http_build_query([
  1751. "l" => $get["country"],
  1752. "o" => "json",
  1753. "sr" => "1",
  1754. "q" => $get["s"],
  1755. "vqd" => $vqd[1],
  1756. "f" => $filters,
  1757. "p" => $nsfw
  1758. ]);
  1759. }
  1760. try{
  1761. $json =
  1762. $this->get(
  1763. $proxy,
  1764. "https://duckduckgo.com/" . $js_link,
  1765. [],
  1766. ddg::req_xhr
  1767. );
  1768. }catch(Exception $error){
  1769. throw new Exception("Failed to fetch JSON");
  1770. }
  1771. $json = json_decode($json, true);
  1772. if($json === null){
  1773. throw new Exception("Failed to decode JSON");
  1774. }
  1775. $out = [
  1776. "status" => "ok",
  1777. "npt" => null,
  1778. "video" => [],
  1779. "author" => [],
  1780. "livestream" => [],
  1781. "playlist" => [],
  1782. "reel" => []
  1783. ];
  1784. if(!isset($json["results"])){
  1785. return $out;
  1786. }
  1787. // get NPT
  1788. if(
  1789. isset($json["next"]) &&
  1790. $json["next"] !== null
  1791. ){
  1792. $out["npt"] =
  1793. $this->backend->store(
  1794. $json["next"],
  1795. "videos",
  1796. $proxy
  1797. );
  1798. }
  1799. foreach($json["results"] as $video){
  1800. $thumb = [
  1801. "ratio" => null,
  1802. "url" => null
  1803. ];
  1804. foreach(["large", "medium", "small"] as $contender){
  1805. if(isset($video["images"][$contender])){
  1806. $thumb = [
  1807. "ratio" => "16:9",
  1808. "url" => $this->bingimg($video["images"][$contender])
  1809. ];
  1810. break;
  1811. }
  1812. }
  1813. $out["video"][] = [
  1814. "title" => $this->titledots($video["title"]),
  1815. "description" => $this->titledots($video["description"]),
  1816. "author" => [
  1817. "name" =>
  1818. (
  1819. isset($video["uploader"]) &&
  1820. $video["uploader"] != ""
  1821. ) ?
  1822. $video["uploader"] : null,
  1823. "url" => null,
  1824. "avatar" => null
  1825. ],
  1826. "date" =>
  1827. (
  1828. isset($video["published"]) &&
  1829. $video["published"] != ""
  1830. ) ?
  1831. strtotime($video["published"]) : null,
  1832. "duration" =>
  1833. (
  1834. isset($video["duration"]) &&
  1835. $video["duration"] != ""
  1836. ) ?
  1837. $this->hms2int($video["duration"]) : null,
  1838. "views" =>
  1839. isset($video["statistics"]["viewCount"]) ?
  1840. (int)$video["statistics"]["viewCount"] : null,
  1841. "thumb" => $thumb,
  1842. "url" => $this->unshiturl($video["content"])
  1843. ];
  1844. }
  1845. return $out;
  1846. }
  1847. public function news($get){
  1848. if($get["npt"]){
  1849. [$js_link, $proxy] = $this->backend->get($get["npt"], "news");
  1850. }else{
  1851. if(strlen($get["s"]) === 0){
  1852. throw new Exception("Search term is empty!");
  1853. }
  1854. $proxy = $this->backend->get_ip();
  1855. $get_filters = [
  1856. "q" => $get["s"],
  1857. "iar" => "news",
  1858. "ia" => "news"
  1859. ];
  1860. if($get["date"] != "any"){
  1861. $date = $get["date"];
  1862. $get_filters["df"] = $date;
  1863. }else{
  1864. $date = "";
  1865. }
  1866. switch($get["nsfw"]){
  1867. case "yes": $get_filters["kp"] = "-2"; break;
  1868. case "maybe": $get_filters["kp"] = "-1"; break;
  1869. case "no": $get_filters["kp"] = "1"; break;
  1870. }
  1871. try{
  1872. $html =
  1873. $this->get(
  1874. $proxy,
  1875. "https://duckduckgo.com/",
  1876. $get_filters,
  1877. ddg::req_web
  1878. );
  1879. }catch(Exception $error){
  1880. throw new Exception("Failed to fetch search page");
  1881. }
  1882. preg_match(
  1883. '/vqd="([0-9-]+)"/',
  1884. $html,
  1885. $vqd
  1886. );
  1887. if(!isset($vqd[1])){
  1888. throw new Exception("Failed to grep VQD token");
  1889. }
  1890. $js_link =
  1891. "news.js?" .
  1892. http_build_query([
  1893. "l" => $get["country"],
  1894. "o" => "json",
  1895. "noamp" => "1",
  1896. "m" => "30",
  1897. "q" => $get["s"],
  1898. "vqd" => $vqd[1],
  1899. "p" => $get_filters["kp"],
  1900. "df" => $date,
  1901. "u" => "bing"
  1902. ]);
  1903. }
  1904. try{
  1905. $json =
  1906. $this->get(
  1907. $proxy,
  1908. "https://duckduckgo.com/" . $js_link,
  1909. [],
  1910. ddg::req_xhr
  1911. );
  1912. }catch(Exception $error){
  1913. throw new Exception("Failed to fetch JSON");
  1914. }
  1915. $json = json_decode($json, true);
  1916. if($json === null){
  1917. throw new Exception("Failed to decode JSON");
  1918. }
  1919. $out = [
  1920. "status" => "ok",
  1921. "npt" => null,
  1922. "news" => []
  1923. ];
  1924. if(!isset($json["results"])){
  1925. return $out;
  1926. }
  1927. // get NPT
  1928. if(
  1929. isset($json["next"]) &&
  1930. $json["next"] !== null
  1931. ){
  1932. $out["npt"] =
  1933. $this->backend->store(
  1934. $json["next"],
  1935. "news",
  1936. $proxy
  1937. );
  1938. }
  1939. foreach($json["results"] as $news){
  1940. if(
  1941. isset($news["image"]) &&
  1942. $news["image"] != ""
  1943. ){
  1944. $thumb = [
  1945. "ratio" => "16:9",
  1946. "url" => $news["image"]
  1947. ];
  1948. }else{
  1949. $thumb = [
  1950. "ratio" => null,
  1951. "url" => null
  1952. ];
  1953. }
  1954. $out["news"][] = [
  1955. "title" => $news["title"],
  1956. "author" =>
  1957. (
  1958. isset($news["source"]) &&
  1959. $news["source"] != ""
  1960. ) ?
  1961. $news["source"] : null,
  1962. "description" =>
  1963. (
  1964. isset($news["excerpt"]) &&
  1965. $news["excerpt"] != ""
  1966. ) ?
  1967. $this->fuckhtml
  1968. ->getTextContent(
  1969. $news["excerpt"]
  1970. ) : null,
  1971. "date" =>
  1972. isset($news["date"]) ?
  1973. (int)$news["date"] : null,
  1974. "thumb" => $thumb,
  1975. "url" => $this->unshiturl($news["url"])
  1976. ];
  1977. }
  1978. return $out;
  1979. }
  1980. private function parse_rich_text($html){
  1981. $description = [];
  1982. // pre-process the html, remove useless elements
  1983. $html =
  1984. strip_tags(
  1985. $html,
  1986. [
  1987. "h1", "h2", "h3", "h4", "h5", "h6", "h7",
  1988. "pre", "code"
  1989. ]
  1990. );
  1991. $html =
  1992. preg_replace(
  1993. '/<(\/?)pre *[^>]*>\s*<\/?code *[^>]*>/i',
  1994. '<$1pre>',
  1995. $html
  1996. );
  1997. $this->fuckhtml->load($html);
  1998. $tags =
  1999. $this->fuckhtml
  2000. ->getElementsByTagName(
  2001. "*"
  2002. );
  2003. if(count($tags) === 0){
  2004. $description[] = [
  2005. "type" => "text",
  2006. "value" =>
  2007. trim(
  2008. $this->fuckhtml
  2009. ->getTextContent(
  2010. $html,
  2011. true,
  2012. false
  2013. )
  2014. )
  2015. ];
  2016. }else{
  2017. $start = 0;
  2018. $was_code_block = true;
  2019. foreach($tags as $tag){
  2020. $text =
  2021. $this->fuckhtml
  2022. ->getTextContent(
  2023. substr(
  2024. $html,
  2025. $start,
  2026. $tag["startPos"] - $start
  2027. ),
  2028. true,
  2029. false
  2030. );
  2031. if($was_code_block){
  2032. $text = ltrim($text);
  2033. $was_code_block = false;
  2034. }
  2035. $description[] = [
  2036. "type" => "text",
  2037. "value" => $text
  2038. ];
  2039. switch($tag["tagName"]){
  2040. case "pre":
  2041. $append = "code";
  2042. $was_code_block = true;
  2043. $c = count($description) - 1;
  2044. $description[$c]["value"] =
  2045. rtrim($description[$c]["value"]);
  2046. break;
  2047. case "code":
  2048. $append = "inline_code";
  2049. $c = count($description) - 1;
  2050. $description[$c]["value"] =
  2051. rtrim($description[$c]["value"]) . " ";
  2052. break;
  2053. case "h1":
  2054. case "h2":
  2055. case "h3":
  2056. case "h4":
  2057. case "h5":
  2058. case "h6":
  2059. case "h7":
  2060. $append = "title";
  2061. $c = count($description) - 1;
  2062. $description[$c]["value"] =
  2063. rtrim($description[$c]["value"]);
  2064. break;
  2065. }
  2066. $description[] = [
  2067. "type" => $append,
  2068. "value" =>
  2069. trim(
  2070. $this->fuckhtml
  2071. ->getTextContent(
  2072. $tag,
  2073. true,
  2074. false
  2075. )
  2076. )
  2077. ];
  2078. $start = $tag["endPos"];
  2079. }
  2080. // shit out remainder
  2081. $description[] = [
  2082. "type" => "text",
  2083. "value" =>
  2084. trim(
  2085. $this->fuckhtml
  2086. ->getTextContent(
  2087. substr(
  2088. $html,
  2089. $start
  2090. ),
  2091. true,
  2092. false
  2093. )
  2094. )
  2095. ];
  2096. }
  2097. return $description;
  2098. }
  2099. private function titledots($title){
  2100. $substr = substr($title, -3);
  2101. if(
  2102. $substr == "..." ||
  2103. $substr == "…"
  2104. ){
  2105. return trim(substr($title, 0, -3));
  2106. }
  2107. return trim($title);
  2108. }
  2109. private function hms2int($time){
  2110. $parts = explode(":", $time, 3);
  2111. $time = 0;
  2112. if(count($parts) === 3){
  2113. // hours
  2114. $time = $time + ((int)$parts[0] * 3600);
  2115. array_shift($parts);
  2116. }
  2117. if(count($parts) === 2){
  2118. // minutes
  2119. $time = $time + ((int)$parts[0] * 60);
  2120. array_shift($parts);
  2121. }
  2122. // seconds
  2123. $time = $time + (int)$parts[0];
  2124. return $time;
  2125. }
  2126. private function unshiturl($url){
  2127. // remove tracking redirect
  2128. // yes, the privacy search engine has click-out tracking. great!
  2129. $domain = parse_url($url, PHP_URL_HOST);
  2130. if($domain == "duckduckgo.com"){
  2131. $query = parse_url($url, PHP_URL_QUERY);
  2132. parse_str($query, $query);
  2133. if(isset($query["uddg"])){
  2134. $url = $query["uddg"];
  2135. $domain = parse_url($url, PHP_URL_HOST);
  2136. }
  2137. }
  2138. // check for domains w/out first short subdomain (ex: www.)
  2139. $subdomain = preg_replace(
  2140. '/^[A-z0-9]{1,3}\./',
  2141. "",
  2142. $domain
  2143. );
  2144. switch($subdomain){
  2145. case "ebay.com.au":
  2146. case "ebay.at":
  2147. case "ebay.ca":
  2148. case "ebay.fr":
  2149. case "ebay.de":
  2150. case "ebay.com.hk":
  2151. case "ebay.ie":
  2152. case "ebay.it":
  2153. case "ebay.com.my":
  2154. case "ebay.nl":
  2155. case "ebay.ph":
  2156. case "ebay.pl":
  2157. case "ebay.com.sg":
  2158. case "ebay.es":
  2159. case "ebay.ch":
  2160. case "ebay.co.uk":
  2161. case "cafr.ebay.ca":
  2162. case "ebay.com":
  2163. case "community.ebay.com":
  2164. case "pages.ebay.com":
  2165. // remove ebay tracking elements
  2166. $old_params = parse_url($url, PHP_URL_QUERY);
  2167. parse_str($old_params, $params);
  2168. if(isset($params["mkevt"])){ unset($params["mkevt"]); }
  2169. if(isset($params["mkcid"])){ unset($params["mkcid"]); }
  2170. if(isset($params["mkrid"])){ unset($params["mkrid"]); }
  2171. if(isset($params["campid"])){ unset($params["campid"]); }
  2172. if(isset($params["customid"])){ unset($params["customid"]); }
  2173. if(isset($params["toolid"])){ unset($params["toolid"]); }
  2174. if(isset($params["_sop"])){ unset($params["_sop"]); }
  2175. if(isset($params["_dcat"])){ unset($params["_dcat"]); }
  2176. if(isset($params["epid"])){ unset($params["epid"]); }
  2177. if(isset($params["epid"])){ unset($params["oid"]); }
  2178. $params = http_build_query($params);
  2179. if(strlen($params) === 0){
  2180. $replace = "\?";
  2181. }else{
  2182. $replace = "";
  2183. }
  2184. $url = preg_replace(
  2185. "/" . $replace . preg_quote($old_params, "/") . "$/",
  2186. $params,
  2187. $url
  2188. );
  2189. break;
  2190. }
  2191. return $url;
  2192. }
  2193. private function bingimg($url){
  2194. $image = parse_url($url);
  2195. $id = null;
  2196. if(isset($image["query"])){
  2197. parse_str($image["query"], $str);
  2198. if(isset($str["id"])){
  2199. $id = $str["id"];
  2200. }
  2201. }
  2202. if($id === null){
  2203. $id = explode("/th/id/", $image["path"], 2);
  2204. if(count($id) !== 2){
  2205. // malformed
  2206. return $url;
  2207. }
  2208. $id = $id[1];
  2209. }
  2210. return "https://" . $image["host"] . "/th?id=" . rawurlencode($id);
  2211. }
  2212. private function bingratio($width, $height){
  2213. $ratio = [
  2214. 474 / $width,
  2215. 474 / $height
  2216. ];
  2217. if($ratio[0] < $ratio[1]){
  2218. $ratio = $ratio[0];
  2219. }else{
  2220. $ratio = $ratio[1];
  2221. }
  2222. return [
  2223. floor($width * $ratio),
  2224. floor($height * $ratio)
  2225. ];
  2226. }
  2227. }