Server.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. return $this;
  354. }
  355. /**
  356. * Retrieve current encoding
  357. *
  358. * @return string
  359. */
  360. public function getEncoding()
  361. {
  362. return $this->_encoding;
  363. }
  364. /**
  365. * Do nothing; persistence is handled via {@link Zend_XmlRpc_Server_Cache}
  366. *
  367. * @param mixed $mode
  368. * @return void
  369. */
  370. public function setPersistence($mode)
  371. {
  372. }
  373. /**
  374. * Set the request object
  375. *
  376. * @param string|Zend_XmlRpc_Request $request
  377. * @return Zend_XmlRpc_Server
  378. * @throws Zend_XmlRpc_Server_Exception on invalid request class or object
  379. */
  380. public function setRequest($request)
  381. {
  382. if (is_string($request) && class_exists($request)) {
  383. $request = new $request();
  384. if (!$request instanceof Zend_XmlRpc_Request) {
  385. require_once 'Zend/XmlRpc/Server/Exception.php';
  386. throw new Zend_XmlRpc_Server_Exception('Invalid request class');
  387. }
  388. $request->setEncoding($this->getEncoding());
  389. } elseif (!$request instanceof Zend_XmlRpc_Request) {
  390. require_once 'Zend/XmlRpc/Server/Exception.php';
  391. throw new Zend_XmlRpc_Server_Exception('Invalid request object');
  392. }
  393. $this->_request = $request;
  394. return $this;
  395. }
  396. /**
  397. * Return currently registered request object
  398. *
  399. * @return null|Zend_XmlRpc_Request
  400. */
  401. public function getRequest()
  402. {
  403. return $this->_request;
  404. }
  405. /**
  406. * Set the class to use for the response
  407. *
  408. * @param string $class
  409. * @return boolean True if class was set, false if not
  410. */
  411. public function setResponseClass($class)
  412. {
  413. if (!class_exists($class) or
  414. ($c = new ReflectionClass($class) and !$c->isSubclassOf('Zend_XmlRpc_Response'))) {
  415. require_once 'Zend/XmlRpc/Server/Exception.php';
  416. throw new Zend_XmlRpc_Server_Exception('Invalid response class');
  417. }
  418. $this->_responseClass = $class;
  419. return true;
  420. }
  421. /**
  422. * Retrieve current response class
  423. *
  424. * @return string
  425. */
  426. public function getResponseClass()
  427. {
  428. return $this->_responseClass;
  429. }
  430. /**
  431. * Retrieve dispatch table
  432. *
  433. * @return array
  434. */
  435. public function getDispatchTable()
  436. {
  437. return $this->_table;
  438. }
  439. /**
  440. * Returns a list of registered methods
  441. *
  442. * Returns an array of dispatchables (Zend_Server_Reflection_Function,
  443. * _Method, and _Class items).
  444. *
  445. * @return array
  446. */
  447. public function getFunctions()
  448. {
  449. return $this->_table->toArray();
  450. }
  451. /**
  452. * Retrieve system object
  453. *
  454. * @return Zend_XmlRpc_Server_System
  455. */
  456. public function getSystem()
  457. {
  458. return $this->_system;
  459. }
  460. /**
  461. * Send arguments to all methods?
  462. *
  463. * If setClass() is used to add classes to the server, this flag defined
  464. * how to handle arguments. If set to true, all methods including constructor
  465. * will receive the arguments. If set to false, only constructor will receive the
  466. * arguments
  467. */
  468. public function sendArgumentsToAllMethods($flag = null)
  469. {
  470. if ($flag === null) {
  471. return $this->_sendArgumentsToAllMethods;
  472. }
  473. $this->_sendArgumentsToAllMethods = (bool)$flag;
  474. return $this;
  475. }
  476. /**
  477. * Map PHP type to XML-RPC type
  478. *
  479. * @param string $type
  480. * @return string
  481. */
  482. protected function _fixType($type)
  483. {
  484. if (isset($this->_typeMap[$type])) {
  485. return $this->_typeMap[$type];
  486. }
  487. return 'void';
  488. }
  489. /**
  490. * Handle an xmlrpc call (actual work)
  491. *
  492. * @param Zend_XmlRpc_Request $request
  493. * @return Zend_XmlRpc_Response
  494. * @throws Zend_XmlRpcServer_Exception|Exception
  495. * Zend_XmlRpcServer_Exceptions are thrown for internal errors; otherwise,
  496. * any other exception may be thrown by the callback
  497. */
  498. protected function _handle(Zend_XmlRpc_Request $request)
  499. {
  500. $method = $request->getMethod();
  501. // Check for valid method
  502. if (!$this->_table->hasMethod($method)) {
  503. require_once 'Zend/XmlRpc/Server/Exception.php';
  504. throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 620);
  505. }
  506. $info = $this->_table->getMethod($method);
  507. $params = $request->getParams();
  508. $argv = $info->getInvokeArguments();
  509. if (0 < count($argv) and $this->sendArgumentsToAllMethods()) {
  510. $params = array_merge($params, $argv);
  511. }
  512. // Check calling parameters against signatures
  513. $matched = false;
  514. $sigCalled = $request->getTypes();
  515. $sigLength = count($sigCalled);
  516. $paramsLen = count($params);
  517. if ($sigLength < $paramsLen) {
  518. for ($i = $sigLength; $i < $paramsLen; ++$i) {
  519. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($params[$i]);
  520. $sigCalled[] = $xmlRpcValue->getType();
  521. }
  522. }
  523. $signatures = $info->getPrototypes();
  524. foreach ($signatures as $signature) {
  525. $sigParams = $signature->getParameters();
  526. if ($sigCalled === $sigParams) {
  527. $matched = true;
  528. break;
  529. }
  530. }
  531. if (!$matched) {
  532. require_once 'Zend/XmlRpc/Server/Exception.php';
  533. throw new Zend_XmlRpc_Server_Exception('Calling parameters do not match signature', 623);
  534. }
  535. $return = $this->_dispatch($info, $params);
  536. $responseClass = $this->getResponseClass();
  537. return new $responseClass($return);
  538. }
  539. /**
  540. * Register system methods with the server
  541. *
  542. * @return void
  543. */
  544. protected function _registerSystemMethods()
  545. {
  546. $system = new Zend_XmlRpc_Server_System($this);
  547. $this->_system = $system;
  548. $this->setClass($system, 'system');
  549. }
  550. }