| 1 |
<?php |
| 2 |
|
| 3 |
namespace PhpOffice\PhpSpreadsheet\Reader; |
| 4 |
|
| 5 |
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
| 6 |
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
| 7 |
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
| 8 |
use PhpOffice\PhpSpreadsheet\Shared\File; |
| 9 |
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| 10 |
|
| 11 |
abstract class BaseReader implements IReader |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Read data only? |
| 15 |
* Identifies whether the Reader should only read data values for cells, and ignore any formatting information; |
| 16 |
* or whether it should read both data and formatting. |
| 17 |
* |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
protected $readDataOnly = false; |
| 21 |
|
| 22 |
/** |
| 23 |
* Read empty cells? |
| 24 |
* Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing |
| 25 |
* null value or empty string. |
| 26 |
* |
| 27 |
* @var bool |
| 28 |
*/ |
| 29 |
protected $readEmptyCells = true; |
| 30 |
|
| 31 |
/** |
| 32 |
* Read charts that are defined in the workbook? |
| 33 |
* Identifies whether the Reader should read the definitions for any charts that exist in the workbook;. |
| 34 |
* |
| 35 |
* @var bool |
| 36 |
*/ |
| 37 |
protected $includeCharts = false; |
| 38 |
|
| 39 |
/** |
| 40 |
* Restrict which sheets should be loaded? |
| 41 |
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded. |
| 42 |
* |
| 43 |
* @var null|string[] |
| 44 |
*/ |
| 45 |
protected $loadSheetsOnly; |
| 46 |
|
| 47 |
/** |
| 48 |
* Allow external images. Use with caution. |
| 49 |
* Improper specification of these within a spreadsheet |
| 50 |
* can subject the caller to security exploits. |
| 51 |
* |
| 52 |
* @var bool |
| 53 |
*/ |
| 54 |
protected $allowExternalImages = false; |
| 55 |
|
| 56 |
/** |
| 57 |
* IReadFilter instance. |
| 58 |
* |
| 59 |
* @var IReadFilter |
| 60 |
*/ |
| 61 |
protected $readFilter; |
| 62 |
|
| 63 |
/** @var resource */ |
| 64 |
protected $fileHandle; |
| 65 |
|
| 66 |
/** |
| 67 |
* @var ?XmlScanner |
| 68 |
*/ |
| 69 |
protected $securityScanner; |
| 70 |
|
| 71 |
public function __construct() |
| 72 |
{ |
| 73 |
$this->readFilter = new DefaultReadFilter(); |
| 74 |
} |
| 75 |
|
| 76 |
public function getReadDataOnly() |
| 77 |
{ |
| 78 |
return $this->readDataOnly; |
| 79 |
} |
| 80 |
|
| 81 |
public function setReadDataOnly($readCellValuesOnly) |
| 82 |
{ |
| 83 |
$this->readDataOnly = (bool) $readCellValuesOnly; |
| 84 |
|
| 85 |
return $this; |
| 86 |
} |
| 87 |
|
| 88 |
public function getReadEmptyCells() |
| 89 |
{ |
| 90 |
return $this->readEmptyCells; |
| 91 |
} |
| 92 |
|
| 93 |
public function setReadEmptyCells($readEmptyCells) |
| 94 |
{ |
| 95 |
$this->readEmptyCells = (bool) $readEmptyCells; |
| 96 |
|
| 97 |
return $this; |
| 98 |
} |
| 99 |
|
| 100 |
public function getIncludeCharts() |
| 101 |
{ |
| 102 |
return $this->includeCharts; |
| 103 |
} |
| 104 |
|
| 105 |
public function setIncludeCharts($includeCharts) |
| 106 |
{ |
| 107 |
$this->includeCharts = (bool) $includeCharts; |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
public function getLoadSheetsOnly() |
| 113 |
{ |
| 114 |
return $this->loadSheetsOnly; |
| 115 |
} |
| 116 |
|
| 117 |
public function setLoadSheetsOnly($sheetList) |
| 118 |
{ |
| 119 |
if ($sheetList === null) { |
| 120 |
return $this->setLoadAllSheets(); |
| 121 |
} |
| 122 |
|
| 123 |
$this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList]; |
| 124 |
|
| 125 |
return $this; |
| 126 |
} |
| 127 |
|
| 128 |
public function setLoadAllSheets() |
| 129 |
{ |
| 130 |
$this->loadSheetsOnly = null; |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
public function getReadFilter() |
| 136 |
{ |
| 137 |
return $this->readFilter; |
| 138 |
} |
| 139 |
|
| 140 |
public function setReadFilter(IReadFilter $readFilter) |
| 141 |
{ |
| 142 |
$this->readFilter = $readFilter; |
| 143 |
|
| 144 |
return $this; |
| 145 |
} |
| 146 |
|
| 147 |
public function getSecurityScanner(): ?XmlScanner |
| 148 |
{ |
| 149 |
return $this->securityScanner; |
| 150 |
} |
| 151 |
|
| 152 |
public function getSecurityScannerOrThrow(): XmlScanner |
| 153 |
{ |
| 154 |
if ($this->securityScanner === null) { |
| 155 |
throw new ReaderException('Security scanner is unexpectedly null'); |
| 156 |
} |
| 157 |
|
| 158 |
return $this->securityScanner; |
| 159 |
} |
| 160 |
|
| 161 |
protected function processFlags(int $flags): void |
| 162 |
{ |
| 163 |
if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) { |
| 164 |
$this->setIncludeCharts(true); |
| 165 |
} |
| 166 |
if (((bool) ($flags & self::READ_DATA_ONLY)) === true) { |
| 167 |
$this->setReadDataOnly(true); |
| 168 |
} |
| 169 |
if (((bool) ($flags & self::SKIP_EMPTY_CELLS) || (bool) ($flags & self::IGNORE_EMPTY_CELLS)) === true) { |
| 170 |
$this->setReadEmptyCells(false); |
| 171 |
} |
| 172 |
if (((bool) ($flags & self::ALLOW_EXTERNAL_IMAGES)) === true) { |
| 173 |
$this->setAllowExternalImages(true); |
| 174 |
} |
| 175 |
if (((bool) ($flags & self::DONT_ALLOW_EXTERNAL_IMAGES)) === true) { |
| 176 |
$this->setAllowExternalImages(false); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet |
| 181 |
{ |
| 182 |
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method'); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Loads Spreadsheet from file. |
| 187 |
* |
| 188 |
* @param int $flags the optional second parameter flags may be used to identify specific elements |
| 189 |
* that should be loaded, but which won't be loaded by default, using these values: |
| 190 |
* IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file |
| 191 |
*/ |
| 192 |
public function load(string $filename, int $flags = 0): Spreadsheet |
| 193 |
{ |
| 194 |
$this->processFlags($flags); |
| 195 |
|
| 196 |
try { |
| 197 |
return $this->loadSpreadsheetFromFile($filename); |
| 198 |
} catch (ReaderException $e) { |
| 199 |
throw $e; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Open file for reading. |
| 205 |
*/ |
| 206 |
protected function openFile(string $filename): void |
| 207 |
{ |
| 208 |
$fileHandle = false; |
| 209 |
if ($filename) { |
| 210 |
File::assertFile($filename); |
| 211 |
|
| 212 |
// Open file |
| 213 |
$fileHandle = fopen($filename, 'rb'); |
| 214 |
} |
| 215 |
if ($fileHandle === false) { |
| 216 |
throw new ReaderException('Could not open file ' . $filename . ' for reading.'); |
| 217 |
} |
| 218 |
|
| 219 |
$this->fileHandle = $fileHandle; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Allow external images. Use with caution. |
| 224 |
* Improper specification of these within a spreadsheet |
| 225 |
* can subject the caller to security exploits. |
| 226 |
*/ |
| 227 |
public function setAllowExternalImages(bool $allowExternalImages) |
| 228 |
{ |
| 229 |
$this->allowExternalImages = $allowExternalImages; |
| 230 |
|
| 231 |
return $this; |
| 232 |
} |
| 233 |
|
| 234 |
public function getAllowExternalImages() |
| 235 |
{ |
| 236 |
return $this->allowExternalImages; |
| 237 |
} |
| 238 |
} |
| 239 |
|