Soap server with attachments refactoring

This commit is contained in:
Petr Bechyně 2016-11-08 15:42:52 +01:00
parent 8d033f9afc
commit 84c37b1d24
23 changed files with 511 additions and 502 deletions

View File

@ -50,11 +50,10 @@ class Curl
/** /**
* Constructor. * Constructor.
* *
* @todo: do not use options as Array
* @param array $options Options array from SoapClient constructor * @param array $options Options array from SoapClient constructor
* @param int $followLocationMaxRedirects Redirection limit for Location header * @param int $followLocationMaxRedirects Redirection limit for Location header
*/ */
public function __construct(array $options = array(), $followLocationMaxRedirects = 10) public function __construct(array $options = [], $followLocationMaxRedirects = 10)
{ {
// set the default HTTP user agent // set the default HTTP user agent
if (!isset($options['user_agent'])) { if (!isset($options['user_agent'])) {
@ -126,7 +125,7 @@ class Curl
/** /**
* Execute HTTP request. * Execute HTTP request.
* Returns true if request was successfull. * Returns true if request was successful.
* *
* @param string $location HTTP location * @param string $location HTTP location
* @param string $request Request body * @param string $request Request body

View File

@ -16,6 +16,7 @@ use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\Mime\MultiPart as MimeMultiPart; use BeSimple\SoapCommon\Mime\MultiPart as MimeMultiPart;
use BeSimple\SoapCommon\Mime\Parser as MimeParser; use BeSimple\SoapCommon\Mime\Parser as MimeParser;
use BeSimple\SoapCommon\Mime\Part as MimePart; use BeSimple\SoapCommon\Mime\Part as MimePart;
use BeSimple\SoapCommon\Mime\Part;
use BeSimple\SoapCommon\SoapRequest; use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFilter; use BeSimple\SoapCommon\SoapRequestFilter;
use BeSimple\SoapCommon\SoapResponse; use BeSimple\SoapCommon\SoapResponse;
@ -31,31 +32,27 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
public function filterRequest(SoapRequest $request, $attachmentType) public function filterRequest(SoapRequest $request, $attachmentType)
{ {
$attachmentsToSend = $request->getAttachments(); $attachmentsToSend = $request->getAttachments();
// build mime message if we have attachments
if (count($attachmentsToSend) > 0) { if (count($attachmentsToSend) > 0) {
$multipart = new MimeMultiPart(); $multipart = new MimeMultiPart('Part_' . rand(10, 15) . '_' . uniqid() . '.' . uniqid());
$soapPart = new MimePart($request->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT); $soapPart = new MimePart($request->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT);
$soapVersion = $request->getVersion(); $soapVersion = $request->getVersion();
// change content type headers for MTOM with SOAP 1.1
if ($soapVersion == SOAP_1_1 && $attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) { if ($soapVersion == SOAP_1_1 && $attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) {
$multipart->setHeader('Content-Type', 'type', 'application/xop+xml'); $multipart->setHeader('Content-Type', 'type', 'application/xop+xml');
$multipart->setHeader('Content-Type', 'start-info', 'text/xml'); $multipart->setHeader('Content-Type', 'start-info', 'text/xml');
$soapPart->setHeader('Content-Type', 'application/xop+xml'); $soapPart->setHeader('Content-Type', 'application/xop+xml');
$soapPart->setHeader('Content-Type', 'type', 'text/xml'); $soapPart->setHeader('Content-Type', 'type', 'text/xml');
} } elseif ($soapVersion == SOAP_1_2) {
// change content type headers for SOAP 1.2
elseif ($soapVersion == SOAP_1_2) {
$multipart->setHeader('Content-Type', 'type', 'application/soap+xml'); $multipart->setHeader('Content-Type', 'type', 'application/soap+xml');
$soapPart->setHeader('Content-Type', 'application/soap+xml'); $soapPart->setHeader('Content-Type', 'application/soap+xml');
} }
$multipart->addPart($soapPart, true); $multipart->addPart($soapPart, true);
foreach ($attachmentsToSend as $cid => $attachment) { foreach ($attachmentsToSend as $cid => $attachment) {
$multipart->addPart($attachment, false); $multipart->addPart($attachment, false);
} }
$request->setContent($multipart->getMimeMessage()); $request->setContent($multipart->getMimeMessage());
// TODO
$headers = $multipart->getHeadersForHttp(); $headers = $multipart->getHeadersForHttp();
list(, $contentType) = explode(': ', $headers[0]); list(, $contentType) = explode(': ', $headers[0]);
@ -67,33 +64,26 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
public function filterResponse(SoapResponse $response, $attachmentType) public function filterResponse(SoapResponse $response, $attachmentType)
{ {
$attachmentsReceived = []; $multiPartMessage = MimeParser::parseMimeMessage(
$response->getContent(),
['Content-Type' => trim($response->getContentType())]
);
$soapPart = $multiPartMessage->getMainPart();
$attachments = $multiPartMessage->getAttachments();
// check content type if it is a multipart mime message $response->setContent($this->sanitizePhpExceptionOnHrefs($soapPart));
$responseContentType = $response->getContentType(); $response->setContentType($soapPart->getHeader('Content-Type'));
if (false !== stripos($responseContentType, 'multipart/related')) { if (count($attachments) > 0) {
// parse mime message $response->setAttachments($attachments);
$headers = array(
'Content-Type' => trim($responseContentType),
);
$multipart = MimeParser::parseMimeMessage($response->getContent(), $headers);
// get soap payload and update SoapResponse object
$soapPart = $multipart->getPart();
// convert href -> myhref for external references as PHP throws exception in this case
// http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/soap/php_encoding.c?view=markup#l3436
$content = preg_replace('/href=(?!#)/', 'myhref=', $soapPart->getContent());
$response->setContent($content);
$response->setContentType($soapPart->getHeader('Content-Type'));
// store attachments
$attachments = $multipart->getParts(false);
foreach ($attachments as $cid => $attachment) {
$attachmentsReceived[$cid] = $attachment;
}
}
if (count($attachmentsReceived) > 0) {
$response->setAttachments($attachmentsReceived);
} }
return $response; return $response;
} }
private function sanitizePhpExceptionOnHrefs(Part $soapPart)
{
// convert href -> myhref for external references as PHP throws exception in this case
// http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/soap/php_encoding.c?view=markup#l3436
return preg_replace('/href=(?!#)/', 'myhref=', $soapPart->getContent());
}
} }

View File

@ -12,9 +12,7 @@
namespace BeSimple\SoapClient; namespace BeSimple\SoapClient;
use BeSimple\SoapCommon\Helper; use BeSimple\SoapCommon\SoapKernel;
use BeSimple\SoapCommon\Converter\MtomTypeConverter;
use BeSimple\SoapCommon\Converter\SwaTypeConverter;
use BeSimple\SoapCommon\SoapOptions\SoapOptions; use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapRequest; use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFactory; use BeSimple\SoapCommon\SoapRequestFactory;
@ -30,20 +28,10 @@ use BeSimple\SoapCommon\SoapRequestFactory;
class SoapClient extends \SoapClient class SoapClient extends \SoapClient
{ {
protected $soapVersion; protected $soapVersion;
protected $tracingEnabled;
/** protected $soapClientOptions;
* Tracing enabled? protected $soapOptions;
* protected $curl;
* @var boolean
*/
protected $tracingEnabled = false;
/**
* cURL instance.
*
* @var \BeSimple\SoapClient\Curl
*/
protected $curl = null;
/** /**
* Last request headers. * Last request headers.
@ -73,13 +61,6 @@ class SoapClient extends \SoapClient
*/ */
private $lastResponse = ''; private $lastResponse = '';
/**
* Soap kernel.
*
* @var \BeSimple\SoapClient\SoapKernel
*/
protected $soapKernel = null;
/** /**
* Constructor. * Constructor.
* *
@ -88,44 +69,99 @@ class SoapClient extends \SoapClient
*/ */
public function __construct(SoapClientOptions $soapClientOptions, SoapOptions $soapOptions) public function __construct(SoapClientOptions $soapClientOptions, SoapOptions $soapOptions)
{ {
$this->soapKernel = new SoapKernel();
$this->soapVersion = $soapOptions->getSoapVersion(); $this->soapVersion = $soapOptions->getSoapVersion();
$this->tracingEnabled = $soapClientOptions->getTrace(); $this->tracingEnabled = $soapClientOptions->getTrace();
$this->soapClientOptions = $soapClientOptions;
$this->soapOptions = $soapOptions;
// @todo: refactor SoapClient: do not use $options as array // @todo: refactor SoapClient: do not use $options as array: refactor Curl
$options = $this->configureMime($soapOptions->toArray());
// @todo: refactor SoapClient: do not use $options as array
$this->curl = new Curl($soapClientOptions->toArray()); $this->curl = new Curl($soapClientOptions->toArray());
// @todo: refactor SoapClient: do not use $options as array $wsdlFile = $this->loadWsdl(
$wsdlFile = $this->loadWsdl($soapOptions->getWsdlFile(), $soapOptions->toArray()); $soapOptions->getWsdlFile(),
$soapOptions->getWsdlCacheType()
);
parent::__construct($wsdlFile, $options); parent::__construct(
$wsdlFile,
$soapClientOptions->toArray() + $soapOptions->toArray()
);
} }
/**
* Custom request method to be able to modify the SOAP messages.
* $oneWay parameter is not used at the moment.
*
* @param string $request Request string
* @param string $location Location
* @param string $action SOAP action
* @param int $version SOAP version
* @param int $oneWay 0|1
*
* @return string
*/
public function __doRequest($request, $location, $action, $version, $oneWay = 0)
{
$soapRequest = $this->createSoapRequest($location, $action, $version, $request);
$soapResponse = $this->getSoapResponseFromRequest($soapRequest);
return $soapResponse->getContent();
}
private function createSoapRequest($location, $action, $version, $request)
{
$soapRequest = SoapRequestFactory::create($location, $action, $version, $request);
if ($this->soapOptions->hasAttachments()) {
$soapKernel = new SoapKernel();
$soapRequest = $soapKernel->filterRequest(
$soapRequest,
$this->getAttachmentFilters(),
$this->soapOptions->getAttachmentType()
);
}
return $soapRequest;
}
/**
* Runs the currently registered request filters on the request, performs
* the HTTP request and runs the response filters.
*
* @param SoapRequest $soapRequest SOAP request object
*
* @return SoapResponse
*/
private function getSoapResponseFromRequest(SoapRequest $soapRequest)
{
$soapResponse = $this->performHttpSoapRequest($soapRequest);
if ($this->soapOptions->hasAttachments()) {
$soapKernel = new SoapKernel();
$soapKernel->filterResponse($soapResponse, $this->getAttachmentFilters(), $this->soapOptions->getAttachmentType());
}
return $soapResponse;
}
/** /**
* Perform HTTP request with cURL. * Perform HTTP request with cURL.
* *
* @param SoapRequest $soapRequest SoapRequest object * @param SoapRequest $soapRequest SoapRequest object
*
* @return SoapResponse * @return SoapResponse
*/ */
private function __doHttpRequest(SoapRequest $soapRequest) private function performHttpSoapRequest(SoapRequest $soapRequest)
{ {
// HTTP headers // HTTP headers
$soapVersion = $soapRequest->getVersion(); $soapVersion = $soapRequest->getVersion();
$soapAction = $soapRequest->getAction(); $soapAction = $soapRequest->getAction();
if (SOAP_1_1 == $soapVersion) { if (SOAP_1_1 == $soapVersion) {
$headers = array( $headers = [
'Content-Type:' . $soapRequest->getContentType(), 'Content-Type:' . $soapRequest->getContentType(),
'SOAPAction: "' . $soapAction . '"', 'SOAPAction: "' . $soapAction . '"',
); ];
} else { } else {
$headers = array( $headers = [
'Content-Type:' . $soapRequest->getContentType() . '; action="' . $soapAction . '"', 'Content-Type:' . $soapRequest->getContentType() . '; action="' . $soapAction . '"',
); ];
} }
$location = $soapRequest->getLocation(); $location = $soapRequest->getLocation();
@ -135,8 +171,7 @@ class SoapClient extends \SoapClient
$options = $this->filterRequestOptions($soapRequest); $options = $this->filterRequestOptions($soapRequest);
// execute HTTP request with cURL $responseSuccessful = $this->curl->exec(
$responseSuccessfull = $this->curl->exec(
$location, $location,
$content, $content,
$headers, $headers,
@ -149,7 +184,7 @@ class SoapClient extends \SoapClient
$this->lastRequest = $soapRequest->getContent(); $this->lastRequest = $soapRequest->getContent();
} }
// in case of an error while making the http request throw a soapFault // in case of an error while making the http request throw a soapFault
if ($responseSuccessfull === false) { if ($responseSuccessful === false) {
// get error message from curl // get error message from curl
$faultstring = $this->curl->getErrorMessage(); $faultstring = $this->curl->getErrorMessage();
throw new \SoapFault('HTTP', $faultstring); throw new \SoapFault('HTTP', $faultstring);
@ -171,53 +206,6 @@ class SoapClient extends \SoapClient
return $soapResponse; return $soapResponse;
} }
/**
* Custom request method to be able to modify the SOAP messages.
* $oneWay parameter is not used at the moment.
*
* @todo: refactor SoapClient: refactoring starts from here
* @param string $request Request string
* @param string $location Location
* @param string $action SOAP action
* @param int $version SOAP version
* @param int $oneWay 0|1
*
* @return string
*/
public function __doRequest($request, $location, $action, $version, $oneWay = 0)
{
// wrap request data in SoapRequest object
$soapRequest = SoapRequestFactory::create($location, $action, $version, $request);
// do actual SOAP request
$soapResponse = $this->__doRequest2($soapRequest);
// return SOAP response to ext/soap
return $soapResponse->getContent();
}
/**
* Runs the currently registered request filters on the request, performs
* the HTTP request and runs the response filters.
*
* @param SoapRequest $soapRequest SOAP request object
*
* @return SoapResponse
*/
protected function __doRequest2(SoapRequest $soapRequest)
{
// run SoapKernel on SoapRequest
$this->soapKernel->filterRequest($soapRequest);
// perform HTTP request with cURL
$soapResponse = $this->__doHttpRequest($soapRequest);
// run SoapKernel on SoapResponse
$this->soapKernel->filterResponse($soapResponse);
return $soapResponse;
}
/** /**
* Filters HTTP headers which will be sent * Filters HTTP headers which will be sent
* *
@ -240,7 +228,7 @@ class SoapClient extends \SoapClient
*/ */
protected function filterRequestOptions(SoapRequest $soapRequest) protected function filterRequestOptions(SoapRequest $soapRequest)
{ {
return array(); return [];
} }
/** /**
@ -284,81 +272,34 @@ class SoapClient extends \SoapClient
} }
/** /**
* Get SoapKernel instance. * @param string $wsdl
* * @param int $wsdlCache
* @return \BeSimple\SoapClient\SoapKernel * @param bool $resolveRemoteIncludes
*/
public function getSoapKernel()
{
return $this->soapKernel;
}
private function configureMime(array $options)
{
// @todo: PBe: refactor same as SoapServer
if (Helper::ATTACHMENTS_TYPE_BASE64 !== $options['attachment_type']) {
// register mime filter in SoapKernel
$mimeFilter = new MimeFilter($options['attachment_type']);
$this->soapKernel->registerFilter($mimeFilter);
// configure type converter
if (Helper::ATTACHMENTS_TYPE_SWA === $options['attachment_type']) {
$converter = new SwaTypeConverter();
$converter->setKernel($this->soapKernel);
} elseif (Helper::ATTACHMENTS_TYPE_MTOM === $options['attachment_type']) {
$xmlMimeFilter = new XmlMimeFilter($options['attachment_type']);
$this->soapKernel->registerFilter($xmlMimeFilter);
$converter = new MtomTypeConverter();
$converter->setKernel($this->soapKernel);
}
// configure typemap
if (!isset($options['typemap'])) {
$options['typemap'] = array();
}
$options['typemap'][] = array(
'type_name' => $converter->getTypeName(),
'type_ns' => $converter->getTypeNamespace(),
'from_xml' => function($input) use ($converter) {
return $converter->convertXmlToPhp($input);
},
'to_xml' => function($input) use ($converter) {
return $converter->convertPhpToXml($input);
},
);
}
return $options;
}
/**
* Downloads WSDL files with cURL. Uses all SoapClient options for
* authentication. Uses the WSDL_CACHE_* constants and the 'soap.wsdl_*'
* ini settings. Does only file caching as SoapClient only supports a file
* name parameter.
*
* @param string $wsdl WSDL file
* @param array(string=>mixed) $options Options array
* *
* @return string * @return string
*/ */
protected function loadWsdl($wsdl, array $options) private function loadWsdl($wsdl, $wsdlCache, $resolveRemoteIncludes = true)
{ {
// option to resolve wsdl/xsd includes
$resolveRemoteIncludes = true;
if (isset($options['resolve_wsdl_remote_includes'])) {
$resolveRemoteIncludes = $options['resolve_wsdl_remote_includes'];
}
// option to enable cache
$wsdlCache = WSDL_CACHE_DISK;
if (isset($options['cache_wsdl'])) {
$wsdlCache = $options['cache_wsdl'];
}
$wsdlDownloader = new WsdlDownloader($this->curl, $resolveRemoteIncludes, $wsdlCache); $wsdlDownloader = new WsdlDownloader($this->curl, $resolveRemoteIncludes, $wsdlCache);
try { try {
$cacheFileName = $wsdlDownloader->download($wsdl); $cacheFileName = $wsdlDownloader->download($wsdl);
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
throw new \SoapFault('WSDL', "SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl . "' : failed to load external entity \"" . $wsdl . "\""); throw new \SoapFault('WSDL', "SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl . "'");
} }
return $cacheFileName; return $cacheFileName;
} }
private function getAttachmentFilters()
{
$filters = [];
if ($this->soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) {
$filters[] = new MimeFilter();
}
if ($this->soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_MTOM) {
$filters[] = new XmlMimeFilter();
}
return $filters;
}
} }

View File

@ -25,54 +25,22 @@ use BeSimple\SoapCommon\Helper;
*/ */
class WsdlDownloader class WsdlDownloader
{ {
/** protected $curl;
* Cache enabled. protected $resolveRemoteIncludes = true;
*
* @var bool
*/
protected $cacheEnabled; protected $cacheEnabled;
/**
* Cache dir.
*
* @var string
*/
protected $cacheDir; protected $cacheDir;
/**
* Cache TTL.
*
* @var int
*/
protected $cacheTtl; protected $cacheTtl;
/** /**
* cURL instance for downloads. * @param Curl $curl
* * @param int $cacheWsdl = Cache::TYPE_NONE|Cache::WSDL_CACHE_DISK|Cache::WSDL_CACHE_BOTH|Cache::WSDL_CACHE_MEMORY
* @var unknown_type * @param boolean $resolveRemoteIncludes
*/ */
protected $curl; public function __construct(Curl $curl, $cacheWsdl, $resolveRemoteIncludes = true)
/**
* Resolve WSDl/XSD includes.
*
* @var boolean
*/
protected $resolveRemoteIncludes = true;
/**
* Constructor.
*
* @param \BeSimple\SoapClient\Curl $curl Curl instance
* @param boolean $resolveRemoteIncludes WSDL/XSD include enabled?
* @param boolean $cacheWsdl Cache constant
*/
public function __construct(Curl $curl, $resolveRemoteIncludes = true, $cacheWsdl = Cache::TYPE_DISK)
{ {
$this->curl = $curl; $this->curl = $curl;
$this->resolveRemoteIncludes = (Boolean) $resolveRemoteIncludes; $this->resolveRemoteIncludes = $resolveRemoteIncludes;
// get current WSDL caching config
$this->cacheEnabled = $cacheWsdl === Cache::TYPE_NONE ? Cache::DISABLED : Cache::ENABLED == Cache::isEnabled(); $this->cacheEnabled = $cacheWsdl === Cache::TYPE_NONE ? Cache::DISABLED : Cache::ENABLED == Cache::isEnabled();
$this->cacheDir = Cache::getDirectory(); $this->cacheDir = Cache::getDirectory();
$this->cacheTtl = Cache::getLifetime(); $this->cacheTtl = Cache::getLifetime();

View File

@ -0,0 +1,12 @@
<?php
namespace BeSimple\SoapCommon;
use BeSimple\SoapCommon\Storage\RequestHandlerAttachmentsStorage;
/** @todo: PBe - refactor this interface + usages -> inconsistent - adding storage, getting items - WTF APi? */
interface AttachmentsHandler
{
public function addAttachmentStorage(RequestHandlerAttachmentsStorage $requestHandlerAttachmentsStorage);
public function getAttachmentsFromStorage();
}

View File

@ -14,16 +14,13 @@ namespace BeSimple\SoapCommon\Converter;
use BeSimple\SoapCommon\Helper; use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\Mime\Part as MimePart; use BeSimple\SoapCommon\Mime\Part as MimePart;
use BeSimple\SoapCommon\SoapKernel;
use BeSimple\SoapCommon\Converter\SoapKernelAwareInterface;
use BeSimple\SoapCommon\Converter\TypeConverterInterface;
/** /**
* MTOM type converter. * MTOM type converter.
* *
* @author Andreas Schamberger <mail@andreass.net> * @author Andreas Schamberger <mail@andreass.net>
*/ */
class MtomTypeConverter implements TypeConverterInterface, SoapKernelAwareInterface class MtomTypeConverter implements TypeConverterInterface
{ {
/** /**
* @var \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance * @var \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance
@ -54,25 +51,6 @@ class MtomTypeConverter implements TypeConverterInterface, SoapKernelAwareInterf
$doc = new \DOMDocument(); $doc = new \DOMDocument();
$doc->loadXML($data); $doc->loadXML($data);
$includes = $doc->getElementsByTagNameNS(Helper::NS_XOP, 'Include');
$include = $includes->item(0);
// convert href -> myhref for external references as PHP throws exception in this case
// http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/soap/php_encoding.c?view=markup#l3436
$ref = $include->getAttribute('myhref');
if ('cid:' === substr($ref, 0, 4)) {
$contentId = urldecode(substr($ref, 4));
if (null !== ($part = $this->soapKernel->getAttachment($contentId))) {
return $part->getContent();
} else {
return null;
}
}
return $data; return $data;
} }
@ -84,8 +62,6 @@ class MtomTypeConverter implements TypeConverterInterface, SoapKernelAwareInterf
$part = new MimePart($data); $part = new MimePart($data);
$contentId = trim($part->getHeader('Content-ID'), '<>'); $contentId = trim($part->getHeader('Content-ID'), '<>');
$this->soapKernel->addAttachment($part);
$doc = new \DOMDocument(); $doc = new \DOMDocument();
$node = $doc->createElement($this->getTypeName()); $node = $doc->createElement($this->getTypeName());
$doc->appendChild($node); $doc->appendChild($node);
@ -97,12 +73,4 @@ class MtomTypeConverter implements TypeConverterInterface, SoapKernelAwareInterf
return $doc->saveXML(); return $doc->saveXML();
} }
/**
* {@inheritDoc}
*/
public function setKernel(SoapKernel $soapKernel)
{
$this->soapKernel = $soapKernel;
}
} }

View File

@ -1,32 +0,0 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (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\SoapCommon\Converter;
use BeSimple\SoapCommon\SoapKernel;
/**
* Internal type converter interface.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
interface SoapKernelAwareInterface
{
/**
* Set SoapKernel instance.
*
* @param \BeSimple\SoapCommon\SoapKernel $soapKernel SoapKernel instance
*
* @return void
*/
function setKernel(SoapKernel $soapKernel);
}

View File

@ -35,23 +35,6 @@ class SwaTypeConverter implements TypeConverterInterface
$doc = new \DOMDocument(); $doc = new \DOMDocument();
$doc->loadXML($data); $doc->loadXML($data);
// convert href -> myhref for external references as PHP throws exception in this case
// http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/soap/php_encoding.c?view=markup#l3436
$ref = $doc->documentElement->getAttribute('myhref');
if ('cid:' === substr($ref, 0, 4)) {
$contentId = urldecode(substr($ref, 4));
// @todo-critical: ci je nyni zodpovednost vygetovat attachmenty
if (null !== ($part = $this->soapKernel->getAttachment($contentId))) {
return $part->getContent();
} else {
return null;
}
}
return $data; return $data;
} }
@ -63,9 +46,6 @@ class SwaTypeConverter implements TypeConverterInterface
$part = new MimePart($data); $part = new MimePart($data);
$contentId = trim($part->getHeader('Content-ID'), '<>'); $contentId = trim($part->getHeader('Content-ID'), '<>');
// @todo-critical: ci je nyni zodpovednost nastrkat attachmenty
//$this->soapKernel->addAttachment($part);
return sprintf('<%s href="%s"/>', $this->getTypeName(), 'cid:' . $contentId); return sprintf('<%s href="%s"/>', $this->getTypeName(), 'cid:' . $contentId);
} }
} }

View File

@ -12,6 +12,7 @@
namespace BeSimple\SoapCommon\Mime; namespace BeSimple\SoapCommon\Mime;
use Exception;
use BeSimple\SoapCommon\Helper; use BeSimple\SoapCommon\Helper;
/** /**
@ -38,16 +39,14 @@ class MultiPart extends PartHeader
/** /**
* Mime parts. * Mime parts.
* *
* @var array(\BeSimple\SoapCommon\Mime\Part) * @var \BeSimple\SoapCommon\Mime\Part[]
*/ */
protected $parts = array(); protected $parts = [];
/** /**
* Construct new mime object. * Construct new mime object.
* *
* @param string $boundary Boundary string * @param string $boundary
*
* @return void
*/ */
public function __construct($boundary = null) public function __construct($boundary = null)
{ {
@ -55,10 +54,9 @@ class MultiPart extends PartHeader
$this->setHeader('Content-Type', 'multipart/related'); $this->setHeader('Content-Type', 'multipart/related');
$this->setHeader('Content-Type', 'type', 'text/xml'); $this->setHeader('Content-Type', 'type', 'text/xml');
$this->setHeader('Content-Type', 'charset', 'utf-8'); $this->setHeader('Content-Type', 'charset', 'utf-8');
if (is_null($boundary)) { if ($boundary !== null) {
$boundary = $this->generateBoundary(); $this->setHeader('Content-Type', 'boundary', $boundary);
} }
$this->setHeader('Content-Type', 'boundary', $boundary);
} }
/** /**
@ -73,10 +71,11 @@ class MultiPart extends PartHeader
$message = ($withHeaders === true) ? $this->generateHeaders() : ""; $message = ($withHeaders === true) ? $this->generateHeaders() : "";
// add parts // add parts
foreach ($this->parts as $part) { foreach ($this->parts as $part) {
$message .= "\r\n" . '--' . $this->getHeader('Content-Type', 'boundary') . "\r\n"; $message .= "\n" . '--' . $this->getHeader('Content-Type', 'boundary') . "\n";
$message .= $part->getMessagePart(); $message .= $part->getMessagePart();
} }
$message .= "\r\n" . '--' . $this->getHeader('Content-Type', 'boundary') . '--'; $message .= "\n" . '--' . $this->getHeader('Content-Type', 'boundary') . '--';
return $message; return $message;
} }
@ -84,22 +83,23 @@ class MultiPart extends PartHeader
* Get string array with MIME headers for usage in HTTP header (with CURL). * Get string array with MIME headers for usage in HTTP header (with CURL).
* Only 'Content-Type' and 'Content-Description' headers are returned. * Only 'Content-Type' and 'Content-Description' headers are returned.
* *
* @return arrray(string) * @return string[]
*/ */
public function getHeadersForHttp() public function getHeadersForHttp()
{ {
$allowed = array( $allowedHeaders = [
'Content-Type', 'Content-Type',
'Content-Description', 'Content-Description',
); ];
$headers = array(); $headers = [];
foreach ($this->headers as $fieldName => $value) { foreach ($this->headers as $fieldName => $value) {
if (in_array($fieldName, $allowed)) { if (in_array($fieldName, $allowedHeaders)) {
$fieldValue = $this->generateHeaderFieldValue($value); $fieldValue = $this->generateHeaderFieldValue($value);
// for http only ISO-8859-1 // for http only ISO-8859-1
$headers[] = $fieldName . ': '. iconv('utf-8', 'ISO-8859-1//TRANSLIT', $fieldValue); $headers[] = $fieldName . ': '. iconv('utf-8', 'ISO-8859-1//TRANSLIT', $fieldValue);
} }
} }
return $headers; return $headers;
} }
@ -122,44 +122,51 @@ class MultiPart extends PartHeader
} }
/** /**
* Get part with given content id. If there is no content id given it * Get part with given content id.
* returns the main part that is defined through the content-id start
* parameter.
* *
* @param string $contentId Content id of desired part * @param string $contentId Content id of desired part
* *
* @return \BeSimple\SoapCommon\Mime\Part|null * @return \BeSimple\SoapCommon\Mime\Part
*/ */
public function getPart($contentId = null) public function getPart($contentId)
{ {
if (is_null($contentId)) {
$contentId = $this->mainPartContentId;
}
if (isset($this->parts[$contentId])) { if (isset($this->parts[$contentId])) {
return $this->parts[$contentId]; return $this->parts[$contentId];
} }
return null;
throw new Exception('MimePart not found by ID: ' . $contentId);
} }
/** /**
* Get all parts. * Get main part.
* *
* @param boolean $includeMainPart Should main part be in result set * @return \BeSimple\SoapCommon\Mime\Part
*
* @return array(\BeSimple\SoapCommon\Mime\Part)
*/ */
public function getParts($includeMainPart = false) public function getMainPart()
{ {
if ($includeMainPart === true) { foreach ($this->parts as $cid => $part) {
$parts = $this->parts; if ($cid === $this->mainPartContentId) {
} else { return $part;
$parts = array();
foreach ($this->parts as $cid => $part) {
if ($cid != $this->mainPartContentId) {
$parts[$cid] = $part;
}
} }
} }
throw new Exception('SoapRequest error: main part not found by Id: ' . $this->mainPartContentId);
}
/**
* Get attachment parts.
*
* @return \BeSimple\SoapCommon\Mime\Part[]
*/
public function getAttachments()
{
$parts = [];
foreach ($this->parts as $cid => $part) {
if ($cid !== $this->mainPartContentId) {
$parts[$cid] = $part;
}
}
return $parts; return $parts;
} }
@ -168,7 +175,7 @@ class MultiPart extends PartHeader
* *
* @return string * @return string
*/ */
protected function generateBoundary() public function generateBoundary()
{ {
return 'urn:uuid:' . Helper::generateUUID(); return 'urn:uuid:' . Helper::generateUUID();
} }

