mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-28 19:49:20 +03:00
158 lines
3.3 KiB
PHP
158 lines
3.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of MailSo.
|
|
*
|
|
* (c) 2014 Usenko Timur
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace MailSo\Mime;
|
|
|
|
/**
|
|
* @category MailSo
|
|
* @package Mime
|
|
*/
|
|
class ParameterCollection extends \MailSo\Base\Collection
|
|
{
|
|
function __construct(string $sRawParams = '')
|
|
{
|
|
parent::__construct();
|
|
|
|
if (0 < \strlen($sRawParams))
|
|
{
|
|
$this->Parse($sRawParams);
|
|
}
|
|
}
|
|
|
|
public function append($oParameter, bool $bToTop = false) : void
|
|
{
|
|
assert($oParameter instanceof Parameter);
|
|
parent::append($oParameter, $bToTop);
|
|
}
|
|
|
|
public function ParameterValueByName(string $sName) : string
|
|
{
|
|
$sName = \strtolower(\trim($sName));
|
|
|
|
foreach ($this as $oParam)
|
|
{
|
|
if ($sName === \strtolower($oParam->Name()))
|
|
{
|
|
return $oParam->Value();
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
public function Parse(string $sRawParams) : self
|
|
{
|
|
$this->Clear();
|
|
|
|
$aDataToParse = \explode(';', $sRawParams);
|
|
|
|
foreach ($aDataToParse as $sParam)
|
|
{
|
|
$this->append(Parameter::CreateFromParameterLine($sParam));
|
|
}
|
|
|
|
$this->reParseParameters();
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function ToString(bool $bConvertSpecialsName = false) : string
|
|
{
|
|
$aResult = array();
|
|
foreach ($this as $oParam)
|
|
{
|
|
$sLine = $oParam->ToString($bConvertSpecialsName);
|
|
if (0 < \strlen($sLine))
|
|
{
|
|
$aResult[] = $sLine;
|
|
}
|
|
}
|
|
|
|
return 0 < \count($aResult) ? \implode('; ', $aResult) : '';
|
|
}
|
|
|
|
private function reParseParameters() : void
|
|
{
|
|
$aDataToReParse = $this->getArrayCopy();
|
|
$sCharset = \MailSo\Base\Enumerations\Charset::UTF_8;
|
|
|
|
$this->Clear();
|
|
|
|
$aPreParams = array();
|
|
foreach ($aDataToReParse as $oParam)
|
|
{
|
|
$aMatch = array();
|
|
$sParamName = $oParam->Name();
|
|
|
|
if (\preg_match('/([^\*]+)\*([\d]{1,2})\*/', $sParamName, $aMatch) && isset($aMatch[1], $aMatch[2])
|
|
&& 0 < \strlen($aMatch[1]) && \is_numeric($aMatch[2]))
|
|
{
|
|
if (!isset($aPreParams[$aMatch[1]]))
|
|
{
|
|
$aPreParams[$aMatch[1]] = array();
|
|
}
|
|
|
|
$sValue = $oParam->Value();
|
|
|
|
if (false !== \strpos($sValue, "''"))
|
|
{
|
|
$aValueParts = \explode("''", $sValue, 2);
|
|
if (\is_array($aValueParts) && 2 === \count($aValueParts) && 0 < \strlen($aValueParts[1]))
|
|
{
|
|
$sCharset = $aValueParts[0];
|
|
$sValue = $aValueParts[1];
|
|
}
|
|
}
|
|
|
|
$aPreParams[$aMatch[1]][(int) $aMatch[2]] = $sValue;
|
|
}
|
|
else if (\preg_match('/([^\*]+)\*/', $sParamName, $aMatch) && isset($aMatch[1]))
|
|
{
|
|
if (!isset($aPreParams[$aMatch[1]]))
|
|
{
|
|
$aPreParams[$aMatch[1]] = array();
|
|
}
|
|
|
|
$sValue = $oParam->Value();
|
|
if (false !== \strpos($sValue, "''"))
|
|
{
|
|
$aValueParts = \explode("''", $sValue, 2);
|
|
if (\is_array($aValueParts) && 2 === \count($aValueParts) && 0 < \strlen($aValueParts[1]))
|
|
{
|
|
$sCharset = $aValueParts[0];
|
|
$sValue = $aValueParts[1];
|
|
}
|
|
}
|
|
|
|
$aPreParams[$aMatch[1]][0] = $sValue;
|
|
}
|
|
else
|
|
{
|
|
$this->append($oParam);
|
|
}
|
|
}
|
|
|
|
foreach ($aPreParams as $sName => $aValues)
|
|
{
|
|
ksort($aValues);
|
|
$sResult = \implode(\array_values($aValues));
|
|
$sResult = \urldecode($sResult);
|
|
|
|
if (0 < \strlen($sCharset))
|
|
{
|
|
$sResult = \MailSo\Base\Utils::ConvertEncoding($sResult,
|
|
$sCharset, \MailSo\Base\Enumerations\Charset::UTF_8);
|
|
}
|
|
|
|
$this->append(new Parameter($sName, $sResult));
|
|
}
|
|
}
|
|
}
|