<?php 


/** 
***************************************************
*欢迎使用免费版SiteTeam企业建站软件！
*这是一个专业为搜索引擎营销而设计的，极稳定、极易维护的建站软件！
*官方客服：help@php.net.cn
*官方下载网址：http://www.SiteTeam.cn 
***************************************************
**/




 function php_2_xmlrpc_type($phptype) { switch(strtolower($phptype)) { case 'string': return $GLOBALS['xmlrpcString']; case 'integer': case $GLOBALS['xmlrpcInt']: case $GLOBALS['xmlrpcI4']: return $GLOBALS['xmlrpcInt']; case 'double': return $GLOBALS['xmlrpcDouble']; case 'boolean': return $GLOBALS['xmlrpcBoolean']; case 'array': return $GLOBALS['xmlrpcArray']; case 'object': return $GLOBALS['xmlrpcStruct']; case $GLOBALS['xmlrpcBase64']: case $GLOBALS['xmlrpcStruct']: return strtolower($phptype); case 'resource': return ''; default: if(class_exists($phptype)) { return $GLOBALS['xmlrpcStruct']; } else { return $GLOBALS['xmlrpcValue']; } } } function xmlrpc_2_php_type($xmlrpctype) { switch(strtolower($xmlrpctype)) { case 'base64': case 'datetime.iso8601': case 'string': return $GLOBALS['xmlrpcString']; case 'int': case 'i4': return 'integer'; case 'struct': case 'array': return 'array'; case 'double': return 'float'; case 'undefined': return 'mixed'; case 'boolean': case 'null': default: return strtolower($xmlrpctype); } } function wrap_php_function($funcname, $newfuncname='', $extra_options=array()) { $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; $catch_warnings = isset($extra_options['suppress_warnings']) && $extra_options['suppress_warnings'] ? '@' : ''; if(version_compare(phpversion(), '5.0.3') == -1) { error_log('XML-RPC: cannot not wrap php functions unless running php version bigger than 5.0.3'); return false; } $exists = false; if(is_array($funcname)) { if(count($funcname) < 2 || (!is_string($funcname[0]) && !is_object($funcname[0]))) { error_log('XML-RPC: syntax for function to be wrapped is wrong'); return false; } if(is_string($funcname[0])) { $plainfuncname = implode('::', $funcname); } elseif(is_object($funcname[0])) { $plainfuncname = get_class($funcname[0]) . '->' . $funcname[1]; } $exists = method_exists($funcname[0], $funcname[1]); } else { $plainfuncname = $funcname; $exists = function_exists($funcname); } if(!$exists) { error_log('XML-RPC: function to be wrapped is not defined: '.$plainfuncname); return false; } else { if($newfuncname == '') { if(is_array($funcname)) { if(is_string($funcname[0])) $xmlrpcfuncname = "{$prefix}_".implode('_', $funcname); else $xmlrpcfuncname = "{$prefix}_".get_class($funcname[0]) . '_' . $funcname[1]; } else { $xmlrpcfuncname = "{$prefix}_$funcname"; } } else { $xmlrpcfuncname = $newfuncname; } while($buildit && function_exists($xmlrpcfuncname)) { $xmlrpcfuncname .= 'x'; } if(is_array($funcname)) { $func =& new ReflectionMethod($funcname[0], $funcname[1]); if($func->isPrivate()) { error_log('XML-RPC: method to be wrapped is private: '.$plainfuncname); return false; } if($func->isProtected()) { error_log('XML-RPC: method to be wrapped is protected: '.$plainfuncname); return false; } if($func->isConstructor()) { error_log('XML-RPC: method to be wrapped is the constructor: '.$plainfuncname); return false; } if($func->isDestructor()) { error_log('XML-RPC: method to be wrapped is the destructor: '.$plainfuncname); return false; } if($func->isAbstract()) { error_log('XML-RPC: method to be wrapped is abstract: '.$plainfuncname); return false; } } else { $func =& new ReflectionFunction($funcname); } if($func->isInternal()) { error_log('XML-RPC: function to be wrapped is internal: '.$plainfuncname); return false; } $desc = ''; $returns = $GLOBALS['xmlrpcValue']; $returnsDocs = ''; $paramDocs = array(); $docs = $func->getDocComment(); if($docs != '') { $docs = explode("\n", $docs); $i = 0; foreach($docs as $doc) { $doc = trim($doc, " \r\t/*"); if(strlen($doc) && strpos($doc, '@') !== 0 && !$i) { if($desc) { $desc .= "\n"; } $desc .= $doc; } elseif(strpos($doc, '@param') === 0) { if(preg_match('/@param\s+(\S+)(\s+\$\S+)?\s+(.+)/', $doc, $matches)) { if(strpos($matches[1], '|')) { $paramDocs[$i]['type'] = 'mixed'; } else { $paramDocs[$i]['type'] = $matches[1]; } $paramDocs[$i]['name'] = trim($matches[2]); $paramDocs[$i]['doc'] = $matches[3]; } $i++; } elseif(strpos($doc, '@return') === 0) { if(preg_match('/@return\s+(\S+)\s+(.+)/', $doc, $matches)) { $returns = php_2_xmlrpc_type($matches[1]); if(isset($matches[2])) { $returnsDocs = $matches[2]; } } } } } $params = array(); $i = 0; foreach($func->getParameters() as $paramobj) { $params[$i] = array(); $params[$i]['name'] = '$'.$paramobj->getName(); $params[$i]['isoptional'] = $paramobj->isOptional(); $i++; } $innercode = ''; $i = 0; $parsvariations = array(); $pars = array(); $pnum = count($params); foreach($params as $param) { if (isset($paramDocs[$i]['name']) && $paramDocs[$i]['name'] && strtolower($paramDocs[$i]['name']) != strtolower($param['name'])) { $paramDocs[$i]['type'] = 'mixed'; } if($param['isoptional']) { $innercode .= "if (\$paramcount > $i) {\n"; $parsvariations[] = $pars; } $innercode .= "\$p$i = \$msg->getParam($i);\n"; if ($decode_php_objects) { $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i, array('decode_php_objs'));\n"; } else { $innercode .= "if (\$p{$i}->kindOf() == 'scalar') \$p$i = \$p{$i}->scalarval(); else \$p$i = php_{$prefix}_decode(\$p$i);\n"; } $pars[] = "\$p$i"; $i++; if($param['isoptional']) { $innercode .= "}\n"; } if($i == $pnum) { $parsvariations[] = $pars; } } $sigs = array(); $psigs = array(); if(count($parsvariations) == 0) { $parsvariations[] = array(); $minpars = 0; } else { $minpars = count($parsvariations[0]); } if($minpars) { $innercode = "\$paramcount = \$msg->getNumParams();\n" . "if (\$paramcount < $minpars) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}');\n" . $innercode; } else { $innercode = "\$paramcount = \$msg->getNumParams();\n" . $innercode; } $innercode .= "\$np = false;\n"; if ( is_array($funcname) && is_object($funcname[0]) ) { $GLOBALS['xmlrpcWPFObjHolder'][$xmlrpcfuncname] =& $funcname[0]; $innercode .= "\$obj =& \$GLOBALS['xmlrpcWPFObjHolder']['$xmlrpcfuncname'];\n"; $realfuncname = '$obj->'.$funcname[1]; } else { $realfuncname = $plainfuncname; } foreach($parsvariations as $pars) { $innercode .= "if (\$paramcount == " . count($pars) . ") \$retval = {$catch_warnings}$realfuncname(" . implode(',', $pars) . "); else\n"; $sig = array($returns); $psig = array($returnsDocs); for($i=0; $i < count($pars); $i++) { if (isset($paramDocs[$i]['type'])) { $sig[] = php_2_xmlrpc_type($paramDocs[$i]['type']); } else { $sig[] = $GLOBALS['xmlrpcValue']; } $psig[] = isset($paramDocs[$i]['doc']) ? $paramDocs[$i]['doc'] : ''; } $sigs[] = $sig; $psigs[] = $psig; } $innercode .= "\$np = true;\n"; $innercode .= "if (\$np) return new {$prefix}resp(0, {$GLOBALS['xmlrpcerr']['incorrect_params']}, '{$GLOBALS['xmlrpcstr']['incorrect_params']}'); else {\n"; $innercode .= "if (is_a(\$retval, '{$prefix}resp')) return \$retval; else\n"; if($returns == $GLOBALS['xmlrpcDateTime'] || $returns == $GLOBALS['xmlrpcBase64']) { $innercode .= "return new {$prefix}resp(new {$prefix}val(\$retval, '$returns'));"; } else { if ($encode_php_objects) $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval, array('encode_php_objs')));\n"; else $innercode .= "return new {$prefix}resp(php_{$prefix}_encode(\$retval));\n"; } $code = "function $xmlrpcfuncname(\$msg) {\n" . $innercode . "}\n}"; if ($buildit) { $allOK = 0; eval($code.'$allOK=1;'); if(!$allOK) { error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap php function '.$plainfuncname); return false; } } $ret = array('function' => $xmlrpcfuncname, 'signature' => $sigs, 'docstring' => $desc, 'signature_docs' => $psigs, 'source' => $code); return $ret; } } function wrap_php_class($classname, $extra_options=array()) { $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; $methodtype = isset($extra_options['method_type']) ? $extra_options['method_type'] : 'auto'; if(version_compare(phpversion(), '5.0.3') == -1) { error_log('XML-RPC: cannot not wrap php functions unless running php version bigger than 5.0.3'); return false; } $result = array(); $mlist = get_class_methods($classname); foreach($mlist as $mname) { if ($methodfilter == '' || preg_match($methodfilter, $mname)) { $func =& new ReflectionMethod($classname, $mname); if(!$func->isPrivate() && !$func->isProtected() && !$func->isConstructor() && !$func->isDestructor() && !$func->isAbstract()) { if(($func->isStatic && ($methodtype == 'all' || $methodtype == 'static' || ($methodtype == 'auto' && is_string($classname)))) || (!$func->isStatic && ($methodtype == 'all' || $methodtype == 'nonstatic' || ($methodtype == 'auto' && is_object($classname))))) { $methodwrap = wrap_php_function(array($classname, $mname), '', $extra_options); if ( $methodwrap ) { $result[$methodwrap['function']] = $methodwrap['function']; } } } } } return $result; } function wrap_xmlrpc_method($client, $methodname, $extra_options=0, $timeout=0, $protocol='', $newfuncname='') { if (!is_array($extra_options)) { $signum = $extra_options; $extra_options = array(); } else { $signum = isset($extra_options['signum']) ? (int)$extra_options['signum'] : 0; $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; $newfuncname = isset($extra_options['new_function_name']) ? $extra_options['new_function_name'] : ''; } $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; $simple_client_copy = isset($extra_options['simple_client_copy']) ? (int)($extra_options['simple_client_copy']) : 0; $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; if (isset($extra_options['return_on_fault'])) { $decode_fault = true; $fault_response = $extra_options['return_on_fault']; } else { $decode_fault = false; $fault_response = ''; } $debug = isset($extra_options['debug']) ? ($extra_options['debug']) : 0; $msgclass = $prefix.'msg'; $valclass = $prefix.'val'; $decodefunc = 'php_'.$prefix.'_decode'; $msg =& new $msgclass('system.methodSignature'); $msg->addparam(new $valclass($methodname)); $client->setDebug($debug); $response =& $client->send($msg, $timeout, $protocol); if($response->faultCode()) { error_log('XML-RPC: could not retrieve method signature from remote server for method '.$methodname); return false; } else { $msig = $response->value(); if ($client->return_type != 'phpvals') { $msig = $decodefunc($msig); } if(!is_array($msig) || count($msig) <= $signum) { error_log('XML-RPC: could not retrieve method signature nr.'.$signum.' from remote server for method '.$methodname); return false; } else { if($newfuncname != '') { $xmlrpcfuncname = $newfuncname; } else { $xmlrpcfuncname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), array('_', ''), $methodname); } while($buildit && function_exists($xmlrpcfuncname)) { $xmlrpcfuncname .= 'x'; } $msig = $msig[$signum]; $mdesc = ''; if(!$buildit) { $msg =& new $msgclass('system.methodHelp'); $msg->addparam(new $valclass($methodname)); $response =& $client->send($msg, $timeout, $protocol); if (!$response->faultCode()) { $mdesc = $response->value(); if ($client->return_type != 'phpvals') { $mdesc = $mdesc->scalarval(); } } } $results = build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname, $msig, $mdesc, $timeout, $protocol, $simple_client_copy, $prefix, $decode_php_objects, $encode_php_objects, $decode_fault, $fault_response); if ($buildit) { $allOK = 0; eval($results['source'].'$allOK=1;'); if($allOK) { return $xmlrpcfuncname; } else { error_log('XML-RPC: could not create function '.$xmlrpcfuncname.' to wrap remote method '.$methodname); return false; } } else { $results['function'] = $xmlrpcfuncname; return $results; } } } } function wrap_xmlrpc_server($client, $extra_options=array()) { $methodfilter = isset($extra_options['method_filter']) ? $extra_options['method_filter'] : ''; $timeout = isset($extra_options['timeout']) ? (int)$extra_options['timeout'] : 0; $protocol = isset($extra_options['protocol']) ? $extra_options['protocol'] : ''; $newclassname = isset($extra_options['new_class_name']) ? $extra_options['new_class_name'] : ''; $encode_php_objects = isset($extra_options['encode_php_objs']) ? (bool)$extra_options['encode_php_objs'] : false; $decode_php_objects = isset($extra_options['decode_php_objs']) ? (bool)$extra_options['decode_php_objs'] : false; $verbatim_client_copy = isset($extra_options['simple_client_copy']) ? !($extra_options['simple_client_copy']) : true; $buildit = isset($extra_options['return_source']) ? !($extra_options['return_source']) : true; $prefix = isset($extra_options['prefix']) ? $extra_options['prefix'] : 'xmlrpc'; $msgclass = $prefix.'msg'; $decodefunc = 'php_'.$prefix.'_decode'; $msg =& new $msgclass('system.listMethods'); $response =& $client->send($msg, $timeout, $protocol); if($response->faultCode()) { error_log('XML-RPC: could not retrieve method list from remote server'); return false; } else { $mlist = $response->value(); if ($client->return_type != 'phpvals') { $mlist = $decodefunc($mlist); } if(!is_array($mlist) || !count($mlist)) { error_log('XML-RPC: could not retrieve meaningful method list from remote server'); return false; } else { if($newclassname != '') { $xmlrpcclassname = $newclassname; } else { $xmlrpcclassname = $prefix.'_'.preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), array('_', ''), $client->server).'_client'; } while($buildit && class_exists($xmlrpcclassname)) { $xmlrpcclassname .= 'x'; } $source = "class $xmlrpcclassname\n{\nvar \$client;\n\n"; $source .= "function $xmlrpcclassname()\n{\n"; $source .= build_client_wrapper_code($client, $verbatim_client_copy, $prefix); $source .= "\$this->client =& \$client;\n}\n\n"; $opts = array('simple_client_copy' => 2, 'return_source' => true, 'timeout' => $timeout, 'protocol' => $protocol, 'encode_php_objs' => $encode_php_objects, 'prefix' => $prefix, 'decode_php_objs' => $decode_php_objects ); foreach($mlist as $mname) { if ($methodfilter == '' || preg_match($methodfilter, $mname)) { $opts['new_function_name'] = preg_replace(array('/\./', '/[^a-zA-Z0-9_\x7f-\xff]/'), array('_', ''), $mname); $methodwrap = wrap_xmlrpc_method($client, $mname, $opts); if ($methodwrap) { if (!$buildit) { $source .= $methodwrap['docstring']; } $source .= $methodwrap['source']."\n"; } else { error_log('XML-RPC: will not create class method to wrap remote method '.$mname); } } } $source .= "}\n"; if ($buildit) { $allOK = 0; eval($source.'$allOK=1;'); if($allOK) { return $xmlrpcclassname; } else { error_log('XML-RPC: could not create class '.$xmlrpcclassname.' to wrap remote server '.$client->server); return false; } } else { return array('class' => $xmlrpcclassname, 'code' => $source, 'docstring' => ''); } } } } function build_remote_method_wrapper_code($client, $methodname, $xmlrpcfuncname, $msig, $mdesc='', $timeout=0, $protocol='', $client_copy_mode=0, $prefix='xmlrpc', $decode_php_objects=false, $encode_php_objects=false, $decode_fault=false, $fault_response='') { $code = "function $xmlrpcfuncname ("; if ($client_copy_mode < 2) { $innercode = build_client_wrapper_code($client, $client_copy_mode, $prefix); $innercode .= "\$client->setDebug(\$debug);\n"; $this_ = ''; } else { $innercode = ''; $this_ = 'this->'; } $innercode .= "\$msg =& new {$prefix}msg('$methodname');\n"; if ($mdesc != '') { $mdesc = "/**\n* ".str_replace('*/', '* /', $mdesc)."\n"; } else { $mdesc = "/**\nFunction $xmlrpcfuncname\n"; } $plist = array(); $pcount = count($msig); for($i = 1; $i < $pcount; $i++) { $plist[] = "\$p$i"; $ptype = $msig[$i]; if($ptype == 'i4' || $ptype == 'int' || $ptype == 'boolean' || $ptype == 'double' || $ptype == 'string' || $ptype == 'dateTime.iso8601' || $ptype == 'base64' || $ptype == 'null') { $innercode .= "\$p$i =& new {$prefix}val(\$p$i, '$ptype');\n"; } else { if ($encode_php_objects) { $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i, array('encode_php_objs'));\n"; } else { $innercode .= "\$p$i =& php_{$prefix}_encode(\$p$i);\n"; } } $innercode .= "\$msg->addparam(\$p$i);\n"; $mdesc .= '* @param '.xmlrpc_2_php_type($ptype)." \$p$i\n"; } if ($client_copy_mode < 2) { $plist[] = '$debug=0'; $mdesc .= "* @param int \$debug when 1 (or 2) will enable debugging of the underlying {$prefix} call (defaults to 0)\n"; } $plist = implode(', ', $plist); $mdesc .= '* @return '.xmlrpc_2_php_type($msig[0])." (or an {$prefix}resp obj instance if call fails)\n*/\n"; $innercode .= "\$res =& \${$this_}client->send(\$msg, $timeout, '$protocol');\n"; if ($decode_fault) { if (is_string($fault_response) && ((strpos($fault_response, '%faultCode%') !== false) || (strpos($fault_response, '%faultString%') !== false))) { $respcode = "str_replace(array('%faultCode%', '%faultString%'), array(\$res->faultCode(), \$res->faultString()), '".str_replace("'", "''", $fault_response)."')"; } else { $respcode = var_export($fault_response, true); } } else { $respcode = '$res'; } if ($decode_php_objects) { $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value(), array('decode_php_objs'));"; } else { $innercode .= "if (\$res->faultcode()) return $respcode; else return php_{$prefix}_decode(\$res->value());"; } $code = $code . $plist. ") {\n" . $innercode . "\n}\n"; return array('source' => $code, 'docstring' => $mdesc); } function build_client_wrapper_code($client, $verbatim_client_copy, $prefix='xmlrpc') { $code = "\$client =& new {$prefix}_client('".str_replace("'", "\'", $client->path). "', '" . str_replace("'", "\'", $client->server) . "', $client->port);\n"; if ($verbatim_client_copy) { foreach($client as $fld => $val) { if($fld != 'debug' && $fld != 'return_type') { $val = var_export($val, true); $code .= "\$client->$fld = $val;\n"; } } } $code .= "\$client->return_type = '{$prefix}vals';\n"; return $code; } ?>