ddg.php 51 KB

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