organized search types , rewrote how results are parsed , made the code cleaner

This commit is contained in:
hnhx 2022-02-19 21:40:56 +01:00
parent f78379b2e4
commit c6959a3507
14 changed files with 301 additions and 299 deletions

View File

@ -1,13 +1,13 @@
<?php
header('Content-Type: application/json');
require "fetch.php";
require "google.php";
$query = $_REQUEST["q"];
$page = (int) $_REQUEST["p"] * 10;
$type = $_REQUEST["img_search"] == "true" ? true : false;
$type = (int) $_REQUEST["type"];
$results = fetch_results($query, $page, $type);
$results = get_google_results($query, $page, $type);
echo json_encode($results, true);
?>

View File

@ -9,20 +9,31 @@
$config_google_language = "en";
/*
Format: "ip:port"
For a TOR connection you would use these settings:
$config_proxy = "127.0.0.1:9050";
$config_proxy_type = 5;
*/
$config_proxy = null;
To send requests trough a proxy uncomment CURLOPT_PROXY and CURLOPT_PROXYTYPE:
/*
1 -> HTTP
2 -> SOCKS4
3 -> SOCKS4a (resolves URL hostname)
4 -> SOCKS5
5 -> SOCKS5 (resolves URL hostname)
CURLOPT_PROXYTYPE options:
CURLPROXY_HTTP
CURLPROXY_SOCKS4
CURLPROXY_SOCKS4A
CURLPROXY_SOCKS5
CURLPROXY_SOCKS5_HOSTNAME
As an example, for a TOR connection you would use these settings:
CURLOPT_PROXY => "127.0.0.1:9050",
CURLOPT_PROXYTYPE => CURLPROXY_SOCKS5,
!!! ONLY CHANGE THE OTHER OPTIONS IF YOU KNOW WHAT YOU ARE DOING !!!
*/
$config_proxy_type = 1;
$config_curl_settings = array(
// CURLOPT_PROXY => "ip:port",
// CURLOPT_PROXYTYPE => CURLPROXY_HTTP,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => $config_user_agent,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_VERBOSE => 1
);
?>

228
fetch.php
View File

