Server.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. * 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. require_once 'Zend/XmlRpc/Server/Exception.php';
  244. throw new Zend_XmlRpc_Server_Exception('Invalid method class', 610);
  245. }
  246. $argv = null;
  247. if (2 < func_num_args()) {
  248. $argv = func_get_args();
  249. $argv = array_slice($argv, 2);
  250. }
  251. $dispatchable = Zend_Server_Reflection::reflectClass($class, $argv, $namespace);
  252. foreach ($dispatchable->getMethods() as $reflection) {
  253. $this->_buildSignature($reflection, $class);
  254. }
  255. }
  256. /**
  257. * Raise an xmlrpc server fault
  258. *
  259. * @param string|Exception $fault
  260. * @param int $code
  261. * @return Zend_XmlRpc_Server_Fault
  262. */
  263. public function fault($fault = null, $code = 404)
  264. {
  265. if (!$fault instanceof Exception) {
  266. $fault = (string) $fault;
  267. if (empty($fault)) {
  268. $fault = 'Unknown Error';
  269. }
  270. require_once 'Zend/XmlRpc/Server/Exception.php';
  271. $fault = new Zend_XmlRpc_Server_Exception($fault, $code);
  272. }
  273. return Zend_XmlRpc_Server_Fault::getInstance($fault);
  274. }
  275. /**
  276. * Handle an xmlrpc call
  277. *
  278. * @param Zend_XmlRpc_Request $request Optional
  279. * @return Zend_XmlRpc_Response|Zend_XmlRpc_Fault
  280. */
  281. public function handle($request = false)
  282. {
  283. // Get request
  284. if ((!$request || !$request instanceof Zend_XmlRpc_Request)
  285. && (null === ($request = $this->getRequest()))
  286. ) {
  287. require_once 'Zend/XmlRpc/Request/Http.php';
  288. $request = new Zend_XmlRpc_Request_Http();
  289. $request->setEncoding($this->getEncoding());
  290. }
  291. $this->setRequest($request);
  292. if ($request->isFault()) {
  293. $response = $request->getFault();
  294. } else {
  295. try {
  296. $response = $this->_handle($request);
  297. } catch (Exception $e) {
  298. $response = $this->fault($e);
  299. }
  300. }
  301. // Set output encoding
  302. $response->setEncoding($this->getEncoding());
  303. return $response;
  304. }
  305. /**
  306. * Load methods as returned from {@link getFunctions}
  307. *
  308. * Typically, you will not use this method; it will be called using the
  309. * results pulled from {@link Zend_XmlRpc_Server_Cache::get()}.
  310. *
  311. * @param array|Zend_Server_Definition $definition
  312. * @return void
  313. * @throws Zend_XmlRpc_Server_Exception on invalid input
  314. */
  315. public function loadFunctions($definition)
  316. {
  317. if (!is_array($definition) && (!$definition instanceof Zend_Server_Definition)) {
  318. if (is_object($definition)) {
  319. $type = get_class($definition);
  320. } else {
  321. $type = gettype($definition);
  322. }
  323. require_once 'Zend/XmlRpc/Server/Exception.php';
  324. throw new Zend_XmlRpc_Server_Exception('Unable to load server definition; must be an array or Zend_Server_Definition, received ' . $type, 612);
  325. }
  326. $this->_table->clearMethods();
  327. $this->_registerSystemMethods();
  328. if ($definition instanceof Zend_Server_Definition) {
  329. $definition = $definition->getMethods();
  330. }
  331. foreach ($definition as $key => $method) {
  332. if ('system.' == substr($key, 0, 7)) {
  333. continue;
  334. }
  335. $this->_table->addMethod($method, $key);
  336. }
  337. }
  338. /**
  339. * Set encoding
  340. *
  341. * @param string $encoding
  342. * @return Zend_XmlRpc_Server
  343. */
  344. public function setEncoding($encoding)
  345. {
  346. $this->_encoding = $encoding;
  347. return $this;
  348. }
  349. /**
  350. * Retrieve current encoding
  351. *
  352. * @return string
  353. */
  354. public function getEncoding()
  355. {
  356. return $this->_encoding;
  357. }
  358. /**
  359. * Do nothing; persistence is handled via {@link Zend_XmlRpc_Server_Cache}
  360. *
  361. * @param mixed $mode
  362. * @return void
  363. */
  364. public function setPersistence($mode)
  365. {
  366. }
  367. /**
  368. * Set the request object
  369. *
  370. * @param string|Zend_XmlRpc_Request $request
  371. * @return Zend_XmlRpc_Server
  372. * @throws Zend_XmlRpc_Server_Exception on invalid request class or object
  373. */
  374. public function setRequest($request)
  375. {
  376. if (is_string($request) && class_exists($request)) {
  377. $request = new $request();
  378. if (!$request instanceof Zend_XmlRpc_Request) {
  379. require_once 'Zend/XmlRpc/Server/Exception.php';
  380. throw new Zend_XmlRpc_Server_Exception('Invalid request class');
  381. }
  382. $request->setEncoding($this->getEncoding());
  383. } elseif (!$request instanceof Zend_XmlRpc_Request) {
  384. require_once 'Zend/XmlRpc/Server/Exception.php';
  385. throw new Zend_XmlRpc_Server_Exception('Invalid request object');
  386. }
  387. $this->_request = $request;
  388. return $this;
  389. }
  390. /**
  391. * Return currently registered request object
  392. *
  393. * @return null|Zend_XmlRpc_Request
  394. */
  395. public function getRequest()
  396. {
  397. return $this->_request;
  398. }
  399. /**
  400. * Set the class to use for the response
  401. *
  402. * @param string $class
  403. * @return boolean True if class was set, false if not
  404. */
  405. public function setResponseClass($class)
  406. {
  407. if (!class_exists($class) or
  408. ($c = new ReflectionClass($class) and !$c->isSubclassOf('Zend_XmlRpc_Response'))) {
  409. require_once 'Zend/XmlRpc/Server/Exception.php';
  410. throw new Zend_XmlRpc_Server_Exception('Invalid response class');
  411. }
  412. $this->_responseClass = $class;
  413. return true;
  414. }
  415. /**
  416. * Retrieve current response class
  417. *
  418. * @return string
  419. */
  420. public function getResponseClass()
  421. {
  422. return $this->_responseClass;
  423. }
  424. /**
  425. * Retrieve dispatch table
  426. *
  427. * @return array
  428. */
  429. public function getDispatchTable()
  430. {
  431. return $this->_table;
  432. }
  433. /**
  434. * Returns a list of registered methods
  435. *
  436. * Returns an array of dispatchables (Zend_Server_Reflection_Function,
  437. * _Method, and _Class items).
  438. *
  439. * @return array
  440. */
  441. public function getFunctions()
  442. {
  443. return $this->_table->toArray();
  444. }
  445. /**
  446. * Retrieve system object
  447. *
  448. * @return Zend_XmlRpc_Server_System
  449. */
  450. public function getSystem()
  451. {
  452. return $this->_system;
  453. }
  454. /**
  455. * Map PHP type to XML-RPC type
  456. *
  457. * @param string $type
  458. * @return string
  459. */
  460. protected function _fixType($type)
  461. {
  462. if (isset($this->_typeMap[$type])) {
  463. return $this->_typeMap[$type];
  464. }
  465. return 'void';
  466. }
  467. /**
  468. * Handle an xmlrpc call (actual work)
  469. *
  470. * @param Zend_XmlRpc_Request $request
  471. * @return Zend_XmlRpc_Response
  472. * @throws Zend_XmlRpcServer_Exception|Exception
  473. * Zend_XmlRpcServer_Exceptions are thrown for internal errors; otherwise,
  474. * any other exception may be thrown by the callback
  475. */
  476. protected function _handle(Zend_XmlRpc_Request $request)
  477. {
  478. $method = $request->getMethod();
  479. // Check for valid method
  480. if (!$this->_table->hasMethod($method)) {
  481. require_once 'Zend/XmlRpc/Server/Exception.php';
  482. throw new Zend_XmlRpc_Server_Exception('Method "' . $method . '" does not exist', 620);
  483. }
  484. $info = $this->_table->getMethod($method);
  485. $params = $request->getParams();
  486. $argv = $info->getInvokeArguments();
  487. if (0 < count($argv)) {
  488. $params = array_merge($params, $argv);
  489. }
  490. // Check calling parameters against signatures
  491. $matched = false;
  492. $sigCalled = $request->getTypes();
  493. $sigLength = count($sigCalled);
  494. $paramsLen = count($params);
  495. if ($sigLength < $paramsLen) {
  496. for ($i = $sigLength; $i < $paramsLen; ++$i) {
  497. $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($params[$i]);
  498. $sigCalled[] = $xmlRpcValue->getType();
  499. }
  500. }
  501. $signatures = $info->getPrototypes();
  502. foreach ($signatures as $signature) {
  503. $sigParams = $signature->getParameters();
  504. if ($sigCalled === $sigParams) {
  505. $matched = true;
  506. break;
  507. }
  508. }
  509. if (!$matched) {
  510. require_once 'Zend/XmlRpc/Server/Exception.php';
  511. throw new Zend_XmlRpc_Server_Exception('Calling parameters do not match signature', 623);
  512. }
  513. $return = $this->_dispatch($info, $params);
  514. $responseClass = $this->getResponseClass();
  515. return new $responseClass($return);
  516. }
  517. /**
  518. * Register system methods with the server
  519. *
  520. * @return void
  521. */
  522. protected function _registerSystemMethods()
  523. {
  524. $system = new Zend_XmlRpc_Server_System($this);
  525. $this->_system = $system;
  526. $this->setClass($system, 'system');
  527. }
  528. }