server.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 803 行 · 第 1/2 页

PHP
803
字号
<?php
/**
 * Demo server for xmlrpc library.
 *
 * Implements a lot of webservices, including a suite of services used for
 * interoperability testing (validator1 methods), and some whose only purpose
 * is to be used for unit-testing the library.
 *
 * Please do not copy this file verbatim into your production server.
 *
 * @version $Id: server.php,v 1.21 2006/08/21 09:24:10 ggiunta Exp $
 **/

// give user a chance to see the source for this server instead of running the services
if ($_SERVER['REQUEST_METHOD'] != 'POST' && isset($_GET['showSource']))
{
	highlight_file(__FILE__);
	die();
}

	include("xmlrpc.inc");
	include("xmlrpcs.inc");
	include("xmlrpc_wrappers.inc");

	/**
	* Used to test usage of object methods in dispatch maps
	*/
	class xmlrpc_server_methods_container
	{
		/*
		* Method used to test logging of php warnings generated by user functions.
		*/
		function phpwarninggenerator($m)
		{
			$a = $b; // this triggers a warning in E_ALL mode, since $b is undefined
			return new xmlrpcresp(new xmlrpcval(1, 'boolean'));
		}
	}


	// a PHP version
	// of the state-number server
	// send me an integer and i'll sell you a state

	$stateNames = array(
		"Alabama", "Alaska", "Arizona", "Arkansas", "California",
		"Colorado", "Columbia", "Connecticut", "Delaware", "Florida",
		"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
		"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan",
		"Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
		"New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina",
		"North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
		"South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
		"Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"
	);

	$findstate_sig=array(array($xmlrpcString, $xmlrpcInt));
	$findstate_doc='When passed an integer between 1 and 51 returns the
