HTML.php 13.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
<?php
/**
 * PHPExcel
 *
 * Copyright (c) 2006 - 2013 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel_Reader
 * @copyright  Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    1.7.9, 2013-06-02
 */


/** PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
	/**
	 * @ignore
	 */
	define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
	require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}

/**
 * PHPExcel_Reader_HTML
 *
 * @category   PHPExcel
 * @package    PHPExcel_Reader
 * @copyright  Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
 */
class PHPExcel_Reader_HTML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
{
	/**
	 * Input encoding
	 *
	 * @var string
	 */
	private $_inputEncoding	= 'ANSI';

	/**
	 * Sheet index to read
	 *
	 * @var int
	 */
	private $_sheetIndex 	= 0;

	/**
	 * Formats
	 *
	 * @var array
	 */
	private $_formats = array( 'h1' => array( 'font' => array( 'bold' => true,
															   'size' => 24,
															 ),
											),	//	Bold, 24pt
							   'h2' => array( 'font' => array( 'bold' => true,
															   'size' => 18,
															 ),
											),	//	Bold, 18pt
							   'h3' => array( 'font' => array( 'bold' => true,
															   'size' => 13.5,
															 ),
											),	//	Bold, 13.5pt
							   'h4' => array( 'font' => array( 'bold' => true,
															   'size' => 12,
															 ),
											),	//	Bold, 12pt
							   'h5' => array( 'font' => array( 'bold' => true,
															   'size' => 10,
															 ),
											),	//	Bold, 10pt
							   'h6' => array( 'font' => array( 'bold' => true,
															   'size' => 7.5,
															 ),
											),	//	Bold, 7.5pt
							   'a'  => array( 'font' => array( 'underline' => true,
															   'color' => array( 'argb' => PHPExcel_Style_Color::COLOR_BLUE,
															                   ),
															 ),
											),	//	Blue underlined
							   'hr' => array( 'borders' => array( 'bottom' => array( 'style' => PHPExcel_Style_Border::BORDER_THIN,
																					 'color' => array( PHPExcel_Style_Color::COLOR_BLACK,
																					                 ),
																				   ),
																),
											),	//	Bottom border
							 );


	/**
	 * Create a new PHPExcel_Reader_HTML
	 */
	public function __construct() {
		$this->_readFilter 	= new PHPExcel_Reader_DefaultReadFilter();
	}

	/**
	 * Validate that the current file is an HTML file
	 *
	 * @return boolean
	 */
	protected function _isValidFormat()
	{
		//	Reading 2048 bytes should be enough to validate that the format is HTML
		$data = fread($this->_fileHandle, 2048);
		if ((strpos('<',$data) !== FALSE) &&
			(strlen($data) !== strlen(strip_tags($data)))) {
			return TRUE;
		}

		return FALSE;
	}

	/**
	 * Loads PHPExcel from file
	 *
	 * @param 	string 		$pFilename
	 * @return 	PHPExcel
	 * @throws 	PHPExcel_Reader_Exception
	 */
	public function load($pFilename)
	{
		// Create new PHPExcel
		$objPHPExcel = new PHPExcel();

		// Load into this instance
		return $this->loadIntoExisting($pFilename, $objPHPExcel);
	}

	/**
	 * Set input encoding
	 *
	 * @param string $pValue Input encoding
	 */
	public function setInputEncoding($pValue = 'ANSI')
	{
		$this->_inputEncoding = $pValue;
		return $this;
	}

	/**
	 * Get input encoding
	 *
	 * @return string
	 */
	public function getInputEncoding()
	{
		return $this->_inputEncoding;
	}

	//	Data Array used for testing only, should write to PHPExcel object on completion of tests
	private $_dataArray = array();

	private $_tableLevel = 0;
	private $_nestedColumn = array('A');

	private function _setTableStartColumn($column) {
		if ($this->_tableLevel == 0)
			$column = 'A';
		++$this->_tableLevel;
		$this->_nestedColumn[$this->_tableLevel] = $column;

		return $this->_nestedColumn[$this->_tableLevel];
	}

	private function _getTableStartColumn() {
		return $this->_nestedColumn[$this->_tableLevel];
	}

	private function _releaseTableStartColumn() {
		--$this->_tableLevel;
		return array_pop($this->_nestedColumn);
	}

