1
0

frontend.php 26 KB

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