From c6959a350720c33b112d6fabe2b89c4698fc7460 Mon Sep 17 00:00:00 2001
From: hnhx
Date: Sat, 19 Feb 2022 21:40:56 +0100
Subject: [PATCH] organized search types , rewrote how results are parsed ,
made the code cleaner
---
api.php | 6 +-
config.php | 39 ++++---
fetch.php | 228 -----------------------------------------
google.php | 37 +++++++
index.xhtml | 4 +-
results/currency.php | 40 ++++++++
results/definition.php | 29 ++++++
results/image.php | 43 ++++++++
results/text.php | 35 +++++++
search.php | 36 +++----
search_frame.php | 32 ++++--
session_destroy.php | 13 ---
static/styles.css | 16 +--
tools.php | 42 ++++++++
14 files changed, 301 insertions(+), 299 deletions(-)
delete mode 100644 fetch.php
create mode 100644 google.php
create mode 100644 results/currency.php
create mode 100644 results/definition.php
create mode 100644 results/image.php
create mode 100644 results/text.php
delete mode 100644 session_destroy.php
create mode 100644 tools.php
diff --git a/api.php b/api.php
index 8d8428f..66c4fe6 100644
--- a/api.php
+++ b/api.php
@@ -1,13 +1,13 @@
\ No newline at end of file
diff --git a/config.php b/config.php
index 0262221..19e84a0 100755
--- a/config.php
+++ b/config.php
@@ -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
+ );
?>
\ No newline at end of file
diff --git a/fetch.php b/fetch.php
deleted file mode 100644
index 39f42fa..0000000
--- a/fetch.php
+++ /dev/null
@@ -1,228 +0,0 @@
- 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 "";
- echo "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
- echo "
";
- }
- }
- }
- }
-
- 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 "";
- echo "$word_to_define meaning
";
- echo "
" . $definition . "
";
- echo "
";
- }
- }
- }
-
- 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;
- }
-?>
\ No newline at end of file
diff --git a/google.php b/google.php
new file mode 100644
index 0000000..0f8153f
--- /dev/null
+++ b/google.php
@@ -0,0 +1,37 @@
+";
+ echo "Google rejected the request! :C";
+ echo "
";
+ 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);
+ }
+ }
+?>
\ No newline at end of file
diff --git a/index.xhtml b/index.xhtml
index acd82c8..d7b147a 100644
--- a/index.xhtml
+++ b/index.xhtml
@@ -14,8 +14,8 @@
-
-
+
+
diff --git a/results/currency.php b/results/currency.php
new file mode 100644
index 0000000..ba31c10
--- /dev/null
+++ b/results/currency.php
@@ -0,0 +1,40 @@
+= 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 "
";
+ echo "$amount_to_convert $base_currency = $conversion_result $currency_to_convert";
+ echo "
";
+ }
+ }
+ }
+ }
+?>
\ No newline at end of file
diff --git a/results/definition.php b/results/definition.php
new file mode 100644
index 0000000..81d20ee
--- /dev/null
+++ b/results/definition.php
@@ -0,0 +1,29 @@
+= 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 "
";
+ echo "$word_to_define meaning
";
+ echo "
" . $definition . "
";
+ echo "
";
+ }
+ }
+ }
+?>
\ No newline at end of file
diff --git a/results/image.php b/results/image.php
new file mode 100644
index 0000000..d51df1c
--- /dev/null
+++ b/results/image.php
@@ -0,0 +1,43 @@
+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;
+ }
+?>
\ No newline at end of file
diff --git a/results/text.php b/results/text.php
new file mode 100644
index 0000000..afd1896
--- /dev/null
+++ b/results/text.php
@@ -0,0 +1,35 @@
+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;
+ }
+?>
\ No newline at end of file
diff --git a/search.php b/search.php
index 9efc743..f6f37dc 100644
--- a/search.php
+++ b/search.php
@@ -17,39 +17,31 @@
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"];
+
?>
>
";
+ $type = $_REQUEST["type"];
+ echo "
";
?>
-
-
+
+
diff --git a/search_frame.php b/search_frame.php
index e965939..df44f46 100644
--- a/search_frame.php
+++ b/search_frame.php
@@ -8,7 +8,8 @@
";
echo "
";
echo "
";
@@ -16,35 +17,42 @@
echo "";
}
+ 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 "
Fetched the results in $end_time seconds
";
- 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 "
";
}
@@ -56,7 +64,7 @@
print_next_pages($page - 10, "<", $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 "
";
}
- else
+ else if ($type == 1) // image search
{
foreach($results as $result)
{
$src = $result["base64"];
$alt = $result["alt"];
- echo "
";
+
+ echo "";
+ echo "
";
+ echo "";
}
}
- require "session_destroy.php";
+ better_session_destroy();
-
?>