⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlrpc.inc

📁 一个全功能的国外博客商业程序
💻 INC
📖 第 1 页 / 共 4 页
字号:
			if (! $this->no_multicall)
			{
				$results = $this->_try_multicall($msgs, $timeout, $method);
				/* TODO - this is not php3-friendly */
				// if($results !== false)
				if(is_array($results))
				{
					// Either the system.multicall succeeded, or the send
					// failed (e.g. due to HTTP timeout). In either case,
					// we're done for now.
					return $results;
				}
				else
				{
					// system.multicall unsupported by server,
					// don't try it next time...
					$this->no_multicall = true;
				}
			}

			// system.multicall is unupported by server:
			//   Emulate multicall via multiple requests
			$results = array();
			//foreach($msgs as $msg)
			@reset($msgs);
			while(list(,$msg) = @each($msgs))
			{
				$results[] = $this->send($msg, $timeout, $method);
			}
			return $results;
		}

		// Attempt to boxcar $msgs via system.multicall.
		function _try_multicall($msgs, $timeout, $method)
		{
			// Construct multicall message
			$calls = array();
			//foreach($msgs as $msg)
			@reset($msgs);
			while(list(,$msg) = @each($msgs))
			{
				$call['methodName'] = new xmlrpcval($msg->method(),'string');
				$numParams = $msg->getNumParams();
				$params = array();
				for ($i = 0; $i < $numParams; $i++)
				{
					$params[$i] = $msg->getParam($i);
				}
				$call['params'] = new xmlrpcval($params, 'array');
				$calls[] = new xmlrpcval($call, 'struct');
			}
			$multicall = new xmlrpcmsg('system.multicall');
			$multicall->addParam(new xmlrpcval($calls, 'array'));

			// Attempt RPC call
			$result = $this->send($multicall, $timeout, $method);
			if(!is_object($result))
			{
				return ($result || 0); // transport failed
			}

			if($result->faultCode() != 0)
			{
				return false;		// system.multicall failed
			}

			// Unpack responses.
			$rets = $result->value();
			if($rets->kindOf() != 'array')
			{
				return false;		// bad return type from system.multicall
			}
			$numRets = $rets->arraysize();
			if($numRets != count($msgs))
			{
				return false;		// wrong number of return values.
			}

			$response = array();
			for ($i = 0; $i < $numRets; $i++)
			{
				$val = $rets->arraymem($i);
				switch ($val->kindOf())
				{
				case 'array':
					if($val->arraysize() != 1)
					{
						return false;		// Bad value
					}
					// Normal return value
					$response[$i] = new xmlrpcresp($val->arraymem(0));
					break;
				case 'struct':
					$code = $val->structmem('faultCode');
					if($code->kindOf() != 'scalar' || $code->scalartyp() != 'int')
					{
						return false;
					}
					$str = $val->structmem('faultString');
					if($str->kindOf() != 'scalar' || $str->scalartyp() != 'string')
					{
						return false;
					}
					$response[$i] = new xmlrpcresp(0, $code->scalarval(), $str->scalarval());
					break;
				default:
					return false;
				}
			}
			return $response;
		}
	} // end class xmlrpc_client

	class xmlrpcresp
	{
		var $val = 0;
		var $errno = 0;
		var $errstr = '';
		var $hdrs = array();

		function xmlrpcresp($val, $fcode = 0, $fstr = '')
		{
			if ($fcode != 0)
			{
				// error
				$this->errno = $fcode;
				$this->errstr = $fstr;
				//$this->errstr = htmlspecialchars($fstr); // XXX: encoding probably shouldn't be done here; fix later.
			}
			elseif (!is_object($val))
			{
				// programmer error
				error_log("Invalid type '" . gettype($val) . "' (value: $val) passed to xmlrpcresp. Defaulting to empty value.");
				$this->val = new xmlrpcval();
			}
			else
			{
				// success
				$this->val = $val;
			}
		}

		function faultCode()
		{
			return $this->errno;
		}

		function faultString()
		{
			return $this->errstr;
		}

		function value()
		{
			return $this->val;
		}

		function serialize()
		{
			$result = "<methodResponse>\n";
			if ($this->errno)
			{
				// G. Giunta 2005/2/13: let non-ASCII response messages be tolerated by clients
				$result .= '<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>' . $this->errno . '</int></value>
</member>
<member>
<name>faultString</name>
<value><string>' . xmlrpc_encode2_entitites($this->errstr) . '</string></value>
</member>
</struct>
</value>
</fault>';
			}
			else
			{
				$result .= "<params>\n<param>\n" .
					$this->val->serialize() . 
					"</param>\n</params>";
			}
			$result .= "\n</methodResponse>";
			return $result;
		}
	}

	class xmlrpcmsg
	{
		var $payload;
		var $methodname;
		var $params=array();
		var $debug=0;

		function xmlrpcmsg($meth, $pars=0)
		{
			$this->methodname=$meth;
			if (is_array($pars) && sizeof($pars)>0)
			{
				for($i=0; $i<sizeof($pars); $i++)
				{
					$this->addParam($pars[$i]);
				}
			}
		}

		function xml_header()
		{
			return "<?xml version=\"1.0\"?" . ">\n<methodCall>\n";
		}

		function xml_footer()
		{
			return "</methodCall>\n";
		}

		function createPayload()
		{
			$this->payload=$this->xml_header();
			$this->payload.='<methodName>' . $this->methodname . "</methodName>\n";
			//	if (sizeof($this->params)) {
			$this->payload.="<params>\n";
			for($i=0; $i<sizeof($this->params); $i++)
			{
				$p=$this->params[$i];
				$this->payload.="<param>\n" . $p->serialize() .
				"</param>\n";
			}
			$this->payload.="</params>\n";
			// }
			$this->payload.=$this->xml_footer();
			//$this->payload=str_replace("\n", "\r\n", $this->payload);
		}

		function method($meth='')
		{
			if ($meth!='')
			{
				$this->methodname=$meth;
			}
			return $this->methodname;
		}

		function serialize()
		{
			$this->createPayload();
			return $this->payload;
		}

		function addParam($par) { $this->params[]=$par; }
		function getParam($i) { return $this->params[$i]; }
		function getNumParams() { return sizeof($this->params); }

		function parseResponseFile($fp)
		{
			$ipd='';
			while($data=fread($fp, 32768))
			{
				$ipd.=$data;
			}
			return $this->parseResponse($ipd);
		}

		function parseResponse($data='')
		{
			global $_xh,$xmlrpcerr,$xmlrpcstr;
			global $xmlrpc_defencoding, $xmlrpc_internalencoding;

			$hdrfnd = 0;
			if($this->debug)
			{
				//by maHo, replaced htmlspecialchars with htmlentities
				print "<PRE>---GOT---\n" . htmlentities($data) . "\n---END---\n</PRE>";
			}

			if($data == '')
			{
				error_log('No response received from server.');
				$r = new xmlrpcresp(0, $xmlrpcerr['no_data'], $xmlrpcstr['no_data']);
				return $r;
			}
			// see if we got an HTTP 200 OK, else bomb
			// but only do this if we're using the HTTP protocol.
			if(ereg("^HTTP",$data))
			{
				// Strip HTTP 1.1 100 Continue header if present
				while (ereg('^HTTP/1.1 1[0-9]{2}', $data))
				{
					$pos = strpos($data, 'HTTP', 12);
					// server sent a Continue header without any (valid) content following...
					// give the client a chance to know it
					if (!$pos && !is_int($pos)) // works fine in php 3, 4 and 5
						break;
					$data = substr($data, $pos);
				}
				if (!ereg("^HTTP/[0-9\\.]+ 200 ", $data))
				{
					$errstr= substr($data, 0, strpos($data, "\n")-1);
					error_log('HTTP error, got response: ' .$errstr);
					$r=new xmlrpcresp(0, $xmlrpcerr['http_error'], $xmlrpcstr['http_error']. ' (' . $errstr . ')');
					return $r;
				}
			}
			$parser = xml_parser_create($xmlrpc_defencoding);

			// G. Giunta 2004/04/06
			// Clean up the accumulator, or it will grow indefinitely long
			// if making xmlrpc calls for a while
			$_xh=array();
			$_xh[$parser]=array();
			$_xh[$parser]['headers'] = array();

			// separate HTTP headers from data
			if (ereg("^HTTP", $data))
			{
				// be tolerant to usage of \n instead of \r\n to separate headers and data
				// (even though it is not valid http)
				$pos = strpos($data,"\r\n\r\n");
				if($pos || is_int($pos))
					$bd = $pos+4;
				else
				{
					$pos = strpos($data,"\n\n");
					if($pos || is_int($pos))
						$bd = $pos+2;
					else
					{
						// No separation between response headers and body: fault?
						$bd = 0;
					}
				}
				// be tolerant to line endings, and extra empty lines
				$ar = split("\r?\n", trim(substr($data, 0, $pos)));
				while (list(,$line) = @each($ar))
				{
					// take care of multi-line headers
					$arr = explode(':',$line);
					if(count($arr) > 1)
					{
						$header_name = trim($arr[0]);
						// TO DO: some headers (the ones that allow a CSV list of values)
						// do allow many values to be passed using multiple header lines.
						// We should add content to $_xh[$parser]['headers'][$header_name]
						// instead of replacing it for those...
						$_xh[$parser]['headers'][$header_name] = $arr[1];
						for ($i = 2; $i < count($arr); $i++)
						{
							$_xh[$parser]['headers'][$header_name] .= ':'.$arr[$i];
						} // while
						$_xh[$parser]['headers'][$header_name] = trim($_xh[$parser]['headers'][$header_name]);
					} else if (isset($header_name))
					{
						$_xh[$parser]['headers'][$header_name] .= ' ' . trim($line);
					}
				}
				$data = substr($data, $bd);

				if ($this->debug && count($_xh[$parser]['headers']))
				{
					print '<PRE>';
					//foreach ($_xh[$parser]['headers'] as $header)
					@reset($_xh[$parser]['headers']);
					while(list($header, $value) = @each($_xh[$parser]['headers']))
					{
						print "HEADER: $header: $value\n";
					}
					print "</PRE>\n";
				}
			}

			// be tolerant of extra whitespace in response body
			$data = trim($data);

			// be tolerant of junk after methodResponse (e.g. javascript automatically inserted by free hosts)
			// idea from Luca Mariano <luca.mariano@email.it> originally in PEARified version of the lib
			$bd = false;
			$pos = strpos($data, "</methodResponse>");
			while ($pos || is_int($pos))
			{
				$bd = $pos+17;
				$pos = strpos($data, "</methodResponse>", $bd);
			}
			if ($bd)
				$data = substr($data, 0, $bd);

			$_xh[$parser]['st']='';
			$_xh[$parser]['cm']=0;
			$_xh[$parser]['isf']=0;
			$_xh[$parser]['ac']='';
			$_xh[$parser]['qt']='';

			xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
			// G. Giunta 2005/02/13: PHP internally uses ISO-8859-1, so we have to tell
			// the xml parser to give us back data in the expected charset
			xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $xmlrpc_internalencoding);

			xml_set_element_handler($parser, 'xmlrpc_se', 'xmlrpc_ee');
			xml_set_character_data_handler($parser, 'xmlrpc_cd');
			xml_set_default_handler($parser, 'xmlrpc_dh');
			//$xmlrpc_value=new xmlrpcval;

			if (!xml_parse($parser, $data, sizeof($data)))
			{
				// thanks to Peter Kocks <peter.kocks@baygate.com>
				if((xml_get_current_line_number($parser)) == 1)
				{
					$errstr = 'XML error at line 1, check URL';
				}
				else
				{
					$errstr = sprintf('XML error: %s at line %d',
						xml_error_string(xml_get_error_code($parser)),
						xml_get_current_line_number($parser));
				}
				error_log($errstr);
				$r=new xmlrpcresp(0, $xmlrpcerr['invalid_return'], $xmlrpcstr['invalid_return'].' ('.$errstr.')');
				xml_parser_free($parser);
				if ($this->debug)
					echo $errstr;
				$r->hdrs = $_xh[$parser]['headers'];
				return $r;
			}
			xml_parser_free($parser);
			if ($this->debug)
			{
				print "<PRE>---EVALING---[" .
				strlen($_xh[$parser]['st']) . " chars]---\n" .
				htmlspecialchars($_xh[$parser]['st']) . ";\n---END---</PRE>";
			}
			if (strlen($_xh[$parser]['st'])==0)
			{
				// then something odd has happened
				// and it's time to generate a client side error
				// indicating something odd went on
				$r=new xmlrpcresp(0, $xmlrpcerr['invalid_return'],
				$xmlrpcstr['invalid_return']);
			}
			else
			{
				$allOK=0;
				@eval('$v=' . $_xh[$parser]['st'] . '; $allOK=1;');
				if (!$allOK)
				{
					$r = new xmlrpcresp(0, $xmlrpcerr['invalid_return'], $xmlrpcstr['invalid_return']);
				}
				else
				if ($_xh[$parser]['isf'])
				{
					$errno_v = $v->structmem('faultCode');
					$errstr_v = $v->structmem('faultString');
					$errno = $errno_v->scalarval();

					if ($errno == 0)
					{
						// FAULT returned, errno needs to reflect that
						$errno = -1;
					}

					$r = new xmlrpcresp($v, $errno, $errstr_v->scalarval());
				}
				else
				{
					$r=new xmlrpcresp($v);
				}
			}

			$r->hdrs = $_xh[$parser]['headers'];
			return $r;
		}
	}

	class xmlrpcval
	{
		var $me=array();
		var $mytype=0;

		function xmlrpcval($val=-1, $type='')
		{
			global $xmlrpcTypes;
			$this->me=array();
			$this->mytype=0;
			if ($val!=-1 || !is_int($val) || $type!='')
			{
				if ($type=='')
				{
					$type='string';
				}
				if ($xmlrpcTypes[$type]==1)
				{
					$this->addScalar($val,$type);
				}
				elseif ($xmlrpcTypes[$type]==2)
				{
					$this->addArray($val);
				}
				elseif ($xmlrpcTypes[$type]==3)
				{
					$this->addStruct($val);
				}
			}
		}

		function addScalar($val, $type='string')
		{
			global $xmlrpcTypes, $xmlrpcBoolean;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -