MetaGer.php 68.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;
Davide Aprea's avatar
Davide Aprea committed
81
    protected $dummy = false;
82

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

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

115
        # Cachebarkeit testen
116
117
118
119
120
121
        try {
            Cache::has('test');
            $this->canCache = true;
        } catch (ConnectionException $e) {
            $this->canCache = false;
        }
122
123
124
125
126
        if ($hash === "") {
            $this->searchUid = md5(uniqid());
        } else {
            $this->searchUid = $hash;
        }
127
128
129
130
131
132
133
        $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.";
134
135
        # A list of all search results already delivered to the user (sorted of course)
        $this->redisCurrentResultList = $redisPrefix . "." . $this->searchUid . ".currentResults";
136
    }
137

138
    # Erstellt aus den gesammelten Ergebnissen den View
139
    public function createView($quicktipResults = [])
140
    {
141
142
        # 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
143

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

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

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

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

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

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

343
        if (count($this->results) <= 0) {
344
345
346
347
348
349
            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');
            }
350
        }
351

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

Phil Höfer's avatar
Phil Höfer committed
367
368
369
370
371
372
373
374
375
376
377
    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) {
378
                    $this->results[] = clone $result;
Phil Höfer's avatar
Phil Höfer committed
379
380
381
                }
            }
            foreach ($engine->ads as $ad) {
382
                $this->ads[] = clone $ad;
Phil Höfer's avatar
Phil Höfer committed
383
384
385
386
            }
        }
    }

Davide Aprea's avatar
Davide Aprea committed
387
388
389
    public function duplicationCheck()
    {
        $arr = [];
Dominik Hebeler's avatar
Dominik Hebeler committed
390
        for ($i = 0; $i < count($this->results); $i++) {
Davide Aprea's avatar
Davide Aprea committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
            $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
408
            if (isset($arr[$link])) {
Davide Aprea's avatar
Davide Aprea committed
409
410
                $arr[$link]->gefVon[] = $this->results[$i]->gefVon[0];
                $arr[$link]->gefVonLink[] = $this->results[$i]->gefVonLink[0];
411
412
413
414
415
416
417
418
419
420

                // The duplicate might already be an adgoal partnershop
                if($this->results[$i]->partnershop){
                    # Den Link hinzufügen:
                    $arr[$link]->logo = $this->results[$i]->logo;
                    $arr[$link]->image = $this->results[$i]->image;
                    $arr[$link]->link = $this->results[$i]->link;
                    $arr[$link]->partnershop = $this->results[$i]->partnershop;
                }

Davide Aprea's avatar
Davide Aprea committed
421
422
                array_splice($this->results, $i, 1);
                $i--;
Dominik Hebeler's avatar
Dominik Hebeler committed
423
                if ($arr[$link]->new === true || $this->results[$i]->new === true) {
424
                    $arr[$link]->changed = true; 
Davide Aprea's avatar
Davide Aprea committed
425
                }
Dominik Hebeler's avatar
Dominik Hebeler committed
426
            } else {
Davide Aprea's avatar
Davide Aprea committed
427
428
429
430
431
                $arr[$link] = &$this->results[$i];
            }
        }
    }

432
    
433

434
    public function humanVerification(&$results)
435
    {
Dominik Hebeler's avatar
Dominik Hebeler committed
436
        # Let's check if we need to implement a redirect for human verification
437
438
        if ($this->verificationCount > 10) {
            foreach ($results as $result) {
Dominik Hebeler's avatar
Dominik Hebeler committed
439
440
441
                $link = $result->link;
                $day = Carbon::now()->day;
                $pw = md5($this->verificationId . $day . $link . env("PROXY_PASSWORD"));
442
443
444
                $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
445
                $result->link = $url;
446
                $result->proxyLink = $proxyUrl;
Dominik Hebeler's avatar
Dominik Hebeler committed
447
448
449
450
            }
        }
    }

Phil Höfer's avatar
Phil Höfer committed
451
452
    public function authorize($key)
    {
453
        return app('App\Models\Key')->requestPermission();
Phil Höfer's avatar
Phil Höfer committed
454
455
    }

