<?php

class prodmCache extends Memcache {
	static private $instance = null;
	static private $port = 11211;
	static function mGet() {
		if(self::$instance == null) {
			self::$instance = new Memcache;
			self::$instance->addServer('192.168.30.51', self::$port);
		}
		return self::$instance;
	}
}

function curl_get_contents($CurlData)
{ 
	##### FETCH INPUT #####
	if (is_array($CurlData))
		$URL = $CurlData['url'];
	else
		$URL = $CurlData;	
	
	if ($CurlData['timeout'])
		$Timeout = $CurlData['timeout'];
	else
		$Timeout = 5;
			
	###### INIT ############ 
	$ch = curl_init($URL);
	###### CURL & OPTION #####
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_TIMEOUT, $Timeout);
	if($CurlData['login'])
		curl_setopt($ch, CURLOPT_USERPWD, $CurlData['login']);
	if($CurlData['ua'])
		curl_setopt($ch, CURLOPT_USERAGENT, $CurlData['ua']);
	if($CurlData['Authorization'])
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: '.$CurlData['Authorization'])); 
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

function parse_shopzilla($data)
{
	$apiKey = "e35ea94a50d2f721b9036f815ffcb42e";
	$publisherId = "35211";

	$searchTerm = $data['term'];
	if(isset($data['biddedOnly']))
		$biddedOnly = $data['biddedOnly'];
	else
		$biddedOnly = "true";

	if(isset($data['placementId']))
		$placementId = $data['placementId'];
	else
		$placementId = 1;

	$mkey = md5("shopzilla_feed_{$searchTerm}");
	$expire = 60*60*4;
	if(!$result = prodmCache::mGet()->get($mkey))
	{	
		$url = "http://catalog.bizrate.com/services/catalog/v1/us/product?apiKey={$apiKey}&placementId={$placementId}&publisherId={$publisherId}&keyword={$searchTerm}&biddedOnly={$biddedOnly}";
		$feedContents = curl_get_contents($url);
		
		$SponsorFeed = new DomDocument();
		$SponsorFeed->loadXML($feedContents);
		
		$Listing = $SponsorFeed->getElementsByTagName("Offer"); 
		for($i = 0; $Listing->item($i) ; $i++) 
		{	 
			if($Listing->item($i))
			{	
				$title = $Listing->item($i)->getElementsByTagName('title')->item(0)->nodeValue;
				$desc = $Listing->item($i)->getElementsByTagName('description')->item(0)->nodeValue;
				$manu = $Listing->item($i)->getElementsByTagName('manufacturer')->item(0)->nodeValue;
				$redirectUrl = $Listing->item($i)->getElementsByTagName('url')->item(0)->nodeValue;
				$image = $Listing->item($i)->getElementsByTagName('Image')->item(1)->nodeValue;
				$price = $Listing->item($i)->getElementsByTagName('price')->item(0)->nodeValue;
				$merchantLogo = $Listing->item($i)->getElementsByTagName('merchantLogoUrl')->item(0)->nodeValue;

				$result[$i] = array("title"=>$title, "description"=>$desc, "manufacturer"=>$manu, "redirectUrl"=>$redirectUrl, "image"=>$image, "price"=>$price, "merchantLogo"=>$merchantLogo);
			}
		}

		if(is_array($result) && count($result) > 0)
			prodmCache::mGet()->set($mkey, $result, 0, $expire);
		else
			prodmCache::mGet()->set($mkey, "NONE", 0, $expire);
	}
	
	if(is_array($result) && count($result) > 0)
		return $result;
	else
		return false;
}

?>