scalartyp() 공개 메소드

public scalartyp ( )
예제 #1
0
/**
 * Takes an xmlrpc value in PHP xmlrpcval object format and translates it into native PHP types.
 *
 * Works with xmlrpc message objects as input, too.
 *
 * Given proper options parameter, can rebuild generic php object instances
 * (provided those have been encoded to xmlrpc format using a corresponding
 * option in php_xmlrpc_encode())
 * PLEASE NOTE that rebuilding php objects involves calling their constructor function.
 * This means that the remote communication end can decide which php code will
 * get executed on your server, leaving the door possibly open to 'php-injection'
 * style of attacks (provided you have some classes defined on your server that
 * might wreak havoc if instances are built outside an appropriate context).
 * Make sure you trust the remote server/client before eanbling this!
 *
 * @author Dan Libby (dan@libby.com)
 *
 * @param xmlrpcval $xmlrpc_val
 * @param array $options if 'decode_php_objs' is set in the options array, xmlrpc structs can be decoded into php objects; if 'dates_as_objects' is set xmlrpc datetimes are decoded as php DateTime objects (standard is
 * @return mixed
 */
function php_xmlrpc_decode($xmlrpc_val, $options = array())
{
    switch ($xmlrpc_val->kindOf()) {
        case 'scalar':
            if (in_array('extension_api', $options)) {
                reset($xmlrpc_val->me);
                list($typ, $val) = each($xmlrpc_val->me);
                switch ($typ) {
                    case 'dateTime.iso8601':
                        $xmlrpc_val->scalar = $val;
                        $xmlrpc_val->xmlrpc_type = 'datetime';
                        $xmlrpc_val->timestamp = iso8601_decode($val);
                        return $xmlrpc_val;
                    case 'base64':
                        $xmlrpc_val->scalar = $val;
                        $xmlrpc_val->type = $typ;
                        return $xmlrpc_val;
                    default:
                        return $xmlrpc_val->scalarval();
                }
            }
            if (in_array('dates_as_objects', $options) && $xmlrpc_val->scalartyp() == 'dateTime.iso8601') {
                // we return a Datetime object instead of a string
                // since now the constructor of xmlrpcval accepts safely strings, ints and datetimes,
                // we cater to all 3 cases here
                $out = $xmlrpc_val->scalarval();
                if (is_string($out)) {
                    $out = strtotime($out);
                }
                if (is_int($out)) {
                    $result = new Datetime();
                    $result->setTimestamp($out);
                    return $result;
                } elseif (is_a($out, 'Datetime')) {
                    return $out;
                }
            }
            return $xmlrpc_val->scalarval();
        case 'array':
            $size = $xmlrpc_val->arraysize();
            $arr = array();
            for ($i = 0; $i < $size; $i++) {
                $arr[] = php_xmlrpc_decode($xmlrpc_val->arraymem($i), $options);
            }
            return $arr;
        case 'struct':
            $xmlrpc_val->structreset();
            // If user said so, try to rebuild php objects for specific struct vals.
            /// @todo should we raise a warning for class not found?
            // shall we check for proper subclass of xmlrpcval instead of
            // presence of _php_class to detect what we can do?
            if (in_array('decode_php_objs', $options) && $xmlrpc_val->_php_class != '' && class_exists($xmlrpc_val->_php_class)) {
                $obj = @new $xmlrpc_val->_php_class();
                while (list($key, $value) = $xmlrpc_val->structeach()) {
                    $obj->{$key} = php_xmlrpc_decode($value, $options);
                }
                return $obj;
            } else {
                $arr = array();
                while (list($key, $value) = $xmlrpc_val->structeach()) {
                    $arr[$key] = php_xmlrpc_decode($value, $options);
                }
                return $arr;
            }
        case 'msg':
            $paramcount = $xmlrpc_val->getNumParams();
            $arr = array();
            for ($i = 0; $i < $paramcount; $i++) {
                $arr[] = php_xmlrpc_decode($xmlrpc_val->getParam($i));
            }
            return $arr;
    }
}