BaseReader.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Reader;
  3. use PhpOffice\PhpSpreadsheet\Shared\File;
  4. abstract class BaseReader implements IReader
  5. {
  6. /**
  7. * Read data only?
  8. * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  9. * or whether it should read both data and formatting.
  10. *
  11. * @var bool
  12. */
  13. protected $readDataOnly = false;
  14. /**
  15. * Read empty cells?
  16. * Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
  17. * null value or empty string.
  18. *
  19. * @var bool
  20. */
  21. protected $readEmptyCells = true;
  22. /**
  23. * Read charts that are defined in the workbook?
  24. * Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
  25. *
  26. * @var bool
  27. */
  28. protected $includeCharts = false;
  29. /**
  30. * Restrict which sheets should be loaded?
  31. * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  32. *
  33. * @var array of string
  34. */
  35. protected $loadSheetsOnly;
  36. /**
  37. * IReadFilter instance.
  38. *
  39. * @var IReadFilter
  40. */
  41. protected $readFilter;
  42. protected $fileHandle;
  43. /**
  44. * Read data only?
  45. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
  46. * If false (the default) it will read data and formatting.
  47. *
  48. * @return bool
  49. */
  50. public function getReadDataOnly()
  51. {
  52. return $this->readDataOnly;
  53. }
  54. /**
  55. * Set read data only
  56. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
  57. * Set to false (the default) to advise the Reader to read both data and formatting for cells.
  58. *
  59. * @param bool $pValue
  60. *
  61. * @return IReader
  62. */
  63. public function setReadDataOnly($pValue)
  64. {
  65. $this->readDataOnly = (bool) $pValue;
  66. return $this;
  67. }
  68. /**
  69. * Read empty cells?
  70. * If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
  71. * If false it will not read data for cells containing a null value or an empty string.
  72. *
  73. * @return bool
  74. */
  75. public function getReadEmptyCells()
  76. {
  77. return $this->readEmptyCells;
  78. }
  79. /**
  80. * Set read empty cells
  81. * Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
  82. * Set to false to advise the Reader to ignore cells containing a null value or an empty string.
  83. *
  84. * @param bool $pValue
  85. *
  86. * @return IReader
  87. */
  88. public function setReadEmptyCells($pValue)
  89. {
  90. $this->readEmptyCells = (bool) $pValue;
  91. return $this;
  92. }
  93. /**
  94. * Read charts in workbook?
  95. * If this is true, then the Reader will include any charts that exist in the workbook.
  96. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  97. * If false (the default) it will ignore any charts defined in the workbook file.
  98. *
  99. * @return bool
  100. */
  101. public function getIncludeCharts()
  102. {
  103. return $this->includeCharts;
  104. }
  105. /**
  106. * Set read charts in workbook
  107. * Set to true, to advise the Reader to include any charts that exist in the workbook.
  108. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  109. * Set to false (the default) to discard charts.
  110. *
  111. * @param bool $pValue
  112. *
  113. * @return IReader
  114. */
  115. public function setIncludeCharts($pValue)
  116. {
  117. $this->includeCharts = (bool) $pValue;
  118. return $this;
  119. }
  120. /**
  121. * Get which sheets to load
  122. * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
  123. * indicating that all worksheets in the workbook should be loaded.
  124. *
  125. * @return mixed
  126. */
  127. public function getLoadSheetsOnly()
  128. {
  129. return $this->loadSheetsOnly;
  130. }
  131. /**
  132. * Set which sheets to load.
  133. *
  134. * @param mixed $value
  135. * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
  136. * If NULL, then it tells the Reader to read all worksheets in the workbook
  137. *
  138. * @return IReader
  139. */
  140. public function setLoadSheetsOnly($value)
  141. {
  142. if ($value === null) {
  143. return $this->setLoadAllSheets();
  144. }
  145. $this->loadSheetsOnly = is_array($value) ? $value : [$value];
  146. return $this;
  147. }
  148. /**
  149. * Set all sheets to load
  150. * Tells the Reader to load all worksheets from the workbook.
  151. *
  152. * @return IReader
  153. */
  154. public function setLoadAllSheets()
  155. {
  156. $this->loadSheetsOnly = null;
  157. return $this;
  158. }
  159. /**
  160. * Read filter.
  161. *
  162. * @return IReadFilter
  163. */
  164. public function getReadFilter()
  165. {
  166. return $this->readFilter;
  167. }
  168. /**
  169. * Set read filter.
  170. *
  171. * @param IReadFilter $pValue
  172. *
  173. * @return IReader
  174. */
  175. public function setReadFilter(IReadFilter $pValue)
  176. {
  177. $this->readFilter = $pValue;
  178. return $this;
  179. }
  180. /**
  181. * Open file for reading.
  182. *
  183. * @param string $pFilename
  184. *
  185. * @throws Exception
  186. */
  187. protected function openFile($pFilename)
  188. {
  189. File::assertFile($pFilename);
  190. // Open file
  191. $this->fileHandle = fopen($pFilename, 'r');
  192. if ($this->fileHandle === false) {
  193. throw new Exception('Could not open file ' . $pFilename . ' for reading.');
  194. }
  195. }
  196. /**
  197. * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks.
  198. *
  199. * @param string $xml
  200. *
  201. * @throws Exception
  202. *
  203. * @return string
  204. */
  205. public function securityScan($xml)
  206. {
  207. $pattern = '/\\0?' . implode('\\0?', str_split('<!DOCTYPE')) . '\\0?/';
  208. if (preg_match($pattern, $xml)) {
  209. throw new Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
  210. }
  211. return $xml;
  212. }
  213. /**
  214. * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks.
  215. *
  216. * @param string $filestream
  217. *
  218. * @throws Exception
  219. *
  220. * @return string
  221. */
  222. public function securityScanFile($filestream)
  223. {
  224. return $this->securityScan(file_get_contents($filestream));
  225. }
  226. }