Revert whole code to legacy

This commit is contained in:
Ghislain Loaec
2018-04-06 10:58:45 +02:00
parent 0ca2fe1fd5
commit 1af0828b15
255 changed files with 6898 additions and 10335 deletions

View File

@ -0,0 +1,26 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
* (c) Andreas Schamberger <mail@andreass.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapServer\Exception;
/**
* ReceiverSoapFault send a "Receiver" fault code to client.
* This fault code is standardized: http://www.w3.org/TR/soap12-part1/#tabsoapfaultcodes
*/
class ReceiverSoapFault extends \SoapFault
{
public function __construct($faultstring, $faultactor = null, $detail = null, $faultname = null, $headerfault = null)
{
parent::__construct('Receiver', $faultstring, $faultactor, $detail, $faultname, $headerfault);
}
}

View File

@ -0,0 +1,26 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
* (c) Andreas Schamberger <mail@andreass.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapServer\Exception;
/**
* SenderSoapFault send a "Sender" fault code to client.
* This fault code is standardized: http://www.w3.org/TR/soap12-part1/#tabsoapfaultcodes
*/
class SenderSoapFault extends \SoapFault
{
public function __construct($faultstring, $faultactor = null, $detail = null, $faultname = null, $headerfault = null)
{
parent::__construct('Sender', $faultstring, $faultactor, $detail, $faultname, $headerfault);
}
}

View File

