MetaGerSearch.php 9.05 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 LaravelLocalization;
10
use View;
Dominik Hebeler's avatar
Dominik Hebeler committed
11
12
13

class MetaGerSearch extends Controller
{
Dominik Hebeler's avatar
Dominik Hebeler committed
14
15

    public function search(Request $request, MetaGer $metager, $timing = false)
16
    {
Dominik Hebeler's avatar
Dominik Hebeler committed
17
18
19
20
        $timings = null;
        if ($timing) {
            $timings = ['starttime' => microtime(true)];
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
21
        $time = microtime(true);
22
        $spamEntries = [];
Dominik Hebeler's avatar
Dominik Hebeler committed
23
        $spamEntry = null;
24
25
26
27
        if (file_exists(config_path('spam.txt'))) {
            $spamEntries = file(config_path('spam.txt'));
        }

28
        $focus = $request->input("focus", "web");
29

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

35
36
37
38
39
40
        # 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(), '/'));
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
41
42
43
44
45
46
47
48
49
50
51
        foreach ($spamEntries as $index => $entry) {
            $entry = trim($entry);
            if (empty($entry)) {
                continue;
            }
            if (preg_match("/" . $entry . "/si", $eingabe)) {
                $spamEntry = $entry;
                break;
            }
        }

52
53
        # Mit gelieferte Formulardaten parsen und abspeichern:
        $metager->parseFormData($request);
Dominik Hebeler's avatar
Dominik Hebeler committed
54
55
56
        if (!empty($timings)) {
            $timings["parseFormData"] = microtime(true) - $time;
        }
57

58
59
        # Nach Spezialsuchen überprüfen:
        $metager->checkSpecialSearches($request);
Dominik Hebeler's avatar
Dominik Hebeler committed
60
61
62
        if (!empty($timings)) {
            $timings["checkSpecialSearches"] = microtime(true) - $time;
        }
63

Dominik Hebeler's avatar
Dominik Hebeler committed
64
65
66
67
        if ($spamEntry !== null && Cache::has('spam.' . $metager->getFokus() . "." . md5($spamEntry))) {
            $responseContent = Cache::get('spam.' . $metager->getFokus() . "." . md5($spamEntry));
            $responseContent = preg_replace('/(name="eingabe"\s+value=")[^"]+/', "$1$eingabe", $responseContent);
            return response($responseContent);
68
69
        }

70
        # Suche für alle zu verwendenden Suchmaschinen als Job erstellen,
71
        # auf Ergebnisse warten und die Ergebnisse laden
Dominik Hebeler's avatar
Dominik Hebeler committed
72
        $metager->createSearchEngines($request, $timings);
73

Dominik Hebeler's avatar
Dominik Hebeler committed
74
        $metager->startSearch($timings);
75
76

        $metager->waitForMainResults();
Dominik Hebeler's avatar
Dominik Hebeler committed
77
78
79
        if (!empty($timings)) {
            $timings["waitForMainResults"] = microtime(true) - $time;
        }
80
81

        $metager->retrieveResults();
Dominik Hebeler's avatar
Dominik Hebeler committed
82
83
84
        if (!empty($timings)) {
            $timings["retrieveResults"] = microtime(true) - $time;
        }
85

86
87
        # Alle Ergebnisse vor der Zusammenführung ranken:
        $metager->rankAll();
Dominik Hebeler's avatar
Dominik Hebeler committed
88
89
90
        if (!empty($timings)) {
            $timings["rankAll"] = microtime(true) - $time;
        }
91

92
        # Ergebnisse der Suchmaschinen kombinieren:
Phil Höfer's avatar
Phil Höfer committed
93
        $metager->prepareResults();
Dominik Hebeler's avatar
Dominik Hebeler committed
94
95
96
        if (!empty($timings)) {
            $timings["prepareResults"] = microtime(true) - $time;
        }
97

Dominik Hebeler's avatar
Dominik Hebeler committed
98
99
100
101
102
103
104
105
        $finished = true;
        foreach ($metager->getEngines() as $engine) {
            if ($engine->loaded) {
                $engine->setNew(false);
                $engine->markNew();
            }
        }

106
        Cache::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 60 * 60);
Dominik Hebeler's avatar
Dominik Hebeler committed
107
108
109
        if (!empty($timings)) {
            $timings["Filled resultloader Cache"] = microtime(true) - $time;
        }
110

111
        # Die Ausgabe erstellen:
112
        $resultpage = $metager->createView();
Dominik Hebeler's avatar
Dominik Hebeler committed
113
114
        if ($spamEntry !== null) {
            Cache::put('spam.' . $metager->getFokus() . "." . md5($spamEntry), $resultpage->render(), 604800);
115
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
116

Dominik Hebeler's avatar
Dominik Hebeler committed
117
118
119
120
121
122
123
124
        if (!empty($timings)) {
            $timings["createView"] = microtime(true) - $time;
        }

        if ($timings) {
            dd($timings);
        }

125
        return $resultpage;
Dominik Hebeler's avatar
Dominik Hebeler committed
126
127
    }

Dominik Hebeler's avatar
Dominik Hebeler committed
128
129
130
131
132
133
134
135
    public function searchTimings(Request $request, MetaGer $metager)
    {
        $request->merge([
            'eingabe' => "Hannover",
        ]);
        return $this->search($request, $metager, true);
    }

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    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', '');

155
156
157
158
159
160
        # Parser Skripte einhängen
        $dir = app_path() . "/Models/parserSkripte/";
        foreach (scandir($dir) as $filename) {
            $path = $dir . $filename;
            if (is_file($path)) {
                require_once $path;
161
            }
162
        }
163

164
165
166
167
        $engines = Cache::get($hash);
        if ($engines === null) {
            return response()->json(['finished' => true]);
        }
168

Dominik Hebeler's avatar
Dominik Hebeler committed
169
        $metager = new MetaGer(substr($hash, strpos($hash, "loader_") + 7));
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

        $metager->parseFormData($request);
        # Nach Spezialsuchen überprüfen:
        $metager->checkSpecialSearches($request);
        $metager->restoreEngines($engines);

        $metager->retrieveResults();
        $metager->rankAll();
        $metager->prepareResults();

        $result = [
            'finished' => true,
            'newResults' => [],
        ];
        $result["nextSearchLink"] = $metager->nextSearchLink();

        foreach ($metager->getResults() as $index => $resultTmp) {
            if ($resultTmp->new) {
                if ($metager->getFokus() !== "bilder") {
                    $view = View::make('layouts.result', ['index' => $index, 'result' => $resultTmp, 'metager' => $metager]);
                    $html = $view->render();
                    $result['newResults'][$index] = $html;
                    $result["imagesearch"] = false;
                } else {
                    $view = View::make('layouts.image_result', ['index' => $index, 'result' => $resultTmp, 'metager' => $metager]);
                    $html = $view->render();
                    $result['newResults'][$index] = $html;
                    $result["imagesearch"] = true;
198
199
200
                }
            }
        }
201

Dominik Hebeler's avatar
Dominik Hebeler committed
202
203
204
205
206
207
208
209
210
211
212
213
        $finished = true;
        foreach ($engines as $engine) {
            if (!$engine->loaded) {
                $finished = false;
            } else {
                $engine->setNew(false);
                $engine->markNew();
            }
        }

        $result["finished"] = $finished;

214
        // Update new Engines
215
        Cache::put("loader_" . $metager->getSearchUid(), $metager->getEngines(), 1 * 60);
216
217
218
        return response()->json($result);
    }

219
220
221
222
223
224
225
226
    public function botProtection($redirect)
    {
        $hash = md5(date('YmdHi'));
        return view('botProtection')
            ->with('hash', $hash)
            ->with('r', $redirect);
    }

227
228
    public function get($url)
    {
229
        $ctx = stream_context_create(array('http' => array('timeout' => 2)));
230
        return file_get_contents($url, false, $ctx);
231
    }
232
233
234
235
236
237

