<?php

/**
@version  "1.1"
@date "17/07/2003"
@author "Peter Dunham"
@param $keywords string - keyword or phrase to search for
@param $catid int - id of category to search in
@param $mid int - id of merchant whose products you wish to search
@param $affiliateID int - id of affiliate
@param $password string - password of affiliate
@param $startNum int - number of first record in recordset to return
@param $limit int - number of results to return. Max 200.
@param $action - either awSearchProducts or awBrowseProducts - note that these are case sensitive

*/
require_once('awsoapclientconfig.inc.php'); # Must be in the same directory as the program requiring awsoapclient.php
require_once($clientDir.'nusoap.php');
require_once($clientDir.'awsoapclientfunctions.inc.php');

$pathToScript = "/".preg_replace("/http[s]?:\/\/[^\/]*\//","",$url);

if($sefriendly==1) 
{
	$PATH_INFO = $HTTP_SERVER_VARS['HTTP_HOST'].$PHP_SELF;
	$HTTP_TEMP_PATH_VARS = explode("/",$PATH_INFO); 

	array_shift($HTTP_TEMP_PATH_VARS); 
	reset($HTTP_TEMP_PATH_VARS); 

	for($ii=0; $ii<sizeof($HTTP_TEMP_PATH_VARS); $ii++) 
	{
		if($querystring == true) 
		{
			$$HTTP_TEMP_PATH_VARS[$ii] = urldecode($HTTP_TEMP_PATH_VARS[$ii+1]);
		}
		if($HTTP_TEMP_PATH_VARS[$ii]!="" and preg_match("/$HTTP_TEMP_PATH_VARS[$ii]$/",$HTTP_SERVER_VARS['SCRIPT_NAME'])) 
		{
			$querystring = true;
		}
	}
}


function awOutput($affiliateID,$password,$mid,$catid,$keywords,$template,$action,$minprice='',$maxprice='',$orderby='') 
{
	global $soapclient,$startNum,$outputType,$keywords,$limit,$hardlimit,$pathToScript,$defaultsearchorderby,$defaultbrowseorderby;
	$soapServer = 'http://products.awin1.com/awsoapproductserver.php';
	if(preg_match("/^[0-9]/",$password)) $password = "^^$$".$password;
	if(!isset($startNum)) $startNum=0;
	if(!isset($limit))$limit=$hardlimit;
	if(!isset($soapclient)) 
	{
		if($action == "awSearchProducts") 
		{
			if($orderby =='')$orderby = $defaultsearchorderby;
			$parameters = array('aid'=>$affiliateID,
							'pw'=>$password,
							'startNum'=>$startNum,
							'limit'=>$limit,
							'target_page'=>$pathToScript,
							'mid'=>$mid,
							'catid'=>$catid,
							'keywords'=>$keywords,
							'minprice'=>$minprice,
							'maxprice'=>$maxprice,
							'orderby' =>$orderby);
			$soapclient = new soapclient($soapServer);
			$soapclient->call('awSearchProducts',$parameters);			
		}
		elseif($action == "awBrowseProducts" or !isset($action)) 
		{
			if($orderby =='')$orderby = $defaultbrowseorderby;
			
			$parameters = array('aid'=>$affiliateID,
						'pw'=>$password,
						'startNum'=>$startNum,
						'limit'=>$limit,
						'target_page'=>$pathToScript,
						'catid'=>$catid,
						'orderby' =>$orderby);
			$soapclient = new soapclient($soapServer);
			$soapclient->call('awBrowseProducts',$parameters);				
		}
	}
	if($soapclient->return['faultstring']) 
	{
		echo($soapclient->return['faultstring']);
		exit;
	}
	$fcontents = readOutputFile($template);
	$fcontents = parseTemplate($soapclient,$fcontents);
	outputContents($fcontents,$outputType);	
}

if($action == "image") 
{
	$imageArray = explode("image=", $HTTP_SERVER_VARS['QUERY_STRING']);
	$imagep = explode("&",$imageArray[1]);
	$image = $imagep[0];
	Header( "Content-type:  image/gif");  
	if($image == "") 
	{
		@readfile('http://products.affiliatewindow.com/images/noimage.gif');
		exit;
	}
	$image = "http://products.affiliatewindow.com/".$image;
	if(!@readfile($image))@readfile('http://products.affiliatewindow.com/images/noimage.gif');
}

if($outputType == "ssi" or $outputType == "javascript") 
{
	if($action == "awBrowseProducts") awOutput($affiliateID,$password,$mid,$catid,$keywords,$browsetemplate,$action);
	if($action == "awSearchProducts") awOutput($affiliateID,$password,$mid,$catid,$keywords,$searchresultstemplate,$action);
}
else 
{
	$urlArray = parse_url($url);
	$clientLocation = preg_replace("/[^\/]*$/","",$urlArray['path'])."awsoapclient.php";
}

function awtoplevelcategories($template='') 
{
	global $action,$affiliateID,$password,$mid,$catid,$keywords,$action,$maincatstemplate,$minprice,$maxprice,$orderby;
	if($template == '')$template = $maincatstemplate;
	awOutput($affiliateID,$password,$mid,$catid,$keywords,$template,$action,$minprice,$maxprice,$orderby);
}


function awsearchform($template='') 
{
	global $searchformtemplate,$action,$mid,$affiliateID,$password,$catid,$minprice,$maxprice,$orderby;
	if($template == '')$template = $searchformtemplate;
	awOutput($affiliateID,$password,$mid,$catid,$keywords,$template,$action,$minprice,$maxprice,$orderby);
}

function awshowproducts($template='') 
{
	global $browsetemplate,$searchresultstemplate,$action,$mid,$affiliateID,$password,$catid,$minprice,$maxprice,$orderby;
	if($template == '')
	{
		if($action == "awBrowseProducts" or $action=='') $template = $browsetemplate;
		else $template = $searchresultstemplate;
	}
	awOutput($affiliateID,$password,$mid,$catid,$keywords,$template,$action,$minprice,$maxprice,$orderby);
}
?>