View File

@ -27,7 +27,7 @@ class Parser
* *
* @return \BeSimple\SoapCommon\Mime\MultiPart * @return \BeSimple\SoapCommon\Mime\MultiPart
*/ */
public static function parseMimeMessage($mimeMessage, array $headers = array()) public static function parseMimeMessage($mimeMessage, array $headers = [])
{ {
$boundary = null; $boundary = null;
$start = null; $start = null;
@ -37,7 +37,7 @@ class Parser
// add given headers, e.g. coming from HTTP headers // add given headers, e.g. coming from HTTP headers
if (count($headers) > 0) { if (count($headers) > 0) {
foreach ($headers as $name => $value) { foreach ($headers as $name => $value) {
if ($name == 'Content-Type') { if ($name === 'Content-Type') {
self::parseContentTypeHeader($multipart, $name, $value); self::parseContentTypeHeader($multipart, $name, $value);
$boundary = $multipart->getHeader('Content-Type', 'boundary'); $boundary = $multipart->getHeader('Content-Type', 'boundary');
$start = $multipart->getHeader('Content-Type', 'start'); $start = $multipart->getHeader('Content-Type', 'start');
@ -49,43 +49,57 @@ class Parser
} }
$content = ''; $content = '';
$currentPart = $multipart; $currentPart = $multipart;
$lines = preg_split("/(\r\n)/", $mimeMessage); $lines = preg_split("/(\r\n)|(\n)/", $mimeMessage);
foreach ($lines as $line) { if (self::hasBoundary($lines)) {
// ignore http status code and POST * foreach ($lines as $line) {
if (substr($line, 0, 5) == 'HTTP/' || substr($line, 0, 4) == 'POST') { // ignore http status code and POST *
continue; if (substr($line, 0, 5) == 'HTTP/' || substr($line, 0, 4) == 'POST') {
}
if (isset($currentHeader)) {
if (isset($line[0]) && ($line[0] === ' ' || $line[0] === "\t")) {
$currentHeader .= $line;
continue; continue;
} }
if (strpos($currentHeader, ':') !== false) { if (isset($currentHeader)) {
list($headerName, $headerValue) = explode(':', $currentHeader, 2); if (isset($line[0]) && ($line[0] === ' ' || $line[0] === "\t")) {
$headerValue = iconv_mime_decode($headerValue, 0, 'utf-8'); $currentHeader .= $line;
if (strpos($headerValue, ';') !== false) { continue;
self::parseContentTypeHeader($currentPart, $headerName, $headerValue);
$boundary = $multipart->getHeader('Content-Type', 'boundary');
$start = $multipart->getHeader('Content-Type', 'start');
} else {
$currentPart->setHeader($headerName, trim($headerValue));
} }
if (strpos($currentHeader, ':') !== false) {
list($headerName, $headerValue) = explode(':', $currentHeader, 2);
$headerValue = iconv_mime_decode($headerValue, 0, 'utf-8');
if (strpos($headerValue, ';') !== false) {
self::parseContentTypeHeader($currentPart, $headerName, $headerValue);
$boundary = $multipart->getHeader('Content-Type', 'boundary');
$start = $multipart->getHeader('Content-Type', 'start');
} else {
$currentPart->setHeader($headerName, trim($headerValue));
}
}
unset($currentHeader);
} }
unset($currentHeader); if ($inHeader) {
} if (trim($line) == '') {
if ($inHeader) { $inHeader = false;
if (trim($line) == '') { continue;
$inHeader = false; }
$currentHeader = $line;
continue; continue;
} } else {
$currentHeader = $line; if (self::isBoundary($line)) {
continue; if (strcmp(trim($line), '--' . $boundary) === 0) {
} else { if ($currentPart instanceof Part) {
// check if we hit any of the boundaries $content = substr($content, 0, -1);
if (strlen($line) > 0 && $line[0] == "-") { self::decodeContent($currentPart, $content);
if (strcmp(trim($line), '--' . $boundary) === 0) { // check if there is a start parameter given, if not set first part
if ($currentPart instanceof Part) { $isMain = (is_null($start) || $start == $currentPart->getHeader('Content-ID')) ? true : false;
$content = substr($content, 0, -2); if ($isMain === true) {
$start = $currentPart->getHeader('Content-ID');
}
$multipart->addPart($currentPart, $isMain);
}
$currentPart = new Part();
$hitFirstBoundary = true;
$inHeader = true;
$content = '';
} elseif (strcmp(trim($line), '--' . $boundary . '--') === 0) {
$content = substr($content, 0, -1);
self::decodeContent($currentPart, $content); self::decodeContent($currentPart, $content);
// check if there is a start parameter given, if not set first part // check if there is a start parameter given, if not set first part
$isMain = (is_null($start) || $start == $currentPart->getHeader('Content-ID')) ? true : false; $isMain = (is_null($start) || $start == $currentPart->getHeader('Content-ID')) ? true : false;
@ -93,34 +107,24 @@ class Parser
$start = $currentPart->getHeader('Content-ID'); $start = $currentPart->getHeader('Content-ID');
} }
$multipart->addPart($currentPart, $isMain); $multipart->addPart($currentPart, $isMain);
$content = '';
} }
$currentPart = new Part(); } else {
$hitFirstBoundary = true; if ($hitFirstBoundary === false) {
$inHeader = true; if (trim($line) !== '') {
$content = ''; $inHeader = true;
} elseif (strcmp(trim($line), '--' . $boundary . '--') === 0) { $currentHeader = $line;
$content = substr($content, 0, -2); continue;
self::decodeContent($currentPart, $content); }
// check if there is a start parameter given, if not set first part
$isMain = (is_null($start) || $start == $currentPart->getHeader('Content-ID')) ? true : false;
if ($isMain === true) {
$start = $currentPart->getHeader('Content-ID');
} }
$multipart->addPart($currentPart, $isMain); $content .= $line . "\n";
$content = '';
} }
} else {
if ($hitFirstBoundary === false) {
if (trim($line) != '') {
$inHeader = true;
$currentHeader = $line;
continue;
}
}
$content .= $line . "\r\n";
} }
} }
} else {
$multipart->addPart(new Part($mimeMessage), true);
} }
return $multipart; return $multipart;
} }
@ -135,7 +139,6 @@ class Parser
* @param string $headerName Header name * @param string $headerName Header name
* @param string $headerValue Header value * @param string $headerValue Header value
* *
* @return null
*/ */
private static function parseContentTypeHeader(PartHeader $part, $headerName, $headerValue) private static function parseContentTypeHeader(PartHeader $part, $headerName, $headerValue)
{ {
@ -168,7 +171,6 @@ class Parser
* @param \BeSimple\SoapCommon\Mime\Part $part Part to add content * @param \BeSimple\SoapCommon\Mime\Part $part Part to add content
* @param string $content Content to decode * @param string $content Content to decode
* *
* @return null
*/ */
private static function decodeContent(Part $part, $content) private static function decodeContent(Part $part, $content)
{ {
@ -184,4 +186,21 @@ class Parser
} }
$part->setContent($content); $part->setContent($content);
} }
private static function hasBoundary(array $lines)
{
foreach ($lines as $line) {
if (self::isBoundary($line)) {
return true;
}
}
return false;
}
private static function isBoundary($line)
{
return strlen($line) > 0 && $line[0] === "-";
}
} }

