2001-08-08 01:00:28 +02:00
|
|
|
<?php
|
|
|
|
// by Edd Dumbill (C) 1999-2001
|
|
|
|
// <edd@usefulinc.com>
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
// License is granted to use or modify this software ("XML-RPC for PHP")
|
|
|
|
// for commercial or non-commercial use provided the copyright of the author
|
|
|
|
// is preserved in any distributed or derivative work.
|
|
|
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR
|
|
|
|
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// XML RPC Server class
|
|
|
|
// requires: xmlrpc.inc
|
|
|
|
|
|
|
|
/* BEGIN server class */
|
|
|
|
class xmlrpc_server
|
|
|
|
{
|
|
|
|
var $dmap = array();
|
2001-08-23 08:47:50 +02:00
|
|
|
var $authed = False;
|
2001-08-08 01:00:28 +02:00
|
|
|
|
2001-08-19 03:18:38 +02:00
|
|
|
function xmlrpc_server($dispMap='', $serviceNow=0)
|
2001-08-08 01:00:28 +02:00
|
|
|
{
|
|
|
|
global $HTTP_RAW_POST_DATA;
|
2001-08-19 03:18:38 +02:00
|
|
|
|
2001-08-08 01:00:28 +02:00
|
|
|
// dispMap is a despatch array of methods
|
|
|
|
// mapped to function names and signatures
|
|
|
|
// if a method
|
|
|
|
// doesn't appear in the map then an unknown
|
|
|
|
// method error is generated
|
2001-08-19 03:18:38 +02:00
|
|
|
if($dispMap)
|
2001-08-08 01:00:28 +02:00
|
|
|
{
|
2001-08-19 03:18:38 +02:00
|
|
|
$this->dmap = $dispMap;
|
|
|
|
if ($serviceNow)
|
|
|
|
{
|
|
|
|
$this->service();
|
|
|
|
}
|
2001-08-08 01:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeDebug()
|
|
|
|
{
|
2001-08-09 04:54:56 +02:00
|
|
|
// global $_xmlrpc_debuginfo;
|
|
|
|
if ($GLOBALS['_xmlrpc_debuginfo'] != '')
|
2001-08-08 01:00:28 +02:00
|
|
|
{
|
2001-08-09 04:54:56 +02:00
|
|
|
return "<!-- DEBUG INFO:\n\n" . $GLOBALS['_xmlrpc_debuginfo'] . "\n-->\n";
|
2001-08-08 01:00:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function service()
|
|
|
|
{
|
2001-08-19 03:18:38 +02:00
|
|
|
global $HTTP_RAW_POST_DATA;
|
|
|
|
|
2001-08-08 01:00:28 +02:00
|
|
|
$r = $this->parseRequest();
|
|
|
|
$payload = "<?xml version=\"1.0\"?>\n" . $this->serializeDebug() . $r->serialize();
|
|
|
|
Header("Content-type: text/xml\r\nContent-length: " . strlen($payload));
|
|
|
|
print $payload;
|
|
|
|
}
|
|
|
|
|
2001-08-19 03:18:38 +02:00
|
|
|
/*
|
|
|
|
add a method to the dispatch map
|
|
|
|
*/
|
|
|
|
function add_to_map($methodname,$function,$sig,$doc)
|
|
|
|
{
|
|
|
|
$this->dmap[$methodname] = array(
|
|
|
|
'function' => $function,
|
|
|
|
'signature' => $sig,
|
|
|
|
'docstring' => $doc
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2001-08-08 01:00:28 +02:00
|
|
|
function verifySignature($in, $sig)
|
|
|
|
{
|
|
|
|
for($i=0; $i<sizeof($sig); $i++)
|
|
|
|
{
|
|
|
|
// check each possible signature in turn
|
|
|
|
$cursig = $sig[$i];
|
|
|
|
if (sizeof($cursig) == $in->getNumParams()+1)
|
|
|
|
{
|
|
|
|
$itsOK = 1;
|
|
|
|
for($n=0; $n<$in->getNumParams(); $n++)
|
|
|
|
{
|
|
|
|
$p = $in->getParam($n);
|
|
|
|
// print "<!-- $p -->\n";
|
|
|
|
if ($p->kindOf() == 'scalar')
|
|
|
|
{
|
|
|
|
$pt = $p->scalartyp();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$pt = $p->kindOf();
|
|
|
|
}
|
|
|
|
// $n+1 as first type of sig is return type
|
|
|
|
if ($pt != $cursig[$n+1])
|
|
|
|
{
|
|
|
|
$itsOK = 0;
|
|
|
|
$pno = $n+1;
|
|
|
|
$wanted = $cursig[$n+1];
|
|
|
|
$got = $pt;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($itsOK)
|
|
|
|
{
|
|
|
|
return array(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-08-20 18:33:26 +02:00
|
|
|
return array(0, "Wanted $wanted, got $got at param $pno)");
|
2001-08-08 01:00:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseRequest($data='')
|
|
|
|
{
|
2001-08-09 04:54:56 +02:00
|
|
|
global $HTTP_RAW_POST_DATA;
|
|
|
|
// global $_xh, $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $xmlrpc_defencoding, $_xmlrpcs_dmap;
|
2001-08-08 01:00:28 +02:00
|
|
|
|
|
|
|
if ($data == '')
|
|
|
|
{
|
|
|
|
$data = $HTTP_RAW_POST_DATA;
|
|
|
|
}
|
2001-08-09 04:54:56 +02:00
|
|
|
$parser = xml_parser_create($GLOBALS['xmlrpc_defencoding']);
|
2001-08-08 01:00:28 +02:00
|
|
|
|
2001-08-09 04:54:56 +02:00
|
|
|
$GLOBALS['_xh'][$parser] = array();
|
|
|
|
$GLOBALS['_xh'][$parser]['st'] = '';
|
|
|
|
$GLOBALS['_xh'][$parser]['cm'] = 0;
|
|
|
|
$GLOBALS['_xh'][$parser]['isf'] = 0;
|
|
|
|
$GLOBALS['_xh'][$parser]['params'] = array();
|
|
|
|
$GLOBALS['_xh'][$parser]['method'] = '';
|
2001-08-08 01:00:28 +02:00
|
|
|
|
|
|
|
// decompose incoming XML into request structure
|
|
|
|
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
|
|
|
|
xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
|
|
|
|
xml_set_character_data_handler($parser, 'xmlrpc_cd');
|
|
|
|
xml_set_default_handler($parser, 'xmlrpc_dh');
|
|
|
|
if (!xml_parse($parser, $data, 1))
|
|
|
|
{
|
|
|
|
// return XML error as a faultCode
|
|
|
|
$r = CreateObject('phpgwapi.xmlrpcresp',0,
|
2001-08-09 04:54:56 +02:00
|
|
|
$GLOBALS['xmlrpcerrxml'] + xml_get_error_code($parser),
|
2001-08-08 01:00:28 +02:00
|
|
|
sprintf('XML error: %s at line %d',
|
|
|
|
xml_error_string(xml_get_error_code($parser)),
|
|
|
|
xml_get_current_line_number($parser))
|
|
|
|
);
|
|
|
|
xml_parser_free($parser);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xml_parser_free($parser);
|
2001-08-09 04:54:56 +02:00
|
|
|
$m = CreateObject('phpgwapi.xmlrpcmsg',$GLOBALS['_xh'][$parser]['method']);
|
2001-08-08 01:00:28 +02:00
|
|
|
// now add parameters in
|
2001-08-09 04:54:56 +02:00
|
|
|
for($i=0; $i<sizeof($GLOBALS['_xh'][$parser]['params']); $i++)
|
2001-08-08 01:00:28 +02:00
|
|
|
{
|
2001-08-09 04:54:56 +02:00
|
|
|
//print "<!-- " . $GLOBALS['_xh'][$parser]['params'][$i]. "-->\n";
|
|
|
|
$plist .= "$i - " . $GLOBALS['_xh'][$parser]['params'][$i]. " \n";
|
|
|
|
$code = '$m->addParam(' . $GLOBALS['_xh'][$parser]['params'][$i] . ');';
|
2001-08-08 01:00:28 +02:00
|
|
|
$code = ereg_replace(',,',",'',",$code);
|
|
|
|
eval($code);
|
|
|
|
}
|
|
|
|
// uncomment this to really see what the server's getting!
|
|
|
|
// xmlrpc_debugmsg($plist);
|
|
|
|
// now to deal with the method
|
2001-08-09 04:54:56 +02:00
|
|
|
$methName = $GLOBALS['_xh'][$parser]['method'];
|
2001-08-08 01:00:28 +02:00
|
|
|
if (ereg("^system\.", $methName))
|
|
|
|
{
|
2001-08-09 04:54:56 +02:00
|
|
|
$dmap = $GLOBALS['_xmlrpcs_dmap'];
|
|
|
|
$sysCall=1;
|
2001-08-08 01:00:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-08-09 04:54:56 +02:00
|
|
|
$dmap = $this->dmap;
|
|
|
|
$sysCall=0;
|
2001-08-08 01:00:28 +02:00
|
|
|
}
|
2001-08-09 01:28:03 +02:00
|
|
|
if(!isset($dmap[$methName]['function']))
|
|
|
|
{
|
2001-08-23 08:47:50 +02:00
|
|
|
if($this->authed)
|
|
|
|
{
|
|
|
|
/* phpgw mod - fetch the (bo) class methods to create the dmap */
|
|
|
|
$method = $methName;
|
|
|
|
$tmp = explode('.',$methName);
|
|
|
|
$methName = $tmp[2];
|
|
|
|
$listmeth = $tmp[0] . '.' . $tmp[1] . '.' . 'list_methods';
|
|
|
|
$dmap = ExecMethod($listmeth,'xmlrpc');
|
|
|
|
$this->dmap = $dmap;
|
|
|
|
}
|
2001-08-09 01:28:03 +02:00
|
|
|
}
|
2001-08-08 01:00:28 +02:00
|
|
|
if (isset($dmap[$methName]['function']))
|
|
|
|
{
|
|
|
|
// dispatch if exists
|
|
|
|
if (isset($dmap[$methName]['signature']))
|
|
|
|
{
|
|
|
|
$sr = $this->verifySignature($m, $dmap[$methName]['signature'] );
|
|
|
|
}
|
|
|
|
if ( (!isset($dmap[$methName]['signature'])) || $sr[0])
|
|
|
|
{
|
|
|
|
// if no signature or correct signature
|
|
|
|
if ($sysCall)
|
|
|
|
{
|
|
|
|
$code = '$r=' . $dmap[$methName]['function'] . '($this, $m);';
|
|
|
|
$code = ereg_replace(',,',",'',",$code);
|
|
|
|
eval($code);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-08-09 01:28:03 +02:00
|
|
|
if(function_exists($dmap[$methName]['function']))
|
|
|
|
{
|
|
|
|
$code = '$r =' . $dmap[$methName]['function'] . '($m);';
|
|
|
|
$code = ereg_replace(',,',",'',",$code);
|
2001-08-20 18:33:26 +02:00
|
|
|
eval($code);
|
2001-08-09 01:28:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-08-20 18:33:26 +02:00
|
|
|
/* phpgw mod - finally, execute the function call and return the values */
|
|
|
|
$params = $GLOBALS['_xh'][$parser]['params'][0];
|
|
|
|
$code = '$p = ' . $params . ';';
|
|
|
|
eval($code);
|
|
|
|
$params = $p->getval();
|
2001-08-23 08:47:50 +02:00
|
|
|
/* _debug_array($params); */
|
|
|
|
if(gettype($params) == 'array')
|
|
|
|
{
|
|
|
|
@reset($params);
|
|
|
|
while(list($key,$val) = @each($params))
|
|
|
|
{
|
|
|
|
if(gettype($val) == 'array')
|
|
|
|
{
|
|
|
|
@reset($val);
|
|
|
|
while(list($key1,$val1) = @each($val))
|
|
|
|
{
|
|
|
|
$tmp = '';
|
|
|
|
if(get_class($val1) == 'xmlrpcval')
|
|
|
|
{
|
|
|
|
$tmp[$key1] = $val1->getval();
|
|
|
|
/* echo '<br>Adding xmlrpc val1: ' . $tmp[$key1] . "\n"; */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* echo '<br>Adding val1: ' . $val1 . "\n"; */
|
|
|
|
$tmp[$key1] = $val1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$_params[$key] = $tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* echo '<br>Adding val: ' . $val . "\n"; */
|
|
|
|
$_params[$key] = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$params = $_params;
|
|
|
|
}
|
|
|
|
/* _debug_array($params); */
|
2001-08-20 18:33:26 +02:00
|
|
|
$res = ExecMethod($method,$params);
|
2001-08-23 08:47:50 +02:00
|
|
|
/* _debug_array($res);exit; */
|
|
|
|
@reset($res);
|
2001-08-20 18:33:26 +02:00
|
|
|
while(list($key,$val) = @each($res))
|
|
|
|
{
|
|
|
|
if(gettype($val) == 'array')
|
|
|
|
{
|
2001-08-23 08:47:50 +02:00
|
|
|
@reset($val);
|
2001-08-20 18:33:26 +02:00
|
|
|
while(list($x,$y) = @each($val))
|
|
|
|
{
|
2001-08-23 08:47:50 +02:00
|
|
|
$aa[$x] = CreateObject('phpgwapi.xmlrpcval',$y,'string');
|
2001-08-20 18:33:26 +02:00
|
|
|
}
|
2001-08-23 08:47:50 +02:00
|
|
|
$ele[$key] = CreateObject('phpgwapi.xmlrpcval',$aa,'struct');
|
2001-08-20 18:33:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$ele[$key] = CreateObject('phpgwapi.xmlrpcval',$val,'string');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',$ele,'struct'));
|
2001-08-23 08:47:50 +02:00
|
|
|
/* _debug_array($r);exit; */
|
2001-08-09 01:28:03 +02:00
|
|
|
}
|
2001-08-08 01:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-08-20 18:33:26 +02:00
|
|
|
$r = CreateObject(
|
|
|
|
'phpgwapi.xmlrpcresp',
|
|
|
|
CreateObject('phpgwapi.xmlrpcval'),
|
|
|
|
$GLOBALS['xmlrpcerr']['incorrect_params'],
|
|
|
|
$GLOBALS['xmlrpcstr']['incorrect_params'] . ': ' . $sr[1]
|
2001-08-08 01:00:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// else prepare error response
|
2001-08-23 08:47:50 +02:00
|
|
|
if(!$this->authed)
|
|
|
|
{
|
|
|
|
$r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval','UNAUTHORIZED','string'));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$r = CreateObject(
|
|
|
|
'phpgwapi.xmlrpcresp',
|
|
|
|
CreateObject('phpgwapi.xmlrpcval'),
|
|
|
|
$GLOBALS['xmlrpcerr']['unknown_method'],
|
|
|
|
$GLOBALS['xmlrpcstr']['unknown_method']
|
|
|
|
);
|
|
|
|
}
|
2001-08-08 01:00:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
|
|
|
function echoInput()
|
|
|
|
{
|
|
|
|
global $HTTP_RAW_POST_DATA;
|
|
|
|
|
|
|
|
// a debugging routine: just echos back the input
|
|
|
|
// packet as a string value
|
|
|
|
|
|
|
|
$r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',"'Aha said I: '" . $HTTP_RAW_POST_DATA,'string'));
|
|
|
|
echo $r->serialize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|