    private function startsWith($haystack, $needle)
    {
        $length = strlen($needle);
        return (substr($haystack, 0, $length) === $needle);
    }
238
239
240

    public function tips(Request $request)
    {
241
242
243
244
245
246
        $tipserver = '';
        if (env('APP_ENV') === "development") {
            $tipserver = "https://dev.quicktips.metager.de/1.1/tips.xml";
        } else {
            $tipserver = "https://quicktips.metager.de/1.1/tips.xml";
        }
247
248
249
        if (LaravelLocalization::getCurrentLocale() == "en") {
            $tipserver .= "?locale=en";
        }
250
        $tips_text = file_get_contents($tipserver);
251
        $tips = [];
252
        try {
Dominik Hebeler's avatar
Dominik Hebeler committed
253
            $tips_xml = \simplexml_load_string($tips_text);
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

            $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);
    }
269

270
    public function quicktips(Request $request)
271
    {
272
273
274
275
276
        $search = $request->input('search', '');
        if(empty($search)){
            abort(404)
        }

277
        $quicktips = new \App\Models\Quicktips\Quicktips($search);
Dominik Hebeler's avatar
Dominik Hebeler committed
278
279
280
        return view('quicktips')
            ->with('quicktips', $quicktips->getResults())
            ->with('search', $search);
281
    }
282
}