View File

@ -69,7 +69,6 @@ class Part extends PartHeader
* @param string $encoding Encoding * @param string $encoding Encoding
* @param string $contentId Content id * @param string $contentId Content id
* *
* @return void
*/ */
public function __construct($content = null, $contentType = 'application/octet-stream', $charset = null, $encoding = self::ENCODING_BINARY, $contentId = null) public function __construct($content = null, $contentType = 'application/octet-stream', $charset = null, $encoding = self::ENCODING_BINARY, $contentId = null)
{ {

View File

@ -19,12 +19,7 @@ namespace BeSimple\SoapCommon\Mime;
*/ */
abstract class PartHeader abstract class PartHeader
{ {
/** protected $headers = [];
* Mime headers.
*
* @var array(string=>mixed|array(mixed))
*/
protected $headers = array();
/** /**
* Add a new header to the mime part. * Add a new header to the mime part.
@ -39,10 +34,10 @@ abstract class PartHeader
{ {
if (isset($this->headers[$name]) && !is_null($subValue)) { if (isset($this->headers[$name]) && !is_null($subValue)) {
if (!is_array($this->headers[$name])) { if (!is_array($this->headers[$name])) {
$this->headers[$name] = array( $this->headers[$name] = [
'@' => $this->headers[$name], '@' => $this->headers[$name],
$value => $subValue, $value => $subValue,
); ];
} else { } else {
$this->headers[$name][$value] = $subValue; $this->headers[$name][$value] = $subValue;
} }
@ -76,6 +71,7 @@ abstract class PartHeader
return $this->headers[$name]; return $this->headers[$name];
} }
} }
return null; return null;
} }
@ -86,19 +82,12 @@ abstract class PartHeader
*/ */
protected function generateHeaders() protected function generateHeaders()
{ {
$charset = strtolower($this->getHeader('Content-Type', 'charset'));
$preferences = array(
'scheme' => 'Q',
'input-charset' => 'utf-8',
'output-charset' => $charset,
);
$headers = ''; $headers = '';
foreach ($this->headers as $fieldName => $value) { foreach ($this->headers as $fieldName => $value) {
$fieldValue = $this->generateHeaderFieldValue($value); $fieldValue = $this->generateHeaderFieldValue($value);
// do not use proper encoding as Apache Axis does not understand this $headers .= $fieldName . ': ' . $fieldValue . "\n";
// $headers .= iconv_mime_encode($field_name, $field_value, $preferences) . "\r\n";
$headers .= $fieldName . ': ' . $fieldValue . "\r\n";
} }
return $headers; return $headers;
} }
@ -124,6 +113,7 @@ abstract class PartHeader
} else { } else {
$fieldValue .= $value; $fieldValue .= $value;
} }
return $fieldValue; return $fieldValue;
} }