Karl's avatar
Karl committed
456
457
458
459
    /*
     * Die Erstellung der Suchmaschinen bis die Ergebnisse da sind mit Unterfunktionen
     */

Dominik Hebeler's avatar
Dominik Hebeler committed
460
    public function createSearchEngines(Request $request, &$timings)
461
    {
Dominik Hebeler's avatar
Dominik Hebeler committed
462
463
464
465
        if (!empty($timings)) {
            $timings["createSearchEngines"]["start"] = microtime(true) - $timings["starttime"];
        }

466
        # Wenn es kein Suchwort gibt
Dominik Hebeler's avatar
Dominik Hebeler committed
467
        if (!$request->filled("eingabe") || $this->q === "") {
468
            return;
469
        }
470

Dominik Hebeler's avatar
Dominik Hebeler committed
471
        $this->enabledSearchengines = [];
472
        $overtureEnabled = false;
473

474
475
476
        # Check if selected focus is valid
        if (empty($this->sumaFile->foki->{$this->fokus})) {
            $this->fokus = "web";
477
        }
478

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

Karl Hasselbring's avatar
Karl Hasselbring committed
481
482
483
        $sumas = [];
        foreach ($sumaNames as $sumaName) {
            $sumas[$sumaName] = $this->sumaFile->sumas->{$sumaName};
484
485
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
486
487
488
489
        if (!empty($timings)) {
            $timings["createSearchEngines"]["created engine array"] = microtime(true) - $timings["starttime"];
        }

Karl Hasselbring's avatar
Karl Hasselbring committed
490
491
        $this->removeAdsFromListIfAdfree($sumas);

Dominik Hebeler's avatar
Dominik Hebeler committed
492
493
494
495
        if (!empty($timings)) {
            $timings["createSearchEngines"]["removed ads"] = microtime(true) - $timings["starttime"];
        }

Karl Hasselbring's avatar
Karl Hasselbring committed
496
        foreach ($sumas as $sumaName => $suma) {
497
            # Check if this engine is disabled and can't be used
Karl Hasselbring's avatar
Karl Hasselbring committed
498
499
500
501
502
503
504
            $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;
505
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
506

507
            $valid = true;
Karl Hasselbring's avatar
Karl Hasselbring committed
508
509
510
511

            # 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)) {
512
513
                    $valid = false;
                    break;
Dominik Hebeler's avatar
Dominik Hebeler committed
514
515
                }
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
516
517

            # Check if this engine can use potentially defined parameter-filter
Dominik Hebeler's avatar
Dominik Hebeler committed
518
519
            if ($valid) {
                foreach ($this->parameterFilter as $filterName => $filter) {
520
                    # We need to check if the searchengine supports the parameter value, too
Karl Hasselbring's avatar
Karl Hasselbring committed
521
                    if (empty($filter->sumas->$sumaName) || empty($filter->sumas->{$sumaName}->values->{$filter->value})) {
Dominik Hebeler's avatar
Dominik Hebeler committed
522
523
524
525
526
                        $valid = false;
                        break;
                    }
                }
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
527

528
            # Check if this engine should only be active when filter is used
Karl Hasselbring's avatar
Karl Hasselbring committed
529
            if ($suma->{"filter-opt-in"}) {
530
531
532
                # 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
533
                    if (!empty($filter->sumas->{$sumaName})) {
534
535
536
537
538
539
540
541
                        $validTmp = true;
                        break;
                    }
                }
                if (!$validTmp) {
                    $valid = false;
                }
            }
Karl Hasselbring's avatar
Karl Hasselbring committed
542
543

            # If the suma is still valid, we can add it
544
            if ($valid) {
Karl Hasselbring's avatar
Karl Hasselbring committed
545
                $this->enabledSearchengines[$sumaName] = $suma;
546
            }
547
        }
548

Dominik Hebeler's avatar
Dominik Hebeler committed
549
550
551
552
        if (!empty($timings)) {
            $timings["createSearchEngines"]["filtered invalid engines"] = microtime(true) - $timings["starttime"];
        }

Karl Hasselbring's avatar
Karl Hasselbring committed
553
554
        # 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"})) {
555
556
557
            $this->enabledSearchengines["yahoo-ads"] = $this->sumaFile->sumas->{"yahoo-ads"};
        }

