PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / libraries / vendor / PhpSpreadsheet / Worksheet / Drawing.php

Drawing.php in TablePress – Tables in WordPress made easy 3.4, at libraries/vendor/PhpSpreadsheet/Worksheet/Drawing.php

263 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TablePress\PhpOffice\PhpSpreadsheet\Worksheet;
4
5 use TablePress\Composer\Pcre\Preg;
6 use TablePress\PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
7 use ZipArchive;
8
9 class Drawing extends BaseDrawing
10 {
11 const IMAGE_TYPES_CONVERTION_MAP = [
12 IMAGETYPE_GIF => IMAGETYPE_PNG,
13 IMAGETYPE_JPEG => IMAGETYPE_JPEG,
14 IMAGETYPE_PNG => IMAGETYPE_PNG,
15 IMAGETYPE_BMP => IMAGETYPE_PNG,
16 ];
17
18 /**
19 * Path.
20 */
21 private string $path;
22
23 /**
24 * Whether or not we are dealing with a URL.
25 */
26 private bool $isUrl;
27
28 /**
29 * Create a new Drawing.
30 */
31 public function __construct()
32 {
33 // Initialise values
34 $this->path = '';
35 $this->isUrl = false;
36
37 // Initialize parent
38 parent::__construct();
39 }
40
41 /**
42 * Get Filename.
43 */
44 public function getFilename(): string
45 {
46 return basename($this->path);
47 }
48
49 /**
50 * Get indexed filename (using image index).
51 */
52 public function getIndexedFilename(): string
53 {
54 return md5($this->path) . '.' . $this->getExtension();
55 }
56
57 /**
58 * Get Extension.
59 */
60 public function getExtension(): string
61 {
62 if (Preg::isMatch('~^data:image/([^;]+);base64,~', $this->path, $matches)) {
63 return $matches[1];
64 }
65 $exploded = explode('.', basename($this->path));
66
67 return $exploded[count($exploded) - 1];
68 }
69
70 /**
71 * Get full filepath to store drawing in zip archive.
72 */
73 public function getMediaFilename(): string
74 {
75 if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
76 throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
77 }
78
79 return sprintf('image%d%s', $this->getImageIndex(), $this->getImageFileExtensionForSave());
80 }
81
82 /**
83 * Get Path.
84 */
85 public function getPath(): string
86 {
87 return $this->path;
88 }
89
90 /**
91 * Set Path.
92 *
93 * @param string $path File path
94 * @param bool $verifyFile Verify file
95 * @param ?ZipArchive $zip Zip archive instance
96 * @param null|callable(string):bool $isWhitelisted
97 *
98 * @return $this
99 */
100 public function setPath(string $path, bool $verifyFile = true, ?ZipArchive $zip = null, bool $allowExternal = true, ?callable $isWhitelisted = null)
101 {
102 $this->isUrl = false;
103 if (Preg::isMatch('~^data:image/[a-z]+;base64,~', $path)) {
104 $this->path = $path;
105
106 return $this;
107 }
108
109 $this->path = '';
110 if ($zip instanceof ZipArchive) {
111 $zipPath = explode('#', $path)[1];
112 $locate = @$zip->locateName($zipPath);
113 if ($locate !== false) {
114 if ($this->isImage($path)) {
115 $this->path = $path;
116 $this->setSizesAndType($path);
117 }
118 }
119 // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979
120 } elseif (
121 filter_var($path, FILTER_VALIDATE_URL)
122 || Preg::isMatch('~^phar://~i', $path)
123 || (Preg::isMatch('/^([\w.\s\x00-\x1f]+):/', $path) && !Preg::isMatch('/^([\w.]+):/', $path))
124 ) {
125 if (!Preg::isMatch('/^(http|https|file|ftp|s3):/', $path)) {
126 throw new PhpSpreadsheetException('Invalid protocol for linked drawing');
127 }
128 if (!$allowExternal) {
129 return $this;
130 }
131 if ($isWhitelisted !== null && !$isWhitelisted($path)) {
132 return $this;
133 }
134 // Implicit that it is a URL, rather store info than running check above on value in other places.
135 $this->isUrl = true;
136 $ctx = null;
137 // https://github.com/php/php-src/issues/16023
138 // https://github.com/php/php-src/issues/17121
139 if (str_starts_with($path, 'https:') || str_starts_with($path, 'http:')) {
140 $ctxArray = [
141 'http' => [
142 'follow_location' => 0,
143 'user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
144 'header' => [
145 //'Connection: keep-alive', // unacceptable performance
146 'Accept: image/*;q=0.9,*/*;q=0.8',
147 ],
148 ],
149 ];
150 if (str_starts_with($path, 'https:')) {
151 $ctxArray['ssl'] = ['crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT];
152 }
153 $ctx = stream_context_create($ctxArray);
154 }
155 $imageContents = @file_get_contents($path, false, $ctx);
156 if ($imageContents !== false) {
157 $filePath = tempnam(sys_get_temp_dir(), 'Drawing');
158 if ($filePath) {
159 $put = @file_put_contents($filePath, $imageContents);
160 if ($put !== false) {
161 if ($this->isImage($filePath)) {
162 $this->path = $path;
163 $this->setSizesAndType($filePath);
164 }
165 unlink($filePath);
166 }
167 }
168 }
169 } else {
170 $exists = @file_exists($path);
171 if ($exists !== false && $this->isImage($path)) {
172 $this->path = $path;
173 $this->setSizesAndType($path);
174 }
175 }
176 if ($this->path === '' && $verifyFile) {
177 throw new PhpSpreadsheetException("File $path not found!");
178 }
179
180 if ($this->worksheet !== null) {
181 if ($this->path !== '') {
182 $this->worksheet->getCell($this->coordinates);
183 }
184 }
185
186 return $this;
187 }
188
189 private function isImage(string $path): bool
190 {
191 $mime = (string) @mime_content_type($path);
192 $retVal = false;
193 if (str_starts_with($mime, 'image/')) {
194 $retVal = true;
195 } elseif ($mime === 'application/octet-stream') {
196 $extension = pathinfo($path, PATHINFO_EXTENSION);
197 $retVal = in_array($extension, ['bin', 'emf'], true);
198 }
199
200 return $retVal;
201 }
202
203 /**
204 * Get isURL.
205 */
206 public function getIsURL(): bool
207 {
208 return $this->isUrl;
209 }
210
211 /**
212 * Get hash code.
213 *
214 * @return string Hash code
215 */
216 public function getHashCode(): string
217 {
218 return md5(
219 $this->path
220 . parent::getHashCode()
221 . __CLASS__
222 );
223 }
224
225 /**
226 * Get Image Type for Save.
227 */
228 public function getImageTypeForSave(): int
229 {
230 if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
231 throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
232 }
233
234 return self::IMAGE_TYPES_CONVERTION_MAP[$this->type];
235 }
236
237 /**
238 * Get Image file extension for Save.
239 */
240 public function getImageFileExtensionForSave(bool $includeDot = true): string
241 {
242 if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
243 throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
244 }
245
246 $result = image_type_to_extension(self::IMAGE_TYPES_CONVERTION_MAP[$this->type], $includeDot);
247
248 return "$result";
249 }
250
251 /**
252 * Get Image mime type.
253 */
254 public function getImageMimeType(): string
255 {
256 if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) {
257 throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.');
258 }
259
260 return image_type_to_mime_type(self::IMAGE_TYPES_CONVERTION_MAP[$this->type]);
261 }
262 }
263