ddg.php 52 KB

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