Searchengine.php 3.92 KB
Newer Older
1
2
<?php

3
namespace App\Models;
4
use App\MetaGer;
5

6
abstract class Searchengine
7
8
{

9
	protected $ch; 	# Curl Handle zum erhalten der Ergebnisse
10
	public $results = [];
11

12
	function __construct(\SimpleXMLElement $engine, $mh, MetaGer $metager)
13
14
15
16
17
18
19
20
21
22
23
24
	{
		foreach($engine->attributes() as $key => $value){
			$this->$key = $value->__toString();
		}
		# User-Agent definieren:
		if( isset($_SERVER['HTTP_USER_AGENT']))
		{
			$this->useragent = $_SERVER['HTTP_USER_AGENT'];
		}else
		{
			$this->useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";
		}
25
26
		$this->ip = $metager->getIp();
		$this->ch = curl_init($this->generateGetString($metager->getEingabe(), $metager->getUrl(), $metager->getLanguage(), $metager->getCategory()) );
27
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
28
29
30
31
32
33
34
35
36
37
38
39
40
		curl_setopt($this->ch, CURLOPT_USERAGENT, $this->useragent); // set browser/user agent
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1); // automatically follow Location: headers (ie redirects)
		curl_setopt($this->ch, CURLOPT_AUTOREFERER, 1); // auto set the referer in the event of a redirect
		curl_setopt($this->ch, CURLOPT_MAXREDIRS, 5); // make sure we dont get stuck in a loop
		curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT , $metager->getTime()); 
		curl_setopt($this->ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 
		curl_setopt($this->ch, CURLOPT_TIMEOUT, 1); // 10s timeout time for cURL connection
		if($this->port ==="443")
		{
			curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, true); // allow https verification if true
			curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2); // check common name and verify with host name
			curl_setopt($this->ch, CURLOPT_SSLVERSION,3); // verify ssl version 2 or 3
		}
41
42

		$this->addCurlHandle($mh);
43
44
45
46
	}

	public abstract function loadResults();

47
48
49
50
51
	public function getCurlInfo()
	{
		return curl_getinfo($this->ch);
	}

52
53
54
	public function addCurlHandle ($mh)
	{
		curl_multi_add_handle($mh, $this->ch);
55
56
	}

57
58
59
60
61
	public function removeCurlHandle ($mh)
	{
		curl_multi_remove_handle($mh, $this->ch);
	}

62
	private function generateGetString($query, $url, $language, $category)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	{
		$getString = "";
		# Protokoll:
		if($this->port === "443"){
			$getString .= "https://";
		}else{
			$getString .= "http://";
		}
		# Host:
		$getString .= $this->host;
		# Port:
		$getString .= ":" . $this->port;
		# Skript:
		$getString .= $this->skript;
		# FormData:
		$getString .= "?" . $this->formData;

		# Wir müssen noch einige Platzhalter in dem GET-String ersetzen:
		if( strpos($getString, "<<USERAGENT>>") ){
			$getString = str_replace("<<USERAGENT>>", $this->urlEncode($this->useragent), $getString);
		}

		if( strpos($getString, "<<QUERY>>") )
		{
87
			$getString = str_replace("<<QUERY>>", $this->urlEncode($query), $getString);
88
89
90
91
		}

		if( strpos($getString, "<<IP>>") )
		{
92
			$getString = str_replace("<<IP>>", $this->urlEncode($this->ip), $getString);
93
94
95
96
		}

		if( strpos($getString, "<<LANGUAGE>>") )
		{
97
			$getString = str_replace("<<LANGUAGE>>", $this->urlEncode($language), $getString);
98
99
100
101
		}

		if( strpos($getString, "<<CATEGORY>>") )
		{
102
			$getString = str_replace("<<CATEGORY>>", $this->urlEncode($category), $getString);
103
		}
104
105
106

		if( strpos($getString, "<<AFFILDATA>>") )
		{
107
			$getString = str_replace("<<AFFILDATA>>", $this->getOvertureAffilData($url), $getString);
108
		}
109
110
111
		return $getString;
	}

112
	protected function urlEncode($string)
113
114
115
116
117
118
119
120
121
122
	{
		if(isset($this->inputEncoding))
		{
			return urlencode(mb_convert_encoding($string, $this->inputEncoding));
		}else
		{
			return urlencode($string);
		}
	}

123
	private function getOvertureAffilData($url)
124
	{
125
	    $affil_data = 'ip=' . $this->ip;
126
127
128
	    $affil_data .= '&ua=' . $this->useragent;  
	    if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
	       $affil_data .= '&xfip=' . $_SERVER['HTTP_X_FORWARDED_FOR'];
129
	    }
130
	    $affilDataValue = $this->urlEncode($affil_data);
131
		# Wir benötigen die ServeUrl:
132
		$serveUrl = $this->urlEncode($url);
133

134
135
136
		return "&affilData=" . $affilDataValue . "&serveUrl=" . $serveUrl;
	}
}