snappymail/rainloop/v/0.0.0/app/libraries/MailSo/Mime/ParameterCollection.php

214 lines
4.3 KiB
PHP
Raw Normal View History

2013-09-25 03:04:44 +08:00
<?php
2014-10-17 18:15:19 +08:00
/*
* 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.
*/
2013-09-25 03:04:44 +08:00
namespace MailSo\Mime;
/**
* @category MailSo
* @package Mime
*/
class ParameterCollection extends \MailSo\Base\Collection
{
/**
* @access protected
*
* @param string $sRawParams = ''
*/
protected function __construct($sRawParams = '')
{
parent::__construct();
2014-10-24 01:59:21 +08:00
if (0 < \strlen($sRawParams))
2013-09-25 03:04:44 +08:00
{
$this->Parse($sRawParams);
}
}
/**
* @param string $sRawParams = ''
*
* @return \MailSo\Mime\ParameterCollection
*/
public static function NewInstance($sRawParams = '')
{
return new self($sRawParams);
}
/**
* @return \MailSo\Mime\Parameter|null
*/
public function &GetByIndex($iIndex)
{
$mResult = null;
$mResult =& parent::GetByIndex($iIndex);
return $mResult;
}
/**
* @param array $aList
*
* @return \MailSo\Mime\ParameterCollection
*
* @throws \MailSo\Base\Exceptions\InvalidArgumentException
*/
public function SetAsArray($aList)
{
parent::SetAsArray($aList);
return $this;
}
/**
* @param string $sName
*
* @return string
*/
public function ParameterValueByName($sName)
{
$sResult = '';
2014-10-24 01:59:21 +08:00
$sName = \trim($sName);
2013-09-25 03:04:44 +08:00
$aParams =& $this->GetAsArray();
foreach ($aParams as /* @var $oParam \MailSo\Mime\ParameterCollection */ $oParam)
{
2014-10-24 01:59:21 +08:00
if (\strtolower($sName) === \strtolower($oParam->Name()))
2013-09-25 03:04:44 +08:00
{
$sResult = $oParam->Value();
break;
}
}
return $sResult;
}
/**
* @param string $sRawParams
*
* @return \MailSo\Mime\ParameterCollection
*/
public function Parse($sRawParams)
{
$this->Clear();
2014-10-24 01:59:21 +08:00
$aDataToParse = \explode(';', $sRawParams);
2013-09-25 03:04:44 +08:00
foreach ($aDataToParse as $sParam)
{
$this->Add(Parameter::CreateFromParameterLine($sParam));
}
$this->reParseParameters();
return $this;
}
/**
* @param bool $bConvertSpecialsName = false
*
* @return string
*/
public function ToString($bConvertSpecialsName = false)
{
$aResult = array();
$aParams =& $this->GetAsArray();
foreach ($aParams as /* @var $oParam \MailSo\Mime\Parameter */ $oParam)
{
$sLine = $oParam->ToString($bConvertSpecialsName);
2014-10-24 01:59:21 +08:00
if (0 < \strlen($sLine))
2013-09-25 03:04:44 +08:00
{
$aResult[] = $sLine;
}
}
2014-10-24 01:59:21 +08:00
return 0 < \count($aResult) ? \implode('; ', $aResult) : '';
2013-09-25 03:04:44 +08:00
}
/**
* @return void
*/
private function reParseParameters()
{
$aDataToReParse = $this->CloneAsArray();
$sCharset = \MailSo\Base\Enumerations\Charset::UTF_8;
$this->Clear();
$aPreParams = array();
foreach ($aDataToReParse as /* @var $oParam \MailSo\Mime\Parameter */ $oParam)
{
$aMatch = array();
$sParamName = $oParam->Name();
2014-10-24 01:59:21 +08:00
if (\preg_match('/([^\*]+)\*([\d]{1,2})\*/', $sParamName, $aMatch) && isset($aMatch[1], $aMatch[2])
&& 0 < \strlen($aMatch[1]) && \is_numeric($aMatch[2]))
2013-09-25 03:04:44 +08:00
{
if (!isset($aPreParams[$aMatch[1]]))
{
$aPreParams[$aMatch[1]] = array();
}
$sValue = $oParam->Value();
2014-10-24 01:59:21 +08:00
if (false !== \strpos($sValue, "''"))
2013-09-25 03:04:44 +08:00
{
2014-10-24 01:59:21 +08:00
$aValueParts = \explode("''", $sValue, 2);
if (\is_array($aValueParts) && 2 === \count($aValueParts) && 0 < \strlen($aValueParts[1]))
{
$sCharset = $aValueParts[0];
$sValue = $aValueParts[1];
}
2013-09-25 03:04:44 +08:00
}
$aPreParams[$aMatch[1]][(int) $aMatch[2]] = $sValue;
}
2014-10-24 01:59:21 +08:00
else if (\preg_match('/([^\*]+)\*/', $sParamName, $aMatch) && isset($aMatch[1]))
2013-09-25 03:04:44 +08:00
{
if (!isset($aPreParams[$aMatch[1]]))
{
$aPreParams[$aMatch[1]] = array();
}
$sValue = $oParam->Value();
2014-10-24 01:59:21 +08:00
if (false !== \strpos($sValue, "''"))
2013-09-25 03:04:44 +08:00
{
2014-10-24 01:59:21 +08:00
$aValueParts = \explode("''", $sValue, 2);
if (\is_array($aValueParts) && 2 === \count($aValueParts) && 0 < \strlen($aValueParts[1]))
{
$sCharset = $aValueParts[0];
$sValue = $aValueParts[1];
}
2013-09-25 03:04:44 +08:00
}
$aPreParams[$aMatch[1]][0] = $sValue;
}
else
{
$this->Add($oParam);
}
}
foreach ($aPreParams as $sName => $aValues)
{
ksort($aValues);
2014-10-24 01:59:21 +08:00
$sResult = \implode(\array_values($aValues));
$sResult = \urldecode($sResult);
2013-09-25 03:04:44 +08:00
2014-10-24 01:59:21 +08:00
if (0 < \strlen($sCharset))
2013-09-25 03:04:44 +08:00
{
$sResult = \MailSo\Base\Utils::ConvertEncoding($sResult,
$sCharset, \MailSo\Base\Enumerations\Charset::UTF_8);
}
$this->Add(Parameter::NewInstance($sName, $sResult));
}
}
}