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 "
"; echo ""; echo "$base_url"; echo "

$title

"; echo "
"; + echo "$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(); - ?> \ No newline at end of file diff --git a/session_destroy.php b/session_destroy.php deleted file mode 100644 index 355c409..0000000 --- a/session_destroy.php +++ /dev/null @@ -1,13 +0,0 @@ - \ No newline at end of file diff --git a/static/styles.css b/static/styles.css index 0378651..8224a17 100644 --- a/static/styles.css +++ b/static/styles.css @@ -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%; } diff --git a/tools.php b/tools.php new file mode 100644 index 0000000..8f91832 --- /dev/null +++ b/tools.php @@ -0,0 +1,42 @@ + \ No newline at end of file