Abstract.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * PHPExcel_Reader_Abstract
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Reader
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. abstract class PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  28. {
  29. /**
  30. * Read data only?
  31. * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  32. * or whether it should read both data and formatting
  33. *
  34. * @var boolean
  35. */
  36. protected $readDataOnly = false;
  37. /**
  38. * Read empty cells?
  39. * Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
  40. * null value or empty string
  41. *
  42. * @var boolean
  43. */
  44. protected $readEmptyCells = true;
  45. /**
  46. * Read charts that are defined in the workbook?
  47. * Identifies whether the Reader should read the definitions for any charts that exist in the workbook;
  48. *
  49. * @var boolean
  50. */
  51. protected $includeCharts = false;
  52. /**
  53. * Restrict which sheets should be loaded?
  54. * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  55. *
  56. * @var array of string
  57. */
  58. protected $loadSheetsOnly;
  59. /**
  60. * PHPExcel_Reader_IReadFilter instance
  61. *
  62. * @var PHPExcel_Reader_IReadFilter
  63. */
  64. protected $readFilter;
  65. protected $fileHandle = null;
  66. /**
  67. * Read data only?
  68. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
  69. * If false (the default) it will read data and formatting.
  70. *
  71. * @return boolean
  72. */
  73. public function getReadDataOnly()
  74. {
  75. return $this->readDataOnly;
  76. }
  77. /**
  78. * Set read data only
  79. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
  80. * Set to false (the default) to advise the Reader to read both data and formatting for cells.
  81. *
  82. * @param boolean $pValue
  83. *
  84. * @return PHPExcel_Reader_IReader
  85. */
  86. public function setReadDataOnly($pValue = false)
  87. {
  88. $this->readDataOnly = $pValue;
  89. return $this;
  90. }
  91. /**
  92. * Read empty cells?
  93. * If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
  94. * If false it will not read data for cells containing a null value or an empty string.
  95. *
  96. * @return boolean
  97. */
  98. public function getReadEmptyCells()
  99. {
  100. return $this->readEmptyCells;
  101. }
  102. /**
  103. * Set read empty cells
  104. * Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
  105. * Set to false to advise the Reader to ignore cells containing a null value or an empty string.
  106. *
  107. * @param boolean $pValue
  108. *
  109. * @return PHPExcel_Reader_IReader
  110. */
  111. public function setReadEmptyCells($pValue = true)
  112. {
  113. $this->readEmptyCells = $pValue;
  114. return $this;
  115. }
  116. /**
  117. * Read charts in workbook?
  118. * If this is true, then the Reader will include any charts that exist in the workbook.
  119. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  120. * If false (the default) it will ignore any charts defined in the workbook file.
  121. *
  122. * @return boolean
  123. */
  124. public function getIncludeCharts()
  125. {
  126. return $this->includeCharts;
  127. }
  128. /**
  129. * Set read charts in workbook
  130. * Set to true, to advise the Reader to include any charts that exist in the workbook.
  131. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  132. * Set to false (the default) to discard charts.
  133. *
  134. * @param boolean $pValue
  135. *
  136. * @return PHPExcel_Reader_IReader
  137. */
  138. public function setIncludeCharts($pValue = false)
  139. {
  140. $this->includeCharts = (boolean) $pValue;
  141. return $this;
  142. }
  143. /**
  144. * Get which sheets to load
  145. * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
  146. * indicating that all worksheets in the workbook should be loaded.
  147. *
  148. * @return mixed
  149. */
  150. public function getLoadSheetsOnly()
  151. {
  152. return $this->loadSheetsOnly;
  153. }
  154. /**
  155. * Set which sheets to load
  156. *
  157. * @param mixed $value
  158. * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
  159. * If NULL, then it tells the Reader to read all worksheets in the workbook
  160. *
  161. * @return PHPExcel_Reader_IReader
  162. */
  163. public function setLoadSheetsOnly($value = null)
  164. {
  165. if ($value === null) {
  166. return $this->setLoadAllSheets();
  167. }
  168. $this->loadSheetsOnly = is_array($value) ? $value : array($value);
  169. return $this;
  170. }
  171. /**
  172. * Set all sheets to load
  173. * Tells the Reader to load all worksheets from the workbook.
  174. *
  175. * @return PHPExcel_Reader_IReader
  176. */
  177. public function setLoadAllSheets()
  178. {
  179. $this->loadSheetsOnly = null;
  180. return $this;
  181. }
  182. /**
  183. * Read filter
  184. *
  185. * @return PHPExcel_Reader_IReadFilter
  186. */
  187. public function getReadFilter()
  188. {
  189. return $this->readFilter;
  190. }
  191. /**
  192. * Set read filter
  193. *
  194. * @param PHPExcel_Reader_IReadFilter $pValue
  195. * @return PHPExcel_Reader_IReader
  196. */
  197. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue)
  198. {
  199. $this->readFilter = $pValue;
  200. return $this;
  201. }
  202. /**
  203. * Open file for reading
  204. *
  205. * @param string $pFilename
  206. * @throws PHPExcel_Reader_Exception
  207. * @return resource
  208. */
  209. protected function openFile($pFilename)
  210. {
  211. // Check if file exists
  212. if (!file_exists($pFilename) || !is_readable($pFilename)) {
  213. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  214. }
  215. // Open file
  216. $this->fileHandle = fopen($pFilename, 'r');
  217. if ($this->fileHandle === false) {
  218. throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading.");
  219. }
  220. }
  221. /**
  222. * Can the current PHPExcel_Reader_IReader read the file?
  223. *
  224. * @param string $pFilename
  225. * @return boolean
  226. * @throws PHPExcel_Reader_Exception
  227. */
  228. public function canRead($pFilename)
  229. {
  230. // Check if file exists
  231. try {
  232. $this->openFile($pFilename);
  233. } catch (Exception $e) {
  234. return false;
  235. }
  236. $readable = $this->isValidFormat();
  237. fclose($this->fileHandle);
  238. return $readable;
  239. }
  240. /**
  241. * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
  242. *
  243. * @param string $xml
  244. * @throws PHPExcel_Reader_Exception
  245. */
  246. public function securityScan($xml)
  247. {
  248. $pattern = '/\\0?' . implode('\\0?', str_split('<!DOCTYPE')) . '\\0?/';
  249. if (preg_match($pattern, $xml)) {
  250. throw new PHPExcel_Reader_Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
  251. }
  252. return $xml;
  253. }
  254. /**
  255. * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
  256. *
  257. * @param string $filestream
  258. * @throws PHPExcel_Reader_Exception
  259. */
  260. public function securityScanFile($filestream)
  261. {
  262. return $this->securityScan(file_get_contents($filestream));
  263. }
  264. }