PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Reader/CSV.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 446 lines | 172 code | 60 blank | 214 comment | 25 complexity | 75421b37b04ece5f853b5d8321f5c568 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /** PHPExcel root directory */
  28. if (! defined('PHPEXCEL_ROOT'))
  29. {
  30. /**
  31. * @ignore
  32. */
  33. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  34. require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  35. }
  36. /**
  37. * PHPExcel_Reader_CSV
  38. *
  39. * @category PHPExcel
  40. * @package PHPExcel_Reader
  41. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  42. */
  43. class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
  44. {
  45. /**
  46. * Input encoding
  47. *
  48. * @access private
  49. * @var string
  50. */
  51. private $_inputEncoding = 'UTF-8';
  52. /**
  53. * Delimiter
  54. *
  55. * @access private
  56. * @var string
  57. */
  58. private $_delimiter = ',';
  59. /**
  60. * Enclosure
  61. *
  62. * @access private
  63. * @var string
  64. */
  65. private $_enclosure = '"';
  66. /**
  67. * Line ending
  68. *
  69. * @access private
  70. * @var string
  71. */
  72. private $_lineEnding = PHP_EOL;
  73. /**
  74. * Sheet index to read
  75. *
  76. * @access private
  77. * @var int
  78. */
  79. private $_sheetIndex = 0;
  80. /**
  81. * Load rows contiguously
  82. *
  83. * @access private
  84. * @var int
  85. */
  86. private $_contiguous = false;
  87. /**
  88. * Row counter for loading rows contiguously
  89. *
  90. * @access private
  91. * @var int
  92. */
  93. private $_contiguousRow = - 1;
  94. /**
  95. * PHPExcel_Reader_IReadFilter instance
  96. *
  97. * @access private
  98. * @var PHPExcel_Reader_IReadFilter
  99. */
  100. private $_readFilter = null;
  101. /**
  102. * Create a new PHPExcel_Reader_CSV
  103. */
  104. public function __construct()
  105. {
  106. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  107. } // function __construct()
  108. /**
  109. * Can the current PHPExcel_Reader_IReader read the file?
  110. *
  111. * @access public
  112. * @param string $pFileName
  113. * @return boolean
  114. * @throws Exception
  115. */
  116. public function canRead($pFilename)
  117. {
  118. // Check if file exists
  119. if (! file_exists($pFilename))
  120. {
  121. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  122. }
  123. return true;
  124. } // function canRead()
  125. /**
  126. * Loads PHPExcel from file
  127. *
  128. * @access public
  129. * @param string $pFilename
  130. * @return PHPExcel
  131. * @throws Exception
  132. */
  133. public function load($pFilename)
  134. {
  135. // Create new PHPExcel
  136. $objPHPExcel = new PHPExcel();
  137. // Load into this instance
  138. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  139. } // function load()
  140. /**
  141. * Read filter
  142. *
  143. * @access public
  144. * @return PHPExcel_Reader_IReadFilter
  145. */
  146. public function getReadFilter()
  147. {
  148. return $this->_readFilter;
  149. } // function getReadFilter()
  150. /**
  151. * Set read filter
  152. *
  153. * @access public
  154. * @param PHPExcel_Reader_IReadFilter $pValue
  155. */
  156. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue)
  157. {
  158. $this->_readFilter = $pValue;
  159. return $this;
  160. } // function setReadFilter()
  161. /**
  162. * Set input encoding
  163. *
  164. * @access public
  165. * @param string $pValue Input encoding
  166. */
  167. public function setInputEncoding($pValue = 'UTF-8')
  168. {
  169. $this->_inputEncoding = $pValue;
  170. return $this;
  171. } // function setInputEncoding()
  172. /**
  173. * Get input encoding
  174. *
  175. * @access public
  176. * @return string
  177. */
  178. public function getInputEncoding()
  179. {
  180. return $this->_inputEncoding;
  181. } // function getInputEncoding()
  182. /**
  183. * Loads PHPExcel from file into PHPExcel instance
  184. *
  185. * @access public
  186. * @param string $pFilename
  187. * @param PHPExcel $objPHPExcel
  188. * @return PHPExcel
  189. * @throws Exception
  190. */
  191. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  192. {
  193. // Check if file exists
  194. if (! file_exists($pFilename))
  195. {
  196. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  197. }
  198. // Create new PHPExcel
  199. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex)
  200. {
  201. $objPHPExcel->createSheet();
  202. }
  203. $objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
  204. // Open file
  205. $fileHandle = fopen($pFilename, 'r');
  206. if ($fileHandle === false)
  207. {
  208. throw new Exception("Could not open file $pFilename for reading.");
  209. }
  210. // Skip BOM, if any
  211. switch ($this->_inputEncoding)
  212. {
  213. case 'UTF-8' :
  214. fgets($fileHandle, 4) == "\xEF\xBB\xBF" ? fseek($fileHandle, 3) : fseek($fileHandle, 0);
  215. break;
  216. case 'UTF-16LE' :
  217. fgets($fileHandle, 3) == "\xFF\xFE" ? fseek($fileHandle, 2) : fseek($fileHandle, 0);
  218. break;
  219. case 'UTF-16BE' :
  220. fgets($fileHandle, 3) == "\xFE\xFF" ? fseek($fileHandle, 2) : fseek($fileHandle, 0);
  221. break;
  222. case 'UTF-32LE' :
  223. fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ? fseek($fileHandle, 4) : fseek($fileHandle, 0);
  224. break;
  225. case 'UTF-32BE' :
  226. fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ? fseek($fileHandle, 4) : fseek($fileHandle, 0);
  227. break;
  228. default :
  229. break;
  230. }
  231. $escapeEnclosures = array("\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure);
  232. // Set our starting row based on whether we're in contiguous mode or not
  233. $currentRow = 1;
  234. if ($this->_contiguous)
  235. {
  236. $currentRow = ($this->_contiguousRow == - 1) ? $objPHPExcel->getActiveSheet()->getHighestRow() : $this->_contiguousRow;
  237. }
  238. // Loop through each line of the file in turn
  239. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE)
  240. {
  241. $columnLetter = 'A';
  242. foreach ($rowData as $rowDatum)
  243. {
  244. if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow))
  245. {
  246. // Unescape enclosures
  247. $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
  248. // Convert encoding if necessary
  249. if ($this->_inputEncoding !== 'UTF-8')
  250. {
  251. $rowDatum = PHPExcel_Shared_String :: ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
  252. }
  253. // Set cell value
  254. $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowDatum);
  255. }
  256. ++ $columnLetter;
  257. }
  258. ++ $currentRow;
  259. }
  260. // Close file
  261. fclose($fileHandle);
  262. if ($this->_contiguous)
  263. {
  264. $this->_contiguousRow = $currentRow;
  265. }
  266. // Return
  267. return $objPHPExcel;
  268. } // function loadIntoExisting()
  269. /**
  270. * Get delimiter
  271. *
  272. * @access public
  273. * @return string
  274. */
  275. public function getDelimiter()
  276. {
  277. return $this->_delimiter;
  278. } // function getDelimiter()
  279. /**
  280. * Set delimiter
  281. *
  282. * @access public
  283. * @param string $pValue Delimiter, defaults to ,
  284. * @return PHPExcel_Reader_CSV
  285. */
  286. public function setDelimiter($pValue = ',')
  287. {
  288. $this->_delimiter = $pValue;
  289. return $this;
  290. } // function setDelimiter()
  291. /**
  292. * Get enclosure
  293. *
  294. * @access public
  295. * @return string
  296. */
  297. public function getEnclosure()
  298. {
  299. return $this->_enclosure;
  300. } // function getEnclosure()
  301. /**
  302. * Set enclosure
  303. *
  304. * @access public
  305. * @param string $pValue Enclosure, defaults to "
  306. * @return PHPExcel_Reader_CSV
  307. */
  308. public function setEnclosure($pValue = '"')
  309. {
  310. if ($pValue == '')
  311. {
  312. $pValue = '"';
  313. }
  314. $this->_enclosure = $pValue;
  315. return $this;
  316. } // function setEnclosure()
  317. /**
  318. * Get line ending
  319. *
  320. * @access public
  321. * @return string
  322. */
  323. public function getLineEnding()
  324. {
  325. return $this->_lineEnding;
  326. } // function getLineEnding()
  327. /**
  328. * Set line ending
  329. *
  330. * @access public
  331. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  332. * @return PHPExcel_Reader_CSV
  333. */
  334. public function setLineEnding($pValue = PHP_EOL)
  335. {
  336. $this->_lineEnding = $pValue;
  337. return $this;
  338. } // function setLineEnding()
  339. /**
  340. * Get sheet index
  341. *
  342. * @access public
  343. * @return int
  344. */
  345. public function getSheetIndex()
  346. {
  347. return $this->_sheetIndex;
  348. } // function getSheetIndex()
  349. /**
  350. * Set sheet index
  351. *
  352. * @access public
  353. * @param int $pValue Sheet index
  354. * @return PHPExcel_Reader_CSV
  355. */
  356. public function setSheetIndex($pValue = 0)
  357. {
  358. $this->_sheetIndex = $pValue;
  359. return $this;
  360. } // function setSheetIndex()
  361. /**
  362. * Set Contiguous
  363. *
  364. * @access public
  365. * @param string $pValue Input encoding
  366. */
  367. public function setContiguous($contiguous = false)
  368. {
  369. $this->_contiguous = (bool) $contiguous;
  370. if (! $contiguous)
  371. {
  372. $this->_contiguousRow = - 1;
  373. }
  374. return $this;
  375. } // function setInputEncoding()
  376. /**
  377. * Get Contiguous
  378. *
  379. * @access public
  380. * @return boolean
  381. */
  382. public function getContiguous()
  383. {
  384. return $this->_contiguous;
  385. } // function getSheetIndex()
  386. }