1
0

frontend.php 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. "qwant" => "Qwant",
  715. "yep" => "Yep",
  716. //"pinterest" => "Pinterest",
  717. "imgur" => "Imgur",
  718. "ftm" => "FindThatMeme"
  719. ]
  720. ];
  721. break;
  722. case "videos":
  723. $filters["scraper"] = [
  724. "display" => "Scraper",
  725. "option" => [
  726. "yt" => "YouTube",
  727. //"fb" => "Facebook videos",
  728. "ddg" => "DuckDuckGo",
  729. "brave" => "Brave",
  730. "yandex" => "Yandex",
  731. "google" => "Google",
  732. "qwant" => "Qwant"
  733. ]
  734. ];
  735. break;
  736. case "news":
  737. $filters["scraper"] = [
  738. "display" => "Scraper",
  739. "option" => [
  740. "ddg" => "DuckDuckGo",
  741. "brave" => "Brave",
  742. "google" => "Google",
  743. "qwant" => "Qwant",
  744. "yep" => "Yep",
  745. "mojeek" => "Mojeek"
  746. ]
  747. ];
  748. break;
  749. case "music":
  750. $filters["scraper"] = [
  751. "display" => "Scraper",
  752. "option" => [
  753. "sc" => "SoundCloud"
  754. //"spotify" => "Spotify"
  755. ]
  756. ];
  757. break;
  758. }
  759. // get scraper name from user input, or default out to preferred scraper
  760. $scraper_out = null;
  761. $first = true;
  762. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  763. if($first === true){
  764. $first = $scraper_name;
  765. }
  766. if($scraper_name == $get_scraper){
  767. $scraper_out = $scraper_name;
  768. }
  769. }
  770. if($scraper_out === null){
  771. $scraper_out = $first;
  772. }
  773. include "scraper/$scraper_out.php";
  774. $lib = new $scraper_out();
  775. // set scraper on $_GET
  776. $_GET["scraper"] = $scraper_out;
  777. // set nsfw on $_GET
  778. if(
  779. isset($_COOKIE["nsfw"]) &&
  780. !isset($_GET["nsfw"])
  781. ){
  782. $_GET["nsfw"] = $_COOKIE["nsfw"];
  783. }
  784. return
  785. [
  786. $lib,
  787. array_merge_recursive(
  788. $filters,
  789. $lib->getfilters($page)
  790. )
  791. ];
  792. }
  793. public function parsegetfilters($parameters, $whitelist){
  794. $sanitized = [];
  795. // add npt token
  796. if(
  797. isset($parameters["npt"]) &&
  798. is_string($parameters["npt"])
  799. ){
  800. $sanitized["npt"] = $parameters["npt"];
  801. }else{
  802. $sanitized["npt"] = false;
  803. }
  804. // we're iterating over $whitelist, so
  805. // you can't polluate $sanitized with useless
  806. // parameters
  807. foreach($whitelist as $parameter => $value){
  808. if(isset($parameters[$parameter])){
  809. if(!is_string($parameters[$parameter])){
  810. $sanitized[$parameter] = null;
  811. continue;
  812. }
  813. // parameter is already set, use that value
  814. $sanitized[$parameter] = $parameters[$parameter];
  815. }else{
  816. // parameter is not set, add it
  817. if(is_string($value["option"])){
  818. // special field: set default value manually
  819. switch($value["option"]){
  820. case "_DATE":
  821. // no date set
  822. $sanitized[$parameter] = false;
  823. break;
  824. case "_SEARCH":
  825. // no search set
  826. $sanitized[$parameter] = "";
  827. break;
  828. }
  829. }else{
  830. // set a default value
  831. $sanitized[$parameter] = array_keys($value["option"])[0];
  832. }
  833. }
  834. // sanitize input
  835. if(is_array($value["option"])){
  836. if(
  837. !in_array(
  838. $sanitized[$parameter],
  839. $keys = array_keys($value["option"])
  840. )
  841. ){
  842. $sanitized[$parameter] = $keys[0];
  843. }
  844. }else{
  845. // sanitize search & string
  846. switch($value["option"]){
  847. case "_DATE":
  848. if($sanitized[$parameter] !== false){
  849. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  850. if($sanitized[$parameter] <= 0){
  851. $sanitized[$parameter] = false;
  852. }
  853. }
  854. break;
  855. case "_SEARCH":
  856. // get search string
  857. $sanitized["s"] = trim($sanitized[$parameter]);
  858. }
  859. }
  860. }
  861. // invert dates if needed
  862. if(
  863. isset($sanitized["older"]) &&
  864. isset($sanitized["newer"]) &&
  865. $sanitized["newer"] !== false &&
  866. $sanitized["older"] !== false &&
  867. $sanitized["newer"] > $sanitized["older"]
  868. ){
  869. // invert
  870. [
  871. $sanitized["older"],
  872. $sanitized["newer"]
  873. ] = [
  874. $sanitized["newer"],
  875. $sanitized["older"]
  876. ];
  877. }
  878. return $sanitized;
  879. }
  880. public function s_to_timestamp($seconds){
  881. if(is_string($seconds)){
  882. return "LIVE";
  883. }
  884. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  885. }
  886. public function generatehtmltabs($page, $query){
  887. $html = null;
  888. foreach(["web", "images", "videos", "news", "music"] as $type){
  889. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  890. if(!empty($params)){
  891. $html .= $params;
  892. }
  893. $html .= '" class="tab';
  894. if($type == $page){
  895. $html .= ' selected';
  896. }
  897. $html .= '">' . ucfirst($type) . '</a>';
  898. }
  899. return $html;
  900. }
  901. public function generatehtmlfilters($filters, $params){
  902. $html = null;
  903. foreach($filters as $filter_name => $filter_values){
  904. if(!isset($filter_values["display"])){
  905. continue;
  906. }
  907. $output = true;
  908. $tmp =
  909. '<div class="filter">' .
  910. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  911. if(is_array($filter_values["option"])){
  912. $tmp .= '<select name="' . $filter_name . '">';
  913. foreach($filter_values["option"] as $option_name => $option_title){
  914. $tmp .= '<option value="' . $option_name . '"';
  915. if($params[$filter_name] == $option_name){
  916. $tmp .= ' selected';
  917. }
  918. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  919. }
  920. $tmp .= '</select>';
  921. }else{
  922. switch($filter_values["option"]){
  923. case "_DATE":
  924. $tmp .= '<input type="date" name="' . $filter_name . '"';
  925. if($params[$filter_name] !== false){
  926. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  927. }
  928. $tmp .= '>';
  929. break;
  930. default:
  931. $output = false;
  932. break;
  933. }
  934. }
  935. $tmp .= '</div>';
  936. if($output === true){
  937. $html .= $tmp;
  938. }
  939. }
  940. return $html;
  941. }
  942. public function buildquery($gets, $ommit = false){
  943. $out = [];
  944. foreach($gets as $key => $value){
  945. if(
  946. $value == null ||
  947. $value == false ||
  948. $key == "npt" ||
  949. $key == "extendedsearch" ||
  950. $value == "any" ||
  951. $value == "all" ||
  952. $key == "spellcheck" ||
  953. (
  954. $ommit === true &&
  955. $key == "s"
  956. )
  957. ){
  958. continue;
  959. }
  960. if(
  961. $key == "older" ||
  962. $key == "newer"
  963. ){
  964. $value = date("Y-m-d", (int)$value);
  965. }
  966. $out[$key] = $value;
  967. }
  968. return http_build_query($out);
  969. }
  970. public function htmlimage($image, $format){
  971. if(
  972. preg_match(
  973. '/^data:/',
  974. $image
  975. )
  976. ){
  977. return htmlspecialchars($image);
  978. }
  979. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  980. }
  981. public function htmlnextpage($gets, $npt, $page){
  982. $query = $this->buildquery($gets);
  983. return $page . "?" . $query . "&npt=" . $npt;
  984. }
  985. }