@ -16,10 +16,9 @@ use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\Mime\MultiPart as MimeMultiPart;
use BeSimple\SoapCommon\Mime\Parser as MimeParser;
use BeSimple\SoapCommon\Mime\Part as MimePart;
use BeSimple\SoapCommon\Mime\Part;
use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFilter;
use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse;
use BeSimple\SoapCommon\SoapResponse;
use BeSimple\SoapCommon\SoapResponseFilter;
/**
@ -29,61 +28,111 @@ use BeSimple\SoapCommon\SoapResponseFilter;
*/
class MimeFilter implements SoapRequestFilter, SoapResponseFilter
{
public function filterRequest(SoapRequest $request, $attachmentType)
/**
* Attachment type.
*
* @var int Helper::ATTACHMENTS_TYPE_SWA | Helper::ATTACHMENTS_TYPE_MTOM
*/
protected $attachmentType = Helper::ATTACHMENTS_TYPE_SWA;
/**
* Constructor.
*
* @param int $attachmentType Helper::ATTACHMENTS_TYPE_SWA | Helper::ATTACHMENTS_TYPE_MTOM
*/
public function __construct($attachmentType)
{
$multiPartMessage = MimeParser::parseMimeMessage(
$request->getContent(),
['Content-Type' => trim($request->getContentType())]
);
$soapPart = $multiPartMessage->getMainPart();
$attachments = $multiPartMessage->getAttachments();
$request->setContent($soapPart->getContent());
$request->setContentType($soapPart->getHeader('Content-Type'));
if (count($attachments) > 0) {
$request->setAttachments($attachments);
}
return $request;
$this->attachmentType = $attachmentType;
}
public function filterResponse(CommonSoapResponse $response, $attachmentType)
/**
* Reset all properties to default values.
*/
public function resetFilter()
{
$this->attachmentType = Helper::ATTACHMENTS_TYPE_SWA;
}
/**
* Modify the given request XML.
*
* @param \BeSimple\SoapCommon\SoapRequest $request SOAP request
*
* @return void
*/
public function filterRequest(SoapRequest $request)
{
// array to store attachments
$attachmentsRecieved = array();
// check content type if it is a multipart mime message
$requestContentType = $request->getContentType();
if (false !== stripos($requestContentType, 'multipart/related')) {
// parse mime message
$headers = array(
'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) {
$attachmentsRecieved[$cid] = $attachment;
}
}
// add attachments to response object
if (count($attachmentsRecieved) > 0) {
$request->setAttachments($attachmentsRecieved);
}
}
/**
* Modify the given response XML.
*
* @param \BeSimple\SoapCommon\SoapResponse $response SOAP response
*
* @return void
*/
public function filterResponse(SoapResponse $response)
{
// get attachments from request object
$attachmentsToSend = $response->getAttachments();
// build mime message if we have attachments
if (count($attachmentsToSend) > 0) {
$multipart = new MimeMultiPart('Part_' . rand(10, 15) . '_' . uniqid() . '.' . uniqid());
$multipart = new MimeMultiPart();
$soapPart = new MimePart($response->getContent(), 'text/xml', 'utf-8', MimePart::ENCODING_EIGHT_BIT);
$soapVersion = $response->getVersion();
if ($soapVersion === SOAP_1_1 && $attachmentType === Helper::ATTACHMENTS_TYPE_MTOM) {
// change content type headers for MTOM with SOAP 1.1
if ($soapVersion == SOAP_1_1 && $this->attachmentType & Helper::ATTACHMENTS_TYPE_MTOM) {
$multipart->setHeader('Content-Type', 'type', 'application/xop+xml');
$multipart->setHeader('Content-Type', 'start-info', 'text/xml');
$soapPart->setHeader('Content-Type', 'application/xop+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');
$soapPart->setHeader('Content-Type', 'application/soap+xml');
}
$multipart->addPart($soapPart, true);
foreach ($attachmentsToSend as $cid => $attachment) {
$multipart->addPart($attachment, false);
}
$response->setContent($multipart->getMimeMessage());
// TODO
$headers = $multipart->getHeadersForHttp();
list(, $contentType) = explode(': ', $headers[0]);
$response->setContentType($contentType);
}
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

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the BeSimpleSoapCommon.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
* (c) Andreas Schamberger <mail@andreass.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\SoapKernel as CommonSoapKernel;
use BeSimple\SoapCommon\SoapRequest as CommonSoapRequest;
use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse;
/**
* SoapKernel for Server.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapKernel extends CommonSoapKernel
{
/**
* {@inheritDoc}
*/
public function filterRequest(CommonSoapRequest $request)
{
parent::filterRequest($request);
$this->attachments = $request->getAttachments();
}
/**
* {@inheritDoc}
*/
public function filterResponse(CommonSoapResponse $response)
{
$response->setAttachments($this->attachments);
$this->attachments = array();
parent::filterResponse($response);
}
}

View File

@ -1,168 +0,0 @@
<?php
namespace BeSimple\SoapServer\SoapOptions;
use Exception;
class SoapServerOptions
{
const SOAP_SERVER_PERSISTENCE_NONE = 0;
const SOAP_SERVER_PERSISTENCE_REQUEST = \SOAP_PERSISTENCE_REQUEST;
const SOAP_SERVER_PERSISTENCE_SESSION = \SOAP_PERSISTENCE_SESSION;
const SOAP_SERVER_KEEP_ALIVE_ON = true;
const SOAP_SERVER_KEEP_ALIVE_OFF = false;
const SOAP_SERVER_ERROR_REPORTING_ON = true;
const SOAP_SERVER_ERROR_REPORTING_OFF = false;
const SOAP_SERVER_EXCEPTIONS_ON = true;
const SOAP_SERVER_EXCEPTIONS_OFF = false;
private $handlerClass;
private $handlerObject;
private $keepAlive;
private $errorReporting;
private $exceptions;
private $persistence;
/**
* @param mixed $handlerClassOrObject
* @param bool $keepAlive = SoapServerOptions::SOAP_SERVER_KEEP_ALIVE_ON|SoapServerOptions::SOAP_SERVER_KEEP_ALIVE_OFF
* @param bool $errorReporting = SoapServerOptions::SOAP_SERVER_ERROR_REPORTING_ON|SoapServerOptions::SOAP_SERVER_ERROR_REPORTING_OFF
* @param bool $exceptions = SoapServerOptions::SOAP_SERVER_EXCEPTIONS_ON|SoapServerOptions::SOAP_SERVER_EXCEPTIONS_OFF
* @param int $persistence = SoapServerOptions::SOAP_SERVER_PERSISTENCE_NONE|SoapServerOptions::SOAP_SERVER_PERSISTENCE_REQUEST|SoapServerOptions::SOAP_SERVER_PERSISTENCE_SESSION
*/
public function __construct(
$handlerClassOrObject,
$keepAlive,
$errorReporting,
$exceptions,
$persistence
) {
$this->handlerClass = $this->resolveHandlerClass($handlerClassOrObject);
$this->handlerObject = $this->resolveHandlerObject($handlerClassOrObject);
$this->keepAlive = $keepAlive;
$this->errorReporting = $errorReporting;
$this->exceptions = $exceptions;
$this->persistence = $persistence;
}
public function getHandler()
{
if ($this->hasHandlerObject() && $this->hasHandlerClass()) {
throw new Exception('Both HandlerClass and HandlerObject set: please specify only one');
}
if ($this->hasHandlerObject()) {
return $this->getHandlerObject();
}
if ($this->hasHandlerClass()) {
return $this->getHandlerClass();
}
throw new Exception('No HandlerClass or HandlerObject set');
}
public function getHandlerInstance()
{
if ($this->hasHandlerClass()) {
$handlerClassName = $this->handlerClass;
return new $handlerClassName;
}
return $this->getHandler();
}
public function hasHandlerClass()
{
return $this->handlerClass !== null;
}
public function getHandlerClass()
{
return $this->handlerClass;
}
public function hasHandlerObject()
{
return $this->handlerObject !== null;
}
public function getHandlerObject()
{
return $this->handlerObject;
}
public function hasPersistence()
{
return $this->persistence !== self::SOAP_SERVER_PERSISTENCE_NONE;
}
public function getPersistence()
{
return $this->persistence;
}
public function isErrorReporting()
{
return $this->errorReporting;
}
public function isExceptions()
{
return $this->exceptions;
}
public function isKeepAlive()
{
return $this->keepAlive;
}
public function toArray()
{
$optionsAsArray = [
'keep_alive' => $this->isKeepAlive(),
'exceptions' => $this->isExceptions(),
];
return $optionsAsArray;
}
/**
* @param mixed $handler
* @return mixed|null
*/
private function resolveHandlerObject($handler)
{
if (is_string($handler) && class_exists($handler)) {
return null;
}
if (is_object($handler)) {
return $handler;
}
throw new \InvalidArgumentException('The handler has to be a class name or an object');
}
/**
* @param mixed $handler
* @return mixed|null
*/
private function resolveHandlerClass($handler)
{
if (is_string($handler) && class_exists($handler)) {
return $handler;
}
if (is_object($handler)) {
return null;
}
throw new \InvalidArgumentException('The handler has to be a class name or an object');
}
}

View File

@ -0,0 +1,72 @@
<?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\SoapServer;
use BeSimple\SoapCommon\SoapRequest as CommonSoapRequest;
use BeSimple\SoapCommon\SoapMessage;
/**
* SoapRequest class for SoapClient. Provides factory function for request object.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapRequest extends CommonSoapRequest
{
/**
* Factory function for SoapRequest.
*
* @param string $content Content
* @param string $version SOAP version
*
* @return BeSimple\SoapClient\SoapRequest
*/
public static function create($content, $version)
{
$request = new SoapRequest();
// $content is if unmodified from SoapClient not a php string type!
$request->setContent((string) (null === $content ? file_get_contents("php://input") : $content));
$request->setLocation(self::getCurrentUrl());
$request->setAction(isset($_SERVER[SoapMessage::SOAP_ACTION_HEADER]) ? $_SERVER[SoapMessage::SOAP_ACTION_HEADER] : null);
$request->setVersion($version);
if (isset($_SERVER[SoapMessage::CONTENT_TYPE_HEADER])) {
$request->setContentType($_SERVER[SoapMessage::CONTENT_TYPE_HEADER]);
} elseif (isset($_SERVER[SoapMessage::HTTP_CONTENT_TYPE_HEADER])) {
$request->setContentType($_SERVER[SoapMessage::HTTP_CONTENT_TYPE_HEADER]);
}
return $request;
}
/**
* Builds the current URL from the $_SERVER array.
*
* @return string
*/
public static function getCurrentUrl()
{
$url = '';
if (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) === 'on' || $_SERVER['HTTPS'] === '1')) {
$url .= 'https://';
} else {
$url .= 'http://';
}
$url .= isset( $_SERVER['SERVER_NAME'] ) ? $_SERVER['SERVER_NAME'] : '';
if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) {
$url .= ":{$_SERVER['SERVER_PORT']}";
}
$url .= isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
return $url;
}
}

