<?php

    
/**
     * Test for curl.
     */
    
if(!function_exists("curl_init"))
        die(
"You need curl installed for this library to function.");
        
    
/**
     * Include our custom parser.
     */
    
require_once('api.xml.php');

    
/**
     * Ubiquity API Client.
     *
     */
    
class API
    
{
        
        
/**
         * Send our message to the API server.
         *
         * @param <String> $xml_data
         * @param <Int> $r_id
         * @param <String> $r_key
         * 
         * @return <String>
         */
        
function send_message($xml_data$r_id$r_key)
        {            
            
/**
             * @ Create our XML Signature.
             */
            
$xml_signature base64_encode(API::__CalcHmacSha1($xml_data$r_key));
            
            
/**
             * @ Encode data.
             */
            
$xml_data base64_encode($xml_data);
            
            
/**
             * @ Post Data
             */
            
$post_data = array(
                
'remote_id' => $r_id,
                
'xml_request' => $xml_data,
                
'xml_signature' => $xml_signature
            
);
                        
            
/**
             * @ Get curl response.
             */
            
return API::get_curl_response($post_data"http://api.ubiquityservers.com/api.php");
        }
        
        
/**
         * Parse XML.
         *
         * @param <String> $response
         * 
         * @return <String>
         */
        
function parse_xml_response($response)
        {
            
/**
             * Parse it, return it.
             */
            
$parser = new API_Parser;
            
$xml $parser->parse($response);
            return 
$xml;
        }
        
        
/**
         * @ Get our curl response and send it.
         *
         * @param <String> $post_vars
         * @param <String> $url
         * 
         * @return <String>
         */
        
function get_curl_response($post_vars$url)
        {
            
/**
             * @ Initiate and set options.
             */
            
$ch curl_init();
            
curl_setopt($chCURLOPT_URL$url);
            
curl_setopt($chCURLOPT_RETURNTRANSFER1);
            
curl_setopt($chCURLOPT_POST1);
            
curl_setopt($chCURLOPT_POSTFIELDS$post_vars);
            
            
/**
             * @ Execute and send response.
             */
            
$response curl_exec($ch);
            return(
$response);
            if (
curl_errno($ch)) {
                print 
"CURL ERROR -> "curl_error($ch);
                die();
            }
        }
        
        
/**
         * Calculate our HMACSHA1 data.
         *
         * @param <String> $data
         * @param <String> $key
         * 
         * @return <String>
         */
        
function __CalcHmacSha1($data$key)
        {    
            
/**
             * Check for errors.
             */
            
$error_function_name "__CalcHmacSha1()";
        
            
/**
             * The $data and $key variables must be populated.
             */
            
$blocksize 64;
            
$hashfunc 'sha1';
            if (
strlen($key) > $blocksize
                
$key pack('H*'$hashfunc($key));
            
$key str_pad($key$blocksizechr(0x00));
            
$ipad str_repeat(chr(0x36), $blocksize);
            
$opad str_repeat(chr(0x5c), $blocksize);
            
            
/**
             * @ Return packed data.
             */
            
$hmac pack('H*'$hashfunc(($key^$opad).pack('H*'$hashfunc(($key^$ipad).$data))));
            return 
$hmac;
        }
        
        
/**
         * Function to create the XML message to be sent to
         *
         * @param <String> $class
         * @param <String> $func
         * @param <Array> $params
         * @param <Int> $r_id
         * @param <String> $r_key
         * 
         * @return <String>
         */
        
function call($func$params)
        {
            
/**
             * Define class.
             */
            
$class 'customer';
            
$r_id REMOTE_ID;
            
$r_key REMOTE_KEY;
            
$reseller_id RESELLER_ID;
            
            
/**
             * Instantiate parser.
             */
            
require_once('api.xml.php');        
            
$parser = new API_Parser;
            
            
/**
             * XML Header.
             */
            
$xml_msg "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
            
            
/**
             * Method String
             */
            
$xml_msg .= "<" $func " xmlns=\"http://api.ubiquityservers.com/schemas/" $class "\">\r\n";
            
            
/**
             * @ Remote ID
             */
            
$xml_msg .= "<remoteid>" $r_id "</remoteid>\r\n";
            
            
/**
             * Remote Key
             */
            
$xml_msg .= "<remotekey>"$r_key ."</remotekey>\r\n";
            
            
/**
             * Reseller Id
             */
            
$xml_msg .= "<resellerid>"$reseller_id ."</resellerid>\r\n";
            
            
/**
             * @ Parameters
             */
            
$xml_msg .= "<parameters>\r\n";
            foreach (
$params as $key=>$value)
            {
                
/**
                 * @ Array
                 */
                
if(is_array($params[$key]))
                {
                    
$xml_msg .= "<" $key ">\r\n";
                    foreach(
$params[$key] AS $loop_key=>$loop_val)
                    {
                        
$xml_msg .= "<" $loop_key ">";
                        
$xml_msg .= $loop_val;
                        
$xml_msg .= "</" $loop_key ">\r\n";
                    }
                    
$xml_msg .= "</" $key ">\r\n";
                }
                
                
/**
                 * @ Non-Array
                 */
                
{            
                    
$xml_msg .= "<" $key ">";
                    
$xml_msg .= $value;
                    
$xml_msg .= "</" $key ">\r\n";
                }
            }
            
$xml_msg .= "</parameters>\r\n";
            
            
/**
             * @ End the request        
             */
            
$xml_msg .= "</" $func ">\r\n";
                                    
            
/**
             * Get the results of the request
             */
            
$response API::send_message($xml_msg$r_id$r_key);
            
$response ereg_replace("\t","",$response);

            
/**
             * API Unsupported.
             */
            
if (stristr($response"Specified API class not found or is un-supported"))
                return (
"Error - " $response);
            if (
stristr($response"Specified API function was not found or is un-supported"))
                return (
"Error - " $response);
            
            
/**
             * @ And, finally, process the info and return an array
             */
            
$xml $parser->parse($response);
            
            
/**
             * @ Make sure that the message is a response
             */
            
if ((!($xml[0]['name'] == "RESPONSE")))// || (!(isset($xml[0]['name']))) || ($xml[0]['name']==""))
                
return ("Invalid return - Response expected");
            
            
/**
             * @ Make sure we have a paramter set
             */
            
if (!($xml[0]['children'][0]['name'] == "PARAMETERS"))
                return (
"Invalid return - Paramaters expected");
                
            
/**
             * @ Check to see if we got an error
             */
            
if (stristr($xml[0]['children'][0]['children'][0]['name'], "ERROR"))
                return (
"Error - " $xml[0]['children'][0]['children'][0]['tagData']);
                
            
/**
             * Return the result.
             */
            
$result_set = array();
            if (
count($xml[0]['children'][0]['children']) > 0)
            {
                foreach (
$xml[0]['children'][0]['children'] as $key)
                {
                    
$result_set[strtolower($key['name'])] = $key['tagData'];
                }    
            }
            
            
/**
             * Return it.
             */
            
if(!isset($result_set['result']))
                return array(
'Error' => 'No result was given.');
            else 
                return 
unserialize(base64_decode($result_set['result']));
            
            return (
$result_set);
        }
        
    } 
/** Class End **/
    
?>