1) added a base-class with some useful functions:

- converting to and from dateTime.iso8601
- converting cat_id-arrays to and from xmlrpc structs with cat-id / name pairs (and creating & updateing new cats)
2) removed unnecessary (and not standard conform) xmlrpc struc arround all responses
3) added detection to copy arrays (numerical keys 0,1,...) to xmlrpc arrays and not structs
Note: 2+3 only apply to the _php class as the _epi class does this by itself
This commit is contained in:
Ralf Becker 2004-03-10 00:38:10 +00:00
parent c9d6478612
commit 0711abd82b
3 changed files with 130 additions and 11 deletions

View File

@ -19,8 +19,123 @@
/* $Id$ */
// contains useful functions for xmlrpc methods
class xmlrpc_server_shared
{
// convert a date-array or timestamp into a datetime.iso8601 string
function date2iso8601($date)
{
if (!is_array($date))
{
return date('Y-m-d\TH:i:s',$date);
}
return sprintf('%04d-%02d-%02dT%02d:%02d:%02d',
$date['year'],$date['month'],$date['mday'],
$date['hour'],$date['min'],$date['sec']);
}
// convert a datetime.iso8601 string into a datearray or timestamp
function iso86012date($isodate,$timestamp=False)
{
if (($arr = split('[-:T]',$isodate)) && count($arr) == 6)
{
foreach(array('year','month','mday','hour','min','sec') as $n => $name)
{
$date[$name] = (int)$arr[$n];
}
return $timestamp ? mktime($date['hour'],$date['min'],$date['sec'],
$date['month'],$date['mday'],$date['year']) : $date;
}
return False;
}
// translate cat-ids to array with id-name pairs
function cats2xmlrpc($cats)
{
if (!is_object($GLOBALS['phpgw']->categories))
{
$GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories');
}
$xcats = array();
foreach($cats as $cat)
{
if ($cat)
{
$xcats[$cat] = stripslashes($GLOBALS['phpgw']->categories->id2name($cat));
}
}
return $xcats;
}
// translate cats back to cat-ids, creating / modifying cats on the fly
function xmlrpc2cats($xcats)
{
if (!is_array($xcats))
{
$xcats = array();
}
elseif (!is_object($GLOBALS['phpgw']->categories))
{
$GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories');
}
$cats = array();
foreach($xcats as $cat => $name)
{
if ($id = $GLOBALS['phpgw']->categories->id2name($name))
{
// existing cat-name use the id
$cat = $id;
}
elseif (!($org_name = stripslashes($GLOBALS['phpgw']->categories->id2name($cat))) || $org_name == '--')
{
// new cat
$cat = $GLOBALS['phpgw']->categories->add(array('name' => $name,'parent' => 0));
}
elseif ($org_name != $name)
{
// cat-name edited
list($cat_vals) =$GLOBALS['phpgw']->categories->return_single($cat);
$cat_vals['name'] = $name;
$GLOBALS['phpgw']->categories->edit($cat_vals);
}
$cats[] = (int)$cat;
}
return $cats;
}
// get list (array with id-name pairs) of all cats of $app
function categories($complete = False,$app = '')
{
if (is_array($complete)) $complete = @$complete[0];
if (!$app) list($app) = explode('.',$this->last_method);
if (!is_object($GLOBALS['phpgw']->categories))
{
$GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories');
}
if ($GLOBALS['phpgw']->categories->app_name != $app)
{
$GLOBALS['phpgw']->categories->categories('',$app);
}
$cats_arr = $GLOBALS['phpgw']->categories->return_sorted_array(0,False,'','','',True);
$cats = array();
if (is_array($cats_arr))
{
foreach($cats_arr as $cat)
{
foreach(array('name','description') as $name)
{
$cat[$name] = stripslashes($cat[$name]);
}
$cats[$cat['id']] = $complete ? $cat : $cat['name'];
}
}
return $cats;
}
}
if(empty($GLOBALS['phpgw_info']['server']['xmlrpc_type']))
{
$GLOBALS['phpgw_info']['server']['xmlrpc_type'] = 'php';
}
include_once(PHPGW_API_INC . SEP . 'class.xmlrpc_server_' . $GLOBALS['phpgw_info']['server']['xmlrpc_type'] . '.inc.php');
include_once(PHPGW_API_INC.SEP.'class.xmlrpc_server_' . $GLOBALS['phpgw_info']['server']['xmlrpc_type'] . '.inc.php');

View File

@ -19,7 +19,7 @@
/* $Id$ */
class xmlrpc_server
class xmlrpc_server extends xmlrpc_server_shared
{
var $server = '';
var $authed = True;
@ -333,6 +333,7 @@
$error_string . ': ' . $this->last_method
);
$this->service($r);
xmlrpc_server_destroy($GLOBALS['xmlrpc_server']);
exit;
}
}

View File

@ -34,7 +34,7 @@
/* $Id$ */
/* BEGIN server class */
class xmlrpc_server
class xmlrpc_server extends xmlrpc_server_shared
{
var $dmap = array();
var $authed = False;
@ -225,15 +225,19 @@
{
if (is_array($_res))
{
$i = 0;
$is_array = True;
foreach($_res as $key => $val)
{
$ele[$key] = $this->build_resp($val,True);
$is_array = $is_array && $i === $key;
++$i;
}
return CreateObject('phpgwapi.xmlrpcval',$ele,'struct');
return CreateObject('phpgwapi.xmlrpcval',$ele,$is_array ? 'array' : 'struct');
}
$_type = (is_integer($_res) ? 'int' : gettype($_res));
if ($_type == string && ereg('^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$',$_res))
if ($_type == 'string' && ereg('^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$',$_res))
{
$_type = 'dateTime.iso8601';
}
@ -412,12 +416,11 @@
list($s,$c,$m) = explode('.',$_methName);
$res = ExecMethod($s . '.' . $c . '.' . $dmap[$methName]['function'],$this->req_array);
}
/* $res = ExecMethod($method,$params); */
/* _debug_array($res);exit; */
$this->resp_struct = array($this->build_resp($res,True));
/*_debug_array($this->resp_struct); */
@reset($this->resp_struct);
$r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',$this->resp_struct,'struct'));
//$this->resp_struct = array($this->build_resp($res,True));
//@reset($this->resp_struct);
//$r = CreateObject('phpgwapi.xmlrpcresp',CreateObject('phpgwapi.xmlrpcval',$this->resp_struct,'struct'));
// this fixes the unnecessary (and not standard-conform) array/xmlrpc struct around everything
$r = CreateObject('phpgwapi.xmlrpcresp',$this->build_resp($res,True));
/* _debug_array($r); */
}
}