frontend.php 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. <?php
  2. class frontend{
  3. public function validateurl($url, $net_validate = false){
  4. $url_parts = parse_url($url);
  5. // check if required parts are there
  6. if(
  7. !isset($url_parts["scheme"]) ||
  8. !(
  9. $url_parts["scheme"] == "http" ||
  10. $url_parts["scheme"] == "https"
  11. ) ||
  12. !isset($url_parts["host"])
  13. ){
  14. return false;
  15. }
  16. if($net_validate){
  17. $ip =
  18. str_replace(
  19. ["[", "]"], // handle ipv6
  20. "",
  21. $url_parts["host"]
  22. );
  23. // if its not an IP
  24. if(!filter_var($ip, FILTER_VALIDATE_IP)){
  25. // resolve domain's IP
  26. $ip = gethostbyname($url_parts["host"] . ".");
  27. }
  28. // check if its localhost
  29. if(
  30. filter_var(
  31. $ip,
  32. FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE
  33. ) === false
  34. ){
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. public function load($template, $replacements = []){
  41. $replacements["server_name"] = htmlspecialchars(config::SERVER_NAME);
  42. $replacements["version"] = config::VERSION;
  43. if(isset($_COOKIE["theme"])){
  44. $theme = str_replace(["/". "."], "", $_COOKIE["theme"]);
  45. if(
  46. $theme != "Dark" &&
  47. !is_file("static/themes/" . $theme . ".css")
  48. ){
  49. $theme = config::DEFAULT_THEME;
  50. }
  51. }else{
  52. $theme = config::DEFAULT_THEME;
  53. }
  54. if($theme != "Dark"){
  55. $replacements["style"] = '<link rel="stylesheet" href="/static/themes/' . rawurlencode($theme) . '.css?v' . config::VERSION . '">';
  56. }else{
  57. $replacements["style"] = "";
  58. }
  59. if(isset($_COOKIE["scraper_ac"])){
  60. $replacements["ac"] = '?ac=' . htmlspecialchars($_COOKIE["scraper_ac"]);
  61. }else{
  62. $replacements["ac"] = '';
  63. }
  64. if(
  65. isset($replacements["timetaken"]) &&
  66. $replacements["timetaken"] !== null
  67. ){
  68. $replacements["timetaken"] = '<div class="timetaken">Took ' . number_format(microtime(true) - $replacements["timetaken"], 2) . 's</div>';
  69. }
  70. $handle = fopen("template/{$template}", "r");
  71. $data = fread($handle, filesize("template/{$template}"));
  72. fclose($handle);
  73. $data = explode("\n", $data);
  74. $html = "";
  75. for($i=0; $i<count($data); $i++){
  76. $html .= trim($data[$i]);
  77. }
  78. foreach($replacements as $key => $value){
  79. $html =
  80. str_replace(
  81. "{%{$key}%}",
  82. $value,
  83. $html
  84. );
  85. }
  86. return trim($html);
  87. }
  88. public function loadheader(array $get, array $filters, string $page, bool $increment_bot_counter = true){
  89. echo
  90. $this->load("header.html", [
  91. "title" => trim(htmlspecialchars($get["s"]) . " ({$page})"),
  92. "description" => ucfirst($page) . ' search results for &quot;' . htmlspecialchars($get["s"]) . '&quot;',
  93. "index" => "no",
  94. "search" => htmlspecialchars($get["s"]),
  95. "tabs" => $this->generatehtmltabs($page, $get["s"]),
  96. "filters" => $this->generatehtmlfilters($filters, $get)
  97. ]);
  98. $headers_raw = getallheaders();
  99. $header_keys = [];
  100. $user_agent = "";
  101. $bad_header = false;
  102. // block bots that present X-Forwarded-For, Via, etc
  103. foreach($headers_raw as $headerkey => $headervalue){
  104. $headerkey = strtolower($headerkey);
  105. if($headerkey == "user-agent"){
  106. $user_agent = $headervalue;
  107. continue;
  108. }
  109. // check header key
  110. if(in_array($headerkey, config::FILTERED_HEADER_KEYS)){
  111. $bad_header = true;
  112. break;
  113. }
  114. }
  115. // SSL check
  116. $bad_ssl = false;
  117. if(
  118. isset($_SERVER["https"]) &&
  119. $_SERVER["https"] == "on" &&
  120. isset($_SERVER["SSL_CIPHER"]) &&
  121. in_array($_SERVER["SSL_CIPHER"], config::FILTERED_HEADER_KEYS)
  122. ){
  123. $bad_ssl = true;
  124. }
  125. if(
  126. $bad_header === true ||
  127. $bad_ssl === true ||
  128. $user_agent == "" ||
  129. // user agent check
  130. preg_match(
  131. config::HEADER_REGEX,
  132. $user_agent
  133. )
  134. ){
  135. // bot detected !!
  136. if($increment_bot_counter){
  137. apcu_inc(intdiv(time(), 3600) . ".bot_requests", 1, $s, 262800);
  138. }
  139. $this->drawerror(
  140. "Tshh, blocked!",
  141. '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>.'
  142. );
  143. }
  144. }
  145. public function drawerror($title, $error, $timetaken = null){
  146. if($timetaken === null){
  147. $timetaken = microtime(true);
  148. }
  149. echo
  150. $this->load("search.html", [
  151. "timetaken" => $timetaken,
  152. "class" => "",
  153. "right-left" => "",
  154. "right-right" => "",
  155. "left" =>
  156. '<div class="infobox">' .
  157. '<h1>' . htmlspecialchars($title) . '</h1>' .
  158. $error .
  159. '</div>'
  160. ]);
  161. die();
  162. }
  163. public function drawscrapererror($error, $get, $target, $timetaken = null){
  164. if($timetaken === null){
  165. $timetaken = microtime(true);
  166. }
  167. $this->drawerror(
  168. "Shit",
  169. 'This scraper returned an error:' .
  170. '<div class="code">' . htmlspecialchars($error) . '</div>' .
  171. 'Things you can try:' .
  172. '<ul>' .
  173. '<li>Use a different scraper</li>' .
  174. '<li>Remove keywords that could cause errors</li>' .
  175. '<li><a href="/instances?target=' . $target . "&" . $this->buildquery($get, false) . '">Try your search on another 4get instance</a></li>' .
  176. '</ul><br>' .
  177. 'If the error persists, please <a href="/about">contact the administrator</a>.',
  178. $timetaken
  179. );
  180. }
  181. public function drawtextresult($site, $greentext = null, $duration = null, $keywords, $tabindex = true, $customhtml = null){
  182. $payload =
  183. '<div class="text-result">';
  184. // add favicon, link and archive links
  185. $payload .= $this->drawlink($site["url"]);
  186. /*
  187. Draw title + description + filetype
  188. */
  189. $payload .=
  190. '<a href="' . htmlspecialchars($site["url"]) . '" class="hover" rel="noreferrer nofollow"';
  191. if($tabindex === false){
  192. $payload .= ' tabindex="-1"';
  193. }
  194. $payload .= '>';
  195. if($site["thumb"]["url"] !== null){
  196. $payload .=
  197. '<div class="thumb-wrap';
  198. switch($site["thumb"]["ratio"]){
  199. case "16:9":
  200. $size = "landscape";
  201. break;
  202. case "9:16":
  203. $payload .= " portrait";
  204. $size = "portrait";
  205. break;
  206. case "1:1":
  207. $payload .= " square";
  208. $size = "square";
  209. break;
  210. }
  211. $payload .=
  212. '">' .
  213. '<img class="thumb" src="' . $this->htmlimage($site["thumb"]["url"], $size) . '" alt="thumb">';
  214. if($duration !== null){
  215. $payload .=
  216. '<div class="duration">' .
  217. htmlspecialchars($duration) .
  218. '</div>';
  219. }
  220. $payload .=
  221. '</div>';
  222. }
  223. $payload .=
  224. '<div class="title">';
  225. if(
  226. isset($site["type"]) &&
  227. $site["type"] != "web"
  228. ){
  229. $payload .= '<div class="type">' . strtoupper($site["type"]) . '</div>';
  230. }
  231. $payload .=
  232. $this->highlighttext($keywords, $site["title"]) .
  233. '</div>';
  234. if($greentext !== null){
  235. $payload .=
  236. '<div class="greentext">' .
  237. htmlspecialchars($greentext) .
  238. '</div>';
  239. }
  240. if($site["description"] !== null){
  241. $payload .=
  242. '<div class="description">' .
  243. $this->highlighttext($keywords, $site["description"]) .
  244. '</div>';
  245. }
  246. $payload .= $customhtml;
  247. $payload .= '</a>';
  248. /*
  249. Sublinks
  250. */
  251. if(
  252. isset($site["sublink"]) &&
  253. !empty($site["sublink"])
  254. ){
  255. usort($site["sublink"], function($a, $b){
  256. return strlen($a["description"]) > strlen($b["description"]);
  257. });
  258. $payload .=
  259. '<div class="sublinks">' .
  260. '<table>';
  261. $opentr = false;
  262. for($i=0; $i<count($site["sublink"]); $i++){
  263. if(($i % 2) === 0){
  264. $opentr = true;
  265. $payload .= '<tr>';
  266. }else{
  267. $opentr = false;
  268. }
  269. $payload .=
  270. '<td>' .
  271. '<a href="' . htmlspecialchars($site["sublink"][$i]["url"]) . '" rel="noreferrer nofollow">' .
  272. '<div class="title">' .
  273. htmlspecialchars($site["sublink"][$i]["title"]) .
  274. '</div>';
  275. if(!empty($site["sublink"][$i]["date"])){
  276. $payload .=
  277. '<div class="greentext">' .
  278. date("jS M y @ g:ia", $site["sublink"][$i]["date"]) .
  279. '</div>';
  280. }
  281. if(!empty($site["sublink"][$i]["description"])){
  282. $payload .=
  283. '<div class="description">' .
  284. $this->highlighttext($keywords, $site["sublink"][$i]["description"]) .
  285. '</div>';
  286. }
  287. $payload .= '</a></td>';
  288. if($opentr === false){
  289. $payload .= '</tr>';
  290. }
  291. }
  292. if($opentr === true){
  293. $payload .= '<td></td></tr>';
  294. }
  295. $payload .= '</table></div>';
  296. }
  297. if(
  298. isset($site["table"]) &&
  299. !empty($site["table"])
  300. ){
  301. $payload .= '<table class="info-table">';
  302. foreach($site["table"] as $title => $value){
  303. $payload .=
  304. '<tr>' .
  305. '<td>' . htmlspecialchars($title) . '</td>' .
  306. '<td>' . htmlspecialchars($value) . '</td>' .
  307. '</tr>';
  308. }
  309. $payload .= '</table>';
  310. }
  311. return $payload . '</div>';
  312. }
  313. public function highlighttext($keywords, $text){
  314. $text = htmlspecialchars($text);
  315. $keywords = explode(" ", $keywords);
  316. $regex = [];
  317. foreach($keywords as $word){
  318. $regex[] = "\b" . preg_quote($word, "/") . "\b";
  319. }
  320. $regex = "/" . implode("|", $regex) . "/i";
  321. return
  322. preg_replace(
  323. $regex,
  324. '<b>${0}</b>',
  325. $text
  326. );
  327. }
  328. function highlightcode($text){
  329. // https://www.php.net/highlight_string
  330. ini_set("highlight.comment", "c-comment");
  331. ini_set("highlight.default", "c-default");
  332. ini_set("highlight.html", "c-default");
  333. ini_set("highlight.keyword", "c-keyword");
  334. ini_set("highlight.string", "c-string");
  335. $text =
  336. trim(
  337. preg_replace(
  338. '/<code [^>]+>/',
  339. "",
  340. str_replace(
  341. [
  342. "<br />",
  343. "&nbsp;",
  344. "<pre>",
  345. "</pre>",
  346. "</code>"
  347. ],
  348. [
  349. "\n",
  350. " ",
  351. "",
  352. "",
  353. ""
  354. ],
  355. explode(
  356. "&lt;?php",
  357. highlight_string("<?php " . $text, true),
  358. 2
  359. )[1]
  360. )
  361. )
  362. );
  363. // replace colors
  364. $classes = ["c-comment", "c-default", "c-keyword", "c-string"];
  365. foreach($classes as $class){
  366. $text = str_replace('<span style="color: ' . $class . '">', '<span class="' . $class . '">', $text);
  367. }
  368. return $text;
  369. }
  370. public function drawlink($link){
  371. /*
  372. Add favicon
  373. */
  374. $host = parse_url($link);
  375. // special case for when we're not drawing a full url
  376. if(!isset($host["host"])){
  377. $payload =
  378. '<div class="url">' .
  379. '<button class="favicon" tabindex="-1">' .
  380. '<img src="/favicon?s=404" alt="xx">' .
  381. '</button>';
  382. }else{
  383. $esc =
  384. explode(
  385. ".",
  386. $host["host"],
  387. 2
  388. );
  389. if(
  390. count($esc) === 2 &&
  391. $esc[0] == "www"
  392. ){
  393. $esc = $esc[1];
  394. }else{
  395. $esc = $esc[0];
  396. }
  397. $esc = substr($esc, 0, 2);
  398. $urlencode = urlencode($link);
  399. $payload =
  400. '<div class="url">' .
  401. '<button class="favicon" tabindex="-1">' .
  402. '<img src="/favicon?s=' . htmlspecialchars($host["scheme"] . "://" . $host["host"]) . '" alt="' . htmlspecialchars($esc) . '">' .
  403. //'<img src="/404.php" alt="' . htmlspecialchars($esc) . '">' .
  404. '</button>' .
  405. '<div class="favicon-dropdown">';
  406. $archives = [];
  407. // add website-specific archives
  408. switch($host["host"]){
  409. case "www.youtube.com":
  410. case "music.youtube.com":
  411. case "youtube.com":
  412. case "m.youtube.com":
  413. case "youtu.be":
  414. if(
  415. (
  416. $host["host"] == "youtu.be" &&
  417. isset($host["path"]) &&
  418. preg_match(
  419. '/^\/([A-Za-z0-9_-]+)/',
  420. $host["path"],
  421. $slug
  422. )
  423. ) ||
  424. (
  425. isset($host["query"]) &&
  426. preg_match(
  427. '/v=([A-Za-z0-9_-]+)/',
  428. $host["query"],
  429. $slug
  430. )
  431. )
  432. ){
  433. // for watch?v=slug
  434. $archives[] = [
  435. "url" => "https://findyoutubevideo.thetechrobo.ca/?q=" . $slug[1],
  436. "favicon" => "findyoutubevideo.thetechrobo.ca",
  437. "favicon_alt" => "fi",
  438. "title" => "FindYouTubeVideo"
  439. ];
  440. $archives[] = [
  441. "url" => "https://filmot.com/video/" . $slug[1] . "/",
  442. "favicon" => "filmot.com",
  443. "favicon_alt" => "fi",
  444. "title" => "Filmot (metadata only)"
  445. ];
  446. }elseif(
  447. preg_match(
  448. '/^\/(?:channel)\/@?([A-Za-z0-9_-]+)/',
  449. $host["path"],
  450. $username
  451. )
  452. ){
  453. $archives[] = [
  454. "url" => "https://filmot.com/channel/" . $username[1] . "/",
  455. "favicon" => "filmot.com",
  456. "favicon_alt" => "fi",
  457. "title" => "Filmot (metadata only)"
  458. ];
  459. }
  460. break;
  461. case "twitch.tv":
  462. case "player.twitch.tv":
  463. case "www.twitch.tv":
  464. if(
  465. isset($host["path"]) &&
  466. preg_match(
  467. '/^\/([A-Za-z0-9_.]+)/',
  468. $host["path"],
  469. $username
  470. ) &&
  471. $username[1] != "videos"
  472. ){
  473. $archives[] = [
  474. "url" => "https://vodvod.top/channels/@" . $username[1],
  475. "favicon" => "vodvod.top",
  476. "favicon_alt" => "vo",
  477. "title" => "vodvod"
  478. ];
  479. $archives[] = [
  480. "url" => "https://twitchtracker.com/" . $username[1],
  481. "favicon" => "twitchtracker.com",
  482. "favicon_alt" => "tw",
  483. "title" => "TwitchTracker"
  484. ];
  485. }
  486. break;
  487. case "kick.com":
  488. case "player.kick.com":
  489. if(
  490. isset($host["path"]) &&
  491. preg_match(
  492. '/^\/([A-Za-z0-9_.]+)/',
  493. $host["path"],
  494. $username
  495. )
  496. ){
  497. $archives[] = [
  498. "url" => "https://lick.lolcat.ca/channel?name=" . $username[1],
  499. "favicon" => "lick.lolcat.ca",
  500. "favicon_alt" => "li",
  501. "title" => "lick"
  502. ];
  503. $archives[] = [
  504. "url" => "https://kicktracker.net/" . $username[1],
  505. "favicon" => "kicktracker.net",
  506. "favicon_alt" => "ki",
  507. "title" => "Kick tracker"
  508. ];
  509. }
  510. break;
  511. case "x.com":
  512. case "twitter.com":
  513. if(
  514. isset($host["path"]) &&
  515. preg_match(
  516. '/^\/([A-Za-z0-9_.]+)/',
  517. $host["path"],
  518. $username
  519. )
  520. ){
  521. $archives[] = [
  522. "url" => "https://web.archive.org/web/*/https://x.com/" . $username[1] . "/status*",
  523. "favicon" => "archive.org",
  524. "favicon_alt" => "ar",
  525. "title" => "Archive.org: Tweets &gt;2023"
  526. ];
  527. $archives[] = [
  528. "url" => "https://web.archive.org/web/*/https://twitter.com/" . $username[1] . "/status*",
  529. "favicon" => "archive.org",
  530. "favicon_alt" => "ar",
  531. "title" => "Archive.org: Tweets &lt;2023"
  532. ];
  533. }
  534. break;
  535. case "www.instagram.com":
  536. case "instagram.com":
  537. if(
  538. isset($host["path"]) &&
  539. preg_match(
  540. '/^\/([A-Za-z0-9_.]+)/',
  541. $host["path"],
  542. $username
  543. ) &&
  544. $username[1] != "p"
  545. ){
  546. $archives[] = [
  547. "url" => "https://instarchiver.net/users?q=" . $username[1],
  548. "favicon" => "instarchiver.net",
  549. "favicon_alt" => "in",
  550. "title" => "Instarchiver"
  551. ];
  552. $archives[] = [
  553. "url" => "https://www.storiesdb.ch/" . $username[1],
  554. "favicon" => "www.storiesdb.ch",
  555. "favicon_alt" => "st",
  556. "title" => "StoriesDB"
  557. ];
  558. }
  559. break;
  560. case "reddit.com":
  561. case "old.reddit.com":
  562. case "www.reddit.com":
  563. if(isset($host["path"])){
  564. // direct thread lookup
  565. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=pullpush
  566. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=artic_shift
  567. if(
  568. preg_match(
  569. '/^\/r\/[^\/]+\/comments\/[^?&]+/',
  570. $host["path"],
  571. $slug
  572. )
  573. ){
  574. $archives[] = [
  575. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=artic_shift",
  576. "favicon" => "reddit.com",
  577. "favicon_alt" => "re",
  578. "title" => "Artic Shift"
  579. ];
  580. $archives[] = [
  581. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=pullpush",
  582. "favicon" => "reddit.com",
  583. "favicon_alt" => "re",
  584. "title" => "PullPush"
  585. ];
  586. }
  587. // subreddit thread search
  588. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=submissions&sort=desc
  589. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=submissions&sort=desc
  590. // subreddit comment search
  591. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=comments&sort=desc
  592. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=comments&sort=desc
  593. elseif(
  594. preg_match(
  595. '/^\/r\/([^\/]+)\/(?:$|\?|&|search|wiki)/',
  596. $host["path"],
  597. $slug
  598. )
  599. ){
  600. $archives[] = [
  601. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=submissions&sort=desc",
  602. "favicon" => "reddit.com",
  603. "favicon_alt" => "re",
  604. "title" => "Artic Shift (Search threads)"
  605. ];
  606. $archives[] = [
  607. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=submissions&sort=desc",
  608. "favicon" => "reddit.com",
  609. "favicon_alt" => "re",
  610. "title" => "PullPush (Search threads)"
  611. ];
  612. $archives[] = [
  613. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=comments&sort=desc",
  614. "favicon" => "reddit.com",
  615. "favicon_alt" => "re",
  616. "title" => "Artic Shift (Search comments)"
  617. ];
  618. $archives[] = [
  619. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=comments&sort=desc",
  620. "favicon" => "reddit.com",
  621. "favicon_alt" => "re",
  622. "title" => "PullPush (Search comments)"
  623. ];
  624. }
  625. // user thread lookup
  626. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=artic_shift
  627. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=pullpush
  628. // user comment lookup
  629. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=artic_shift
  630. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=pullpush
  631. elseif(
  632. preg_match(
  633. '/^\/(?:u|user)\/([^\/]+)\//',
  634. $host["path"],
  635. $slug
  636. )
  637. ){
  638. $archives[] = [
  639. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=artic_shift",
  640. "favicon" => "reddit.com",
  641. "favicon_alt" => "re",
  642. "title" => "Artic Shift (Search threads)"
  643. ];
  644. $archives[] = [
  645. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=pullpush",
  646. "favicon" => "reddit.com",
  647. "favicon_alt" => "re",
  648. "title" => "PullPush (Search threads)"
  649. ];
  650. $archives[] = [
  651. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=artic_shift",
  652. "favicon" => "reddit.com",
  653. "favicon_alt" => "re",
  654. "title" => "Artic Shift (Search comments)"
  655. ];
  656. $archives[] = [
  657. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=pullpush",
  658. "favicon" => "reddit.com",
  659. "favicon_alt" => "re",
  660. "title" => "PullPush (Search comments)"
  661. ];
  662. }
  663. }
  664. break;
  665. }
  666. // detect git
  667. if(
  668. (
  669. stripos(
  670. $host["host"],
  671. "git."
  672. ) !== false ||
  673. $host["host"] == "codeberg.org" ||
  674. $host["host"] == "sourceforge.net" ||
  675. $host["host"] == "github.com"
  676. ) &&
  677. isset($host["path"]) &&
  678. preg_match(
  679. '/^(\/[^\/]+\/[^\/]+\/?)/',
  680. $host["path"],
  681. $edge
  682. )
  683. ){
  684. $archives[] = [
  685. "url" => "https://archive.softwareheritage.org/browse/origin/directory/?origin_url=" . urlencode($host["scheme"] . "://" . $host["host"] . $edge[1]),
  686. "favicon" => "www.softwareheritage.org",
  687. "favicon_alt" => "so",
  688. "title" => "SoftwareHeritage"
  689. ];
  690. }
  691. $archives =
  692. array_merge(
  693. $archives,
  694. [
  695. [
  696. "url" => "https://web.archive.org/web/" . $urlencode,
  697. "favicon" => "archive.org",
  698. "favicon_alt" => "ar",
  699. "title" => "Archive.org"
  700. ],
  701. [
  702. "url" => "https://archive.ph/newest/" . htmlspecialchars($link),
  703. "favicon" => "archive.ph",
  704. "favicon_alt" => "ar",
  705. "title" => "Archive.ph"
  706. ],
  707. [
  708. "url" => "https://yandex.com/search/?text=" . urlencode("url:" . $link),
  709. "favicon" => "yandex.com",
  710. "favicon_alt" => "ya",
  711. "title" => "Yandex cache"
  712. ],
  713. [
  714. "url" => "https://ghostarchive.org/search?term=" . $urlencode,
  715. "favicon" => "ghostarchive.org",
  716. "favicon_alt" => "gh",
  717. "title" => "Ghostarchive"
  718. ],
  719. [
  720. "url" => "https://arquivo.pt/wayback/" . htmlspecialchars($link),
  721. "favicon" => "arquivo.pt",
  722. "favicon_alt" => "ar",
  723. "title" => "Arquivo.pt"
  724. ],
  725. [
  726. "url" => "https://megalodon.jp/?url=" . $urlencode,
  727. "favicon" => "megalodon.jp",
  728. "favicon_alt" => "me",
  729. "title" => "Megalodon"
  730. ],
  731. [
  732. "url" => "https://www.webcitation.org/query?url=" . $urlencode,
  733. "favicon" => "webcitation.org",
  734. "favicon_alt" => "we",
  735. "title" => "Webcitation"
  736. ]
  737. ]
  738. );
  739. foreach($archives as $archive){
  740. $payload .= '<a href="' . $archive["url"] . '" class="list" target="_BLANK"><img src="/favicon?s=https://' . $archive["favicon"] . '" alt="' . $archive["favicon_alt"] . '">' . $archive["title"] . '</a>';
  741. }
  742. $payload .= '</div>';
  743. }
  744. /*
  745. Draw link
  746. */
  747. $parts = explode("/", $link);
  748. $clickurl = "";
  749. // remove trailing /
  750. $c = count($parts) - 1;
  751. if($parts[$c] == ""){
  752. $parts[$c - 1] = $parts[$c - 1] . "/";
  753. unset($parts[$c]);
  754. }
  755. // merge https://site together
  756. if(isset($host["host"])){
  757. $parts = [
  758. $parts[0] . $parts[1] . '//' . $parts[2],
  759. ...array_slice($parts, 3, count($parts) - 1)
  760. ];
  761. }
  762. $c = count($parts);
  763. for($i=0; $i<$c; $i++){
  764. if($i !== 0){ $clickurl .= "/"; }
  765. $clickurl .= $parts[$i];
  766. if($i === $c - 1){
  767. $parts[$i] = rtrim($parts[$i], "/");
  768. }
  769. $payload .=
  770. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  771. htmlspecialchars(urldecode($parts[$i])) .
  772. '</a>';
  773. if($i !== $c - 1){
  774. $payload .= '<span class="separator"></span>';
  775. }
  776. }
  777. return $payload . '</div>';
  778. }
  779. public function getscraperfilters($page){
  780. // hack: enable error reporting if configured
  781. if(config::DISPLAY_ERRORS === true){
  782. ini_set('display_errors', 1);
  783. ini_set('display_startup_errors', 1);
  784. }
  785. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  786. if(
  787. isset($_GET["scraper"]) &&
  788. is_string($_GET["scraper"])
  789. ){
  790. $get_scraper = $_GET["scraper"];
  791. }else{
  792. if(
  793. isset($_GET["npt"]) &&
  794. is_string($_GET["npt"])
  795. ){
  796. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  797. $get_scraper =
  798. preg_replace(
  799. '/[0-9]+$/',
  800. "",
  801. $get_scraper
  802. );
  803. }
  804. }
  805. // add search field
  806. $filters =
  807. [
  808. "s" => [
  809. "option" => "_SEARCH"
  810. ]
  811. ];
  812. // define default scrapers
  813. switch($page){
  814. case "web":
  815. $filters["scraper"] = [
  816. "display" => "Scraper",
  817. "option" => [
  818. //"fget" => "fget",
  819. "ddg" => "DuckDuckGo",
  820. //"yahoo" => "Yahoo!",
  821. "brave" => "Brave",
  822. "yandex" => "Yandex",
  823. "google" => "Google",
  824. "google_api" => "Google API",
  825. "google_cse" => "Google CSE",
  826. "yahoo_japan" => "Yahoo! JAPAN",
  827. "startpage" => "Startpage",
  828. "yep" => "Yep",
  829. "mwmbl" => "Mwmbl",
  830. "mojeek" => "Mojeek",
  831. "naver" => "Naver",
  832. "baidu" => "Baidu",
  833. "coccoc" => "Cốc Cốc",
  834. "solofield" => "Solofield",
  835. "marginalia" => "Marginalia",
  836. "purili" => "Purili",
  837. "wiby" => "wiby"
  838. ]
  839. ];
  840. break;
  841. case "images":
  842. $filters["scraper"] = [
  843. "display" => "Scraper",
  844. "option" => [
  845. "ddg" => "DuckDuckGo",
  846. "yandex" => "Yandex",
  847. "brave" => "Brave",
  848. "google" => "Google",
  849. "google_api" => "Google API",
  850. "google_cse" => "Google CSE",
  851. "yahoo_japan" => "Yahoo! JAPAN",
  852. "startpage" => "Startpage",
  853. "naver" => "Naver",
  854. "baidu" => "Baidu",
  855. "solofield" => "Solofield",
  856. "pinterest" => "Pinterest",
  857. "flickr" => "Flickr",
  858. "pexels" => "Pexels",
  859. "pixabay" => "Pixabay",
  860. "unsplash" => "Unsplash",
  861. "fivehpx" => "500px",
  862. "vsco" => "VSCO",
  863. "imgur" => "Imgur",
  864. "ftm" => "FindThatMeme"
  865. ]
  866. ];
  867. break;
  868. case "videos":
  869. $filters["scraper"] = [
  870. "display" => "Scraper",
  871. "option" => [
  872. "yt" => "YouTube",
  873. //"archiveorg" => "Archive.org",
  874. //"dailymotion" => "Dailymotion",
  875. "vimeo" => "Vimeo",
  876. //"odysee" => "Odysee",
  877. "sepiasearch" => "Sepia Search",
  878. //"fb" => "Facebook videos",
  879. "ddg" => "DuckDuckGo",
  880. "brave" => "Brave",
  881. "yandex" => "Yandex",
  882. "google" => "Google",
  883. "yahoo_japan" => "Yahoo! JAPAN",
  884. "startpage" => "Startpage",
  885. "naver" => "Naver",
  886. "baidu" => "Baidu",
  887. "coccoc" => "Cốc Cốc",
  888. "purili" => "Purili",
  889. "solofield" => "Solofield"
  890. ]
  891. ];
  892. break;
  893. case "news":
  894. $filters["scraper"] = [
  895. "display" => "Scraper",
  896. "option" => [
  897. "ddg" => "DuckDuckGo",
  898. "brave" => "Brave",
  899. "google" => "Google",
  900. "yahoo_japan" => "Yahoo! JAPAN",
  901. "startpage" => "Startpage",
  902. //"mojeek" => "Mojeek",
  903. "baidu" => "Baidu"
  904. ]
  905. ];
  906. break;
  907. case "music":
  908. $filters["scraper"] = [
  909. "display" => "Scraper",
  910. "option" => [
  911. "sc" => "SoundCloud",
  912. "swisscows" => "Swisscows (SoundCloud)"
  913. //"spotify" => "Spotify"
  914. ]
  915. ];
  916. break;
  917. case "booru":
  918. $filters["scraper"] = [
  919. "display" => "Scraper",
  920. "option" => [
  921. "safebooru" => "Safebooru",
  922. "konachan" => "Konachan",
  923. "tbib" => "The Big Imageboard",
  924. "gelbooru" => "Gelbooru",
  925. "yandere" => "Yande.re",
  926. "tbib" => "The Big Imageboard",
  927. "sankakucomplex" => "SankakuComplex",
  928. "soybooru" => "SoyBooru"
  929. ]
  930. ];
  931. break;
  932. }
  933. // get scraper name from user input, or default out to preferred scraper
  934. $scraper_out = null;
  935. $first = true;
  936. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  937. if($first === true){
  938. $first = $scraper_name;
  939. }
  940. if($scraper_name == $get_scraper){
  941. $scraper_out = $scraper_name;
  942. }
  943. }
  944. if($scraper_out === null){
  945. $scraper_out = $first;
  946. }
  947. include "scraper/$scraper_out.php";
  948. $lib = new $scraper_out();
  949. // set scraper on $_GET
  950. $_GET["scraper"] = $scraper_out;
  951. // set nsfw on $_GET
  952. if(
  953. isset($_COOKIE["nsfw"]) &&
  954. !isset($_GET["nsfw"])
  955. ){
  956. $_GET["nsfw"] = $_COOKIE["nsfw"];
  957. }
  958. return
  959. [
  960. $lib,
  961. array_merge_recursive(
  962. $filters,
  963. $lib->getfilters($page)
  964. )
  965. ];
  966. }
  967. public function parsegetfilters($parameters, $whitelist){
  968. $sanitized = [];
  969. // add npt token
  970. if(
  971. isset($parameters["npt"]) &&
  972. is_string($parameters["npt"])
  973. ){
  974. $sanitized["npt"] = $parameters["npt"];
  975. }else{
  976. $sanitized["npt"] = false;
  977. }
  978. // we're iterating over $whitelist, so
  979. // you can't polluate $sanitized with useless
  980. // parameters
  981. foreach($whitelist as $parameter => $value){
  982. if(isset($parameters[$parameter])){
  983. if(!is_string($parameters[$parameter])){
  984. $sanitized[$parameter] = null;
  985. continue;
  986. }
  987. // parameter is already set, use that value
  988. $sanitized[$parameter] = $parameters[$parameter];
  989. }else{
  990. // parameter is not set, add it
  991. if(is_string($value["option"])){
  992. // special field: set default value manually
  993. switch($value["option"]){
  994. case "_DATE":
  995. // no date set
  996. $sanitized[$parameter] = false;
  997. break;
  998. case "_SEARCH":
  999. // no search set
  1000. $sanitized[$parameter] = "";
  1001. break;
  1002. }
  1003. }else{
  1004. // set a default value
  1005. $sanitized[$parameter] = array_keys($value["option"])[0];
  1006. }
  1007. }
  1008. // sanitize input
  1009. if(is_array($value["option"])){
  1010. if(
  1011. !in_array(
  1012. $sanitized[$parameter],
  1013. $keys = array_keys($value["option"])
  1014. )
  1015. ){
  1016. $sanitized[$parameter] = $keys[0];
  1017. }
  1018. }else{
  1019. // sanitize search & string
  1020. switch($value["option"]){
  1021. case "_DATE":
  1022. if($sanitized[$parameter] !== false){
  1023. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  1024. if($sanitized[$parameter] <= 0){
  1025. $sanitized[$parameter] = false;
  1026. }
  1027. }
  1028. break;
  1029. case "_SEARCH":
  1030. // get search string
  1031. $sanitized["s"] = trim($sanitized[$parameter]);
  1032. }
  1033. }
  1034. }
  1035. // invert dates if needed
  1036. if(
  1037. isset($sanitized["older"]) &&
  1038. isset($sanitized["newer"]) &&
  1039. $sanitized["newer"] !== false &&
  1040. $sanitized["older"] !== false &&
  1041. $sanitized["newer"] > $sanitized["older"]
  1042. ){
  1043. // invert
  1044. [
  1045. $sanitized["older"],
  1046. $sanitized["newer"]
  1047. ] = [
  1048. $sanitized["newer"],
  1049. $sanitized["older"]
  1050. ];
  1051. }
  1052. return $sanitized;
  1053. }
  1054. public function s_to_timestamp($seconds){
  1055. if(is_string($seconds)){
  1056. return "LIVE";
  1057. }
  1058. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  1059. }
  1060. public function generatehtmltabs($page, $query){
  1061. $html = null;
  1062. //foreach(["web", "images", "videos", "news", "music", "booru"] as $type){
  1063. foreach(["web", "images", "videos", "news", "music"] as $type){
  1064. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  1065. if(!empty($params)){
  1066. $html .= $params;
  1067. }
  1068. $html .= '" class="tab';
  1069. if($type == $page){
  1070. $html .= ' selected';
  1071. }
  1072. $html .= '">' . ucfirst($type) . '</a>';
  1073. }
  1074. return $html;
  1075. }
  1076. public function generatehtmlfilters($filters, $params){
  1077. $html = null;
  1078. foreach($filters as $filter_name => $filter_values){
  1079. if(!isset($filter_values["display"])){
  1080. continue;
  1081. }
  1082. $output = true;
  1083. $tmp =
  1084. '<div class="filter">' .
  1085. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  1086. if(is_array($filter_values["option"])){
  1087. $tmp .= '<select name="' . $filter_name . '">';
  1088. foreach($filter_values["option"] as $option_name => $option_title){
  1089. $tmp .= '<option value="' . $option_name . '"';
  1090. if($params[$filter_name] == $option_name){
  1091. $tmp .= ' selected';
  1092. }
  1093. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  1094. }
  1095. $tmp .= '</select>';
  1096. }else{
  1097. switch($filter_values["option"]){
  1098. case "_DATE":
  1099. $tmp .= '<input type="date" name="' . $filter_name . '"';
  1100. if($params[$filter_name] !== false){
  1101. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  1102. }
  1103. $tmp .= '>';
  1104. break;
  1105. default:
  1106. $output = false;
  1107. break;
  1108. }
  1109. }
  1110. $tmp .= '</div>';
  1111. if($output === true){
  1112. $html .= $tmp;
  1113. }
  1114. }
  1115. return $html;
  1116. }
  1117. public function buildquery($gets, $ommit = false){
  1118. $out = [];
  1119. foreach($gets as $key => $value){
  1120. if(
  1121. $value == null ||
  1122. $value == false ||
  1123. $key == "npt" ||
  1124. $key == "extendedsearch" ||
  1125. $value == "any" ||
  1126. $value == "all" ||
  1127. $key == "spellcheck" ||
  1128. (
  1129. $ommit === true &&
  1130. $key == "s"
  1131. )
  1132. ){
  1133. continue;
  1134. }
  1135. if(
  1136. $key == "older" ||
  1137. $key == "newer"
  1138. ){
  1139. $value = date("Y-m-d", (int)$value);
  1140. }
  1141. $out[$key] = $value;
  1142. }
  1143. return http_build_query($out);
  1144. }
  1145. public function increment_real_reqs($scraper){
  1146. apcu_inc(intdiv(time(), 3600) . ".real_requests", 1, $s, 262800);
  1147. apcu_inc(intdiv(time(), 3600) . ".$scraper.requests", 1, $s, 262800);
  1148. }
  1149. public function htmlimage($image, $format){
  1150. if(
  1151. preg_match(
  1152. '/^data:/',
  1153. $image
  1154. )
  1155. ){
  1156. return htmlspecialchars($image);
  1157. }
  1158. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  1159. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  1160. }
  1161. public function htmlnextpage($gets, $npt, $page){
  1162. $query = $this->buildquery($gets);
  1163. return $page . "?" . $query . "&npt=" . $npt;
  1164. }
  1165. }