Skip to content

Commit

Permalink
WS-Adressing and WS-Security Filters
Browse files Browse the repository at this point in the history
  • Loading branch information
aschamberger committed Dec 11, 2011
1 parent 9643fd4 commit 1122df8
Show file tree
Hide file tree
Showing 11 changed files with 1,270 additions and 260 deletions.
8 changes: 8 additions & 0 deletions src/BeSimple/SoapClient/Curl.php
Expand Up @@ -144,6 +144,7 @@ public function exec($location, $request = null, $requestHeaders = array())
private function execManualRedirect($redirects = 0)
{
if ($redirects > $this->followLocationMaxRedirects) {

// TODO Redirection limit reached, aborting
return false;
}
Expand Down Expand Up @@ -171,9 +172,11 @@ private function execManualRedirect($redirects = 0)
}
$newUrl = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query'] ? '?' . $url['query'] : '');
curl_setopt($this->ch, CURLOPT_URL, $newUrl);

return $this->execManualRedirect($redirects++);
}
}

return $response;
}

Expand Down Expand Up @@ -225,8 +228,10 @@ public function getErrorMessage()
$errorCodeMapping = $this->getErrorCodeMapping();
$errorNumber = curl_errno($this->ch);
if (isset($errorCodeMapping[$errorNumber])) {

return $errorCodeMapping[$errorNumber];
}

return curl_error($this->ch);
}

Expand Down Expand Up @@ -258,6 +263,7 @@ public function getResponse()
public function getResponseBody()
{
$headerSize = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);

return substr($this->response, $headerSize);
}

Expand All @@ -279,6 +285,7 @@ public function getResponseContentType()
public function getResponseHeaders()
{
$headerSize = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);

return substr($this->response, 0, $headerSize);
}

Expand All @@ -300,6 +307,7 @@ public function getResponseStatusCode()
public function getResponseStatusMessage()
{
preg_match('/HTTP\/(1\.[0-1]+) ([0-9]{3}) (.*)/', $this->response, $matches);

return trim(array_pop($matches));
}
}
172 changes: 172 additions & 0 deletions src/BeSimple/SoapClient/FilterHelper.php
@@ -0,0 +1,172 @@
<?php

/*
* This file is part of the BeSimpleSoapClient.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace BeSimple\SoapClient;

/**
* Soap request/response filter helper for manipulating SOAP messages.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class FilterHelper
{
/**
* DOMDocument on which the helper functions operate.
*
* @var \DOMDocument
*/
protected $domDocument = null;

/**
* Namespaces added.
*
* @var array(string=>string)
*/
protected $namespaces = array();

/**
* Constructor.
*
* @param \DOMDocument $domDocument
*/
public function __construct(\DOMDocument $domDocument)
{
$this->domDocument = $domDocument;
}

/**
* Add new soap header.
*
* @param \DOMElement $node
* @param boolean $mustUnderstand
* @param string $actor
* @param string $soapVersion
* @return void
*/
public function addHeaderElement(\DOMElement $node, $mustUnderstand = null, $actor = null, $soapVersion = SOAP_1_1)
{
$root = $this->domDocument->documentElement;
$namespace = $root->namespaceURI;
$prefix = $root->prefix;
if (null !== $mustUnderstand) {
$node->appendChild(new \DOMAttr($prefix . ':mustUnderstand', (int)$mustUnderstand));
}
if (null !== $actor) {
$attributeName = ($soapVersion == SOAP_1_1) ? 'actor' : 'role';
$node->appendChild(new \DOMAttr($prefix . ':' . $attributeName, $actor));
}
$nodeListHeader = $root->getElementsByTagNameNS($namespace, 'Header');
// add header if not there
if ($nodeListHeader->length == 0) {
// new header element
$header = $this->domDocument->createElementNS($namespace, $prefix . ':Header');
// try to add it before body
$nodeListBody = $root->getElementsByTagNameNS($namespace, 'Body');
if ($nodeListBody->length == 0) {
$root->appendChild($header);
} else {
$body = $nodeListBody->item(0);
$header = $body->parentNode->insertBefore($header, $body);
}
$header->appendChild($node);
} else {
$nodeListHeader->item(0)->appendChild($node);
}
}

/**
* Add new soap body element.
*
* @param \DOMElement $node
* @return void
*/
public function addBodyElement(\DOMElement $node)
{
$root = $this->domDocument->documentElement;
$namespace = $root->namespaceURI;
$prefix = $root->prefix;
$nodeList = $this->domDocument->getElementsByTagNameNS($namespace, 'Body');
// add body if not there
if ($nodeList->length == 0) {
// new body element
$body = $this->domDocument->createElementNS($namespace, $prefix . ':Body');
$root->appendChild($body);
$body->appendChild($node);
} else {
$nodeList->item(0)->appendChild($node);
}
}

/**
* Add new namespace to root tag.
*
* @param string $prefix
* @param string $namespaceURI
* @return void
*/
public function addNamespace($prefix, $namespaceURI)
{
if (!isset($this->namespaces[$namespaceURI])) {
$root = $this->domDocument->documentElement;
$root->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $prefix, $namespaceURI);
$this->namespaces[$namespaceURI] = $prefix;
}
}

/**
* Create new element for given namespace.
*
* @param string $namespaceURI
* @param string $name
* @param string $value
* @return \DOMElement
*/
public function createElement($namespaceURI, $name, $value = null)
{
$prefix = $this->namespaces[$namespaceURI];

return $this->domDocument->createElementNS($namespaceURI, $prefix . ':' . $name, $value);
}

/**
* Add new attribute to element with given namespace.
*
* @param \DOMElement $element
* @param string $namespaceURI
* @param string $name
* @param string $value
* @return void
*/
public function setAttribute(\DOMElement $element, $namespaceURI = null, $name, $value)
{
if (null !== $namespaceURI) {
$prefix = $this->namespaces[$namespaceURI];
$element->setAttributeNS($namespaceURI, $prefix . ':' . $name, $value);
} else {
$element->setAttribute($name, $value);
}
}

/**
* Register namespace.
*
* @param string $prefix
* @param string $namespaceURI
* @return void
*/
public function registerNamespace($prefix, $namespaceURI)
{
if (!isset($this->namespaces[$namespaceURI])) {
$this->namespaces[$namespaceURI] = $prefix;
}
}
}

0 comments on commit 1122df8

Please sign in to comment.