@ -1,228 +0,0 @@
<?php
require("config.php");
$proxy_type = null;
if ($config_proxy != null)
{
switch($config_proxy_type)
{
case 1:
$proxy_type = CURLPROXY_HTTP;
break;
case 2:
$proxy_type = CURLPROXY_SOCKS4;
break;
case 3:
$proxy_type = CURLPROXY_SOCKS4A;
break;
case 4:
$proxy_type = CURLPROXY_SOCKS5;
break;
case 5:
$proxy_type = CURLPROXY_SOCKS5_HOSTNAME;
break;
default:
$proxy_type = CURLPROXY_HTTP;
break;
}
}
$curl_settings = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => $config_user_agent,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_VERBOSE => 1
);
function get_base_url($url)
{
$split_url = explode("/", $url);
$base_url = $split_url[0] . "//" . $split_url[2] . "/";
return $base_url;
}
function special_search($query)
{
$query_lower = strtolower($query);
// Check for currency convesion
if (strpos($query_lower, "to"))
convert_currency($query);
// Check for definition
else if (strpos($query_lower, "mean"))
define_word($query);
}
function convert_currency($query)
{
global $curl_settings , $proxy_type , $config_proxy;
$split_query = explode(" ", $query);
if (count($split_query) >= 4)
{
$amount_to_convert = floatval($split_query[0]);
if ($amount_to_convert != 0)
{
$base_currency = strtoupper($split_query[1]);
$currency_to_convert = strtoupper($split_query[3]);
$ch = curl_init("https://cdn.moneyconvert.net/api/latest.json");
if ($config_proxy != null)
{
curl_setopt($ch, CURLOPT_PROXY, $config_proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, $proxy_type);
}
curl_setopt_array($ch, $curl_settings);
$response = curl_exec($ch);
$json_response = json_decode($response, true);
$rates = $json_response["rates"];
if (array_key_exists($base_currency, $rates) && array_key_exists($currency_to_convert, $rates))
{
$base_currency_response = $rates[$base_currency];
$currency_to_convert_response = $rates[$currency_to_convert];
$conversion_result = ($currency_to_convert_response / $base_currency_response) * $amount_to_convert;
echo "<p id=\"special-result\">";
echo "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
echo "</p>";
}
}
}
}
function define_word($query)
{
global $curl_settings , $proxy_type , $config_proxy;
$split_query = explode(" ", $query);
if (count($split_query) >= 2)
{
$reversed_split_q = array_reverse($split_query);
$word_to_define = $reversed_split_q[1];
$ch = curl_init("https://api.dictionaryapi.dev/api/v2/entries/en/$word_to_define");
if ($config_proxy != null)
{
curl_setopt($ch, CURLOPT_PROXY, $config_proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, $proxy_type);
}
curl_setopt_array($ch, $curl_settings);
$response = curl_exec($ch);
$json_response = json_decode($response, true);
if (!array_key_exists("title", $json_response))
{
$definition = $json_response[0]["meanings"][0]["definitions"][0]["definition"];
echo "<p id=\"special-result\">";
echo "$word_to_define meaning<br/>";
echo "<br/>" . $definition . "<br/>";
echo "</p>";
}
}
}
function fetch_results($query, $page, $get_images=false)
{
global $curl_settings , $proxy_type , $config_proxy;
require "config.php";
$query_encoded = urlencode($query);
$google = "https://www.google.$config_google_domain/search?&q=$query_encoded&start=$page&hl=$config_google_language";
if ($get_images)
$google .= "&tbm=isch";
$ch = curl_init($google);
if ($config_proxy != null)
{
curl_setopt($ch, CURLOPT_PROXY, $config_proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, $proxy_type);
}
curl_setopt_array($ch, $curl_settings);
$response = curl_exec($ch);
$htmlDom = new DOMDocument;
@$htmlDom->loadHTML($response);
$xpath = new DOMXPath($htmlDom);
$results = array();
if ($get_images)
{
$mh = curl_multi_init();
$chs = array();
foreach($xpath->query(".//following::img") as $image)
{
$alt = $image->getAttribute("alt");
$src = $image->getAttribute("data-src");
if (!empty($src) && !empty($alt))
{
$ch = curl_init($src);
if ($config_proxy != null)
{
curl_setopt($ch, CURLOPT_PROXY, $config_proxy);
curl_setopt($ch, CURLOPT_PROXYTYPE, $proxy_type);
}
curl_setopt_array($ch, $curl_settings);
array_push($chs, $ch);
curl_multi_add_handle($mh, $ch);
}
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
foreach($chs as $ch)
{
$img_base64 = base64_encode(curl_multi_getcontent($ch));
array_push($results,
array (
"base64" => $img_base64,
"alt" => $alt
)
);
}
}
else
{
foreach($xpath->query("//div[contains(@class, 'yuRUbf')]") as $div)
{
$title = $div->getElementsByTagName("h3")[0]->textContent;
$url = $div->getElementsByTagName("a")[0]->getAttribute("href");
$base_url = get_base_url($url);
array_push($results,
array (
"title" => $title,
"url" => $url,
"base_url" => $base_url
)
);
}
}
return $results;
}
?>

37
google.php Normal file
View File

@ -0,0 +1,37 @@
<?php
function get_google_results($query, $page, $type=0)
{
require_once "config.php";
require_once "results/image.php";
require_once "results/text.php";
$query_encoded = urlencode($query);
$google = "https://www.google.$config_google_domain/search?&q=$query_encoded&start=$page&hl=$config_google_language";
if ($type == 1)
$google .= "&tbm=isch";
$ch = curl_init($google);
curl_setopt_array($ch, $config_curl_settings);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
{
echo "<p id=\"special-result\">";
echo "Google rejected the request! :C";
echo "</p>";
die();
}
$htmlDom = new DOMDocument;
@$htmlDom->loadHTML($response);
$xpath = new DOMXPath($htmlDom);
switch ($type)
{
case 0: return text_results($xpath);
case 1: return image_results($xpath);
default: return text_results($xpath);
}
}
?>

View File

@ -14,8 +14,8 @@
<input type="text" name="q"/>
<input type="hidden" name="p" value="0"/>
<input type="submit" style="display:none"/> <br/>
<button name="type" value="text" type="submit">Search with LibreX</button>
<button name="type" value="img" type="submit">Search images with LibreX</button>
<button name="type" value="0" type="submit">Search with LibreX</button>
<button name="type" value="1" type="submit">Search images with LibreX</button>
</form>
<div class="info-container">

40
results/currency.php Normal file
View File

@ -0,0 +1,40 @@
<?php
function currency_results($query)
{
require "config.php";
$split_query = explode(" ", $query);
if (count($split_query) >= 4)
{
$amount_to_convert = floatval($split_query[0]);
if ($amount_to_convert != 0)
{
$base_currency = strtoupper($split_query[1]);
$currency_to_convert = strtoupper($split_query[3]);
$ch = curl_init("https://cdn.moneyconvert.net/api/latest.json");
curl_setopt_array($ch, $config_curl_settings);
$response = curl_exec($ch);
$json_response = json_decode($response, true);
$rates = $json_response["rates"];
if (array_key_exists($base_currency, $rates) && array_key_exists($currency_to_convert, $rates))
{
$base_currency_response = $rates[$base_currency];
$currency_to_convert_response = $rates[$currency_to_convert];
$conversion_result = ($currency_to_convert_response / $base_currency_response) * $amount_to_convert;
echo "<p id=\"special-result\">";
echo "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
echo "</p>";
}
}
}
}
?>

29
results/definition.php Normal file
View File

@ -0,0 +1,29 @@
<?php
function definition_results($query)
{
require "config.php";
$split_query = explode(" ", $query);
if (count($split_query) >= 2)
{
$reversed_split_q = array_reverse($split_query);
$word_to_define = $reversed_split_q[1];
$ch = curl_init("https://api.dictionaryapi.dev/api/v2/entries/en/$word_to_define");
curl_setopt_array($ch, $config_curl_settings);
$response = curl_exec($ch);
$json_response = json_decode($response, true);
if (!array_key_exists("title", $json_response))
{
$definition = $json_response[0]["meanings"][0]["definitions"][0]["definition"];
echo "<p id=\"special-result\">";
echo "$word_to_define meaning<br/>";
echo "<br/>" . $definition . "<br/>";
echo "</p>";
}
}
}
?>

43
results/image.php Normal file
View File

@ -0,0 +1,43 @@
<?php
function image_results($xpath)
{
require "config.php";
$mh = curl_multi_init();
$chs = $alts = $results = array();
foreach($xpath->query("//img[@data-src]") as $image)
{
$alt = $image->getAttribute("alt");
$src = $image->getAttribute("data-src");
if (!empty($alt))
{
$ch = curl_init($src);
curl_setopt_array($ch, $config_curl_settings);
array_push($chs, $ch);
curl_multi_add_handle($mh, $ch);
array_push($alts, $alt);
}
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
for ($i=0; count($chs)>$i; $i++)
{
$img_base64 = base64_encode(curl_multi_getcontent($chs[$i]));
array_push($results,
array (
"base64" => $img_base64,
"alt" => $alts[$i]
)
);
}
return $results;
}
?>

35
results/text.php Normal file
View File

@ -0,0 +1,35 @@
<?php
function text_results($xpath)
{
require_once "tools.php";
$results = array();
foreach($xpath->query("//div[@id='search']//div[contains(@class, 'g')]") as $result)
{
$url = $xpath->evaluate(".//div[@class='yuRUbf']//a/@href", $result)[0];
if ($url == null)
continue;
if (!empty($results)) // filter duplicate results
if (end($results)["url"] == $url->textContent)
continue;
$title = $xpath->evaluate(".//h3", $result)[0];
$description = $xpath->evaluate(".//div[contains(@class, 'VwiC3b')]", $result)[0];
array_push($results,
array (
"title" => $title->textContent,
"url" => $url->textContent,
"base_url" => get_base_url($url->textContent),
"description" => $description == null ? "No description was provided for this site." : $description->textContent
)
);
}
return $results;
}
?>

View File

@ -17,39 +17,31 @@
<?php
session_start();
$valid_query = true;
$query = trim($_REQUEST["q"]);
if (!isset($_REQUEST["q"]))
$valid_query = false;
else if (1 > strlen($_REQUEST["q"]) || strlen($_REQUEST["q"]) > 256)
$valid_query = false;
if ($valid_query)
{
$query = $_REQUEST["q"];
echo "value=\"$query\"";
$_SESSION["q"] = trim($query);
$_SESSION["p"] = $_REQUEST["p"];
$_SESSION["type"] = $_REQUEST["type"];
}
else
if (1 > strlen($query) || strlen($query) > 256)
{
header("Location: /");
die();
}
}
echo "value=\"$query\"";
$_SESSION["q"] = $query;
$_SESSION["p"] = $_REQUEST["p"];
$_SESSION["type"] = $_REQUEST["type"];
?>
>
<br>
<?php
if (isset($_REQUEST["type"]))
if ($_REQUEST["type"] == "img")
echo "<input type=\"hidden\" name=\"type\" value=\"img\"/>";
$type = $_REQUEST["type"];
echo "<input type=\"hidden\" name=\"type\" value=\"$type\"/>";
?>
<button type="submit" style="display:none;"></button>
<div class="result_change">
<button name="type" value="text">Text results</button>
<button name="type" value="img">Image results</button>
<button name="type" value="0">Text results</button>
<button name="type" value="1">Image results</button>
</div>
<hr>

View File

@ -8,7 +8,8 @@
</head>
<body>
<?php
function print_next_pages($page, $button_val, $q) {
function print_next_pages($page, $button_val, $q)
{
echo "<form id=\"page\" action=\"search.php\" target=\"_top\" method=\"post\">";
echo "<input type=\"hidden\" name=\"p\" value=\"" . $page . "\" />";
echo "<input type=\"hidden\" name=\"q\" value=\"$q\" />";
@ -16,35 +17,42 @@
echo "</form>";
}
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
require("fetch.php");
require_once "google.php";
require_once "tools.php";
$query = $_SESSION["q"];
$page = (int) htmlspecialchars($_SESSION["p"]);
$search_type = $_SESSION["type"] == "img" ? true : false;
$type = (int) $_SESSION["type"];
$start_time = microtime(true);
$results = fetch_results($query, $page, $search_type);
$results = get_google_results($query, $page, $type);
$end_time = number_format(microtime(true) - $start_time, 2, '.', '');
echo "<p id=\"time\">Fetched the results in $end_time seconds</p>";
if ($_SESSION["type"] != "img")
if ($type == 0) // text search
{
special_search($query);
check_for_special_search($query);
foreach($results as $result)
{
$title = $result["title"];
$url = $result["url"];
$base_url = $result["base_url"];
$description = $result["description"];
echo "<div class=\"result-container\">";
echo "<a href=\"$url\" target=\"_blank\">";
echo "$base_url";
echo "<h2>$title</h2>";
echo "</a>";
echo "<span>$description</span>";
echo "</div>";
}
@ -56,7 +64,7 @@
print_next_pages($page - 10, "&lt;", $query);
}
for ($i=$page / 10; $page / 10 + 10>$i; $i++)
for ($i=$page / 10; $page / 10 + 10 > $i; $i++)
{
$page_input = $i * 10;
$page_button = $i + 1;
@ -68,19 +76,21 @@
echo "</div>";
}
else
else if ($type == 1) // image search
{
foreach($results as $result)
{
$src = $result["base64"];
$alt = $result["alt"];
echo "<a href=\"data:image/jpeg;base64,$src\" target=\"_blank\"><img src=\"data:image/jpeg;base64,$src\"></a>";
echo "<a title=\"$alt\" href=\"data:image/jpeg;base64,$src\" target=\"_blank\">";
echo "<img src=\"data:image/jpeg;base64,$src\">";
echo "</a>";
}
}
require "session_destroy.php";
better_session_destroy();
?>
</body>
</html>

View File

@ -1,13 +0,0 @@
<?php
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
?>

View File

@ -10,7 +10,7 @@ body {
padding: 0;
}
hr { margin:30px 0px 30px 0px; }
hr { margin: 30px 0px 30px 0px; }
iframe {
width: 100%;
@ -103,6 +103,13 @@ img { padding:15px; }
margin-left:10%;
}
.result-container {
max-width: 550px;
margin-top: 30px;
}
.result-container span { font-size: 15px; }
.result-container a {
font-size: 14px;
color:#bdc1c6;
@ -118,10 +125,6 @@ img { padding:15px; }
.result-container h2:hover { text-decoration: underline; }
.result-container {
margin-top:6px;
}
#special-result {
padding:10px;
border: 1px solid #bdc1c6;
@ -129,7 +132,8 @@ img { padding:15px; }
}
.page-container {
margin-bottom:70px;
margin-top:50px;
margin-bottom:50px;
margin-left:15%;
}

42
tools.php Normal file
View File

@ -0,0 +1,42 @@
<?php
function get_base_url($url)
{
$split_url = explode("/", $url);
$base_url = $split_url[0] . "//" . $split_url[2] . "/";
return $base_url;
}
function better_session_destroy()
{
$_SESSION = array();
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
}
function check_for_special_search($query)
{
$query_lower = strtolower($query);
// Check for currency convesion
if (strpos($query_lower, "to"))
{
require_once "results/currency.php";
currency_results($query);
}
// Check for definition
else if (strpos($query_lower, "mean"))
{
require_once "results/definition.php";
definition_results($query);
}
}
?>