| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Worksheet; |
| 4 |
|
| 5 |
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
| 6 |
use ZipArchive; |
| 7 |
|
| 8 |
class Drawing extends BaseDrawing |
| 9 |
{ |
| 10 |
const IMAGE_TYPES_CONVERTION_MAP = [ |
| 11 |
IMAGETYPE_GIF => IMAGETYPE_PNG, |
| 12 |
IMAGETYPE_JPEG => IMAGETYPE_JPEG, |
| 13 |
IMAGETYPE_PNG => IMAGETYPE_PNG, |
| 14 |
IMAGETYPE_BMP => IMAGETYPE_PNG, |
| 15 |
]; |
| 16 |
|
| 17 |
/** |
| 18 |
* Path. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $path; |
| 23 |
|
| 24 |
/** |
| 25 |
* Whether or not we are dealing with a URL. |
| 26 |
* |
| 27 |
* @var bool |
| 28 |
*/ |
| 29 |
private $isUrl; |
| 30 |
|
| 31 |
/** |
| 32 |
* Create a new Drawing. |
| 33 |
*/ |
| 34 |
public function __construct() |
| 35 |
{ |
| 36 |
// Initialise values |
| 37 |
$this->path = ''; |
| 38 |
$this->isUrl = false; |
| 39 |
|
| 40 |
// Initialize parent |
| 41 |
parent::__construct(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get Filename. |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public function getFilename() |
| 50 |
{ |
| 51 |
return basename($this->path); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get indexed filename (using image index). |
| 56 |
*/ |
| 57 |
public function getIndexedFilename(): string |
| 58 |
{ |
| 59 |
return md5($this->path) . '.' . $this->getExtension(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get Extension. |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function getExtension() |
| 68 |
{ |
| 69 |
$exploded = explode('.', basename($this->path)); |
| 70 |
|
| 71 |
return $exploded[count($exploded) - 1]; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get full filepath to store drawing in zip archive. |
| 76 |
* |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
public function getMediaFilename() |
| 80 |
{ |
| 81 |
if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) { |
| 82 |
throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.'); |
| 83 |
} |
| 84 |
|
| 85 |
return sprintf('image%d%s', $this->getImageIndex(), $this->getImageFileExtensionForSave()); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get Path. |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
public function getPath() |
| 94 |
{ |
| 95 |
return $this->path; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Set Path. |
| 100 |
* |
| 101 |
* @param string $path File path |
| 102 |
* @param bool $verifyFile Verify file |
| 103 |
* @param ZipArchive $zip Zip archive instance |
| 104 |
* @param bool $allowExternal |
| 105 |
* |
| 106 |
* @return $this |
| 107 |
*/ |
| 108 |
public function setPath($path, $verifyFile = true, $zip = null, $allowExternal = true) |
| 109 |
{ |
| 110 |
$this->isUrl = false; |
| 111 |
if (preg_match('~^data:image/[a-z]+;base64,~', $path) === 1) { |
| 112 |
$this->path = $path; |
| 113 |
|
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
$this->path = ''; |
| 118 |
// Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979 |
| 119 |
if (filter_var($path, FILTER_VALIDATE_URL) || (preg_match('/^([\w\s\x00-\x1f]+):/u', $path) && !preg_match('/^([\w]+):/u', $path))) { |
| 120 |
if (!preg_match('/^(http|https|file|ftp|s3):/', $path)) { |
| 121 |
throw new PhpSpreadsheetException('Invalid protocol for linked drawing'); |
| 122 |
} |
| 123 |
if (!$allowExternal) { |
| 124 |
return $this; |
| 125 |
} |
| 126 |
// Implicit that it is a URL, rather store info than running check above on value in other places. |
| 127 |
$this->isUrl = true; |
| 128 |
$ctx = null; |
| 129 |
// https://github.com/php/php-src/issues/16023 |
| 130 |
// https://github.com/php/php-src/issues/17121 |
| 131 |
if (preg_match('/^https?:/', $path) === 1) { |
| 132 |
$ctxArray = [ |
| 133 |
'http' => [ |
| 134 |
'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', |
| 135 |
'header' => [ |
| 136 |
//'Connection: keep-alive', // unacceptable performance |
| 137 |
'Accept: image/*;q=0.9,*/*;q=0.8', |
| 138 |
], |
| 139 |
], |
| 140 |
]; |
| 141 |
if (preg_match('/^https:/', $path) === 1) { |
| 142 |
$ctxArray['ssl'] = ['crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT]; |
| 143 |
} |
| 144 |
$ctx = stream_context_create($ctxArray); |
| 145 |
} |
| 146 |
$imageContents = @file_get_contents($path, false, $ctx); |
| 147 |
if ($imageContents !== false) { |
| 148 |
$filePath = tempnam(sys_get_temp_dir(), 'Drawing'); |
| 149 |
if ($filePath) { |
| 150 |
$put = @file_put_contents($filePath, $imageContents); |
| 151 |
if ($put !== false) { |
| 152 |
if ($this->isImage($filePath)) { |
| 153 |
$this->path = $path; |
| 154 |
$this->setSizesAndType($filePath); |
| 155 |
} |
| 156 |
unlink($filePath); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} elseif ($zip instanceof ZipArchive) { |
| 161 |
$zipPath = explode('#', $path)[1]; |
| 162 |
$locate = @$zip->locateName($zipPath); |
| 163 |
if ($locate !== false) { |
| 164 |
if ($this->isImage($path)) { |
| 165 |
$this->path = $path; |
| 166 |
$this->setSizesAndType($path); |
| 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 (strpos($mime, 'image/') === 0) { |
| 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 |
* Set isURL. |
| 213 |
* |
| 214 |
* @return $this |
| 215 |
* |
| 216 |
* @deprecated 3.7.0 not needed, property is set by setPath |
| 217 |
*/ |
| 218 |
public function setIsURL(bool $isUrl): self |
| 219 |
{ |
| 220 |
$this->isUrl = $isUrl; |
| 221 |
|
| 222 |
return $this; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Get hash code. |
| 227 |
* |
| 228 |
* @return string Hash code |
| 229 |
*/ |
| 230 |
public function getHashCode() |
| 231 |
{ |
| 232 |
return md5( |
| 233 |
$this->path . |
| 234 |
parent::getHashCode() . |
| 235 |
__CLASS__ |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get Image Type for Save. |
| 241 |
*/ |
| 242 |
public function getImageTypeForSave(): int |
| 243 |
{ |
| 244 |
if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) { |
| 245 |
throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.'); |
| 246 |
} |
| 247 |
|
| 248 |
return self::IMAGE_TYPES_CONVERTION_MAP[$this->type]; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get Image file extention for Save. |
| 253 |
*/ |
| 254 |
public function getImageFileExtensionForSave(bool $includeDot = true): 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 |
$result = image_type_to_extension(self::IMAGE_TYPES_CONVERTION_MAP[$this->type], $includeDot); |
| 261 |
|
| 262 |
return "$result"; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Get Image mime type. |
| 267 |
*/ |
| 268 |
public function getImageMimeType(): string |
| 269 |
{ |
| 270 |
if (!array_key_exists($this->type, self::IMAGE_TYPES_CONVERTION_MAP)) { |
| 271 |
throw new PhpSpreadsheetException('Unsupported image type in comment background. Supported types: PNG, JPEG, BMP, GIF.'); |
| 272 |
} |
| 273 |
|
| 274 |
return image_type_to_mime_type(self::IMAGE_TYPES_CONVERTION_MAP[$this->type]); |
| 275 |
} |
| 276 |
} |
| 277 |
|