ddg.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. <?php
  2. class ddg{
  3. public function __construct(){
  4. include "lib/backend.php";
  5. $this->backend = new backend("ddg");
  6. include "lib/fuckhtml.php";
  7. $this->fuckhtml = new fuckhtml();
  8. }
  9. /*
  10. curl functions
  11. */
  12. private const req_web = 0;
  13. private const req_xhr = 1;
  14. private function get($proxy, $url, $get = [], $reqtype = self::req_web){
  15. $curlproc = curl_init();
  16. if($get !== []){
  17. $get = http_build_query($get);
  18. $url .= "?" . $get;
  19. }
  20. curl_setopt($curlproc, CURLOPT_URL, $url);
  21. // http2 bypass
  22. curl_setopt($curlproc, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  23. switch($reqtype){
  24. case self::req_web:
  25. $headers =
  26. ["User-Agent: " . config::USER_AGENT,
  27. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
  28. "Accept-Language: en-US,en;q=0.5",
  29. "Accept-Encoding: gzip",
  30. "DNT: 1",
  31. "Sec-GPC: 1",
  32. "Connection: keep-alive",
  33. "Upgrade-Insecure-Requests: 1",
  34. "Sec-Fetch-Dest: document",
  35. "Sec-Fetch-Mode: navigate",
  36. "Sec-Fetch-Site: same-origin",
  37. "Sec-Fetch-User: ?1",
  38. "Priority: u=0, i",
  39. "TE: trailers"];
  40. break;
  41. case self::req_xhr:
  42. $headers =
  43. ["User-Agent: " . config::USER_AGENT,
  44. "Accept: */*",
  45. "Accept-Language: en-US,en;q=0.5",
  46. "Accept-Encoding: gzip",
  47. "Referer: https://duckduckgo.com/",
  48. "DNT: 1",
  49. "Sec-GPC: 1",
  50. "Connection: keep-alive",
  51. "Sec-Fetch-Dest: script",
  52. "Sec-Fetch-Mode: no-cors",
  53. "Sec-Fetch-Site: same-site",
  54. "Priority: u=1"];
  55. break;
  56. }
  57. $this->backend->assign_proxy($curlproc, $proxy);
  58. curl_setopt($curlproc, CURLOPT_ENCODING, ""); // default encoding
  59. curl_setopt($curlproc, CURLOPT_HTTPHEADER, $headers);
  60. curl_setopt($curlproc, CURLOPT_RETURNTRANSFER, true);
  61. curl_setopt($curlproc, CURLOPT_SSL_VERIFYHOST, 2);
  62. curl_setopt($curlproc, CURLOPT_SSL_VERIFYPEER, true);
  63. curl_setopt($curlproc, CURLOPT_CONNECTTIMEOUT, 30);
  64. curl_setopt($curlproc, CURLOPT_TIMEOUT, 30);
  65. $data = curl_exec($curlproc);
  66. if(curl_errno($curlproc)){
  67. throw new Exception(curl_error($curlproc));
  68. }
  69. curl_close($curlproc);
  70. return $data;
  71. }
  72. public function getfilters($pagetype){
  73. $base = [
  74. "country" => [
  75. "display" => "Country",
  76. "option" => [
  77. "us-en" => "US (English)",
  78. "ar-es" => "Argentina",
  79. "au-en" => "Australia",
  80. "at-de" => "Austria",
  81. "be-fr" => "Belgium (fr)",
  82. "be-nl" => "Belgium (nl)",
  83. "br-pt" => "Brazil",
  84. "bg-bg" => "Bulgaria",
  85. "ca-en" => "Canada (en)",
  86. "ca-fr" => "Canada (fr)",
  87. "ct-ca" => "Catalonia",
  88. "cl-es" => "Chile",
  89. "cn-zh" => "China",
  90. "co-es" => "Colombia",
  91. "hr-hr" => "Croatia",
  92. "cz-cs" => "Czech Republic",
  93. "dk-da" => "Denmark",
  94. "ee-et" => "Estonia",
  95. "fi-fi" => "Finland",
  96. "fr-fr" => "France",
  97. "de-de" => "Germany",
  98. "gr-el" => "Greece",
  99. "hk-tzh" => "Hong Kong",
  100. "hu-hu" => "Hungary",
  101. "in-en" => "India (en)",
  102. "id-en" => "Indonesia (en)",
  103. "ie-en" => "Ireland",
  104. "il-en" => "Israel (en)",
  105. "it-it" => "Italy",
  106. "jp-jp" => "Japan",
  107. "kr-kr" => "Korea",
  108. "lv-lv" => "Latvia",
  109. "lt-lt" => "Lithuania",
  110. "my-en" => "Malaysia (en)",
  111. "mx-es" => "Mexico",
  112. "nl-nl" => "Netherlands",
  113. "nz-en" => "New Zealand",
  114. "no-no" => "Norway",
  115. "pk-en" => "Pakistan (en)",
  116. "pe-es" => "Peru",
  117. "ph-en" => "Philippines (en)",
  118. "pl-pl" => "Poland",
  119. "pt-pt" => "Portugal",
  120. "ro-ro" => "Romania",
  121. "ru-ru" => "Russia",
  122. "xa-ar" => "Saudi Arabia",
  123. "sg-en" => "Singapore",
  124. "sk-sk" => "Slovakia",
  125. "sl-sl" => "Slovenia",
  126. "za-en" => "South Africa",
  127. "es-ca" => "Spain (ca)",
  128. "es-es" => "Spain (es)",
  129. "se-sv" => "Sweden",
  130. "ch-de" => "Switzerland (de)",
  131. "ch-fr" => "Switzerland (fr)",
  132. "tw-tzh" => "Taiwan",
  133. "th-en" => "Thailand (en)",
  134. "tr-tr" => "Turkey",
  135. "us-es" => "US (Spanish)",
  136. "ua-uk" => "Ukraine",
  137. "uk-en" => "United Kingdom",
  138. "vn-en" => "Vietnam (en)"
  139. ]
  140. ]
  141. ];
  142. switch($pagetype){
  143. case "web":
  144. $base["country"]["option"] =
  145. array_merge(["any" => "All Regions"], $base["country"]["option"]);
  146. return array_merge($base,
  147. [
  148. "nsfw" => [
  149. "display" => "NSFW",
  150. "option" => [
  151. "yes" => "Yes",
  152. "maybe" => "Maybe",
  153. "no" => "No"
  154. ]
  155. ],
  156. "newer" => [
  157. "display" => "Newer than",
  158. "option" => "_DATE"
  159. ],
  160. "older" => [
  161. "display" => "Older than",
  162. "option" => "_DATE"
  163. ],
  164. "extendedsearch" => [
  165. // undefined display
  166. "option" => [
  167. "yes" => "Yes",
  168. "no" => "No",
  169. ]
  170. ]
  171. ]
  172. );
  173. break;
  174. case "images":
  175. return array_merge($base,
  176. [
  177. "nsfw" => [
  178. "display" => "NSFW",
  179. "option" => [
  180. "yes" => "Yes",
  181. "no" => "No"
  182. ]
  183. ],
  184. "date" => [
  185. "display" => "Time posted",
  186. "option" => [
  187. "any" => "Any time",
  188. "Day" => "Past day",
  189. "Week" => "Past week",
  190. "Month" => "Past month"
  191. ]
  192. ],
  193. "size" => [
  194. "display" => "Size",
  195. "option" => [
  196. "any" => "Any size",
  197. "Small" => "Small",
  198. "Medium" => "Medium",
  199. "Large" => "Large",
  200. "Wallpaper" => "Wallpaper"
  201. ]
  202. ],
  203. "color" => [
  204. "display" => "Colors",
  205. "option" => [
  206. "any" => "All colors",
  207. "Monochrome" => "Black and white",
  208. "Red" => "Red",
  209. "Orange" => "Orange",
  210. "Yellow" => "Yellow",
  211. "Green" => "Green",
  212. "Blue" => "Blue",
  213. "Purple" => "Purple",
  214. "Pink" => "Pink",
  215. "Brown" => "Brown",
  216. "Black" => "Black",
  217. "Gray" => "Gray",
  218. "Teal" => "Teal",
  219. "White" => "White"
  220. ]
  221. ],
  222. "type" => [
  223. "display" => "Type",
  224. "option" => [
  225. "any" => "All types",
  226. "photo" => "Photograph",
  227. "clipart" => "Clipart",
  228. "gif" => "Animated GIF",
  229. "transparent" => "Transparent"
  230. ]
  231. ],
  232. "layout" => [
  233. "display" => "Layout",
  234. "option" => [
  235. "any" => "All layouts",
  236. "Square" => "Square",
  237. "Tall" => "Tall",
  238. "Wide" => "Wide"
  239. ]
  240. ],
  241. "license" => [
  242. "display" => "License",
  243. "option" => [
  244. "any" => "All licenses",
  245. "Any" => "All Creative Commons",
  246. "Public" => "Public domain",
  247. "Share" => "Free to Share and Use",
  248. "ShareCommercially" => "Free to Share and Use Commercially",
  249. "Modify" => "Free to Modify, Share, and Use",
  250. "ModifyCommercially" => "Free to Modify, Share, and Use Commercially"
  251. ]
  252. ]
  253. ]
  254. );
  255. break;
  256. case "videos":
  257. return array_merge($base,
  258. [
  259. "nsfw" => [
  260. "display" => "NSFW",
  261. "option" => [
  262. "yes" => "Yes",
  263. "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. $out = [
  330. "status" => "ok",
  331. "spelling" => [
  332. "type" => "no_correction",
  333. "using" => null,
  334. "correction" => null
  335. ],
  336. "npt" => null,
  337. "answer" => [],
  338. "web" => [],
  339. "image" => [],
  340. "video" => [],
  341. "news" => [],
  342. "related" => []
  343. ];
  344. if($get["npt"]){
  345. [$js_link, $proxy] = $this->backend->get($get["npt"], "web");
  346. $js_link = "https://links.duckduckgo.com" . $js_link;
  347. $html = "";
  348. $get["extendedsearch"] = "no";
  349. }else{
  350. if(strlen($get["s"]) === 0){
  351. throw new Exception("Search term is empty!");
  352. }
  353. $proxy = $this->backend->get_ip();
  354. // generate filters
  355. $get_filters = [
  356. "q" => $get["s"]
  357. ];
  358. if($get["country"] == "any"){
  359. $get_filters["kl"] = "wt-wt";
  360. }else{
  361. $get_filters["kl"] = $get["country"];
  362. }
  363. switch($get["nsfw"]){
  364. case "yes": $get_filters["kp"] = "-2"; break;
  365. case "maybe": $get_filters["kp"] = "-1"; break;
  366. case "no": $get_filters["kp"] = "1"; break;
  367. }
  368. $df = true;
  369. if($get["newer"] === false){
  370. if($get["older"] !== false){
  371. $start = 36000;
  372. $end = $get["older"];
  373. }else{
  374. $df = false;
  375. }
  376. }else{
  377. $start = $get["newer"];
  378. if($get["older"] !== false){
  379. $end = $get["older"];
  380. }else{
  381. $end = time();
  382. }
  383. }
  384. if($df === true){
  385. $get_filters["df"] = date("Y-m-d", $start) . ".." . date("Y-m-d", $end);
  386. }
  387. //
  388. // Get HTML
  389. //
  390. try{
  391. $html = $this->get(
  392. $proxy,
  393. "https://duckduckgo.com/",
  394. $get_filters
  395. );
  396. }catch(Exception $e){
  397. throw new Exception("Failed to fetch search page");
  398. }
  399. $this->fuckhtml->load($html);
  400. $script =
  401. $this->fuckhtml
  402. ->getElementById(
  403. "deep_preload_link",
  404. "link"
  405. );
  406. if(
  407. $script === null ||
  408. !isset($script["attributes"]["href"])
  409. ){
  410. throw new Exception("Failed to grep d.js");
  411. }
  412. $js_link =
  413. $this->fuckhtml
  414. ->getTextContent(
  415. $script["attributes"]["href"]
  416. );
  417. }
  418. //
  419. // Get d.js
  420. //
  421. try{
  422. $js = $this->get(
  423. $proxy,
  424. $js_link,
  425. [],
  426. ddg::req_xhr
  427. );
  428. }catch(Exception $e){
  429. throw new Exception("Failed to fetch d.js");
  430. }
  431. //echo htmlspecialchars($js);
  432. $js_tmp =
  433. preg_split(
  434. '/DDG\.pageLayout\.load\(\s*\'d\'\s*,\s*/',
  435. $js,
  436. 2
  437. );
  438. if(count($js_tmp) <= 1){
  439. throw new Exception("Failed to grep pageLayout(d)");
  440. }
  441. $json =
  442. json_decode(
  443. $this->fuckhtml
  444. ->extract_json(
  445. $js_tmp[1]
  446. ),
  447. true
  448. );
  449. if($json === null){
  450. throw new Exception("Failed to decode JSON");
  451. }
  452. //
  453. // Get search results + NPT token
  454. //
  455. foreach($json as $item){
  456. if(isset($item["c"])){
  457. $table = [];
  458. // get youtube video information
  459. if(isset($item["video"]["thumbnail_url_template"])){
  460. $thumb =
  461. [
  462. "ratio" => "16:9",
  463. "url" => $this->bingimg($item["video"]["thumbnail_url_template"])
  464. ];
  465. }else{
  466. $thumb =
  467. [
  468. "ratio" => null,
  469. "url" => null
  470. ];
  471. }
  472. // get table items
  473. if(isset($item["rf"])){
  474. foreach($item["rf"] as $hint){
  475. if(
  476. !isset($hint["label"]["text"]) ||
  477. !isset($hint["items"][0]["text"])
  478. ){
  479. continue;
  480. }
  481. $text = [];
  482. foreach($hint["items"] as $text_part){
  483. $text[] = $text_part["text"];
  484. }
  485. $text = implode(", ", $text);
  486. if(is_numeric($text)){
  487. $text = number_format((string)$text);
  488. }
  489. $table[$hint["label"]["text"]] = $text;
  490. }
  491. }
  492. // get ratings
  493. if(isset($item["ar"])){
  494. foreach($item["ar"] as $rating){
  495. if(
  496. isset($rating["aggregateRating"]["bestRating"]) &&
  497. isset($rating["aggregateRating"]["ratingValue"])
  498. ){
  499. $text = $rating["aggregateRating"]["ratingValue"] . "/" . $rating["aggregateRating"]["bestRating"];
  500. if(isset($rating["aggregateRating"]["reviewCount"])){
  501. $text .= " (" . number_format($rating["aggregateRating"]["reviewCount"]) . " votes)";
  502. }
  503. $table["Rating"] = $text;
  504. }
  505. }
  506. }
  507. // get sublinks
  508. $sublinks = [];
  509. if(isset($item["l"])){
  510. foreach($item["l"] as $sublink){
  511. $sublinks[] = [
  512. "title" => $this->titledots($sublink["text"]),
  513. "description" => $this->titledots($sublink["snippet"]),
  514. "url" => $sublink["targetUrl"],
  515. "date" => null
  516. ];
  517. }
  518. }
  519. // parse search result
  520. $out["web"][] = [
  521. "title" =>
  522. $this->titledots(
  523. $this->fuckhtml
  524. ->getTextContent(
  525. $item["t"]
  526. )
  527. ),
  528. "description" =>
  529. isset($item["a"]) ?
  530. $this->titledots(
  531. $this->fuckhtml
  532. ->getTextContent(
  533. $item["a"]
  534. )
  535. ) : null,
  536. "url" => $this->unshiturl($item["c"]),
  537. "date" =>
  538. isset($item["e"]) ?
  539. strtotime($item["e"]) : null,
  540. "type" => "web",
  541. "thumb" => $thumb,
  542. "sublink" => $sublinks,
  543. "table" => $table
  544. ];
  545. continue;
  546. }
  547. if(isset($item["n"])){
  548. // get NPT
  549. $out["npt"] =
  550. $this->backend->store(
  551. $item["n"],
  552. "web",
  553. $proxy
  554. );
  555. continue;
  556. }
  557. }
  558. //
  559. // Get spelling
  560. //
  561. $js_tmp =
  562. preg_split(
  563. '/DDG\.page\.showMessage\(\s*\'spelling\'\s*,\s*/',
  564. $js,
  565. 2
  566. );
  567. if(count($js_tmp) > 1){
  568. $json =
  569. json_decode(
  570. $this->fuckhtml
  571. ->extract_json(
  572. $js_tmp[1]
  573. ),
  574. true
  575. );
  576. if($json !== null){
  577. // parse spelling
  578. // qc=2: including
  579. switch((int)$json["qc"]){
  580. case 2:
  581. $type = "including";
  582. break;
  583. default:
  584. $type = "not_many";
  585. break;
  586. }
  587. $out["spelling"] = [
  588. "type" => $type,
  589. "using" =>
  590. $this->fuckhtml
  591. ->getTextContent(
  592. $json["suggestion"]
  593. ),
  594. "correction" => $json["recourseText"]
  595. ];
  596. }
  597. }
  598. //
  599. // Get images
  600. //
  601. $js_tmp =
  602. preg_split(
  603. '/DDG\.duckbar\.load\(\s*\'images\'\s*,\s*/',
  604. $js,
  605. 2
  606. );
  607. if(count($js_tmp) > 1){
  608. $json =
  609. json_decode(
  610. $this->fuckhtml
  611. ->extract_json(
  612. $js_tmp[1]
  613. ),
  614. true
  615. );
  616. if($json !== null){
  617. foreach($json["results"] as $image){
  618. $ratio = $this->bingratio((int)$image["width"], (int)$image["height"]);
  619. $out["image"][] = [
  620. "title" => $image["title"],
  621. "source" => [
  622. [
  623. "url" => $image["image"],
  624. "width" => (int)$image["width"],
  625. "height" => (int)$image["height"]
  626. ],
  627. [
  628. "url" => $this->bingimg($image["thumbnail"]),
  629. "width" => $ratio[0],
  630. "height" => $ratio[1]
  631. ]
  632. ],
  633. "url" => $this->unshiturl($image["url"])
  634. ];
  635. }
  636. }
  637. }
  638. //
  639. // Get videos
  640. //
  641. $js_tmp =
  642. preg_split(
  643. '/DDG\.duckbar\.load\(\s*\'videos\'\s*,\s*/',
  644. $js,
  645. 2
  646. );
  647. if(count($js_tmp) > 1){
  648. $json =
  649. json_decode(
  650. $this->fuckhtml
  651. ->extract_json(
  652. $js_tmp[1]
  653. ),
  654. true
  655. );
  656. if($json !== null){
  657. foreach($json["results"] as $video){
  658. $thumb = [
  659. "ratio" => null,
  660. "url" => null
  661. ];
  662. foreach(["large", "medium", "small"] as $contender){
  663. if(isset($video["images"][$contender])){
  664. $thumb = [
  665. "ratio" => "16:9",
  666. "url" => $this->bingimg($video["images"][$contender])
  667. ];
  668. break;
  669. }
  670. }
  671. $out["video"][] = [
  672. "title" => $this->titledots($video["title"]),
  673. "description" =>
  674. $video["description"] != "" ?
  675. $this->titledots($video["description"]) : null,
  676. "date" =>
  677. isset($video["published"]) ?
  678. strtotime($video["published"]) : null,
  679. "duration" =>
  680. $video["duration"] != "" ?
  681. $this->hms2int($video["duration"]) : null,
  682. "views" =>
  683. isset($video["statistics"]["viewCount"]) ?
  684. (int)$video["statistics"]["viewCount"] : null,
  685. "thumb" => $thumb,
  686. "url" => $this->unshiturl($video["content"])
  687. ];
  688. }
  689. }
  690. }
  691. //
  692. // Get news
  693. //
  694. $js_tmp =
  695. preg_split(
  696. '/DDG\.duckbar\.load\(\s*\'news\'\s*,\s*/',
  697. $js,
  698. 2
  699. );
  700. if(count($js_tmp) > 1){
  701. $json =
  702. json_decode(
  703. $this->fuckhtml
  704. ->extract_json(
  705. $js_tmp[1]
  706. ),
  707. true
  708. );
  709. if($json !== null){
  710. foreach($json["results"] as $news){
  711. if(isset($news["image"])){
  712. $thumb = [
  713. "ratio" => "16:9",
  714. "url" => $news["image"]
  715. ];
  716. }else{
  717. $thumb = [
  718. "ratio" => null,
  719. "url" => null
  720. ];
  721. }
  722. $out["news"][] = [
  723. "title" => $news["title"],
  724. "description" =>
  725. $this->fuckhtml
  726. ->getTextContent(
  727. $news["excerpt"]
  728. ),
  729. "date" => (int)$news["date"],
  730. "thumb" => $thumb,
  731. "url" => $news["url"]
  732. ];
  733. }
  734. }
  735. }
  736. //
  737. // Get related searches
  738. //
  739. $js_tmp =
  740. preg_split(
  741. '/DDG\.duckbar\.loadModule\(\s*\'related_searches\'\s*,\s*/',
  742. $js,
  743. 2
  744. );
  745. if(count($js_tmp) > 1){
  746. $json =
  747. json_decode(
  748. $this->fuckhtml
  749. ->extract_json(
  750. $js_tmp[1]
  751. ),
  752. true
  753. );
  754. if($json !== null){
  755. foreach($json["results"] as $related){
  756. $out["related"][] = $related["text"];
  757. }
  758. }
  759. }
  760. //
  761. // Get instant answers
  762. //
  763. $js_tmp =
  764. preg_split(
  765. '/DDG\.duckbar\.add\(\s*/',
  766. $html . $js,
  767. 2
  768. );
  769. if(count($js_tmp) > 1){
  770. $json =
  771. json_decode(
  772. $this->fuckhtml
  773. ->extract_json(
  774. $js_tmp[1]
  775. ),
  776. true
  777. );
  778. if($json !== null){
  779. $json = $json["data"];
  780. $table = [];
  781. $sublinks = [];
  782. $description = [];
  783. // get official website
  784. if(
  785. isset($json["OfficialWebsite"]) &&
  786. $json["OfficialWebsite"] !== null
  787. ){
  788. $sublinks["Website"] = $json["OfficialWebsite"];
  789. }
  790. // get sublinks & table elements
  791. if(isset($json["Infobox"]["content"])){
  792. foreach($json["Infobox"]["content"] as $info){
  793. if($info["data_type"] == "string"){
  794. // add table element
  795. $table[$info["label"]] = $info["value"];
  796. continue;
  797. }
  798. if($info["data_type"] == "wd_description"){
  799. $description[] = [
  800. "type" => "quote",
  801. "value" => $info["value"]
  802. ];
  803. continue;
  804. }
  805. // add sublink
  806. switch($info["data_type"]){
  807. case "official_site":
  808. case "official_website":
  809. $type = "Website";
  810. break;
  811. case "wikipedia": $type = "Wikipedia"; break;
  812. case "itunes": $type = "iTunes"; break;
  813. case "amazon": $type = "Amazon"; break;
  814. case "imdb_title_id":
  815. case "imdb_id":
  816. case "imdb_name_id":
  817. $type = "IMDb";
  818. $delim = substr($info["value"], 0, 2);
  819. if($delim == "nm"){
  820. $prefix = "https://www.imdb.com/name/";
  821. }elseif($delim == "tt"){
  822. $prefix = "https://www.imdb.com/title/";
  823. }elseif($delim == "co"){
  824. $prefix = "https://www.imdb.com/search/title/?companies=";
  825. }else{
  826. $prefix = "https://www.imdb.com/title/";
  827. }
  828. break;
  829. case "imdb_name_id": $prefix = "https://www.imdb.com/name/"; $type = "IMDb"; break;
  830. case "twitter_profile": $prefix = "https://twitter.com/"; $type = "Twitter"; break;
  831. case "instagram_profile": $prefix = "https://instagram.com/"; $type = "Instagram"; break;
  832. case "facebook_profile": $prefix = "https://facebook.com/"; $type = "Facebook"; break;
  833. case "spotify_artist_id": $prefix = "https://open.spotify.com/artist/"; $type = "Spotify"; break;
  834. case "itunes_artist_id": $prefix = "https://music.apple.com/us/artist/"; $type = "iTunes"; break;
  835. case "rotten_tomatoes": $prefix = "https://rottentomatoes.com/"; $type = "Rotten Tomatoes"; break;
  836. case "youtube_channel": $prefix = "https://youtube.com/channel/"; $type = "YouTube"; break;
  837. case "soundcloud_id": $prefix = "https://soundcloud.com/"; $type = "SoundCloud"; break;
  838. default:
  839. $prefix = null;
  840. $type = false;
  841. }
  842. if($type !== false){
  843. if($prefix === null){
  844. $sublinks[$type] = $info["value"];
  845. }else{
  846. $sublinks[$type] = $prefix . $info["value"];
  847. }
  848. }
  849. }
  850. }
  851. if(isset($json["Abstract"])){
  852. $description[] =
  853. [
  854. "type" => "text",
  855. "value" => $json["Abstract"]
  856. ];
  857. }
  858. $out["answer"][] = [
  859. "title" => $json["Heading"],
  860. "description" => $description,
  861. "url" => $json["AbstractURL"],
  862. "thumb" =>
  863. (isset($json["Image"]) && $json["Image"]) !== null ?
  864. "https://duckduckgo.com" . $json["Image"] : null,
  865. "table" => $table,
  866. "sublink" => $sublinks
  867. ];
  868. }
  869. }
  870. if($get["extendedsearch"] == "no"){
  871. return $out;
  872. }
  873. //
  874. // Get wordnik definition
  875. //
  876. //nrj('/js/spice/dictionary/definition/create', null, null, null, null, 'dictionary_definition');
  877. preg_match(
  878. '/nrj\(\s*\'([^\']+)\'/',
  879. $js,
  880. $nrj
  881. );
  882. if(isset($nrj[1])){
  883. $nrj = $nrj[1];
  884. preg_match(
  885. '/\/js\/spice\/dictionary\/definition\/([^\/]+)/',
  886. $nrj,
  887. $word
  888. );
  889. if(isset($word[1])){
  890. $word = $word[1];
  891. // found wordnik definition & word
  892. try{
  893. $nik =
  894. $this->get(
  895. $proxy,
  896. "https://duckduckgo.com/js/spice/dictionary/definition/" . $word,
  897. [],
  898. ddg::req_xhr
  899. );
  900. }catch(Exception $e){
  901. // fail gracefully
  902. return $out;
  903. }
  904. // remove javascript
  905. $js_tmp =
  906. preg_split(
  907. '/ddg_spice_dictionary_definition\(\s*/',
  908. $nik,
  909. 2
  910. );
  911. if(count($js_tmp) > 1){
  912. $nik =
  913. json_decode(
  914. $this->fuckhtml
  915. ->extract_json(
  916. $js_tmp[1]
  917. ),
  918. true
  919. );
  920. }
  921. if($nik === null){
  922. return $out;
  923. }
  924. $answer_cat = [];
  925. $answer = [];
  926. foreach($nik as $snippet){
  927. if(!isset($snippet["partOfSpeech"])){ continue; }
  928. $push = [];
  929. // add text snippet
  930. if(isset($snippet["text"])){
  931. $push[] = [
  932. "type" => "text",
  933. "value" =>
  934. $this->fuckhtml
  935. ->getTextContent(
  936. $snippet["text"]
  937. )
  938. ];
  939. }
  940. // add example uses
  941. if(isset($snippet["exampleUses"])){
  942. foreach($snippet["exampleUses"] as $example){
  943. $push[] = [
  944. "type" => "quote",
  945. "value" => "\"" .
  946. $this->fuckhtml
  947. ->getTextContent(
  948. $example["text"]
  949. ) . "\""
  950. ];
  951. }
  952. }
  953. // add citations
  954. if(isset($snippet["citations"])){
  955. foreach($snippet["citations"] as $citation){
  956. if(!isset($citation["cite"])){ continue; }
  957. $text =
  958. $this->fuckhtml
  959. ->getTextContent(
  960. $citation["cite"]
  961. );
  962. if(isset($citation["source"])){
  963. $text .=
  964. " - " .
  965. $this->fuckhtml
  966. ->getTextContent(
  967. $citation["source"]
  968. );
  969. }
  970. $push[] = [
  971. "type" => "quote",
  972. "value" => $text
  973. ];
  974. }
  975. }
  976. // add related words
  977. if(isset($snippet["relatedWords"])){
  978. $relations = [];
  979. foreach($snippet["relatedWords"] as $related){
  980. $words = [];
  981. foreach($related["words"] as $wrd){
  982. $words[] =
  983. $this->fuckhtml
  984. ->getTextContent(
  985. $wrd
  986. );
  987. }
  988. if(count($words) !== 0){
  989. $relations[ucfirst($related["relationshipType"]) . "s"] =
  990. implode(", ", $words);
  991. }
  992. }
  993. foreach($relations as $relation_title => $relation_content){
  994. $push[] = [
  995. "type" => "quote",
  996. "value" => $relation_title . ": " . $relation_content
  997. ];
  998. }
  999. }
  1000. if(count($push) !== 0){
  1001. // push data to answer_cat
  1002. if(!isset($answer_cat[$snippet["partOfSpeech"]])){
  1003. $answer_cat[$snippet["partOfSpeech"]] = [];
  1004. }
  1005. $answer_cat[$snippet["partOfSpeech"]] =
  1006. array_merge(
  1007. $answer_cat[$snippet["partOfSpeech"]],
  1008. $push
  1009. );
  1010. }
  1011. }
  1012. foreach($answer_cat as $answer_title => $answer_content){
  1013. $i = 0;
  1014. $answer[] = [
  1015. "type" => "title",
  1016. "value" => $answer_title
  1017. ];
  1018. $old_type = $answer[count($answer) - 1]["type"];
  1019. foreach($answer_content as $ans){
  1020. if(
  1021. $ans["type"] == "text" &&
  1022. $old_type == "text"
  1023. ){
  1024. $i++;
  1025. $c = count($answer) - 1;
  1026. // append text to existing textfield
  1027. $answer[$c] = [
  1028. "type" => "text",
  1029. "value" => $answer[$c]["value"] . "\n" . $i . ". " . $ans["value"]
  1030. ];
  1031. }elseif($ans["type"] == "text"){
  1032. $i++;
  1033. $answer[] = [
  1034. "type" => "text",
  1035. "value" => $i . ". " . $ans["value"]
  1036. ];
  1037. }else{
  1038. // append normally
  1039. $answer[] = $ans;
  1040. }
  1041. $old_type = $ans["type"];
  1042. }
  1043. }
  1044. // yeah.. sometimes duckduckgo doesnt give us a definition back
  1045. if(count($answer) !== 0){
  1046. $out["answer"][] = [
  1047. "title" => ucfirst($word),
  1048. "description" => $answer,
  1049. "url" => "https://www.wordnik.com/words/" . $word,
  1050. "thumb" => null,
  1051. "table" => [],
  1052. "sublink" => []
  1053. ];
  1054. }
  1055. }
  1056. }
  1057. return $out;
  1058. }
  1059. public function image($get){
  1060. if($get["npt"]){
  1061. [$js_link, $proxy] = $this->backend->get($get["npt"], "images");
  1062. }else{
  1063. if(strlen($get["s"]) === 0){
  1064. throw new Exception("Search term is empty!");
  1065. }
  1066. $proxy = $this->backend->get_ip();
  1067. $filters = [];
  1068. if($get["date"] != "any"){ $filters[] = "time:{$get["date"]}"; }
  1069. if($get["size"] != "any"){ $filters[] = "size:{$get["size"]}"; }
  1070. if($get["color"] != "any"){ $filters[] = "color:{$get["color"]}"; }
  1071. if($get["type"] != "any"){ $filters[] = "type:{$get["type"]}"; }
  1072. if($get["layout"] != "any"){ $filters[] = "layout:{$get["layout"]}"; }
  1073. if($get["license"] != "any"){ $filters[] = "license:{$get["license"]}"; }
  1074. $filters = implode(",", $filters);
  1075. $get_filters = [
  1076. "q" => $get["s"],
  1077. "iax" => "images",
  1078. "ia" => "images"
  1079. ];
  1080. if($filters != ""){
  1081. $get_filters["iaf"] = $filters;
  1082. }
  1083. $nsfw = $get["nsfw"] == "yes" ? "-2" : "-1";
  1084. $get_filters["kp"] = $nsfw;
  1085. try{
  1086. $html = $this->get(
  1087. $proxy,
  1088. "https://duckduckgo.com",
  1089. $get_filters,
  1090. ddg::req_web
  1091. );
  1092. }catch(Exception $err){
  1093. throw new Exception("Failed to fetch search page");
  1094. }
  1095. preg_match(
  1096. '/vqd="([0-9-]+)"/',
  1097. $html,
  1098. $vqd
  1099. );
  1100. if(!isset($vqd[1])){
  1101. throw new Exception("Failed to grep VQD token");
  1102. }
  1103. $js_link =
  1104. "i.js?" .
  1105. http_build_query([
  1106. "l" => $get["country"],
  1107. "o" => "json",
  1108. "q" => $get["s"],
  1109. "vqd" => $vqd[1],
  1110. "f" => $filters,
  1111. "p" => $nsfw
  1112. ]);
  1113. }
  1114. try{
  1115. $json =
  1116. $this->get(
  1117. $proxy,
  1118. "https://duckduckgo.com/" . $js_link,
  1119. [],
  1120. ddg::req_xhr
  1121. );
  1122. }catch(Exception $error){
  1123. throw new Exception("Failed to get i.js");
  1124. }
  1125. $json = json_decode($json, true);
  1126. if($json === null){
  1127. throw new Exception("Failed to decode JSON");
  1128. }
  1129. $out = [
  1130. "status" => "ok",
  1131. "npt" => null,
  1132. "image" => []
  1133. ];
  1134. if(!isset($json["results"])){
  1135. return $out;
  1136. }
  1137. // get npt
  1138. if(
  1139. isset($json["next"]) &&
  1140. $json["next"] !== null
  1141. ){
  1142. $vqd = null;
  1143. if(isset($vqd[1])){
  1144. $vqd = $vqd[1];
  1145. }else{
  1146. $vqd = array_values($json["vqd"]);
  1147. if(count($vqd) > 0){
  1148. $vqd = $vqd[0];
  1149. }
  1150. }
  1151. if($vqd !== null){
  1152. $out["npt"] =
  1153. $this->backend->store(
  1154. $json["next"] . "&vqd=" . $vqd,
  1155. "images",
  1156. $proxy
  1157. );
  1158. }
  1159. }
  1160. // get images
  1161. foreach($json["results"] as $image){
  1162. $ratio =
  1163. $this->bingratio(
  1164. (int)$image["width"],
  1165. (int)$image["height"]
  1166. );
  1167. $out["image"][] = [
  1168. "title" => $this->titledots($image["title"]),
  1169. "source" => [
  1170. [
  1171. "url" => $image["image"],
  1172. "width" => (int)$image["width"],
  1173. "height" => (int)$image["height"]
  1174. ],
  1175. [
  1176. "url" => $this->bingimg($image["thumbnail"]),
  1177. "width" => $ratio[0],
  1178. "height" => $ratio[1]
  1179. ]
  1180. ],
  1181. "url" => $this->unshiturl($image["url"])
  1182. ];
  1183. }
  1184. return $out;
  1185. }
  1186. public function video($get){
  1187. if($get["npt"]){
  1188. [$js_link, $proxy] = $this->backend->get($get["npt"], "videos");
  1189. }else{
  1190. if(strlen($get["s"]) === 0){
  1191. throw new Exception("Search term is empty!");
  1192. }
  1193. $proxy = $this->backend->get_ip();
  1194. $get_filters = [
  1195. "q" => $get["s"],
  1196. "iax" => "videos",
  1197. "ia" => "videos"
  1198. ];
  1199. $nsfw = $get["nsfw"] == "yes" ? "-2" : "-1";
  1200. $get_filters["kp"] = $nsfw;
  1201. $filters = [];
  1202. if($get["date"] != "any"){ $filters[] = "publishedAfter:{$date}"; }
  1203. if($get["resolution"] != "any"){ $filters[] = "videoDefinition:{$resolution}"; }
  1204. if($get["duration"] != "any"){ $filters[] = "videoDuration:{$duration}"; }
  1205. if($get["license"] != "any"){ $filters[] = "videoLicense:{$license}"; }
  1206. $filters = implode(",", $filters);
  1207. if($filters != ""){
  1208. $get_filters["iaf"] = $filters;
  1209. }
  1210. try{
  1211. $html =
  1212. $this->get(
  1213. $proxy,
  1214. "https://duckduckgo.com/",
  1215. $get_filters,
  1216. ddg::req_web
  1217. );
  1218. }catch(Exception $error){
  1219. throw new Exception("Failed to fetch search page");
  1220. }
  1221. preg_match(
  1222. '/vqd="([0-9-]+)"/',
  1223. $html,
  1224. $vqd
  1225. );
  1226. if(!isset($vqd[1])){
  1227. throw new Exception("Failed to grep VQD token");
  1228. }
  1229. $js_link =
  1230. "v.js?" .
  1231. http_build_query([
  1232. "l" => $get["country"],
  1233. "o" => "json",
  1234. "sr" => "1",
  1235. "q" => $get["s"],
  1236. "vqd" => $vqd[1],
  1237. "f" => $filters,
  1238. "p" => $nsfw
  1239. ]);
  1240. }
  1241. try{
  1242. $json =
  1243. $this->get(
  1244. $proxy,
  1245. "https://duckduckgo.com/" . $js_link,
  1246. [],
  1247. ddg::req_xhr
  1248. );
  1249. }catch(Exception $error){
  1250. throw new Exception("Failed to fetch JSON");
  1251. }
  1252. $json = json_decode($json, true);
  1253. if($json === null){
  1254. throw new Exception("Failed to decode JSON");
  1255. }
  1256. $out = [
  1257. "status" => "ok",
  1258. "npt" => null,
  1259. "video" => [],
  1260. "author" => [],
  1261. "livestream" => [],
  1262. "playlist" => [],
  1263. "reel" => []
  1264. ];
  1265. if(!isset($json["results"])){
  1266. return $out;
  1267. }
  1268. // get NPT
  1269. if(
  1270. isset($json["next"]) &&
  1271. $json["next"] !== null
  1272. ){
  1273. $out["npt"] =
  1274. $this->backend->store(
  1275. $json["next"],
  1276. "videos",
  1277. $proxy
  1278. );
  1279. }
  1280. foreach($json["results"] as $video){
  1281. $thumb = [
  1282. "ratio" => null,
  1283. "url" => null
  1284. ];
  1285. foreach(["large", "medium", "small"] as $contender){
  1286. if(isset($video["images"][$contender])){
  1287. $thumb = [
  1288. "ratio" => "16:9",
  1289. "url" => $this->bingimg($video["images"][$contender])
  1290. ];
  1291. break;
  1292. }
  1293. }
  1294. $out["video"][] = [
  1295. "title" => $this->titledots($video["title"]),
  1296. "description" => $this->titledots($video["description"]),
  1297. "author" => [
  1298. "name" =>
  1299. (
  1300. isset($video["uploader"]) &&
  1301. $video["uploader"] != ""
  1302. ) ?
  1303. $video["uploader"] : null,
  1304. "url" => null,
  1305. "avatar" => null
  1306. ],
  1307. "date" =>
  1308. (
  1309. isset($video["published"]) &&
  1310. $video["published"] != ""
  1311. ) ?
  1312. strtotime($video["published"]) : null,
  1313. "duration" =>
  1314. (
  1315. isset($video["duration"]) &&
  1316. $video["duration"] != ""
  1317. ) ?
  1318. $this->hms2int($video["duration"]) : null,
  1319. "views" =>
  1320. isset($video["statistics"]["viewCount"]) ?
  1321. (int)$video["statistics"]["viewCount"] : null,
  1322. "thumb" => $thumb,
  1323. "url" => $this->unshiturl($video["content"])
  1324. ];
  1325. }
  1326. return $out;
  1327. }
  1328. public function news($get){
  1329. if($get["npt"]){
  1330. [$js_link, $proxy] = $this->backend->get($get["npt"], "news");
  1331. }else{
  1332. if(strlen($get["s"]) === 0){
  1333. throw new Exception("Search term is empty!");
  1334. }
  1335. $proxy = $this->backend->get_ip();
  1336. $get_filters = [
  1337. "q" => $get["s"],
  1338. "iar" => "news",
  1339. "ia" => "news"
  1340. ];
  1341. if($get["date"] != "any"){
  1342. $date = $get["date"];
  1343. $get_filters["df"] = $date;
  1344. }else{
  1345. $date = "";
  1346. }
  1347. switch($get["nsfw"]){
  1348. case "yes": $get_filters["kp"] = "-2"; break;
  1349. case "maybe": $get_filters["kp"] = "-1"; break;
  1350. case "no": $get_filters["kp"] = "1"; break;
  1351. }
  1352. try{
  1353. $html =
  1354. $this->get(
  1355. $proxy,
  1356. "https://duckduckgo.com/",
  1357. $get_filters,
  1358. ddg::req_web
  1359. );
  1360. }catch(Exception $error){
  1361. throw new Exception("Failed to fetch search page");
  1362. }
  1363. preg_match(
  1364. '/vqd="([0-9-]+)"/',
  1365. $html,
  1366. $vqd
  1367. );
  1368. if(!isset($vqd[1])){
  1369. throw new Exception("Failed to grep VQD token");
  1370. }
  1371. $js_link =
  1372. "news.js?" .
  1373. http_build_query([
  1374. "l" => $get["country"],
  1375. "o" => "json",
  1376. "noamp" => "1",
  1377. "m" => "30",
  1378. "q" => $get["s"],
  1379. "vqd" => $vqd[1],
  1380. "p" => $get_filters["kp"],
  1381. "df" => $date,
  1382. "u" => "bing"
  1383. ]);
  1384. }
  1385. try{
  1386. $json =
  1387. $this->get(
  1388. $proxy,
  1389. "https://duckduckgo.com/" . $js_link,
  1390. [],
  1391. ddg::req_xhr
  1392. );
  1393. }catch(Exception $error){
  1394. throw new Exception("Failed to fetch JSON");
  1395. }
  1396. $json = json_decode($json, true);
  1397. if($json === null){
  1398. throw new Exception("Failed to decode JSON");
  1399. }
  1400. $out = [
  1401. "status" => "ok",
  1402. "npt" => null,
  1403. "news" => []
  1404. ];
  1405. if(!isset($json["results"])){
  1406. return $out;
  1407. }
  1408. // get NPT
  1409. if(
  1410. isset($json["next"]) &&
  1411. $json["next"] !== null
  1412. ){
  1413. $out["npt"] =
  1414. $this->backend->store(
  1415. $json["next"],
  1416. "news",
  1417. $proxy
  1418. );
  1419. }
  1420. foreach($json["results"] as $news){
  1421. if(
  1422. isset($news["image"]) &&
  1423. $news["image"] != ""
  1424. ){
  1425. $thumb = [
  1426. "ratio" => "16:9",
  1427. "url" => $news["image"]
  1428. ];
  1429. }else{
  1430. $thumb = [
  1431. "ratio" => null,
  1432. "url" => null
  1433. ];
  1434. }
  1435. $out["news"][] = [
  1436. "title" => $news["title"],
  1437. "author" =>
  1438. (
  1439. isset($news["source"]) &&
  1440. $news["source"] != ""
  1441. ) ?
  1442. $news["source"] : null,
  1443. "description" =>
  1444. (
  1445. isset($news["excerpt"]) &&
  1446. $news["excerpt"] != ""
  1447. ) ?
  1448. $this->fuckhtml
  1449. ->getTextContent(
  1450. $news["excerpt"]
  1451. ) : null,
  1452. "date" =>
  1453. isset($news["date"]) ?
  1454. (int)$news["date"] : null,
  1455. "thumb" => $thumb,
  1456. "url" => $this->unshiturl($news["url"])
  1457. ];
  1458. }
  1459. return $out;
  1460. }
  1461. private function titledots($title){
  1462. $substr = substr($title, -3);
  1463. if(
  1464. $substr == "..." ||
  1465. $substr == "…"
  1466. ){
  1467. return trim(substr($title, 0, -3));
  1468. }
  1469. return trim($title);
  1470. }
  1471. private function hms2int($time){
  1472. $parts = explode(":", $time, 3);
  1473. $time = 0;
  1474. if(count($parts) === 3){
  1475. // hours
  1476. $time = $time + ((int)$parts[0] * 3600);
  1477. array_shift($parts);
  1478. }
  1479. if(count($parts) === 2){
  1480. // minutes
  1481. $time = $time + ((int)$parts[0] * 60);
  1482. array_shift($parts);
  1483. }
  1484. // seconds
  1485. $time = $time + (int)$parts[0];
  1486. return $time;
  1487. }
  1488. private function unshiturl($url){
  1489. // check for domains w/out first short subdomain (ex: www.)
  1490. $domain = parse_url($url, PHP_URL_HOST);
  1491. $subdomain = preg_replace(
  1492. '/^[A-z0-9]{1,3}\./',
  1493. "",
  1494. $domain
  1495. );
  1496. switch($subdomain){
  1497. case "ebay.com.au":
  1498. case "ebay.at":
  1499. case "ebay.ca":
  1500. case "ebay.fr":
  1501. case "ebay.de":
  1502. case "ebay.com.hk":
  1503. case "ebay.ie":
  1504. case "ebay.it":
  1505. case "ebay.com.my":
  1506. case "ebay.nl":
  1507. case "ebay.ph":
  1508. case "ebay.pl":
  1509. case "ebay.com.sg":
  1510. case "ebay.es":
  1511. case "ebay.ch":
  1512. case "ebay.co.uk":
  1513. case "cafr.ebay.ca":
  1514. case "ebay.com":
  1515. case "community.ebay.com":
  1516. case "pages.ebay.com":
  1517. // remove ebay tracking elements
  1518. $old_params = parse_url($url, PHP_URL_QUERY);
  1519. parse_str($old_params, $params);
  1520. if(isset($params["mkevt"])){ unset($params["mkevt"]); }
  1521. if(isset($params["mkcid"])){ unset($params["mkcid"]); }
  1522. if(isset($params["mkrid"])){ unset($params["mkrid"]); }
  1523. if(isset($params["campid"])){ unset($params["campid"]); }
  1524. if(isset($params["customid"])){ unset($params["customid"]); }
  1525. if(isset($params["toolid"])){ unset($params["toolid"]); }
  1526. if(isset($params["_sop"])){ unset($params["_sop"]); }
  1527. if(isset($params["_dcat"])){ unset($params["_dcat"]); }
  1528. if(isset($params["epid"])){ unset($params["epid"]); }
  1529. if(isset($params["epid"])){ unset($params["oid"]); }
  1530. $params = http_build_query($params);
  1531. if(strlen($params) === 0){
  1532. $replace = "\?";
  1533. }else{
  1534. $replace = "";
  1535. }
  1536. $url = preg_replace(
  1537. "/" . $replace . preg_quote($old_params, "/") . "$/",
  1538. $params,
  1539. $url
  1540. );
  1541. break;
  1542. }
  1543. return $url;
  1544. }
  1545. private function bingimg($url){
  1546. $parse = parse_url($url);
  1547. parse_str($parse["query"], $parts);
  1548. return "https://" . $parse["host"] . "/th?id=" . urlencode($parts["id"]);
  1549. }
  1550. private function bingratio($width, $height){
  1551. $ratio = [
  1552. 474 / $width,
  1553. 474 / $height
  1554. ];
  1555. if($ratio[0] < $ratio[1]){
  1556. $ratio = $ratio[0];
  1557. }else{
  1558. $ratio = $ratio[1];
  1559. }
  1560. return [
  1561. floor($width * $ratio),
  1562. floor($height * $ratio)
  1563. ];
  1564. }
  1565. }