Open Journal Systems  3.3.0
DataPart.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
17 
21 class DataPart extends TextPart
22 {
23  private static $mimeTypes;
24 
25  private $filename;
26  private $mediaType;
27  private $cid;
28  private $handle;
29 
33  public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
34  {
35  if (null === $contentType) {
36  $contentType = 'application/octet-stream';
37  }
38  list($this->mediaType, $subtype) = explode('/', $contentType);
39 
40  parent::__construct($body, null, $subtype, $encoding);
41 
42  $this->filename = $filename;
43  $this->setName($filename);
44  $this->setDisposition('attachment');
45  }
46 
47  public static function fromPath(string $path, string $name = null, string $contentType = null): self
48  {
49  // FIXME: if file is not readable, exception?
50 
51  if (null === $contentType) {
52  $ext = strtolower(substr($path, strrpos($path, '.') + 1));
53  if (null === self::$mimeTypes) {
54  self::$mimeTypes = new MimeTypes();
55  }
56  $contentType = self::$mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';
57  }
58 
59  if (false === $handle = @fopen($path, 'r', false)) {
60  throw new InvalidArgumentException(sprintf('Unable to open path "%s".', $path));
61  }
62  $p = new self($handle, $name ?: basename($path), $contentType);
63  $p->handle = $handle;
64 
65  return $p;
66  }
67 
71  public function asInline()
72  {
73  return $this->setDisposition('inline');
74  }
75 
76  public function getContentId(): string
77  {
78  return $this->cid ?: $this->cid = $this->generateContentId();
79  }
80 
81  public function hasContentId(): bool
82  {
83  return null !== $this->cid;
84  }
85 
86  public function getMediaType(): string
87  {
88  return $this->mediaType;
89  }
90 
91  public function getPreparedHeaders(): Headers
92  {
93  $headers = parent::getPreparedHeaders();
94 
95  if (null !== $this->cid) {
96  $headers->setHeaderBody('Id', 'Content-ID', $this->cid);
97  }
98 
99  if (null !== $this->filename) {
100  $headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename);
101  }
102 
103  return $headers;
104  }
105 
106  public function asDebugString(): string
107  {
108  $str = parent::asDebugString();
109  if (null !== $this->filename) {
110  $str .= ' filename: '.$this->filename;
111  }
112 
113  return $str;
114  }
115 
116  private function generateContentId(): string
117  {
118  return bin2hex(random_bytes(16)).'@symfony';
119  }
120 
121  public function __destruct()
122  {
123  if (null !== $this->handle && \is_resource($this->handle)) {
124  fclose($this->handle);
125  }
126  }
127 
131  public function __sleep()
132  {
133  // converts the body to a string
134  parent::__sleep();
135 
136  $this->_parent = [];
137  foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
138  $r = new \ReflectionProperty(TextPart::class, $name);
139  $r->setAccessible(true);
140  $this->_parent[$name] = $r->getValue($this);
141  }
142  $this->_headers = $this->getHeaders();
143 
144  return ['_headers', '_parent', 'filename', 'mediaType'];
145  }
146 
147  public function __wakeup()
148  {
149  $r = new \ReflectionProperty(AbstractPart::class, 'headers');
150  $r->setAccessible(true);
151  $r->setValue($this, $this->_headers);
152  unset($this->_headers);
153 
154  foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
155  $r = new \ReflectionProperty(TextPart::class, $name);
156  $r->setAccessible(true);
157  $r->setValue($this, $this->_parent[$name]);
158  }
159  unset($this->_parent);
160  }
161 }
Symfony\Component\Mime\Part\AbstractPart\getHeaders
getHeaders()
Definition: AbstractPart.php:28
Symfony\Component\Mime\Part\DataPart\asDebugString
asDebugString()
Definition: DataPart.php:106
Symfony\Component\Mime\Part\DataPart\__construct
__construct($body, string $filename=null, string $contentType=null, string $encoding=null)
Definition: DataPart.php:33
Symfony\Component\Mime\Part\DataPart\fromPath
static fromPath(string $path, string $name=null, string $contentType=null)
Definition: DataPart.php:47
Symfony\Component\Mime\Part\DataPart\__destruct
__destruct()
Definition: DataPart.php:121
Symfony\Component\Mime\Part\DataPart
Definition: DataPart.php:21
Symfony\Component\Mime\Part
Definition: AbstractMultipartPart.php:12
Symfony\Component\Mime\Part\TextPart\setDisposition
setDisposition(string $disposition)
Definition: TextPart.php:75
Symfony\Component\Mime\Part\DataPart\hasContentId
hasContentId()
Definition: DataPart.php:81
Symfony\Component\Mime\Part\TextPart
Definition: TextPart.php:24
Symfony\Component\Mime\Part\DataPart\getMediaType
getMediaType()
Definition: DataPart.php:86
Symfony\Component\Mime\Part\DataPart\asInline
asInline()
Definition: DataPart.php:71
Symfony\Component\Mime\Part\DataPart\__sleep
__sleep()
Definition: DataPart.php:131
Symfony\Component\Mime\Exception\InvalidArgumentException
Definition: vendor/symfony/mime/Exception/InvalidArgumentException.php:17
Symfony\Component\Mime\Header\Headers
Definition: Headers.php:22
Symfony\Component\Mime\MimeTypes
Definition: MimeTypes.php:37
Symfony\Component\Mime\Part\TextPart\setName
setName($name)
Definition: TextPart.php:87
Symfony\Component\Mime\Part\DataPart\getContentId
getContentId()
Definition: DataPart.php:76
Symfony\Component\Mime\Part\DataPart\getPreparedHeaders
getPreparedHeaders()
Definition: DataPart.php:91
Symfony\Component\Mime\Part\DataPart\__wakeup
__wakeup()
Definition: DataPart.php:147