1
0

web.php 11 KB

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