| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\IValueBinder; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
| 9 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\File; |
| 10 |
use TablePress\PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 11 |
|
| 12 |
abstract class BaseReader implements IReader2 |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Read data only? |
| 16 |
* Identifies whether the Reader should only read data values for cells, and ignore any formatting information; |
| 17 |
* or whether it should read both data and formatting. |
| 18 |
*/ |
| 19 |
protected bool $readDataOnly = false; |
| 20 |
|
| 21 |
/** |
| 22 |
* Read empty cells? |
| 23 |
* Identifies whether the Reader should read data values for all cells, or should ignore cells containing |
| 24 |
* null value or empty string. |
| 25 |
*/ |
| 26 |
protected bool $readEmptyCells = true; |
| 27 |
|
| 28 |
/** |
| 29 |
* Read charts that are defined in the workbook? |
| 30 |
* Identifies whether the Reader should read the definitions for any charts that exist in the workbook;. |
| 31 |
*/ |
| 32 |
protected bool $includeCharts = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* Restrict which sheets should be loaded? |
| 36 |
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded. |
| 37 |
* This property is ignored for Csv, Html, and Slk. |
| 38 |
* |
| 39 |
* @var null|string[] |
| 40 |
*/ |
| 41 |
protected ?array $loadSheetsOnly = null; |
| 42 |
|
| 43 |
/** |
| 44 |
* Ignore rows with no cells? |
| 45 |
* Identifies whether the Reader should ignore rows with no cells. |
| 46 |
* Currently implemented only for Xlsx. |
| 47 |
*/ |
| 48 |
protected bool $ignoreRowsWithNoCells = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* Allow external images. Use with caution. |
| 52 |
* Improper specification of these within a spreadsheet |
| 53 |
* can subject the caller to security exploits. |
| 54 |
*/ |
| 55 |
protected bool $allowExternalImages = false; |
| 56 |
|
| 57 |
/** |
| 58 |
* Create a blank sheet if none are read, |
| 59 |
* possibly due to a typo when using LoadSheetsOnly. |
| 60 |
*/ |
| 61 |
protected bool $createBlankSheetIfNoneRead = false; |
| 62 |
|
| 63 |
/** |
| 64 |
* Enable drawing pass-through? |
| 65 |
* Identifies whether the Reader should preserve unsupported drawing elements (shapes, grouped images, etc.) |
| 66 |
* by storing the original XML for pass-through during write operations. |
| 67 |
* When enabled, drawings cannot be modified programmatically but are preserved exactly. |
| 68 |
*/ |
| 69 |
protected bool $enableDrawingPassThrough = false; |
| 70 |
|
| 71 |
/** |
| 72 |
* IReadFilter instance. |
| 73 |
*/ |
| 74 |
protected IReadFilter $readFilter; |
| 75 |
|
| 76 |
/** @var resource */ |
| 77 |
protected $fileHandle; |
| 78 |
|
| 79 |
protected ?XmlScanner $securityScanner = null; |
| 80 |
|
| 81 |
protected ?IValueBinder $valueBinder = null; |
| 82 |
|
| 83 |
/** @var null|callable(string):bool function to return whether image path is okay */ |
| 84 |
protected $isWhitelisted; |
| 85 |
|
| 86 |
public function __construct() |
| 87 |
{ |
| 88 |
$this->readFilter = new DefaultReadFilter(); |
| 89 |
} |
| 90 |
|
| 91 |
public function getReadDataOnly(): bool |
| 92 |
{ |
| 93 |
return $this->readDataOnly; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return static |
| 98 |
*/ |
| 99 |
public function setReadDataOnly(bool $readCellValuesOnly) |
| 100 |
{ |
| 101 |
$this->readDataOnly = $readCellValuesOnly; |
| 102 |
|
| 103 |
return $this; |
| 104 |
} |
| 105 |
|
| 106 |
public function getReadEmptyCells(): bool |
| 107 |
{ |
| 108 |
return $this->readEmptyCells; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @return static |
| 113 |
*/ |
| 114 |
public function setReadEmptyCells(bool $readEmptyCells) |
| 115 |
{ |
| 116 |
$this->readEmptyCells = $readEmptyCells; |
| 117 |
|
| 118 |
return $this; |
| 119 |
} |
| 120 |
|
| 121 |
public function getIgnoreRowsWithNoCells(): bool |
| 122 |
{ |
| 123 |
return $this->ignoreRowsWithNoCells; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @return static |
| 128 |
*/ |
| 129 |
public function setIgnoreRowsWithNoCells(bool $ignoreRowsWithNoCells) |
| 130 |
{ |
| 131 |
$this->ignoreRowsWithNoCells = $ignoreRowsWithNoCells; |
| 132 |
|
| 133 |
return $this; |
| 134 |
} |
| 135 |
|
| 136 |
public function getIncludeCharts(): bool |
| 137 |
{ |
| 138 |
return $this->includeCharts; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @return static |
| 143 |
*/ |
| 144 |
public function setIncludeCharts(bool $includeCharts) |
| 145 |
{ |
| 146 |
$this->includeCharts = $includeCharts; |
| 147 |
|
| 148 |
return $this; |
| 149 |
} |
| 150 |
|
| 151 |
public function getEnableDrawingPassThrough(): bool |
| 152 |
{ |
| 153 |
return $this->enableDrawingPassThrough; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* @return static |
| 158 |
*/ |
| 159 |
public function setEnableDrawingPassThrough(bool $enableDrawingPassThrough) |
| 160 |
{ |
| 161 |
$this->enableDrawingPassThrough = $enableDrawingPassThrough; |
| 162 |
|
| 163 |
return $this; |
| 164 |
} |
| 165 |
|
| 166 |
/** @return null|string[] */ |
| 167 |
public function getLoadSheetsOnly(): ?array |
| 168 |
{ |
| 169 |
return $this->loadSheetsOnly; |
| 170 |
} |
| 171 |
|
| 172 |
/** @param null|string|string[] $sheetList |
| 173 |
* @return static */ |
| 174 |
public function setLoadSheetsOnly($sheetList) |
| 175 |
{ |
| 176 |
if ($sheetList === null) { |
| 177 |
return $this->setLoadAllSheets(); |
| 178 |
} |
| 179 |
|
| 180 |
$this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList]; |
| 181 |
|
| 182 |
return $this; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @return static |
| 187 |
*/ |
| 188 |
public function setLoadAllSheets() |
| 189 |
{ |
| 190 |
$this->loadSheetsOnly = null; |
| 191 |
|
| 192 |
return $this; |
| 193 |
} |
| 194 |
|
| 195 |
public function getReadFilter(): IReadFilter |
| 196 |
{ |
| 197 |
return $this->readFilter; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* @return static |
| 202 |
*/ |
| 203 |
public function setReadFilter(IReadFilter $readFilter) |
| 204 |
{ |
| 205 |
$this->readFilter = $readFilter; |
| 206 |
|
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* USE WITH CAUTION (and in conjunction with setIsWhiteListed)! |
| 212 |
* Allow external images; |
| 213 |
* these can be specified within a spreadsheet |
| 214 |
* in a way that can subject the caller to security exploits. |
| 215 |
* @return static |
| 216 |
*/ |
| 217 |
public function setAllowExternalImages(bool $allowExternalImages) |
| 218 |
{ |
| 219 |
$this->allowExternalImages = $allowExternalImages; |
| 220 |
|
| 221 |
return $this; |
| 222 |
} |
| 223 |
|
| 224 |
public function getAllowExternalImages(): bool |
| 225 |
{ |
| 226 |
return $this->allowExternalImages; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* USE WITH CAUTION! |
| 231 |
* Supply a callback to determine whether a path should be whitelisted, |
| 232 |
* used in conjunction with setAllowExternalImages; |
| 233 |
* supplying a method which might return true |
| 234 |
* can subject the caller to security exploits. |
| 235 |
* |
| 236 |
* @param callable(string):bool $isWhitelisted |
| 237 |
* @return static |
| 238 |
*/ |
| 239 |
public function setIsWhitelisted(callable $isWhitelisted) |
| 240 |
{ |
| 241 |
$this->isWhitelisted = $isWhitelisted; |
| 242 |
|
| 243 |
return $this; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Create a blank sheet if none are read, |
| 248 |
* possibly due to a typo when using LoadSheetsOnly. |
| 249 |
* @return static |
| 250 |
*/ |
| 251 |
public function setCreateBlankSheetIfNoneRead(bool $createBlankSheetIfNoneRead) |
| 252 |
{ |
| 253 |
$this->createBlankSheetIfNoneRead = $createBlankSheetIfNoneRead; |
| 254 |
|
| 255 |
return $this; |
| 256 |
} |
| 257 |
|
| 258 |
public function getSecurityScanner(): ?XmlScanner |
| 259 |
{ |
| 260 |
return $this->securityScanner; |
| 261 |
} |
| 262 |
|
| 263 |
public function getSecurityScannerOrThrow(): XmlScanner |
| 264 |
{ |
| 265 |
if ($this->securityScanner === null) { |
| 266 |
throw new ReaderException('Security scanner is unexpectedly null'); |
| 267 |
} |
| 268 |
|
| 269 |
return $this->securityScanner; |
| 270 |
} |
| 271 |
|
| 272 |
protected function processFlags(int $flags): void |
| 273 |
{ |
| 274 |
if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) { |
| 275 |
$this->setIncludeCharts(true); |
| 276 |
} |
| 277 |
if (((bool) ($flags & self::READ_DATA_ONLY)) === true) { |
| 278 |
$this->setReadDataOnly(true); |
| 279 |
} |
| 280 |
if (((bool) ($flags & self::IGNORE_EMPTY_CELLS)) === true) { |
| 281 |
$this->setReadEmptyCells(false); |
| 282 |
} |
| 283 |
if (((bool) ($flags & self::IGNORE_ROWS_WITH_NO_CELLS)) === true) { |
| 284 |
$this->setIgnoreRowsWithNoCells(true); |
| 285 |
} |
| 286 |
if (((bool) ($flags & self::ALLOW_EXTERNAL_IMAGES)) === true) { |
| 287 |
$this->setAllowExternalImages(true); |
| 288 |
} |
| 289 |
if (((bool) ($flags & self::DONT_ALLOW_EXTERNAL_IMAGES)) === true) { |
| 290 |
$this->setAllowExternalImages(false); |
| 291 |
} |
| 292 |
if (((bool) ($flags & self::CREATE_BLANK_SHEET_IF_NONE_READ)) === true) { |
| 293 |
$this->setCreateBlankSheetIfNoneRead(true); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet |
| 298 |
{ |
| 299 |
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method'); |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Loads Spreadsheet from file. |
| 304 |
* |
| 305 |
* @param int $flags the optional second parameter flags may be used to identify specific elements |
| 306 |
* that should be loaded, but which won't be loaded by default, using these values: |
| 307 |
* IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file |
| 308 |
*/ |
| 309 |
public function load(string $filename, int $flags = 0): Spreadsheet |
| 310 |
{ |
| 311 |
$this->processFlags($flags); |
| 312 |
|
| 313 |
try { |
| 314 |
return $this->loadSpreadsheetFromFile($filename); |
| 315 |
} catch (ReaderException $e) { |
| 316 |
throw $e; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Open file for reading. |
| 322 |
*/ |
| 323 |
protected function openFile(string $filename): void |
| 324 |
{ |
| 325 |
$fileHandle = false; |
| 326 |
if ($filename) { |
| 327 |
File::assertFile($filename); |
| 328 |
|
| 329 |
// Open file |
| 330 |
$fileHandle = fopen($filename, 'rb'); |
| 331 |
} |
| 332 |
if ($fileHandle === false) { |
| 333 |
throw new ReaderException('Could not open file ' . $filename . ' for reading.'); |
| 334 |
} |
| 335 |
|
| 336 |
$this->fileHandle = $fileHandle; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
| 341 |
* |
| 342 |
* @return array<int, array{worksheetName: string, lastColumnLetter: string, lastColumnIndex: int, totalRows: int, totalColumns: int, sheetState: string}> |
| 343 |
*/ |
| 344 |
public function listWorksheetInfo(string $filename): array |
| 345 |
{ |
| 346 |
throw new PhpSpreadsheetException('Reader classes must implement their own listWorksheetInfo() method'); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Returns names of the worksheets from a file, |
| 351 |
* possibly without parsing the whole file to a Spreadsheet object. |
| 352 |
* Readers will often have a more efficient method with which |
| 353 |
* they can override this method. |
| 354 |
* |
| 355 |
* @return string[] |
| 356 |
*/ |
| 357 |
public function listWorksheetNames(string $filename): array |
| 358 |
{ |
| 359 |
$returnArray = []; |
| 360 |
$info = $this->listWorksheetInfo($filename); |
| 361 |
foreach ($info as $infoArray) { |
| 362 |
$returnArray[] = $infoArray['worksheetName']; |
| 363 |
} |
| 364 |
|
| 365 |
return $returnArray; |
| 366 |
} |
| 367 |
|
| 368 |
public function getValueBinder(): ?IValueBinder |
| 369 |
{ |
| 370 |
return $this->valueBinder; |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* @return static |
| 375 |
*/ |
| 376 |
public function setValueBinder(?IValueBinder $valueBinder) |
| 377 |
{ |
| 378 |
$this->valueBinder = $valueBinder; |
| 379 |
|
| 380 |
return $this; |
| 381 |
} |
| 382 |
|
| 383 |
protected function newSpreadsheet(): Spreadsheet |
| 384 |
{ |
| 385 |
return new Spreadsheet(); |
| 386 |
} |
| 387 |
} |
| 388 |
|