web.php 11 KB

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