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

3
namespace App\Models;
4

5
abstract class Searchengine
6 7
{

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

10
	function __construct(\SimpleXMLElement $engine, $mh, $query, $time)
11 12 13 14 15 16 17 18 19 20 21 22 23
	{
		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";
		}

24
		$this->ch = curl_init($this->generateGetString($query));
25 26
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
27 28 29
		curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT , $time); 

		$this->addCurlHandle($mh);
30 31 32 33 34 35 36
	}

	public abstract function loadResults();

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

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

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

		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);
		}
86 87 88 89 90

		if( strpos($getString, "<<AFFILDATA>>") )
		{
			$getString = str_replace("<<AFFILDATA>>", $this->getOvertureAffilData(), $getString);
		}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		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()
	{
107 108 109 110
	    $affil_data = 'ip=' . IP;
	    $affil_data .= '&ua=' . $this->useragent;  
	    if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
	       $affil_data .= '&xfip=' . $_SERVER['HTTP_X_FORWARDED_FOR'];
111
	    }
112
	    $affilDataValue = $this->urlEncode($affil_data);
113
		# Wir benötigen die ServeUrl:
114
		$serveUrl = $this->urlEncode(Request::url());#
115

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