	private function _flushCell($sheet,$column,$row,&$cellContent) {
		if (is_string($cellContent)) {
			//	Simple String content
			if (trim($cellContent) > '') {
				//	Only actually write it if there's content in the string
//				echo 'FLUSH CELL: ' , $column , $row , ' => ' , $cellContent , '<br />';
				//	Write to worksheet to be done here...
				//	... we return the cell so we can mess about with styles more easily
				$cell = $sheet->setCellValue($column.$row,$cellContent,true);
				$this->_dataArray[$row][$column] = $cellContent;
			}
		} else {
			//	We have a Rich Text run
			//	TODO
			$this->_dataArray[$row][$column] = 'RICH TEXT: ' . $cellContent;
		}
		$cellContent = (string) '';
	}

	private function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent){
		foreach($element->childNodes as $child){
			if ($child instanceof DOMText) {
				$domText = preg_replace('/\s+/',' ',trim($child->nodeValue));
				if (is_string($cellContent)) {
					//	simply append the text if the cell content is a plain text string
					$cellContent .= $domText;
				} else {
					//	but if we have a rich text run instead, we need to append it correctly
					//	TODO
				}
			} elseif($child instanceof DOMElement) {
//				echo '<b>DOM ELEMENT: </b>' , strtoupper($child->nodeName) , '<br />';

				$attributeArray = array();
				foreach($child->attributes as $attribute) {
//					echo '<b>ATTRIBUTE: </b>' , $attribute->name , ' => ' , $attribute->value , '<br />';
					$attributeArray[$attribute->name] = $attribute->value;
				}

				switch($child->nodeName) {
					case 'meta' :
						foreach($attributeArray as $attributeName => $attributeValue) {
							switch($attributeName) {
								case 'content':
									//	TODO
									//	Extract character set, so we can convert to UTF-8 if required
									break;
							}
						}
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
						break;
					case 'title' :
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
						$sheet->setTitle($cellContent);
						$cellContent = '';
						break;
					case 'span'  :
					case 'div'   :
					case 'font'  :
					case 'i'     :
					case 'em'    :
					case 'strong':
					case 'b'     :
//						echo 'STYLING, SPAN OR DIV<br />';
						if ($cellContent > '')
							$cellContent .= ' ';
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
						if ($cellContent > '')
							$cellContent .= ' ';
//						echo 'END OF STYLING, SPAN OR DIV<br />';
						break;
					case 'hr' :
						$this->_flushCell($sheet,$column,$row,$cellContent);
						++$row;
						if (isset($this->_formats[$child->nodeName])) {
							$sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]);
						} else {
							$cellContent = '----------';
							$this->_flushCell($sheet,$column,$row,$cellContent);
						}
						++$row;
					case 'br' :
						if ($this->_tableLevel > 0) {
							//	If we're inside a table, replace with a \n
							$cellContent .= "\n";
						} else {
							//	Otherwise flush our existing content and move the row cursor on
							$this->_flushCell($sheet,$column,$row,$cellContent);
							++$row;
						}
//						echo 'HARD LINE BREAK: ' , '<br />';
						break;
					case 'a'  :
//						echo 'START OF HYPERLINK: ' , '<br />';
						foreach($attributeArray as $attributeName => $attributeValue) {
							switch($attributeName) {
								case 'href':
//									echo 'Link to ' , $attributeValue , '<br />';
									$sheet->getCell($column.$row)->getHyperlink()->setUrl($attributeValue);
									if (isset($this->_formats[$child->nodeName])) {
										$sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]);
									}
									break;
							}
						}
						$cellContent .= ' ';
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//						echo 'END OF HYPERLINK:' , '<br />';
						break;
					case 'h1' :
					case 'h2' :
					case 'h3' :
					case 'h4' :
					case 'h5' :
					case 'h6' :
					case 'ol' :
					case 'ul' :
					case 'p'  :
						if ($this->_tableLevel > 0) {
							//	If we're inside a table, replace with a \n
							$cellContent .= "\n";
//							echo 'LIST ENTRY: ' , '<br />';
							$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//							echo 'END OF LIST ENTRY:' , '<br />';
						} else {
							if ($cellContent > '') {
								$this->_flushCell($sheet,$column,$row,$cellContent);
								$row += 2;
							}
//							echo 'START OF PARAGRAPH: ' , '<br />';
							$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//							echo 'END OF PARAGRAPH:' , '<br />';
							$this->_flushCell($sheet,$column,$row,$cellContent);

							if (isset($this->_formats[$child->nodeName])) {
								$sheet->getStyle($column.$row)->applyFromArray($this->_formats[$child->nodeName]);
							}

							$row += 2;
							$column = 'A';
						}
						break;
					case 'li'  :
						if ($this->_tableLevel > 0) {
							//	If we're inside a table, replace with a \n
							$cellContent .= "\n";
//							echo 'LIST ENTRY: ' , '<br />';
							$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//							echo 'END OF LIST ENTRY:' , '<br />';
						} else {
							if ($cellContent > '') {
								$this->_flushCell($sheet,$column,$row,$cellContent);
							}
							++$row;
//							echo 'LIST ENTRY: ' , '<br />';
							$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//							echo 'END OF LIST ENTRY:' , '<br />';
							$this->_flushCell($sheet,$column,$row,$cellContent);
							$column = 'A';
						}
						break;
					case 'table' :
						$this->_flushCell($sheet,$column,$row,$cellContent);
						$column = $this->_setTableStartColumn($column);
