Commit 6d318cfa authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Added Parameter Filter

parent 67f81ca0
...@@ -29,6 +29,8 @@ class MetaGer ...@@ -29,6 +29,8 @@ class MetaGer
protected $phrases = []; protected $phrases = [];
protected $engines = []; protected $engines = [];
protected $results = []; protected $results = [];
protected $queryFilter = [];
protected $parameterFilter = [];
protected $ads = []; protected $ads = [];
protected $warnings = []; protected $warnings = [];
protected $errors = []; protected $errors = [];
...@@ -528,7 +530,7 @@ class MetaGer ...@@ -528,7 +530,7 @@ class MetaGer
return; return;
} }
$enabledSearchengines = []; $this->enabledSearchengines = [];
$overtureEnabled = false; $overtureEnabled = false;
# Check if selected focus is valid # Check if selected focus is valid
...@@ -546,22 +548,31 @@ class MetaGer ...@@ -546,22 +548,31 @@ class MetaGer
# Check if this engine can use eventually defined query-filter # Check if this engine can use eventually defined query-filter
$valid = true; $valid = true;
foreach ($this->queryFilter as $queryFilter => $filter) { foreach ($this->queryFilter as $queryFilter => $filter) {
if (empty($this->sumaFile->filter->$queryFilter->sumas->$suma)) { if (empty($this->sumaFile->filter->{"query-filter"}->$queryFilter->sumas->$suma)) {
$valid = false; $valid = false;
break; break;
} }
} }
# Check if this engine can use eventually defined parameter-filter
if ($valid) {
foreach ($this->parameterFilter as $filterName => $filter) {
if (empty($filter->sumas->$suma)) {
$valid = false;
break;
}
}
}
# If it can we add it # If it can we add it
if ($valid) { if ($valid) {
$enabledSearchengines[$suma] = $this->sumaFile->sumas->{$suma}; $this->enabledSearchengines[$suma] = $this->sumaFile->sumas->{$suma};
} }
} }
if (sizeof($enabledSearchengines) === 0) { if (sizeof($this->enabledSearchengines) === 0) {
$filter = ""; $filter = "";
foreach ($this->queryFilter as $queryFilter => $filterPhrase) { foreach ($this->queryFilter as $queryFilter => $filterPhrase) {
$filter .= trans($this->sumaFile->filter->{$queryFilter}->name) . ","; $filter .= trans($this->sumaFile->filter->{"query-filter"}->{$queryFilter}->name) . ",";
} }
$filter = rtrim($filter, ","); $filter = rtrim($filter, ",");
$error = trans('metaGer.engines.noSpecialSearch', ['fokus' => trans($this->sumaFile->foki->{$this->fokus}->{"display-name"}), $error = trans('metaGer.engines.noSpecialSearch', ['fokus' => trans($this->sumaFile->foki->{$this->fokus}->{"display-name"}),
...@@ -570,7 +581,6 @@ class MetaGer ...@@ -570,7 +581,6 @@ class MetaGer
} }
$engines = []; $engines = [];
$typeslist = []; $typeslist = [];
$counter = 0; $counter = 0;
...@@ -581,8 +591,9 @@ class MetaGer ...@@ -581,8 +591,9 @@ class MetaGer
$engine->setResultHash($this->getHashCode()); $engine->setResultHash($this->getHashCode());
} }
} else { } else {
$engines = $this->actuallyCreateSearchEngines($enabledSearchengines); $engines = $this->actuallyCreateSearchEngines($this->enabledSearchengines);
} }
# Wir starten alle Suchen # Wir starten alle Suchen
foreach ($engines as $engine) { foreach ($engines as $engine) {
$engine->startSearch($this); $engine->startSearch($this);
...@@ -661,6 +672,24 @@ class MetaGer ...@@ -661,6 +672,24 @@ class MetaGer
return $engines; return $engines;
} }
public function getAvailableParameterFilter()
{
$parameterFilter = $this->sumaFile->filter->{"parameter-filter"};
$availableFilter = [];
foreach ($parameterFilter as $filterName => $filter) {
# Check if any of the enabled search engines provide this filter
foreach ($this->enabledSearchengines as $engineName => $engine) {
if (!empty($filter->sumas->$engineName)) {
$availableFilter[$filterName] = $filter;
}
}
}
return $availableFilter;
}
public function isBildersuche() public function isBildersuche()
{ {
return $this->fokus === "bilder"; return $this->fokus === "bilder";
...@@ -843,13 +872,6 @@ class MetaGer ...@@ -843,13 +872,6 @@ class MetaGer
} }
} }
# Nicht fertige Engines verwefen
foreach ($engines as $engine) {
if (!$engine->loaded) {
$engine->shutdown();
}
}
$this->engines = $engines; $this->engines = $engines;
} }
...@@ -1006,10 +1028,7 @@ class MetaGer ...@@ -1006,10 +1028,7 @@ class MetaGer
$this->searchCheckPhrase(); $this->searchCheckPhrase();
# Check for query-filter (i.e. Sitesearch, etc.): # Check for query-filter (i.e. Sitesearch, etc.):
foreach ($this->sumaFile->filter as $filterName => $filter) { foreach ($this->sumaFile->filter->{"query-filter"} as $filterName => $filter) {
if ($filter->type !== "query-filter") {
continue;
}
if (!empty($filter->{"optional-parameter"}) && $request->filled($filter->{"optional-parameter"})) { if (!empty($filter->{"optional-parameter"}) && $request->filled($filter->{"optional-parameter"})) {
$this->queryFilter[$filterName] = $request->input($filter->{"optional-parameter"}); $this->queryFilter[$filterName] = $request->input($filter->{"optional-parameter"});
} else if (preg_match_all("/" . $filter->regex . "/si", $this->q, $matches) > 0) { } else if (preg_match_all("/" . $filter->regex . "/si", $this->q, $matches) > 0) {
...@@ -1027,6 +1046,19 @@ class MetaGer ...@@ -1027,6 +1046,19 @@ class MetaGer
} }
} }
# Check for parameter-filter (i.e. SafeSearch)
$this->parameterFilter = [];
$usedParameters = [];
foreach ($this->sumaFile->filter->{"parameter-filter"} as $filterName => $filter) {
if (!empty($usedParameters[$filter->{"get-parameter"}])) {
die("Der Get-Parameter \"" . $filter->{"get-parameter"} . "\" wird mehrfach verwendet!");
} else {
$usedParameters[$filter->{"get-parameter"}] = true;
}
if ($request->filled($filter->{"get-parameter"})) {
$this->parameterFilter[$filterName] = $filter;
}
}
$this->searchCheckHostBlacklist($request); $this->searchCheckHostBlacklist($request);
$this->searchCheckDomainBlacklist($request); $this->searchCheckDomainBlacklist($request);
$this->searchCheckUrlBlacklist(); $this->searchCheckUrlBlacklist();
...@@ -1336,7 +1368,12 @@ class MetaGer ...@@ -1336,7 +1368,12 @@ class MetaGer
public function generateSearchLink($fokus, $results = true) public function generateSearchLink($fokus, $results = true)
{ {
$requestData = $this->request->except(['page', 'next']); $except = ['page', 'next'];
# Remove every Filter
foreach ($this->sumaFile->filter->{"parameter-filter"} as $filterName => $filter) {
$except[] = $filter->{"get-parameter"};
}
$requestData = $this->request->except($except);
$requestData['focus'] = $fokus; $requestData['focus'] = $fokus;
$requestData['out'] = ""; $requestData['out'] = "";
...@@ -1509,6 +1546,11 @@ class MetaGer ...@@ -1509,6 +1546,11 @@ class MetaGer
return $this->queryFilter; return $this->queryFilter;
} }
public function getParameterFilter()
{
return $this->parameterFilter;
}
public function getTime() public function getTime()
{ {
return $this->time; return $this->time;
......
...@@ -7,6 +7,7 @@ use App\MetaGer; ...@@ -7,6 +7,7 @@ use App\MetaGer;
use Cache; use Cache;
use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Request;
abstract class Searchengine abstract class Searchengine
{ {
...@@ -46,9 +47,11 @@ abstract class Searchengine ...@@ -46,9 +47,11 @@ abstract class Searchengine
$this->name = $name; $this->name = $name;
# Cache Standarddauer 60 # Cache Standarddauer 60
if (!isset($this->cacheDuration)) { $this->cacheDuration = 60;
$this->cacheDuration = 60; if (!empty($engine->{"cache-duration"}) && $engine->{"cache-duration"} >= 0) {
$this->cacheDuration = $engine->{"cache-duration"};
} }
$this->useragent = $metager->getUserAgent(); $this->useragent = $metager->getUserAgent();
$this->ip = $metager->getIp(); $this->ip = $metager->getIp();
$this->startTime = microtime(); $this->startTime = microtime();
...@@ -64,12 +67,23 @@ abstract class Searchengine ...@@ -64,12 +67,23 @@ abstract class Searchengine
$q = $metager->getQ(); $q = $metager->getQ();
$filters = $metager->getSumaFile()->filter; $filters = $metager->getSumaFile()->filter;
foreach ($metager->getQueryFilter() as $queryFilter => $filter) { foreach ($metager->getQueryFilter() as $queryFilter => $filter) {
$filterOptions = $filters->$queryFilter; $filterOptions = $filters->{"query-filter"}->$queryFilter;
$filterOptionsEngine = $filterOptions->sumas->{$this->name}; $filterOptionsEngine = $filterOptions->sumas->{$this->name};
$query = $filterOptionsEngine->prefix . $filter . $filterOptionsEngine->suffix; $query = $filterOptionsEngine->prefix . $filter . $filterOptionsEngine->suffix;
$q = $query . " " . $q; $q = $query . " " . $q;
} }
# Parse enabled Parameter-Filter
foreach ($metager->getParameterFilter() as $filterName => $filter) {
$inputParameter = Request::input($filter->{"get-parameter"}, "");
if (empty($inputParameter) || empty($filter->sumas->{$name}->values->{$inputParameter})) {
continue;
}
$engineParameterKey = $filter->sumas->{$name}->{"get-parameter"};
$engineParameterValue = $filter->sumas->{$name}->values->{$inputParameter};
$this->engine->{"get-parameter"}->{$engineParameterKey} = $engineParameterValue;
}
$this->getString = $this->generateGetString($q); $this->getString = $this->generateGetString($q);
$this->hash = md5($this->engine->host . $this->getString . $this->engine->port . $this->name); $this->hash = md5($this->engine->host . $this->getString . $this->engine->port . $this->name);
$this->resultHash = $metager->getHashCode(); $this->resultHash = $metager->getHashCode();
......
...@@ -9,9 +9,9 @@ class Ebay extends Searchengine ...@@ -9,9 +9,9 @@ class Ebay extends Searchengine
{ {
public $results = []; public $results = [];
public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager) public function __construct($name, \stdClass $engine, \App\MetaGer $metager)
{ {
parent::__construct($engine, $metager); parent::__construct($name, $engine, $metager);
} }
public function loadResults($result) public function loadResults($result)
...@@ -24,15 +24,15 @@ class Ebay extends Searchengine ...@@ -24,15 +24,15 @@ class Ebay extends Searchengine
} }
$results = $content->{"searchResult"}; $results = $content->{"searchResult"};
$count = 0; $count = 0;
foreach ($results->{"item"} as $result) { foreach ($results->{"item"} as $result) {
$title = $result->{"title"}->__toString(); $title = $result->{"title"}->__toString();
$link = $result->{"viewItemURL"}->__toString(); $link = $result->{"viewItemURL"}->__toString();
$anzeigeLink = $link; $anzeigeLink = $link;
$time = $result->{"listingInfo"}->{"endTime"}->__toString(); $time = $result->{"listingInfo"}->{"endTime"}->__toString();
$time = date(DATE_RFC2822, strtotime($time)); $time = date(DATE_RFC2822, strtotime($time));
$price = intval($result->{"sellingStatus"}->{"convertedCurrentPrice"}->__toString()) * 100; $price = intval($result->{"sellingStatus"}->{"convertedCurrentPrice"}->__toString()) * 100;
$descr = "Preis: " . $result->{"sellingStatus"}->{"convertedCurrentPrice"}->__toString() . " €"; $descr = "Preis: " . $result->{"sellingStatus"}->{"convertedCurrentPrice"}->__toString() . " €";
$descr .= ", Versandkosten: " . $result->{"shippingInfo"}->{"shippingServiceCost"}->__toString() . " €"; $descr .= ", Versandkosten: " . $result->{"shippingInfo"}->{"shippingServiceCost"}->__toString() . " €";
if (isset($result->{"listingInfo"}->{"listingType"})) { if (isset($result->{"listingInfo"}->{"listingType"})) {
$descr .= ", Auktionsart: " . $result->{"listingInfo"}->{"listingType"}->__toString(); $descr .= ", Auktionsart: " . $result->{"listingInfo"}->{"listingType"}->__toString();
...@@ -51,11 +51,11 @@ class Ebay extends Searchengine ...@@ -51,11 +51,11 @@ class Ebay extends Searchengine
$link, $link,
$anzeigeLink, $anzeigeLink,
$descr, $descr,
$this->displayName,$this->homepage, $this->engine->{"display-name"}, $this->engine->homepage,
$this->counter, $this->counter,
['partnershop' => false, ['partnershop' => false,
'price' => $price, 'price' => $price,
'image' => $image] 'image' => $image]
); );
$count++; $count++;
} }
......
...@@ -6,11 +6,11 @@ use App\Models\Searchengine; ...@@ -6,11 +6,11 @@ use App\Models\Searchengine;
class Minisucher extends Searchengine class Minisucher extends Searchengine
{ {
public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager) public function __construct($name, \stdClass $engine, \App\MetaGer $metager)
{ {
parent::__construct($engine, $metager); parent::__construct($name, $engine, $metager);
# Für die Newssuche stellen wir die Minisucher auf eine Sortierung nach Datum um. # Für die Newssuche stellen wir die Minisucher auf eine Sortierung nach Datum um.
if($metager->getFokus() === "nachrichten"){ if ($metager->getFokus() === "nachrichten") {
$this->getString .= "sort=" . $this->urlencode("documentDate desc"); $this->getString .= "sort=" . $this->urlencode("documentDate desc");
} }
} }
...@@ -28,25 +28,26 @@ class Minisucher extends Searchengine ...@@ -28,25 +28,26 @@ class Minisucher extends Searchengine
$results = $content->xpath('//response/result/doc'); $results = $content->xpath('//response/result/doc');
$string = ""; $string = "";
$counter = 0; $counter = 0;
$providerCounter = []; $providerCounter = [];
foreach ($results as $result) { foreach ($results as $result) {
try { try {
$counter++; $counter++;
$result = simplexml_load_string($result->saveXML()); $result = simplexml_load_string($result->saveXML());
$title = $result->xpath('//doc/arr[@name="title"]/str')[0]->__toString(); $title = $result->xpath('//doc/arr[@name="title"]/str')[0]->__toString();
$link = $result->xpath('//doc/str[@name="url"]')[0]->__toString(); $link = $result->xpath('//doc/str[@name="url"]')[0]->__toString();
$anzeigeLink = $link; $anzeigeLink = $link;
$descr = ""; $descr = "";
$descriptions = $content->xpath("//response/lst[@name='highlighting']/lst[@name='$link']/arr[@name='content']/str"); $descriptions = $content->xpath("//response/lst[@name='highlighting']/lst[@name='$link']/arr[@name='content']/str");
foreach ($descriptions as $description) { foreach ($descriptions as $description) {
$descr .= $description->__toString(); $descr .= $description->__toString();
} }
$descr = strip_tags($descr);
$descr = strip_tags($descr);
$dateString = $result->xpath('//doc/date[@name="documentDate"]')[0]->__toString(); $dateString = $result->xpath('//doc/date[@name="documentDate"]')[0]->__toString();
...@@ -56,18 +57,10 @@ class Minisucher extends Searchengine ...@@ -56,18 +57,10 @@ class Minisucher extends Searchengine
$additionalInformation = ['date' => $dateVal]; $additionalInformation = ['date' => $dateVal];
$minism = simplexml_load_string($this->engine)["subcollections"]; $minism = $this->engine->{"display-name"};
$gefVon = "Minisucher: $minism";
$subcollection = $result->xpath('//doc/str[@name="subcollection"]')[0]->__toString(); $subcollection = $result->xpath('//doc/str[@name="subcollection"]')[0]->__toString();
if(!$subcollection) {
$gefVon = "Minisucher: $minism";
} else {
$minism = array_map('strtolower', explode(', ', $minism));
$subcollection = array_map('strtolower', explode(' ', $subcollection));
$result = implode(', ', array_intersect($subcollection, $minism));
$gefVon = "Minisucher: $result";
}
$this->results[] = new \App\Models\Result( $this->results[] = new \App\Models\Result(
$this->engine, $this->engine,
$title, $title,
...@@ -78,6 +71,7 @@ class Minisucher extends Searchengine ...@@ -78,6 +71,7 @@ class Minisucher extends Searchengine
$counter, $counter,
$additionalInformation $additionalInformation
); );
} catch (\ErrorException $e) { } catch (\ErrorException $e) {
continue; continue;
} }
......
...@@ -9,9 +9,9 @@ class Mnogosearch extends Searchengine ...@@ -9,9 +9,9 @@ class Mnogosearch extends Searchengine
{ {
public $results = []; public $results = [];
public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager) public function __construct($name, \stdClass $engine, \App\MetaGer $metager)
{ {
parent::__construct($engine, $metager); parent::__construct($name, $engine, $metager);
} }
public function loadResults($result) public function loadResults($result)
...@@ -28,9 +28,9 @@ class Mnogosearch extends Searchengine ...@@ -28,9 +28,9 @@ class Mnogosearch extends Searchengine
$title = $node->filter('table > tr > td ')->eq(1)->filter('td > div')->text(); $title = $node->filter('table > tr > td ')->eq(1)->filter('td > div')->text();
$title = preg_replace("/\s+/si", " ", $title); $title = preg_replace("/\s+/si", " ", $title);
$link = $node->filter('table > tr > td ')->eq(1)->filter('td > div > a')->attr('href'); $link = $node->filter('table > tr > td ')->eq(1)->filter('td > div > a')->attr('href');
$anzeigeLink = $link; $anzeigeLink = $link;
$descr = $node->filter('table > tr > td ')->eq(1)->filter('td > div')->eq(1)->text(); $descr = $node->filter('table > tr > td ')->eq(1)->filter('td > div')->eq(1)->text();
$this->counter++; $this->counter++;
$this->results[] = new \App\Models\Result( $this->results[] = new \App\Models\Result(
...@@ -39,7 +39,7 @@ class Mnogosearch extends Searchengine ...@@ -39,7 +39,7 @@ class Mnogosearch extends Searchengine
$link, $link,
$anzeigeLink, $anzeigeLink,
$descr, $descr,
$this->displayName,$this->homepage, $this->engine->{"display-name"}, $this->engine->homepage,
$this->counter $this->counter
); );
}); });
......
...@@ -8,9 +8,9 @@ class Nebel extends Searchengine ...@@ -8,9 +8,9 @@ class Nebel extends Searchengine
{ {
public $results = []; public $results = [];
public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager) public function __construct($name, \stdClass $engine, \App\MetaGer $metager)
{ {
parent::__construct($engine, $metager); parent::__construct($name, $engine, $metager);
} }
public function loadResults($result) public function loadResults($result)
...@@ -22,10 +22,10 @@ class Nebel extends Searchengine ...@@ -22,10 +22,10 @@ class Nebel extends Searchengine
continue; continue;
} }
$title = $res[1]; $title = $res[1];
$link = $res[0]; $link = $res[0];
$anzeigeLink = $link; $anzeigeLink = $link;
$descr = $res[2]; $descr = $res[2];
$this->counter++; $this->counter++;
$this->results[] = new \App\Models\Result( $this->results[] = new \App\Models\Result(
...@@ -34,7 +34,7 @@ class Nebel extends Searchengine ...@@ -34,7 +34,7 @@ class Nebel extends Searchengine
$link, $link,
$anzeigeLink, $anzeigeLink,
$descr, $descr,
$this->displayName,$this->homepage, $this->engine->{"display-name"}, $this->engine->homepage,
$this->counter $this->counter
); );
} }
......
...@@ -8,9 +8,9 @@ class Witch extends Searchengine ...@@ -8,9 +8,9 @@ class Witch extends Searchengine
{ {
public $results = []; public $results = [];
public function __construct(\SimpleXMLElement $engine, \App\MetaGer $metager) public function __construct($name, \stdClass $engine, \App\MetaGer $metager)
{ {
parent::__construct($engine, $metager); parent::__construct($name, $engine, $metager);
} }
public function loadResults($result) public function loadResults($result)
...@@ -25,10 +25,10 @@ class Witch extends Searchengine ...@@ -25,10 +25,10 @@ class Witch extends Searchengine
if (sizeof($res) !== 4 || $res[3] === "'Kein Ergebnis'") { if (sizeof($res) !== 4 || $res[3] === "'Kein Ergebnis'") {
continue; continue;
} }
$title = trim($res[0], "'"); $title = trim($res[0], "'");
$link = trim($res[2], "'"); $link = trim($res[2], "'");
$anzeigeLink = $link; $anzeigeLink = $link;
$descr = trim($res[1], "'"); $descr = trim($res[1], "'");
$this->counter++; $this->counter++;
$this->results[] = new \App\Models\Result( $this->results[] = new \App\Models\Result(
...@@ -37,7 +37,7 @@ class Witch extends Searchengine ...@@ -37,7 +37,7 @@ class Witch extends Searchengine
$link, $link,