2
0

Server.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_XmlRpc
  17. * @subpackage Server
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Extends Zend_Server_Abstract
  24. */
  25. require_once 'Zend/Server/Abstract.php';
  26. /**
  27. * XMLRPC Request
  28. */
  29. require_once 'Zend/XmlRpc/Request.php';
  30. /**
  31. * XMLRPC Response
  32. */
  33. require_once 'Zend/XmlRpc/Response.php';
  34. /**
  35. * XMLRPC HTTP Response
  36. */
  37. require_once 'Zend/XmlRpc/Response/Http.php';
  38. /**
  39. * XMLRPC server fault class
  40. */
  41. require_once 'Zend/XmlRpc/Server/Fault.php';
  42. /**
  43. * XMLRPC server system methods class
  44. */
  45. require_once 'Zend/XmlRpc/Server/System.php';
  46. /**
  47. * Convert PHP to and from xmlrpc native types
  48. */
  49. require_once 'Zend/XmlRpc/Value.php';
  50. /**
  51. * Reflection API for function/method introspection
  52. */
  53. require_once 'Zend/Server/Reflection.php';
  54. /**
  55. * Zend_Server_Reflection_Function_Abstract
  56. */
  57. require_once 'Zend/Server/Reflection/Function/Abstract.php';
  58. /**
  59. * Specifically grab the Zend_Server_Reflection_Method for manually setting up
  60. * system.* methods and handling callbacks in {@link loadFunctions()}.
  61. */
  62. require_once 'Zend/Server/Reflection/Method.php';
  63. /**
  64. * An XML-RPC server implementation
  65. *
  66. * Example:
  67. * <code>
  68. * require_once 'Zend/XmlRpc/Server.php';
  69. * require_once 'Zend/XmlRpc/Server/Cache.php';
  70. * require_once 'Zend/XmlRpc/Server/Fault.php';
  71. * require_once 'My/Exception.php';
  72. * require_once 'My/Fault/Observer.php';
  73. *
  74. * // Instantiate server
  75. * $server = new Zend_XmlRpc_Server();
  76. *
  77. * // Allow some exceptions to report as fault responses:
  78. * Zend_XmlRpc_Server_Fault::attachFaultException('My_Exception');
  79. * Zend_XmlRpc_Server_Fault::attachObserver('My_Fault_Observer');
  80. *
  81. * // Get or build dispatch table:
  82. * if (!Zend_XmlRpc_Server_Cache::get($filename, $server)) {
  83. * require_once 'Some/Service/Class.php';
  84. * require_once 'Another/Service/Class.php';
  85. *
  86. * // Attach Some_Service_Class in 'some' namespace
  87. * $server->setClass('Some_Service_Class', 'some');
  88. *
  89. * // Attach Another_Service_Class in 'another' namespace
  90. * $server->setClass('Another_Service_Class', 'another');
  91. *
  92. * // Create dispatch table cache file
  93. * Zend_XmlRpc_Server_Cache::save($filename, $server);
  94. * }
  95. *
  96. * $response = $server->handle();
  97. * echo $response;
  98. * </code>
  99. *
  100. * @category Zend
  101. * @package Zend_XmlRpc
  102. * @subpackage Server
  103. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  104. * @license http://framework.zend.com/license/new-bsd New BSD License
  105. */
  106. class Zend_XmlRpc_Server extends Zend_Server_Abstract
  107. {
  108. /**
  109. * Character encoding
  110. * @var string
  111. */
  112. protected $_encoding = 'UTF-8';
  113. /**
  114. * Request processed
  115. * @var null|Zend_XmlRpc_Request
  116. */
  117. protected $_request = null;
  118. /**
  119. * Class to use for responses; defaults to {@link Zend_XmlRpc_Response_Http}
  120. * @var string
  121. */
  122. protected $_responseClass = 'Zend_XmlRpc_Response_Http';
  123. /**
  124. * Dispatch table of name => method pairs
  125. * @var Zend_XmlRpc_Server_ServerDefinition
  126. */
  127. protected $_table;
  128. /**
  129. * PHP types => XML-RPC types
  130. * @var array
  131. */
  132. protected $_typeMap = array(
  133. 'i4' => 'i4',
  134. 'int' => 'int',
  135. 'integer' => 'int',
  136. 'double' => 'double',
  137. 'float' => 'double',
  138. 'real' => 'double',
  139. 'boolean' => 'boolean',
  140. 'bool' => 'boolean',
  141. 'true' => 'boolean',
  142. 'false' => 'boolean',
  143. 'string' => 'string',
  144. 'str' => 'string',
  145. 'base64' => 'base64',
  146. 'dateTime.iso8601' => 'dateTime.iso8601',
  147. 'date' => 'dateTime.iso8601',
  148. 'time' => 'dateTime.iso8601',
  149. 'time' => 'dateTime.iso8601',
  150. 'array' => 'array',
  151. 'struct' => 'struct',
  152. 'null' => 'nil',
  153. 'nil' => 'nil',
  154. 'void' => 'void',
  155. 'mixed' => 'struct'
  156. );
  157. /**
  158. * Constructor
  159. *
  160. * Creates system.* methods.
  161. *
  162. * @return void
  163. */
  164. public function __construct()
  165. {
  166. $this->_table = new Zend_Server_Definition();
  167. $this->_registerSystemMethods();
  168. }
  169. /**
  170. * Proxy calls to system object
  171. *
  172. * @param string $method
  173. * @param array $params
  174. * @return mixed
  175. * @throws Zend_XmlRpc_Server_Exception
  176. */
  177. public function __call($method, $params)
  178. {
  179. $system = $this->getSystem();
  180. if (!method_exists($system, $method)) {
  181. require_once 'Zend/XmlRpc/Server/Exception.php';
  182. throw new Zend_XmlRpc_Server_Exception('Unknown instance method called on server: ' . $method);
  183. }
  184. return call_user_func_array(array($system, $method), $params);
  185. }
  186. /**
  187. * Attach a callback as an XMLRPC method
  188. *
  189. * Attaches a callback as an XMLRPC method, prefixing the XMLRPC method name
  190. * with $namespace, if provided. Reflection is done on the callback's
  191. * docblock to create the methodHelp for the XMLRPC method.
  192. *
  193. * Additional arguments to pass to the function at dispatch may be passed;
  194. * any arguments following the namespace will be aggregated and passed at
  195. * dispatch time.
  196. *
  197. * @param string|array $function Valid callback
  198. * @param string $namespace Optional namespace prefix
  199. * @return void
  200. * @throws Zend_XmlRpc_Server_Exception
  201. */
  202. public function addFunction($function, $namespace = '')
  203. {
  204. if (!is_string($function) && !is_array($function)) {
  205. require_once 'Zend/XmlRpc/Server/Exception.php';
  206. throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611);
  207. }
  208. $argv = null;
  209. if (2 < func_num_args()) {
  210. $argv = func_get_args();
  211. $argv = array_slice($argv, 2);
  212. }
  213. $function = (array) $function;
  214. foreach ($function as $func) {
  215. if (!is_string($func) || !function_exists($func)) {
  216. require_once 'Zend/XmlRpc/Server/Exception.php';
  217. throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611);
  218. }
  219. $reflection = Zend_Server_Reflection::reflectFunction($func, $argv, $namespace);
  220. $this->_buildSignature($reflection);
  221. }
  222. }
  223. /**
  224. * Attach class methods as XMLRPC method handlers
  225. *
  226. * $class may be either a class name or an object. Reflection is done on the
  227. * class or object to determine the available public methods, and each is
  228. * attached to the server as an available method; if a $namespace has been
  229. * provided, that namespace is used to prefix the XMLRPC method names.
  230. *
  231. * Any additional arguments beyond $namespace will be passed to a method at
  232. * invocation.
  233. *
  234. * @param string|object $class
  235. * @param string $namespace Optional
  236. * @param mixed $argv Optional arguments to pass to methods
  237. * @return void
  238. * @throws Zend_XmlRpc_Server_Exception on invalid input
  239. */
  240. public function setClass($class, $namespace = '', $argv = null)
  241. {
  242. if (is_string($class) && !class_exists($class)) {
  243. if (!class_exists($class)) {
  244. require_once 'Zend/XmlRpc/Server/Exception.php';
  245. throw new Zend_XmlRpc_Server_Exception('Invalid method class', 610);
  246. }
  247. }
  248. $argv = null;
  249. if (3 < func_num_args()) {
  250. $argv = func_get_args();
  251. $argv = array_slice($argv, 3);
  252. }
  253. $dispatchable = Zend_Server_Reflection::reflectClass($class, $argv, $namespace);
  254. foreach ($dispatchable->getMethods() as $reflection) {
  255. $this->_buildSignature($reflection, $class);
  256. }
  257. }
  258. /**
  259. * Raise an xmlrpc server fault
  260. *
  261. * @param string|Exception $fault
  262. * @param int $code
  263. * @return Zend_XmlRpc_Server_Fault
  264. */
  265. public function fault($fault = null, $code = 404)
  266. {
  267. if (!$fault instanceof Exception) {
  268. $fault = (string) $fault;
  269. if (empty($fault)) {
  270. $fault = 'Unknown error';
  271. }
  272. require_once 'Zend/XmlRpc/Server/Exception.php';
  273. $fault = new Zend_XmlRpc_Server_Exception($fault, $code);
  274. }
  275. return Zend_XmlRpc_Server_Fault::getInstance($fault);
  276. }
  277. /**
  278. * Handle an xmlrpc call
  279. *
  280. * @param Zend_XmlRpc_Request $request Optional
  281. * @return Zend_XmlRpc_Response|Zend_XmlRpc_Fault
  282. */
  283. public function handle($request = false)
  284. {
  285. // Get request
  286. if ((!$request || !$request instanceof Zend_XmlRpc_Request)
  287. && (null === ($request = $this->getRequest()))
  288. ) {
  289. require_once 'Zend/XmlRpc/Request/Http.php';
  290. $request = new Zend_XmlRpc_Request_Http();
  291. $request->setEncoding($this->getEncoding());
  292. }
  293. $this->setRequest($request);
  294. if ($request->isFault()) {
  295. $response = $request->getFault();
  296. } else {
  297. try {
  298. $response = $this->_handle($request);
  299. } catch (Exception $e) {
  300. $response = $this->fault($e);
  301. }
  302. }
  303. // Set output encoding
  304. $response->setEncoding($this->getEncoding());
  305. return $response;
  306. }
  307. /**
  308. * Load methods as returned from {@link getFunctions}
  309. *
  310. * Typically, you will not use this method; it will be called using the
  311. * results pulled from {@link Zend_XmlRpc_Server_Cache::get()}.
  312. *
  313. * @param array|Zend_Server_Definition $definition
  314. * @return void
  315. * @throws Zend_XmlRpc_Server_Exception on invalid input
  316. */
  317. public function loadFunctions($definition)
  318. {
  319. if (!is_array($definition) && (!$definition instanceof Zend_Server_Definition)) {
  320. if (is_object($definition)) {
  321. $type = get_class($definition);
  322. } else {
  323. $type = gettype($definition);
  324. }
  325. require_once 'Zend/XmlRpc/Server/Exception.php';
  326. throw new Zend_XmlRpc_Server_Exception('Unable to load server definition; must be an array or Zend_Server_Definition, received ' . $type, 612);
  327. }
  328. $this->_table->clearMethods();
  329. $this->_registerSystemMethods();
  330. if ($definition instanceof Zend_Server_Definition) {
  331. $definition = $definition->getMethods();
  332. }
  333. foreach ($definition as $key => $method) {
  334. if ('system.' == substr($key, 0, 7)) {
  335. continue;
  336. }
  337. $this->_table->addMethod($method, $key);
  338. }
  339. }
  340. /**
  341. * Set encoding
  342. *
  343. * @param string $encoding
  344. * @return Zend_XmlRpc_Server
  345. */
  346. public function setEncoding($encoding)
  347. {
  348. $this->_encoding = $encoding;
  349. return $this;
  350. }
  351. /**
  352. * Retrieve current encoding
  353. *
  354. * @return string
  355. */
  356. public function getEncoding()
  357. {
  358. return $this->_encoding;
  359. }
  360. /**
  361. * Do nothing; persistence is handled via {@link Zend_XmlRpc_Server_Cache}
  362. *
  363. * @param mixed $mode
  364. * @return void
  365. */
  366. public function setPersistence($mode)
  367. {
  368. }
  369. /**
  370. * Set the request object
  371. *
  372. * @param string|Zend_XmlRpc_Request $request
  373. * @return Zend_XmlRpc_Server
  374. * @throws Zend_XmlRpc_Server_Exception on invalid request class or object
  375. */
  376. public function setRequest($request)
  377. {
  378. if (is_string($request) && class_exists($request)) {
  379. $request = new $request();
  380. if (!$request instanceof Zend_XmlRpc_Request) {
  381. require_once 'Zend/XmlRpc/Server/Exception.php';
  382. throw new Zend_XmlRpc_Server_Exception('Invalid request class');
  383. }
  384. $request->setEncoding($this->getEncoding());
  385. } elseif (!$request instanceof Zend_XmlRpc_Request) {
  386. require_once 'Zend/XmlRpc/Server/Exception.php';
  387. throw new Zend_XmlRpc_Server_Exception('Invalid request object');
  388. }
  389. $this->_request = $request;
  390. return $this;
  391. }
  392. /**
  393. * Return currently registered request object
  394. *
  395. * @return null|Zend_XmlRpc_Request
  396. */
  397. public function getRequest()
  398. {
  399. return $this->_request;
  400. }
  401. /**
  402. * Set the class to use for the response
  403. *
  404. * @param string $class
  405. * @return boolean True if class was set, false if not
  406. */
  407. public function setResponseClass($class)
  408. {
  409. if (class_exists($class)) {
  410. $reflection = new ReflectionClass($class);
  411. if ($reflection->isSubclassOf(new ReflectionClass('Zend_XmlRpc_Response'))) {
  412. $this->_responseClass = $class;
  413. return true;
  414. }
  415. }
  416. return false;
  417. }
  418. /**
  419. * Retrieve current response class
  420. *
  421. * @return string
  422. */
  423. public function getResponseClass()
  424. {
  425. return $this->_responseClass;
  426. }
  427. /**
  428. * Retrieve dispatch table
  429. *
  430. * @return array
  431. */
  432. public function getDispatchTable()
  433. {
  434. return $this->_table;
  435. }
  436. /**
  437. * Returns a list of registered methods
  438. *
  439. * Returns an array of dispatchables (Zend_Server_Reflection_Function,
  440. * _Method, and _Class items).
  441. *
  442. * @return array
  443. */
  444. public function getFunctions()
  445. {
  446. return $this->_table->toArray();
  447. }
  448. /**
  449. * Retrieve system object
  450. *
  451. * @return Zend_XmlRpc_Server_System
  452. */
  453. public function getSystem()
  454. {
  455. return $this->_system;
  456. }
  457. /**
  458. * Map PHP type to XML-RPC type
  459. *
  460. * @param string $type
  461. * @return string
  462. */
  463. protected function _fixType($type)
  464. {
  465. if (isset($this->_typeMap[$type])) {
  466. return $this->_typeMap[$type];
  467. }
  468. return 'void';
  469. }
  470. /**
  471. * Handle an xmlrpc call (actual work)
  472. *
  473. * @param Zend_XmlRpc_Request $request
  474. * @return Zend_XmlRpc_Response
  475. * @throws Zend_XmlRpcServer_Exception|Exception
  476. * Zend_XmlRpcServer_Exceptions are thrown for internal errors; otherwise,
  477. * any other exception may be thrown by the callback
  478. */
  479. protected function _handle(Zend_XmlRpc_Request $request)
  480. {
  481. $method = $request->getMethod();
  482. // Check for valid method
  483. if (!$this->_table->hasMethod($method)) {
  484. require_once 'Zend/XmlRpc/Server/Exception.php';
  485. throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 620);
  486. }
  487. $info = $this->_table->getMethod($method);
  488. $params = $request->getParams();
  489. $argv = $info->getInvokeArguments();
  490. if (0 < count($argv)) {
  491. $params = array_merge($params, $argv);
  492. }
  493. // Check calling parameters against signatures
  494. $matched = false;
  495. $sigCalled = $request->getTypes();
  496. $sigLength = count($sigCalled);
  497. $paramsLen = count($params);
  498. if ($sigLength < $paramsLen) {
  499. for ($i = $sigLength; $i < $paramsLen; ++$i) {
  500. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($params[$i]);
  501. $sigCalled[] = $xmlRpcValue->getType();
  502. }
  503. }
  504. $signatures = $info->getPrototypes();
  505. foreach ($signatures as $signature) {
  506. $sigParams = $signature->getParameters();
  507. if ($sigCalled === $sigParams) {
  508. $matched = true;
  509. break;
  510. }
  511. }
  512. if (!$matched) {
  513. require_once 'Zend/XmlRpc/Server/Exception.php';
  514. throw new Zend_XmlRpc_Server_Exception('Calling parameters do not match signature', 623);
  515. }
  516. $return = $this->_dispatch($info, $params);
  517. $responseClass = $this->getResponseClass();
  518. return new $responseClass($return);
  519. }
  520. /**
  521. * Register system methods with the server
  522. *
  523. * @return void
  524. */
  525. protected function _registerSystemMethods()
  526. {
  527. $system = new Zend_XmlRpc_Server_System($this);
  528. $this->_system = $system;
  529. $this->setClass($system, 'system');
  530. }
  531. }