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

📄 class-xmlrpc.php

📁 在综合英文版XOOPS 2.09, 2.091, 2.092 的基础上正式发布XOOPS 2.09中文版 XOOPS 2.09x 版主要是PHP5升级、bug修正和安全补正: 1 全面兼容PHP 5.
💻 PHP
📖 第 1 页 / 共 3 页
字号:
  var $errstring;
  var $debug=0;
  var $username="";
  var $password="";
  var $cert="";
  var $certpass="";
  
  function xmlrpc_client($path, $server, $port=0) {
    $this->port=$port; $this->server=$server; $this->path=$path;
  }

  function setDebug($in) {
		if ($in) { 
			$this->debug=1;
		} else {
			$this->debug=0;
		}
  }

  function setCredentials($u, $p) {
    $this->username=$u;
    $this->password=$p;
  }

  function setCertificate($cert, $certpass) {
    $this->cert = $cert;
    $this->certpass = $certpass;
  }

  function send($msg, $timeout=0, $method='http') {
    // where msg is an xmlrpcmsg
    $msg->debug=$this->debug;
 
    if ($method == 'https') {
      return $this->sendPayloadHTTPS($msg,
				     $this->server,
				     $this->port, $timeout,
				     $this->username, $this->password,
				     $this->cert,
				     $this->certpass);
    } else {
      return $this->sendPayloadHTTP10($msg, $this->server, $this->port,
				      $timeout, $this->username, 
				      $this->password);
    }
  }

  function sendPayloadHTTP10($msg, $server, $port, $timeout=0,
			     $username="", $password="") {
    if ($port==0) $port=80;
    if($timeout>0)
      $fp=@fsockopen($server, $port,
		    $this->errno, $this->errstr, $timeout);
#      $fp=@fsockopen($server, $port,
#		    &$this->errno, &$this->errstr, $timeout);
    else
      $fp=@fsockopen($server, $port,
		    $this->errno, $this->errstr);
#      $fp=@fsockopen($server, $port,
#		    &$this->errno, &$this->errstr);
    if (!$fp) {   
      return 0;
    }
    // Only create the payload if it was not created previously
    if(empty($msg->payload)) $msg->createPayload();
    
    // thanks to Grant Rauscher <grant7@firstworld.net>
    // for this
    $credentials="";
    if ($username!="") {
      $credentials="Authorization: Basic " .
	base64_encode($username . ":" . $password) . "\r\n";
    }
    
    $op= "POST " . $this->path. " HTTP/1.0\r\nUser-Agent: PHP XMLRPC 1.0\r\n" .
      "Host: ". $this->server  . "\r\n" .
      $credentials . 
      "Content-Type: text/xml\r\nContent-Length: " .
      strlen($msg->payload) . "\r\n\r\n" .
      $msg->payload;
	socket_set_timeout($fp, 10);    
    if (!fputs($fp, $op, strlen($op))) {
      $this->errstr="Write error";
      return 0;
    }
    $resp=$msg->parseResponseFile($fp);
    fclose($fp);
    return $resp;
  }

  // contributed by Justin Miller <justin@voxel.net>
  // requires curl to be built into PHP
  function sendPayloadHTTPS($msg, $server, $port, $timeout=0,
			    $username="", $password="", $cert="",
			    $certpass="") {
    global $xmlrpcerr, $xmlrpcstr;
    if ($port == 0) $port = 443;
    
    // Only create the payload if it was not created previously
    if(empty($msg->payload)) $msg->createPayload();
    
    if (!function_exists("curl_init")) {
      $r=new xmlrpcresp(0, $xmlrpcerr["no_ssl"],
			$xmlrpcstr["no_ssl"]);
      return $r;
    }

    $curl = curl_init("https://" . $server . ':' . $port .
		      $this->path);
    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // results into variable
    if ($this->debug) {
      curl_setopt($curl, CURLOPT_VERBOSE, 1);
    }
    curl_setopt($curl, CURLOPT_USERAGENT, 'PHP XMLRPC 1.0');
    // required for XMLRPC
    curl_setopt($curl, CURLOPT_POST, 1);
    // post the data
    curl_setopt($curl, CURLOPT_POSTFIELDS, $msg->payload);
    // the data
    curl_setopt($curl, CURLOPT_HEADER, 1);
    // return the header too
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
    // required for XMLRPC
    if ($timeout) curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 :
			      $timeout - 1);
    // timeout is borked
    if ($username && $password) curl_setopt($curl, CURLOPT_USERPWD,
					    "$username:$password"); 
    // set auth stuff
    if ($cert) curl_setopt($curl, CURLOPT_SSLCERT, $cert);
    // set cert file
    if ($certpass) curl_setopt($curl, CURLOPT_SSLCERTPASSWD,
    		       $certpass);                    
    // set cert password
    
    $result = curl_exec($curl);
    
    if (!$result) {
      $resp=new xmlrpcresp(0, 
			   $xmlrpcerr["curl_fail"],
			   $xmlrpcstr["curl_fail"]. ": ". 
			   curl_error($curl));
    } else {
      $resp = $msg->parseResponse($result);
    }
    curl_close($curl);
    return $resp;
  }

} // end class xmlrpc_client

