Files
BeSimpleSoap/src/BeSimple/SoapCommon/Mime/MultiPart.php

189 lines
4.8 KiB
PHP
Raw Normal View History

2011-10-22 11:25:26 +02:00
<?php
/*
* This file is part of 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.
*/
2011-10-22 11:32:26 +02:00
namespace BeSimple\SoapCommon\Mime;
use Exception;
2011-10-22 11:32:26 +02:00
use BeSimple\SoapCommon\Helper;
2011-10-22 11:25:26 +02:00
/**
* Mime multi part container.
*
* Headers:
* - MIME-Version
* - Content-Type
* - Content-ID
* - Content-Location
* - Content-Description
*
* @author Andreas Schamberger <mail@andreass.net>
*/
class MultiPart extends PartHeader
{
/**
* Content-ID of main part.
*
* @var string
*/
protected $mainPartContentId;
/**
* Mime parts.
*
* @var \BeSimple\SoapCommon\Mime\Part[]
2011-10-22 11:25:26 +02:00
*/
protected $parts = [];
2011-10-22 11:25:26 +02:00
/**
* Construct new mime object.
*
* @param string $boundary
2011-10-22 11:25:26 +02:00
*/
public function __construct($boundary = null)
{
$this->setHeader('MIME-Version', '1.0');
$this->setHeader('Content-Type', 'multipart/related');
$this->setHeader('Content-Type', 'type', 'text/xml');
$this->setHeader('Content-Type', 'charset', 'utf-8');
if ($boundary !== null) {
$this->setHeader('Content-Type', 'boundary', $boundary);
2011-10-22 11:25:26 +02:00
}
}
/**
* Get mime message of this object (without headers).
*
* @param boolean $withHeaders Returned mime message contains headers
2011-12-17 16:25:49 +01:00
*
2011-10-22 11:25:26 +02:00
* @return string
*/
public function getMimeMessage($withHeaders = false)
{
$message = ($withHeaders === true) ? $this->generateHeaders() : "";
// add parts
foreach ($this->parts as $part) {
$message .= "\n" . '--' . $this->getHeader('Content-Type', 'boundary') . "\n";
2011-10-22 11:25:26 +02:00
$message .= $part->getMessagePart();
}
$message .= "\n" . '--' . $this->getHeader('Content-Type', 'boundary') . '--';
2011-10-22 11:25:26 +02:00
return $message;
}
/**
* Get string array with MIME headers for usage in HTTP header (with CURL).
2011-10-31 11:55:14 +01:00
* Only 'Content-Type' and 'Content-Description' headers are returned.
2011-10-22 11:25:26 +02:00
*
* @return string[]
2011-10-22 11:25:26 +02:00
*/
public function getHeadersForHttp()
{
$allowedHeaders = [
2011-10-22 11:25:26 +02:00
'Content-Type',
'Content-Description',
];
$headers = [];
2011-10-22 11:25:26 +02:00
foreach ($this->headers as $fieldName => $value) {
if (in_array($fieldName, $allowedHeaders)) {
2011-10-31 11:55:14 +01:00
$fieldValue = $this->generateHeaderFieldValue($value);
2011-10-22 11:25:26 +02:00
// for http only ISO-8859-1
$headers[] = $fieldName . ': '. iconv('utf-8', 'ISO-8859-1//TRANSLIT', $fieldValue);
}
}
2011-10-22 11:25:26 +02:00
return $headers;
}
/**
* Add new part to MIME message.
*
* @param \BeSimple\SoapCommon\Mime\Part $part Part that is added
* @param boolean $isMain Is the given part the main part of mime message
2011-12-17 16:25:49 +01:00
*
2011-10-22 11:25:26 +02:00
* @return void
*/
public function addPart(Part $part, $isMain = false)
{
$contentId = trim($part->getHeader('Content-ID'), '<>');
if ($isMain === true) {
$this->mainPartContentId = $contentId;
$this->setHeader('Content-Type', 'start', $part->getHeader('Content-ID'));
} else {
$part->setHeader('Content-Location', $contentId);
2011-10-22 11:25:26 +02:00
}
$this->parts[$contentId] = $part;
}
/**
* Get part with given content id.
2011-10-22 11:25:26 +02:00
*
* @param string $contentId Content id of desired part
2011-12-17 16:25:49 +01:00
*
* @return \BeSimple\SoapCommon\Mime\Part
2011-10-22 11:25:26 +02:00
*/
public function getPart($contentId)
2011-10-22 11:25:26 +02:00
{
if (isset($this->parts[$contentId])) {
return $this->parts[$contentId];
}
throw new Exception('MimePart not found by ID: ' . $contentId);
2011-10-22 11:25:26 +02:00
}
/**
* Get main part.
2011-10-22 11:25:26 +02:00
*
* @return \BeSimple\SoapCommon\Mime\Part
*/
public function getMainPart()
{
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.
2011-12-17 16:25:49 +01:00
*
* @return \BeSimple\SoapCommon\Mime\Part[]
2011-10-22 11:25:26 +02:00
*/
public function getAttachments()
2011-10-22 11:25:26 +02:00
{
$parts = [];
foreach ($this->parts as $cid => $part) {
if ($cid !== $this->mainPartContentId) {
$parts[$cid] = $part;
2011-10-22 11:25:26 +02:00
}
}
2011-10-22 11:25:26 +02:00
return $parts;
}
/**
* Returns a unique boundary string.
*
* @return string
*/
public function generateBoundary()
2011-10-22 11:25:26 +02:00
{
2016-11-24 15:18:00 +01:00
return 'multipart-boundary-' . Helper::generateUUID() . '@response.info';
2011-10-22 11:25:26 +02:00
}
public function getMainPartContentId()
{
return $this->mainPartContentId;
}
2011-10-22 11:25:26 +02:00
}