View File

@ -1,13 +1,59 @@
<?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\SoapServer;
use BeSimple\SoapCommon\SoapResponse as CommonSoapResponse;
use BeSimple\SoapCommon\SoapMessage;
/**
* SoapResponse class for SoapClient. Provides factory function for response object.
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class SoapResponse extends CommonSoapResponse
{
public function getResponseContent()
/**
* Factory function for SoapResponse.
*
* @param string $content Content
* @param string $location Location
* @param string $action SOAP action
* @param string $version SOAP version
*
* @return BeSimple\SoapClient\SoapResponse
*/
public static function create($content, $location, $action, $version)
{
return $this->getContent();
$response = new SoapResponse();
$response->setContent($content);
$response->setLocation($location);
$response->setAction($action);
$response->setVersion($version);
$contentType = SoapMessage::getContentTypeForVersion($version);
$response->setContentType($contentType);
return $response;
}
/**
* Send SOAP response to client.
*/
public function send()
{
// set Content-Type header
header('Content-Type: '.$this->getContentType());
// send content to client
echo $this->getContent();
}
}

View File

@ -1,92 +0,0 @@
<?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\SoapServer;
use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapClient\SoapResponseTracingData;
use BeSimple\SoapCommon\Mime\PartFactory;
use BeSimple\SoapCommon\SoapMessage;
class SoapResponseFactory
{
/**
* Factory function for SoapServer\SoapResponse.
*
* @param string $content Content
* @param string $location Location
* @param string $action SOAP action
* @param string $version SOAP version
* @param SoapAttachment[] $attachments SOAP attachments
*
* @return SoapResponse
*/
public static function create(
$content,
$location,
$action,
$version,
array $attachments = []
) {
$response = new SoapResponse();
$response->setContent($content);
$response->setLocation($location);
$response->setAction($action);
$response->setVersion($version);
$contentType = SoapMessage::getContentTypeForVersion($version);
$response->setContentType($contentType);
if (count($attachments) > 0) {
$response->setAttachments(
PartFactory::createAttachmentParts($attachments)
);
}
return $response;
}
/**
* Factory function for SoapServer\SoapResponse.
*
* @param string $content Content
* @param string $location Location
* @param string $action SOAP action
* @param string $version SOAP version
* @param SoapResponseTracingData $tracingData Data value object suitable for tracing SOAP traffic
* @param SoapAttachment[] $attachments SOAP attachments
*
* @return SoapResponse
*/
public static function createWithTracingData(
$content,
$location,
$action,
$version,
SoapResponseTracingData $tracingData,
array $attachments = []
) {
$response = new SoapResponse();
$response->setContent($content);
$response->setLocation($location);
$response->setAction($action);
$response->setVersion($version);
$response->setTracingData($tracingData);
$contentType = SoapMessage::getContentTypeForVersion($version);
$response->setContentType($contentType);
if (count($attachments) > 0) {
$response->setAttachments(
PartFactory::createAttachmentParts($attachments)
);
}
return $response;
}
}

