/class/xml/rpc/xmlrpctag.php

https://gitlab.com/VoyaTrax/vtCMS3 · PHP · 410 lines · 225 code · 44 blank · 141 comment · 7 complexity · 7f2678424a20599b964ed53011435c37 MD5 · raw file

  1. <?php
  2. /*
  3. You may not change or alter any portion of this comment or credits
  4. of supporting developers from this source code or any supporting source code
  5. which is considered copyrighted (c) material of the original comment or credit authors.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. */
  10. /**
  11. * @copyright The XOOPS Project http://sourceforge.net/projects/xoops/
  12. * @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
  13. * @package class
  14. * @subpackage xml
  15. * @since 1.0.0
  16. * @author Kazumi Ono (AKA onokazu)
  17. * @version $Id $
  18. */
  19. abstract class XoopsXmlRpcDocument
  20. {
  21. /**
  22. * @var array of XoopsXmlRpcTag
  23. */
  24. protected $_tags = array();
  25. /**
  26. * @param XoopsXmlRpcTag $tagobj
  27. * @return void
  28. */
  29. public function add(XoopsXmlRpcTag &$tagobj)
  30. {
  31. $this->_tags[] = $tagobj;
  32. }
  33. abstract function render();
  34. }
  35. class XoopsXmlRpcResponse extends XoopsXmlRpcDocument
  36. {
  37. /**
  38. * @return string
  39. */
  40. public function render()
  41. {
  42. $payload = '';
  43. foreach ($this->_tags as $tag) {
  44. /* @var $tag XoopsXmlRpcTag */
  45. if (!$tag->isFault()) {
  46. $payload .= $tag->render();
  47. } else {
  48. return '<?xml version="1.0"?><methodResponse>' . $tag->render() . '</methodResponse>';
  49. }
  50. }
  51. return '<?xml version="1.0"?><methodResponse><params><param>' . $payload . '</param></params></methodResponse>';
  52. }
  53. }
  54. class XoopsXmlRpcRequest extends XoopsXmlRpcDocument
  55. {
  56. /**
  57. * @var string
  58. */
  59. public $methodName;
  60. /**
  61. * @param string $methodName
  62. */
  63. public function __construct($methodName)
  64. {
  65. $this->methodName = trim($methodName);
  66. }
  67. public function render()
  68. {
  69. $payload = '';
  70. foreach ($this->_tags as $tag) {
  71. /* @var $tag XoopsXmlRpcTag */
  72. $payload .= '<param>' . $tag->render() . '</param>';
  73. }
  74. return '<?xml version="1.0"?><methodCall><methodName>' . $this->methodName . '</methodName><params>' . $payload . '</params></methodCall>';
  75. }
  76. }
  77. abstract class XoopsXmlRpcTag
  78. {
  79. /**
  80. * @var bool
  81. */
  82. protected $_fault = false;
  83. /**
  84. * @param string $text
  85. * @return mixed
  86. */
  87. public function encode(&$text)
  88. {
  89. $text = preg_replace(array("/\&([a-z\d\#]+)\;/i", "/\&/", "/\#\|\|([a-z\d\#]+)\|\|\#/i"),
  90. array("#||\\1||#", "&amp;", "&\\1;"), str_replace(array("<", ">"), array("&lt;", "&gt;"), $text));
  91. return $text;
  92. }
  93. /**
  94. * @param bool $fault
  95. * @return void
  96. */
  97. public function setFault($fault = true)
  98. {
  99. $this->_fault = (intval($fault) > 0) ? true : false;
  100. }
  101. /**
  102. * @return bool
  103. */
  104. public function isFault()
  105. {
  106. return $this->_fault;
  107. }
  108. /**
  109. * @abstract
  110. * @return void
  111. */
  112. abstract function render();
  113. }
  114. class XoopsXmlRpcFault extends XoopsXmlRpcTag
  115. {
  116. /**
  117. * @var int
  118. */
  119. protected $_code;
  120. /**
  121. * @var string
  122. */
  123. protected $_extra;
  124. /**
  125. * @param int $code
  126. * @param string $extra
  127. */
  128. public function __construct($code, $extra = null)
  129. {
  130. $this->setFault(true);
  131. $this->_code = intval($code);
  132. $this->_extra = isset($extra) ? trim($extra) : '';
  133. }
  134. /**
  135. * @return string
  136. */
  137. public function render()
  138. {
  139. switch ($this->_code) {
  140. case 101:
  141. $string = 'Invalid server URI';
  142. break;
  143. case 102:
  144. $string = 'Parser parse error';
  145. break;
  146. case 103:
  147. $string = 'Module not found';
  148. break;
  149. case 104:
  150. $string = 'User authentication failed';
  151. break;
  152. case 105:
  153. $string = 'Module API not found';
  154. break;
  155. case 106:
  156. $string = 'Method response error';
  157. break;
  158. case 107:
  159. $string = 'Method not supported';
  160. break;
  161. case 108:
  162. $string = 'Invalid parameter';
  163. break;
  164. case 109:
  165. $string = 'Missing parameters';
  166. break;
  167. case 110:
  168. $string = 'Selected blog application does not exist';
  169. break;
  170. case 111:
  171. $string = 'Method permission denied';
  172. break;
  173. default:
  174. $string = 'Method response error';
  175. break;
  176. }
  177. $string .= "\n" . $this->_extra;
  178. return '<fault><value><struct><member><name>faultCode</name><value>' . $this->_code . '</value></member><member><name>faultString</name><value>' . $this->encode($string) . '</value></member></struct></value></fault>';
  179. }
  180. }
  181. class XoopsXmlRpcInt extends XoopsXmlRpcTag
  182. {
  183. /**
  184. * @var int
  185. */
  186. protected $_value;
  187. /**
  188. * @param $value
  189. */
  190. public function __construct($value)
  191. {
  192. $this->_value = intval($value);
  193. }
  194. /**
  195. * @return string
  196. */
  197. public function render()
  198. {
  199. return '<value><int>' . $this->_value . '</int></value>';
  200. }
  201. }
  202. class XoopsXmlRpcDouble extends XoopsXmlRpcTag
  203. {
  204. /**
  205. * @var float
  206. */
  207. protected $_value;
  208. /**
  209. * @param float $value
  210. */
  211. public function __construct($value)
  212. {
  213. $this->_value = (float)$value;
  214. }
  215. /**
  216. * @return string
  217. */
  218. public function render()
  219. {
  220. return '<value><double>' . $this->_value . '</double></value>';
  221. }
  222. }
  223. class XoopsXmlRpcBoolean extends XoopsXmlRpcTag
  224. {
  225. /**
  226. * @var int
  227. */
  228. protected $_value;
  229. /**
  230. * @param int|bool $value
  231. */
  232. public function __construct($value)
  233. {
  234. $this->_value = (!empty($value) && $value != false) ? 1 : 0;
  235. }
  236. /**
  237. * @return string
  238. */
  239. public function render()
  240. {
  241. return '<value><boolean>' . $this->_value . '</boolean></value>';
  242. }
  243. }
  244. class XoopsXmlRpcString extends XoopsXmlRpcTag
  245. {
  246. /**
  247. * @var string
  248. */
  249. protected $_value;
  250. /**
  251. * @param string $value
  252. */
  253. public function __construct($value)
  254. {
  255. $this->_value = strval($value);
  256. }
  257. /**
  258. * @return string
  259. */
  260. public function render()
  261. {
  262. return '<value><string>' . $this->encode($this->_value) . '</string></value>';
  263. }
  264. }
  265. class XoopsXmlRpcDatetime extends XoopsXmlRpcTag
  266. {
  267. /**
  268. * @var int
  269. */
  270. protected $_value;
  271. /**
  272. * @param int|string $value
  273. */
  274. public function __construct($value)
  275. {
  276. if (!is_numeric($value)) {
  277. $this->_value = strtotime($value);
  278. } else {
  279. $this->_value = intval($value);
  280. }
  281. }
  282. /**
  283. * @return string
  284. */
  285. public function render()
  286. {
  287. return '<value><dateTime.iso8601>' . gmstrftime("%Y%m%dT%H:%M:%S", $this->_value) . '</dateTime.iso8601></value>';
  288. }
  289. }
  290. class XoopsXmlRpcBase64 extends XoopsXmlRpcTag
  291. {
  292. /**
  293. * @var string
  294. */
  295. protected $_value;
  296. /**
  297. * @param string $value
  298. */
  299. public function __construct($value)
  300. {
  301. $this->_value = base64_encode($value);
  302. }
  303. /**
  304. * @return string
  305. */
  306. public function render()
  307. {
  308. return '<value><base64>' . $this->_value . '</base64></value>';
  309. }
  310. }
  311. class XoopsXmlRpcArray extends XoopsXmlRpcTag
  312. {
  313. /**
  314. * @var array of XoopsXmlRpcTag
  315. */
  316. protected $_tags = array();
  317. /**
  318. * @param XoopsXmlRpcTag $tagobj
  319. * @return void
  320. */
  321. public function add(XoopsXmlRpcTag &$tagobj)
  322. {
  323. $this->_tags[] = $tagobj;
  324. }
  325. /**
  326. * @return string
  327. */
  328. public function render()
  329. {
  330. $ret = '<value><array><data>';
  331. foreach ($this->_tags as $tag) {
  332. /* @var $tag XoopsXmlRpcTag */
  333. $ret .= $tag->render();
  334. }
  335. $ret .= '</data></array></value>';
  336. return $ret;
  337. }
  338. }
  339. class XoopsXmlRpcStruct extends XoopsXmlRpcTag
  340. {
  341. /**
  342. * @var array of array containing XoopsXmlRpcTag
  343. */
  344. protected $_tags = array();
  345. /**
  346. * @param $name
  347. * @param XoopsXmlRpcTag $tagobj
  348. * @return void
  349. */
  350. public function add($name, XoopsXmlRpcTag &$tagobj)
  351. {
  352. $this->_tags[] = array('name' => $name, 'value' => $tagobj);
  353. }
  354. public function render()
  355. {
  356. $ret = '<value><struct>';
  357. foreach ($this->_tags as $tag) {
  358. /* @var $tag['value'] XoopsXmlRplTag */
  359. $ret .= '<member><name>' . $this->encode($tag['name']) . '</name>' . $tag['value']->render() . '</member>';
  360. }
  361. $ret .= '</struct></value>';
  362. return $ret;
  363. }
  364. }
  365. ?>