From a6c60fcf6f83de63b36744af7ea8bb3bc751372a Mon Sep 17 00:00:00 2001
From: Dominik Pfennig <dominik@suma-ev.de>
Date: Wed, 11 May 2016 11:30:36 +0200
Subject: [PATCH] =?UTF-8?q?Einen=20Teil=20der=20Logik=20von=20MetaGer=20ha?=
 =?UTF-8?q?be=20ich=20nun=20nach=20PHP=20=C3=BCberf=C3=BChrt?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/Http/Controllers/MetaGerSearch.php |  50 +++++++++++-
 app/MetaGer/Results.php                |  33 ++++----
 app/MetaGer/Search.php                 |  65 ++++++++++++++++
 app/MetaGer/Searchengine.php           | 102 +++++++++++++++++++++++++
 config/.gitignore                      |   2 +-
 config/app.php                         |   3 +-
 resources/lang/en/fokiNames.php        |  12 +--
 7 files changed, 240 insertions(+), 27 deletions(-)
 create mode 100644 app/MetaGer/Search.php
 create mode 100644 app/MetaGer/Searchengine.php

diff --git a/app/Http/Controllers/MetaGerSearch.php b/app/Http/Controllers/MetaGerSearch.php
index c7a736f6b..995447fcc 100644
--- a/app/Http/Controllers/MetaGerSearch.php
+++ b/app/Http/Controllers/MetaGerSearch.php
@@ -6,6 +6,8 @@ use App\Http\Controllers\Controller;
 use Illuminate\Http\Request;
 use App\MetaGer\Forwarder;
 use App\MetaGer\Results;
+use App;
+use App\MetaGer\Search;
 
 class MetaGerSearch extends Controller
 {
@@ -28,8 +30,52 @@ class MetaGerSearch extends Controller
 
     public function search(Request $request)
     {
-        $results = new Results($request);
-        return $results->loadSearchEngines();
+        # Zunächst überprüfen wir die eingegebenen Einstellungen:
+        # FOKUS
+        $fokus = $request->input('focus', 'web');
+        $fokus = trans('fokiNames.'.$fokus);
+        if(strpos($fokus,".")){
+            $fokus = trans('fokiNames.web');
+        }
+        define("FOKUS", $fokus);
+        # SUMA-FILE
+        if(App::isLocale("en")){
+            define("SUMA_FILE", config_path() . "/sumasEn.xml");
+        }else{
+            define("SUMA_FILE", config_path() . "/sumas.xml");
+        }
+        if(!file_exists(SUMA_FILE)){
+            die("Suma-File konnte nicht gefunden werden");
+        }
+        # Sucheingabe:
+        $eingabe = trim($request->input('eingabe', ''));
+        if(strlen($eingabe) === 0){
+            return 'Achtung: Sie haben keinen Suchbegriff eingegeben. Sie können ihre Suchbegriffe oben eingeben und es erneut versuchen.';
+        }else{
+            define("Q", $eingabe);
+        }
+        # IP:
+        if( isset($_SERVER['HTTP_FROM']) )
+        {
+            define("IP", $_SERVER['HTTP_FROM']);
+        }else
+        {
+            define("IP", "127.0.0.1");
+        }
+        # Language:
+        if( isset($_SERVER['HTTP_LANGUAGE']) )
+        {
+            define("LANGUAGE", $_SERVER['HTTP_LANGUAGE']);
+        }else
+        {
+            define("LANGUAGE", "");
+        }
+        # Category
+        define("CATEGORY", $request->input('category', ''));
+ 
+        $searchengines = Search::loadSearchEngines($request);
+        $results = new Results($searchengines);
+        return $results->results;
     }
 
 }
\ No newline at end of file
diff --git a/app/MetaGer/Results.php b/app/MetaGer/Results.php
index e23527f09..25f9177f1 100644
--- a/app/MetaGer/Results.php
+++ b/app/MetaGer/Results.php
@@ -2,30 +2,31 @@
 
 namespace App\MetaGer;
 use Illuminate\Http\Request;