View File

@ -12,123 +12,73 @@
namespace BeSimple\SoapServer;
use BeSimple\SoapBundle\Soap\SoapAttachment;
use BeSimple\SoapCommon\AttachmentsHandlerInterface;
use BeSimple\SoapCommon\SoapKernel;
use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapCommon\SoapRequest;
use BeSimple\SoapCommon\SoapRequestFactory;
use BeSimple\SoapCommon\Storage\RequestHandlerAttachmentsStorage;
use BeSimple\SoapServer\SoapOptions\SoapServerOptions;
use BeSimple\SoapCommon\Helper;
use BeSimple\SoapCommon\Converter\MtomTypeConverter;
use BeSimple\SoapCommon\Converter\SwaTypeConverter;
use Exception;
use InvalidArgumentException;
/**
* Extended SoapServer that allows adding filters for SwA, MTOM, ... .
*
* @author Andreas Schamberger <mail@andreass.net>
* @author Christian Kerl <christian-kerl@web.de>
* @author Petr Bechyně <mail@petrbechyne.com>
*/
class SoapServer extends \SoapServer
{
const SOAP_SERVER_REQUEST_FAILED = false;
/**
* Soap version.
*
* @var int
*/
protected $soapVersion = SOAP_1_1;
protected $soapVersion;
protected $soapServerOptions;
protected $soapOptions;
/**
* Soap kernel.
*
* @var \BeSimple\SoapServer\SoapKernel
*/
protected $soapKernel = null;
/**
* Constructor.
*
* @param SoapServerOptions $soapServerOptions
* @param SoapOptions $soapOptions
* @param string $wsdl WSDL file
* @param array(string=>mixed) $options Options array
*/
public function __construct(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions)
public function __construct($wsdl, array $options = array())
{
$this->validateSoapConfigs($soapServerOptions, $soapOptions);
$this->soapVersion = $soapOptions->getSoapVersion();
$this->soapServerOptions = $soapServerOptions;
$this->soapOptions = $soapOptions;
parent::__construct(
$soapOptions->getWsdlFile(),
$soapServerOptions->toArray() + $soapOptions->toArray()
);
// store SOAP version
if (isset($options['soap_version'])) {
$this->soapVersion = $options['soap_version'];
}
// create soap kernel instance
$this->soapKernel = new SoapKernel();
// set up type converter and mime filter
$this->configureMime($options);
// we want the exceptions option to be set
$options['exceptions'] = true;
parent::__construct($wsdl, $options);
}
/**
* Custom handle method to be able to modify the SOAP messages.
*
* @deprecated
* @param string $request Request string
* @return string
*/
public function handle($request = null)
{
throw new Exception(
'The handle method is now deprecated, because it accesses $_SERVER, $_POST. Use handleRequest instead'
);
}
// wrap request data in SoapRequest object
$soapRequest = SoapRequest::create($request, $this->soapVersion);
/**
* Custom handle method to be able to modify the SOAP messages.
*
* @param string $requestUrl
* @param string $soapAction
* @param string $requestContentType
* @param string $requestContent = null
* @return SoapRequest
*/
public function createRequest($requestUrl, $soapAction, $requestContentType, $requestContent = null)
{
$soapRequest = SoapRequestFactory::createWithContentType(
$requestUrl,
$soapAction,
$this->soapVersion,
$requestContentType,
$requestContent
);
if ($this->soapOptions->hasAttachments()) {
$soapRequest = SoapKernel::filterRequest(
$soapRequest,
$this->getAttachmentFilters(),
$this->soapOptions->getAttachmentType()
);
}
return $soapRequest;
}
public function handleRequest(SoapRequest $soapRequest)
{
// handle actual SOAP request
try {
return $this->handleSoapRequest($soapRequest);
$soapResponse = $this->handle2($soapRequest);
} catch (\SoapFault $fault) {
$this->fault($fault->faultcode, $fault->faultstring, $fault->faultactor, $fault->detail);
return self::SOAP_SERVER_REQUEST_FAILED;
// issue an error to the client
$this->fault($fault->faultcode, $fault->faultstring);
}
}
public function handleWsdlRequest(SoapRequest $soapRequest)
{
ob_start();
parent::handle();
$nativeSoapServerResponse = ob_get_clean();
return SoapResponseFactory::create(
$nativeSoapServerResponse,
$soapRequest->getLocation(),
$soapRequest->getAction(),
$soapRequest->getVersion()
);
// send SOAP response to client
$soapResponse->send();
}
/**
@ -140,133 +90,81 @@ class SoapServer extends \SoapServer
*
* @return SoapResponse
*/
private function handleSoapRequest(SoapRequest $soapRequest)
public function handle2(SoapRequest $soapRequest)
{
/** @var AttachmentsHandlerInterface $handler */
$handler = $this->soapServerOptions->getHandler();
if ($this->soapOptions->hasAttachments()) {
$this->injectAttachmentStorage($handler, $soapRequest);
}
// run SoapKernel on SoapRequest
$this->soapKernel->filterRequest($soapRequest);
// call parent \SoapServer->handle() and buffer output
ob_start();
parent::handle($soapRequest->getContent());
$nativeSoapServerResponse = ob_get_clean();
$attachments = [];
if ($this->soapOptions->hasAttachments()) {
$attachments = $handler->getAttachmentStorage()->getAttachments();
}
$response = ob_get_clean();
// Remove headers added by SoapServer::handle() method
header_remove('Content-Length');
header_remove('Content-Type');
return $this->createResponse(
// wrap response data in SoapResponse object
$soapResponse = SoapResponse::create(
$response,
$soapRequest->getLocation(),
$soapRequest->getAction(),
$soapRequest->getVersion(),
$nativeSoapServerResponse,
$attachments
$soapRequest->getVersion()
);
}
/**
* @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, array $attachments = [])
{
$soapResponse = SoapResponseFactory::create(
$responseContent,
$requestLocation,
$soapAction,
$soapVersion,
$attachments
);
if ($this->soapOptions->hasAttachments()) {
$soapResponse = SoapKernel::filterResponse(
$soapResponse,
$this->getAttachmentFilters(),
$this->soapOptions->getAttachmentType()
);
}
// run SoapKernel on SoapResponse
$this->soapKernel->filterResponse($soapResponse);
return $soapResponse;
}
private function injectAttachmentStorage(AttachmentsHandlerInterface $handler, SoapRequest $soapRequest)
/**
* Get SoapKernel instance.
*
* @return \BeSimple\SoapServer\SoapKernel
*/
public function getSoapKernel()
{
$attachments = [];
if ($soapRequest->hasAttachments()) {
foreach ($soapRequest->getAttachments() as $attachment) {
$fileName = $attachment->getHeader('Content-Disposition', 'filename');
if ($fileName === null) {
$fileName = basename($attachment->getHeader('Content-Location'));
}
$attachments[] = new SoapAttachment(
$fileName,
$attachment->getHeader('Content-Type'),
$attachment->getContent()
);
}
}
$handler->addAttachmentStorage(new RequestHandlerAttachmentsStorage($attachments));
return $this->soapKernel;
}
/**
* Legacy code: TypeConverters should be resolved in SoapServer::__construct()
* To be removed if all tests pass
* Configure filter and type converter for SwA/MTOM.
*
* @deprecated
* @param SoapOptions $soapOptions
* @return SoapOptions
* @param array &$options SOAP constructor options array.
*
* @return void
*/
private function configureTypeConverters(SoapOptions $soapOptions)
private function configureMime(array &$options)
{
if ($soapOptions->getAttachmentType() !== SoapOptions::SOAP_ATTACHMENTS_TYPE_BASE64) {
if ($soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_SWA) {
$soapOptions->getTypeConverterCollection()->add(new SwaTypeConverter());
} elseif ($soapOptions->getAttachmentType() === SoapOptions::SOAP_ATTACHMENTS_TYPE_MTOM) {
$soapOptions->getTypeConverterCollection()->add(new MtomTypeConverter());
} else {
throw new InvalidArgumentException('Unresolved SOAP_ATTACHMENTS_TYPE: ' . $soapOptions->getAttachmentType());
if (isset($options['attachment_type']) && 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);
}
}
return $soapOptions;
}
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;
}
private function validateSoapConfigs(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions)
{
if ($soapOptions->hasAttachments()) {
if (!$soapServerOptions->getHandlerInstance() instanceof AttachmentsHandlerInterface) {
throw new InvalidArgumentException(
sprintf(
'%s::handlerObject or handlerClass (instance of %s given) must implement %s in order to handle with attachments',
SoapServerOptions::class,
get_class($soapServerOptions->getHandlerInstance()),
AttachmentsHandlerInterface::class
)
);
// 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);
},
);
}
}
}

