dolibarr/htdocs/includes/swiftmailer/lib/classes/Swift/Mime/MimePart.php

213 lines
5.7 KiB
PHP
Raw Normal View History

2016-04-16 18:15:03 +02:00
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* A MIME part, in a multipart message.
*
* @author Chris Corbyn
*/
class Swift_Mime_MimePart extends Swift_Mime_SimpleMimeEntity
{
/** The format parameter last specified by the user */
2018-01-21 15:55:56 +01:00
protected $userFormat;
2016-04-16 18:15:03 +02:00
/** The charset last specified by the user */
2018-01-21 15:55:56 +01:00
protected $userCharset;
2016-04-16 18:15:03 +02:00
/** The delsp parameter last specified by the user */
2018-01-21 15:55:56 +01:00
protected $userDelSp;
2016-04-16 18:15:03 +02:00
/** The nesting level of this MimePart */
2018-01-21 15:55:56 +01:00
private $nestingLevel = self::LEVEL_ALTERNATIVE;
2016-04-16 18:15:03 +02:00
/**
* Create a new MimePart with $headers, $encoder and $cache.
*
2018-01-21 15:55:56 +01:00
* @param Swift_Mime_SimpleHeaderSet $headers
* @param Swift_Mime_ContentEncoder $encoder
* @param Swift_KeyCache $cache
* @param Swift_IdGenerator $idGenerator
* @param string $charset
2016-04-16 18:15:03 +02:00
*/
2018-01-21 15:55:56 +01:00
public function __construct(Swift_Mime_SimpleHeaderSet $headers, Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, Swift_IdGenerator $idGenerator, $charset = null)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
parent::__construct($headers, $encoder, $cache, $idGenerator);
2016-04-16 18:15:03 +02:00
$this->setContentType('text/plain');
2018-01-21 15:55:56 +01:00
if (null !== $charset) {
2016-04-16 18:15:03 +02:00
$this->setCharset($charset);
}
}
/**
* Set the body of this entity, either as a string, or as an instance of
* {@link Swift_OutputByteStream}.
*
* @param mixed $body
* @param string $contentType optional
* @param string $charset optional
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function setBody($body, $contentType = null, $charset = null)
{
if (isset($charset)) {
$this->setCharset($charset);
}
2018-01-21 15:55:56 +01:00
$body = $this->convertString($body);
2016-04-16 18:15:03 +02:00
parent::setBody($body, $contentType);
return $this;
}
/**
* Get the character set of this entity.
*
* @return string
*/
public function getCharset()
{
2018-01-21 15:55:56 +01:00
return $this->getHeaderParameter('Content-Type', 'charset');
2016-04-16 18:15:03 +02:00
}
/**
* Set the character set of this entity.
*
* @param string $charset
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function setCharset($charset)
{
2018-01-21 15:55:56 +01:00
$this->setHeaderParameter('Content-Type', 'charset', $charset);
if ($charset !== $this->userCharset) {
$this->clearCache();
2016-04-16 18:15:03 +02:00
}
2018-01-21 15:55:56 +01:00
$this->userCharset = $charset;
2016-04-16 18:15:03 +02:00
parent::charsetChanged($charset);
return $this;
}
/**
* Get the format of this entity (i.e. flowed or fixed).
*
* @return string
*/
public function getFormat()
{
2018-01-21 15:55:56 +01:00
return $this->getHeaderParameter('Content-Type', 'format');
2016-04-16 18:15:03 +02:00
}
/**
* Set the format of this entity (flowed or fixed).
*
* @param string $format
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function setFormat($format)
{
2018-01-21 15:55:56 +01:00
$this->setHeaderParameter('Content-Type', 'format', $format);
$this->userFormat = $format;
2016-04-16 18:15:03 +02:00
return $this;
}
/**
* Test if delsp is being used for this entity.
*
* @return bool
*/
public function getDelSp()
{
2018-01-21 15:55:56 +01:00
return 'yes' === $this->getHeaderParameter('Content-Type', 'delsp');
2016-04-16 18:15:03 +02:00
}
/**
* Turn delsp on or off for this entity.
*
* @param bool $delsp
*
2018-01-21 15:55:56 +01:00
* @return $this
2016-04-16 18:15:03 +02:00
*/
public function setDelSp($delsp = true)
{
2018-01-21 15:55:56 +01:00
$this->setHeaderParameter('Content-Type', 'delsp', $delsp ? 'yes' : null);
$this->userDelSp = $delsp;
2016-04-16 18:15:03 +02:00
return $this;
}
/**
* Get the nesting level of this entity.
*
* @see LEVEL_TOP, LEVEL_ALTERNATIVE, LEVEL_MIXED, LEVEL_RELATED
*
* @return int
*/
public function getNestingLevel()
{
2018-01-21 15:55:56 +01:00
return $this->nestingLevel;
2016-04-16 18:15:03 +02:00
}
/**
* Receive notification that the charset has changed on this document, or a
* parent document.
*
* @param string $charset
*/
public function charsetChanged($charset)
{
$this->setCharset($charset);
}
/** Fix the content-type and encoding of this entity */
2018-01-21 15:55:56 +01:00
protected function fixHeaders()
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
parent::fixHeaders();
2016-04-16 18:15:03 +02:00
if (count($this->getChildren())) {
2018-01-21 15:55:56 +01:00
$this->setHeaderParameter('Content-Type', 'charset', null);
$this->setHeaderParameter('Content-Type', 'format', null);
$this->setHeaderParameter('Content-Type', 'delsp', null);
2016-04-16 18:15:03 +02:00
} else {
2018-01-21 15:55:56 +01:00
$this->setCharset($this->userCharset);
$this->setFormat($this->userFormat);
$this->setDelSp($this->userDelSp);
2016-04-16 18:15:03 +02:00
}
}
/** Set the nesting level of this entity */
2018-01-21 15:55:56 +01:00
protected function setNestingLevel($level)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
$this->nestingLevel = $level;
2016-04-16 18:15:03 +02:00
}
/** Encode charset when charset is not utf-8 */
2018-01-21 15:55:56 +01:00
protected function convertString($string)
2016-04-16 18:15:03 +02:00
{
$charset = strtolower($this->getCharset());
2018-01-21 15:55:56 +01:00
if (!in_array($charset, array('utf-8', 'iso-8859-1', 'iso-8859-15', ''))) {
2016-04-16 18:15:03 +02:00
// mb_convert_encoding must be the first one to check, since iconv cannot convert some words.
if (function_exists('mb_convert_encoding')) {
$string = mb_convert_encoding($string, $charset, 'utf-8');
} elseif (function_exists('iconv')) {
$string = iconv('utf-8//TRANSLIT//IGNORE', $charset, $string);
} else {
throw new Swift_SwiftException('No suitable convert encoding function (use UTF-8 as your charset or install the mbstring or iconv extension).');
}
return $string;
}
return $string;
}
}