1
0

ddg.php 51 KB

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