View File

@ -12,46 +12,189 @@
namespace BeSimple\SoapServer;
use BeSimple\SoapCommon\SoapOptions\SoapOptions;
use BeSimple\SoapServer\SoapOptions\SoapServerOptions;
use Exception;
use BeSimple\SoapCommon\AbstractSoapBuilder;
use BeSimple\SoapCommon\Helper;
/**
* SoapServerBuilder provides a SoapServer instance from SoapServerOptions and SoapOptions.
* SoapServerBuilder provides a fluent interface to configure and create a SoapServer instance.
*
* @author Petr Bechyně <mail@petrbechyne.com>
* @author Christian Kerl <christian-kerl@web.de>
*/
class SoapServerBuilder
class SoapServerBuilder extends AbstractSoapBuilder
{
protected $persistence;
protected $errorReporting;
protected $handlerClass;
protected $handlerObject;
/**
* Builds a SoapServer instance.
* Create new instance with default options.
*
* @param SoapServerOptions $soapServerOptions
* @param SoapOptions $soapOptions
*
* @return SoapServer
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function build(SoapServerOptions $soapServerOptions, SoapOptions $soapOptions)
static public function createWithDefaults()
{
use_soap_error_handler($soapServerOptions->isErrorReporting());
return parent::createWithDefaults()
->withErrorReporting(false);
}
$server = new SoapServer($soapServerOptions, $soapOptions);
if ($soapServerOptions->hasPersistence()) {
$server->setPersistence($soapServerOptions->getPersistence());
}
if ($soapServerOptions->hasHandlerClass()) {
$server->setClass($soapServerOptions->getHandlerClass());
}
if ($soapServerOptions->hasHandlerObject()) {
$server->setObject($soapServerOptions->getHandlerObject());
}
if ($soapServerOptions->hasHandlerClass() && $soapServerOptions->hasHandlerObject()) {
/**
* Initializes all options with the defaults used in the native SoapServer.
*/
public function __construct()
{
parent::__construct();
throw new Exception(
'Could not create SoapServer: HandlerClass and HandlerObject are set: please specify only one'
);
// TODO: this is not the default, but safer
$this->withErrorReporting(false);
}
/**
* Finally returns a SoapClient instance.
*
* @return \BeSimple\SoapServer\SoapServer
*/
public function build()
{
$this->validateOptions();
use_soap_error_handler($this->errorReporting);
$server = new SoapServer($this->wsdl, $this->getSoapOptions());
if (null !== $this->persistence) {
$server->setPersistence($this->persistence);
}
if (null !== $this->handlerClass) {
$server->setClass($this->handlerClass);
} elseif (null !== $this->handlerObject) {
$server->setObject($this->handlerObject);
}
return $server;
}
}
/**
* Cofigures the SOAP actor.
*
* @param string $actor Actor name
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withActor($actor)
{
$this->options['actor'] = $actor;
return $this;
}
/**
* Enables persistence.
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withPersistanceRequest()
{
$this->persistence = SOAP_PERSISTENCE_REQUEST;
return $this;
}
/**
* Enables the HTTP session. The handler object is persisted between multiple requests in a session.
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withPersistenceSession()
{
$this->persistence = SOAP_PERSISTENCE_SESSION;
return $this;
}
/**
* Enables reporting of internal errors to clients. This should only be enabled in development environments.
*
* @param boolean $enable Enable error reporting
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withErrorReporting($enable = true)
{
$this->errorReporting = $enable;
return $this;
}
/**
* SOAP attachment type Base64.
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withBase64Attachments()
{
$this->options['attachment_type'] = Helper::ATTACHMENTS_TYPE_BASE64;
return $this;
}
/**
* SOAP attachment type SwA.
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withSwaAttachments()
{
$this->options['attachment_type'] = Helper::ATTACHMENTS_TYPE_SWA;
return $this;
}
/**
* SOAP attachment type MTOM.
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withMtomAttachments()
{
$this->options['attachment_type'] = Helper::ATTACHMENTS_TYPE_MTOM;
return $this;
}
/**
* Configures the handler class or object.
*
* @param mixed $handler Can be either a class name or an object.
*
* @return \BeSimple\SoapServer\SoapServerBuilder
*/
public function withHandler($handler)
{
if (is_string($handler) && class_exists($handler)) {
$this->handlerClass = $handler;
$this->handlerObject = null;
} elseif (is_object($handler)) {
$this->handlerClass = null;
$this->handlerObject = $handler;
} else {
throw new \InvalidArgumentException('The handler has to be a class name or an object');
}
return $this;
}
/**
* Validate options.
*/
protected function validateOptions()
{
$this->validateWsdl();
if (null === $this->handlerClass && null === $this->handlerObject) {
throw new \InvalidArgumentException('The handler has to be configured!');
}
}
}

