From 478bbf4c5e7defc3097cd5a3abeef8003dec80c3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Phil=20H=C3=B6fer?= <phil@suma-ev.de>
Date: Tue, 13 Jun 2017 10:23:31 +0200
Subject: [PATCH] MetaGer-Key eingebaut

---
 app/MetaGer.php                               | 38 +++++++++++++++++++
 .../views/metager3resultsrss20.blade.php      |  2 +
 resources/views/metager3rich.blade.php        |  2 +-
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/app/MetaGer.php b/app/MetaGer.php
index afa81b3cf..9bf1e969b 100644
--- a/app/MetaGer.php
+++ b/app/MetaGer.php
@@ -40,6 +40,8 @@ class MetaGer
     protected $ip;
     protected $language;
     protected $agent;
+    protected $apiKey        = "";
+    protected $apiAuthorized = false;
     # Konfigurationseinstellungen:
     protected $sumaFile;
     protected $mobile;
@@ -148,6 +150,7 @@ class MetaGer
                         ->with('mobile', $this->mobile)
                         ->with('warnings', $this->warnings)
                         ->with('errors', $this->errors)
+                        ->with('apiAuthorized', $this->apiAuthorized)
                         ->with('metager', $this)
                         ->with('browser', (new Agent())->browser());
                     break;
@@ -155,6 +158,7 @@ class MetaGer
                     return view('metager3resultsrss20')
                         ->with('results', $viewResults)
                         ->with('eingabe', $this->eingabe)
+                        ->with('apiAuthorized', $this->apiAuthorized)
                         ->with('metager', $this)
                         ->with('resultcount', sizeof($viewResults));
                     break;
@@ -190,6 +194,9 @@ class MetaGer
         // filter
         // augment (boost&adgoal)
         // authorize
+        if ($this->apiKey) {
+            $this->apiAuthorized = $this->authorize($this->apiKey);
+        }
         // misc (WiP)
         if ($this->fokus == "nachrichten") {
             $this->results = array_filter($this->results, function ($v, $k) {
@@ -462,6 +469,34 @@ class MetaGer
         return $results;
     }
 
+    public function authorize($key)
+    {
+        $postdata = http_build_query(array(
+            'dummy' => rand(),
+        ));
+        $opts = array('http' => array(
+            'method'  => 'POST',
+            'header'  => 'Content-type: application/x-www-form-urlencoded',
+            'content' => $postdata,
+        ),
+        );
+
+        $context = stream_context_create($opts);
+
+        try {
+            $link   = "https://key.metager3.de/" . urlencode($key) . "/request-permission/api-access";
+            $result = json_decode(file_get_contents($link, false, $context));
+            if ($result->{'api-access'} == true) {
+                return true;
+            } else {
+                return false;
+            }
+
+        } catch (\ErrorException $e) {
+            return false;
+        }
+    }
+
     /*
      * Die Erstellung der Suchmaschinen bis die Ergebnisse da sind mit Unterfunktionen
      */
@@ -994,6 +1029,9 @@ class MetaGer
         } else {
             $this->quicktips = true;
         }
+
+        $this->apiKey = $request->input('key', '');
+
         $this->out = $request->input('out', "html");
         # Standard output format html
         if ($this->out !== "html" && $this->out !== "json" && $this->out !== "results" && $this->out !== "results-with-style" && $this->out !== "result-count" && $this->out !== "rss20" && $this->out !== "rich") {
diff --git a/resources/views/metager3resultsrss20.blade.php b/resources/views/metager3resultsrss20.blade.php
index f8fb9ac93..8bdf72132 100644
--- a/resources/views/metager3resultsrss20.blade.php
+++ b/resources/views/metager3resultsrss20.blade.php
@@ -8,6 +8,7 @@
      <opensearch:totalResults>{{ $resultcount }}</opensearch:totalResults>
      <opensearch:Query role="request" searchTerms="{{ htmlspecialchars($eingabe, ENT_QUOTES) }}"/>
 
+  @if($apiAuthorized)
   @foreach($metager->getResults() as $result)
      <item>
        <title>{!! htmlspecialchars($result->titel, ENT_XML1, 'UTF-8'); !!}</title>
@@ -18,5 +19,6 @@
        </description>
      </item>
   @endforeach
+  @endif
    </channel>
  </rss>
diff --git a/resources/views/metager3rich.blade.php b/resources/views/metager3rich.blade.php
index 9676713ab..b2008cb9d 100644
--- a/resources/views/metager3rich.blade.php
+++ b/resources/views/metager3rich.blade.php
@@ -33,7 +33,7 @@
 			</details>
 		<main class="results-container">
 		@foreach($metager->getResults() as $result)
-			@if($result->number % 7 === 0)
+			@if($result->number % 7 === 0 && !$apiAuthorized)
 				@include('layouts.rich.ad', ['result' => $metager->popAd()])
 			@endif
 				@include('layouts.rich.result', ['result' => $result])
-- 
GitLab