MetaGerSearch.php 8.84 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1
2
3
4
<?php

namespace App\Http\Controllers;

5
use App;
6
use App\MetaGer;
7
use Cache;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Redis;
10
use LaravelLocalization;
11
use View;
Dominik Hebeler's avatar
Dominik Hebeler committed
12

13
const TIP_SERVER = 'http://metager3.de:63825/tips.xml';
14

Dominik Hebeler's avatar
Dominik Hebeler committed
15
16
class MetaGerSearch extends Controller
{
17
    public function search(Request $request, MetaGer $metager)
18
    {
19
20
21
22
23
        $spamEntries = [];
        if (file_exists(config_path('spam.txt'))) {
            $spamEntries = file(config_path('spam.txt'));
        }

24
        $focus = $request->input("focus", "web");
25

26
27
28
29
30
        if ($focus === "maps") {
            $searchinput = $request->input('eingabe', '');
            return redirect()->to('https://maps.metager.de/map/' . $searchinput . '/1240908.5493525574,6638783.2192695495,6');
        }

31
32
33
34
35
36
        # If there is no query parameter we redirect to the startpage
        $eingabe = $request->input('eingabe', '');
        if (empty(trim($eingabe))) {
            return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), '/'));
        }

37
38
        # Mit gelieferte Formulardaten parsen und abspeichern:
        $metager->parseFormData($request);
39

40
41
        # Nach Spezialsuchen überprüfen:
        $metager->checkSpecialSearches($request);
42

43
44
45
46
        if (Cache::has('spam.' . $metager->getFokus() . "." . md5($metager->getQ()))) {
            return response(Cache::get('spam.' . $metager->getFokus() . "." . md5($metager->getQ())));
        }

47
48
49
        # Die Quicktips als Job erstellen
        $quicktips = $metager->createQuicktips();

50
        # Suche für alle zu verwendenden Suchmaschinen als Job erstellen,
51
        # auf Ergebnisse warten und die Ergebnisse laden
52
        $metager->createSearchEngines($request);
53

54
55
56
57
58
59
        $metager->startSearch();

        $metager->waitForMainResults();

        $metager->retrieveResults();

60
61
        # Versuchen die Ergebnisse der Quicktips zu laden
        $quicktipResults = $quicktips->loadResults();
62
63
64
        # Alle Ergebnisse vor der Zusammenführung ranken:
        $metager->rankAll();

65
        # Ergebnisse der Suchmaschinen kombinieren:
Phil Höfer's avatar
Phil Höfer committed
66
        $metager->prepareResults();
67

68
69
70
71
72
73
        # Save the results in Redis
        $redis = Redis::connection(env('REDIS_RESULT_CONNECTION'));
        $pipeline = $redis->pipeline();
        foreach ($metager->getResults() as $result) {
            $pipeline->rpush($metager->getRedisCurrentResultList(), base64_encode(serialize($result)));
        }
74
        $pipeline->expire($metager->getRedisCurrentResultList(), env('REDIS_RESULT_CACHE_DURATION'));
75
76
        $pipeline->execute();

77
        # Die Ausgabe erstellen:
78
79
80
        $resultpage = $metager->createView($quicktipResults);
        foreach ($spamEntries as $index => $entry) {
            $entry = trim($entry);
Dominik Hebeler's avatar
Dominik Hebeler committed
81
82
83
            if (empty($entry)) {
                continue;
            }
84
85
86
87
88
            if (preg_match("/" . $entry . "/si", $metager->getEingabe())) {
                Cache::put('spam.' . $metager->getFokus() . "." . md5($metager->getQ()), $resultpage->render(), 1440);
            }
        }
        return $resultpage;