-
+use File;
 class Results
 {
-	private $request;
 	private $fokiNames = [];
 	private $fokus;
-	function __construct (Request $request)
+	function __construct ($engines)
 	{
-		$this->request = $request;
-
-		$this->fokiNames[] = trans('fokiNames.web');
-		$this->fokiNames[] = trans('fokiNames.nachrichten');
-		$this->fokiNames[] = trans('fokiNames.wissenschaft');
-		$this->fokiNames[] = trans('fokiNames.produktsuche');
-		$this->fokiNames[] = trans('fokiNames.bilder');
-		$this->fokiNames[] = trans('fokiNames.angepasst');
+		$this->results = $this->loadResults($engines);
 	}
 
-	public function loadSearchEngines ()
+	private function loadResults($engines)
 	{
-		# Wenn keine persönlichen Einstellungen gesetzt sind, dafür aber ein existierender Fokus von uns,
-		# werden die zu dem Fokus gehörenden Suchmaschinen angeschaltet.
-		return $this->request->input('focus', 'test');
-		return var_dump($this->fokiNames);
+		# Das Laden der Ergebnisse besteht aus 2 Elementaren Schritten:
+		# 1. GET-Requests abarbeiten
+		# 2. Ergebnisse parsen
+
+		# 1. GET-Requests abarbeiten
+		# Wir generiern zunächst alle GET-Strings:
+		$getStrings = [];
+		foreach($engines as $engine)
+		{
+			$getStrings[] = $engine->generateGetString();
+		}
+
+		return print_r($getStrings, TRUE);
 	}
 
 }