View File

@ -45,12 +45,12 @@ abstract class SoapMessage
/** /**
* Content types for SOAP versions. * Content types for SOAP versions.
* *
* @var array(string=>string) * @var array (string=>string)
*/ */
static protected $versionToContentTypeMap = array( static protected $versionToContentTypeMap = [
SOAP_1_1 => 'text/xml; charset=utf-8', SOAP_1_1 => 'text/xml; charset=utf-8',
SOAP_1_2 => 'application/soap+xml; charset=utf-8' SOAP_1_2 => 'application/soap+xml; charset=utf-8'
); ];
/** /**
* SOAP action. * SOAP action.
@ -64,7 +64,7 @@ abstract class SoapMessage
* *
* @var array(\BeSimple\SoapCommon\Mime\Part) * @var array(\BeSimple\SoapCommon\Mime\Part)
*/ */
protected $attachments = array(); protected $attachments;
/** /**
* Message content (MIME Message or SOAP Envelope). * Message content (MIME Message or SOAP Envelope).
@ -111,8 +111,8 @@ abstract class SoapMessage
*/ */
public static function getContentTypeForVersion($version) public static function getContentTypeForVersion($version)
{ {
if (!in_array($version, array(SOAP_1_1, SOAP_1_2))) { if (!in_array($version, [SOAP_1_1, SOAP_1_2])) {
throw new \InvalidArgumentException("The 'version' argument has to be either 'SOAP_1_1' or 'SOAP_1_2'!"); throw new \InvalidArgumentException('Invalid SOAP version: ' . $version);
} }
return self::$versionToContentTypeMap[$version]; return self::$versionToContentTypeMap[$version];
@ -138,10 +138,15 @@ abstract class SoapMessage
$this->action = $action; $this->action = $action;
} }
public function hasAttachments()
{
return $this->attachments !== null;
}
/** /**
* Get attachments. * Get attachments.
* *
* @return array(\BeSimple\SoapCommon\Mime\Part) * @return \BeSimple\SoapCommon\Mime\Part[]
*/ */
public function getAttachments() public function getAttachments()
{ {
@ -151,7 +156,7 @@ abstract class SoapMessage
/** /**
* Set SOAP action. * Set SOAP action.
* *
* @param array(\BeSimple\SoapCommon\Mime\Part) $attachments Attachment array * @param \BeSimple\SoapCommon\Mime\Part[] $attachments
*/ */
public function setAttachments(array $attachments) public function setAttachments(array $attachments)
{ {
@ -165,10 +170,11 @@ abstract class SoapMessage
*/ */
public function getContent() public function getContent()
{ {
if (null !== $this->contentDomDocument) { if ($this->contentDomDocument !== null) {
$this->content = $this->contentDomDocument->saveXML(); $this->content = $this->contentDomDocument->saveXML();
$this->contentDomDocument = null; $this->contentDomDocument = null;
} }
return $this->content; return $this->content;
} }

View File

@ -90,6 +90,11 @@ class SoapOptions
return $this->wsdlCacheDir; return $this->wsdlCacheDir;
} }
public function isWsdlCached()
{
return $this->wsdlCacheType !== self::SOAP_CACHE_TYPE_NONE;
}
public function getWsdlCacheType() public function getWsdlCacheType()
{ {
return $this->wsdlCacheType; return $this->wsdlCacheType;

View File

@ -10,11 +10,12 @@ class SoapRequestFactory
* @param string $location Location * @param string $location Location
* @param string $action SOAP action * @param string $action SOAP action
* @param string $version SOAP version * @param string $version SOAP version
* @param string $contentType Content Type
* @param string $content Content * @param string $content Content
* *
* @return SoapRequest * @return SoapRequest
*/ */
public static function create($location, $action, $version, $content = null) public static function create($location, $action, $version, $contentType, $content = null)
{ {
$request = new SoapRequest(); $request = new SoapRequest();
// $content is if unmodified from SoapClient not a php string type! // $content is if unmodified from SoapClient not a php string type!
@ -22,7 +23,6 @@ class SoapRequestFactory
$request->setLocation($location); $request->setLocation($location);
$request->setAction($action); $request->setAction($action);
$request->setVersion($version); $request->setVersion($version);
$contentType = SoapMessage::getContentTypeForVersion($version);
$request->setContentType($contentType); $request->setContentType($contentType);
return $request; return $request;

View File

@ -0,0 +1,26 @@
<?php
namespace BeSimple\SoapCommon\Storage\AbstractStorage;
abstract class AbstractStorage
{
private $items;
protected function getItems()
{
$items = $this->items;
$this->resetItems();
return $items;
}
protected function setItems(array $items)
{
$this->items = $items;
}
private function resetItems()
{
$this->items = [];
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace BeSimple\SoapCommon\Storage;
use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapCommon\Storage\AbstractStorage\AbstractStorage;
class RequestHandlerAttachmentsStorage extends AbstractStorage
{
/**
* @param SoapAttachment[] $attachments
*/
public function __construct(array $attachments)
{
parent::setItems($attachments);
}
/**
* @return SoapAttachment[]
*/
public function getAttachments()
{
return parent::getItems();
}
}

View File

@ -16,6 +16,7 @@ use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\Mime\MultiPart as MimeMultiPart; use BeSimple\SoapCommon\Mime\MultiPart as MimeMultiPart;
use BeSimple\SoapCommon\Mime\Parser as MimeParser; use BeSimple\SoapCommon\Mime\Parser as MimeParser;
use BeSimple\SoapCommon\Mime\Part as MimePart; use BeSimple\SoapCommon\Mime\Part as MimePart;
use BeSimple\SoapCommon\Mime\Part;
use BeSimple\SoapCommon\SoapRequest; use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFilter; use BeSimple\SoapCommon\SoapRequestFilter;
use BeSimple\SoapCommon\SoapResponse; use BeSimple\SoapCommon\SoapResponse;
@ -30,32 +31,17 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
{ {
public function filterRequest(SoapRequest $request, $attachmentType) public function filterRequest(SoapRequest $request, $attachmentType)
{ {
$attachmentsReceived = []; $multiPartMessage = MimeParser::parseMimeMessage(
$request->getContent(),
['Content-Type' => trim($request->getContentType())]
);
$soapPart = $multiPartMessage->getMainPart();
$attachments = $multiPartMessage->getAttachments();
// check content type if it is a multipart mime message $request->setContent($this->sanitizePhpExceptionOnHrefs($soapPart));
$requestContentType = $request->getContentType(); $request->setContentType($soapPart->getHeader('Content-Type'));
if (stripos($requestContentType, 'multipart/related') !== false) { if (count($attachments) > 0) {
// parse mime message $request->setAttachments($attachments);
$headers = [
'Content-Type' => trim($requestContentType),
];
$multipart = MimeParser::parseMimeMessage($request->getContent(), $headers);
// get soap payload and update SoapResponse object
$soapPart = $multipart->getPart();
// convert href -> myhref for external references as PHP throws exception in this case
// http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/soap/php_encoding.c?view=markup#l3436
$content = preg_replace('/href=(?!#)/', 'myhref=', $soapPart->getContent());
$request->setContent($content);
$request->setContentType($soapPart->getHeader('Content-Type'));
// store attachments
$attachments = $multipart->getParts(false);
foreach ($attachments as $cid => $attachment) {
$attachmentsReceived[$cid] = $attachment;
}
}
if (count($attachmentsReceived) > 0) {
$request->setAttachments($attachmentsReceived);
} }
return $request; return $request;
@ -65,28 +51,26 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
{ {
$attachmentsToSend = $response->getAttachments(); $attachmentsToSend = $response->getAttachments();
if (count($attachmentsToSend) > 0) { if (count($attachmentsToSend) > 0) {
$multipart = new MimeMultiPart(); $multipart = new MimeMultiPart('Part_' . rand(10, 15) . '_' . uniqid() . '.' . uniqid());
$soapPart = new MimePart($response->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT); $soapPart = new MimePart($response->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT);
$soapVersion = $response->getVersion(); $soapVersion = $response->getVersion();
// change content type headers for MTOM with SOAP 1.1
if ($soapVersion == SOAP_1_1 && $attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) { if ($soapVersion === SOAP_1_1 && $attachmentType === Helper::ATTACHMENTS_TYPE_MTOM) {
$multipart->setHeader('Content-Type', 'type', 'application/xop+xml'); $multipart->setHeader('Content-Type', 'type', 'application/xop+xml');
$multipart->setHeader('Content-Type', 'start-info', 'text/xml'); $multipart->setHeader('Content-Type', 'start-info', 'text/xml');
$soapPart->setHeader('Content-Type', 'application/xop+xml'); $soapPart->setHeader('Content-Type', 'application/xop+xml');
$soapPart->setHeader('Content-Type', 'type', 'text/xml'); $soapPart->setHeader('Content-Type', 'type', 'text/xml');
} } elseif ($soapVersion === SOAP_1_2) {
// change content type headers for SOAP 1.2
elseif ($soapVersion == SOAP_1_2) {
$multipart->setHeader('Content-Type', 'type', 'application/soap+xml'); $multipart->setHeader('Content-Type', 'type', 'application/soap+xml');
$soapPart->setHeader('Content-Type', 'application/soap+xml'); $soapPart->setHeader('Content-Type', 'application/soap+xml');
} }
$multipart->addPart($soapPart, true); $multipart->addPart($soapPart, true);
foreach ($attachmentsToSend as $cid => $attachment) { foreach ($attachmentsToSend as $cid => $attachment) {
$multipart->addPart($attachment, false); $multipart->addPart($attachment, false);
} }
$response->setContent($multipart->getMimeMessage()); $response->setContent($multipart->getMimeMessage());
// TODO
$headers = $multipart->getHeadersForHttp(); $headers = $multipart->getHeadersForHttp();
list(, $contentType) = explode(': ', $headers[0]); list(, $contentType) = explode(': ', $headers[0]);
@ -95,4 +79,11 @@ class MimeFilter implements SoapRequestFilter, SoapResponseFilter
return $response; return $response;
} }
private function sanitizePhpExceptionOnHrefs(Part $soapPart)
{
// convert href -> myhref for external references as PHP throws exception in this case
// http://svn.php.net/viewvc/php/php-src/branches/PHP_5_4/ext/soap/php_encoding.c?view=markup#l3436
return preg_replace('/href=(?!#)/', 'myhref=', $soapPart->getContent());
}
} }

View File

@ -2,6 +2,8 @@
namespace BeSimple\SoapServer\SoapOptions; namespace BeSimple\SoapServer\SoapOptions;
use Exception;
class SoapServerOptions class SoapServerOptions
{ {
const SOAP_SERVER_PERSISTENCE_NONE = 0; const SOAP_SERVER_PERSISTENCE_NONE = 0;
@ -43,6 +45,22 @@ class SoapServerOptions
$this->persistence = $persistence; $this->persistence = $persistence;
} }
public function getHandler()
{
if ($this->hasHandlerObject()) {
return $this->getHandlerObject();
} else if ($this->hasHandlerClass()) {
return $this->getHandlerClass();
} else {
throw new Exception('No HandlerClass or HandlerObject set');
}
}
public function hasHandlerClass() public function hasHandlerClass()
{ {
return $this->handlerClass !== null; return $this->handlerClass !== null;

View File

@ -8,9 +8,6 @@ class SoapResponse extends CommonSoapResponse
{ {
public function getResponseContent() public function getResponseContent()
{ {
// set Content-Type header
header('Content-Type: ' . $this->getContentType());
return $this->getContent(); return $this->getContent();
} }
} }

View File

@ -12,6 +12,8 @@
namespace BeSimple\SoapServer; namespace BeSimple\SoapServer;
use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapCommon\Mime\Part;
use BeSimple\SoapCommon\SoapMessage; use BeSimple\SoapCommon\SoapMessage;
class SoapResponseFactory class SoapResponseFactory
@ -23,7 +25,7 @@ class SoapResponseFactory
* @param string $location Location * @param string $location Location
* @param string $action SOAP action * @param string $action SOAP action
* @param string $version SOAP version * @param string $version SOAP version
* @param array $attachments SOAP attachments * @param SoapAttachment[] $attachments SOAP attachments
* *
* @return SoapResponse * @return SoapResponse
*/ */
@ -37,6 +39,33 @@ class SoapResponseFactory
$contentType = SoapMessage::getContentTypeForVersion($version); $contentType = SoapMessage::getContentTypeForVersion($version);
$response->setContentType($contentType); $response->setContentType($contentType);
if (count($attachments) > 0) {
$response->setAttachments(
self::createAttachmentParts($attachments)
);
}
return $response; return $response;
} }
/**
* @param SoapAttachment[] $attachments SOAP attachments
* @return Part[]
*/
private function createAttachmentParts(array $attachments = [])
{
$parts = [];
foreach ($attachments as $attachment) {
$part = new Part(
$attachment->getContent(),
'application/pdf',
'utf-8',
Part::ENCODING_BINARY,
$attachment->getId()
);
$parts[] = $part;
}
return $parts;
}
} }

View File

@ -12,10 +12,13 @@
namespace BeSimple\SoapServer; namespace BeSimple\SoapServer;
use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapCommon\AttachmentsHandler;
use BeSimple\SoapCommon\SoapKernel; use BeSimple\SoapCommon\SoapKernel;
use BeSimple\SoapCommon\SoapOptions\SoapOptions; use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapRequest; use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFactory; use BeSimple\SoapCommon\SoapRequestFactory;
use BeSimple\SoapCommon\Storage\RequestHandlerAttachmentsStorage;
use BeSimple\SoapServer\SoapOptions\SoapServerOptions; use BeSimple\SoapServer\SoapOptions\SoapServerOptions;
use BeSimple\SoapCommon\Converter\MtomTypeConverter; use BeSimple\SoapCommon\Converter\MtomTypeConverter;
use BeSimple\SoapCommon\Converter\SwaTypeConverter; use BeSimple\SoapCommon\Converter\SwaTypeConverter;
@ -44,9 +47,6 @@ class SoapServer extends \SoapServer
*/ */
public function __construct(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions) public function __construct(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions)
{ {
if ($soapOptions->hasAttachments()) {
$soapOptions = $this->configureTypeConverters($soapOptions);
}
$this->soapVersion = $soapOptions->getSoapVersion(); $this->soapVersion = $soapOptions->getSoapVersion();
$this->soapServerOptions = $soapServerOptions; $this->soapServerOptions = $soapServerOptions;
$this->soapOptions = $soapOptions; $this->soapOptions = $soapOptions;
@ -60,6 +60,7 @@ class SoapServer extends \SoapServer
/** /**
* Custom handle method to be able to modify the SOAP messages. * Custom handle method to be able to modify the SOAP messages.
* *
* @deprecated Please, use createRequest + handleRequest methods
* @param string $requestUrl * @param string $requestUrl
* @param string $soapAction * @param string $soapAction
* @param string $requestContent = null * @param string $requestContent = null
@ -67,15 +68,9 @@ class SoapServer extends \SoapServer
*/ */
public function handle($requestUrl, $soapAction, $requestContent = null) public function handle($requestUrl, $soapAction, $requestContent = null)
{ {
try { return $this->handleRequest(
$this->createRequest($requestUrl, $soapAction, $requestContent)
return $this->getSoapResponse($requestUrl, $soapAction, $requestContent)->getResponseContent(); );
} catch (\SoapFault $fault) {
$this->fault($fault->faultcode, $fault->faultstring);
return self::SOAP_SERVER_REQUEST_FAILED;
}
} }
/** /**
@ -83,20 +78,42 @@ class SoapServer extends \SoapServer
* *
* @param string $requestUrl * @param string $requestUrl
* @param string $soapAction * @param string $soapAction
* @param string $requestContentType
* @param string $requestContent = null * @param string $requestContent = null
* @return SoapResponse * @return SoapRequest
*/ */
public function getSoapResponse($requestUrl, $soapAction, $requestContent = null) public function createRequest($requestUrl, $soapAction, $requestContentType, $requestContent = null)
{ {
$soapRequest = SoapRequestFactory::create( $soapRequest = SoapRequestFactory::create(
$requestUrl, $requestUrl,
$soapAction, $soapAction,
$this->soapVersion, $this->soapVersion,
$requestContentType,
$requestContent $requestContent
); );
$soapResponse = $this->handleSoapRequest($soapRequest); $soapKernel = new SoapKernel();
if ($this->soapOptions->hasAttachments()) {
$soapRequest = $soapKernel->filterRequest(
$soapRequest,
$this->getAttachmentFilters(),
$this->soapOptions->getAttachmentType()
);
}
return $soapResponse; return $soapRequest;
}
public function handleRequest(SoapRequest $soapRequest)
{
try {
return $this->handleSoapRequest($soapRequest);
} catch (\SoapFault $fault) {
$this->fault($fault->faultcode, $fault->faultstring);
return self::SOAP_SERVER_REQUEST_FAILED;
}
} }
/** /**
@ -110,33 +127,87 @@ class SoapServer extends \SoapServer
*/ */
private function handleSoapRequest(SoapRequest $soapRequest) private function handleSoapRequest(SoapRequest $soapRequest)
{ {
$soapKernel = new SoapKernel(); /** @var AttachmentsHandler $handler */
$handler = $this->soapServerOptions->getHandler();
if ($this->soapOptions->hasAttachments()) { if ($this->soapOptions->hasAttachments()) {
$soapRequest = $soapKernel->filterRequest($soapRequest, $this->getFilters(), $this->soapOptions->getAttachmentType()); $this->injectAttachmentStorage($handler, $soapRequest, $this->soapOptions->getAttachmentType());
} }
ob_start(); ob_start();
parent::handle($soapRequest->getContent()); parent::handle($soapRequest->getContent());
$response = ob_get_clean(); $nativeSoapServerResponse = ob_get_clean();
$attachments = [];
if ($this->soapOptions->hasAttachments()) {
$attachments = $handler->getAttachmentsFromStorage();
}
// Remove headers added by SoapServer::handle() method // Remove headers added by SoapServer::handle() method
header_remove('Content-Length'); header_remove('Content-Length');
header_remove('Content-Type'); header_remove('Content-Type');
$soapResponse = SoapResponseFactory::create( return $this->createResponse(
$response,
$soapRequest->getLocation(), $soapRequest->getLocation(),
$soapRequest->getAction(), $soapRequest->getAction(),
$soapRequest->getVersion() $soapRequest->getVersion(),
$nativeSoapServerResponse,
$attachments
); );
}
/**
* @param string $requestLocation
* @param string $soapAction
* @param string $soapVersion
* @param string|null $responseContent
* @param SoapAttachment[] $attachments
* @return SoapResponse
*/
private function createResponse($requestLocation, $soapAction, $soapVersion, $responseContent = null, $attachments = [])
{
$soapResponse = SoapResponseFactory::create(
$responseContent,
$requestLocation,
$soapAction,
$soapVersion,
$attachments
);
$soapKernel = new SoapKernel();
if ($this->soapOptions->hasAttachments()) { if ($this->soapOptions->hasAttachments()) {
$soapResponse = $soapKernel->filterResponse($soapResponse, $this->getFilters(), $this->soapOptions->getAttachmentType()); $soapResponse = $soapKernel->filterResponse(
$soapResponse,
$this->getAttachmentFilters(),
$this->soapOptions->getAttachmentType()
);
} }
return $soapResponse; return $soapResponse;
} }
private function injectAttachmentStorage(AttachmentsHandler $handler, SoapRequest $soapRequest, $attachmentType)
{
$attachments = [];
if ($soapRequest->hasAttachments()) {
foreach ($soapRequest->getAttachments() as $attachment) {
$attachments[] = new SoapAttachment(
$attachment->getHeader('Content-Disposition', 'filename'),
$attachmentType,
$attachment->getContent()
);
}
}
$handler->addAttachmentStorage(new RequestHandlerAttachmentsStorage($attachments));
}
/**
* Legacy code: TypeConverters should be resolved in SoapServer::__construct()
* To be removed if all tests pass
*
* @deprecated
* @param SoapOptions $soapOptions
* @return SoapOptions
*/
private function configureTypeConverters(SoapOptions $soapOptions) private function configureTypeConverters(SoapOptions $soapOptions)
{ {
if ($soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) { if ($soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) {
@ -152,7 +223,7 @@ class SoapServer extends \SoapServer
return $soapOptions; return $soapOptions;
} }
private function getFilters() private function getAttachmentFilters()
{ {
$filters = []; $filters = [];
if ($this->soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) { if ($this->soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) {

View File

@ -31,7 +31,7 @@ class XmlMimeFilter implements SoapResponseFilter
{ {
} }
public function filterResponse(SoapResponse $response) public function filterResponse(SoapResponse $response, $attachmentType)
{ {
$dom = $response->getContentDocument(); $dom = $response->getContentDocument();