<?php
include_once('curl.php');

$skimdomain = urldecode(@$_GET['domain']);
if($skimdomain == ''){$skimdomain = 'go.redirectingat.com';}
$skimid = urldecode(@$_GET['id']);
$getpage = urldecode(@$_GET['rss']);

$curl = new Curl();
$page = $curl->get($getpage);
if (empty($page))
	die();
$page = substr($page, strpos($page, '<'));
//$domainpage = $curl->get('http://api.skimlinks.com/merchants/domains/phps');
//$domainpage = $curl->get('http://api.everyfeed.net/merchants/phps/3e1867f5aee83045775fbe355e6a3ce1/domains');
$domainpage = $curl->get('http://api-merchants.skimlinks.com/merchants/phps/3e1867f5aee83045775fbe355e6a3ce1/domains');
$curl->close();
$domainpage = unserialize($domainpage);
$domains = $domainpage['domains'];

function foo($matches) {
	global $domains, $skimid, $skimdomain;
	$new_url = $matches[4];
	$purl = parse_url($new_url);
	if (array_key_exists('scheme', $purl) && in_array($purl['scheme'], array('http', 'https'))) {
		$hostName = $purl['host'];
		if (strstr($hostName, ".ebay.com")){$hostName = "ebay.com";}
		if (strstr($hostName, ".ebay.co.uk")){$hostName = "ebay.co.uk";}
		if (strstr($hostName, ".ebay.it")){$hostName = "ebay.it";}
		if (strstr($hostName, ".ebay.ca")){$hostName = "ebay.ca";}
		if (strstr($hostName, ".ebay.fr")){$hostName = "ebay.fr";}
		if (strstr($hostName, ".ebay.de")){$hostName = "ebay.de";}
		if (strstr($hostName, ".ebay.ie")){$hostName = "ebay.ie";}
		if (strstr($hostName, ".ebay.at")){$hostName = "ebay.at";}
		if (strstr($hostName, ".ebay.com.au")){$hostName = "ebay.com.au";}
		if (strstr($hostName, ".ebay.nl")){$hostName = "ebay.nl";}
		if (substr($hostName, 0, 4) == 'www.') {
			$hostName= substr($hostName, 4);
		}
		if(!is_null($domains)){
			if (in_array($hostName, $domains)) {
				$new_url = "http://".$skimdomain."?id=".$skimid."&amp;xs=1&amp;url=".urlencode($new_url). "&amp;sref=skimrss";
			}
		}
	}
	return $matches[1] . $matches[2] . $matches[3] . $new_url . $matches[5] . $matches[6] . $matches[7];
}

$page = preg_replace_callback(
	'/(<|&lt;)(a\s+href\s?=)(\'|"|&quot;|&#39;)(.*?)(\3)(.*?)(&gt;|>)/is',
	'foo',
	$page
);

header('Content-type: text/xml');
print $page;