\ No newline at end of file
diff --git a/app/MetaGer/Search.php b/app/MetaGer/Search.php
new file mode 100644
index 000000000..2f5eb50c3
--- /dev/null
+++ b/app/MetaGer/Search.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace App\MetaGer;
+use Illuminate\Http\Request;
+use App\MetaGer\Searchengine;
+
+class Search
+{
+	public static function loadSearchEngines(Request $request)
+	{
+
+        # Überprüfe, welche Sumas eingeschaltet sind
+        $xml = simplexml_load_file(SUMA_FILE);
+        $enabledSearchengines = [];
+        $overtureEnabled = FALSE;
+        
+        if(FOKUS === "angepasst")
+        {
+            $sumas = $xml->xpath("suma");
+            foreach($sumas as $suma)
+            {
+                if($request->has($suma["service"]) 
+                	|| ( FOKUS !== "bilder" 
+                		&& ($suma["name"]->__toString() === "qualigo" 
+                			|| $suma["name"]->__toString() === "similar_product_ads" 
+                			|| ( !$overtureEnabled && $suma["name"]->__toString() === "overtureAds" )
+                			)
+                		)
+                	){
+                	if($suma["name"]->__toString() === "overture")
+                	{
+                		$overtureEnabled = TRUE;
+                	}
+                    $enabledSearchengines[] = $suma;
+                }
+            }
+        }else{
+            $sumas = $xml->xpath("suma");
+            foreach($sumas as $suma){
+                $types = explode(",",$suma["type"]);
+                if(in_array(FOKUS, $types) 
+                	|| ( FOKUS !== "bilder" 
+                		&& ($suma["name"]->__toString() === "qualigo" 
+                			|| $suma["name"]->__toString() === "similar_product_ads" 
+                			|| ( !$overtureEnabled && $suma["name"]->__toString() === "overtureAds" )
+                			)
+                		)
+                	){
+                	if($suma["name"]->__toString() === "overture")
+                	{
+                		$overtureEnabled = TRUE;
+                	}
+                    $enabledSearchengines[] = $suma;
+                }
+            }
+        }
+        
+		$engines = [];
+		foreach($enabledSearchengines as $engine){
+			$engines[] = new Searchengine($engine);
+		}
+
+        return $engines;
+	}
+}
\ No newline at end of file
diff --git a/app/MetaGer/Searchengine.php b/app/MetaGer/Searchengine.php
new file mode 100644
index 000000000..339e80136
--- /dev/null
+++ b/app/MetaGer/Searchengine.php
@@ -0,0 +1,102 @@
+<?php
+
+namespace App\MetaGer;
+
+class Searchengine
+{
+
+
+	function __construct(\SimpleXMLElement $engine)
+	{
+		foreach($engine->attributes() as $key => $value){
+			$this->$key = $value->__toString();
+		}
+		# User-Agent definieren:
+		if( isset($_SERVER['HTTP_USER_AGENT']))
+		{
+			$this->useragent = $_SERVER['HTTP_USER_AGENT'];
+		}else
+		{
+			$this->useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
+		}
+
+	}
+
+	public function generateGetString()
+	{
+		$getString = "";
+		# Protokoll:
+		if($this->port === "443"){
+			$getString .= "https://";
+		}else{
+			$getString .= "http://";
+		}
+		# Host:
+		$getString .= $this->host;
+		# Port:
+		$getString .= ":" . $this->port;
+		# Skript:
+		$getString .= $this->skript;
+		# FormData:
+		$getString .= "?" . $this->formData;
+
+		# Wir müssen noch einige Platzhalter in dem GET-String ersetzen:
+		if( strpos($getString, "<<USERAGENT>>") ){
+			$getString = str_replace("<<USERAGENT>>", $this->urlEncode($this->useragent), $getString);
+		}
+
+		if( strpos($getString, "<<QUERY>>") )
+		{
+			$getString = str_replace("<<QUERY>>", $this->urlEncode(Q), $getString);
+		}
+
+		if( strpos($getString, "<<IP>>") )
+		{
+			$getString = str_replace("<<IP>>", $this->urlEncode(IP), $getString);
+		}
+
+		if( strpos($getString, "<<LANGUAGE>>") )
+		{
+			$getString = str_replace("<<LANGUAGE>>", $this->urlEncode(LANGUAGE), $getString);
+		}
+
+		if( strpos($getString, "<<CATEGORY>>") )
+		{
+			$getString = str_replace("<<CATEGORY>>", $this->urlEncode(CATEGORY), $getString);
+		}
+		return $getString;
+	}
+
+	private function urlEncode($string)
+	{
+		if(isset($this->inputEncoding))
+		{
+			return urlencode(mb_convert_encoding($string, $this->inputEncoding));
+		}else
+		{
+			return urlencode($string);
+		}
+	}
+
+	private function getOvertureAffilData()
+	{
+		$xfip="$_SERVER['HTTP_X_FORWARDED_FOR']";
+		$xfip=~ s/^(\d+\.\d+\.\d+)\.\d+/$1\.0/;
+	    my $overt_ip=$ENV{'HTTP_FROM'};
+	    my $overt_xfip=$xfip;
+	    # anonymisierte IPs an Overture:
+	    $overt_ip=~s#(\d+)\.(\d+)\.(\d+)\.\d+#$1\.$2\.$3\.0#;
+	    $overt_xfip=~s#(\d+)\.(\d+)\.(\d+)\.\d+#$1\.$2\.$3\.0#;
+	    $affil_data .= 'ip=' . $overt_ip;
+	    $affil_data .= '&ua=' . $self->{useragent};  
+	    if ($xfip) {
+	       $affil_data .= '&xfip=' . $overt_xfip;
+	    }
+	    $affil_data = URI::Escape::uri_escape_utf8($affil_data);
+	    $affilDataValue=$affil_data;
+		# Wir benötigen die ServeUrl:
+		$serveUrl = $cgi->self_url();#URI::Escape::uri_escape_utf8($cgi->self_url());
+		$serveUrl = URI::Escape::uri_escape_utf8($serveUrl);
+		return "&affilData=" . $affilDataValue . "&serveUrl=" . $serveUrl;
+	}
+}
\ No newline at end of file
diff --git a/config/.gitignore b/config/.gitignore
index 25e0c0488..b81c7954b 100644
--- a/config/.gitignore
+++ b/config/.gitignore
@@ -1 +1 @@
-metager.ini
\ No newline at end of file
+*.xml
\ No newline at end of file
diff --git a/config/app.php b/config/app.php
index 9627e582b..a3cbee8d2 100644
--- a/config/app.php
+++ b/config/app.php
@@ -202,8 +202,7 @@ return [
         'URL' => Illuminate\Support\Facades\URL::class,
         'Validator' => Illuminate\Support\Facades\Validator::class,
         'View' => Illuminate\Support\Facades\View::class,
-        'LaravelLocalization'   => Mcamara\LaravelLocalization\Facades\LaravelLocalization::class,
-
+        'LaravelLocalization'   => Mcamara\LaravelLocalization\Facades\LaravelLocalization::class,        
     ],
 
 ];
diff --git a/resources/lang/en/fokiNames.php b/resources/lang/en/fokiNames.php
index 0c737f771..a7e8fcc4f 100644
--- a/resources/lang/en/fokiNames.php
+++ b/resources/lang/en/fokiNames.php
@@ -1,10 +1,10 @@
 <?php
 
 return [
-	'web' 			=>	"web",
-	'nachrichten'	=>	"news",
-	'wissenschaft'	=>	'science',
-	'produktsuche'	=>	'shopping',
-	'bilder'		=>	'picture',
-	'angepasst'		=>	'custom'
+	'web' 		=>	"web",
+	'news'		=>	"nachrichten",
+	'science'	=>	'wissenschaft',
+	'shopping'	=>	'produktsuche',
+	'picture'	=>	'bilder',
+	'custom'	=>	'angepasst'
 ];
\ No newline at end of file
-- 
GitLab