ddg.php 52 KB

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