HTML.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. if (!defined('PHPEXCEL_ROOT')) {
  8. /**
  9. * @ignore
  10. */
  11. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  12. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  13. }
  14. /**
  15. * PHPExcel_Reader_HTML
  16. *
  17. * Copyright (c) 2006 - 2015 PHPExcel
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2.1 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  32. *
  33. * @category PHPExcel
  34. * @package PHPExcel_Reader
  35. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  36. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  37. * @version ##VERSION##, ##DATE##
  38. */
  39. /** PHPExcel root directory */
  40. class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  41. {
  42. /**
  43. * Input encoding
  44. *
  45. * @var string
  46. */
  47. protected $inputEncoding = 'ANSI';
  48. /**
  49. * Sheet index to read
  50. *
  51. * @var int
  52. */
  53. protected $sheetIndex = 0;
  54. /**
  55. * Formats
  56. *
  57. * @var array
  58. */
  59. protected $formats = array(
  60. 'h1' => array(
  61. 'font' => array(
  62. 'bold' => true,
  63. 'size' => 24,
  64. ),
  65. ), // Bold, 24pt
  66. 'h2' => array(
  67. 'font' => array(
  68. 'bold' => true,
  69. 'size' => 18,
  70. ),
  71. ), // Bold, 18pt
  72. 'h3' => array(
  73. 'font' => array(
  74. 'bold' => true,
  75. 'size' => 13.5,
  76. ),
  77. ), // Bold, 13.5pt
  78. 'h4' => array(
  79. 'font' => array(
  80. 'bold' => true,
  81. 'size' => 12,
  82. ),
  83. ), // Bold, 12pt
  84. 'h5' => array(
  85. 'font' => array(
  86. 'bold' => true,
  87. 'size' => 10,
  88. ),
  89. ), // Bold, 10pt
  90. 'h6' => array(
  91. 'font' => array(
  92. 'bold' => true,
  93. 'size' => 7.5,
  94. ),
  95. ), // Bold, 7.5pt
  96. 'a' => array(
  97. 'font' => array(
  98. 'underline' => true,
  99. 'color' => array(
  100. 'argb' => PHPExcel_Style_Color::COLOR_BLUE,
  101. ),
  102. ),
  103. ), // Blue underlined
  104. 'hr' => array(
  105. 'borders' => array(
  106. 'bottom' => array(
  107. 'style' => PHPExcel_Style_Border::BORDER_THIN,
  108. 'color' => array(
  109. PHPExcel_Style_Color::COLOR_BLACK,
  110. ),
  111. ),
  112. ),
  113. ), // Bottom border
  114. );
  115. protected $rowspan = array();
  116. /**
  117. * Create a new PHPExcel_Reader_HTML
  118. */
  119. public function __construct()
  120. {
  121. $this->readFilter = new PHPExcel_Reader_DefaultReadFilter();
  122. }
  123. /**
  124. * Validate that the current file is an HTML file
  125. *
  126. * @return boolean
  127. */
  128. protected function isValidFormat()
  129. {
  130. // Reading 2048 bytes should be enough to validate that the format is HTML
  131. $data = fread($this->fileHandle, 2048);
  132. if ((strpos($data, '<') !== false) &&
  133. (strlen($data) !== strlen(strip_tags($data)))) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. /**
  139. * Loads PHPExcel from file
  140. *
  141. * @param string $pFilename
  142. * @return PHPExcel
  143. * @throws PHPExcel_Reader_Exception
  144. */
  145. public function load($pFilename)
  146. {
  147. // Create new PHPExcel
  148. $objPHPExcel = new PHPExcel();
  149. // Load into this instance
  150. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  151. }
  152. /**
  153. * Set input encoding
  154. *
  155. * @param string $pValue Input encoding
  156. */
  157. public function setInputEncoding($pValue = 'ANSI')
  158. {
  159. $this->inputEncoding = $pValue;
  160. return $this;
  161. }
  162. /**
  163. * Get input encoding
  164. *
  165. * @return string
  166. */
  167. public function getInputEncoding()
  168. {
  169. return $this->inputEncoding;
  170. }
  171. // Data Array used for testing only, should write to PHPExcel object on completion of tests
  172. protected $dataArray = array();
  173. protected $tableLevel = 0;
  174. protected $nestedColumn = array('A');
  175. protected function setTableStartColumn($column)
  176. {
  177. if ($this->tableLevel == 0) {
  178. $column = 'A';
  179. }
  180. ++$this->tableLevel;
  181. $this->nestedColumn[$this->tableLevel] = $column;
  182. return $this->nestedColumn[$this->tableLevel];
  183. }
  184. protected function getTableStartColumn()
  185. {
  186. return $this->nestedColumn[$this->tableLevel];
  187. }
  188. protected function releaseTableStartColumn()
  189. {
  190. --$this->tableLevel;
  191. return array_pop($this->nestedColumn);
  192. }
  193. protected function flushCell($sheet, $column, $row, &$cellContent)
  194. {
  195. if (is_string($cellContent)) {
  196. // Simple String content
  197. if (trim($cellContent) > '') {
  198. // Only actually write it if there's content in the string
  199. // echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '<br />';
  200. // Write to worksheet to be done here...
  201. // ... we return the cell so we can mess about with styles more easily
  202. $sheet->setCellValue($column . $row, $cellContent, true);
  203. $this->dataArray[$row][$column] = $cellContent;
  204. }
  205. } else {
  206. // We have a Rich Text run
  207. // TODO
  208. $this->dataArray[$row][$column] = 'RICH TEXT: ' . $cellContent;
  209. }
  210. $cellContent = (string) '';
  211. }
  212. protected function processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent, $format = null)
  213. {
  214. foreach ($element->childNodes as $child) {
  215. if ($child instanceof DOMText) {
  216. $domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue));
  217. if (is_string($cellContent)) {
  218. // simply append the text if the cell content is a plain text string
  219. $cellContent .= $domText;
  220. } else {
  221. // but if we have a rich text run instead, we need to append it correctly
  222. // TODO
  223. }
  224. } elseif ($child instanceof DOMElement) {
  225. // echo '<b>DOM ELEMENT: </b>' , strtoupper($child->nodeName) , '<br />';
  226. $attributeArray = array();
  227. foreach ($child->attributes as $attribute) {
  228. // echo '<b>ATTRIBUTE: </b>' , $attribute->name , ' => ' , $attribute->value , '<br />';
  229. $attributeArray[$attribute->name] = $attribute->value;
  230. }
  231. switch ($child->nodeName) {
  232. case 'meta':
  233. foreach ($attributeArray as $attributeName => $attributeValue) {
  234. switch ($attributeName) {
  235. case 'content':
  236. // TODO
  237. // Extract character set, so we can convert to UTF-8 if required
  238. break;
  239. }
  240. }
  241. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  242. break;
  243. case 'title':
  244. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  245. $sheet->setTitle($cellContent);
  246. $cellContent = '';
  247. break;
  248. case 'span':
  249. case 'div':
  250. case 'font':
  251. case 'i':
  252. case 'em':
  253. case 'strong':
  254. case 'b':
  255. // echo 'STYLING, SPAN OR DIV<br />';
  256. if ($cellContent > '') {
  257. $cellContent .= ' ';
  258. }
  259. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  260. if ($cellContent > '') {
  261. $cellContent .= ' ';
  262. }
  263. // echo 'END OF STYLING, SPAN OR DIV<br />';
  264. break;
  265. case 'hr':
  266. $this->flushCell($sheet, $column, $row, $cellContent);
  267. ++$row;
  268. if (isset($this->formats[$child->nodeName])) {
  269. $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
  270. } else {
  271. $cellContent = '----------';
  272. $this->flushCell($sheet, $column, $row, $cellContent);
  273. }
  274. ++$row;
  275. // Add a break after a horizontal rule, simply by allowing the code to dropthru
  276. case 'br':
  277. if ($this->tableLevel > 0) {
  278. // If we're inside a table, replace with a \n
  279. $cellContent .= "\n";
  280. } else {
  281. // Otherwise flush our existing content and move the row cursor on
  282. $this->flushCell($sheet, $column, $row, $cellContent);
  283. ++$row;
  284. }
  285. // echo 'HARD LINE BREAK: ' , '<br />';
  286. break;
  287. case 'a':
  288. // echo 'START OF HYPERLINK: ' , '<br />';
  289. foreach ($attributeArray as $attributeName => $attributeValue) {
  290. switch ($attributeName) {
  291. case 'href':
  292. // echo 'Link to ' , $attributeValue , '<br />';
  293. $sheet->getCell($column . $row)->getHyperlink()->setUrl($attributeValue);
  294. if (isset($this->formats[$child->nodeName])) {
  295. $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
  296. }
  297. break;
  298. }
  299. }
  300. $cellContent .= ' ';
  301. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  302. // echo 'END OF HYPERLINK:' , '<br />';
  303. break;
  304. case 'h1':
  305. case 'h2':
  306. case 'h3':
  307. case 'h4':
  308. case 'h5':
  309. case 'h6':
  310. case 'ol':
  311. case 'ul':
  312. case 'p':
  313. if ($this->tableLevel > 0) {
  314. // If we're inside a table, replace with a \n
  315. $cellContent .= "\n";
  316. // echo 'LIST ENTRY: ' , '<br />';
  317. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  318. // echo 'END OF LIST ENTRY:' , '<br />';
  319. } else {
  320. if ($cellContent > '') {
  321. $this->flushCell($sheet, $column, $row, $cellContent);
  322. $row++;
  323. }
  324. // echo 'START OF PARAGRAPH: ' , '<br />';
  325. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  326. // echo 'END OF PARAGRAPH:' , '<br />';
  327. $this->flushCell($sheet, $column, $row, $cellContent);
  328. if (isset($this->formats[$child->nodeName])) {
  329. $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
  330. }
  331. $row++;
  332. $column = 'A';
  333. }
  334. break;
  335. case 'li':
  336. if ($this->tableLevel > 0) {
  337. // If we're inside a table, replace with a \n
  338. $cellContent .= "\n";
  339. // echo 'LIST ENTRY: ' , '<br />';
  340. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  341. // echo 'END OF LIST ENTRY:' , '<br />';
  342. } else {
  343. if ($cellContent > '') {
  344. $this->flushCell($sheet, $column, $row, $cellContent);
  345. }
  346. ++$row;
  347. // echo 'LIST ENTRY: ' , '<br />';
  348. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  349. // echo 'END OF LIST ENTRY:' , '<br />';
  350. $this->flushCell($sheet, $column, $row, $cellContent);
  351. $column = 'A';
  352. }
  353. break;
  354. case 'table':
  355. $this->flushCell($sheet, $column, $row, $cellContent);
  356. $column = $this->setTableStartColumn($column);
  357. // echo 'START OF TABLE LEVEL ' , $this->tableLevel , '<br />';
  358. if ($this->tableLevel > 1) {
  359. --$row;
  360. }
  361. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  362. // echo 'END OF TABLE LEVEL ' , $this->tableLevel , '<br />';
  363. $column = $this->releaseTableStartColumn();
  364. if ($this->tableLevel > 1) {
  365. ++$column;
  366. } else {
  367. ++$row;
  368. }
  369. break;
  370. case 'thead':
  371. case 'tbody':
  372. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  373. break;
  374. case 'tr':
  375. $column = $this->getTableStartColumn();
  376. $cellContent = '';
  377. // echo 'START OF TABLE ' , $this->tableLevel , ' ROW<br />';
  378. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  379. ++$row;
  380. // echo 'END OF TABLE ' , $this->tableLevel , ' ROW<br />';
  381. break;
  382. case 'th':
  383. case 'td':
  384. // echo 'START OF TABLE ' , $this->tableLevel , ' CELL<br />';
  385. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  386. // echo 'END OF TABLE ' , $this->tableLevel , ' CELL<br />';
  387. while (isset($this->rowspan[$column . $row])) {
  388. ++$column;
  389. }
  390. $this->flushCell($sheet, $column, $row, $cellContent);
  391. // if (isset($attributeArray['style']) && !empty($attributeArray['style'])) {
  392. // $styleAry = $this->getPhpExcelStyleArray($attributeArray['style']);
  393. //
  394. // if (!empty($styleAry)) {
  395. // $sheet->getStyle($column . $row)->applyFromArray($styleAry);
  396. // }
  397. // }
  398. if (isset($attributeArray['rowspan']) && isset($attributeArray['colspan'])) {
  399. //create merging rowspan and colspan
  400. $columnTo = $column;
  401. for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
  402. ++$columnTo;
  403. }
  404. $range = $column . $row . ':' . $columnTo . ($row + $attributeArray['rowspan'] - 1);
  405. foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
  406. $this->rowspan[$value] = true;
  407. }
  408. $sheet->mergeCells($range);
  409. $column = $columnTo;
  410. } elseif (isset($attributeArray['rowspan'])) {
  411. //create merging rowspan
  412. $range = $column . $row . ':' . $column . ($row + $attributeArray['rowspan'] - 1);
  413. foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
  414. $this->rowspan[$value] = true;
  415. }
  416. $sheet->mergeCells($range);
  417. } elseif (isset($attributeArray['colspan'])) {
  418. //create merging colspan
  419. $columnTo = $column;
  420. for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
  421. ++$columnTo;
  422. }
  423. $sheet->mergeCells($column . $row . ':' . $columnTo . $row);
  424. $column = $columnTo;
  425. }
  426. ++$column;
  427. break;
  428. case 'body':
  429. $row = 1;
  430. $column = 'A';
  431. $content = '';
  432. $this->tableLevel = 0;
  433. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  434. break;
  435. default:
  436. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  437. }
  438. }
  439. }
  440. }
  441. /**
  442. * Loads PHPExcel from file into PHPExcel instance
  443. *
  444. * @param string $pFilename
  445. * @param PHPExcel $objPHPExcel
  446. * @return PHPExcel
  447. * @throws PHPExcel_Reader_Exception
  448. */
  449. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  450. {
  451. // Open file to validate
  452. $this->openFile($pFilename);
  453. if (!$this->isValidFormat()) {
  454. fclose($this->fileHandle);
  455. throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid HTML file.");
  456. }
  457. // Close after validating
  458. fclose($this->fileHandle);
  459. // Create new PHPExcel
  460. while ($objPHPExcel->getSheetCount() <= $this->sheetIndex) {
  461. $objPHPExcel->createSheet();
  462. }
  463. $objPHPExcel->setActiveSheetIndex($this->sheetIndex);
  464. // Create a new DOM object
  465. $dom = new domDocument;
  466. // Reload the HTML file into the DOM object
  467. $loaded = $dom->loadHTML(mb_convert_encoding($this->securityScanFile($pFilename), 'HTML-ENTITIES', 'UTF-8'));
  468. if ($loaded === false) {
  469. throw new PHPExcel_Reader_Exception('Failed to load ' . $pFilename . ' as a DOM Document');
  470. }
  471. // Discard white space
  472. $dom->preserveWhiteSpace = false;
  473. $row = 0;
  474. $column = 'A';
  475. $content = '';
  476. $this->processDomElement($dom, $objPHPExcel->getActiveSheet(), $row, $column, $content);
  477. // Return
  478. return $objPHPExcel;
  479. }
  480. /**
  481. * Get sheet index
  482. *
  483. * @return int
  484. */
  485. public function getSheetIndex()
  486. {
  487. return $this->sheetIndex;
  488. }
  489. /**
  490. * Set sheet index
  491. *
  492. * @param int $pValue Sheet index
  493. * @return PHPExcel_Reader_HTML
  494. */
  495. public function setSheetIndex($pValue = 0)
  496. {
  497. $this->sheetIndex = $pValue;
  498. return $this;
  499. }
  500. /**
  501. * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
  502. *
  503. * @param string $xml
  504. * @throws PHPExcel_Reader_Exception
  505. */
  506. public function securityScan($xml)
  507. {
  508. $pattern = '/\\0?' . implode('\\0?', str_split('<!ENTITY')) . '\\0?/';
  509. if (preg_match($pattern, $xml)) {
  510. throw new PHPExcel_Reader_Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
  511. }
  512. return $xml;
  513. }
  514. }