1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: 22: 23:
24:
25:
26: 27: 28:
29:
30:
31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: class Zend_XmlRpc_Request
49: {
50: 51: 52: 53:
54: protected $_encoding = 'UTF-8';
55:
56: 57: 58: 59:
60: protected $_method;
61:
62: 63: 64: 65:
66: protected $_xml;
67:
68: 69: 70: 71:
72: protected $_params = array();
73:
74: 75: 76: 77:
78: protected $_fault = null;
79:
80: 81: 82: 83:
84: protected $_types = array();
85:
86: 87: 88: 89:
90: protected $_xmlRpcParams = array();
91:
92: 93: 94: 95: 96: 97:
98: public function __construct($method = null, $params = null)
99: {
100: if ($method !== null) {
101: $this->setMethod($method);
102: }
103:
104: if ($params !== null) {
105: $this->setParams($params);
106: }
107: }
108:
109:
110: 111: 112: 113: 114: 115:
116: public function setEncoding($encoding)
117: {
118: $this->_encoding = $encoding;
119: Zend_XmlRpc_Value::setEncoding($encoding);
120: return $this;
121: }
122:
123: 124: 125: 126: 127:
128: public function getEncoding()
129: {
130: return $this->_encoding;
131: }
132:
133: 134: 135: 136: 137: 138:
139: public function setMethod($method)
140: {
141: if (!is_string($method) || !preg_match('/^[a-z0-9_.:\/]+$/i', $method)) {
142: $this->_fault = new Zend_XmlRpc_Fault(634, 'Invalid method name ("' . $method . '")');
143: $this->_fault->setEncoding($this->getEncoding());
144: return false;
145: }
146:
147: $this->_method = $method;
148: return true;
149: }
150:
151: 152: 153: 154: 155:
156: public function getMethod()
157: {
158: return $this->_method;
159: }
160:
161: 162: 163: 164: 165: 166: 167: 168: 169: 170:
171: public function addParam($value, $type = null)
172: {
173: $this->_params[] = $value;
174: if (null === $type) {
175:
176: if ($value instanceof Zend_XmlRpc_Value) {
177: $type = $value->getType();
178: } else {
179: $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($value);
180: $type = $xmlRpcValue->getType();
181: }
182: }
183: $this->_types[] = $type;
184: $this->_xmlRpcParams[] = array('value' => $value, 'type' => $type);
185: }
186:
187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207:
208: public function setParams()
209: {
210: $argc = func_num_args();
211: $argv = func_get_args();
212: if (0 == $argc) {
213: return;
214: }
215:
216: if ((1 == $argc) && is_array($argv[0])) {
217: $params = array();
218: $types = array();
219: $wellFormed = true;
220: foreach ($argv[0] as $arg) {
221: if (!is_array($arg) || !isset($arg['value'])) {
222: $wellFormed = false;
223: break;
224: }
225: $params[] = $arg['value'];
226:
227: if (!isset($arg['type'])) {
228: $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg['value']);
229: $arg['type'] = $xmlRpcValue->getType();
230: }
231: $types[] = $arg['type'];
232: }
233: if ($wellFormed) {
234: $this->_xmlRpcParams = $argv[0];
235: $this->_params = $params;
236: $this->_types = $types;
237: } else {
238: $this->_params = $argv[0];
239: $this->_types = array();
240: $xmlRpcParams = array();
241: foreach ($argv[0] as $arg) {
242: if ($arg instanceof Zend_XmlRpc_Value) {
243: $type = $arg->getType();
244: } else {
245: $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
246: $type = $xmlRpcValue->getType();
247: }
248: $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
249: $this->_types[] = $type;
250: }
251: $this->_xmlRpcParams = $xmlRpcParams;
252: }
253: return;
254: }
255:
256: $this->_params = $argv;
257: $this->_types = array();
258: $xmlRpcParams = array();
259: foreach ($argv as $arg) {
260: if ($arg instanceof Zend_XmlRpc_Value) {
261: $type = $arg->getType();
262: } else {
263: $xmlRpcValue = Zend_XmlRpc_Value::getXmlRpcValue($arg);
264: $type = $xmlRpcValue->getType();
265: }
266: $xmlRpcParams[] = array('value' => $arg, 'type' => $type);
267: $this->_types[] = $type;
268: }
269: $this->_xmlRpcParams = $xmlRpcParams;
270: }
271:
272: 273: 274: 275: 276:
277: public function getParams()
278: {
279: return $this->_params;
280: }
281:
282: 283: 284: 285: 286:
287: public function getTypes()
288: {
289: return $this->_types;
290: }
291:
292: 293: 294: 295: 296: 297:
298: public function loadXml($request)
299: {
300: if (!is_string($request)) {
301: $this->_fault = new Zend_XmlRpc_Fault(635);
302: $this->_fault->setEncoding($this->getEncoding());
303: return false;
304: }
305:
306: $loadEntities = libxml_disable_entity_loader(true);
307: try {
308: $xml = new SimpleXMLElement($request);
309: libxml_disable_entity_loader($loadEntities);
310: } catch (Exception $e) {
311:
312: $this->_fault = new Zend_XmlRpc_Fault(631);
313: $this->_fault->setEncoding($this->getEncoding());
314: libxml_disable_entity_loader($loadEntities);
315: return false;
316: }
317:
318:
319: if (empty($xml->methodName)) {
320:
321: $this->_fault = new Zend_XmlRpc_Fault(632);
322: $this->_fault->setEncoding($this->getEncoding());
323: return false;
324: }
325:
326: $this->_method = (string) $xml->methodName;
327:
328:
329: if (!empty($xml->params)) {
330: $types = array();
331: $argv = array();
332: foreach ($xml->params->children() as $param) {
333: if (!isset($param->value)) {
334: $this->_fault = new Zend_XmlRpc_Fault(633);
335: $this->_fault->setEncoding($this->getEncoding());
336: return false;
337: }
338:
339: try {
340: $param = Zend_XmlRpc_Value::getXmlRpcValue($param->value, Zend_XmlRpc_Value::XML_STRING);
341: $types[] = $param->getType();
342: $argv[] = $param->getValue();
343: } catch (Exception $e) {
344: $this->_fault = new Zend_XmlRpc_Fault(636);
345: $this->_fault->setEncoding($this->getEncoding());
346: return false;
347: }
348: }
349:
350: $this->_types = $types;
351: $this->_params = $argv;
352: }
353:
354: $this->_xml = $request;
355:
356: return true;
357: }
358:
359: 360: 361: 362: 363: 364:
365: public function isFault()
366: {
367: return $this->_fault instanceof Zend_XmlRpc_Fault;
368: }
369:
370: 371: 372: 373: 374:
375: public function getFault()
376: {
377: return $this->_fault;
378: }
379:
380: 381: 382: 383: 384:
385: protected function _getXmlRpcParams()
386: {
387: $params = array();
388: if (is_array($this->_xmlRpcParams)) {
389: foreach ($this->_xmlRpcParams as $param) {
390: $value = $param['value'];
391: $type = isset($param['type']) ? $param['type'] : Zend_XmlRpc_Value::AUTO_DETECT_TYPE;
392:
393: if (!$value instanceof Zend_XmlRpc_Value) {
394: $value = Zend_XmlRpc_Value::getXmlRpcValue($value, $type);
395: }
396: $params[] = $value;
397: }
398: }
399:
400: return $params;
401: }
402:
403: 404: 405: 406: 407:
408: public function saveXml()
409: {
410: $args = $this->_getXmlRpcParams();
411: $method = $this->getMethod();
412:
413: $generator = Zend_XmlRpc_Value::getGenerator();
414: $generator->openElement('methodCall')
415: ->openElement('methodName', $method)
416: ->closeElement('methodName');
417:
418: if (is_array($args) && count($args)) {
419: $generator->openElement('params');
420:
421: foreach ($args as $arg) {
422: $generator->openElement('param');
423: $arg->generateXml();
424: $generator->closeElement('param');
425: }
426: $generator->closeElement('params');
427: }
428: $generator->closeElement('methodCall');
429:
430: return $generator->flush();
431: }
432:
433: 434: 435: 436: 437:
438: public function __toString()
439: {
440: return $this->saveXML();
441: }
442: }
443: