Abstract.php 8.0 KB

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