1
0

web.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /*
  3. Initialize random shit
  4. */
  5. include "data/config.php";
  6. include "lib/frontend.php";
  7. $frontend = new frontend();
  8. [$scraper, $filters] = $frontend->getscraperfilters("web");
  9. $get = $frontend->parsegetfilters($_GET, $filters);
  10. /*
  11. Captcha
  12. */
  13. include "lib/bot_protection.php";
  14. new bot_protection($frontend, $get, $filters, "web", true);
  15. $payload = [
  16. "timetaken" => microtime(true),
  17. "class" => "",
  18. "right-left" => "",
  19. "right-right" => "",
  20. "left" => ""
  21. ];
  22. // increment request count
  23. $frontend->increment_real_reqs($get["scraper"]);
  24. try{
  25. $results = $scraper->web($get);
  26. }catch(Exception $error){
  27. $frontend->drawscrapererror($error->getMessage(), $get, "web", $payload["timetaken"]);
  28. }
  29. /*
  30. Prepend Oracle output, if applicable
  31. */
  32. include("oracles/encoder.php");
  33. include("oracles/calc.php");
  34. include("oracles/time.php");
  35. include("oracles/numerics.php");
  36. $oracles = [new calculator(), new encoder(), new time(), new numerics()];
  37. $fortune = "";
  38. foreach ($oracles as $oracle) {
  39. if ($oracle->check_query($_GET["s"])) {
  40. $resp = $oracle->generate_response($_GET["s"]);
  41. if ($resp != "") {
  42. $fortune .= "<div class=\"infobox\">";
  43. foreach ($resp as $title => $r) {
  44. if ($title) {
  45. $fortune .= "<h3>".htmlspecialchars($title)."</h3><div class=\"code\">".htmlspecialchars($r)."</div>";
  46. }
  47. else {
  48. $fortune .= "<i>".$r."</i><br>";
  49. }
  50. }
  51. $fortune .= "<small>Answer provided by oracle: ".$oracle->info["name"]."</small></div>";
  52. }
  53. break;
  54. }
  55. }
  56. $payload["left"] = $fortune;
  57. $answerlen = 0;
  58. /*
  59. Spelling checker
  60. */
  61. if($results["spelling"]["type"] != "no_correction"){
  62. switch($results["spelling"]["type"]){
  63. case "including":
  64. $type = "Including results for";
  65. break;
  66. case "not_many":
  67. $type = "Not many results contains";
  68. break;
  69. }
  70. $payload["left"] .=
  71. '<div class="infobox">' .
  72. $type . ' <b>' . htmlspecialchars($results["spelling"]["using"]) . '</b>.<br>' .
  73. 'Did you mean <a href="?s=' .
  74. urlencode($results["spelling"]["correction"]) .
  75. '&' .
  76. $frontend->buildquery($get, true) .
  77. '&spellcheck=no">' .
  78. htmlspecialchars($results["spelling"]["correction"]) .
  79. '</a>?' .
  80. '</div>';
  81. }
  82. /*
  83. Populate links
  84. */
  85. if(count($results["web"]) === 0){
  86. $payload["left"] .=
  87. '<div class="infobox">' .
  88. "<h1>Nobody here but us chickens!</h1>" .
  89. 'Have you tried:' .
  90. '<ul>' .
  91. '<li>Using a different scraper</li>' .
  92. '<li>Using fewer keywords</li>' .
  93. '<li>Defining broader filters (Is NSFW turned off?)</li>' .
  94. '</ul>' .
  95. '</div>';
  96. }
  97. foreach($results["web"] as $site){
  98. $n = null;
  99. if($site["date"] !== null){
  100. $date = date("jS M y @ g:ia", $site["date"]);
  101. }else{
  102. $date = null;
  103. }
  104. $payload["left"] .= $frontend->drawtextresult($site, $date, $n, $get["s"]);
  105. }
  106. $right = [];
  107. /*
  108. Generate images
  109. */
  110. if(count($results["image"]) !== 0){
  111. $answerlen++;
  112. $right["image"] =
  113. '<div class="answer-wrapper">' .
  114. '<input id="answer' . $answerlen . '" class="spoiler" type="checkbox">' .
  115. '<div class="answer">' .
  116. '<div class="answer-title">' .
  117. '<a class="answer-title" href="/images?s=' . urlencode($get["s"]) . '"><h2>Images</h2></a>' .
  118. '</div>' .
  119. '<div class="images">';
  120. foreach($results["image"] as $image){
  121. $right["image"] .=
  122. '<a class="image" href="' . htmlspecialchars($image["url"]) . '" rel="noreferrer nofollow" title="' . htmlspecialchars($image["title"]) . '" data-json="' . htmlspecialchars(json_encode($image["source"])) . '" tabindex="-1">' .
  123. '<img src="' . $frontend->htmlimage($image["source"][count($image["source"]) - 1]["url"], "square") . '" alt="thumb">';
  124. if(
  125. $image["source"][0]["width"] !== null &&
  126. $image["source"][0]["height"] !== null
  127. ){
  128. $right["image"] .= '<div class="duration">' . $image["source"][0]["width"] . 'x' . $image["source"][0]["height"] . '</div>';
  129. }
  130. $right["image"] .= '</a>';
  131. }
  132. $right["image"] .=
  133. '</div></div>' .
  134. '<label class="spoiler-button" for="answer' . $answerlen . '"></label></div>';
  135. }
  136. /*
  137. Generate videos
  138. */
  139. if(count($results["video"]) !== 0){
  140. $answerlen++;
  141. $right["video"] =
  142. '<div class="answer-wrapper">' .
  143. '<input id="answer' . $answerlen . '" class="spoiler" type="checkbox">' .
  144. '<div class="answer">' .
  145. '<div class="answer-title">' .
  146. '<a class="answer-title" href="/videos?s=' . urlencode($get["s"]) . '"><h2>Videos</h2></a>' .
  147. '</div>';
  148. foreach($results["video"] as $video){
  149. if($video["views"] !== null){
  150. $greentext = number_format($video["views"]) . " views";
  151. }else{
  152. $greentext = null;
  153. }
  154. if($video["date"] !== null){
  155. if($greentext !== null){
  156. $greentext .= " • ";
  157. }
  158. $greentext .= date("jS M y @ g:ia", $video["date"]);
  159. }
  160. if($video["duration"] !== null){
  161. if($video["duration"] == "_LIVE"){
  162. $duration = 'LIVE';
  163. }else{
  164. $duration = $frontend->s_to_timestamp($video["duration"]);
  165. }
  166. }else{
  167. $duration = null;
  168. }
  169. $right["video"] .= $frontend->drawtextresult($video, $greentext, $duration, $get["s"], false);
  170. }
  171. $right["video"] .=
  172. '</div>' .
  173. '<label class="spoiler-button" for="answer' . $answerlen . '"></label></div>';
  174. }
  175. /*
  176. Generate news
  177. */
  178. if(count($results["news"]) !== 0){
  179. $answerlen++;
  180. $right["news"] =
  181. '<div class="answer-wrapper">' .
  182. '<input id="answer' . $answerlen . '" class="spoiler" type="checkbox">' .
  183. '<div class="answer">' .
  184. '<div class="answer-title">' .
  185. '<a class="answer-title" href="/news?s=' . urlencode($get["s"]) . '"><h2>News</h2></a>' .
  186. '</div>';
  187. foreach($results["news"] as $news){
  188. if($news["date"] !== null){
  189. $greentext = date("jS M y @ g:ia", $news["date"]);
  190. }else{
  191. $greentext = null;
  192. }
  193. $right["news"] .= $frontend->drawtextresult($news, $greentext, null, $get["s"], false);
  194. }
  195. $right["news"] .=
  196. '</div>' .
  197. '<label class="spoiler-button" for="answer' . $answerlen . '"></label></div>';
  198. }
  199. /*
  200. Generate answers
  201. */
  202. if(count($results["answer"]) !== 0){
  203. $right["answer"] = "";
  204. foreach($results["answer"] as $answer){
  205. $answerlen++;
  206. $right["answer"] .=
  207. '<div class="answer-wrapper">' .
  208. '<input id="answer' . $answerlen . '" class="spoiler" type="checkbox">' .
  209. '<div class="answer"><div class="wiki-head">';
  210. if(!empty($answer["title"])){
  211. $right["answer"] .=
  212. '<div class="answer-title">';
  213. if(!empty($answer["url"])){
  214. $right["answer"] .= '<a class="answer-title" href="' . htmlspecialchars($answer["url"]) . '" rel="noreferrer nofollow">';
  215. }
  216. $right["answer"] .= '<h1>' . htmlspecialchars($answer["title"]) . '</h1>';
  217. if(!empty($answer["url"])){
  218. $right["answer"] .= '</a>';
  219. }
  220. $right["answer"] .= '</div>';
  221. }
  222. if(!empty($answer["url"])){
  223. $right["answer"] .=
  224. $frontend->drawlink($answer["url"]);
  225. }
  226. $right["answer"] .= '<div class="description">';
  227. if(!empty($answer["thumb"])){
  228. $right["answer"] .=
  229. '<a href="' . htmlspecialchars($answer["thumb"]) . '" rel="noreferrer nofollow" class="photo">' .
  230. '<img src="' . $frontend->htmlimage($answer["thumb"], "cover") . '" alt="thumb" class="openimg">' .
  231. '</a>';
  232. }
  233. foreach($answer["description"] as $description){
  234. switch($description["type"]){
  235. case "text":
  236. $right["answer"] .= $frontend->highlighttext($get["s"], $description["value"]);
  237. break;
  238. case "title":
  239. $right["answer"] .=
  240. '<h2>' .
  241. htmlspecialchars($description["value"]) .
  242. '</h2>';
  243. break;
  244. case "italic":
  245. $right["answer"] .=
  246. '<i>' .
  247. $frontend->highlighttext($get["s"], $description["value"]) .
  248. '</i>';
  249. break;
  250. case "quote":
  251. $right["answer"] .=
  252. '<div class="quote">' .
  253. $frontend->highlighttext($get["s"], $description["value"]) .
  254. '</div>';
  255. break;
  256. case "code":
  257. $right["answer"] .=
  258. '<div class="code" tabindex="-1">' .
  259. $frontend->highlightcode($description["value"], true) .
  260. '</div>';
  261. break;
  262. case "inline_code":
  263. $right["answer"] .=
  264. '<div class="code-inline">' .
  265. htmlspecialchars($description["value"]) .
  266. '</div>';
  267. break;
  268. case "link":
  269. $right["answer"] .=
  270. '<a href="' . htmlspecialchars($description["url"]) . '" rel="noreferrer nofollow" class="underline" tabindex="-1">' . htmlspecialchars($description["value"]) . '</a>';
  271. break;
  272. case "image":
  273. $right["answer"] .=
  274. '<a href="' . htmlspecialchars($description["url"]) . '" rel="noreferrer nofollow" tabindex="-1"><img src="' . $frontend->htmlimage($description["url"], "thumb") . '" alt="image" class="fullimg openimg"></a>';
  275. break;
  276. case "audio":
  277. $right["answer"] .=
  278. '<audio src="/audio/linear?s=' . urlencode($description["url"]) . '" controls><a href="/audio/linear?s=' . urlencode($description["url"]) . '">Listen to the pronunciation audio</a></audio>';
  279. break;
  280. }
  281. }
  282. $right["answer"] .= '</div>';
  283. if(count($answer["table"]) !== 0){
  284. $right["answer"] .= '<table>';
  285. foreach($answer["table"] as $info => $value){
  286. $right["answer"] .=
  287. '<tr>' .
  288. '<td>' . $info . '</td>' .
  289. '<td>' . $value . '</td>' .
  290. '</tr>';
  291. }
  292. $right["answer"] .= '</table>';
  293. }
  294. if(count($answer["sublink"]) !== 0){
  295. $right["answer"] .= '<div class="socials">';
  296. $icons = glob("static/icon/*");
  297. foreach($answer["sublink"] as $website => $url){
  298. $flag = false;
  299. $icon = str_replace(" ", "", strtolower($website));
  300. foreach($icons as $path){
  301. if(pathinfo($path, PATHINFO_FILENAME) == $icon){
  302. $flag = true;
  303. break;
  304. }
  305. }
  306. if($flag === false){
  307. $icon = "website";
  308. }
  309. $right["answer"] .=
  310. '<a href="' . htmlspecialchars($url) . '" rel="noreferrer nofollow" tabindex="-1">' .
  311. '<div class="center">' .
  312. '<img src="/static/icon/' . $icon . '.png" alt="icon">' .
  313. '<div class="title">' . $website . '</div>' .
  314. '</div>' .
  315. '</a>';
  316. }
  317. $right["answer"] .= '</div>';
  318. }
  319. $right["answer"] .=
  320. '</div></div>' .
  321. '<label class="spoiler-button" for="answer' . $answerlen . '"></label></div>';
  322. }
  323. }
  324. /*
  325. Add right containers
  326. */
  327. if(isset($right["answer"])){
  328. if(count($right) >= 2){
  329. $payload["right-right"] = $right["answer"];
  330. unset($right["answer"]);
  331. }
  332. }
  333. $c = 0;
  334. foreach($right as $snippet){
  335. if($c % 2 === 0){
  336. $payload["right-left"] .= $snippet;
  337. }else{
  338. $payload["right-right"] .= $snippet;
  339. }
  340. $c++;
  341. }
  342. if($c !== 0){
  343. $payload["class"] = " has-answer";
  344. }
  345. /*
  346. Generate related searches
  347. */
  348. $c = count($results["related"]);
  349. if($c !== 0){
  350. $payload["left"] .= '<h3>Related searches</h3><table class="related">';
  351. $opentr = false;
  352. for($i=0; $i<$c; $i++){
  353. if(($i % 2) === 0){
  354. $opentr = true;
  355. $payload["left"] .= '<tr>';
  356. }else{
  357. $opentr = false;
  358. }
  359. $payload["left"] .=
  360. '<td>' .
  361. '<a href="/web?s=' .
  362. urlencode($results["related"][$i]) . "&" .
  363. $frontend->buildquery($get, true) .
  364. '">' .
  365. htmlspecialchars($results["related"][$i]) .
  366. '</a>';
  367. $payload["left"] .= '</td>';
  368. if($opentr === false){
  369. $payload["left"] .= '</tr>';
  370. }
  371. }
  372. if($opentr === true){
  373. $payload["left"] .= '<td></td></tr>';
  374. }
  375. $payload["left"] .= '</table>';
  376. }
  377. /*
  378. Load next page
  379. */
  380. if($results["npt"] !== null){
  381. $payload["left"] .=
  382. '<a href="' . $frontend->htmlnextpage($get, $results["npt"], "web") . '" class="nextpage">Next page &gt;</a>';
  383. }
  384. echo $frontend->load("search.html", $payload);