View File

@ -1,19 +0,0 @@
<?php
namespace BeSimple\SoapServer;
use BeSimple\SoapServer\SoapOptions\SoapServerOptions;
class SoapServerOptionsBuilder
{
public static function createWithDefaults($handlerClassOrObject)
{
return new SoapServerOptions(
$handlerClassOrObject,
SoapServerOptions::SOAP_SERVER_KEEP_ALIVE_OFF,
SoapServerOptions::SOAP_SERVER_ERROR_REPORTING_OFF,
SoapServerOptions::SOAP_SERVER_EXCEPTIONS_ON,
SoapServerOptions::SOAP_SERVER_PERSISTENCE_NONE
);
}
}

View File

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the BeSimpleSoapServer.
*
* (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\SoapServer\Tests;
use BeSimple\SoapServer\SoapServerBuilder;
/**
* UnitTest for \BeSimple\SoapServer\SoapServerBuilder
*
* @author Christian Kerl <christian-kerl@web.de>
*/
class SoapServerBuilderTest extends \PHPUnit_Framework_TestCase
{
public function testUnconfiguredWsdl()
{
$builder = $this->getSoapServerBuilder();
$this->setExpectedException('InvalidArgumentException');
$builder->build();
}
public function testUnconfiguredHandler()
{
$builder = $this->getSoapServerBuilder();
$builder->withWsdl('my.wsdl');
$this->setExpectedException('InvalidArgumentException');
$builder->build();
}
public function getSoapServerBuilder()
{
return new SoapServerBuilder();
}
}

View File

@ -67,11 +67,10 @@ class WsSecurityFilter extends WsSecurityFilterClientServer implements SoapReque
* Modify the given request XML.
*
* @param \BeSimple\SoapCommon\SoapRequest $request SOAP request
* @param int $attachmentType
*
* @return void
*/
public function filterRequest(CommonSoapRequest $request, $attachmentType)
public function filterRequest(CommonSoapRequest $request)
{
// get \DOMDocument from SOAP request
$dom = $request->getContentDocument();
@ -153,11 +152,10 @@ class WsSecurityFilter extends WsSecurityFilterClientServer implements SoapReque
* Modify the given request XML.
*
* @param \BeSimple\SoapCommon\SoapResponse $response SOAP response
* @param int $attachmentType
*
* @return void
*/
public function filterResponse(CommonSoapResponse $response, $attachmentType)
public function filterResponse(CommonSoapResponse $response)
{
// get \DOMDocument from SOAP response
$dom = $response->getContentDocument();
@ -192,7 +190,7 @@ class WsSecurityFilter extends WsSecurityFilterClientServer implements SoapReque
}
if (null !== $this->userSecurityKey && $this->userSecurityKey->hasKeys()) {
$guid = 'CertId-' . Helper::generateUuid();
$guid = 'CertId-' . Helper::generateUUID();
// add token references
$keyInfo = null;
if (null !== $this->tokenReferenceSignature) {
@ -218,7 +216,7 @@ class WsSecurityFilter extends WsSecurityFilterClientServer implements SoapReque
// encrypt soap document
if (null !== $this->serviceSecurityKey && $this->serviceSecurityKey->hasKeys()) {
$guid = 'EncKey-' . Helper::generateUuid();
$guid = 'EncKey-' . Helper::generateUUID();
// add token references
$keyInfo = null;
if (null !== $this->tokenReferenceEncryption) {

View File

@ -31,8 +31,16 @@ class XmlMimeFilter implements SoapResponseFilter
{
}
public function filterResponse(SoapResponse $response, $attachmentType)
/**
* Modify the given response XML.
*
* @param \BeSimple\SoapCommon\SoapResponse $response SOAP request
*
* @return void
*/
public function filterResponse(SoapResponse $response)
{
// get \DOMDocument from SOAP request
$dom = $response->getContentDocument();
// create FilterHelper
@ -58,6 +66,5 @@ class XmlMimeFilter implements SoapResponseFilter
}
}
return $response;
}
}