ReaderAbstract.Class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: phperstar
  5. * Date: 2020/8/10
  6. * Time: 4:24 PM
  7. */
  8. namespace Util\PHPExcel\Reader;
  9. use Mall\Framework\Core\ResultWrapper;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Util\PHPExcel\Reader\IReader;
  12. use Util\PHPExcel\Reader\IReadFilter;
  13. abstract class ReaderAbstract implements IReader
  14. {
  15. protected $fileHandle = null;
  16. /**
  17. * Read data only?
  18. * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  19. * or whether it should read both data and formatting
  20. *
  21. * @var boolean
  22. */
  23. protected $readDataOnly = false;
  24. /**
  25. * PHPExcel_Reader_IReadFilter instance
  26. *
  27. * @var IReadFilter
  28. */
  29. protected $readFilter;
  30. /**
  31. * Restrict which sheets should be loaded?
  32. * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  33. *
  34. * @var array of string
  35. */
  36. protected $loadSheetsOnly;
  37. /**
  38. * Can the current PHPExcel_Reader_IReader read the file?
  39. *
  40. * @param string $pFilename
  41. * @return ResultWrapper
  42. */
  43. public function canRead($pFilename)
  44. {
  45. // Check if file exists
  46. $reuslt = $this->openFile($pFilename);
  47. if(!$reuslt->isSuccess()){
  48. return ResultWrapper::fail($reuslt->getData(), $reuslt->getErrorCode());
  49. }
  50. $readable = $this->isValidFormat();
  51. fclose($this->fileHandle);
  52. return ResultWrapper::success($readable);
  53. }
  54. /**
  55. * Open file for reading
  56. *
  57. * @param string $pFilename
  58. * @return ResultWrapper
  59. */
  60. protected function openFile($pFilename)
  61. {
  62. // Check if file exists
  63. if (!file_exists($pFilename) || !is_readable($pFilename)) {
  64. return ResultWrapper::fail('文件不存在',$pFilename, ErrorCode::$notAllowAccess);
  65. }
  66. // Open file
  67. $this->fileHandle = fopen($pFilename, 'r');
  68. if ($this->fileHandle === false) {
  69. return ResultWrapper::fail('读取文件失败',$pFilename, ErrorCode::$notAllowAccess);
  70. }
  71. }
  72. /**
  73. * 获取只读取数据模式配置
  74. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
  75. * If false (the default) it will read data and formatting.
  76. *
  77. * @return boolean
  78. */
  79. public function getReadDataOnly()
  80. {
  81. return $this->readDataOnly;
  82. }
  83. /**
  84. * 设置是否只读取数据
  85. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
  86. * Set to false (the default) to advise the Reader to read both data and formatting for cells.
  87. *
  88. * @param boolean $pValue
  89. *
  90. * @return PHPExcel_Reader_IReader
  91. */
  92. public function setReadDataOnly($pValue = false)
  93. {
  94. $this->readDataOnly = $pValue;
  95. return $this;
  96. }
  97. /**
  98. * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
  99. *
  100. * @param string $xml
  101. * @throws PHPExcel_Reader_Exception
  102. */
  103. public function securityScan($xml)
  104. {
  105. $pattern = '/\\0?' . implode('\\0?', str_split('<!DOCTYPE')) . '\\0?/';
  106. if (preg_match($pattern, $xml)) {
  107. throw new PHPExcel_Reader_Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
  108. }
  109. return $xml;
  110. }
  111. /**
  112. * Read filter
  113. *
  114. * @return PHPExcel_Reader_IReadFilter
  115. */
  116. public function getReadFilter()
  117. {
  118. return $this->readFilter;
  119. }
  120. /**
  121. * Set read filter
  122. *
  123. * @param PHPExcel_Reader_IReadFilter $pValue
  124. * @return PHPExcel_Reader_IReader
  125. */
  126. public function setReadFilter(IReadFilter $pValue)
  127. {
  128. $this->readFilter = $pValue;
  129. return $this;
  130. }
  131. /**
  132. * Get which sheets to load
  133. * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
  134. * indicating that all worksheets in the workbook should be loaded.
  135. *
  136. * @return mixed
  137. */
  138. public function getLoadSheetsOnly()
  139. {
  140. return $this->loadSheetsOnly;
  141. }
  142. /**
  143. * Set which sheets to load
  144. *
  145. * @param mixed $value
  146. * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
  147. * If NULL, then it tells the Reader to read all worksheets in the workbook
  148. *
  149. * @return PHPExcel_Reader_IReader
  150. */
  151. public function setLoadSheetsOnly($value = null)
  152. {
  153. if ($value === null) {
  154. return $this->setLoadAllSheets();
  155. }
  156. $this->loadSheetsOnly = is_array($value) ? $value : array($value);
  157. return $this;
  158. }
  159. /**
  160. * Set all sheets to load
  161. * Tells the Reader to load all worksheets from the workbook.
  162. *
  163. * @return PHPExcel_Reader_IReader
  164. */
  165. public function setLoadAllSheets()
  166. {
  167. $this->loadSheetsOnly = null;
  168. return $this;
  169. }
  170. }