Dominik Hebeler's avatar
Dominik Hebeler committed
89
90
    }

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    public function loadMore(Request $request)
    {
        /**
         * There are three forms of requests to the resultpage
         * 1. Initial Request: Loads the fastest searchengines and sends them to the user
         * 2. Load more results (with JS): Loads new search engines that answered after the initial request was send
         * 3. Load more results (without JS): Loads new search engines that answered within 1s timeout
         */
        if ($request->filled('loadMore') && $request->filled('script') && $request->input('script') === "yes") {
            return $this->loadMoreJS($request);
        }

    }

    private function loadMoreJS(Request $request)
    {
        # Create a MetaGer Instance with the supplied hash
        $hash = $request->input('loadMore', '');

        $metager = new MetaGer($hash);
        $redis = Redis::connection(env('REDIS_RESULT_CONNECTION'));

        $result = [];
        # Check if there should be more results
        $stats = $redis->hgetall($metager->getRedisEngineResult() . "status");
        $stats["startTime"] = floatval($stats["startTime"]);
        $stats["engineCount"] = intval($stats["engineCount"]);
        $stats["engineAnswered"] = intval($stats["engineAnswered"]);
        $stats["engineDelivered"] = intval($stats["engineDelivered"]);

        $result["finished"] = true;
        $result["engineCount"] = $stats["engineCount"];
        $result["engineAnswered"] = $stats["engineAnswered"];
        $result["engineDelivered"] = $stats["engineDelivered"];
        $result["timeWaiting"] = microtime(true) - $stats["startTime"];

        # Check if we can abort
        if ($stats["engineAnswered"] > $stats["engineDelivered"]/*&& $result["timeWaiting"] <= 10 */) {
            $metager->parseFormData($request);
            # Nach Spezialsuchen überprüfen:
            $metager->checkSpecialSearches($request);

            # Read which search engines are new
            $newEngines = [];

136
            while (($engine = $redis->lpop($metager->getRedisResultWaitingKey())) != null) {
137
138
139
                $result["engineDelivered"]++;
                $newEngines[$engine] = $metager->getSumaFile()->sumas->{$engine};
            }
140
141
142
143
144
145
146
147
148
            $cache = Cache::get($hash);
            if ($cache != null) {
                $metager->setNext(unserialize($cache)["engines"]);
            }

            # Check if this request is not for page one
            $metager->setEngines($request, $newEngines);

            # Add the results already delivered to the user
149
150
151
152
153
154
155
156
157
            $results = $redis->lrange($metager->getRedisCurrentResultList(), 0, -1);
            foreach ($results as $index => $oldResult) {
                $results[$index] = unserialize(base64_decode($oldResult));
                $results[$index]->new = false;
            }
            $metager->setResults($results);
            $metager->retrieveResults();
            $metager->rankAll();
            $metager->prepareResults();
158
            $result["nextSearchLink"] = $metager->nextSearchLink();
159
160
161
            $results = $metager->getResults();
            foreach ($results as $index => $resultTmp) {
                if ($resultTmp->new) {
162
                    if ($metager->getFokus() !== "bilder") {
163
                        $view = View::make('layouts.result', ['index' => $index, 'result' => $resultTmp, 'metager' => $metager]);
164
165
166
167
                        $html = $view->render();
                        $result['newResults'][$index] = $html;
                        $result["imagesearch"] = false;
                    } else {
168
                        $view = View::make('layouts.image_result', ['index' => $index, 'result' => $resultTmp, 'metager' => $metager]);
169
170
171
172
                        $html = $view->render();
                        $result['newResults'][$index] = $html;
                        $result["imagesearch"] = true;
                    }
173
174
                }
            }
175
176
177
178
179
180
181
182
            # Save the results in Redis
            $pipeline = $redis->pipeline();
            $pipeline->hincrby($metager->getRedisEngineResult() . "status", "engineDelivered", sizeof($newEngines));
            $pipeline->hset($metager->getRedisEngineResult() . "status", "nextSearchLink", $result["nextSearchLink"]);
            foreach ($metager->getResults() as $resultTmp) {
                $resultTmp->new = false;
                $pipeline->rpush($metager->getRedisCurrentResultList(), base64_encode(serialize($resultTmp)));
            }
183
            $pipeline->expire($metager->getRedisCurrentResultList(), env('REDIS_RESULT_CACHE_DURATION'));
184
            $pipeline->execute();
185
186
187
188
189

        }
        return response()->json($result);
    }

190
191
192
193
194
195
196
197
    public function botProtection($redirect)
    {
        $hash = md5(date('YmdHi'));
        return view('botProtection')
            ->with('hash', $hash)
            ->with('r', $redirect);
    }

198
199
    public function get($url)
    {
200
        $ctx = stream_context_create(array('http' => array('timeout' => 2)));
201
        return file_get_contents($url, false, $ctx);
202
    }
203
204
205
206
207
208

    private function startsWith($haystack, $needle)
    {
        $length = strlen($needle);
        return (substr($haystack, 0, $length) === $needle);
    }
209
210
211
212

    public function tips(Request $request)
    {
        $tips_text = file_get_contents(TIP_SERVER);
213
        $tips = [];
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
        try {
            $tips_xml = simplexml_load_string($tips_text);

            $tips_xml->registerXPathNamespace('mg', 'http://metager.de/tips/');
            $tips_xml = $tips_xml->xpath('mg:tip');
            foreach ($tips_xml as $tip_xml) {
                $tips[] = $tip_xml->__toString();
            }
        } catch (\Exception $e) {
            Log::error("A problem occurred loading tips from the tip server.");
            Log::error($e->getMessage());
            abort(500);
        }
        return view('tips')
            ->with('title', trans('tips.title'))
            ->with('tips', $tips);
    }
231
}