Example #1
0
 public function testFaultStringWithoutStringTypeDeclaration()
 {
     $xml = $this->_createNonStandardXml();
     $parsed = $this->_fault->loadXml($xml);
     $this->assertTrue($parsed, $xml);
     $this->assertEquals('Error string', $this->_fault->getMessage());
 }
Example #2
0
 /**
  * Load a response from an XML response
  *
  * Attempts to load a response from an XMLRPC response, autodetecting if it
  * is a fault response.
  *
  * @param string $response
  * @return boolean True if a valid XMLRPC response, false if a fault
  * response or invalid input
  */
 public function loadXml($response)
 {
     if (!is_string($response)) {
         $this->fault = new Fault(650);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     // @see ZF-12293 - disable external entities for security purposes
     $loadEntities = libxml_disable_entity_loader(true);
     $useInternalXmlErrors = libxml_use_internal_errors(true);
     try {
         $dom = new \DOMDocument();
         $dom->loadXML($response);
         foreach ($dom->childNodes as $child) {
             if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
                 throw new Exception\ValueException('Invalid XML: Detected use of illegal DOCTYPE');
             }
         }
         // TODO: Locate why this passes tests but a simplexml import doesn't
         //$xml = simplexml_import_dom($dom);
         $xml = new \SimpleXMLElement($response);
         libxml_disable_entity_loader($loadEntities);
         libxml_use_internal_errors($useInternalXmlErrors);
     } catch (\Exception $e) {
         libxml_disable_entity_loader($loadEntities);
         libxml_use_internal_errors($useInternalXmlErrors);
         // Not valid XML
         $this->fault = new Fault(651);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     if (!empty($xml->fault)) {
         // fault response
         $this->fault = new Fault();
         $this->fault->setEncoding($this->getEncoding());
         $this->fault->loadXml($response);
         return false;
     }
     if (empty($xml->params)) {
         // Invalid response
         $this->fault = new Fault(652);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     try {
         if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
             throw new Exception\ValueException('Missing XML-RPC value in XML');
         }
         $valueXml = $xml->params->param->value->asXML();
         $value = AbstractValue::getXmlRpcValue($valueXml, AbstractValue::XML_STRING);
     } catch (Exception\ValueException $e) {
         $this->fault = new Fault(653);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     $this->setReturnValue($value->getValue());
     return true;
 }
Example #3
0
 /**
  * Load XML and parse into request components
  *
  * @param string $request
  * @return boolean True on success, false if an error occurred.
  */
 public function loadXml($request)
 {
     if (!is_string($request)) {
         $this->fault = new Fault(635);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     // @see ZF-12293 - disable external entities for security purposes
     $loadEntities = libxml_disable_entity_loader(true);
     try {
         $xml = new \SimpleXMLElement($request);
         libxml_disable_entity_loader($loadEntities);
     } catch (\Exception $e) {
         // Not valid XML
         $this->fault = new Fault(631);
         $this->fault->setEncoding($this->getEncoding());
         libxml_disable_entity_loader($loadEntities);
         return false;
     }
     // Check for method name
     if (empty($xml->methodName)) {
         // Missing method name
         $this->fault = new Fault(632);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     $this->method = (string) $xml->methodName;
     // Check for parameters
     if (!empty($xml->params)) {
         $types = array();
         $argv = array();
         foreach ($xml->params->children() as $param) {
             if (!isset($param->value)) {
                 $this->fault = new Fault(633);
                 $this->fault->setEncoding($this->getEncoding());
                 return false;
             }
             try {
                 $param = AbstractValue::getXmlRpcValue($param->value, AbstractValue::XML_STRING);
                 $types[] = $param->getType();
                 $argv[] = $param->getValue();
             } catch (\Exception $e) {
                 $this->fault = new Fault(636);
                 $this->fault->setEncoding($this->getEncoding());
                 return false;
             }
         }
         $this->types = $types;
         $this->params = $argv;
     }
     $this->xml = $request;
     return true;
 }
Example #4
0
 /**
  * Load a response from an XML response
  *
  * Attempts to load a response from an XMLRPC response, autodetecting if it
  * is a fault response.
  *
  * @param string $response
  * @return boolean True if a valid XMLRPC response, false if a fault
  * response or invalid input
  */
 public function loadXml($response)
 {
     if (!is_string($response)) {
         $this->fault = new Fault(650);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     // @see ZF-12293 - disable external entities for security purposes
     $loadEntities = libxml_disable_entity_loader(true);
     $useInternalXmlErrors = libxml_use_internal_errors(true);
     try {
         $xml = new \SimpleXMLElement($response);
         libxml_disable_entity_loader($loadEntities);
         libxml_use_internal_errors($useInternalXmlErrors);
     } catch (\Exception $e) {
         libxml_disable_entity_loader($loadEntities);
         libxml_use_internal_errors($useInternalXmlErrors);
         // Not valid XML
         $this->fault = new Fault(651);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     if (!empty($xml->fault)) {
         // fault response
         $this->fault = new Fault();
         $this->fault->setEncoding($this->getEncoding());
         $this->fault->loadXml($response);
         return false;
     }
     if (empty($xml->params)) {
         // Invalid response
         $this->fault = new Fault(652);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     try {
         if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
             throw new Exception\ValueException('Missing XML-RPC value in XML');
         }
         $valueXml = $xml->params->param->value->asXML();
         $value = AbstractValue::getXmlRpcValue($valueXml, AbstractValue::XML_STRING);
     } catch (Exception\ValueException $e) {
         $this->fault = new Fault(653);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     $this->setReturnValue($value->getValue());
     return true;
 }
Example #5
0
 /**
  * Constructor
  *
  * @param  \Exception $e
  * @return Fault
  */
 public function __construct(\Exception $e)
 {
     $this->exception = $e;
     $code = 404;
     $message = 'Unknown error';
     foreach (array_keys(static::$faultExceptionClasses) as $class) {
         if ($e instanceof $class) {
             $code = $e->getCode();
             $message = $e->getMessage();
             break;
         }
     }
     parent::__construct($code, $message);
     // Notify exception observers, if present
     if (!empty(static::$observers)) {
         foreach (array_keys(static::$observers) as $observer) {
             $observer::observe($this);
         }
     }
 }
 /**
  * Load a response from an XML response
  *
  * Attempts to load a response from an XMLRPC response, autodetecting if it
  * is a fault response.
  *
  * @param string $response
  * @throws Exception\ValueException if invalid XML
  * @return bool True if a valid XMLRPC response, false if a fault
  * response or invalid input
  */
 public function loadXml($response)
 {
     if (!is_string($response)) {
         $this->fault = new Fault(650);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     try {
         $xml = XmlSecurity::scan($response);
     } catch (\ZendXml\Exception\RuntimeException $e) {
         $this->fault = new Fault(651);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     if (!empty($xml->fault)) {
         // fault response
         $this->fault = new Fault();
         $this->fault->setEncoding($this->getEncoding());
         $this->fault->loadXml($response);
         return false;
     }
     if (empty($xml->params)) {
         // Invalid response
         $this->fault = new Fault(652);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     try {
         if (!isset($xml->params) || !isset($xml->params->param) || !isset($xml->params->param->value)) {
             throw new Exception\ValueException('Missing XML-RPC value in XML');
         }
         $valueXml = $xml->params->param->value->asXML();
         $value = AbstractValue::getXmlRpcValue($valueXml, AbstractValue::XML_STRING);
     } catch (Exception\ValueException $e) {
         $this->fault = new Fault(653);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     $this->setReturnValue($value->getValue());
     return true;
 }
Example #7
0
 /**
  * Constructor
  *
  * @param  Exception $e
  * @return Zend\XmlRpc\Server\Fault
  */
 public function __construct(\Exception $e)
 {
     $this->_exception = $e;
     $code = 404;
     $message = 'Unknown error';
     $exceptionClass = get_class($e);
     foreach (array_keys(self::$_faultExceptionClasses) as $class) {
         if ($e instanceof $class) {
             $code = $e->getCode();
             $message = $e->getMessage();
             break;
         }
     }
     parent::__construct($code, $message);
     // Notify exception observers, if present
     if (!empty(self::$_observers)) {
         foreach (array_keys(self::$_observers) as $observer) {
             call_user_func(array($observer, 'observe'), $this);
         }
     }
 }
Example #8
0
 /**
  * Load XML and parse into request components
  *
  * @param string $request
  * @throws Exception\ValueException if invalid XML
  * @return bool True on success, false if an error occurred.
  */
 public function loadXml($request)
 {
     if (!is_string($request)) {
         $this->fault = new Fault(635);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     // @see ZF-12293 - disable external entities for security purposes
     $loadEntities = libxml_disable_entity_loader(true);
     $xmlErrorsFlag = libxml_use_internal_errors(true);
     try {
         $dom = new DOMDocument();
         $dom->loadXML($request);
         foreach ($dom->childNodes as $child) {
             if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
                 throw new Exception\ValueException('Invalid XML: Detected use of illegal DOCTYPE');
             }
         }
         ErrorHandler::start();
         $xml = simplexml_import_dom($dom);
         $error = ErrorHandler::stop();
         libxml_disable_entity_loader($loadEntities);
         libxml_use_internal_errors($xmlErrorsFlag);
     } catch (\Exception $e) {
         // Not valid XML
         $this->fault = new Fault(631);
         $this->fault->setEncoding($this->getEncoding());
         libxml_disable_entity_loader($loadEntities);
         libxml_use_internal_errors($xmlErrorsFlag);
         return false;
     }
     if (!$xml instanceof SimpleXMLElement || $error) {
         // Not valid XML
         $this->fault = new Fault(631);
         $this->fault->setEncoding($this->getEncoding());
         libxml_use_internal_errors($xmlErrorsFlag);
         return false;
     }
     // Check for method name
     if (empty($xml->methodName)) {
         // Missing method name
         $this->fault = new Fault(632);
         $this->fault->setEncoding($this->getEncoding());
         return false;
     }
     $this->method = (string) $xml->methodName;
     // Check for parameters
     if (!empty($xml->params)) {
         $types = array();
         $argv = array();
         foreach ($xml->params->children() as $param) {
             if (!isset($param->value)) {
                 $this->fault = new Fault(633);
                 $this->fault->setEncoding($this->getEncoding());
                 return false;
             }
             try {
                 $param = AbstractValue::getXmlRpcValue($param->value, AbstractValue::XML_STRING);
                 $types[] = $param->getType();
                 $argv[] = $param->getValue();
             } catch (\Exception $e) {
                 $this->fault = new Fault(636);
                 $this->fault->setEncoding($this->getEncoding());
                 return false;
             }
         }
         $this->types = $types;
         $this->params = $argv;
     }
     $this->xml = $request;
     return true;
 }
Example #9
0
 public function testGettingAllMethodSignaturesDegradesToLooping()
 {
     // system.listMethods() will return ['foo', 'bar']
     $whatListMethodsReturns = array('foo', 'bar');
     $response = $this->getServerResponseFor($whatListMethodsReturns);
     $this->httpAdapter->setResponse($response);
     // system.multicall() will return a fault
     $fault = new XmlRpc\Fault(7, 'bad method');
     $xml = $fault->saveXml();
     $response = $this->makeHttpResponseFrom($xml);
     $this->httpAdapter->addResponse($response);
     // system.methodSignature('foo') will return [['int'], ['int', 'string']]
     $fooSignatures = array(array('int'), array('int', 'string'));
     $response = $this->getServerResponseFor($fooSignatures);
     $this->httpAdapter->addResponse($response);
     // system.methodSignature('bar') will return [['boolean']]
     $barSignatures = array(array('boolean'));
     $response = $this->getServerResponseFor($barSignatures);
     $this->httpAdapter->addResponse($response);
     $i = $this->xmlrpcClient->getIntrospector();
     $expected = array('foo' => $fooSignatures, 'bar' => $barSignatures);
     $this->assertEquals($expected, $i->getSignatureForEachMethod());
     $request = $this->xmlrpcClient->getLastRequest();
     $this->assertEquals('system.methodSignature', $request->getMethod());
 }
Example #10
0
 public function testUnknownErrorIsUsedIfUnknownErrorCodeEndEmptyMessageIsPassed()
 {
     $fault = new XmlRpc\Fault(1234);
     $this->assertSame(1234, $fault->getCode());
     $this->assertSame('Unknown error', $fault->getMessage());
 }
Example #11
0
 public function testRpcMethodCallThrowsOnXmlRpcFault()
 {
     $code = 9;
     $message = 'foo';
     $fault = new XmlRpc\Fault($code, $message);
     $xml = $fault->saveXml();
     $response = $this->makeHttpResponseFrom($xml);
     $this->httpAdapter->setResponse($response);
     $this->setExpectedException('Zend\\XmlRpc\\Client\\Exception\\FaultException', $message, $code);
     $this->xmlrpcClient->call('foo');
 }