<?php

// pwp.class.php
// -------------------------------------------------------------------------------------------------
// Connection object for access to PWP servers
// supporting full failover
//
// Project    > dmClubNewYear08:C37
// Copyright  > (C) Digital Mail Limited (2005-2008)
// Author     > RAK for DML
// -------------------------------------------------------------------------------------------------

require_once('http.class.php');

class PWP extends HTTP
{

	var $protocol_version;		// The version of the protocol to use
	var $client_app_version = 'unspecified'; // The app using this class can set this (it is added to all requests)
	
	// ---------------------------------------------------------------------------------------------

	function PWP($server, $protocol_version, $method = 'POST', $timeout = 15)
	{
		// CONSTRUCTOR
		
		parent::HTTP($server, $method, $timeout);
		$this->comobject 		= 'PWP';
		$this->protocol_version = $protocol_version;
	}
	
	// ---------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------

	function request()
	{
		// Execute a request to a PWP protocol server
		//
		// This function expects a response consisting of name="urlencoded value" pairs,
		// separated by white space

		$this->_R['_Protocol'] 		= $this->protocol_version;
		$this->_R['ClientAppVer'] 	= $this->client_app_version;
		$this->_R['ClientURL'] 		= 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		parent::request();
		if ($this->result->status) 
		{
			if (strpos($this->result->response, 'Cmd=') === FALSE)
			{
				$this->result->status = FALSE;
				$this->result->error  = 'Malformed PWP Response';
			}
			else
			{
				$this->result->response = $this->decode_pwp($this->result->response);
			}
		}
		return $this->result;
	}
	
	// ---------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------

	function decode_pwp($pwp_string)
	{
		// Split PWP output string and assign values to $response array
		// eg if we receive Number="45", $response['Number'] will be set to 45
		
		if (strlen($pwp_string) == 0) return array();
	
		$this->debug->msg('<b>DECODING PWP</b>');
		$response = array();
		$pairs = preg_split('/\s/m', $pwp_string);
		for ($i=0; $i<count($pairs); $i++)
		{
			preg_match('/(.+)="(.*)"/', $pairs[$i], $match);
			if ($match)
			{
				$match[1] = urldecode($match[1]);
				$match[2] = urldecode($match[2]);
	
				if (strpos($match[1], '.') > 0) // Make array from structured data
				{ 
					$indeces = explode('.', $match[1]);
					$loop = count($indeces);
					for ($j=0; $j<$loop; $j++)
					{
						if ($indeces[$j]+1 > 1)
						{
							if (is_numeric($indeces[$j])) $indeces[$j] = (int) $indeces[$j]; // Convert to integer
						}
					}				
					switch(count($indeces))
					{
						case 2:
							$response[ $indeces[0] ][ $indeces[1] ] = $match[2]; 
							$$this->debug->msg("\$response[$indeces[0]][$indeces[1]] = " . $match[2]);
							break;
						case 3:
							$response[ $indeces[0] ][ $indeces[1] ][ $indeces[2] ] = $match[2];
							$this->debug->msg("\$response[$indeces[0]][$indeces[1]][$indeces[2]] = " .  $match[2]);
							break;
						case 4:
							$response[ $indeces[0] ][ $indeces[1] ][ $indeces[2] ][ $indeces[3] ] = $match[2];
							$this->debug->msg("\$response[$indeces[0]][$indeces[1]][$indeces[2]][$indeces[3]] = " . $match[2]);
							break;
						case 5:
							$response[ $indeces[0] ][ $indeces[1] ][ $indeces[2] ][ $indeces[3] ][ $indeces[4] ] = $match[2];
							$this->debug->msg("\$response[$indeces[0]][$indeces[1]][$indeces[2]][$indeces[3]][$indeces[4]] = " .  $match[2]);
							break;
						case 6:
							$response[ $indeces[0] ][ $indeces[1] ][ $indeces[2] ][ $indeces[3] ][ $indeces[4] ][ $indeces[5] ] = $match[2];
							$this->debug->msg("\$response[$indeces[0]][$indeces[1]][$indeces[2]][$indeces[3]][$indeces[4]][$indeces[5]] = " .  $match[2]);
							break;
						case 7:
							$response[ $indeces[0] ][ $indeces[1] ][ $indeces[2] ][ $indeces[3] ][ $indeces[4] ][ $indeces[5] ][ $indeces[6] ] = $match[2];
							$this->debug->msg("\$response[$indeces[0]][$indeces[1]][$indeces[2]][$indeces[3]][$indeces[4]][$indeces[5]][$indeces[6]] = " .  $match[2]);
							break;
					}

				}
				else // Just create a simple variable
				{ 
					$response[$match[1]] = $match[2];
					$this->debug->msg("\$response[$match[1]] = " . $match[2]);
				}
			
			}
		}
		$this->debug->msg('');
		return $response;
	}
	
	// ---------------------------------------------------------------------------------------------

}

?>