47ce6e80a868cb66850c78d6ce1febbad55d993f.svn-base 20 KB

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