558
559
        # 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
560
        if ($this->getFokus() === "web" && empty($this->enabledSearchengines["yahoo"]) && \Cookie::get("web_engine_bing") !== "off"  && isset($this->sumaFile->sumas->{"bing"})) {
561
562
563
            $this->enabledSearchengines["bing"] = $this->sumaFile->sumas->{"bing"};
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
564
        if (sizeof($this->enabledSearchengines) === 0) {
565
566
            $filter = "";
            foreach ($this->queryFilter as $queryFilter => $filterPhrase) {
Dominik Hebeler's avatar
Dominik Hebeler committed
567
                $filter .= trans($this->sumaFile->filter->{"query-filter"}->{$queryFilter}->name) . ",";
568
569
            }
            $filter = rtrim($filter, ",");
Karl Hasselbring's avatar
Karl Hasselbring committed
570
571
            $error = trans('metaGer.engines.noSpecialSearch', [
                'fokus' => trans($this->sumaFile->foki->{$this->fokus}->{"display-name"}),
572
                'filter' => $filter,
Karl Hasselbring's avatar
Karl Hasselbring committed
573
            ]);
574
            $this->errors[] = $error;
575
        }
576
        $this->setEngines($request);
Dominik Hebeler's avatar
Dominik Hebeler committed
577
578
579
        if (!empty($timings)) {
            $timings["createSearchEngines"]["saved engines"] = microtime(true) - $timings["starttime"];
        }
580
    }
581

Karl Hasselbring's avatar
Karl Hasselbring committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
    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;
                    }
                }
            }
        }
    }

600
601
    public function setEngines(Request $request, $enabledSearchengines = [])
    {
602
        if ($this->requestIsCached($request)) {
603
            # If this is a page other than 1 the request is "cached"
604
            $engines = $this->getCachedEngines($request);
Dominik Hebeler's avatar
Dominik Hebeler committed
605
            # We need to edit some Options of the Cached Search Engines
Phil Höfer's avatar
Phil Höfer committed
606
            foreach ($engines as $engine) {
607
                $engine->setResultHash($this->getSearchUid());
Dominik Hebeler's avatar
Dominik Hebeler committed
608
            }
609
            $this->engines = $engines;
610
        } else {
611
612
613
            if (sizeof($enabledSearchengines) > 0) {
                $this->enabledSearchengines = $enabledSearchengines;
            }
614
            $this->actuallyCreateSearchEngines($this->enabledSearchengines);
615
        }
616
    }
Dominik Hebeler's avatar
Dominik Hebeler committed
617

Dominik Hebeler's avatar
Dominik Hebeler committed
618
    public function startSearch(&$timings)