name of a US state, where the integer is the index of that state name
in an alphabetic order.';


	function findstate($m)
	{
		global $xmlrpcerruser, $stateNames;
		$err="";
		// get the first param
		$sno=$m->getParam(0);

		// param must be there and of the correct type: server object does the
		// validation for us

		// extract the value of the state number
		$snv=$sno->scalarval();
		// look it up in our array (zero-based)
		if (isset($stateNames[$snv-1]))
		{
			$sname=$stateNames[$snv-1];
		}
		else
		{
			// not, there so complain
			$err="I don't have a state for the index '" . $snv . "'";
		}

		// if we generated an error, create an error return response
		if ($err)
		{
			return new xmlrpcresp(0, $xmlrpcerruser, $err);
		}
		else
		{
			// otherwise, we create the right response
			// with the state name
			return new xmlrpcresp(new xmlrpcval($sname));
		}
	}

	/**
	* Inner code of the state-number server.
	* Used to test auto-registration of PHP funcions as xmlrpc methods.
	* @param integer $stateno the state number
	* @return string the name of the state (or error descrption)
	*/
	function inner_findstate($stateno)
	{
		global $stateNames;
		if (isset($stateNames[$stateno-1]))
		{
			return $stateNames[$stateno-1];
		}
		else
		{
			// not, there so complain
			return "I don't have a state for the index '" . $stateno . "'";
		}
	}
	$findstate2_sig = wrap_php_function('inner_findstate');

	$addtwo_sig=array(array($xmlrpcInt, $xmlrpcInt, $xmlrpcInt));
	$addtwo_doc='Add two integers together and return the result';
	function addtwo($m)
	{
		$s=$m->getParam(0);
		$t=$m->getParam(1);
		return new xmlrpcresp(new xmlrpcval($s->scalarval()+$t->scalarval(),"int"));
	}

	$addtwodouble_sig=array(array($xmlrpcDouble, $xmlrpcDouble, $xmlrpcDouble));
	$addtwodouble_doc='Add two doubles together and return the result';
	function addtwodouble($m)
	{
		$s=$m->getParam(0);
		$t=$m->getParam(1);
		return new xmlrpcresp(new xmlrpcval($s->scalarval()+$t->scalarval(),"double"));
	}

	$stringecho_sig=array(array($xmlrpcString, $xmlrpcString));
	$stringecho_doc='Accepts a string parameter, returns the string.';
	function stringecho($m)
	{
		// just sends back a string
		$s=$m->getParam(0);
		$v = $s->scalarval();
		return new xmlrpcresp(new xmlrpcval($s->scalarval()));
	}

	$echoback_sig=array(array($xmlrpcString, $xmlrpcString));
	$echoback_doc='Accepts a string parameter, returns the entire incoming payload';
	function echoback($m)
	{
		// just sends back a string with what i got
		// sent to me, just escaped, that's all
		//
		// $m is an incoming message
		$s="I got the following message:\n" . $m->serialize();
		return new xmlrpcresp(new xmlrpcval($s));
	}

	$echosixtyfour_sig=array(array($xmlrpcString, $xmlrpcBase64));
	$echosixtyfour_doc='Accepts a base64 parameter and returns it decoded as a string';
	function echosixtyfour($m)
	{
		// accepts an encoded value, but sends it back
		// as a normal string. this is to test base64 encoding
		// is working as expected
		$incoming=$m->getParam(0);
		return new xmlrpcresp(new xmlrpcval($incoming->scalarval(), "string"));
	}

	$bitflipper_sig=array(array($xmlrpcArray, $xmlrpcArray));
	$bitflipper_doc='Accepts an array of booleans, and returns them inverted';
	function bitflipper($m)
	{
		global $xmlrpcArray;

		$v=$m->getParam(0);
		$sz=$v->arraysize();
		$rv=new xmlrpcval(array(), $xmlrpcArray);

		for($j=0; $j<$sz; $j++)
		{
			$b=$v->arraymem($j);
			if ($b->scalarval())
			{
				$rv->addScalar(false, "boolean");
			}
			else
			{
				$rv->addScalar(true, "boolean");
			}
		}

		return new xmlrpcresp($rv);
	}

	// Sorting demo
	//
	// send me an array of structs thus:
	//
	// Dave 35
	// Edd  45
	// Fred 23
	// Barney 37
	//
	// and I'll return it to you in sorted order

	function agesorter_compare($a, $b)
	{
		global $agesorter_arr;

		// don't even ask me _why_ these come padded with
		// hyphens, I couldn't tell you :p
		$a=ereg_replace("-", "", $a);
		$b=ereg_replace("-", "", $b);

		if ($agesorter_arr[$a]==$agesorter[$b])
		{
			return 0;
		}
		return ($agesorter_arr[$a] > $agesorter_arr[$b]) ? -1 : 1;
	}

	$agesorter_sig=array(array($xmlrpcArray, $xmlrpcArray));
	$agesorter_doc='Send this method an array of [string, int] structs, eg:
<pre>
 Dave   35
 Edd    45
 Fred   23
 Barney 37
</pre>
And the array will be returned with the entries sorted by their numbers.
';
	function agesorter($m)
	{
		global $agesorter_arr, $xmlrpcerruser, $s;

		xmlrpc_debugmsg("Entering 'agesorter'");
		// get the parameter
		$sno=$m->getParam(0);
		// error string for [if|when] things go wrong
		$err="";
		// create the output value
		$v=new xmlrpcval();
		$agar=array();

		if (isset($sno) && $sno->kindOf()=="array")
		{
			$max=$sno->arraysize();
			// TODO: create debug method to print can work once more
			// print "<!-- found $max array elements -->\n";
			for($i=0; $i<$max; $i++)
			{
				$rec=$sno->arraymem($i);
				if ($rec->kindOf()!="struct")
				{
					$err="Found non-struct in array at element $i";
					break;
				}
				// extract name and age from struct
				$n=$rec->structmem("name");
				$a=$rec->structmem("age");
				// $n and $a are xmlrpcvals,
				// so get the scalarval from them
				$agar[$n->scalarval()]=$a->scalarval();
			}

			$agesorter_arr=$agar;
			// hack, must make global as uksort() won't
			// allow us to pass any other auxilliary information
			uksort($agesorter_arr, agesorter_compare);
			$outAr=array();
			while (list( $key, $val ) = each( $agesorter_arr ) )
			{
				// recreate each struct element
				$outAr[]=new xmlrpcval(array("name" =>
				new xmlrpcval($key),
				"age" =>
				new xmlrpcval($val, "int")), "struct");
			}
			// add this array to the output value
			$v->addArray($outAr);
		}
		else
		{
			$err="Must be one parameter, an array of structs";
		}

		if ($err)
		{
			return new xmlrpcresp(0, $xmlrpcerruser, $err);
		}
		else
		{
			return new xmlrpcresp($v);
		}
	}

	// signature and instructions, place these in the dispatch
	// map
	$mail_send_sig=array(array(
		$xmlrpcBoolean, $xmlrpcString, $xmlrpcString,
		$xmlrpcString, $xmlrpcString, $xmlrpcString,
		$xmlrpcString, $xmlrpcString
	));

	$mail_send_doc='mail.send(recipient, subject, text, sender, cc, bcc, mimetype)<br/>
