ddg.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  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" => $json["suggestion"],
  590. "correction" => $json["recourseText"]
  591. ];
  592. }
  593. }
  594. //
  595. // Get images
  596. //
  597. $js_tmp =
  598. preg_split(
  599. '/DDG\.duckbar\.load\(\s*\'images\'\s*,\s*/',
  600. $js,
  601. 2
  602. );
  603. if(count($js_tmp) > 1){
  604. $json =
  605. json_decode(
  606. $this->fuckhtml
  607. ->extract_json(
  608. $js_tmp[1]
  609. ),
  610. true
  611. );
  612. if($json !== null){
  613. foreach($json["results"] as $image){
  614. $ratio = $this->bingratio((int)$image["width"], (int)$image["height"]);
  615. $out["image"][] = [
  616. "title" => $image["title"],
  617. "source" => [
  618. [
  619. "url" => $image["image"],
  620. "width" => (int)$image["width"],
  621. "height" => (int)$image["height"]
  622. ],
  623. [
  624. "url" => $this->bingimg($image["thumbnail"]),
  625. "width" => $ratio[0],
  626. "height" => $ratio[1]
  627. ]
  628. ],
  629. "url" => $this->unshiturl($image["url"])
  630. ];
  631. }
  632. }
  633. }
  634. //
  635. // Get videos
  636. //
  637. $js_tmp =
  638. preg_split(
  639. '/DDG\.duckbar\.load\(\s*\'videos\'\s*,\s*/',
  640. $js,
  641. 2
  642. );
  643. if(count($js_tmp) > 1){
  644. $json =
  645. json_decode(
  646. $this->fuckhtml
  647. ->extract_json(
  648. $js_tmp[1]
  649. ),
  650. true
  651. );
  652. if($json !== null){
  653. foreach($json["results"] as $video){
  654. $thumb = [
  655. "ratio" => null,
  656. "url" => null
  657. ];
  658. foreach(["large", "medium", "small"] as $contender){
  659. if(isset($video["images"][$contender])){
  660. $thumb = [
  661. "ratio" => "16:9",
  662. "url" => $this->bingimg($video["images"][$contender])
  663. ];
  664. break;
  665. }
  666. }
  667. $out["video"][] = [
  668. "title" => $this->titledots($video["title"]),
  669. "description" =>
  670. $video["description"] != "" ?
  671. $this->titledots($video["description"]) : null,
  672. "date" =>
  673. isset($video["published"]) ?
  674. strtotime($video["published"]) : null,
  675. "duration" =>
  676. $video["duration"] != "" ?
  677. $this->hms2int($video["duration"]) : null,
  678. "views" =>
  679. isset($video["statistics"]["viewCount"]) ?
  680. (int)$video["statistics"]["viewCount"] : null,
  681. "thumb" => $thumb,
  682. "url" => $this->unshiturl($video["content"])
  683. ];
  684. }
  685. }
  686. }
  687. //
  688. // Get news
  689. //
  690. $js_tmp =
  691. preg_split(
  692. '/DDG\.duckbar\.load\(\s*\'news\'\s*,\s*/',
  693. $js,
  694. 2
  695. );
  696. if(count($js_tmp) > 1){
  697. $json =
  698. json_decode(
  699. $this->fuckhtml
  700. ->extract_json(
  701. $js_tmp[1]
  702. ),
  703. true
  704. );
  705. if($json !== null){
  706. foreach($json["results"] as $news){
  707. if(isset($news["image"])){
  708. $thumb = [
  709. "ratio" => "16:9",
  710. "url" => $news["image"]
  711. ];
  712. }else{
  713. $thumb = [
  714. "ratio" => null,
  715. "url" => null
  716. ];
  717. }
  718. $out["news"][] = [
  719. "title" => $news["title"],
  720. "description" =>
  721. $this->fuckhtml
  722. ->getTextContent(
  723. $news["excerpt"]
  724. ),
  725. "date" => (int)$news["date"],
  726. "thumb" => $thumb,
  727. "url" => $news["url"]
  728. ];
  729. }
  730. }
  731. }
  732. //
  733. // Get related searches
  734. //
  735. $js_tmp =
  736. preg_split(
  737. '/DDG\.duckbar\.loadModule\(\s*\'related_searches\'\s*,\s*/',
  738. $js,
  739. 2
  740. );
  741. if(count($js_tmp) > 1){
  742. $json =
  743. json_decode(
  744. $this->fuckhtml
  745. ->extract_json(
  746. $js_tmp[1]
  747. ),
  748. true
  749. );
  750. if($json !== null){
  751. foreach($json["results"] as $related){
  752. $out["related"][] = $related["text"];
  753. }
  754. }
  755. }
  756. //
  757. // Get instant answers
  758. //
  759. $js_tmp =
  760. preg_split(
  761. '/DDG\.duckbar\.add\(\s*/',
  762. $html . $js,
  763. 2
  764. );
  765. if(count($js_tmp) > 1){
  766. $json =
  767. json_decode(
  768. $this->fuckhtml
  769. ->extract_json(
  770. $js_tmp[1]
  771. ),
  772. true
  773. );
  774. if($json !== null){
  775. $json = $json["data"];
  776. $table = [];
  777. $sublinks = [];
  778. $description = [];
  779. // get official website
  780. if(
  781. isset($json["OfficialWebsite"]) &&
  782. $json["OfficialWebsite"] !== null
  783. ){
  784. $sublinks["Website"] = $json["OfficialWebsite"];
  785. }
  786. // get sublinks & table elements
  787. if(isset($json["Infobox"]["content"])){
  788. foreach($json["Infobox"]["content"] as $info){
  789. if($info["data_type"] == "string"){
  790. // add table element
  791. $table[$info["label"]] = $info["value"];
  792. continue;
  793. }
  794. if($info["data_type"] == "wd_description"){
  795. $description[] = [
  796. "type" => "quote",
  797. "value" => $info["value"]
  798. ];
  799. continue;
  800. }
  801. // add sublink
  802. switch($info["data_type"]){
  803. case "official_site":
  804. case "official_website":
  805. $type = "Website";
  806. break;
  807. case "wikipedia": $type = "Wikipedia"; break;
  808. case "itunes": $type = "iTunes"; break;
  809. case "amazon": $type = "Amazon"; break;
  810. case "imdb_title_id":
  811. case "imdb_id":
  812. case "imdb_name_id":
  813. $type = "IMDb";
  814. $delim = substr($info["value"], 0, 2);
  815. if($delim == "nm"){
  816. $prefix = "https://www.imdb.com/name/";
  817. }elseif($delim == "tt"){
  818. $prefix = "https://www.imdb.com/title/";
  819. }elseif($delim == "co"){
  820. $prefix = "https://www.imdb.com/search/title/?companies=";
  821. }else{
  822. $prefix = "https://www.imdb.com/title/";
  823. }
  824. break;
  825. case "imdb_name_id": $prefix = "https://www.imdb.com/name/"; $type = "IMDb"; break;
  826. case "twitter_profile": $prefix = "https://twitter.com/"; $type = "Twitter"; break;
  827. case "instagram_profile": $prefix = "https://instagram.com/"; $type = "Instagram"; break;
  828. case "facebook_profile": $prefix = "https://facebook.com/"; $type = "Facebook"; break;
  829. case "spotify_artist_id": $prefix = "https://open.spotify.com/artist/"; $type = "Spotify"; break;
  830. case "itunes_artist_id": $prefix = "https://music.apple.com/us/artist/"; $type = "iTunes"; break;
  831. case "rotten_tomatoes": $prefix = "https://rottentomatoes.com/"; $type = "Rotten Tomatoes"; break;
  832. case "youtube_channel": $prefix = "https://youtube.com/channel/"; $type = "YouTube"; break;
  833. case "soundcloud_id": $prefix = "https://soundcloud.com/"; $type = "SoundCloud"; break;
  834. default:
  835. $prefix = null;
  836. $type = false;
  837. }
  838. if($type !== false){
  839. if($prefix === null){
  840. $sublinks[$type] = $info["value"];
  841. }else{
  842. $sublinks[$type] = $prefix . $info["value"];
  843. }
  844. }
  845. }
  846. }
  847. if(isset($json["Abstract"])){
  848. $description[] =
  849. [
  850. "type" => "text",
  851. "value" => $json["Abstract"]
  852. ];
  853. }
  854. $out["answer"][] = [
  855. "title" => $json["Heading"],
  856. "description" => $description,
  857. "url" => $json["AbstractURL"],
  858. "thumb" =>
  859. (isset($json["Image"]) && $json["Image"]) !== null ?
  860. "https://duckduckgo.com" . $json["Image"] : null,
  861. "table" => $table,
  862. "sublink" => $sublinks
  863. ];
  864. }
  865. }
  866. if($get["extendedsearch"] == "no"){
  867. return $out;
  868. }
  869. //
  870. // Get wordnik definition
  871. //
  872. //nrj('/js/spice/dictionary/definition/create', null, null, null, null, 'dictionary_definition');
  873. preg_match(
  874. '/nrj\(\s*\'([^\']+)\'/',
  875. $js,
  876. $nrj
  877. );
  878. if(isset($nrj[1])){
  879. $nrj = $nrj[1];
  880. preg_match(
  881. '/\/js\/spice\/dictionary\/definition\/([^\/]+)/',
  882. $nrj,
  883. $word
  884. );
  885. if(isset($word[1])){
  886. $word = $word[1];
  887. // found wordnik definition & word
  888. try{
  889. $nik =
  890. $this->get(
  891. $proxy,
  892. "https://duckduckgo.com/js/spice/dictionary/definition/" . $word,
  893. [],
  894. ddg::req_xhr
  895. );
  896. }catch(Exception $e){
  897. // fail gracefully
  898. return $out;
  899. }
  900. // remove javascript
  901. $js_tmp =
  902. preg_split(
  903. '/ddg_spice_dictionary_definition\(\s*/',
  904. $nik,
  905. 2
  906. );
  907. if(count($js_tmp) > 1){
  908. $nik =
  909. json_decode(
  910. $this->fuckhtml
  911. ->extract_json(
  912. $js_tmp[1]
  913. ),
  914. true
  915. );
  916. }
  917. if($nik === null){
  918. return $out;
  919. }
  920. $answer_cat = [];
  921. $answer = [];
  922. foreach($nik as $snippet){
  923. if(!isset($snippet["partOfSpeech"])){ continue; }
  924. $push = [];
  925. // add text snippet
  926. if(isset($snippet["text"])){
  927. $push[] = [
  928. "type" => "text",
  929. "value" =>
  930. $this->fuckhtml
  931. ->getTextContent(
  932. $snippet["text"]
  933. )
  934. ];
  935. }
  936. // add example uses
  937. if(isset($snippet["exampleUses"])){
  938. foreach($snippet["exampleUses"] as $example){
  939. $push[] = [
  940. "type" => "quote",
  941. "value" => "\"" .
  942. $this->fuckhtml
  943. ->getTextContent(
  944. $example["text"]
  945. ) . "\""
  946. ];
  947. }
  948. }
  949. // add citations
  950. if(isset($snippet["citations"])){
  951. foreach($snippet["citations"] as $citation){
  952. $push[] = [
  953. "type" => "quote",
  954. "value" =>
  955. $this->fuckhtml
  956. ->getTextContent(
  957. $citation["cite"]
  958. ) . " - " .
  959. $this->fuckhtml
  960. ->getTextContent(
  961. $citation["source"]
  962. )
  963. ];
  964. }
  965. }
  966. // add related words
  967. if(isset($snippet["relatedWords"])){
  968. $relations = [];
  969. foreach($snippet["relatedWords"] as $related){
  970. $words = [];
  971. foreach($related["words"] as $wrd){
  972. $words[] =
  973. $this->fuckhtml
  974. ->getTextContent(
  975. $wrd
  976. );
  977. }
  978. if(count($words) !== 0){
  979. $relations[ucfirst($related["relationshipType"]) . "s"] =
  980. implode(", ", $words);
  981. }
  982. }
  983. foreach($relations as $relation_title => $relation_content){
  984. $push[] = [
  985. "type" => "quote",
  986. "value" => $relation_title . ": " . $relation_content
  987. ];
  988. }
  989. }
  990. if(count($push) !== 0){
  991. // push data to answer_cat
  992. if(!isset($answer_cat[$snippet["partOfSpeech"]])){
  993. $answer_cat[$snippet["partOfSpeech"]] = [];
  994. }
  995. $answer_cat[$snippet["partOfSpeech"]] =
  996. array_merge(
  997. $answer_cat[$snippet["partOfSpeech"]],
  998. $push
  999. );
  1000. }
  1001. }
  1002. foreach($answer_cat as $answer_title => $answer_content){
  1003. $i = 0;
  1004. $answer[] = [
  1005. "type" => "title",
  1006. "value" => $answer_title
  1007. ];
  1008. $old_type = $answer[count($answer) - 1]["type"];
  1009. foreach($answer_content as $ans){
  1010. if(
  1011. $ans["type"] == "text" &&
  1012. $old_type == "text"
  1013. ){
  1014. $i++;
  1015. $c = count($answer) - 1;
  1016. // append text to existing textfield
  1017. $answer[$c] = [
  1018. "type" => "text",
  1019. "value" => $answer[$c]["value"] . "\n" . $i . ". " . $ans["value"]
  1020. ];
  1021. }elseif($ans["type"] == "text"){
  1022. $i++;
  1023. $answer[] = [
  1024. "type" => "text",
  1025. "value" => $i . ". " . $ans["value"]
  1026. ];
  1027. }else{
  1028. // append normally
  1029. $answer[] = $ans;
  1030. }
  1031. $old_type = $ans["type"];
  1032. }
  1033. }
  1034. // yeah.. sometimes duckduckgo doesnt give us a definition back
  1035. if(count($answer) !== 0){
  1036. $out["answer"][] = [
  1037. "title" => ucfirst($word),
  1038. "description" => $answer,
  1039. "url" => "https://www.wordnik.com/words/" . $word,
  1040. "thumb" => null,
  1041. "table" => [],
  1042. "sublink" => []
  1043. ];
  1044. }
  1045. }
  1046. }
  1047. return $out;
  1048. }
  1049. public function image($get){
  1050. if($get["npt"]){
  1051. [$js_link, $proxy] = $this->backend->get($get["npt"], "images");
  1052. }else{
  1053. if(strlen($get["s"]) === 0){
  1054. throw new Exception("Search term is empty!");
  1055. }
  1056. $proxy = $this->backend->get_ip();
  1057. $filters = [];
  1058. if($get["date"] != "any"){ $filters[] = "time:{$get["date"]}"; }
  1059. if($get["size"] != "any"){ $filters[] = "size:{$get["size"]}"; }
  1060. if($get["color"] != "any"){ $filters[] = "color:{$get["color"]}"; }
  1061. if($get["type"] != "any"){ $filters[] = "type:{$get["type"]}"; }
  1062. if($get["layout"] != "any"){ $filters[] = "layout:{$get["layout"]}"; }
  1063. if($get["license"] != "any"){ $filters[] = "license:{$get["license"]}"; }
  1064. $filters = implode(",", $filters);
  1065. $get_filters = [
  1066. "q" => $get["s"],
  1067. "iax" => "images",
  1068. "ia" => "images"
  1069. ];
  1070. if($filters != ""){
  1071. $get_filters["iaf"] = $filters;
  1072. }
  1073. $nsfw = $get["nsfw"] == "yes" ? "-2" : "-1";
  1074. $get_filters["kp"] = $nsfw;
  1075. try{
  1076. $html = $this->get(
  1077. $proxy,
  1078. "https://duckduckgo.com",
  1079. $get_filters,
  1080. ddg::req_web
  1081. );
  1082. }catch(Exception $err){
  1083. throw new Exception("Failed to fetch search page");
  1084. }
  1085. preg_match(
  1086. '/vqd="([0-9-]+)"/',
  1087. $html,
  1088. $vqd
  1089. );
  1090. if(!isset($vqd[1])){
  1091. throw new Exception("Failed to grep VQD token");
  1092. }
  1093. $js_link =
  1094. "i.js?" .
  1095. http_build_query([
  1096. "l" => $get["country"],
  1097. "o" => "json",
  1098. "q" => $get["s"],
  1099. "vqd" => $vqd[1],
  1100. "f" => $filters,
  1101. "p" => $nsfw
  1102. ]);
  1103. }
  1104. try{
  1105. $json =
  1106. $this->get(
  1107. $proxy,
  1108. "https://duckduckgo.com/" . $js_link,
  1109. [],
  1110. ddg::req_xhr
  1111. );
  1112. }catch(Exception $error){
  1113. throw new Exception("Failed to get i.js");
  1114. }
  1115. $json = json_decode($json, true);
  1116. if($json === null){
  1117. throw new Exception("Failed to decode JSON");
  1118. }
  1119. $out = [
  1120. "status" => "ok",
  1121. "npt" => null,
  1122. "image" => []
  1123. ];
  1124. if(!isset($json["results"])){
  1125. return $out;
  1126. }
  1127. // get npt
  1128. if(
  1129. isset($json["next"]) &&
  1130. $json["next"] !== null
  1131. ){
  1132. $vqd = null;
  1133. if(isset($vqd[1])){
  1134. $vqd = $vqd[1];
  1135. }else{
  1136. $vqd = array_values($json["vqd"]);
  1137. if(count($vqd) > 0){
  1138. $vqd = $vqd[0];
  1139. }
  1140. }
  1141. if($vqd !== null){
  1142. $out["npt"] =
  1143. $this->backend->store(
  1144. $json["next"] . "&vqd=" . $vqd,
  1145. "images",
  1146. $proxy
  1147. );
  1148. }
  1149. }
  1150. // get images
  1151. foreach($json["results"] as $image){
  1152. $ratio =
  1153. $this->bingratio(
  1154. (int)$image["width"],
  1155. (int)$image["height"]
  1156. );
  1157. $out["image"][] = [
  1158. "title" => $this->titledots($image["title"]),
  1159. "source" => [
  1160. [
  1161. "url" => $image["image"],
  1162. "width" => (int)$image["width"],
  1163. "height" => (int)$image["height"]
  1164. ],
  1165. [
  1166. "url" => $this->bingimg($image["thumbnail"]),
  1167. "width" => $ratio[0],
  1168. "height" => $ratio[1]
  1169. ]
  1170. ],
  1171. "url" => $this->unshiturl($image["url"])
  1172. ];
  1173. }
  1174. return $out;
  1175. }
  1176. public function video($get){
  1177. if($get["npt"]){
  1178. [$js_link, $proxy] = $this->backend->get($get["npt"], "videos");
  1179. }else{
  1180. if(strlen($get["s"]) === 0){
  1181. throw new Exception("Search term is empty!");
  1182. }
  1183. $proxy = $this->backend->get_ip();
  1184. $get_filters = [
  1185. "q" => $get["s"],
  1186. "iax" => "videos",
  1187. "ia" => "videos"
  1188. ];
  1189. $nsfw = $get["nsfw"] == "yes" ? "-2" : "-1";
  1190. $get_filters["kp"] = $nsfw;
  1191. $filters = [];
  1192. if($get["date"] != "any"){ $filters[] = "publishedAfter:{$date}"; }
  1193. if($get["resolution"] != "any"){ $filters[] = "videoDefinition:{$resolution}"; }
  1194. if($get["duration"] != "any"){ $filters[] = "videoDuration:{$duration}"; }
  1195. if($get["license"] != "any"){ $filters[] = "videoLicense:{$license}"; }
  1196. $filters = implode(",", $filters);
  1197. if($filters != ""){
  1198. $get_filters["iaf"] = $filters;
  1199. }
  1200. try{
  1201. $html =
  1202. $this->get(
  1203. $proxy,
  1204. "https://duckduckgo.com/",
  1205. $get_filters,
  1206. ddg::req_web
  1207. );
  1208. }catch(Exception $error){
  1209. throw new Exception("Failed to fetch search page");
  1210. }
  1211. preg_match(
  1212. '/vqd="([0-9-]+)"/',
  1213. $html,
  1214. $vqd
  1215. );
  1216. if(!isset($vqd[1])){
  1217. throw new Exception("Failed to grep VQD token");
  1218. }
  1219. $js_link =
  1220. "v.js?" .
  1221. http_build_query([
  1222. "l" => $get["country"],
  1223. "o" => "json",
  1224. "sr" => "1",
  1225. "q" => $get["s"],
  1226. "vqd" => $vqd[1],
  1227. "f" => $filters,
  1228. "p" => $nsfw
  1229. ]);
  1230. }
  1231. try{
  1232. $json =
  1233. $this->get(
  1234. $proxy,
  1235. "https://duckduckgo.com/" . $js_link,
  1236. [],
  1237. ddg::req_xhr
  1238. );
  1239. }catch(Exception $error){
  1240. throw new Exception("Failed to fetch JSON");
  1241. }
  1242. $json = json_decode($json, true);
  1243. if($json === null){
  1244. throw new Exception("Failed to decode JSON");
  1245. }
  1246. $out = [
  1247. "status" => "ok",
  1248. "npt" => null,
  1249. "video" => [],
  1250. "author" => [],
  1251. "livestream" => [],
  1252. "playlist" => [],
  1253. "reel" => []
  1254. ];
  1255. if(!isset($json["results"])){
  1256. return $out;
  1257. }
  1258. // get NPT
  1259. if(
  1260. isset($json["next"]) &&
  1261. $json["next"] !== null
  1262. ){
  1263. $out["npt"] =
  1264. $this->backend->store(
  1265. $json["next"],
  1266. "videos",
  1267. $proxy
  1268. );
  1269. }
  1270. foreach($json["results"] as $video){
  1271. $thumb = [
  1272. "ratio" => null,
  1273. "url" => null
  1274. ];
  1275. foreach(["large", "medium", "small"] as $contender){
  1276. if(isset($video["images"][$contender])){
  1277. $thumb = [
  1278. "ratio" => "16:9",
  1279. "url" => $this->bingimg($video["images"][$contender])
  1280. ];
  1281. break;
  1282. }
  1283. }
  1284. $out["video"][] = [
  1285. "title" => $this->titledots($video["title"]),
  1286. "description" => $this->titledots($video["description"]),
  1287. "author" => [
  1288. "name" =>
  1289. (
  1290. isset($video["uploader"]) &&
  1291. $video["uploader"] != ""
  1292. ) ?
  1293. $video["uploader"] : null,
  1294. "url" => null,
  1295. "avatar" => null
  1296. ],
  1297. "date" =>
  1298. (
  1299. isset($video["published"]) &&
  1300. $video["published"] != ""
  1301. ) ?
  1302. strtotime($video["published"]) : null,
  1303. "duration" =>
  1304. (
  1305. isset($video["duration"]) &&
  1306. $video["duration"] != ""
  1307. ) ?
  1308. $this->hms2int($video["duration"]) : null,
  1309. "views" =>
  1310. isset($video["statistics"]["viewCount"]) ?
  1311. (int)$video["statistics"]["viewCount"] : null,
  1312. "thumb" => $thumb,
  1313. "url" => $this->unshiturl($video["content"])
  1314. ];
  1315. }
  1316. return $out;
  1317. }
  1318. public function news($get){
  1319. if($get["npt"]){
  1320. [$js_link, $proxy] = $this->backend->get($get["npt"], "news");
  1321. }else{
  1322. if(strlen($get["s"]) === 0){
  1323. throw new Exception("Search term is empty!");
  1324. }
  1325. $proxy = $this->backend->get_ip();
  1326. $get_filters = [
  1327. "q" => $get["s"],
  1328. "iar" => "news",
  1329. "ia" => "news"
  1330. ];
  1331. if($get["date"] != "any"){
  1332. $date = $get["date"];
  1333. $get_filters["df"] = $date;
  1334. }else{
  1335. $date = "";
  1336. }
  1337. switch($get["nsfw"]){
  1338. case "yes": $get_filters["kp"] = "-2"; break;
  1339. case "maybe": $get_filters["kp"] = "-1"; break;
  1340. case "no": $get_filters["kp"] = "1"; break;
  1341. }
  1342. try{
  1343. $html =
  1344. $this->get(
  1345. $proxy,
  1346. "https://duckduckgo.com/",
  1347. $get_filters,
  1348. ddg::req_web
  1349. );
  1350. }catch(Exception $error){
  1351. throw new Exception("Failed to fetch search page");
  1352. }
  1353. preg_match(
  1354. '/vqd="([0-9-]+)"/',
  1355. $html,
  1356. $vqd
  1357. );
  1358. if(!isset($vqd[1])){
  1359. throw new Exception("Failed to grep VQD token");
  1360. }
  1361. $js_link =
  1362. "news.js?" .
  1363. http_build_query([
  1364. "l" => $get["country"],
  1365. "o" => "json",
  1366. "noamp" => "1",
  1367. "m" => "30",
  1368. "q" => $get["s"],
  1369. "vqd" => $vqd[1],
  1370. "p" => $get_filters["kp"],
  1371. "df" => $date,
  1372. "u" => "bing"
  1373. ]);
  1374. }
  1375. try{
  1376. $json =
  1377. $this->get(
  1378. $proxy,
  1379. "https://duckduckgo.com/" . $js_link,
  1380. [],
  1381. ddg::req_xhr
  1382. );
  1383. }catch(Exception $error){
  1384. throw new Exception("Failed to fetch JSON");
  1385. }
  1386. $json = json_decode($json, true);
  1387. if($json === null){
  1388. throw new Exception("Failed to decode JSON");
  1389. }
  1390. $out = [
  1391. "status" => "ok",
  1392. "npt" => null,
  1393. "news" => []
  1394. ];
  1395. if(!isset($json["results"])){
  1396. return $out;
  1397. }
  1398. // get NPT
  1399. if(
  1400. isset($json["next"]) &&
  1401. $json["next"] !== null
  1402. ){
  1403. $out["npt"] =
  1404. $this->backend->store(
  1405. $json["next"],
  1406. "news",
  1407. $proxy
  1408. );
  1409. }
  1410. foreach($json["results"] as $news){
  1411. if(
  1412. isset($news["image"]) &&
  1413. $news["image"] != ""
  1414. ){
  1415. $thumb = [
  1416. "ratio" => "16:9",
  1417. "url" => $news["image"]
  1418. ];
  1419. }else{
  1420. $thumb = [
  1421. "ratio" => null,
  1422. "url" => null
  1423. ];
  1424. }
  1425. $out["news"][] = [
  1426. "title" => $news["title"],
  1427. "author" =>
  1428. (
  1429. isset($news["source"]) &&
  1430. $news["source"] != ""
  1431. ) ?
  1432. $news["source"] : null,
  1433. "description" =>
  1434. (
  1435. isset($news["excerpt"]) &&
  1436. $news["excerpt"] != ""
  1437. ) ?
  1438. $this->fuckhtml
  1439. ->getTextContent(
  1440. $news["excerpt"]
  1441. ) : null,
  1442. "date" =>
  1443. isset($news["date"]) ?
  1444. (int)$news["date"] : null,
  1445. "thumb" => $thumb,
  1446. "url" => $this->unshiturl($news["url"])
  1447. ];
  1448. }
  1449. return $out;
  1450. }
  1451. private function titledots($title){
  1452. $substr = substr($title, -3);
  1453. if(
  1454. $substr == "..." ||
  1455. $substr == "…"
  1456. ){
  1457. return trim(substr($title, 0, -3));
  1458. }
  1459. return trim($title);
  1460. }
  1461. private function hms2int($time){
  1462. $parts = explode(":", $time, 3);
  1463. $time = 0;
  1464. if(count($parts) === 3){
  1465. // hours
  1466. $time = $time + ((int)$parts[0] * 3600);
  1467. array_shift($parts);
  1468. }
  1469. if(count($parts) === 2){
  1470. // minutes
  1471. $time = $time + ((int)$parts[0] * 60);
  1472. array_shift($parts);
  1473. }
  1474. // seconds
  1475. $time = $time + (int)$parts[0];
  1476. return $time;
  1477. }
  1478. private function unshiturl($url){
  1479. // check for domains w/out first short subdomain (ex: www.)
  1480. $domain = parse_url($url, PHP_URL_HOST);
  1481. $subdomain = preg_replace(
  1482. '/^[A-z0-9]{1,3}\./',
  1483. "",
  1484. $domain
  1485. );
  1486. switch($subdomain){
  1487. case "ebay.com.au":
  1488. case "ebay.at":
  1489. case "ebay.ca":
  1490. case "ebay.fr":
  1491. case "ebay.de":
  1492. case "ebay.com.hk":
  1493. case "ebay.ie":
  1494. case "ebay.it":
  1495. case "ebay.com.my":
  1496. case "ebay.nl":
  1497. case "ebay.ph":
  1498. case "ebay.pl":
  1499. case "ebay.com.sg":
  1500. case "ebay.es":
  1501. case "ebay.ch":
  1502. case "ebay.co.uk":
  1503. case "cafr.ebay.ca":
  1504. case "ebay.com":
  1505. case "community.ebay.com":
  1506. case "pages.ebay.com":
  1507. // remove ebay tracking elements
  1508. $old_params = parse_url($url, PHP_URL_QUERY);
  1509. parse_str($old_params, $params);
  1510. if(isset($params["mkevt"])){ unset($params["mkevt"]); }
  1511. if(isset($params["mkcid"])){ unset($params["mkcid"]); }
  1512. if(isset($params["mkrid"])){ unset($params["mkrid"]); }
  1513. if(isset($params["campid"])){ unset($params["campid"]); }
  1514. if(isset($params["customid"])){ unset($params["customid"]); }
  1515. if(isset($params["toolid"])){ unset($params["toolid"]); }
  1516. if(isset($params["_sop"])){ unset($params["_sop"]); }
  1517. if(isset($params["_dcat"])){ unset($params["_dcat"]); }
  1518. if(isset($params["epid"])){ unset($params["epid"]); }
  1519. if(isset($params["epid"])){ unset($params["oid"]); }
  1520. $params = http_build_query($params);
  1521. if(strlen($params) === 0){
  1522. $replace = "\?";
  1523. }else{
  1524. $replace = "";
  1525. }
  1526. $url = preg_replace(
  1527. "/" . $replace . preg_quote($old_params, "/") . "$/",
  1528. $params,
  1529. $url
  1530. );
  1531. break;
  1532. }
  1533. return $url;
  1534. }
  1535. private function bingimg($url){
  1536. $parse = parse_url($url);
  1537. parse_str($parse["query"], $parts);
  1538. return "https://" . $parse["host"] . "/th?id=" . urlencode($parts["id"]);
  1539. }
  1540. private function bingratio($width, $height){
  1541. $ratio = [
  1542. 474 / $width,
  1543. 474 / $height
  1544. ];
  1545. if($ratio[0] < $ratio[1]){
  1546. $ratio = $ratio[0];
  1547. }else{
  1548. $ratio = $ratio[1];
  1549. }
  1550. return [
  1551. floor($width * $ratio),
  1552. floor($height * $ratio)
  1553. ];
  1554. }
  1555. }