619
    {
620
621
622
623
624
        if (!empty($timings)) {
            $timings["startSearch"]["start"] = microtime(true) - $timings["starttime"];
        }

        # Check all engines for Cached responses
Dominik Hebeler's avatar
Dominik Hebeler committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
        $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()
    {
642
643
644
645
646
        if ($this->canCache()) {
            $keys = [];
            foreach ($this->engines as $engine) {
                $keys[] = $engine->hash;
            }
647
648
649
650
            # Noch searchengines enabled
            if (empty($keys)) {
                return;
            }
651
652
653
654
655
656
657
658
            $cacheValues = Cache::many($keys);
            foreach ($this->engines as $engine) {
                if ($cacheValues[$engine->hash] !== null) {
                    $engine->cached = true;
                    $engine->retrieveResults($this, $cacheValues[$engine->hash]);
                }
            }
        }
659
660
    }

661
662
    # Spezielle Suchen und Sumas

663
    public function sumaIsSelected($suma, $request, $custom)
664
    {
665
        if ($custom) {
666
            if ($request->filled("engine_" . strtolower($suma["name"]))) {
667
668
669
670
671
672
673
674
675
676
677
                return true;
            }
        } else {
            $types = explode(",", $suma["type"]);
            if (in_array($this->fokus, $types)) {
                return true;
            }
        }
        return false;
    }

678
    public function actuallyCreateSearchEngines($enabledSearchengines)
679
680
    {
        $engines = [];
681
682
        foreach ($enabledSearchengines as $engineName => $engine) {
            if (!isset($engine->{"parser-class"})) {
Dominik Hebeler's avatar
Dominik Hebeler committed
683
684
                die(var_dump($engine));
            }
685
            # Setze Pfad zu Parser
686
            $path = "App\\Models\\parserSkripte\\" . $engine->{"parser-class"};
687
688

            # Prüfe ob Parser vorhanden
689
690
691
            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"}]);
692
693
694
695
696
697
                continue;
            }

            # Es wird versucht die Suchengine zu erstellen
            $time = microtime();
            try {
698
                $tmp = new $path($engineName, $engine, $this);
699
            } catch (\ErrorException $e) {
700
                Log::error("Konnte " . $engine->{"display-name"} . " nicht abfragen. " . $e);
701
702
703
                continue;
            }

704
            $engines[] = $tmp;
705
        }
706
        $this->engines = $engines;
707
708
    }

Dominik Hebeler's avatar
Dominik Hebeler committed
709
710
711
712
713
714
715
    public function getAvailableParameterFilter()
    {
        $parameterFilter = $this->sumaFile->filter->{"parameter-filter"};

        $availableFilter = [];

        foreach ($parameterFilter as $filterName => $filter) {
716
            $values = clone $filter->values;
Dominik Hebeler's avatar
Dominik Hebeler committed
717
718
719
            # Check if any of the enabled search engines provide this filter
            foreach ($this->enabledSearchengines as $engineName => $engine) {
                if (!empty($filter->sumas->$engineName)) {
720
721
                    if (empty($availableFilter[$filterName])) {
                        $availableFilter[$filterName] = $filter;
722
723
                        foreach ($availableFilter[$filterName]->values as $key => $value) {
                            if ($key !== "nofilter") {
724
725
726
                                unset($availableFilter[$filterName]->values->{$key});
                            }
                        }
727
728
                    }
                    if (empty($availableFilter[$filterName]->values)) {
729
                        $availableFilter[$filterName]->values = new \stdClass();
730
731
                    }
                    foreach ($filter->sumas->{$engineName}->values as $key => $value) {
732
                        $availableFilter[$filterName]->values->{$key} = $values->$key;
733
                    }
Dominik Hebeler's avatar
Dominik Hebeler committed
734
735
                }
            }
736
737
            # 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) {
738
                if ($this->sumaFile->sumas->{$suma}->{"filter-opt-in"} && \Cookie::get($this->getFokus() . "_engine_" . $suma) !== "off") {
739
                    if (!empty($filter->sumas->{$suma})) {
740
741
                        # 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
742
743
                            || (!empty($this->sumaFile->sumas->{$suma}->{"auto-disabled"}) && $this->sumaFile->sumas->{$suma}->{"auto-disabled"} === true)
                        ) {
744
745
                            continue;
                        }
746
747
                        if (empty($availableFilter[$filterName])) {
                            $availableFilter[$filterName] = $filter;
748
749
                            foreach ($availableFilter[$filterName]->values as $key => $value) {
                                if ($key !== "nofilter") {
750
751
752
                                    unset($availableFilter[$filterName]->values->{$key});
                                }
                            }
753
754
                        }
                        if (empty($availableFilter[$filterName]->values)) {
755
                            $availableFilter[$filterName]->values = new \stdClass();
756
757
                        }
                        foreach ($filter->sumas->{$suma}->values as $key => $value) {
758
                            $availableFilter[$filterName]->values->{$key} = $values->$key;
759
                        }
760
761
762
                    }
                }
            }
Dominik Hebeler's avatar
Dominik Hebeler committed
763
764
        }