recipient, cc, and bcc are strings, comma-separated lists of email addresses, as described above.<br/>
subject is a string, the subject of the message.<br/>
sender is a string, it\'s the email address of the person sending the message. This string can not be
a comma-separated list, it must contain a single email address only.<br/>
text is a string, it contains the body of the message.<br/>
mimetype, a string, is a standard MIME type, for example, text/plain.
';
	// WARNING; this functionality depends on the sendmail -t option
	// it may not work with Windows machines properly; particularly
	// the Bcc option. Sneak on your friends at your own risk!
	function mail_send($m)
	{
		global $xmlrpcerruser, $xmlrpcBoolean;
		$err="";

		$mTo=$m->getParam(0);
		$mSub=$m->getParam(1);
		$mBody=$m->getParam(2);
		$mFrom=$m->getParam(3);
		$mCc=$m->getParam(4);
		$mBcc=$m->getParam(5);
		$mMime=$m->getParam(6);

		if ($mTo->scalarval()=="")
		{
			$err="Error, no 'To' field specified";
		}

		if ($mFrom->scalarval()=="")
		{
			$err="Error, no 'From' field specified";
		}

		$msghdr="From: " . $mFrom->scalarval() . "\n";
		$msghdr.="To: ". $mTo->scalarval() . "\n";

		if ($mCc->scalarval()!="")
		{
			$msghdr.="Cc: " . $mCc->scalarval(). "\n";
		}
		if ($mBcc->scalarval()!="")
		{
			$msghdr.="Bcc: " . $mBcc->scalarval(). "\n";
		}
		if ($mMime->scalarval()!="")
		{
			$msghdr.="Content-type: " . $mMime->scalarval() . "\n";
		}
		$msghdr.="X-Mailer: XML-RPC for PHP mailer 1.0";

		if ($err=="")
		{
			if (!mail("",
				$mSub->scalarval(),
				$mBody->scalarval(),
				$msghdr))
			{
				$err="Error, could not send the mail.";
			}
		}

		if ($err)
		{
			return new xmlrpcresp(0, $xmlrpcerruser, $err);
		}
		else
		{
			return new xmlrpcresp(new xmlrpcval("true", $xmlrpcBoolean));
		}
	}

	$getallheaders_sig=array(array($xmlrpcStruct));
	$getallheaders_doc='Returns a struct containing all the HTTP headers received with the request. Provides limited functionality with IIS';
	function getallheaders_xmlrpc($m)
	{
		global $xmlrpcerruser;
		if (function_exists('getallheaders'))
		{
			return new xmlrpcresp(php_xmlrpc_encode(getallheaders()));
		}
		else
		{
			$headers = array();
			// IIS: poor man's version of getallheaders
			foreach ($_SERVER as $key => $val)
				if (strpos($key, 'HTTP_') === 0)
				{
					$key = ucfirst(str_replace('_', '-', strtolower(substr($key, 5))));
					$headers[$key] = $val;
				}
			return new xmlrpcresp(php_xmlrpc_encode($headers));
		}
	}

	$setcookies_sig=array(array($xmlrpcInt, $xmlrpcStruct));

⌨️ 快捷键说明

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