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

196 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2016-04-16 16:15:03 +00: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.
*/
2018-01-21 14:55:56 +00:00
use Egulias\EmailValidator\EmailValidator;
2016-04-16 16:15:03 +00:00
/**
* Creates MIME headers.
*
* @author Chris Corbyn
*/
2018-01-21 14:55:56 +00:00
class Swift_Mime_SimpleHeaderFactory implements Swift_Mime_CharsetObserver
2016-04-16 16:15:03 +00:00
{
/** The HeaderEncoder used by these headers */
2018-01-21 14:55:56 +00:00
private $encoder;
2016-04-16 16:15:03 +00:00
/** The Encoder used by parameters */
2018-01-21 14:55:56 +00:00
private $paramEncoder;
2016-04-16 16:15:03 +00:00
2018-01-21 14:55:56 +00:00
/** Strict EmailValidator */
private $emailValidator;
2016-04-16 16:15:03 +00:00
/** The charset of created Headers */
2018-01-21 14:55:56 +00:00
private $charset;
2016-04-16 16:15:03 +00:00
2021-12-07 16:11:34 +00:00
/** Swift_AddressEncoder */
private $addressEncoder;
2016-04-16 16:15:03 +00:00
/**
* Creates a new SimpleHeaderFactory using $encoder and $paramEncoder.
*
2021-12-07 16:11:34 +00:00
* @param string|null $charset
2016-04-16 16:15:03 +00:00
*/
2021-12-07 16:11:34 +00:00
public function __construct(Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder, EmailValidator $emailValidator, $charset = null, Swift_AddressEncoder $addressEncoder = null)
2016-04-16 16:15:03 +00:00
{
2018-01-21 14:55:56 +00:00
$this->encoder = $encoder;
$this->paramEncoder = $paramEncoder;
$this->emailValidator = $emailValidator;
$this->charset = $charset;
2021-12-07 16:11:34 +00:00
$this->addressEncoder = $addressEncoder ?? new Swift_AddressEncoder_IdnAddressEncoder();
2016-04-16 16:15:03 +00:00
}
/**
* Create a new Mailbox Header with a list of $addresses.
*
* @param string $name
* @param array|string|null $addresses
*
* @return Swift_Mime_Header
*/
public function createMailboxHeader($name, $addresses = null)
{
2021-12-07 16:11:34 +00:00
$header = new Swift_Mime_Headers_MailboxHeader($name, $this->encoder, $this->emailValidator, $this->addressEncoder);
2016-04-16 16:15:03 +00:00
if (isset($addresses)) {
$header->setFieldBodyModel($addresses);
}
2018-01-21 14:55:56 +00:00
$this->setHeaderCharset($header);
2016-04-16 16:15:03 +00:00
return $header;
}
/**
2018-01-21 14:55:56 +00:00
* Create a new Date header using $dateTime.
2016-04-16 16:15:03 +00:00
*
2021-12-07 16:11:34 +00:00
* @param string $name
2016-04-16 16:15:03 +00:00
*
* @return Swift_Mime_Header
*/
2018-01-21 14:55:56 +00:00
public function createDateHeader($name, DateTimeInterface $dateTime = null)
2016-04-16 16:15:03 +00:00
{
2018-01-21 14:55:56 +00:00
$header = new Swift_Mime_Headers_DateHeader($name);
if (isset($dateTime)) {
$header->setFieldBodyModel($dateTime);
2016-04-16 16:15:03 +00:00
}
2018-01-21 14:55:56 +00:00
$this->setHeaderCharset($header);
2016-04-16 16:15:03 +00:00
return $header;
}
/**
* Create a new basic text header with $name and $value.
*
* @param string $name
* @param string $value
*
* @return Swift_Mime_Header
*/
public function createTextHeader($name, $value = null)
{
2018-01-21 14:55:56 +00:00
$header = new Swift_Mime_Headers_UnstructuredHeader($name, $this->encoder);
2016-04-16 16:15:03 +00:00
if (isset($value)) {
$header->setFieldBodyModel($value);
}
2018-01-21 14:55:56 +00:00
$this->setHeaderCharset($header);
2016-04-16 16:15:03 +00:00
return $header;
}
/**
* Create a new ParameterizedHeader with $name, $value and $params.
*
* @param string $name
* @param string $value
* @param array $params
*
2018-01-21 14:55:56 +00:00
* @return Swift_Mime_Headers_ParameterizedHeader
2016-04-16 16:15:03 +00:00
*/
2021-12-07 16:11:34 +00:00
public function createParameterizedHeader($name, $value = null, $params = [])
2016-04-16 16:15:03 +00:00
{
2021-12-07 16:11:34 +00:00
$header = new Swift_Mime_Headers_ParameterizedHeader($name, $this->encoder, ('content-disposition' == strtolower($name ?? '')) ? $this->paramEncoder : null);
2016-04-16 16:15:03 +00:00
if (isset($value)) {
$header->setFieldBodyModel($value);
}
foreach ($params as $k => $v) {
$header->setParameter($k, $v);
}
2018-01-21 14:55:56 +00:00
$this->setHeaderCharset($header);
2016-04-16 16:15:03 +00:00
return $header;
}
/**
* Create a new ID header for Message-ID or Content-ID.
*
* @param string $name
* @param string|array $ids
*
* @return Swift_Mime_Header
*/
public function createIdHeader($name, $ids = null)
{
2018-01-21 14:55:56 +00:00
$header = new Swift_Mime_Headers_IdentificationHeader($name, $this->emailValidator);
2016-04-16 16:15:03 +00:00
if (isset($ids)) {
$header->setFieldBodyModel($ids);
}
2018-01-21 14:55:56 +00:00
$this->setHeaderCharset($header);
2016-04-16 16:15:03 +00:00
return $header;
}
/**
* Create a new Path header with an address (path) in it.
*
* @param string $name
* @param string $path
*
* @return Swift_Mime_Header
*/
public function createPathHeader($name, $path = null)
{
2018-01-21 14:55:56 +00:00
$header = new Swift_Mime_Headers_PathHeader($name, $this->emailValidator);
2016-04-16 16:15:03 +00:00
if (isset($path)) {
$header->setFieldBodyModel($path);
}
2018-01-21 14:55:56 +00:00
$this->setHeaderCharset($header);
2016-04-16 16:15:03 +00:00
return $header;
}
/**
* Notify this observer that the entity's charset has changed.
*
* @param string $charset
*/
public function charsetChanged($charset)
{
2018-01-21 14:55:56 +00:00
$this->charset = $charset;
$this->encoder->charsetChanged($charset);
$this->paramEncoder->charsetChanged($charset);
2016-04-16 16:15:03 +00:00
}
/**
* Make a deep copy of object.
*/
public function __clone()
{
2018-01-21 14:55:56 +00:00
$this->encoder = clone $this->encoder;
$this->paramEncoder = clone $this->paramEncoder;
2021-12-07 16:11:34 +00:00
$this->addressEncoder = clone $this->addressEncoder;
2016-04-16 16:15:03 +00:00
}
/** Apply the charset to the Header */
2018-01-21 14:55:56 +00:00
private function setHeaderCharset(Swift_Mime_Header $header)
2016-04-16 16:15:03 +00:00
{
2018-01-21 14:55:56 +00:00
if (isset($this->charset)) {
$header->setCharset($this->charset);
2016-04-16 16:15:03 +00:00
}
}
}