Server.php 17 KB

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