composer package updates
[openemr.git] / vendor / zendframework / zend-mail / src / Header / MessageId.php
blob84f64413ec90432211656a6ce46d9dd091040837
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-mail for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Mail\Header;
10 class MessageId implements HeaderInterface
12 /**
13 * @var string
15 protected $messageId;
17 public static function fromString($headerLine)
19 list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
20 $value = HeaderWrap::mimeDecodeValue($value);
22 // check to ensure proper header type for this factory
23 if (strtolower($name) !== 'message-id') {
24 throw new Exception\InvalidArgumentException('Invalid header line for Message-ID string');
27 $header = new static();
28 $header->setId($value);
30 return $header;
33 public function getFieldName()
35 return 'Message-ID';
38 public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
40 return $this->messageId;
43 public function setEncoding($encoding)
45 // This header must be always in US-ASCII
46 return $this;
49 public function getEncoding()
51 return 'ASCII';
54 public function toString()
56 return 'Message-ID: ' . $this->getFieldValue();
59 /**
60 * Set the message id
62 * @param string|null $id
63 * @return MessageId
65 public function setId($id = null)
67 if ($id === null) {
68 $id = $this->createMessageId();
69 } else {
70 $id = trim($id, '<>');
73 if (! HeaderValue::isValid($id)
74 || preg_match("/[\r\n]/", $id)
75 ) {
76 throw new Exception\InvalidArgumentException('Invalid ID detected');
79 $this->messageId = sprintf('<%s>', $id);
80 return $this;
83 /**
84 * Retrieve the message id
86 * @return string
88 public function getId()
90 return $this->messageId;
93 /**
94 * Creates the Message-ID
96 * @return string
98 public function createMessageId()
100 $time = time();
102 if (isset($_SERVER['REMOTE_ADDR'])) {
103 $user = $_SERVER['REMOTE_ADDR'];
104 } else {
105 $user = getmypid();
108 $rand = mt_rand();
110 if (isset($_SERVER["SERVER_NAME"])) {
111 $hostName = $_SERVER["SERVER_NAME"];
112 } else {
113 $hostName = php_uname('n');
116 return sha1($time . $user . $rand) . '@' . $hostName;