765
766
767
768
        # Set the current values for the filters
        foreach ($availableFilter as $filterName => $filter) {
            if (\Request::filled($filter->{"get-parameter"})) {
                $filter->value = \Request::input($filter->{"get-parameter"});
769
            } elseif (\Cookie::get($this->getFokus() . "_setting_" . $filter->{"get-parameter"}) !== null) {
770
771
772
773
                $filter->value = \Cookie::get($this->getFokus() . "_setting_" . $filter->{"get-parameter"});
            }
        }

Dominik Hebeler's avatar
Dominik Hebeler committed
774
775
776
        return $availableFilter;
    }

777
778
    public function isBildersuche()
    {
779
        return $this->fokus === "bilder";
780
781
782
783
    }

    public function sumaIsAdsuche($suma, $overtureEnabled)
    {
784
        $sumaName = $suma["name"]->__toString();
785
        return
786
787
            $sumaName === "qualigo"
            || $sumaName === "similar_product_ads"
Karl Hasselbring's avatar
Karl Hasselbring committed
788
            || (!$overtureEnabled && $sumaName === "overtureAds");
789
790
791
792
793
    }

    public function sumaIsDisabled($suma)
    {
        return
794
795
        isset($suma['disabled'])
        && $suma['disabled']->__toString() === "1";
796
797
798
799
800
    }

    public function sumaIsOverture($suma)
    {
        return
801
802
        $suma["name"]->__toString() === "overture"
        || $suma["name"]->__toString() === "overtureAds";
803
804
805
806
807
    }

    public function sumaIsNotAdsuche($suma)
    {
        return
808
809
810
        $suma["name"]->__toString() !== "qualigo"
        && $suma["name"]->__toString() !== "similar_product_ads"
        && $suma["name"]->__toString() !== "overtureAds";
811
812
813
814
815
    }

    public function requestIsCached($request)
    {
        return
816
817
818
        $request->filled('next')
        && Cache::has($request->input('next'))
        && unserialize(Cache::get($request->input('next')))['page'] > 1;
819
820
821
822
    }

    public function getCachedEngines($request)
    {
823
        $next = unserialize(Cache::get($request->input('next')));
824
        $this->page = $next['page'];
825
        $engines = $next['engines'];
826
827
828
        return $engines;
    }

829
    public function waitForMainResults()
830
    {
831
        $engines = $this->engines;
832
        $enginesToWaitFor = [];
833
834
835
        $mainEngines = $this->sumaFile->foki->{$this->fokus}->main;
        foreach ($mainEngines as $mainEngine) {
            foreach ($engines as $engine) {
836
                if ($engine->name === $mainEngine) {
837
                    $enginesToWaitFor[] = $engine->hash;
838
                }
839
840
            }
        }
841

842
843
844
845
846
        # If no main engines are enabled by the user we will wait for all results
        if (sizeof($enginesToWaitFor) === 0) {
            foreach ($engines as $engine) {
                $enginesToWaitFor[] = $engine->hash;
            }
847
848
849
850
851
852
853
854
855
856
857
        } else {
            $newEnginesToWaitFor = [];
            // Don't wait for engines that are already loaded in Cache
            foreach ($enginesToWaitFor as $engineToWaitFor) {
                foreach ($engines as $engine) {
                    if ($engine->hash === $engineToWaitFor && !$engine->loaded) {
                        $newEnginesToWaitFor[] = $engineToWaitFor;
                    }
                }
            }
            $enginesToWaitFor = $newEnginesToWaitFor;
858
859
        }

Phil Höfer's avatar
Phil Höfer committed
860
        $timeStart = microtime(true);
861
        while (sizeof($enginesToWaitFor) > 0) {
862
            if ((microtime(true) - $timeStart) >= 2) {
863
                break;
864
865
866
867
868
            }
            $answer = Redis::brpop($enginesToWaitFor, 2);
            
            if ($answer === null) {
                continue;
869
870
            } else {
                Redis::lpush($answer[0], $answer[1]);
Dominik Hebeler's avatar
Dominik Hebeler committed
871
                Redis::expire($answer[0], 60);
872
873
874
875
876
877
878