frontend.php 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. <?php
  2. class frontend{
  3. public function load($template, $replacements = []){
  4. $replacements["server_name"] = htmlspecialchars(config::SERVER_NAME);
  5. $replacements["version"] = config::VERSION;
  6. if(isset($_COOKIE["theme"])){
  7. $theme = str_replace(["/". "."], "", $_COOKIE["theme"]);
  8. if(
  9. $theme != "Dark" &&
  10. !is_file("static/themes/" . $theme . ".css")
  11. ){
  12. $theme = config::DEFAULT_THEME;
  13. }
  14. }else{
  15. $theme = config::DEFAULT_THEME;
  16. }
  17. if($theme != "Dark"){
  18. $replacements["style"] = '<link rel="stylesheet" href="/static/themes/' . rawurlencode($theme) . '.css?v' . config::VERSION . '">';
  19. }else{
  20. $replacements["style"] = "";
  21. }
  22. if(isset($_COOKIE["scraper_ac"])){
  23. $replacements["ac"] = '?ac=' . htmlspecialchars($_COOKIE["scraper_ac"]);
  24. }else{
  25. $replacements["ac"] = '';
  26. }
  27. if(
  28. isset($replacements["timetaken"]) &&
  29. $replacements["timetaken"] !== null
  30. ){
  31. $replacements["timetaken"] = '<div class="timetaken">Took ' . number_format(microtime(true) - $replacements["timetaken"], 2) . 's</div>';
  32. }
  33. $handle = fopen("template/{$template}", "r");
  34. $data = fread($handle, filesize("template/{$template}"));
  35. fclose($handle);
  36. $data = explode("\n", $data);
  37. $html = "";
  38. for($i=0; $i<count($data); $i++){
  39. $html .= trim($data[$i]);
  40. }
  41. foreach($replacements as $key => $value){
  42. $html =
  43. str_replace(
  44. "{%{$key}%}",
  45. $value,
  46. $html
  47. );
  48. }
  49. return trim($html);
  50. }
  51. public function loadheader(array $get, array $filters, string $page){
  52. echo
  53. $this->load("header.html", [
  54. "title" => trim(htmlspecialchars($get["s"]) . " ({$page})"),
  55. "description" => ucfirst($page) . ' search results for &quot;' . htmlspecialchars($get["s"]) . '&quot;',
  56. "index" => "no",
  57. "search" => htmlspecialchars($get["s"]),
  58. "tabs" => $this->generatehtmltabs($page, $get["s"]),
  59. "filters" => $this->generatehtmlfilters($filters, $get)
  60. ]);
  61. $headers_raw = getallheaders();
  62. $header_keys = [];
  63. $user_agent = "";
  64. $bad_header = false;
  65. foreach($headers_raw as $headerkey => $headervalue){
  66. $headerkey = strtolower($headerkey);
  67. if($headerkey == "user-agent"){
  68. $user_agent = $headervalue;
  69. continue;
  70. }
  71. // check header key
  72. if(in_array($headerkey, config::FILTERED_HEADER_KEYS)){
  73. $bad_header = true;
  74. break;
  75. }
  76. }
  77. if(
  78. preg_match(
  79. config::HEADER_REGEX,
  80. $user_agent
  81. ) ||
  82. $bad_header === true
  83. ){
  84. // bot detected !!
  85. apcu_inc("captcha_gen");
  86. $this->drawerror(
  87. "Tshh, blocked!",
  88. 'Your browser, IP or IP range has been blocked from this 4get instance. If this is an error, please <a href="/about">contact the administrator</a>.'
  89. );
  90. die();
  91. }
  92. }
  93. public function drawerror($title, $error, $timetaken = null){
  94. if($timetaken === null){
  95. $timetaken = microtime(true);
  96. }
  97. echo
  98. $this->load("search.html", [
  99. "timetaken" => $timetaken,
  100. "class" => "",
  101. "right-left" => "",
  102. "right-right" => "",
  103. "left" =>
  104. '<div class="infobox">' .
  105. '<h1>' . htmlspecialchars($title) . '</h1>' .
  106. $error .
  107. '</div>'
  108. ]);
  109. die();
  110. }
  111. public function drawscrapererror($error, $get, $target, $timetaken = null){
  112. if($timetaken === null){
  113. $timetaken = microtime(true);
  114. }
  115. $this->drawerror(
  116. "Shit",
  117. 'This scraper returned an error:' .
  118. '<div class="code">' . htmlspecialchars($error) . '</div>' .
  119. 'Things you can try:' .
  120. '<ul>' .
  121. '<li>Use a different scraper</li>' .
  122. '<li>Remove keywords that could cause errors</li>' .
  123. '<li><a href="/instances?target=' . $target . "&" . $this->buildquery($get, false) . '">Try your search on another 4get instance</a></li>' .
  124. '</ul><br>' .
  125. 'If the error persists, please <a href="/about">contact the administrator</a>.',
  126. $timetaken
  127. );
  128. }
  129. public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
  130. $payload =
  131. '<div class="text-result">';
  132. // add favicon, link and archive links
  133. $payload .= $this->drawlink($site["url"]);
  134. /*
  135. Draw title + description + filetype
  136. */
  137. $payload .=
  138. '<a href="' . htmlspecialchars($site["url"]) . '" class="hover" rel="noreferrer nofollow"';
  139. if($tabindex === false){
  140. $payload .= ' tabindex="-1"';
  141. }
  142. $payload .= '>';
  143. if($site["thumb"]["url"] !== null){
  144. $payload .=
  145. '<div class="thumb-wrap';
  146. switch($site["thumb"]["ratio"]){
  147. case "16:9":
  148. $size = "landscape";
  149. break;
  150. case "9:16":
  151. $payload .= " portrait";
  152. $size = "portrait";
  153. break;
  154. case "1:1":
  155. $payload .= " square";
  156. $size = "square";
  157. break;
  158. }
  159. $payload .=
  160. '">' .
  161. '<img class="thumb" src="' . $this->htmlimage($site["thumb"]["url"], $size) . '" alt="thumb">';
  162. if($duration !== null){
  163. $payload .=
  164. '<div class="duration">' .
  165. htmlspecialchars($duration) .
  166. '</div>';
  167. }
  168. $payload .=
  169. '</div>';
  170. }
  171. $payload .=
  172. '<div class="title">';
  173. if(
  174. isset($site["type"]) &&
  175. $site["type"] != "web"
  176. ){
  177. $payload .= '<div class="type">' . strtoupper($site["type"]) . '</div>';
  178. }
  179. $payload .=
  180. $this->highlighttext($keywords, $site["title"]) .
  181. '</div>';
  182. if($greentext !== null){
  183. $payload .=
  184. '<div class="greentext">' .
  185. htmlspecialchars($greentext) .
  186. '</div>';
  187. }
  188. if($site["description"] !== null){
  189. $payload .=
  190. '<div class="description">' .
  191. $this->highlighttext($keywords, $site["description"]) .
  192. '</div>';
  193. }
  194. $payload .= $customhtml;
  195. $payload .= '</a>';
  196. /*
  197. Sublinks
  198. */
  199. if(
  200. isset($site["sublink"]) &&
  201. !empty($site["sublink"])
  202. ){
  203. usort($site["sublink"], function($a, $b){
  204. return strlen($a["description"]) > strlen($b["description"]);
  205. });
  206. $payload .=
  207. '<div class="sublinks">' .
  208. '<table>';
  209. $opentr = false;
  210. for($i=0; $i<count($site["sublink"]); $i++){
  211. if(($i % 2) === 0){
  212. $opentr = true;
  213. $payload .= '<tr>';
  214. }else{
  215. $opentr = false;
  216. }
  217. $payload .=
  218. '<td>' .
  219. '<a href="' . htmlspecialchars($site["sublink"][$i]["url"]) . '" rel="noreferrer nofollow">' .
  220. '<div class="title">' .
  221. htmlspecialchars($site["sublink"][$i]["title"]) .
  222. '</div>';
  223. if(!empty($site["sublink"][$i]["date"])){
  224. $payload .=
  225. '<div class="greentext">' .
  226. date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
  227. '</div>';
  228. }
  229. if(!empty($site["sublink"][$i]["description"])){
  230. $payload .=
  231. '<div class="description">' .
  232. $this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
  233. '</div>';
  234. }
  235. $payload .= '</a></td>';
  236. if($opentr === false){
  237. $payload .= '</tr>';
  238. }
  239. }
  240. if($opentr === true){
  241. $payload .= '<td></td></tr>';
  242. }
  243. $payload .= '</table></div>';
  244. }
  245. if(
  246. isset($site["table"]) &&
  247. !empty($site["table"])
  248. ){
  249. $payload .= '<table class="info-table">';
  250. foreach($site["table"] as $title => $value){
  251. $payload .=
  252. '<tr>' .
  253. '<td>' . htmlspecialchars($title) . '</td>' .
  254. '<td>' . htmlspecialchars($value) . '</td>' .
  255. '</tr>';
  256. }
  257. $payload .= '</table>';
  258. }
  259. return $payload . '</div>';
  260. }
  261. public function highlighttext($keywords, $text){
  262. $text = htmlspecialchars($text);
  263. $keywords = explode(" ", $keywords);
  264. $regex = [];
  265. foreach($keywords as $word){
  266. $regex[] = "\b" . preg_quote($word, "/") . "\b";
  267. }
  268. $regex = "/" . implode("|", $regex) . "/i";
  269. return
  270. preg_replace(
  271. $regex,
  272. '<b>${0}</b>',
  273. $text
  274. );
  275. }
  276. function highlightcode($text){
  277. // https://www.php.net/highlight_string
  278. ini_set("highlight.comment", "c-comment");
  279. ini_set("highlight.default", "c-default");
  280. ini_set("highlight.html", "c-default");
  281. ini_set("highlight.keyword", "c-keyword");
  282. ini_set("highlight.string", "c-string");
  283. $text =
  284. trim(
  285. preg_replace(
  286. '/<\/span>$/',
  287. "", // remove stray ending span because of the <?php stuff
  288. str_replace(
  289. [
  290. '<br />',
  291. '&nbsp;'
  292. ],
  293. [
  294. "\n", // replace <br> with newlines
  295. " " // replace html entity to space
  296. ],
  297. str_replace(
  298. [
  299. // leading <?php garbage
  300. "<span style=\"color: c-default\">\n&lt;?php&nbsp;",
  301. "<code>",
  302. "</code>"
  303. ],
  304. "",
  305. highlight_string("<?php " . $text, true)
  306. )
  307. )
  308. )
  309. );
  310. // replace colors
  311. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  312. foreach($classes as $class){
  313. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  314. }
  315. return $text;
  316. }
  317. public function drawlink($link){
  318. /*
  319. Add favicon
  320. */
  321. $host = parse_url($link);
  322. $esc =
  323. explode(
  324. ".",
  325. $host["host"],
  326. 2
  327. );
  328. if(
  329. count($esc) === 2 &&
  330. $esc[0] == "www"
  331. ){
  332. $esc = $esc[1];
  333. }else{
  334. $esc = $esc[0];
  335. }
  336. $esc = substr($esc, 0, 2);
  337. $urlencode = urlencode($link);
  338. $payload =
  339. '<div class="url">' .
  340. '<button class="favicon" tabindex="-1">' .
  341. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  342. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  343. '</button>' .
  344. '<div class="favicon-dropdown">';
  345. /*
  346. Add archive links
  347. */
  348. if(
  349. $host["host"] == "boards.4chan.org" ||
  350. $host["host"] == "boards.4channel.org"
  351. ){
  352. $archives = [];
  353. $path = explode("/", $host["path"]);
  354. $count = count($path);
  355. // /pol/thread/417568063/post-shitty-memes-if-you-want-to
  356. if($count !== 0){
  357. $isboard = true;
  358. switch($path[1]){
  359. case "con":
  360. break;
  361. case "q":
  362. $archives[] = "desuarchive.org";
  363. break;
  364. case "qa":
  365. $archives[] = "desuarchive.org";
  366. break;
  367. case "qb":
  368. $archives[] = "arch.b4k.co";
  369. break;
  370. case "trash":
  371. $archives[] = "desuarchive.org";
  372. break;
  373. case "a":
  374. $archives[] = "desuarchive.org";
  375. break;
  376. case "c":
  377. $archives[] = "desuarchive.org";
  378. break;
  379. case "w":
  380. break;
  381. case "m":
  382. $archives[] = "desuarchive.org";
  383. break;
  384. case "cgl":
  385. $archives[] = "desuarchive.org";
  386. $archives[] = "warosu.org";
  387. break;
  388. case "f":
  389. $archives[] = "archive.4plebs.org";
  390. break;
  391. case "n":
  392. break;
  393. case "jp":
  394. $archives[] = "warosu.org";
  395. break;
  396. case "vt":
  397. $archives[] = "warosu.org";
  398. break;
  399. case "v":
  400. $archives[] = "arch.b4k.co";
  401. break;
  402. case "vg":
  403. $archives[] = "arch.b4k.co";
  404. break;
  405. case "vm":
  406. $archives[] = "arch.b4k.co";
  407. break;
  408. case "vmg":
  409. $archives[] = "arch.b4k.co";
  410. break;
  411. case "vp":
  412. $archives[] = "arch.b4k.co";
  413. break;
  414. case "vr":
  415. $archives[] = "desuarchive.org";
  416. $archives[] = "warosu.org";
  417. break;
  418. case "vrpg":
  419. $archives[] = "arch.b4k.co";
  420. break;
  421. case "vst":
  422. $archives[] = "arch.b4k.co";
  423. break;
  424. case "co":
  425. $archives[] = "desuarchive.org";
  426. break;
  427. case "g":
  428. $archives[] = "desuarchive.org";
  429. $archives[] = "arch.b4k.co";
  430. break;
  431. case "tv":
  432. $archives[] = "archive.4plebs.org";
  433. break;
  434. case "k":
  435. $archives[] = "desuarchive.org";
  436. break;
  437. case "o":
  438. $archives[] = "archive.4plebs.org";
  439. break;
  440. case "an":
  441. $archives[] = "desuarchive.org";
  442. break;
  443. case "tg":
  444. $archives[] = "desuarchive.org";
  445. $archives[] = "archive.4plebs.org";
  446. break;
  447. case "sp":
  448. $archives[] = "archive.4plebs.org";
  449. break;
  450. case "xs":
  451. $archives[] = "eientei.xyz";
  452. break;
  453. case "pw":
  454. break;
  455. case "sci":
  456. $archives[] = "warosu.org";
  457. $archives[] = "eientei.xyz";
  458. break;
  459. case "his":
  460. $archives[] = "desuarchive.org";
  461. break;
  462. case "int":
  463. $archives[] = "desuarchive.org";
  464. break;
  465. case "out":
  466. break;
  467. case "toy":
  468. break;
  469. case "i":
  470. $archives[] = "archiveofsins.com";
  471. $archives[] = "eientei.xyz";
  472. break;
  473. case "po":
  474. break;
  475. case "p":
  476. break;
  477. case "ck":
  478. $archives[] = "warosu.org";
  479. break;
  480. case "ic":
  481. $archives[] = "warosu.org";
  482. break;
  483. case "wg":
  484. break;
  485. case "lit":
  486. $archives[] = "warosu.org";
  487. break;
  488. case "mu":
  489. $archives[] = "desuarchive.org";
  490. break;
  491. case "fa":
  492. $archives[] = "warosu.org";
  493. break;
  494. case "3":
  495. $archives[] = "warosu.org";
  496. $archives[] = "eientei.xyz";
  497. break;
  498. case "gd":
  499. break;
  500. case "diy":
  501. $archives[] = "warosu.org";
  502. break;
  503. case "wsg":
  504. $archives[] = "desuarchive.org";
  505. break;
  506. case "qst":
  507. break;
  508. case "biz":
  509. $archives[] = "warosu.org";
  510. break;
  511. case "trv":
  512. $archives[] = "archive.4plebs.org";
  513. break;
  514. case "fit":
  515. $archives[] = "desuarchive.org";
  516. break;
  517. case "x":
  518. $archives[] = "archive.4plebs.org";
  519. break;
  520. case "adv":
  521. $archives[] = "archive.4plebs.org";
  522. break;
  523. case "lgbt":
  524. $archives[] = "archiveofsins.com";
  525. break;
  526. case "mlp":
  527. $archives[] = "desuarchive.org";
  528. $archives[] = "arch.b4k.co";
  529. break;
  530. case "news":
  531. break;
  532. case "wsr":
  533. break;
  534. case "vip":
  535. break;
  536. case "b":
  537. $archives[] = "thebarchive.com";
  538. break;
  539. case "r9k":
  540. $archives[] = "desuarchive.org";
  541. break;
  542. case "pol":
  543. $archives[] = "archive.4plebs.org";
  544. break;
  545. case "bant":
  546. $archives[] = "thebarchive.com";
  547. break;
  548. case "soc":
  549. $archives[] = "archiveofsins.com";
  550. break;
  551. case "s4s":
  552. $archives[] = "archive.4plebs.org";
  553. break;
  554. case "s":
  555. $archives[] = "archiveofsins.com";
  556. break;
  557. case "hc":
  558. $archives[] = "archiveofsins.com";
  559. break;
  560. case "hm":
  561. $archives[] = "archiveofsins.com";
  562. break;
  563. case "h":
  564. $archives[] = "archiveofsins.com";
  565. break;
  566. case "e":
  567. break;
  568. case "u":
  569. $archives[] = "archiveofsins.com";
  570. break;
  571. case "d":
  572. $archives[] = "desuarchive.org";
  573. break;
  574. case "t":
  575. $archives[] = "archiveofsins.com";
  576. break;
  577. case "hr":
  578. $archives[] = "archive.4plebs.org";
  579. break;
  580. case "gif":
  581. break;
  582. case "aco":
  583. $archives[] = "desuarchive.org";
  584. break;
  585. case "r":
  586. $archives[] = "archiveofsins.com";
  587. break;
  588. default:
  589. $isboard = false;
  590. break;
  591. }
  592. if($isboard === true){
  593. $archives[] = "archived.moe";
  594. }
  595. $trail = "";
  596. if(
  597. isset($path[2]) &&
  598. isset($path[3]) &&
  599. $path[2] == "thread"
  600. ){
  601. $trail .= "/" . $path[1] . "/thread/" . $path[3];
  602. }elseif($isboard){
  603. $trail = "/" . $path[1] . "/";
  604. }
  605. for($i=0; $i<count($archives); $i++){
  606. $payload .=
  607. '<a href="https://' . $archives[$i] . $trail . '" class="list" target="_BLANK">' .
  608. '<img src="/favicon?s=https://' . $archives[$i] . '" alt="' . $archives[$i][0] . $archives[$i][1] . '">' .
  609. $archives[$i] .
  610. '</a>';
  611. }
  612. }
  613. }
  614. $payload .=
  615. '<a href="https://webcache.googleusercontent.com/search?q=cache:' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://google.com" alt="go">Google cache</a>' .
  616. '<a href="https://web.archive.org/web/' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.org" alt="ar">Archive.org</a>' .
  617. '<a href="https://archive.ph/newest/' . htmlspecialchars($link) . '" class="list" target="_BLANK"><img src="/favicon?s=https://archive.is" alt="ar">Archive.is</a>' .
  618. '<a href="https://ghostarchive.org/search?term=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://ghostarchive.org" alt="gh">Ghostarchive</a>' .
  619. '<a href="https://www.bing.com/search?q=url%3A' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://bing.com" alt="bi">Bing cache</a>' .
  620. '<a href="https://megalodon.jp/?url=' . $urlencode . '" class="list" target="_BLANK"><img src="/favicon?s=https://megalodon.jp" alt="me">Megalodon</a>' .
  621. '</div>';
  622. /*
  623. Draw link
  624. */
  625. $parts = explode("/", $link);
  626. $clickurl = "";
  627. // remove trailing /
  628. $c = count($parts) - 1;
  629. if($parts[$c] == ""){
  630. $parts[$c - 1] = $parts[$c - 1] . "/";
  631. unset($parts[$c]);
  632. }
  633. // merge https://site together
  634. $parts = [
  635. $parts[0] . $parts[1] . '//' . $parts[2],
  636. ...array_slice($parts, 3, count($parts) - 1)
  637. ];
  638. $c = count($parts);
  639. for($i=0; $i<$c; $i++){
  640. if($i !== 0){ $clickurl .= "/"; }
  641. $clickurl .= $parts[$i];
  642. if($i === $c - 1){
  643. $parts[$i] = rtrim($parts[$i], "/");
  644. }
  645. $payload .=
  646. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  647. htmlspecialchars(urldecode($parts[$i])) .
  648. '</a>';
  649. if($i !== $c - 1){
  650. $payload .= '<span class="separator"></span>';
  651. }
  652. }
  653. return $payload . '</div>';
  654. }
  655. public function getscraperfilters($page){
  656. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  657. if(
  658. isset($_GET["scraper"]) &&
  659. is_string($_GET["scraper"])
  660. ){
  661. $get_scraper = $_GET["scraper"];
  662. }else{
  663. if(
  664. isset($_GET["npt"]) &&
  665. is_string($_GET["npt"])
  666. ){
  667. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  668. $get_scraper =
  669. preg_replace(
  670. '/[0-9]+$/',
  671. "",
  672. $get_scraper
  673. );
  674. }
  675. }
  676. // add search field
  677. $filters =
  678. [
  679. "s" => [
  680. "option" => "_SEARCH"
  681. ]
  682. ];
  683. // define default scrapers
  684. switch($page){
  685. case "web":
  686. $filters["scraper"] = [
  687. "display" => "Scraper",
  688. "option" => [
  689. "ddg" => "DuckDuckGo",
  690. "brave" => "Brave",
  691. "yandex" => "Yandex",
  692. "google" => "Google",
  693. "startpage" => "Startpage",
  694. "qwant" => "Qwant",
  695. "yep" => "Yep",
  696. "greppr" => "Greppr",
  697. "crowdview" => "Crowdview",
  698. "mwmbl" => "Mwmbl",
  699. "mojeek" => "Mojeek",
  700. "marginalia" => "Marginalia",
  701. "wiby" => "wiby",
  702. "curlie" => "Curlie"
  703. ]
  704. ];
  705. break;
  706. case "images":
  707. $filters["scraper"] = [
  708. "display" => "Scraper",
  709. "option" => [
  710. "ddg" => "DuckDuckGo",
  711. "yandex" => "Yandex",
  712. "brave" => "Brave",
  713. "google" => "Google",
  714. "startpage" => "Startpage",
  715. "qwant" => "Qwant",
  716. "yep" => "Yep",
  717. //"pinterest" => "Pinterest",
  718. "imgur" => "Imgur",
  719. "ftm" => "FindThatMeme"
  720. ]
  721. ];
  722. break;
  723. case "videos":
  724. $filters["scraper"] = [
  725. "display" => "Scraper",
  726. "option" => [
  727. "yt" => "YouTube",
  728. //"fb" => "Facebook videos",
  729. "ddg" => "DuckDuckGo",
  730. "brave" => "Brave",
  731. "yandex" => "Yandex",
  732. "google" => "Google",
  733. "startpage" => "Startpage",
  734. "qwant" => "Qwant"
  735. ]
  736. ];
  737. break;
  738. case "news":
  739. $filters["scraper"] = [
  740. "display" => "Scraper",
  741. "option" => [
  742. "ddg" => "DuckDuckGo",
  743. "brave" => "Brave",
  744. "google" => "Google",
  745. "startpage" => "Startpage",
  746. "qwant" => "Qwant",
  747. "yep" => "Yep",
  748. "mojeek" => "Mojeek"
  749. ]
  750. ];
  751. break;
  752. case "music":
  753. $filters["scraper"] = [
  754. "display" => "Scraper",
  755. "option" => [
  756. "sc" => "SoundCloud"
  757. //"spotify" => "Spotify"
  758. ]
  759. ];
  760. break;
  761. }
  762. // get scraper name from user input, or default out to preferred scraper
  763. $scraper_out = null;
  764. $first = true;
  765. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  766. if($first === true){
  767. $first = $scraper_name;
  768. }
  769. if($scraper_name == $get_scraper){
  770. $scraper_out = $scraper_name;
  771. }
  772. }
  773. if($scraper_out === null){
  774. $scraper_out = $first;
  775. }
  776. include "scraper/$scraper_out.php";
  777. $lib = new $scraper_out();
  778. // set scraper on $_GET
  779. $_GET["scraper"] = $scraper_out;
  780. // set nsfw on $_GET
  781. if(
  782. isset($_COOKIE["nsfw"]) &&
  783. !isset($_GET["nsfw"])
  784. ){
  785. $_GET["nsfw"] = $_COOKIE["nsfw"];
  786. }
  787. return
  788. [
  789. $lib,
  790. array_merge_recursive(
  791. $filters,
  792. $lib->getfilters($page)
  793. )
  794. ];
  795. }
  796. public function parsegetfilters($parameters, $whitelist){
  797. $sanitized = [];
  798. // add npt token
  799. if(
  800. isset($parameters["npt"]) &&
  801. is_string($parameters["npt"])
  802. ){
  803. $sanitized["npt"] = $parameters["npt"];
  804. }else{
  805. $sanitized["npt"] = false;
  806. }
  807. // we're iterating over $whitelist, so
  808. // you can't polluate $sanitized with useless
  809. // parameters
  810. foreach($whitelist as $parameter => $value){
  811. if(isset($parameters[$parameter])){
  812. if(!is_string($parameters[$parameter])){
  813. $sanitized[$parameter] = null;
  814. continue;
  815. }
  816. // parameter is already set, use that value
  817. $sanitized[$parameter] = $parameters[$parameter];
  818. }else{
  819. // parameter is not set, add it
  820. if(is_string($value["option"])){
  821. // special field: set default value manually
  822. switch($value["option"]){
  823. case "_DATE":
  824. // no date set
  825. $sanitized[$parameter] = false;
  826. break;
  827. case "_SEARCH":
  828. // no search set
  829. $sanitized[$parameter] = "";
  830. break;
  831. }
  832. }else{
  833. // set a default value
  834. $sanitized[$parameter] = array_keys($value["option"])[0];
  835. }
  836. }
  837. // sanitize input
  838. if(is_array($value["option"])){
  839. if(
  840. !in_array(
  841. $sanitized[$parameter],
  842. $keys = array_keys($value["option"])
  843. )
  844. ){
  845. $sanitized[$parameter] = $keys[0];
  846. }
  847. }else{
  848. // sanitize search & string
  849. switch($value["option"]){
  850. case "_DATE":
  851. if($sanitized[$parameter] !== false){
  852. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  853. if($sanitized[$parameter] <= 0){
  854. $sanitized[$parameter] = false;
  855. }
  856. }
  857. break;
  858. case "_SEARCH":
  859. // get search string
  860. $sanitized["s"] = trim($sanitized[$parameter]);
  861. }
  862. }
  863. }
  864. // invert dates if needed
  865. if(
  866. isset($sanitized["older"]) &&
  867. isset($sanitized["newer"]) &&
  868. $sanitized["newer"] !== false &&
  869. $sanitized["older"] !== false &&
  870. $sanitized["newer"] > $sanitized["older"]
  871. ){
  872. // invert
  873. [
  874. $sanitized["older"],
  875. $sanitized["newer"]
  876. ] = [
  877. $sanitized["newer"],
  878. $sanitized["older"]
  879. ];
  880. }
  881. return $sanitized;
  882. }
  883. public function s_to_timestamp($seconds){
  884. if(is_string($seconds)){
  885. return "LIVE";
  886. }
  887. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  888. }
  889. public function generatehtmltabs($page, $query){
  890. $html = null;
  891. foreach(["web", "images", "videos", "news", "music"] as $type){
  892. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  893. if(!empty($params)){
  894. $html .= $params;
  895. }
  896. $html .= '" class="tab';
  897. if($type == $page){
  898. $html .= ' selected';
  899. }
  900. $html .= '">' . ucfirst($type) . '</a>';
  901. }
  902. return $html;
  903. }
  904. public function generatehtmlfilters($filters, $params){
  905. $html = null;
  906. foreach($filters as $filter_name => $filter_values){
  907. if(!isset($filter_values["display"])){
  908. continue;
  909. }
  910. $output = true;
  911. $tmp =
  912. '<div class="filter">' .
  913. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  914. if(is_array($filter_values["option"])){
  915. $tmp .= '<select name="' . $filter_name . '">';
  916. foreach($filter_values["option"] as $option_name => $option_title){
  917. $tmp .= '<option value="' . $option_name . '"';
  918. if($params[$filter_name] == $option_name){
  919. $tmp .= ' selected';
  920. }
  921. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  922. }
  923. $tmp .= '</select>';
  924. }else{
  925. switch($filter_values["option"]){
  926. case "_DATE":
  927. $tmp .= '<input type="date" name="' . $filter_name . '"';
  928. if($params[$filter_name] !== false){
  929. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  930. }
  931. $tmp .= '>';
  932. break;
  933. default:
  934. $output = false;
  935. break;
  936. }
  937. }
  938. $tmp .= '</div>';
  939. if($output === true){
  940. $html .= $tmp;
  941. }
  942. }
  943. return $html;
  944. }
  945. public function buildquery($gets, $ommit = false){
  946. $out = [];
  947. foreach($gets as $key => $value){
  948. if(
  949. $value == null ||
  950. $value == false ||
  951. $key == "npt" ||
  952. $key == "extendedsearch" ||
  953. $value == "any" ||
  954. $value == "all" ||
  955. $key == "spellcheck" ||
  956. (
  957. $ommit === true &&
  958. $key == "s"
  959. )
  960. ){
  961. continue;
  962. }
  963. if(
  964. $key == "older" ||
  965. $key == "newer"
  966. ){
  967. $value = date("Y-m-d", (int)$value);
  968. }
  969. $out[$key] = $value;
  970. }
  971. return http_build_query($out);
  972. }
  973. public function htmlimage($image, $format){
  974. if(
  975. preg_match(
  976. '/^data:/',
  977. $image
  978. )
  979. ){
  980. return htmlspecialchars($image);
  981. }
  982. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  983. }
  984. public function htmlnextpage($gets, $npt, $page){
  985. $query = $this->buildquery($gets);
  986. return $page . "?" . $query . "&npt=" . $npt;
  987. }
  988. }