class xmlrpcresp {
  var $xv;
  var $fn;
  var $fs;
  var $hdrs;

  function xmlrpcresp($val, $fcode=0, $fstr="") {
    if ($fcode!=0) {
      $this->xv=0;
      $this->fn=$fcode;
      $this->fs=trim(htmlspecialchars($fstr));
	  logIO("O",$this->fs);
    } else {
      $this->xv=$val;
      $this->fn=0;
    }
  }

  function faultCode() { 
		if (isset($this->fn)) 
			return $this->fn;
		else
			return 0; 
	}

  function faultString() { return $this->fs; }
  function value() { return $this->xv; }

  function serialize() { 
	$rs="<methodResponse>";
	if ($this->fn) {
	  $rs.="<fault>
  <value>
    <struct>
      <member>
        <name>faultCode</name>
        <value><int>" . $this->fn . "</int></value>
      </member>
      <member>
        <name>faultString</name>
        <value><string>" . $this->fs . "</string></value>
      </member>
    </struct>
  </value>
</fault>";
	} else {
	  $rs.="<params><param>" . $this->xv->serialize() . 
		"</param></params>";
	}
	$rs.="</methodResponse>";

	/* begin Logging
	$f=fopen("xmlrpc/xmlrpc.log","a+");
	fwrite($f, date("Ymd H:i:s")."\n\nResponse:\n\n".$rs);
	fclose($f);
	end Logging */
	logIO("O",$rs);

	return $rs;
  }
}

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();
		logIO("O",$this->payload);
		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;

	
	$parser = xml_parser_create($xmlrpc_defencoding);

	$_xh[$parser]=array();

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

	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");
	$xmlrpc_value=new xmlrpcval;

	if ($this->debug)
	  print "<PRE>---GOT---\n" . htmlspecialchars($data) . 
		"\n---END---\n</PRE>";
	if ($data=="") {
	  error_log("No response received from server.");
	  $r=new xmlrpcresp(0, $xmlrpcerr["no_data"],
			    $xmlrpcstr["no_data"]);
	  xml_parser_free($parser);
	  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) && 
			!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 . ")");
		xml_parser_free($parser);
		return $r;
	}

	// if using HTTP, then gotta get rid of HTTP headers here
	// and we store them in the 'ha' bit of our data array
	if (ereg("^HTTP", $data)) {
		$ar=explode("\r\n", $data);
		$newdata="";
		$hdrfnd=0;
		for ($i=0; $i<sizeof($ar); $i++) {
			if (!$hdrfnd) {
				if (strlen($ar[$i])>0) {
					$_xh[$parser]['ha'].=$ar[$i]. "\r\n";
				} else {
					$hdrfnd=1; 
				}
			} else {
				$newdata.=$ar[$i] . "\r\n";
			}
		}
		$data=$newdata;
	}
	
	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"]);
		xml_parser_free($parser);
		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 {
	  eval('$v=' . $_xh[$parser]['st'] . '; $allOK=1;');
	  if ($_xh[$parser]['isf']) {
		$f=$v->structmem("faultCode");
		$fs=$v->structmem("faultString");
		$r=new xmlrpcresp($v, $f->scalarval(), 
						  $fs->scalarval());
	  } else {
		$r=new xmlrpcresp($v);
	  }
	}
	$r->hdrs=split("\r?\n", $_xh[$parser]['ha']);
	return $r;
  }

⌨️ 快捷键说明

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