Searchengine.php 2.81 KB
Newer Older
1
2
3
<?php

namespace App\MetaGer;
4
use Request;
5

6
abstract class Searchengine
7
8
{

9
	protected $ch; 	# Curl Handle zum erhalten der Ergebnisse
10

11
12
13
14
15
16
17
18
19
20
21
22
23
24
	function __construct(\SimpleXMLElement $engine)
	{
		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
27
28
29
30
31
32
33
34
35
		$this->ch = curl_init($this->generateGetString());
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT , TIME); 
	}

	public abstract function loadResults();

	public function addCurlHandle ($mh)
	{
		curl_multi_add_handle($mh, $this->ch);
36
37
	}

38
39
40
41
42
43
	public function removeCurlHandle ($mh)
	{
		curl_multi_remove_handle($mh, $this->ch);
	}

	private function generateGetString()
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	{
		$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>>") )
		{
			$getString = str_replace("<<QUERY>>", $this->urlEncode(Q), $getString);
		}

		if( strpos($getString, "<<IP>>") )
		{
			$getString = str_replace("<<IP>>", $this->urlEncode(IP), $getString);
		}

		if( strpos($getString, "<<LANGUAGE>>") )
		{
			$getString = str_replace("<<LANGUAGE>>", $this->urlEncode(LANGUAGE), $getString);
		}

		if( strpos($getString, "<<CATEGORY>>") )
		{
			$getString = str_replace("<<CATEGORY>>", $this->urlEncode(CATEGORY), $getString);
		}
85
86
87
88
89

		if( strpos($getString, "<<AFFILDATA>>") )
		{
			$getString = str_replace("<<AFFILDATA>>", $this->getOvertureAffilData(), $getString);
		}
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
		return $getString;
	}

	private function urlEncode($string)
	{
		if(isset($this->inputEncoding))
		{
			return urlencode(mb_convert_encoding($string, $this->inputEncoding));
		}else
		{
			return urlencode($string);
		}
	}

	private function getOvertureAffilData()
	{
106
107
108
109
	    $affil_data = 'ip=' . IP;
	    $affil_data .= '&ua=' . $this->useragent;  
	    if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
	       $affil_data .= '&xfip=' . $_SERVER['HTTP_X_FORWARDED_FOR'];
110
	    }
111
	    $affilDataValue = $this->urlEncode($affil_data);
112
		# Wir benötigen die ServeUrl:
113
		$serveUrl = $this->urlEncode(Request::url());#
114

115
116
117
		return "&affilData=" . $affilDataValue . "&serveUrl=" . $serveUrl;
	}
}