MetaGer.php 73.1 KB
Newer Older
1
<?php
Karl Hasselbring's avatar
Karl Hasselbring committed
2

3
4
5
namespace App;

use App;
6
use Cache;
7
use Carbon;
Davide's avatar
Davide committed
8
use Cookie;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Redis;
11
use Jenssegers\Agent\Agent;
Dominik Hebeler's avatar
Bugfix    
Dominik Hebeler committed
12
use LaravelLocalization;
13
use Log;
14
use Monospice\LaravelRedisSentinel\RedisSentinel;
15
use Predis\Connection\ConnectionException;
16
17
18

class MetaGer
{
Dominik Hebeler's avatar
Dominik Hebeler committed
19
20
    const FETCHQUEUE_KEY = "fetcher.queue";

21
    # Einstellungen für die Suche
22
23
    public $alteredQuery = "";
    public $alterationOverrideQuery = "";
24
    protected $fokus;
Dominik Hebeler's avatar
update    
Dominik Hebeler committed
25
    protected $test;
26
27
28
29
30
31
    protected $eingabe;
    protected $q;
    protected $page;
    protected $lang;
    protected $cache = "";
    protected $site;
32
    protected $time = 2000;
33
    protected $hostBlacklist = [];
34
    protected $domainBlacklist = [];
35
36
37
38
    private $urlBlacklist = [];
    protected $stopWords = [];
    protected $phrases = [];
    protected $engines = [];
39
    protected $totalResults = 0;
40
    protected $results = [];
Dominik Hebeler's avatar
Dominik Hebeler committed
41
42
    protected $queryFilter = [];
    protected $parameterFilter = [];
43
    protected $ads = [];
44
    protected $infos = [];
45
46
47
    protected $warnings = [];
    protected $errors = [];
    protected $addedHosts = [];
48
    protected $availableFoki = [];
49
50
    protected $startCount = 0;
    protected $canCache = false;
51
    # Daten über die Abfrage$
52
    protected $ip;
Dominik Hebeler's avatar
Dominik Hebeler committed
53
    protected $useragent;
54
55
    protected $language;
    protected $agent;
56
    protected $apiKey = "";
Phil Höfer's avatar
Phil Höfer committed
57
    protected $apiAuthorized = false;
58
    protected $next = [];
59
60
61
62
63
    # Konfigurationseinstellungen:
    protected $sumaFile;
    protected $mobile;
    protected $resultCount;
    protected $sprueche;
64
    protected $newtab;
65
    protected $domainsBlacklisted = [];
66
    protected $adDomainsBlacklisted = [];
67
    protected $urlsBlacklisted = [];
68
    protected $adUrlsBlacklisted = [];
69
    protected $url;
70
    protected $fullUrl;
Aria's avatar
Aria committed
71
    protected $enabledSearchengines = [];
72
    protected $languageDetect;
Dominik Hebeler's avatar
Dominik Hebeler committed
73
74
    protected $verificationId;
    protected $verificationCount;
75
    protected $searchUid;
76
77
78
79
    protected $redisResultWaitingKey;
    protected $redisResultEngineList;
    protected $redisEngineResult;
    protected $redisCurrentResultList;
Dominik Hebeler's avatar
Dominik Hebeler committed
80
    public $starttime;
81

82
    public function __construct($hash = "")
83
    {
84
        # Timer starten
85
        $this->starttime = microtime(true);
86
        # Versuchen Blacklists einzulesen
87
        if (file_exists(config_path() . "/blacklistDomains.txt") && file_exists(config_path() . "/blacklistUrl.txt")) {
88
            $tmp = file_get_contents(config_path() . "/blacklistDomains.txt");
89
            $this->domainsBlacklisted = explode("\n", $tmp);
90
91
            $tmp = file_get_contents(config_path() . "/blacklistUrl.txt");
            $this->urlsBlacklisted = explode("\n", $tmp);
92
        } else {
93
            Log::warning("Achtung: Eine, oder mehrere Blacklist Dateien, konnten nicht geöffnet werden");
94
        }
95
96
97
98
99
100
101
102
103
        # Versuchen Blacklists einzulesen
        if (file_exists(config_path() . "/adBlacklistDomains.txt") && file_exists(config_path() . "/adBlacklistUrl.txt")) {
            $tmp = file_get_contents(config_path() . "/adBlacklistDomains.txt");
            $this->adDomainsBlacklisted = explode("\n", $tmp);
            $tmp = file_get_contents(config_path() . "/adBlacklistUrl.txt");
            $this->adUrlsBlacklisted = explode("\n", $tmp);
        } else {
            Log::warning("Achtung: Eine, oder mehrere Blacklist Dateien, konnten nicht geöffnet werden");
        }
104

105
        # Parser Skripte einhängen
106
107
108
109
        $dir = app_path() . "/Models/parserSkripte/";
        foreach (scandir($dir) as $filename) {
            $path = $dir . $filename;
            if (is_file($path)) {
110
                require_once $path;
111
112
113
            }
        }

114
        # Cachebarkeit testen
115
116
117
118
119
120
        try {
            Cache::has('test');
            $this->canCache = true;
        } catch (ConnectionException $e) {
            $this->canCache = false;
        }
121
122
123
124
125
        if ($hash === "") {
            $this->searchUid = md5(uniqid());
        } else {
            $this->searchUid = $hash;
        }
126
127
128
129
130
131
132
        $redisPrefix = "search";
        # This is a list on which the MetaGer process can do a blocking pop to wait for new results
        $this->redisResultWaitingKey = $redisPrefix . "." . $this->searchUid . ".ready";
        # This is a list of searchengines which have delivered results for this search
        $this->redisResultEngineList = $redisPrefix . "." . $this->searchUid . ".engines";
        # This is the key where the results of the engine are stored as well as some statistical data
        $this->redisEngineResult = $redisPrefix . "." . $this->searchUid . ".results.";
133
134
        # A list of all search results already delivered to the user (sorted of course)
        $this->redisCurrentResultList = $redisPrefix . "." . $this->searchUid . ".currentResults";
135
    }
136

137
    # Erstellt aus den gesammelten Ergebnissen den View
138
    public function createView($quicktipResults = [])
139
    {
140
141
        # Hiermit werden die evtl. ausgewählten SuMas extrahiert, damit die Input-Boxen richtig gesetzt werden können
        $focusPages = [];
Aria Givi's avatar
Aria Givi committed
142

143
        foreach ($this->request->all() as $key => $value) {
144
145
            if (starts_with($key, 'engine_') && $value === 'on') {
                $focusPages[] = $key;
146
147
148
            }
        }

149
        $viewResults = [];
150
        # Wir extrahieren alle notwendigen Variablen und geben Sie an unseren View:
151
        foreach ($this->results as $result) {
152
153
154
155
            $viewResults[] = get_object_vars($result);
        }
        # Wir müssen natürlich noch den Log für die durchgeführte Suche schreiben:
        $this->createLogs();
156
157
        if ($this->fokus === "bilder") {
            switch ($this->out) {
158
                case 'results':
159
                    return view('resultpages.results_images')
160
161
162
163
164
                        ->with('results', $viewResults)
                        ->with('eingabe', $this->eingabe)
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
Phil Höfer's avatar
Phil Höfer committed
165
                        ->with('apiAuthorized', $this->apiAuthorized)
166
                        ->with('metager', $this)
167
                        ->with('browser', (new Agent())->browser());
168
                default:
169
                    return view('resultpages.resultpage_images')
170
171
172
173
174
                        ->with('results', $viewResults)
                        ->with('eingabe', $this->eingabe)
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
Phil Höfer's avatar
Phil Höfer committed
175
                        ->with('apiAuthorized', $this->apiAuthorized)
176
                        ->with('metager', $this)
177
                        ->with('browser', (new Agent())->browser())
178
                        ->with('quicktips', $quicktipResults)
179
180
                        ->with('focus', $this->fokus)
                        ->with('resultcount', count($this->results));
181
            }
182
183
184
        } else {
            switch ($this->out) {
                case 'results':
185
                    return view('resultpages.results')
186
187
188
189
190
                        ->with('results', $viewResults)
                        ->with('eingabe', $this->eingabe)
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
Phil Höfer's avatar
Phil Höfer committed
191
                        ->with('apiAuthorized', $this->apiAuthorized)
192
                        ->with('metager', $this)
Dominik Hebeler's avatar
Dominik Hebeler committed
193
194
                        ->with('browser', (new Agent())->browser())
                        ->with('fokus', $this->fokus);
195
196
                    break;
                case 'results-with-style':
197
                    return view('resultpages.resultpage')
198
199
200
201
202
                        ->with('results', $viewResults)
                        ->with('eingabe', $this->eingabe)
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
Phil Höfer's avatar
Phil Höfer committed
203
                        ->with('apiAuthorized', $this->apiAuthorized)
204
205
                        ->with('metager', $this)
                        ->with('suspendheader', "yes")
Dominik Hebeler's avatar
Dominik Hebeler committed
206
207
                        ->with('browser', (new Agent())->browser())
                        ->with('fokus', $this->fokus);
208
                    break;
Davide Aprea's avatar
Davide Aprea committed
209
                
210
                case 'api':
Dominik Hebeler's avatar
Dominik Hebeler committed
211
                    return view('resultpages.metager3resultsatom10', ['eingabe' => $this->eingabe, 'resultcount' => sizeof($viewResults), 'key' => $this->apiKey, 'metager' => $this]);
212
                    break;
213
                case 'atom10':
Dominik Hebeler's avatar
Dominik Hebeler committed
214
                    return view('resultpages.metager3resultsatom10', ['eingabe' => $this->eingabe, 'resultcount' => sizeof($viewResults), 'key' => $this->apiKey, 'metager' => $this]);
Davide Aprea's avatar
Davide Aprea committed
215
                    break;
216
                case 'result-count':
217
218
                    # Wir geben die Ergebniszahl und die benötigte Zeit zurück:
                    return sizeof($viewResults) . ";" . round((microtime(true) - $this->starttime), 2);
219
                    break;
220
                default:
221
                    return view('resultpages.resultpage')
222
                        ->with('eingabe', $this->eingabe)
223
                        ->with('focusPages', $focusPages)
224
225
226
                        ->with('mobile', $this->mobile)
                        ->with('warnings', $this->warnings)
                        ->with('errors', $this->errors)
Phil Höfer's avatar
Phil Höfer committed
227
                        ->with('apiAuthorized', $this->apiAuthorized)
228
                        ->with('metager', $this)
229
                        ->with('browser', (new Agent())->browser())
230
                        ->with('quicktips', $quicktipResults)
231
232
                        ->with('resultcount', count($this->results))
                        ->with('focus', $this->fokus);
233
234
                    break;
            }
235
236
237
        }
    }

238
    public function prepareResults(&$timings = null)
239
    {
Phil Höfer's avatar
Phil Höfer committed
240
241
        $engines = $this->engines;
        // combine
242
        $this->combineResults($engines);
243
        if (!empty($timings)) {
244
245
            $timings["prepareResults"]["combined results"] = microtime(true) - $timings["starttime"];
        }
Phil Höfer's avatar
Phil Höfer committed
246
        // misc (WiP)
247
        if ($this->fokus == "nachrichten") {
248
249
250
            $this->results = array_filter($this->results, function ($v, $k) {
                return !is_null($v->getRank());
            }, ARRAY_FILTER_USE_BOTH);
251
252
253
254
255
256
257
258
259
260
            uasort($this->results, function ($a, $b) {
                $datea = $a->getDate();
                $dateb = $b->getDate();
                return $dateb - $datea;
            });
        } else {
            uasort($this->results, function ($a, $b) {
                if ($a->getRank() == $b->getRank()) {
                    return 0;
                }
261

262
263
264
                return ($a->getRank() < $b->getRank()) ? 1 : -1;
            });
        }
265
        if (!empty($timings)) {
266
267
            $timings["prepareResults"]["sorted results"] = microtime(true) - $timings["starttime"];
        }
268
269
        # Validate Results
        $newResults = [];
270
271
        foreach ($this->results as $result) {
            if ($result->isValid($this)) {
272
                $newResults[] = $result;
273
            }
274
275
        }
        $this->results = $newResults;
276
        if (!empty($timings)) {
277
278
            $timings["prepareResults"]["validated results"] = microtime(true) - $timings["starttime"];
        }
279
        
Davide Aprea's avatar
Davide Aprea committed
280
        $this->duplicationCheck();
Dominik Hebeler's avatar
Dominik Hebeler committed
281
        if (!empty($timings)) {
282
283
            $timings["prepareResults"]["duplications checked"] = microtime(true) - $timings["starttime"];
        }
284
285
286
287
        # Validate Advertisements
        $newResults = [];
        foreach ($this->ads as $ad) {
            if (($ad->strippedHost !== "" && (in_array($ad->strippedHost, $this->adDomainsBlacklisted) ||
288
289
                in_array($ad->strippedLink, $this->adUrlsBlacklisted))) || ($ad->strippedHostAnzeige !== "" && (in_array($ad->strippedHostAnzeige, $this->adDomainsBlacklisted) ||
                in_array($ad->strippedLinkAnzeige, $this->adUrlsBlacklisted)))
Karl Hasselbring's avatar
Karl Hasselbring committed
290
            ) {
291
292
293
294
295
                continue;
            }
            $newResults[] = $ad;
        }
        $this->ads = $newResults;
296
        if (!empty($timings)) {
297
298
            $timings["prepareResults"]["validated ads"] = microtime(true) - $timings["starttime"];
        }
299
        #Adgoal Implementation
300
301
302
303
304
        if (empty($this->adgoalLoaded)) {
            $this->adgoalLoaded = false;
        }
        if (!$this->apiAuthorized && !$this->adgoalLoaded) {
            if (empty($this->adgoalHash)) {
Dominik Hebeler's avatar
Dominik Hebeler committed
305
306
307
308
309
                if (!empty($this->jskey)) {
                    $js = Redis::connection('cache')->lpop("js" . $this->jskey);
                    if ($js !== null && boolval($js)) {
                        $this->javascript = true;
                    }
310
                }
311
                $this->adgoalHash = $this->startAdgoal($this->results);
312
                if (!empty($timings)) {
313
314
                    $timings["prepareResults"]["started adgoal"] = microtime(true) - $timings["starttime"];
                }
315
            }
316
        
Dominik Hebeler's avatar
Dominik Hebeler committed
317
            if (!$this->javascript) {
318
                $this->adgoalLoaded = $this->parseAdgoal($this->results, $this->adgoalHash, true);
319
                if (!empty($timings)) {
320
321
                    $timings["prepareResults"]["parsed adgoal"] = microtime(true) - $timings["starttime"];
                }
322
323
            } else {
                $this->adgoalLoaded = $this->parseAdgoal($this->results, $this->adgoalHash, false);
324
                if (!empty($timings)) {
325
326
                    $timings["prepareResults"]["parsed adgoal"] = microtime(true) - $timings["starttime"];
                }
327
328
329
            }
        } else {
            $this->adgoalLoaded = true;
330
        }
331

Dominik Hebeler's avatar
Dominik Hebeler committed
332
        # Human Verification
333
334
        $this->humanVerification($this->results);
        $this->humanVerification($this->ads);
335
        if (!empty($timings)) {
336
337
            $timings["prepareResults"]["human verification"] = microtime(true) - $timings["starttime"];
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
338

339
        $counter = 0;
340
        $firstRank = 0;
341

342
        if (count($this->results) <= 0) {
343
344
345
346
347
348
            if (strlen($this->site) > 0) {
                $no_sitesearch_query = str_replace(urlencode("site:" . $this->site), "", $this->fullUrl);
                $this->errors[] = trans('metaGer.results.failedSitesearch', ['altSearch' => $no_sitesearch_query]);
            } else {
                $this->errors[] = trans('metaGer.results.failed');
            }
349
        }
350

351
        if ($this->canCache() && isset($this->next) && count($this->next) > 0 && count($this->results) > 0) {
352
            $page = $this->page + 1;
353
            $this->next = [
354
355
                'page' => $page,
                'engines' => $this->next,
356
            ];
357
            Cache::put($this->getSearchUid(), serialize($this->next), 60 * 60);
358
            if (!empty($timings)) {
359
360
                $timings["prepareResults"]["filled cache"] = microtime(true) - $timings["starttime"];
            }
361
362
        } else {
            $this->next = [];
363
        }
364
    }
365

Phil Höfer's avatar
Phil Höfer committed
366
367
368
369
370
371
372
373
374
375
376
    public function combineResults($engines)
    {
        foreach ($engines as $engine) {
            if (isset($engine->next)) {
                $this->next[] = $engine->next;
            }
            if (isset($engine->last)) {
                $this->last[] = $engine->last;
            }
            foreach ($engine->results as $result) {
                if ($result->valid) {
377
                    $this->results[] = clone $result;
Phil Höfer's avatar
Phil Höfer committed
378
379
380
                }
            }
            foreach ($engine->ads as $ad) {
381
                $this->ads[] = clone $ad;
Phil Höfer's avatar
Phil Höfer committed
382
383
384
385
            }
        }
    }

Davide Aprea's avatar
Davide Aprea committed
386
387
388
    public function duplicationCheck()
    {
        $arr = [];
Dominik Hebeler's avatar
Dominik Hebeler committed
389
        for ($i = 0; $i < count($this->results); $i++) {
Davide Aprea's avatar
Davide Aprea committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
            $link = $this->results[$i]->link;

            if (strpos($link, "http://") === 0) {
                $link = substr($link, 7);
            }
    
            if (strpos($link, "https://") === 0) {
                $link = substr($link, 8);
            }
    
            if (strpos($link, "www.") === 0) {
                $link = substr($link, 4);
            }
    
            $link = trim($link, "/");
            $hash = md5($link);

Dominik Hebeler's avatar
Dominik Hebeler committed
407
            if (isset($arr[$link])) {
Davide Aprea's avatar
Davide Aprea committed
408
409
                $arr[$link]->gefVon[] = $this->results[$i]->gefVon[0];
                $arr[$link]->gefVonLink[] = $this->results[$i]->gefVonLink[0];
Davide Aprea's avatar
Davide Aprea committed
410
411
                array_splice($this->results, $i, 1);
                $i--;
Dominik Hebeler's avatar
Dominik Hebeler committed
412
                if ($arr[$link]->new === true || $this->results[$i]->new === true) {
Davide Aprea's avatar
Davide Aprea committed
413
414
                    $arr[$link]->changed = true;
                }
Dominik Hebeler's avatar
Dominik Hebeler committed
415
            } else {
Davide Aprea's avatar
Davide Aprea committed
416
417
418
419
420
                $arr[$link] = &$this->results[$i];
            }
        }
    }

421
    public function startAdgoal(&$results)
422
    {
423
        $publicKey = getenv('adgoal_public');
424
        $privateKey = getenv('adgoal_private');
425
        if ($publicKey === false) {
426
            return true;
427
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
428
        $linkList = "";
429
430
431
432
433
434
435
436
        foreach ($results as $result) {
            if (!$result->new) {
                continue;
            }
            $link = $result->link;
            if (strpos($link, "http") !== 0) {
                $link = "http://" . $link;
            }
Dominik Hebeler's avatar
Dominik Hebeler committed
437
            $linkList .= $link . ",";
438
439
            $result->tld = parse_url($link, PHP_URL_HOST);
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
440
441

        $linkList = rtrim($linkList, ",");
442
443

        # Hashwert
Dominik Hebeler's avatar
Dominik Hebeler committed
444
        $hash = md5($linkList . $privateKey);
445

446
447
448
        # Query
        $query = $this->q;

Dominik Hebeler's avatar
Dominik Hebeler committed
449
450
451
452
453
454
455
456
457
        $link = "https://xf.gdprvalidate.de/v4/check";

        $postfields = [
            "key" => $publicKey,
            "panel" => "ZMkW9eSKJS",
            "member" => "338b9Bnm",
            "signature" => $hash,
            "links" => $linkList
        ];
458
459
460
461
462
463
464
465

        // Submit fetch job to worker
        $mission = [
            "resulthash" => $hash,
            "url" => $link,
            "useragent" => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0",
            "username" => null,
            "password" => null,
Dominik Hebeler's avatar
Dominik Hebeler committed
466
467
468
            "headers" => [
                "Content-Type" => "application/x-www-form-urlencoded"
            ],
469
            "cacheDuration" => 60,
470
            "name" => "Adgoal",
Dominik Hebeler's avatar
Dominik Hebeler committed
471
472
473
474
            "curlopts" => [
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => \http_build_query($postfields)
            ]
475
476
477
478
479
480
481
482
483
484
485
486
487
        ];
        $mission = json_encode($mission);
        Redis::rpush(\App\MetaGer::FETCHQUEUE_KEY, $mission);

        return $hash;
    }

    public function parseAdgoal(&$results, $hash, $waitForResult)
    {
        # Wait for result
        $startTime = microtime(true);
        $answer = null;

488
        # Hash is true if Adgoal request wasn't started in the first place
489
        if ($hash === true) {
490
491
            return true;
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
492
        
493
494
        if ($waitForResult) {
            while (microtime(true) - $startTime < 5) {
495
                $answer = Cache::get($hash);
496
497
498
499
500
501
502
                if ($answer === null) {
                    usleep(50 * 1000);
                } else {
                    break;
                }
            }
        } else {
503
            $answer = Cache::get($hash);
504
505
506
507
        }
        if ($answer === null) {
            return false;
        }
Dominik Hebeler's avatar
Dominik Hebeler committed
508
        
509
        try {
Dominik Hebeler's avatar
Dominik Hebeler committed
510
            $answer = json_decode($answer, true);
511
512

            # Nun müssen wir nur noch die Links für die Advertiser ändern:
513
            foreach ($results as $result) {
514
                $link = $result->link;
515
516
517
                $result->tld = parse_url($link, PHP_URL_HOST);
            }

Dominik Hebeler's avatar
Dominik Hebeler committed
518
519
520
521
            foreach ($answer as $partnershop) {
                $targetUrl = parse_url($partnershop["click_url"], PHP_URL_QUERY);
                parse_str($targetUrl, $params);
                $targetUrl = $params["url"];
522

523
                foreach ($results as $result) {
Dominik Hebeler's avatar
Dominik Hebeler committed
524
525
                    if ($result->link === $targetUrl && !$result->partnershop) {
                        # Ein Advertiser gefunden
526
                        if ($result->image !== "") {
Dominik Hebeler's avatar
Dominik Hebeler committed
527
                            $result->logo = $partnershop["logo"];
528
                        } else {
Dominik Hebeler's avatar
Dominik Hebeler committed
529
                            $result->image = $partnershop["logo"];
530
531
                        }

532
                        # Den Link hinzufügen:
Dominik Hebeler's avatar
Dominik Hebeler committed
533
                        $result->link = $partnershop["click_url"];
534
                        $result->partnershop = true;
Davide Aprea's avatar
Davide Aprea committed
535
                        $result->changed = true;
536
537
538
                    }
                }
            }
539
        } catch (\ErrorException $e) {
540
            Log::error($e->getMessage());
Dominik Hebeler's avatar
Dominik Hebeler committed
541
        } finally {
542
            $requestTime = microtime(true) - $startTime;
Dominik Hebeler's avatar
Dominik Hebeler committed
543
            \App\PrometheusExporter::Duration($requestTime, "adgoal");
544
        }
545
        return true;
546
    }
547

548
    public function humanVerification(&$results)
549
    {
Dominik Hebeler's avatar
Dominik Hebeler committed
550
        # Let's check if we need to implement a redirect for human verification
551
552
        if ($this->verificationCount > 10) {
            foreach ($results as $result) {
Dominik Hebeler's avatar
Dominik Hebeler committed
553
554
555
                $link = $result->link;
                $day = Carbon::now()->day;
                $pw = md5($this->verificationId . $day . $link . env("PROXY_PASSWORD"));
556
557
558
                $url = route('humanverification', ['mm' => $this->verificationId, 'pw' => $pw, "url" => urlencode(str_replace("/", "<<SLASH>>", base64_encode($link)))]);
                $proxyPw = md5($this->verificationId . $day . $result->proxyLink . env("PROXY_PASSWORD"));
                $proxyUrl = route('humanverification', ['mm' => $this->verificationId, 'pw' => $proxyPw, "url" => urlencode(str_replace("/", "<<SLASH>>", base64_encode($result->proxyLink)))]);
Dominik Hebeler's avatar
Dominik Hebeler committed
559
                $result->link = $url;
560
                $result->proxyLink = $proxyUrl;
Dominik Hebeler's avatar
Dominik Hebeler committed
561
562
563
564
            }
        }
    }

Phil Höfer's avatar
Phil Höfer committed
565
566
    public function authorize($key)
    {
567
        return app('App\Models\Key')->requestPermission();
Phil Höfer's avatar
Phil Höfer committed
568
569
    }

Karl's avatar
Karl committed
570
571
572
573
    /*
     * Die Erstellung der Suchmaschinen bis die Ergebnisse da sind mit Unterfunktionen
     */

Dominik Hebeler's avatar
Dominik Hebeler committed
574
    public function createSearchEngines(Request $request, &$timings)
575
    {
Dominik Hebeler's avatar
Dominik Hebeler committed
576
577
578
579
        if (!empty($timings)) {
            $timings["createSearchEngines"]["start"] = microtime(true) - $timings["starttime"];
        }

580
        # Wenn es kein Suchwort gibt
Dominik Hebeler's avatar
Dominik Hebeler committed
581
        if (!$request->filled("eingabe") || $this->q === "") {
582
            return;
583
        }
584

Dominik Hebeler's avatar
Dominik Hebeler committed
585
        $this->enabledSearchengines = [];
586
        $overtureEnabled = false;
587

588
589
590
        # Check if selected focus is valid
        if (empty($this->sumaFile->foki->{$this->fokus})) {
            $this->fokus = "web";
591
        }
592

Karl Hasselbring's avatar
Karl Hasselbring committed
593
        $sumaNames = $this->sumaFile->foki->{$this->fokus}->sumas;
594

Karl Hasselbring's avatar
Karl Hasselbring committed
595
596
597
        $sumas = [];
        foreach ($sumaNames as $sumaName) {
            $sumas[$sumaName] = $this->sumaFile->sumas->{$sumaName};
598
599
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
600
601
602
603
        if (!empty($timings)) {
            $timings["createSearchEngines"]["created engine array"] = microtime(true) - $timings["starttime"];
        }

Karl Hasselbring's avatar
Karl Hasselbring committed
604
605
        $this->removeAdsFromListIfAdfree($sumas);

Dominik Hebeler's avatar
Dominik Hebeler committed
606
607
608
609
        if (!empty($timings)) {
            $timings["createSearchEngines"]["removed ads"] = microtime(true) - $timings["starttime"];
        }

Karl Hasselbring's avatar
Karl Hasselbring committed
610
        foreach ($sumas as $sumaName => $suma) {
611
            # Check if this engine is disabled and can't be used
Karl Hasselbring's avatar
Karl Hasselbring committed
612
613
614
615
616
617
618
            $disabled = empty($suma->disabled) ? false : $suma->disabled;
            $autoDisabled = empty($suma->{"auto-disabled"}) ? false : $suma->{"auto-disabled"};
            if (
                $disabled || $autoDisabled
                || \Cookie::get($this->getFokus() . "_engine_" . $sumaName) === "off"
            ) {
                continue;
619
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
620

621
            $valid = true;
Karl Hasselbring's avatar
Karl Hasselbring committed
622
623
624
625

            # Check if this engine can use potentially defined query-filter
            foreach ($this->queryFilter as $filterName => $filter) {
                if (empty($this->sumaFile->filter->{"query-filter"}->$filterName->sumas->$sumaName)) {
626
627
                    $valid = false;
                    break;
Dominik Hebeler's avatar
Dominik Hebeler committed
628
629
                }
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
630
631

            # Check if this engine can use potentially defined parameter-filter
Dominik Hebeler's avatar
Dominik Hebeler committed
632
633
            if ($valid) {
                foreach ($this->parameterFilter as $filterName => $filter) {
634
                    # We need to check if the searchengine supports the parameter value, too
Karl Hasselbring's avatar
Karl Hasselbring committed
635
                    if (empty($filter->sumas->$sumaName) || empty($filter->sumas->{$sumaName}->values->{$filter->value})) {
Dominik Hebeler's avatar
Dominik Hebeler committed
636
637
638
639
640
                        $valid = false;
                        break;
                    }
                }
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
641

642
            # Check if this engine should only be active when filter is used
Karl Hasselbring's avatar
Karl Hasselbring committed
643
            if ($suma->{"filter-opt-in"}) {
644
645
646
                # This search engine should only be used when a parameter filter of it is used
                $validTmp = false;
                foreach ($this->parameterFilter as $filterName => $filter) {
Karl Hasselbring's avatar
Karl Hasselbring committed
647
                    if (!empty($filter->sumas->{$sumaName})) {
648
649
650
651
652
653
654
655
                        $validTmp = true;
                        break;
                    }
                }
                if (!$validTmp) {
                    $valid = false;
                }
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
656
657

            # If the suma is still valid, we can add it
658
            if ($valid) {
Karl Hasselbring's avatar
Karl Hasselbring committed
659
                $this->enabledSearchengines[$sumaName] = $suma;
660
            }
661
        }
662

Dominik Hebeler's avatar
Dominik Hebeler committed
663
664
665
666
        if (!empty($timings)) {
            $timings["createSearchEngines"]["filtered invalid engines"] = microtime(true) - $timings["starttime"];
        }

Karl Hasselbring's avatar
Karl Hasselbring committed
667
668
        # Include Yahoo Ads if Yahoo is not enabled as a searchengine
        if (!$this->apiAuthorized && $this->fokus != "bilder" && empty($this->enabledSearchengines["yahoo"]) && isset($this->sumaFile->sumas->{"yahoo-ads"})) {
669
670
671
            $this->enabledSearchengines["yahoo-ads"] = $this->sumaFile->sumas->{"yahoo-ads"};
        }

672
673
        # Special case if search engines are disabled
        # Since bing is normally only active if a filter is set but it should be active, too if yahoo is disabled
Davide Aprea's avatar
Davide Aprea committed
674
        if ($this->getFokus() === "web" && empty($this->enabledSearchengines["yahoo"]) && \Cookie::get("web_engine_bing") !== "off"  && isset($this->sumaFile->sumas->{"bing"})) {
675
676
677
            $this->enabledSearchengines["bing"] = $this->sumaFile->sumas->{"bing"};
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
678
        if (sizeof($this->enabledSearchengines) === 0) {
679
680
            $filter = "";
            foreach ($this->queryFilter as $queryFilter => $filterPhrase) {
Dominik Hebeler's avatar
Dominik Hebeler committed
681
                $filter .= trans($this->sumaFile->filter->{"query-filter"}->{$queryFilter}->name) . ",";
682
683
            }
            $filter = rtrim($filter, ",");
Karl Hasselbring's avatar
Karl Hasselbring committed
684
685
            $error = trans('metaGer.engines.noSpecialSearch', [
                'fokus' => trans($this->sumaFile->foki->{$this->fokus}->{"display-name"}),
686
                'filter' => $filter,
Karl Hasselbring's avatar
Karl Hasselbring committed
687
            ]);
688
            $this->errors[] = $error;
689
        }
690
        $this->setEngines($request);
Dominik Hebeler's avatar
Dominik Hebeler committed
691
692
693
        if (!empty($timings)) {
            $timings["createSearchEngines"]["saved engines"] = microtime(true) - $timings["starttime"];
        }
694
    }
695

Karl Hasselbring's avatar
Karl Hasselbring committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
    private function removeAdsFromListIfAdfree(&$sumas)
    {
        if ($this->apiAuthorized) {
            foreach ($sumas as $sumaName => $suma) {
                $ads = $suma->ads ?? false;
                if ($ads) {
                    unset($sumas[$sumaName]);

                    $adBackups = $suma->{"ad-backups"} ?? [];
                    $adBackupName = $adBackups->{$this->fokus} ?? null;
                    if (isset($adBackupName)) {
                        $this->sumaFile->sumas->{$adBackupName}->{"filter-opt-in"} = false;
                    }
                }
            }
        }
    }

714
715
    public function setEngines(Request $request, $enabledSearchengines = [])
    {
716
        if ($this->requestIsCached($request)) {
717
            # If this is a page other than 1 the request is "cached"
718
            $engines = $this->getCachedEngines($request);
Dominik Hebeler's avatar
Dominik Hebeler committed
719
            # We need to edit some Options of the Cached Search Engines
Phil Höfer's avatar
Phil Höfer committed
720
            foreach ($engines as $engine) {
721
                $engine->setResultHash($this->getSearchUid());
Dominik Hebeler's avatar
Dominik Hebeler committed
722
            }
723
            $this->engines = $engines;
724
        } else {
725
726
727
            if (sizeof($enabledSearchengines) > 0) {
                $this->enabledSearchengines = $enabledSearchengines;
            }
728
            $this->actuallyCreateSearchEngines($this->enabledSearchengines);
729
        }
730
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
731

Dominik Hebeler's avatar
Dominik Hebeler committed
732
    public function startSearch(&$timings)
733
    {
734
735
736
737
738
        if (!empty($timings)) {
            $timings["startSearch"]["start"] = microtime(true) - $timings["starttime"];
        }

        # Check all engines for Cached responses
Dominik Hebeler's avatar
Dominik Hebeler committed
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
        $this->checkCache();

        if (!empty($timings)) {
            $timings["startSearch"]["cache checked"] = microtime(true) - $timings["starttime"];
        }

        # Wir starten alle Suchen
        foreach ($this->engines as $engine) {
            $engine->startSearch($this, $timings);
        }
        if (!empty($timings)) {
            $timings["startSearch"]["searches started"] = microtime(true) - $timings["starttime"];
        }
    }

    public function checkCache()
    {
756
757
758
759
760
        if ($this->canCache()) {
            $keys = [];
            foreach ($this->engines as $engine) {
                $keys[] = $engine->hash;
            }
761
762
763
764
            # Noch searchengines enabled
            if (empty($keys)) {
                return;
            }
765
766
767
768
769
770
771
772
            $cacheValues = Cache::many($keys);
            foreach ($this->engines as $engine) {
                if ($cacheValues[$engine->hash] !== null) {
                    $engine->cached = true;
                    $engine->retrieveResults($this, $cacheValues[$engine->hash]);
                }
            }
        }
773
774
    }

775
776
    # Spezielle Suchen und Sumas

777
    public function sumaIsSelected($suma, $request, $custom)
778
    {
779
        if ($custom) {
780
            if ($request->filled("engine_" . strtolower($suma["name"]))) {
781
782
783
784
785
786
787
788
789
790
791
                return true;
            }
        } else {
            $types = explode(",", $suma["type"]);
            if (in_array($this->fokus, $types)) {
                return true;
            }
        }
        return false;
    }

792
    public function actuallyCreateSearchEngines($enabledSearchengines)
793
794
    {
        $engines = [];
795
796
        foreach ($enabledSearchengines as $engineName => $engine) {
            if (!isset($engine->{"parser-class"})) {
Dominik Hebeler's avatar
Dominik Hebeler committed
797
798
                die(var_dump($engine));
            }
799
            # Setze Pfad zu Parser
800
            $path = "App\\Models\\parserSkripte\\" . $engine->{"parser-class"};
801
802

            # Prüfe ob Parser vorhanden
803
804
805
            if (!file_exists(app_path() . "/Models/parserSkripte/" . $engine->{"parser-class"} . ".php")) {
                Log::error("Konnte " . $engine->{"display-name"} . " nicht abfragen, da kein Parser existiert");
                $this->errors[] = trans('metaGer.engines.noParser', ['engine' => $engine->{"display-name"}]);
806
807
808
809
810
811
                continue;
            }

            # Es wird versucht die Suchengine zu erstellen
            $time = microtime();
            try {
812
                $tmp = new $path($engineName, $engine, $this);
813
            } catch (\ErrorException $e) {
814
                Log::error("Konnte " . $engine->{"display-name"} . " nicht abfragen. " . $e);
815
816
817
                continue;
            }

818
            $engines[] = $tmp;
819
        }
820
        $this->engines = $engines;
821
822
    }

Dominik Hebeler's avatar
Dominik Hebeler committed
823
824
825
826
827
828
829
    public function getAvailableParameterFilter()
    {
        $parameterFilter = $this->sumaFile->filter->{"parameter-filter"};

        $availableFilter = [];

        foreach ($parameterFilter as $filterName => $filter) {
830
            $values = clone $filter->values;
Dominik Hebeler's avatar
Dominik Hebeler committed
831
832
833
            # Check if any of the enabled search engines provide this filter
            foreach ($this->enabledSearchengines as $engineName => $engine) {
                if (!empty($filter->sumas->$engineName)) {
834
835
                    if (empty($availableFilter[$filterName])) {
                        $availableFilter[$filterName] = $filter;
836
837
                        foreach ($availableFilter[$filterName]->values as $key => $value) {
                            if ($key !== "nofilter") {
838
839
840
                                unset($availableFilter[$filterName]->values->{$key});
                            }
                        }
841
842
                    }
                    if (empty($availableFilter[$filterName]->values)) {
843
                        $availableFilter[$filterName]->values = new \stdClass();
844
845
                    }
                    foreach ($filter->sumas->{$engineName}->values as $key => $value) {
846
                        $availableFilter[$filterName]->values->{$key} = $values->$key;
847
                    }
Dominik Hebeler's avatar
Dominik Hebeler committed
848
849
                }
            }
850
851
            # We will also add the filter from the opt-in search engines (the searchengines that are only used when a filter of it is too)
            foreach ($this->sumaFile->foki->{$this->fokus}->sumas as $suma) {
852
                if ($this->sumaFile->sumas->{$suma}->{"filter-opt-in"} && \Cookie::get($this->getFokus() . "_engine_" . $suma) !== "off") {
853
                    if (!empty($filter->sumas->{$suma})) {
854
855
                        # If the searchengine is disabled this filter shouldn't be available
                        if ((!empty($this->sumaFile->sumas->{$suma}->disabled) && $this->sumaFile->sumas->{$suma}->disabled === true)
Karl Hasselbring's avatar
Karl Hasselbring committed
856
857
                            || (!empty($this->sumaFile->sumas->{$suma}->{"auto-disabled"}) && $this->sumaFile->sumas->{$suma}->{"auto-disabled"} === true)
                        ) {
858
859
                            continue;
                        }
860
861
                        if (empty($availableFilter[$filterName])) {
                            $availableFilter[$filterName] = $filter;
862
863
                            foreach ($availableFilter[$filterName]->values as $key => $value) {
                                if ($key !== "nofilter") {
864
865
866
                                    unset($availableFilter[$filterName]->values->{$key});
                                }
                            }
867
868
                        }
                        if (empty($availableFilter[$filterName]->values)) {
869
                            $availableFilter[$filterName]->values = new \stdClass();