5ee81758ef9d74ca5b478ac4e441d9ed5d39e9e7.svn-base 6.9 KB

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