search.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  3. <head>
  4. <title> <?php echo $_REQUEST["q"]; ?> - LibreX</title>
  5. <meta http-equiv="Content-type" content="application/xhtml+xml;charset=utf-8"/>
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  7. <meta name="description" content="A privacy respecting meta search engine."/>
  8. <meta name="referrer" content="no-referrer"/>
  9. <link rel="stylesheet" type="text/css" href="static/styles.css"/>
  10. <link title="LibreX search" type="application/opensearchdescription+xml" href="/opensearch.xml?method=POST" rel="search"/>
  11. <link rel="shortcut icon" href="static/librex.png" />
  12. </head>
  13. <body>
  14. <form class="small-search-container" method="post" enctype="multipart/form-data" autocomplete="off">
  15. <a href="/"><img id="logo" src="static/librex.png" alt="librex"></a>
  16. <input type="hidden" name="p" value="0">
  17. <input type="text" name="q"
  18. <?php
  19. $query = trim($_REQUEST["q"]);
  20. if (1 > strlen($query) || strlen($query) > 256)
  21. {
  22. header("Location: /");
  23. die();
  24. }
  25. echo "value=\"$query\"";
  26. ?>
  27. >
  28. <br>
  29. <?php
  30. $type = $_REQUEST["type"];
  31. echo "<input type=\"hidden\" name=\"type\" value=\"$type\"/>";
  32. ?>
  33. <button type="submit" style="display:none;"></button>
  34. <div class="result-change">
  35. <button name="type" value="0"><img src="static/text_result.png" id="change-image" style="width:20px;">Text</button>
  36. <button name="type" value="1"><img src="static/image_result.png" id="change-image">Images</button>
  37. <button name="type" value="2"><img src="static/video_result.png" id="change-image" style="width:40px;">Videos</button>
  38. </div>
  39. <hr>
  40. </form>
  41. <?php
  42. require_once "google.php";
  43. require_once "config.php";
  44. require_once "tools.php";
  45. function print_next_page_button($page, $button_val, $q, $type)
  46. {
  47. echo "<form id=\"page\" action=\"search.php\" target=\"_top\" method=\"post\" enctype=\"multipart/form-data\" autocomplete=\"off\">";
  48. echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
  49. echo "<input type=\"hidden\" name=\"q\" value=\"$q\" />";
  50. echo "<input type=\"hidden\" name=\"type\" value=\"$type\" />";
  51. echo "<button type=\"submit\">$button_val</button>";
  52. echo "</form>";
  53. }
  54. function print_text_results($results)
  55. {
  56. global $query;
  57. check_for_special_search($query);
  58. foreach($results as $result)
  59. {
  60. $title = $result["title"];
  61. $url = $result["url"];
  62. $base_url = $result["base_url"];
  63. $description = $result["description"];
  64. echo "<div class=\"result-container\">";
  65. echo "<a href=\"$url\">";
  66. echo "$base_url";
  67. echo "<h2>$title</h2>";
  68. echo "</a>";
  69. echo "<span>$description</span>";
  70. echo "</div>";
  71. }
  72. }
  73. function print_image_results($results)
  74. {
  75. echo "<div class=\"image-result-container\">";
  76. foreach($results as $result)
  77. {
  78. $src = $result["base64"];
  79. $alt = $result["alt"];
  80. echo "<a title=\"$alt\" href=\"data:image/jpeg;base64,$src\" target=\"_blank\">";
  81. echo "<img src=\"data:image/jpeg;base64,$src\" width=\"350\" height=\"200\">";
  82. echo "</a>";
  83. }
  84. echo "</div>";
  85. }
  86. function print_video_results($results)
  87. {
  88. foreach($results as $result)
  89. {
  90. $title = $result["title"];
  91. $url = $result["url"];
  92. $base_url = $result["base_url"];
  93. echo "<div class=\"result-container\">";
  94. echo "<a href=\"$url\">";
  95. echo "$base_url";
  96. echo "<h2>$title</h2>";
  97. echo "</a>";
  98. echo "</div>";
  99. }
  100. }
  101. $page = isset($_REQUEST["p"]) ? (int) $_REQUEST["p"] : 0;
  102. $type = isset($_REQUEST["type"]) ? (int) $_REQUEST["type"] : 0;
  103. $start_time = microtime(true);
  104. $results = get_google_results($query, $page, $type);
  105. $end_time = number_format(microtime(true) - $start_time, 2, '.', '');
  106. echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
  107. switch ($type)
  108. {
  109. case 0:
  110. print_text_results($results);
  111. break;
  112. case 1:
  113. print_image_results($results);
  114. break;
  115. case 2:
  116. print_video_results($results);
  117. break;
  118. default:
  119. print_text_results($results);
  120. break;
  121. }
  122. if ($type != 1 )
  123. {
  124. echo "<div class=\"page-container\">";
  125. if ($page != 0)
  126. {
  127. print_next_page_button(0, "&lt;&lt;", $query, $type);
  128. print_next_page_button($page - 10, "&lt;", $query, $type);
  129. }
  130. for ($i=$page / 10; $page / 10 + 10 > $i; $i++)
  131. {
  132. $page_input = $i * 10;
  133. $page_button = $i + 1;
  134. print_next_page_button($page_input, $page_button, $query, $type);
  135. }
  136. print_next_page_button($page + 10, "&gt;", $query, $type);
  137. echo "</div>";
  138. }
  139. ?>
  140. <div class="info-container">
  141. <a href="/">LibreX</a>
  142. <a href="https://github.com/hnhx/librex/" target="_blank">Source code &amp; Other instances</a>
  143. <a href="/donate.xhtml" id="right">Donate ❤️</a>
  144. </div>
  145. </body>
  146. </html>