PageRenderTime 37ms CodeModel.GetById 11ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/PHPExcel/Reader/CSV.php

https://bitbucket.org/nfredricks/wp-employee-time
PHP | 505 lines | 205 code | 74 blank | 226 comment | 36 complexity | 61912d2ffd0dcba4212627bb06eb3be9 MD5 | raw file
  1<?php
  2/**
  3 * PHPExcel
  4 *
  5 * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 24 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 25 * @version    1.7.8, 2012-10-12
 26 */
 27
 28
 29/** PHPExcel root directory */
 30if (!defined('PHPEXCEL_ROOT')) {
 31	/**
 32	 * @ignore
 33	 */
 34	define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
 35	require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
 36}
 37
 38/**
 39 * PHPExcel_Reader_CSV
 40 *
 41 * @category   PHPExcel
 42 * @package    PHPExcel_Reader
 43 * @copyright  Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
 44 */
 45class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
 46{
 47	/**
 48	 * Input encoding
 49	 *
 50	 * @access	private
 51	 * @var	string
 52	 */
 53	private $_inputEncoding	= 'UTF-8';
 54
 55	/**
 56	 * Delimiter
 57	 *
 58	 * @access	private
 59	 * @var string
 60	 */
 61	private $_delimiter		= ',';
 62
 63	/**
 64	 * Enclosure
 65	 *
 66	 * @access	private
 67	 * @var	string
 68	 */
 69	private $_enclosure		= '"';
 70
 71	/**
 72	 * Line ending
 73	 *
 74	 * @access	private
 75	 * @var	string
 76	 */
 77	private $_lineEnding	= PHP_EOL;
 78
 79	/**
 80	 * Sheet index to read
 81	 *
 82	 * @access	private
 83	 * @var	int
 84	 */
 85	private $_sheetIndex	= 0;
 86
 87	/**
 88	 * Load rows contiguously
 89	 *
 90	 * @access	private
 91	 * @var	int
 92	 */
 93	private $_contiguous	= false;
 94
 95
 96	/**
 97	 * Row counter for loading rows contiguously
 98	 *
 99	 * @access	private
100	 * @var	int
101	 */
102	private $_contiguousRow	= -1;
103
104	/**
105	 * PHPExcel_Reader_IReadFilter instance
106	 *
107	 * @access	private
108	 * @var	PHPExcel_Reader_IReadFilter
109	 */
110	private $_readFilter = null;
111
112
113	/**
114	 * Create a new PHPExcel_Reader_CSV
115	 */
116	public function __construct() {
117		$this->_readFilter		= new PHPExcel_Reader_DefaultReadFilter();
118	}	//	function __construct()
119
120
121	/**
122	 * Can the current PHPExcel_Reader_IReader read the file?
123	 *
124	 * @access	public
125	 * @param 	string 		$pFileName
126	 * @return boolean
127	 * @throws Exception
128	 */
129	public function canRead($pFilename)
130	{
131		// Check if file exists
132		if (!file_exists($pFilename)) {
133			throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
134		}
135
136		return true;
137	}	//	function canRead()
138
139
140	/**
141	 * Read filter
142	 *
143	 * @access	public
144	 * @return PHPExcel_Reader_IReadFilter
145	 */
146	public function getReadFilter() {
147		return $this->_readFilter;
148	}	//	function getReadFilter()
149
150
151	/**
152	 * Set read filter
153	 *
154	 * @access	public
155	 * @param	PHPExcel_Reader_IReadFilter $pValue
156	 */
157	public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
158		$this->_readFilter = $pValue;
159		return $this;
160	}	//	function setReadFilter()
161
162
163	/**
164	 * Set input encoding
165	 *
166	 * @access	public
167	 * @param string $pValue Input encoding
168	 */
169	public function setInputEncoding($pValue = 'UTF-8')
170	{
171		$this->_inputEncoding = $pValue;
172		return $this;
173	}	//	function setInputEncoding()
174
175
176	/**
177	 * Get input encoding
178	 *
179	 * @access	public
180	 * @return string
181	 */
182	public function getInputEncoding()
183	{
184		return $this->_inputEncoding;
185	}	//	function getInputEncoding()
186
187
188	/**
189	 * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
190	 *
191	 * @access	public
192	 * @param 	string 		$pFilename
193	 * @throws	Exception
194	 */
195	public function listWorksheetInfo($pFilename)
196	{
197		// Check if file exists
198		if (!file_exists($pFilename)) {
199			throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
200		}
201
202		// Open file
203		$fileHandle = fopen($pFilename, 'r');
204		if ($fileHandle === false) {
205			throw new Exception("Could not open file " . $pFilename . " for reading.");
206		}
207
208		// Skip BOM, if any
209		switch ($this->_inputEncoding) {
210			case 'UTF-8':
211				fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
212				fseek($fileHandle, 3) : fseek($fileHandle, 0);
213				break;
214			case 'UTF-16LE':
215				fgets($fileHandle, 3) == "\xFF\xFE" ?
216				fseek($fileHandle, 2) : fseek($fileHandle, 0);
217				break;
218			case 'UTF-16BE':
219				fgets($fileHandle, 3) == "\xFE\xFF" ?
220				fseek($fileHandle, 2) : fseek($fileHandle, 0);
221				break;
222			case 'UTF-32LE':
223				fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
224				fseek($fileHandle, 4) : fseek($fileHandle, 0);
225				break;
226			case 'UTF-32BE':
227				fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
228				fseek($fileHandle, 4) : fseek($fileHandle, 0);
229				break;
230			default:
231				break;
232		}
233
234		$escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure );
235
236		$worksheetInfo = array();
237		$worksheetInfo[0]['worksheetName'] = 'Worksheet';
238		$worksheetInfo[0]['lastColumnLetter'] = 'A';
239		$worksheetInfo[0]['lastColumnIndex'] = 0;
240		$worksheetInfo[0]['totalRows'] = 0;
241		$worksheetInfo[0]['totalColumns'] = 0;
242
243		// Loop through each line of the file in turn
244		while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
245			$worksheetInfo[0]['totalRows']++;
246			$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
247		}
248
249		$worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
250		$worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
251
252		// Close file
253		fclose($fileHandle);
254
255		return $worksheetInfo;
256	}
257
258
259	/**
260	 * Loads PHPExcel from file
261	 *
262	 * @access	public
263	 * @param 	string 		$pFilename
264	 * @return PHPExcel
265	 * @throws Exception
266	 */
267	public function load($pFilename)
268	{
269		// Create new PHPExcel
270		$objPHPExcel = new PHPExcel();
271
272		// Load into this instance
273		return $this->loadIntoExisting($pFilename, $objPHPExcel);
274	}	//	function load()
275
276
277	/**
278	 * Loads PHPExcel from file into PHPExcel instance
279	 *
280	 * @access	public
281	 * @param 	string 		$pFilename
282	 * @param	PHPExcel	$objPHPExcel
283	 * @return 	PHPExcel
284	 * @throws 	Exception
285	 */
286	public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
287	{
288		// Check if file exists
289		if (!file_exists($pFilename)) {
290			throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
291		}
292
293		// Create new PHPExcel
294		while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
295			$objPHPExcel->createSheet();
296		}
297		$sheet = $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
298
299		$lineEnding = ini_get('auto_detect_line_endings');
300		ini_set('auto_detect_line_endings', true);
301
302		// Open file
303		$fileHandle = fopen($pFilename, 'r');
304		if ($fileHandle === false) {
305			throw new Exception("Could not open file $pFilename for reading.");
306		}
307
308		// Skip BOM, if any
309		switch ($this->_inputEncoding) {
310			case 'UTF-8':
311				fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
312					fseek($fileHandle, 3) : fseek($fileHandle, 0);
313				break;
314			case 'UTF-16LE':
315				fgets($fileHandle, 3) == "\xFF\xFE" ?
316					fseek($fileHandle, 2) : fseek($fileHandle, 0);
317				break;
318			case 'UTF-16BE':
319				fgets($fileHandle, 3) == "\xFE\xFF" ?
320					fseek($fileHandle, 2) : fseek($fileHandle, 0);
321				break;
322			case 'UTF-32LE':
323				fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
324					fseek($fileHandle, 4) : fseek($fileHandle, 0);
325				break;
326			case 'UTF-32BE':
327				fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
328					fseek($fileHandle, 4) : fseek($fileHandle, 0);
329				break;
330			default:
331				break;
332		}
333
334		$escapeEnclosures = array( "\\" . $this->_enclosure,
335								   $this->_enclosure . $this->_enclosure
336								 );
337
338		// Set our starting row based on whether we're in contiguous mode or not
339		$currentRow = 1;
340		if ($this->_contiguous) {
341			$currentRow = ($this->_contiguousRow == -1) ? $sheet->getHighestRow(): $this->_contiguousRow;
342		}
343
344		// Loop through each line of the file in turn
345		while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
346			$columnLetter = 'A';
347			foreach($rowData as $rowDatum) {
348				if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
349					// Unescape enclosures
350					$rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
351
352					// Convert encoding if necessary
353					if ($this->_inputEncoding !== 'UTF-8') {
354						$rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
355					}
356
357					// Set cell value
358					$sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
359				}
360				++$columnLetter;
361			}
362			++$currentRow;
363		}
364
365		// Close file
366		fclose($fileHandle);
367
368		if ($this->_contiguous) {
369			$this->_contiguousRow = $currentRow;
370		}
371
372		ini_set('auto_detect_line_endings', $lineEnding);
373
374		// Return
375		return $objPHPExcel;
376	}	//	function loadIntoExisting()
377
378
379	/**
380	 * Get delimiter
381	 *
382	 * @access	public
383	 * @return string
384	 */
385	public function getDelimiter() {
386		return $this->_delimiter;
387	}	//	function getDelimiter()
388
389
390	/**
391	 * Set delimiter
392	 *
393	 * @access	public
394	 * @param	string	$pValue		Delimiter, defaults to ,
395	 * @return	PHPExcel_Reader_CSV
396	 */
397	public function setDelimiter($pValue = ',') {
398		$this->_delimiter = $pValue;
399		return $this;
400	}	//	function setDelimiter()
401
402
403	/**
404	 * Get enclosure
405	 *
406	 * @access	public
407	 * @return string
408	 */
409	public function getEnclosure() {
410		return $this->_enclosure;
411	}	//	function getEnclosure()
412
413
414	/**
415	 * Set enclosure
416	 *
417	 * @access	public
418	 * @param	string	$pValue		Enclosure, defaults to "
419	 * @return PHPExcel_Reader_CSV
420	 */
421	public function setEnclosure($pValue = '"') {
422		if ($pValue == '') {
423			$pValue = '"';
424		}
425		$this->_enclosure = $pValue;
426		return $this;
427	}	//	function setEnclosure()
428
429
430	/**
431	 * Get line ending
432	 *
433	 * @access	public
434	 * @return string
435	 */
436	public function getLineEnding() {
437		return $this->_lineEnding;
438	}	//	function getLineEnding()
439
440
441	/**
442	 * Set line ending
443	 *
444	 * @access	public
445	 * @param	string	$pValue		Line ending, defaults to OS line ending (PHP_EOL)
446	 * @return PHPExcel_Reader_CSV
447	 */
448	public function setLineEnding($pValue = PHP_EOL) {
449		$this->_lineEnding = $pValue;
450		return $this;
451	}	//	function setLineEnding()
452
453
454	/**
455	 * Get sheet index
456	 *
457	 * @access	public
458	 * @return int
459	 */
460	public function getSheetIndex() {
461		return $this->_sheetIndex;
462	}	//	function getSheetIndex()
463
464
465	/**
466	 * Set sheet index
467	 *
468	 * @access	public
469	 * @param	int		$pValue		Sheet index
470	 * @return PHPExcel_Reader_CSV
471	 */
472	public function setSheetIndex($pValue = 0) {
473		$this->_sheetIndex = $pValue;
474		return $this;
475	}	//	function setSheetIndex()
476
477
478	/**
479	 * Set Contiguous
480	 *
481	 * @access	public
482	 * @param string $pValue Input encoding
483	 */
484	public function setContiguous($contiguous = false)
485	{
486		$this->_contiguous = (bool)$contiguous;
487		if (!$contiguous) {
488			$this->_contiguousRow	= -1;
489		}
490
491		return $this;
492	}	//	function setInputEncoding()
493
494
495	/**
496	 * Get Contiguous
497	 *
498	 * @access	public
499	 * @return boolean
500	 */
501	public function getContiguous() {
502		return $this->_contiguous;
503	}	//	function getSheetIndex()
504
505}