frontend.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. $fansites = [
  408. [
  409. "url" => "https://cum.st/creators?cq=",
  410. "favicon" => "cum.st",
  411. "favicon_alt" => "cu",
  412. "title" => "OnlyHaven"
  413. ],
  414. [
  415. "url" => "https://pawchive.pw/artists?q=",
  416. "favicon" => "pawchive.pw",
  417. "favicon_alt" => "pa",
  418. "title" => "Pawchive"
  419. ],
  420. [
  421. "url" => "https://bakemono.app/creators?q=",
  422. "favicon" => "bakemono.app",
  423. "favicon_alt" => "ba",
  424. "title" => "Bakemono"
  425. ],
  426. [
  427. "url" => "https://kemono.cr/artists?q=",
  428. "favicon" => "kemono.cr",
  429. "favicon_alt" => "ke",
  430. "title" => "Kemono"
  431. ],
  432. [
  433. "url" => "https://coomer.st/artists?q=",
  434. "favicon" => "coomer.st",
  435. "favicon_alt" => "co",
  436. "title" => "Coomer"
  437. ]
  438. ];
  439. // add website-specific archives
  440. switch($host["host"]){
  441. case "www.youtube.com":
  442. case "music.youtube.com":
  443. case "youtube.com":
  444. case "m.youtube.com":
  445. case "youtu.be":
  446. if(
  447. (
  448. $host["host"] == "youtu.be" &&
  449. isset($host["path"]) &&
  450. preg_match(
  451. '/^\/([A-Za-z0-9_-]+)/',
  452. $host["path"],
  453. $slug
  454. )
  455. ) ||
  456. (
  457. isset($host["query"]) &&
  458. preg_match(
  459. '/v=([A-Za-z0-9_-]+)/',
  460. $host["query"],
  461. $slug
  462. )
  463. )
  464. ){
  465. // for watch?v=slug
  466. $archives[] = [
  467. "url" => "https://findyoutubevideo.thetechrobo.ca/?q=" . $slug[1],
  468. "favicon" => "findyoutubevideo.thetechrobo.ca",
  469. "favicon_alt" => "fi",
  470. "title" => "FindYouTubeVideo"
  471. ];
  472. $archives[] = [
  473. "url" => "https://filmot.com/video/" . $slug[1] . "/",
  474. "favicon" => "filmot.com",
  475. "favicon_alt" => "fi",
  476. "title" => "Filmot (metadata only)"
  477. ];
  478. }elseif(
  479. preg_match(
  480. '/^\/(?:channel)\/@?([A-Za-z0-9_-]+)/',
  481. $host["path"],
  482. $username
  483. )
  484. ){
  485. $archives[] = [
  486. "url" => "https://filmot.com/channel/" . $username[1] . "/",
  487. "favicon" => "filmot.com",
  488. "favicon_alt" => "fi",
  489. "title" => "Filmot (metadata only)"
  490. ];
  491. }
  492. break;
  493. case "m.twitch.tv":
  494. case "twitch.tv":
  495. case "player.twitch.tv":
  496. case "www.twitch.tv":
  497. if(
  498. isset($host["path"]) &&
  499. preg_match(
  500. '/^\/([A-Za-z0-9_.]+)/',
  501. $host["path"],
  502. $username
  503. ) &&
  504. $username[1] != "videos"
  505. ){
  506. $archives[] = [
  507. "url" => "https://vodvod.top/channels/@" . $username[1],
  508. "favicon" => "vodvod.top",
  509. "favicon_alt" => "vo",
  510. "title" => "vodvod"
  511. ];
  512. $archives[] = [
  513. "url" => "https://twitchtracker.com/" . $username[1],
  514. "favicon" => "twitchtracker.com",
  515. "favicon_alt" => "tw",
  516. "title" => "TwitchTracker"
  517. ];
  518. }
  519. break;
  520. case "kick.com":
  521. case "player.kick.com":
  522. if(
  523. isset($host["path"]) &&
  524. preg_match(
  525. '/^\/([A-Za-z0-9_.]+)/',
  526. $host["path"],
  527. $username
  528. )
  529. ){
  530. $archives[] = [
  531. "url" => "https://lick.lolcat.ca/channel?name=" . $username[1],
  532. "favicon" => "lick.lolcat.ca",
  533. "favicon_alt" => "li",
  534. "title" => "lick"
  535. ];
  536. $archives[] = [
  537. "url" => "https://kicktracker.net/" . $username[1],
  538. "favicon" => "kicktracker.net",
  539. "favicon_alt" => "ki",
  540. "title" => "Kick tracker"
  541. ];
  542. }
  543. break;
  544. case "x.com":
  545. case "twitter.com":
  546. if(
  547. isset($host["path"]) &&
  548. preg_match(
  549. '/^\/([A-Za-z0-9_.]+)/',
  550. $host["path"],
  551. $username
  552. )
  553. ){
  554. $archives[] = [
  555. "url" => "https://web.archive.org/web/*/https://x.com/" . $username[1] . "/status*",
  556. "favicon" => "archive.org",
  557. "favicon_alt" => "ar",
  558. "title" => "Archive.org: Tweets &gt;2023"
  559. ];
  560. $archives[] = [
  561. "url" => "https://web.archive.org/web/*/https://twitter.com/" . $username[1] . "/status*",
  562. "favicon" => "archive.org",
  563. "favicon_alt" => "ar",
  564. "title" => "Archive.org: Tweets &lt;2023"
  565. ];
  566. }
  567. break;
  568. case "www.instagram.com":
  569. case "instagram.com":
  570. if(
  571. isset($host["path"]) &&
  572. preg_match(
  573. '/^\/([A-Za-z0-9_.]+)/',
  574. $host["path"],
  575. $username
  576. ) &&
  577. $username[1] != "p"
  578. ){
  579. $archives[] = [
  580. "url" => "https://instarchiver.net/users?q=" . $username[1],
  581. "favicon" => "instarchiver.net",
  582. "favicon_alt" => "in",
  583. "title" => "Instarchiver"
  584. ];
  585. $archives[] = [
  586. "url" => "https://www.storiesdb.ch/" . $username[1],
  587. "favicon" => "www.storiesdb.ch",
  588. "favicon_alt" => "st",
  589. "title" => "StoriesDB"
  590. ];
  591. }
  592. break;
  593. case "reddit.com":
  594. case "old.reddit.com":
  595. case "www.reddit.com":
  596. if(isset($host["path"])){
  597. // direct thread lookup
  598. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=pullpush
  599. // https://ihsoyct.github.io/r/selfhosted/comments/16emfv0/4get_a_proxy_search_engine_that_doesnt_suck/?backend=artic_shift
  600. if(
  601. preg_match(
  602. '/^\/r\/[^\/]+\/comments\/[^?&]+/',
  603. $host["path"],
  604. $slug
  605. )
  606. ){
  607. $archives[] = [
  608. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=artic_shift",
  609. "favicon" => "reddit.com",
  610. "favicon_alt" => "re",
  611. "title" => "Artic Shift"
  612. ];
  613. $archives[] = [
  614. "url" => "https://ihsoyct.github.io{$slug[0]}?backend=pullpush",
  615. "favicon" => "reddit.com",
  616. "favicon_alt" => "re",
  617. "title" => "PullPush"
  618. ];
  619. }
  620. // subreddit thread search
  621. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=submissions&sort=desc
  622. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=submissions&sort=desc
  623. // subreddit comment search
  624. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=artic_shift&mode=comments&sort=desc
  625. // https://ihsoyct.github.io/?subreddit=selfhosted&backend=pullpush&mode=comments&sort=desc
  626. elseif(
  627. preg_match(
  628. '/^\/r\/([^\/]+)\/(?:(?:search|wiki|about)\/?)?(?:$|\?|&)/',
  629. $host["path"],
  630. $slug
  631. )
  632. ){
  633. $archives[] = [
  634. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=submissions&sort=desc",
  635. "favicon" => "reddit.com",
  636. "favicon_alt" => "re",
  637. "title" => "Artic Shift (Search threads)"
  638. ];
  639. $archives[] = [
  640. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=submissions&sort=desc",
  641. "favicon" => "reddit.com",
  642. "favicon_alt" => "re",
  643. "title" => "PullPush (Search threads)"
  644. ];
  645. $archives[] = [
  646. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=artic_shift&mode=comments&sort=desc",
  647. "favicon" => "reddit.com",
  648. "favicon_alt" => "re",
  649. "title" => "Artic Shift (Search comments)"
  650. ];
  651. $archives[] = [
  652. "url" => "https://ihsoyct.github.io/?subreddit={$slug[1]}&backend=pullpush&mode=comments&sort=desc",
  653. "favicon" => "reddit.com",
  654. "favicon_alt" => "re",
  655. "title" => "PullPush (Search comments)"
  656. ];
  657. }
  658. // user thread lookup
  659. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=artic_shift
  660. // https://ihsoyct.github.io/index.html?author=google&mode=submissions&backend=pullpush
  661. // user comment lookup
  662. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=artic_shift
  663. // https://ihsoyct.github.io/index.html?author=google&mode=comments&backend=pullpush
  664. elseif(
  665. preg_match(
  666. '/^\/(?:u|user)\/([^\/]+)\/(?:(?:comments|submitted)\/?)?(?:$|\?|&)/',
  667. $host["path"],
  668. $slug
  669. )
  670. ){
  671. $archives[] = [
  672. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=artic_shift",
  673. "favicon" => "reddit.com",
  674. "favicon_alt" => "re",
  675. "title" => "Artic Shift (Search threads)"
  676. ];
  677. $archives[] = [
  678. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=submissions&backend=pullpush",
  679. "favicon" => "reddit.com",
  680. "favicon_alt" => "re",
  681. "title" => "PullPush (Search threads)"
  682. ];
  683. $archives[] = [
  684. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=artic_shift",
  685. "favicon" => "reddit.com",
  686. "favicon_alt" => "re",
  687. "title" => "Artic Shift (Search comments)"
  688. ];
  689. $archives[] = [
  690. "url" => "https://ihsoyct.github.io/index.html?author={$slug[1]}&mode=comments&backend=pullpush",
  691. "favicon" => "reddit.com",
  692. "favicon_alt" => "re",
  693. "title" => "PullPush (Search comments)"
  694. ];
  695. }
  696. }
  697. break;
  698. // https://cum.st/creators?cq= onlyfans fansly patreon
  699. // https://pawchive.pw/artists?q= patreon pixiv/fanbox discord
  700. // https://bakemono.app/creators?q= pixiv/fanbox fansly onlyfans patreon
  701. // https://kemono.cr/artists?q= patreon pixiv/fanbox fantia afdian boosty gumroad subscribestar dlsite
  702. // https://coomer.st/artists?q= onlyfans fansly candfans
  703. // cant look up
  704. /*
  705. case "fantia.jp": // https://fantia.jp/fanclubs/ <integer>
  706. */
  707. // normal
  708. case "onlyfans.com": // https://onlyfans.com/ <username>
  709. case "fansly.com": // https://fansly.com/ <username>
  710. case "fanbox.cc":
  711. case "patreon.com": // https://www.patreon.com/ <username>
  712. case "www.patreon.com":
  713. case "boosty.to": // https://boosty.to/ <username>
  714. case "www.subscribestar.com": // https://www.subscribestar.com/ <username>
  715. case "subscribestar.com":
  716. case "www.dlsite.com": // https://www.dlsite.com/ <username> /
  717. case "dlsite.com":
  718. case "candfans.com": // https://candfans.com/ <username>
  719. case "afdian.com": // https://afdian.com/a/ <username>
  720. if(
  721. isset($host["path"]) &&
  722. (
  723. (
  724. $host["host"] == "afdian.com" &&
  725. preg_match(
  726. '/^\/a\/([A-Za-z0-9-_.]+)/',
  727. $host["path"],
  728. $username
  729. )
  730. ) ||
  731. preg_match(
  732. '/^\/([A-Za-z0-9-_.]+)/',
  733. $host["path"],
  734. $username
  735. )
  736. )
  737. ){
  738. foreach($fansites as $fansite){
  739. $archives[] = [
  740. "url" => $fansite["url"] . $this->sanitize_slug($username[1]),
  741. "favicon" => $fansite["favicon"],
  742. "favicon_alt" => $fansite["favicon_alt"],
  743. "title" => $fansite["title"]
  744. ];
  745. }
  746. }
  747. break;
  748. }
  749. // special fansite cases
  750. if(
  751. preg_match(
  752. '/^([A-Za-z0-9_]+)\.(?:fanbox\.cc|gumroad\.com)/',
  753. $host["host"],
  754. $username
  755. )
  756. ){
  757. foreach($fansites as $fansite){
  758. $archives[] = [
  759. "url" => $fansite["url"] . $this->sanitize_slug($username[1]),
  760. "favicon" => $fansite["favicon"],
  761. "favicon_alt" => $fansite["favicon_alt"],
  762. "title" => $fansite["title"]
  763. ];
  764. }
  765. }
  766. // detect git
  767. if(
  768. (
  769. stripos(
  770. $host["host"],
  771. "git."
  772. ) !== false ||
  773. $host["host"] == "codeberg.org" ||
  774. $host["host"] == "sourceforge.net" ||
  775. $host["host"] == "github.com"
  776. ) &&
  777. isset($host["path"]) &&
  778. preg_match(
  779. '/^(\/[^\/]+\/[^\/]+\/?)/',
  780. $host["path"],
  781. $edge
  782. )
  783. ){
  784. $archives[] = [
  785. "url" => "https://archive.softwareheritage.org/browse/origin/directory/?origin_url=" . urlencode($host["scheme"] . "://" . $host["host"] . $edge[1]),
  786. "favicon" => "www.softwareheritage.org",
  787. "favicon_alt" => "so",
  788. "title" => "SoftwareHeritage"
  789. ];
  790. }
  791. $archives =
  792. array_merge(
  793. $archives,
  794. [
  795. [
  796. "url" => "https://web.archive.org/web/" . $urlencode,
  797. "favicon" => "archive.org",
  798. "favicon_alt" => "ar",
  799. "title" => "Archive.org"
  800. ],
  801. [
  802. "url" => "https://archive.ph/newest/" . htmlspecialchars($link),
  803. "favicon" => "archive.ph",
  804. "favicon_alt" => "ar",
  805. "title" => "Archive.ph"
  806. ],
  807. [
  808. "url" => "https://yandex.com/search/?text=" . urlencode("url:" . $link),
  809. "favicon" => "yandex.com",
  810. "favicon_alt" => "ya",
  811. "title" => "Yandex cache"
  812. ],
  813. [
  814. "url" => "https://ghostarchive.org/search?term=" . $urlencode,
  815. "favicon" => "ghostarchive.org",
  816. "favicon_alt" => "gh",
  817. "title" => "Ghostarchive"
  818. ],
  819. [
  820. "url" => "https://arquivo.pt/wayback/" . htmlspecialchars($link),
  821. "favicon" => "arquivo.pt",
  822. "favicon_alt" => "ar",
  823. "title" => "Arquivo.pt"
  824. ],
  825. [
  826. "url" => "https://megalodon.jp/?url=" . $urlencode,
  827. "favicon" => "megalodon.jp",
  828. "favicon_alt" => "me",
  829. "title" => "Megalodon"
  830. ],
  831. [
  832. "url" => "https://www.webcitation.org/query?url=" . $urlencode,
  833. "favicon" => "webcitation.org",
  834. "favicon_alt" => "we",
  835. "title" => "Webcitation"
  836. ]
  837. ]
  838. );
  839. foreach($archives as $archive){
  840. $payload .= '<a href="' . $archive["url"] . '" class="list" target="_BLANK"><img src="/favicon?s=https://' . $archive["favicon"] . '" alt="' . $archive["favicon_alt"] . '">' . $archive["title"] . '</a>';
  841. }
  842. $payload .= '</div>';
  843. }
  844. /*
  845. Draw link
  846. */
  847. $parts = explode("/", $link);
  848. $clickurl = "";
  849. // remove trailing /
  850. $c = count($parts) - 1;
  851. if($parts[$c] == ""){
  852. $parts[$c - 1] = $parts[$c - 1] . "/";
  853. unset($parts[$c]);
  854. }
  855. // merge https://site together
  856. if(isset($host["host"])){
  857. $parts = [
  858. $parts[0] . $parts[1] . '//' . $parts[2],
  859. ...array_slice($parts, 3, count($parts) - 1)
  860. ];
  861. }
  862. $c = count($parts);
  863. for($i=0; $i<$c; $i++){
  864. if($i !== 0){ $clickurl .= "/"; }
  865. $clickurl .= $parts[$i];
  866. if($i === $c - 1){
  867. $parts[$i] = rtrim($parts[$i], "/");
  868. }
  869. $payload .=
  870. '<a class="part" href="' . htmlspecialchars($clickurl) . '" rel="noreferrer nofollow" tabindex="-1">' .
  871. htmlspecialchars(urldecode($parts[$i])) .
  872. '</a>';
  873. if($i !== $c - 1){
  874. $payload .= '<span class="separator"></span>';
  875. }
  876. }
  877. return $payload . '</div>';
  878. }
  879. public function getscraperfilters($page){
  880. // hack: enable error reporting if configured
  881. if(config::DISPLAY_ERRORS === true){
  882. ini_set('display_errors', 1);
  883. ini_set('display_startup_errors', 1);
  884. }
  885. $get_scraper = isset($_COOKIE["scraper_$page"]) ? $_COOKIE["scraper_$page"] : null;
  886. if(
  887. isset($_GET["scraper"]) &&
  888. is_string($_GET["scraper"])
  889. ){
  890. $get_scraper = $_GET["scraper"];
  891. }else{
  892. if(
  893. isset($_GET["npt"]) &&
  894. is_string($_GET["npt"])
  895. ){
  896. $get_scraper = explode(".", $_GET["npt"], 2)[0];
  897. $get_scraper =
  898. preg_replace(
  899. '/[0-9]+$/',
  900. "",
  901. $get_scraper
  902. );
  903. }
  904. }
  905. // add search field
  906. $filters =
  907. [
  908. "s" => [
  909. "option" => "_SEARCH"
  910. ]
  911. ];
  912. // define default scrapers
  913. switch($page){
  914. case "web":
  915. $filters["scraper"] = [
  916. "display" => "Scraper",
  917. "option" => [
  918. //"fget" => "fget",
  919. "ddg" => "DuckDuckGo",
  920. //"yahoo" => "Yahoo!",
  921. "brave" => "Brave",
  922. "yandex" => "Yandex",
  923. "google" => "Google",
  924. "google_api" => "Google API",
  925. "google_cse" => "Google CSE",
  926. "yahoo_japan" => "Yahoo! JAPAN",
  927. "startpage" => "Startpage",
  928. "yep" => "Yep",
  929. "mwmbl" => "Mwmbl",
  930. "mojeek" => "Mojeek",
  931. "naver" => "Naver",
  932. "baidu" => "Baidu",
  933. "coccoc" => "Cốc Cốc",
  934. "solofield" => "Solofield",
  935. "marginalia" => "Marginalia",
  936. "purili" => "Purili",
  937. "wiby" => "wiby"
  938. ]
  939. ];
  940. break;
  941. case "images":
  942. $filters["scraper"] = [
  943. "display" => "Scraper",
  944. "option" => [
  945. "ddg" => "DuckDuckGo",
  946. "yandex" => "Yandex",
  947. "brave" => "Brave",
  948. "google" => "Google",
  949. "google_api" => "Google API",
  950. "google_cse" => "Google CSE",
  951. "yahoo_japan" => "Yahoo! JAPAN",
  952. "startpage" => "Startpage",
  953. "naver" => "Naver",
  954. "baidu" => "Baidu",
  955. "solofield" => "Solofield",
  956. "pinterest" => "Pinterest",
  957. "flickr" => "Flickr",
  958. "pexels" => "Pexels",
  959. "pixabay" => "Pixabay",
  960. "unsplash" => "Unsplash",
  961. "fivehpx" => "500px",
  962. "vsco" => "VSCO",
  963. "imgur" => "Imgur",
  964. "ftm" => "FindThatMeme"
  965. ]
  966. ];
  967. break;
  968. case "videos":
  969. $filters["scraper"] = [
  970. "display" => "Scraper",
  971. "option" => [
  972. "yt" => "YouTube",
  973. //"archiveorg" => "Archive.org",
  974. //"dailymotion" => "Dailymotion",
  975. "vimeo" => "Vimeo",
  976. //"odysee" => "Odysee",
  977. "sepiasearch" => "Sepia Search",
  978. //"fb" => "Facebook videos",
  979. "ddg" => "DuckDuckGo",
  980. "brave" => "Brave",
  981. "yandex" => "Yandex",
  982. "google" => "Google",
  983. "yahoo_japan" => "Yahoo! JAPAN",
  984. "startpage" => "Startpage",
  985. "naver" => "Naver",
  986. "baidu" => "Baidu",
  987. "coccoc" => "Cốc Cốc",
  988. "purili" => "Purili",
  989. "solofield" => "Solofield"
  990. ]
  991. ];
  992. break;
  993. case "news":
  994. $filters["scraper"] = [
  995. "display" => "Scraper",
  996. "option" => [
  997. "ddg" => "DuckDuckGo",
  998. "brave" => "Brave",
  999. "google" => "Google",
  1000. "yahoo_japan" => "Yahoo! JAPAN",
  1001. "startpage" => "Startpage",
  1002. //"mojeek" => "Mojeek",
  1003. "baidu" => "Baidu"
  1004. ]
  1005. ];
  1006. break;
  1007. case "music":
  1008. $filters["scraper"] = [
  1009. "display" => "Scraper",
  1010. "option" => [
  1011. "sc" => "SoundCloud",
  1012. "swisscows" => "Swisscows (SoundCloud)"
  1013. //"spotify" => "Spotify"
  1014. ]
  1015. ];
  1016. break;
  1017. case "booru":
  1018. $filters["scraper"] = [
  1019. "display" => "Scraper",
  1020. "option" => [
  1021. "safebooru" => "Safebooru",
  1022. "konachan" => "Konachan",
  1023. "tbib" => "The Big Imageboard",
  1024. "gelbooru" => "Gelbooru",
  1025. "yandere" => "Yande.re",
  1026. "tbib" => "The Big Imageboard",
  1027. "sankakucomplex" => "SankakuComplex",
  1028. "soybooru" => "SoyBooru"
  1029. ]
  1030. ];
  1031. break;
  1032. }
  1033. // get scraper name from user input, or default out to preferred scraper
  1034. $scraper_out = null;
  1035. $first = true;
  1036. foreach($filters["scraper"]["option"] as $scraper_name => $scraper_pretty){
  1037. if($first === true){
  1038. $first = $scraper_name;
  1039. }
  1040. if($scraper_name == $get_scraper){
  1041. $scraper_out = $scraper_name;
  1042. }
  1043. }
  1044. if($scraper_out === null){
  1045. $scraper_out = $first;
  1046. }
  1047. include "scraper/$scraper_out.php";
  1048. $lib = new $scraper_out();
  1049. // set scraper on $_GET
  1050. $_GET["scraper"] = $scraper_out;
  1051. // set nsfw on $_GET
  1052. if(
  1053. isset($_COOKIE["nsfw"]) &&
  1054. !isset($_GET["nsfw"])
  1055. ){
  1056. $_GET["nsfw"] = $_COOKIE["nsfw"];
  1057. }
  1058. return
  1059. [
  1060. $lib,
  1061. array_merge_recursive(
  1062. $filters,
  1063. $lib->getfilters($page)
  1064. )
  1065. ];
  1066. }
  1067. public function parsegetfilters($parameters, $whitelist){
  1068. $sanitized = [];
  1069. // add npt token
  1070. if(
  1071. isset($parameters["npt"]) &&
  1072. is_string($parameters["npt"])
  1073. ){
  1074. $sanitized["npt"] = $parameters["npt"];
  1075. }else{
  1076. $sanitized["npt"] = false;
  1077. }
  1078. // we're iterating over $whitelist, so
  1079. // you can't polluate $sanitized with useless
  1080. // parameters
  1081. foreach($whitelist as $parameter => $value){
  1082. if(isset($parameters[$parameter])){
  1083. if(!is_string($parameters[$parameter])){
  1084. $sanitized[$parameter] = null;
  1085. continue;
  1086. }
  1087. // parameter is already set, use that value
  1088. $sanitized[$parameter] = $parameters[$parameter];
  1089. }else{
  1090. // parameter is not set, add it
  1091. if(is_string($value["option"])){
  1092. // special field: set default value manually
  1093. switch($value["option"]){
  1094. case "_DATE":
  1095. // no date set
  1096. $sanitized[$parameter] = false;
  1097. break;
  1098. case "_SEARCH":
  1099. // no search set
  1100. $sanitized[$parameter] = "";
  1101. break;
  1102. }
  1103. }else{
  1104. // set a default value
  1105. $sanitized[$parameter] = array_keys($value["option"])[0];
  1106. }
  1107. }
  1108. // sanitize input
  1109. if(is_array($value["option"])){
  1110. if(
  1111. !in_array(
  1112. $sanitized[$parameter],
  1113. $keys = array_keys($value["option"])
  1114. )
  1115. ){
  1116. $sanitized[$parameter] = $keys[0];
  1117. }
  1118. }else{
  1119. // sanitize search & string
  1120. switch($value["option"]){
  1121. case "_DATE":
  1122. if($sanitized[$parameter] !== false){
  1123. $sanitized[$parameter] = strtotime($sanitized[$parameter]);
  1124. if($sanitized[$parameter] <= 0){
  1125. $sanitized[$parameter] = false;
  1126. }
  1127. }
  1128. break;
  1129. case "_SEARCH":
  1130. // get search string
  1131. $sanitized["s"] = trim($sanitized[$parameter]);
  1132. }
  1133. }
  1134. }
  1135. // invert dates if needed
  1136. if(
  1137. isset($sanitized["older"]) &&
  1138. isset($sanitized["newer"]) &&
  1139. $sanitized["newer"] !== false &&
  1140. $sanitized["older"] !== false &&
  1141. $sanitized["newer"] > $sanitized["older"]
  1142. ){
  1143. // invert
  1144. [
  1145. $sanitized["older"],
  1146. $sanitized["newer"]
  1147. ] = [
  1148. $sanitized["newer"],
  1149. $sanitized["older"]
  1150. ];
  1151. }
  1152. return $sanitized;
  1153. }
  1154. public function s_to_timestamp($seconds){
  1155. if(is_string($seconds)){
  1156. return "LIVE";
  1157. }
  1158. return ($seconds >= 60) ? ltrim(gmdate("H:i:s", $seconds), ":0") : gmdate("0:s", $seconds);
  1159. }
  1160. public function generatehtmltabs($page, $query){
  1161. $html = null;
  1162. //foreach(["web", "images", "videos", "news", "music", "booru"] as $type){
  1163. foreach(["web", "images", "videos", "news", "music"] as $type){
  1164. $html .= '<a href="/' . $type . '?s=' . urlencode($query);
  1165. if(!empty($params)){
  1166. $html .= $params;
  1167. }
  1168. $html .= '" class="tab';
  1169. if($type == $page){
  1170. $html .= ' selected';
  1171. }
  1172. $html .= '">' . ucfirst($type) . '</a>';
  1173. }
  1174. return $html;
  1175. }
  1176. public function generatehtmlfilters($filters, $params){
  1177. $html = null;
  1178. foreach($filters as $filter_name => $filter_values){
  1179. if(!isset($filter_values["display"])){
  1180. continue;
  1181. }
  1182. $output = true;
  1183. $tmp =
  1184. '<div class="filter">' .
  1185. '<div class="title">' . htmlspecialchars($filter_values["display"]) . '</div>';
  1186. if(is_array($filter_values["option"])){
  1187. $tmp .= '<select name="' . $filter_name . '">';
  1188. foreach($filter_values["option"] as $option_name => $option_title){
  1189. $tmp .= '<option value="' . $option_name . '"';
  1190. if($params[$filter_name] == $option_name){
  1191. $tmp .= ' selected';
  1192. }
  1193. $tmp .= '>' . htmlspecialchars($option_title) . '</option>';
  1194. }
  1195. $tmp .= '</select>';
  1196. }else{
  1197. switch($filter_values["option"]){
  1198. case "_DATE":
  1199. $tmp .= '<input type="date" name="' . $filter_name . '"';
  1200. if($params[$filter_name] !== false){
  1201. $tmp .= ' value="' . date("Y-m-d", $params[$filter_name]) . '"';
  1202. }
  1203. $tmp .= '>';
  1204. break;
  1205. default:
  1206. $output = false;
  1207. break;
  1208. }
  1209. }
  1210. $tmp .= '</div>';
  1211. if($output === true){
  1212. $html .= $tmp;
  1213. }
  1214. }
  1215. return $html;
  1216. }
  1217. public function buildquery($gets, $ommit = false){
  1218. $out = [];
  1219. foreach($gets as $key => $value){
  1220. if(
  1221. $value == null ||
  1222. $value == false ||
  1223. $key == "npt" ||
  1224. $key == "extendedsearch" ||
  1225. $value == "any" ||
  1226. $value == "all" ||
  1227. $key == "spellcheck" ||
  1228. (
  1229. $ommit === true &&
  1230. $key == "s"
  1231. )
  1232. ){
  1233. continue;
  1234. }
  1235. if(
  1236. $key == "older" ||
  1237. $key == "newer"
  1238. ){
  1239. $value = date("Y-m-d", (int)$value);
  1240. }
  1241. $out[$key] = $value;
  1242. }
  1243. return http_build_query($out);
  1244. }
  1245. private function sanitize_slug($username){
  1246. return urlencode(preg_replace('/[-_]/', " ", $username));
  1247. }
  1248. public function increment_real_reqs($scraper){
  1249. apcu_inc(intdiv(time(), 3600) . ".real_requests", 1, $s, 262800);
  1250. apcu_inc(intdiv(time(), 3600) . ".$scraper.requests", 1, $s, 262800);
  1251. }
  1252. public function htmlimage($image, $format){
  1253. if(
  1254. preg_match(
  1255. '/^data:/',
  1256. $image
  1257. )
  1258. ){
  1259. return htmlspecialchars($image);
  1260. }
  1261. //return "https://4get.ca/proxy?i=" . urlencode($image) . "&s=" . $format;
  1262. return "/proxy?i=" . urlencode($image) . "&s=" . $format;
  1263. }
  1264. public function htmlnextpage($gets, $npt, $page){
  1265. $query = $this->buildquery($gets);
  1266. return $page . "?" . $query . "&npt=" . $npt;
  1267. }
  1268. }