File: /var/www/ilya/data/www/kamforum.ru/ips_kernel/class_api_server.php
<?php
/*
+---------------------------------------------------------------------------
| Invision Power KERNEL
| ========================================
| by Matthew Mecham
| (c) 2006 Invision Power Services
| http://www.invisionpower.com
| ========================================
+---------------------------------------------------------------------------
| INVISION POWER DYNAMIC IS NOT FREE SOFTWARE!
| http://www.invisionpower.com/dynamic/
+---------------------------------------------------------------------------
|
| > API SERVER: Send and receive API data
| > Script written by Matt Mecham
| > Date started: Friday 6th January 2006 (12:24)
|
+---------------------------------------------------------------------------
*/
/**
* Class API SERVER
*
* Class to send and receive API data
* <code>
* # APPLICATION: SEND AN API REQUEST AND PARSE DATA
* $api_server->api_send_request( 'http://www.domain.com/xmlrpc.php', 'get_members', array( 'name' => 'matt', 'email' => 'matt@email.com' ) );
* # APPLICATION: PICK UP REPLY AND PARSE
* print_r( $api_server->params );
*
* # SERVER: PARSE DATA, MAKE DATA AND RETURN
* $api_server->api_decode_request( $_SERVER['RAW_HTTP_POST_DATA'] );
* print $api_server->method_name;
* print_r( $api_server->params );
* # SERVER: SEND DATA BACK
* # Is complex array, so we choose to encode and send with the , 1 flag
* $api_server->api_send_reply( array( 'matt' => array( 'email' => 'matt@email.com', 'joined' => '01-01-2005' ) ), 1 );
* </code>
*
* @package IPS_KERNEL
* @author Matt Mecham
* @version 2.1
*/
if ( ! defined( 'IPS_CLASSES_PATH' ) )
{
/**
* Define classes path
*/
define( 'IPS_CLASSES_PATH', dirname(__FILE__) );
}
/**
* Class API Server
*
* Easy method of getting and sending data
*
* @package IPS_KERNEL
* @author Matt Mecham
* @version 2.1
*/
class class_api_server
{
/**
* XML-RPC class
* @var object
*/
var $xmlrpc;
/**
* XML-RPC serialized 64 key
* @var string
*/
var $serialized_key = '__serialized64__';
/**
* Server function object
* @var object
*/
var $xml_server;
/*-------------------------------------------------------------------------*/
// Constructor
/*-------------------------------------------------------------------------*/
function class_api_server()
{
if ( ! is_object( $this->xmlrpc ) )
{
require_once( IPS_CLASSES_PATH . '/class_xml_rpc.php' );
$this->xmlrpc = new class_xml_rpc();
}
}
/*-------------------------------------------------------------------------*/
// MIMIC SOAP SERVER: Add object map
/*-------------------------------------------------------------------------*/
/**
* Add object map to this class
*
* @param object Server class object
* @param string Document type
* @return boolean
*/
function add_object_map( $server_class, $doc_type='UTF-8' )
{
$this->xmlrpc->doc_type = $doc_type;
if ( is_object( $server_class ) )
{
$this->xml_server =& $server_class;
return TRUE;
}
else
{
return FALSE;
}
}
/*-------------------------------------------------------------------------*/
// MIMIC SOAP SERVER: get_xmlRPC
/*-------------------------------------------------------------------------*/
/**
* Add object map to this class
*
* @param string Incoming data
* @return boolean
*/
function get_xml_rpc( $incoming='' )
{
if ( ! $incoming )
{
$incoming = $GLOBALS['HTTP_RAW_POST_DATA'];
}
if ( ! $this->xml_server )
{
return FALSE;
}
//-----------------------------------------
// Get data and dispatch
//-----------------------------------------
$this->api_decode_request( $incoming );
//-----------------------------------------
// Got function?
//-----------------------------------------
if ( $this->method_name AND is_array( $this->xml_server->__dispatch_map[ $this->method_name ] ) )
{
$func = $this->method_name;
$_params = array();
//-----------------------------------------
// Figure out params to use...
//-----------------------------------------
if ( is_array( $this->params ) and is_array( $this->xml_server->__dispatch_map[ $func ]['in'] ) )
{
foreach( $this->xml_server->__dispatch_map[ $func ]['in'] as $field => $type )
{
$_var = $this->params[ $field ];
switch ($type)
{
default:
case 'string':
$_var = (string) $_var;
break;
case 'int':
case 'i4':
$_var = (int) $_var;
break;
case 'double':
$_var = (double) $_var;
break;
case 'boolean':
$_var = (bool) $_var;
break;
case 'base64':
$_var = trim($_var);
break;
}
$_params[ $field ] = $_var;
}
}
if ( is_array( $_params ) )
{
@call_user_func_array( array( &$this->xml_server, $func), $_params );
}
else
{
@call_user_func( array( &$this->xml_server, $func), $_params );
}
}
else
{
//-----------------------------------------
// Return false
//-----------------------------------------
$this->api_send_error( 100, 'No methodRequest function defined / found' );
exit();
}
}
/*-------------------------------------------------------------------------*/
// Return API Request
/*-------------------------------------------------------------------------*/
/**
* Return API Request
*
* @param array Array of params to send
* @param int Complex data: Encode before sending
* @return nuffink
*/
function api_send_reply( $data=array(), $complex_data=0 )
{
//-----------------------------------------
// Check
//-----------------------------------------
if ( ! is_array( $data ) OR ! count( $data ) )
{
# No data? Just return true
$this->xmlrpc->xml_rpc_return_true();
}
//-----------------------------------------
// Complex data?
//-----------------------------------------
if ( $complex_data )
{
$_tmp = $data;
$data = array();
$data[ $this->serialized_key ] = $this->encode_base64_array( $_tmp );
$this->xmlrpc->map_type_to_key[ $this->serialized_key ] = 'base64';
}
//-----------------------------------------
// Send...
//-----------------------------------------
$this->xmlrpc->xml_rpc_return_params( $data );
}
/*-------------------------------------------------------------------------*/
// Return API Request( ERROR )
/*-------------------------------------------------------------------------*/
/**
* Return API Request (ERROR)
*
* @param int Error Code
* @param string Error message
* @return nuffink
*/
function api_send_error( $error_code, $error_msg )
{
$this->xmlrpc->xml_rpc_return_error( $error_code, $error_msg );
}
/*-------------------------------------------------------------------------*/
// Decode API Request
/*-------------------------------------------------------------------------*/
/**
* Decode API Request
*
* @param string Raw data picked up
* @return notsure
*/
function api_decode_request( $raw_data )
{
//-----------------------------------------
// INIT
//-----------------------------------------
//-----------------------------------------
// Get data...
//-----------------------------------------
$raw = $this->xmlrpc->xml_rpc_decode( $raw_data );
//-----------------------------------------
// Process return data
//-----------------------------------------
$this->api_process_data( $raw );
}
/*-------------------------------------------------------------------------*/
// Send API Request
/*-------------------------------------------------------------------------*/
/**
* Send API Request
*
* @param string URL to send request to
* @param string Method name for API to pick up
* @param array Data to send
* @param int Complex data: Encode before sending
* @return notsure
*/
function api_send_request( $url, $method_name, $data=array(), $complex_data=0 )
{
//-----------------------------------------
// INIT
//-----------------------------------------
$return_data = array();
$raw = array();
//-----------------------------------------
// Complex data?
//-----------------------------------------
if ( $complex_data )
{
$_tmp = $data;
$data = array();
$data[ $this->serialized_key ] = $this->encode_base64_array( $_tmp );
$this->xmlrpc->map_type_to_key[ $this->serialized_key ] = 'base64';
}
//-----------------------------------------
// Get data...
//-----------------------------------------
$return_data = $this->xmlrpc->xml_rpc_send( $url, $method_name, $data );
if ( count( $this->xmlrpc->errors ) )
{
$this->errors = $this->xmlrpc->errors;
return;
}
//-----------------------------------------
// Process return data
//-----------------------------------------
$this->api_process_data( $return_data );
}
/*-------------------------------------------------------------------------*/
// Process returned data
/*-------------------------------------------------------------------------*/
/**
* Process returned data
*
* @param array Raw array
* @return array Cleaned array
*/
function api_process_data( $raw=array() )
{
//-----------------------------------------
// INIT
//-----------------------------------------
$_params = $this->xmlrpc->xml_rpc_get_params( $raw );
$this->method_name = $this->xmlrpc->xml_rpc_get_method_name( $raw );
$this->params = array();
//-----------------------------------------
// Debug?
//-----------------------------------------
if ( IPS_XML_RPC_DEBUG_ON )
{
$this->xmlrpc->_add_debug( "API_PROCESS_DECODE: IN PARAMS: " . var_export( $raw, TRUE ) );
$this->xmlrpc->_add_debug( "API_PROCESS_DECODE: OUT PARAMS: " . var_export( $_params, TRUE ) );
}
//-----------------------------------------
// Fix up params
//-----------------------------------------
if ( is_array( $_params[0] ) )
{
foreach( $_params[0] as $k => $v )
{
if ( $k == $this->serialized_key )
{
$_tmp = $this->decode_base64_array( $v );
if ( is_array( $_tmp ) and count( $_tmp ) )
{
$this->params = array_merge( $this->params, $_tmp );
}
}
else
{
$this->params[ $k ] = $v;
}
}
}
}
/*-------------------------------------------------------------------------*/
// Encode array
/*-------------------------------------------------------------------------*/
/**
* Encode array
*
* @param array Raw array
* @return string Encoded string
*/
function encode_base64_array( $array )
{
return base64_encode( serialize( $array ) );
}
/*-------------------------------------------------------------------------*/
// Dencode array
/*-------------------------------------------------------------------------*/
/**
* Dencode array
*
* @param string Encoded string
* @return array Raw array
*/
function decode_base64_array( $data )
{
return unserialize( base64_decode( $data ) );
}
}
?>