PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / vendor / symfony / mime / Part / DataPart.php

DataPart.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at vendor/symfony/mime/Part/DataPart.php

184 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Mime\Part;
13
14 use Symfony\Component\Mime\Exception\InvalidArgumentException;
15 use Symfony\Component\Mime\Header\Headers;
16 use Symfony\Component\Mime\MimeTypes;
17
18 /**
19 * @author Fabien Potencier <[email protected]>
20 */
21 class DataPart extends TextPart
22 {
23 /** @internal */
24 protected $_parent;
25
26 private static $mimeTypes;
27
28 private $filename;
29 private $mediaType;
30 private $cid;
31 private $handle;
32
33 /**
34 * @param resource|string $body
35 */
36 public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
37 {
38 unset($this->_parent);
39
40 if (null === $contentType) {
41 $contentType = 'application/octet-stream';
42 }
43 [$this->mediaType, $subtype] = explode('/', $contentType);
44
45 parent::__construct($body, null, $subtype, $encoding);
46
47 if (null !== $filename) {
48 $this->filename = $filename;
49 $this->setName($filename);
50 }
51 $this->setDisposition('attachment');
52 }
53
54 public static function fromPath(string $path, string $name = null, string $contentType = null): self
55 {
56 if (null === $contentType) {
57 $ext = strtolower(substr($path, strrpos($path, '.') + 1));
58 if (null === self::$mimeTypes) {
59 self::$mimeTypes = new MimeTypes();
60 }
61 $contentType = self::$mimeTypes->getMimeTypes($ext)[0] ?? 'application/octet-stream';
62 }
63
64 if ((is_file($path) && !is_readable($path)) || is_dir($path)) {
65 throw new InvalidArgumentException(sprintf('Path "%s" is not readable.', $path));
66 }
67
68 if (false === $handle = @fopen($path, 'r', false)) {
69 throw new InvalidArgumentException(sprintf('Unable to open path "%s".', $path));
70 }
71
72 if (!is_file($path)) {
73 $cache = fopen('php://temp', 'r+');
74 stream_copy_to_stream($handle, $cache);
75 $handle = $cache;
76 }
77
78 $p = new self($handle, $name ?: basename($path), $contentType);
79 $p->handle = $handle;
80
81 return $p;
82 }
83
84 /**
85 * @return $this
86 */
87 public function asInline()
88 {
89 return $this->setDisposition('inline');
90 }
91
92 public function getContentId(): string
93 {
94 return $this->cid ?: $this->cid = $this->generateContentId();
95 }
96
97 public function hasContentId(): bool
98 {
99 return null !== $this->cid;
100 }
101
102 public function getMediaType(): string
103 {
104 return $this->mediaType;
105 }
106
107 public function getPreparedHeaders(): Headers
108 {
109 $headers = parent::getPreparedHeaders();
110
111 if (null !== $this->cid) {
112 $headers->setHeaderBody('Id', 'Content-ID', $this->cid);
113 }
114
115 if (null !== $this->filename) {
116 $headers->setHeaderParameter('Content-Disposition', 'filename', $this->filename);
117 }
118
119 return $headers;
120 }
121
122 public function asDebugString(): string
123 {
124 $str = parent::asDebugString();
125 if (null !== $this->filename) {
126 $str .= ' filename: '.$this->filename;
127 }
128
129 return $str;
130 }
131
132 private function generateContentId(): string
133 {
134 return bin2hex(random_bytes(16)).'@symfony';
135 }
136
137 public function __destruct()
138 {
139 if (null !== $this->handle && \is_resource($this->handle)) {
140 fclose($this->handle);
141 }
142 }
143
144 /**
145 * @return array
146 */
147 public function __sleep()
148 {
149 // converts the body to a string
150 parent::__sleep();
151
152 $this->_parent = [];
153 foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
154 $r = new \ReflectionProperty(TextPart::class, $name);
155 $r->setAccessible(true);
156 $this->_parent[$name] = $r->getValue($this);
157 }
158 $this->_headers = $this->getHeaders();
159
160 return ['_headers', '_parent', 'filename', 'mediaType'];
161 }
162
163 public function __wakeup()
164 {
165 $r = new \ReflectionProperty(AbstractPart::class, 'headers');
166 $r->setAccessible(true);
167 $r->setValue($this, $this->_headers);
168 unset($this->_headers);
169
170 if (!\is_array($this->_parent)) {
171 throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
172 }
173 foreach (['body', 'charset', 'subtype', 'disposition', 'name', 'encoding'] as $name) {
174 if (null !== $this->_parent[$name] && !\is_string($this->_parent[$name])) {
175 throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
176 }
177 $r = new \ReflectionProperty(TextPart::class, $name);
178 $r->setAccessible(true);
179 $r->setValue($this, $this->_parent[$name]);
180 }
181 unset($this->_parent);
182 }
183 }
184