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

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

View File

@ -27,7 +27,7 @@ class Parser
*
* @return \BeSimple\SoapCommon\Mime\MultiPart
*/
public static function parseMimeMessage($mimeMessage, array $headers = array())
public static function parseMimeMessage($mimeMessage, array $headers = [])
{
$boundary = null;
$start = null;
@ -37,7 +37,7 @@ class Parser
// add given headers, e.g. coming from HTTP headers
if (count($headers) > 0) {
foreach ($headers as $name => $value) {
if ($name == 'Content-Type') {
if ($name === 'Content-Type') {
self::parseContentTypeHeader($multipart, $name, $value);
$boundary = $multipart->getHeader('Content-Type', 'boundary');
$start = $multipart->getHeader('Content-Type', 'start');
@ -49,43 +49,57 @@ class Parser
}
$content = '';
$currentPart = $multipart;
$lines = preg_split("/(\r\n)/", $mimeMessage);
foreach ($lines as $line) {
// ignore http status code and POST *
if (substr($line, 0, 5) == 'HTTP/' || substr($line, 0, 4) == 'POST') {
continue;
}
if (isset($currentHeader)) {
if (isset($line[0]) && ($line[0] === ' ' || $line[0] === "\t")) {
$currentHeader .= $line;
$lines = preg_split("/(\r\n)|(\n)/", $mimeMessage);
if (self::hasBoundary($lines)) {
foreach ($lines as $line) {
// ignore http status code and POST *
if (substr($line, 0, 5) == 'HTTP/' || substr($line, 0, 4) == 'POST') {
continue;
}
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));
if (isset($currentHeader)) {
if (isset($line[0]) && ($line[0] === ' ' || $line[0] === "\t")) {
$currentHeader .= $line;
continue;
}
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) == '') {
$inHeader = false;
if ($inHeader) {
if (trim($line) == '') {
$inHeader = false;
continue;
}
$currentHeader = $line;
continue;
}
$currentHeader = $line;
continue;
} else {
// check if we hit any of the boundaries
if (strlen($line) > 0 && $line[0] == "-") {
if (strcmp(trim($line), '--' . $boundary) === 0) {
if ($currentPart instanceof Part) {
$content = substr($content, 0, -2);
} else {
if (self::isBoundary($line)) {
if (strcmp(trim($line), '--' . $boundary) === 0) {
if ($currentPart instanceof Part) {
$content = substr($content, 0, -1);
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);
}
$currentPart = new Part();
$hitFirstBoundary = true;
$inHeader = true;
$content = '';
} elseif (strcmp(trim($line), '--' . $boundary . '--') === 0) {
$content = substr($content, 0, -1);
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;
@ -93,34 +107,24 @@ class Parser
$start = $currentPart->getHeader('Content-ID');
}
$multipart->addPart($currentPart, $isMain);
$content = '';
}
$currentPart = new Part();
$hitFirstBoundary = true;
$inHeader = true;
$content = '';
} elseif (strcmp(trim($line), '--' . $boundary . '--') === 0) {
$content = substr($content, 0, -2);
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');
} else {
if ($hitFirstBoundary === false) {
if (trim($line) !== '') {
$inHeader = true;
$currentHeader = $line;
continue;
}
}
$multipart->addPart($currentPart, $isMain);
$content = '';
$content .= $line . "\n";
}
} 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;
}
@ -135,7 +139,6 @@ class Parser
* @param string $headerName Header name
* @param string $headerValue Header value
*
* @return null
*/
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 string $content Content to decode
*
* @return null
*/
private static function decodeContent(Part $part, $content)
{
@ -184,4 +186,21 @@ class Parser
}
$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 $contentId Content id
*
* @return void
*/
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
{
/**
* Mime headers.
*
* @var array(string=>mixed|array(mixed))
*/
protected $headers = array();
protected $headers = [];
/**
* Add a new header to the mime part.
@ -39,10 +34,10 @@ abstract class PartHeader
{
if (isset($this->headers[$name]) && !is_null($subValue)) {
if (!is_array($this->headers[$name])) {
$this->headers[$name] = array(
$this->headers[$name] = [
'@' => $this->headers[$name],
$value => $subValue,
);
];
} else {
$this->headers[$name][$value] = $subValue;
}
@ -76,6 +71,7 @@ abstract class PartHeader
return $this->headers[$name];
}
}
return null;
}
@ -86,19 +82,12 @@ abstract class PartHeader
*/
protected function generateHeaders()
{
$charset = strtolower($this->getHeader('Content-Type', 'charset'));
$preferences = array(
'scheme' => 'Q',
'input-charset' => 'utf-8',
'output-charset' => $charset,
);
$headers = '';
foreach ($this->headers as $fieldName => $value) {
$fieldValue = $this->generateHeaderFieldValue($value);
// do not use proper encoding as Apache Axis does not understand this
// $headers .= iconv_mime_encode($field_name, $field_value, $preferences) . "\r\n";
$headers .= $fieldName . ': ' . $fieldValue . "\r\n";
$headers .= $fieldName . ': ' . $fieldValue . "\n";
}
return $headers;
}
@ -124,6 +113,7 @@ abstract class PartHeader
} else {
$fieldValue .= $value;
}
return $fieldValue;
}
@ -143,4 +133,4 @@ abstract class PartHeader
return $string;
}
}
}
}