Server.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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-2009 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-2009 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_Server_Definition
  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. 'i8' => 'Zend_Crypt_Math_BigInteger',
  137. 'double' => 'double',
  138. 'float' => 'double',
  139. 'real' => 'double',
  140. 'boolean' => 'boolean',
  141. 'bool' => 'boolean',
  142. 'true' => 'boolean',
  143. 'false' => 'boolean',
  144. 'string' => 'string',
  145. 'str' => 'string',
  146. 'base64' => 'base64',
  147. 'dateTime.iso8601' => 'dateTime.iso8601',
  148. 'date' => 'dateTime.iso8601',
  149. 'time' => 'dateTime.iso8601',
  150. 'time' => 'dateTime.iso8601',
  151. 'Zend_Date' => 'dateTime.iso8601',
  152. 'DateTime' => 'dateTime.iso8601',
  153. 'array' => 'array',
  154. 'struct' => 'struct',
  155. 'null' => 'nil',
  156. 'nil' => 'nil',
  157. 'void' => 'void',
  158. 'mixed' => 'struct'
  159. );
  160. /**
  161. * Send arguments to all methods or just constructor?
  162. *
  163. * @var bool
  164. */
  165. protected $_sendArgumentsToAllMethods = true;
  166. /**
  167. * Constructor
  168. *
  169. * Creates system.* methods.
  170. *
  171. * @return void
  172. */
  173. public function __construct()
  174. {
  175. $this->_table = new Zend_Server_Definition();
  176. $this->_registerSystemMethods();
  177. }
  178. /**
  179. * Proxy calls to system object
  180. *
  181. * @param string $method
  182. * @param array $params
  183. * @return mixed
  184. * @throws Zend_XmlRpc_Server_Exception
  185. */
  186. public function __call($method, $params)
  187. {
  188. $system = $this->getSystem();
  189. if (!method_exists($system, $method)) {
  190. require_once 'Zend/XmlRpc/Server/Exception.php';
  191. throw new Zend_XmlRpc_Server_Exception('Unknown instance method called on server: ' . $method);
  192. }
  193. return call_user_func_array(array($system, $method), $params);
  194. }
  195. /**
  196. * Attach a callback as an XMLRPC method
  197. *
  198. * Attaches a callback as an XMLRPC method, prefixing the XMLRPC method name
  199. * with $namespace, if provided. Reflection is done on the callback's
  200. * docblock to create the methodHelp for the XMLRPC method.
  201. *
  202. * Additional arguments to pass to the function at dispatch may be passed;
  203. * any arguments following the namespace will be aggregated and passed at
  204. * dispatch time.
  205. *
  206. * @param string|array $function Valid callback
  207. * @param string $namespace Optional namespace prefix
  208. * @return void
  209. * @throws Zend_XmlRpc_Server_Exception
  210. */
  211. public function addFunction($function, $namespace = '')
  212. {
  213. if (!is_string($function) && !is_array($function)) {
  214. require_once 'Zend/XmlRpc/Server/Exception.php';
  215. throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611);
  216. }
  217. $argv = null;
  218. if (2 < func_num_args()) {
  219. $argv = func_get_args();
  220. $argv = array_slice($argv, 2);
  221. }
  222. $function = (array) $function;
  223. foreach ($function as $func) {
  224. if (!is_string($func) || !function_exists($func)) {
  225. require_once 'Zend/XmlRpc/Server/Exception.php';
  226. throw new Zend_XmlRpc_Server_Exception('Unable to attach function; invalid', 611);
  227. }
  228. $reflection = Zend_Server_Reflection::reflectFunction($func, $argv, $namespace);
  229. $this->_buildSignature($reflection);
  230. }
  231. }
  232. /**
  233. * Attach class methods as XMLRPC method handlers
  234. *
  235. * $class may be either a class name or an object. Reflection is done on the
  236. * class or object to determine the available public methods, and each is
  237. * attached to the server as an available method; if a $namespace has been
  238. * provided, that namespace is used to prefix the XMLRPC method names.
  239. *
  240. * Any additional arguments beyond $namespace will be passed to a method at
  241. * invocation.
  242. *
  243. * @param string|object $class
  244. * @param string $namespace Optional
  245. * @param mixed $argv Optional arguments to pass to methods
  246. * @return void
  247. * @throws Zend_XmlRpc_Server_Exception on invalid input
  248. */
  249. public function setClass($class, $namespace = '', $argv = null)
  250. {
  251. if (is_string($class) && !class_exists($class)) {
  252. require_once 'Zend/XmlRpc/Server/Exception.php';
  253. throw new Zend_XmlRpc_Server_Exception('Invalid method class', 610);
  254. }
  255. $argv = null;
  256. if (2 < func_num_args()) {
  257. $argv = func_get_args();
  258. $argv = array_slice($argv, 2);
  259. }
  260. $dispatchable = Zend_Server_Reflection::reflectClass($class, $argv, $namespace);
  261. foreach ($dispatchable->getMethods() as $reflection) {
  262. $this->_buildSignature($reflection, $class);
  263. }
  264. }
  265. /**
  266. * Raise an xmlrpc server fault
  267. *
  268. * @param string|Exception $fault
  269. * @param int $code
  270. * @return Zend_XmlRpc_Server_Fault
  271. */
  272. public function fault($fault = null, $code = 404)
  273. {
  274. if (!$fault instanceof Exception) {
  275. $fault = (string) $fault;
  276. if (empty($fault)) {
  277. $fault = 'Unknown Error';
  278. }
  279. require_once 'Zend/XmlRpc/Server/Exception.php';
  280. $fault = new Zend_XmlRpc_Server_Exception($fault, $code);
  281. }
  282. return Zend_XmlRpc_Server_Fault::getInstance($fault);
  283. }
  284. /**
  285. * Handle an xmlrpc call
  286. *
  287. * @param Zend_XmlRpc_Request $request Optional
  288. * @return Zend_XmlRpc_Response|Zend_XmlRpc_Fault
  289. */
  290. public function handle($request = false)
  291. {
  292. // Get request
  293. if ((!$request || !$request instanceof Zend_XmlRpc_Request)
  294. && (null === ($request = $this->getRequest()))
  295. ) {
  296. require_once 'Zend/XmlRpc/Request/Http.php';
  297. $request = new Zend_XmlRpc_Request_Http();
  298. $request->setEncoding($this->getEncoding());
  299. }
  300. $this->setRequest($request);
  301. if ($request->isFault()) {
  302. $response = $request->getFault();
  303. } else {
  304. try {
  305. $response = $this->_handle($request);
  306. } catch (Exception $e) {
  307. $response = $this->fault($e);
  308. }
  309. }
  310. // Set output encoding
  311. $response->setEncoding($this->getEncoding());
  312. return $response;
  313. }
  314. /**
  315. * Load methods as returned from {@link getFunctions}
  316. *
  317. * Typically, you will not use this method; it will be called using the
  318. * results pulled from {@link Zend_XmlRpc_Server_Cache::get()}.
  319. *
  320. * @param array|Zend_Server_Definition $definition
  321. * @return void
  322. * @throws Zend_XmlRpc_Server_Exception on invalid input
  323. */
  324. public function loadFunctions($definition)
  325. {
  326. if (!is_array($definition) && (!$definition instanceof Zend_Server_Definition)) {
  327. if (is_object($definition)) {
  328. $type = get_class($definition);
  329. } else {
  330. $type = gettype($definition);
  331. }
  332. require_once 'Zend/XmlRpc/Server/Exception.php';
  333. throw new Zend_XmlRpc_Server_Exception('Unable to load server definition; must be an array or Zend_Server_Definition, received ' . $type, 612);
  334. }
  335. $this->_table->clearMethods();
  336. $this->_registerSystemMethods();
  337. if ($definition instanceof Zend_Server_Definition) {
  338. $definition = $definition->getMethods();
  339. }
  340. foreach ($definition as $key => $method) {
  341. if ('system.' == substr($key, 0, 7)) {
  342. continue;
  343. }
  344. $this->_table->addMethod($method, $key);
  345. }
  346. }
  347. /**
  348. * Set encoding
  349. *
  350. * @param string $encoding
  351. * @return Zend_XmlRpc_Server
  352. */
  353. public function setEncoding($encoding)
  354. {
  355. $this->_encoding = $encoding;
  356. Zend_XmlRpc_Value::setEncoding($encoding);
  357. return $this;
  358. }
  359. /**
  360. * Retrieve current encoding
  361. *
  362. * @return string
  363. */
  364. public function getEncoding()
  365. {
  366. return $this->_encoding;
  367. }
  368. /**
  369. * Do nothing; persistence is handled via {@link Zend_XmlRpc_Server_Cache}
  370. *
  371. * @param mixed $mode
  372. * @return void
  373. */
  374. public function setPersistence($mode)
  375. {
  376. }
  377. /**
  378. * Set the request object
  379. *
  380. * @param string|Zend_XmlRpc_Request $request
  381. * @return Zend_XmlRpc_Server
  382. * @throws Zend_XmlRpc_Server_Exception on invalid request class or object
  383. */
  384. public function setRequest($request)
  385. {
  386. if (is_string($request) && class_exists($request)) {
  387. $request = new $request();
  388. if (!$request instanceof Zend_XmlRpc_Request) {
  389. require_once 'Zend/XmlRpc/Server/Exception.php';
  390. throw new Zend_XmlRpc_Server_Exception('Invalid request class');
  391. }
  392. $request->setEncoding($this->getEncoding());
  393. } elseif (!$request instanceof Zend_XmlRpc_Request) {
  394. require_once 'Zend/XmlRpc/Server/Exception.php';
  395. throw new Zend_XmlRpc_Server_Exception('Invalid request object');
  396. }
  397. $this->_request = $request;
  398. return $this;
  399. }
  400. /**
  401. * Return currently registered request object
  402. *
  403. * @return null|Zend_XmlRpc_Request
  404. */
  405. public function getRequest()
  406. {
  407. return $this->_request;
  408. }
  409. /**
  410. * Set the class to use for the response
  411. *
  412. * @param string $class
  413. * @return boolean True if class was set, false if not
  414. */
  415. public function setResponseClass($class)
  416. {
  417. if (!class_exists($class) or
  418. ($c = new ReflectionClass($class) and !$c->isSubclassOf('Zend_XmlRpc_Response'))) {
  419. require_once 'Zend/XmlRpc/Server/Exception.php';
  420. throw new Zend_XmlRpc_Server_Exception('Invalid response class');
  421. }
  422. $this->_responseClass = $class;
  423. return true;
  424. }
  425. /**
  426. * Retrieve current response class
  427. *
  428. * @return string
  429. */
  430. public function getResponseClass()
  431. {
  432. return $this->_responseClass;
  433. }
  434. /**
  435. * Retrieve dispatch table
  436. *
  437. * @return array
  438. */
  439. public function getDispatchTable()
  440. {
  441. return $this->_table;
  442. }
  443. /**
  444. * Returns a list of registered methods
  445. *
  446. * Returns an array of dispatchables (Zend_Server_Reflection_Function,
  447. * _Method, and _Class items).
  448. *
  449. * @return array
  450. */
  451. public function getFunctions()
  452. {
  453. return $this->_table->toArray();
  454. }
  455. /**
  456. * Retrieve system object
  457. *
  458. * @return Zend_XmlRpc_Server_System
  459. */
  460. public function getSystem()
  461. {
  462. return $this->_system;
  463. }
  464. /**
  465. * Send arguments to all methods?
  466. *
  467. * If setClass() is used to add classes to the server, this flag defined
  468. * how to handle arguments. If set to true, all methods including constructor
  469. * will receive the arguments. If set to false, only constructor will receive the
  470. * arguments
  471. */
  472. public function sendArgumentsToAllMethods($flag = null)
  473. {
  474. if ($flag === null) {
  475. return $this->_sendArgumentsToAllMethods;
  476. }
  477. $this->_sendArgumentsToAllMethods = (bool)$flag;
  478. return $this;
  479. }
  480. /**
  481. * Map PHP type to XML-RPC type
  482. *
  483. * @param string $type
  484. * @return string
  485. */
  486. protected function _fixType($type)
  487. {
  488. if (isset($this->_typeMap[$type])) {
  489. return $this->_typeMap[$type];
  490. }
  491. return 'void';
  492. }
  493. /**
  494. * Handle an xmlrpc call (actual work)
  495. *
  496. * @param Zend_XmlRpc_Request $request
  497. * @return Zend_XmlRpc_Response
  498. * @throws Zend_XmlRpcServer_Exception|Exception
  499. * Zend_XmlRpcServer_Exceptions are thrown for internal errors; otherwise,
  500. * any other exception may be thrown by the callback
  501. */
  502. protected function _handle(Zend_XmlRpc_Request $request)
  503. {
  504. $method = $request->getMethod();
  505. // Check for valid method
  506. if (!$this->_table->hasMethod($method)) {
  507. require_once 'Zend/XmlRpc/Server/Exception.php';
  508. throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 620);
  509. }
  510. $info = $this->_table->getMethod($method);
  511. $params = $request->getParams();
  512. $argv = $info->getInvokeArguments();
  513. if (0 < count($argv) and $this->sendArgumentsToAllMethods()) {
  514. $params = array_merge($params, $argv);
  515. }
  516. // Check calling parameters against signatures
  517. $matched = false;
  518. $sigCalled = $request->getTypes();
  519. $sigLength = count($sigCalled);
  520. $paramsLen = count($params);
  521. if ($sigLength < $paramsLen) {
  522. for ($i = $sigLength; $i < $paramsLen; ++$i) {
  523. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($params[$i]);
  524. $sigCalled[] = $xmlRpcValue->getType();
  525. }
  526. }
  527. $signatures = $info->getPrototypes();
  528. foreach ($signatures as $signature) {
  529. $sigParams = $signature->getParameters();
  530. if ($sigCalled === $sigParams) {
  531. $matched = true;
  532. break;
  533. }
  534. }
  535. if (!$matched) {
  536. require_once 'Zend/XmlRpc/Server/Exception.php';
  537. throw new Zend_XmlRpc_Server_Exception('Calling parameters do not match signature', 623);
  538. }
  539. $return = $this->_dispatch($info, $params);
  540. $responseClass = $this->getResponseClass();
  541. return new $responseClass($return);
  542. }
  543. /**
  544. * Register system methods with the server
  545. *
  546. * @return void
  547. */
  548. protected function _registerSystemMethods()
  549. {
  550. $system = new Zend_XmlRpc_Server_System($this);
  551. $this->_system = $system;
  552. $this->setClass($system, 'system');
  553. }
  554. }