//						echo 'START OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
						if ($this->_tableLevel > 1)
							--$row;
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//						echo 'END OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
						$column = $this->_releaseTableStartColumn();
						if ($this->_tableLevel > 1) {
							++$column;
						} else {
							++$row;
						}
						break;
					case 'thead' :
					case 'tbody' :
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
						break;
					case 'tr' :
						++$row;
						$column = $this->_getTableStartColumn();
						$cellContent = '';
//						echo 'START OF TABLE ' , $this->_tableLevel , ' ROW<br />';
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//						echo 'END OF TABLE ' , $this->_tableLevel , ' ROW<br />';
						break;
					case 'th' :
					case 'td' :
//						echo 'START OF TABLE ' , $this->_tableLevel , ' CELL<br />';
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
//						echo 'END OF TABLE ' , $this->_tableLevel , ' CELL<br />';
						$this->_flushCell($sheet,$column,$row,$cellContent);
						++$column;
						break;
					case 'body' :
						$row = 1;
						$column = 'A';
						$content = '';
						$this->_tableLevel = 0;
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
						break;
					default:
						$this->_processDomElement($child,$sheet,$row,$column,$cellContent);
				}
			}
		}
	}

	/**
	 * Loads PHPExcel from file into PHPExcel instance
	 *
	 * @param 	string 		$pFilename
	 * @param	PHPExcel	$objPHPExcel
	 * @return 	PHPExcel
	 * @throws 	PHPExcel_Reader_Exception
	 */
	public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
	{
		// Open file to validate
		$this->_openFile($pFilename);
		if (!$this->_isValidFormat()) {
			fclose ($this->_fileHandle);
			throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid HTML file.");
		}
		//	Close after validating
		fclose ($this->_fileHandle);

		// Create new PHPExcel
		while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
			$objPHPExcel->createSheet();
		}
		$objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );

		//	Create a new DOM object
		$dom = new domDocument;
		//	Reload the HTML file into the DOM object
		$loaded = $dom->loadHTMLFile($pFilename);
		if ($loaded === FALSE) {
			throw new PHPExcel_Reader_Exception('Failed to load ',$pFilename,' as a DOM Document');
		}

		//	Discard white space
		$dom->preserveWhiteSpace = false;


		$row = 0;
		$column = 'A';
		$content = '';
		$this->_processDomElement($dom,$objPHPExcel->getActiveSheet(),$row,$column,$content);

//		echo '<hr />';
//		var_dump($this->_dataArray);

		// Return
		return $objPHPExcel;
	}

	/**
	 * Get sheet index
	 *
	 * @return int
	 */
	public function getSheetIndex() {
		return $this->_sheetIndex;
	}

	/**
	 * Set sheet index
	 *
	 * @param	int		$pValue		Sheet index
	 * @return PHPExcel_Reader_HTML
	 */
	public function setSheetIndex($pValue = 0) {
		$this->_sheetIndex = $pValue;
		return $this;
	}

}