Rework of Iterators for existing cells only

This commit is contained in:
MarkBaker 2015-04-30 19:07:49 +01:00
parent 75bb9d7eda
commit 7ccb90064b
4 changed files with 104 additions and 168 deletions

View File

@ -35,47 +35,28 @@
* @package PHPExcel_Worksheet * @package PHPExcel_Worksheet
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/ */
class PHPExcel_Worksheet_CellIterator implements Iterator abstract class PHPExcel_Worksheet_CellIterator
{ {
/** /**
* PHPExcel_Worksheet to iterate * PHPExcel_Worksheet to iterate
* *
* @var PHPExcel_Worksheet * @var PHPExcel_Worksheet
*/ */
private $_subject; protected $_subject;
/**
* Row index
*
* @var int
*/
private $_rowIndex;
/** /**
* Current iterator position * Current iterator position
* *
* @var int * @var mixed
*/ */
private $_position = 0; protected $_position = null;
/** /**
* Loop only existing cells * Iterate only existing cells
* *
* @var boolean * @var boolean
*/ */
private $_onlyExistingCells = true; protected $_onlyExistingCells = false;
/**
* Create a new cell iterator
*
* @param PHPExcel_Worksheet $subject
* @param int $rowIndex
*/
public function __construct(PHPExcel_Worksheet $subject = null, $rowIndex = 1) {
// Set subject and row index
$this->_subject = $subject;
$this->_rowIndex = $rowIndex;
}
/** /**
* Destructor * Destructor
@ -84,63 +65,6 @@ class PHPExcel_Worksheet_CellIterator implements Iterator
unset($this->_subject); unset($this->_subject);
} }
/**
* Rewind iterator
*/
public function rewind() {
$this->_position = 0;
}
/**
* Current PHPExcel_Cell
*
* @return PHPExcel_Cell
*/
public function current() {
return $this->_subject->getCellByColumnAndRow($this->_position, $this->_rowIndex);
}
/**
* Current key
*
* @return int
*/
public function key() {
return $this->_position;
}
/**
* Next value
*/
public function next() {
++$this->_position;
}
/**
* Are there any more PHPExcel_Cell instances available?
*
* @return boolean
*/
public function valid() {
// columnIndexFromString() returns an index based at one,
// treat it as a count when comparing it to the base zero
// position.
$columnCount = PHPExcel_Cell::columnIndexFromString($this->_subject->getHighestColumn());
if ($this->_onlyExistingCells) {
// If we aren't looking at an existing cell, either
// because the first column doesn't exist or next() has
// been called onto a nonexistent cell, then loop until we
// find one, or pass the last column.
while ($this->_position < $columnCount &&
!$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) {
++$this->_position;
}
}
return $this->_position < $columnCount;
}
/** /**
* Get loop only existing cells * Get loop only existing cells
* *
@ -150,12 +74,23 @@ class PHPExcel_Worksheet_CellIterator implements Iterator
return $this->_onlyExistingCells; return $this->_onlyExistingCells;
} }
/**
* Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
*
* @throws PHPExcel_Exception
*/
abstract protected function adjustForExistingOnlyRange() {
}
/** /**
* Set the iterator to loop only existing cells * Set the iterator to loop only existing cells
* *
* @param boolean $value * @param boolean $value
* @throws PHPExcel_Exception
*/ */
public function setIterateOnlyExistingCells($value = true) { public function setIterateOnlyExistingCells($value = true) {
$this->_onlyExistingCells = $value; $this->_onlyExistingCells = (boolean) $value;
$this->adjustForExistingOnlyRange();
} }
} }

View File

@ -35,49 +35,28 @@
* @package PHPExcel_Worksheet * @package PHPExcel_Worksheet
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/ */
class PHPExcel_Worksheet_ColumnCellIterator implements Iterator class PHPExcel_Worksheet_ColumnCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator
{ {
/**
* PHPExcel_Worksheet to iterate
*
* @var PHPExcel_Worksheet
*/
private $_subject;
/** /**
* Column index * Column index
* *
* @var string * @var string
*/ */
private $_columnIndex; protected $_columnIndex;
/** /**
* Current iterator position
*
* @var int
*/
private $_position = 1;
/**
* Start position * Start position
* *
* @var int * @var int
*/ */
private $_startRow = 1; protected $_startRow = 1;
/** /**
* End position * End position
* *
* @var int * @var int
*/ */
private $_endRow = 1; protected $_endRow = 1;
/**
* Loop only existing cells
*
* @var boolean
*/
private $_onlyExistingCells = true;
/** /**
* Create a new row iterator * Create a new row iterator
@ -106,10 +85,12 @@ class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
* (Re)Set the start row and the current row pointer * (Re)Set the start row and the current row pointer
* *
* @param integer $startRow The row number at which to start iterating * @param integer $startRow The row number at which to start iterating
* @return PHPExcel_Worksheet_RowIterator * @return PHPExcel_Worksheet_ColumnCellIterator
* @throws PHPExcel_Exception
*/ */
public function resetStart($startRow = 1) { public function resetStart($startRow = 1) {
$this->_startRow = $startRow; $this->_startRow = $startRow;
$this->adjustForExistingOnlyRange();
$this->seek($startRow); $this->seek($startRow);
return $this; return $this;
@ -119,10 +100,12 @@ class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
* (Re)Set the end row * (Re)Set the end row
* *
* @param integer $endRow The row number at which to stop iterating * @param integer $endRow The row number at which to stop iterating
* @return PHPExcel_Worksheet_RowIterator * @return PHPExcel_Worksheet_ColumnCellIterator
* @throws PHPExcel_Exception
*/ */
public function resetEnd($endRow = null) { public function resetEnd($endRow = null) {
$this->_endRow = ($endRow) ? $endRow : $this->_subject->getHighestRow(); $this->_endRow = ($endRow) ? $endRow : $this->_subject->getHighestRow();
$this->adjustForExistingOnlyRange();
return $this; return $this;
} }
@ -131,12 +114,14 @@ class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
* Set the row pointer to the selected row * Set the row pointer to the selected row
* *
* @param integer $row The row number to set the current pointer at * @param integer $row The row number to set the current pointer at
* @return PHPExcel_Worksheet_RowIterator * @return PHPExcel_Worksheet_ColumnCellIterator
* @throws PHPExcel_Exception * @throws PHPExcel_Exception
*/ */
public function seek($row = 1) { public function seek($row = 1) {
if (($row < $this->_startRow) || ($row > $this->_endRow)) { if (($row < $this->_startRow) || ($row > $this->_endRow)) {
throw new PHPExcel_Exception("Row $row is out of range ({$this->_startRow} - {$this->_endRow})"); throw new PHPExcel_Exception("Row $row is out of range ({$this->_startRow} - {$this->_endRow})");
} elseif ($this->_onlyExistingCells && !($this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $row))) {
throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
} }
$this->_position = $row; $this->_position = $row;
@ -151,7 +136,7 @@ class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
} }
/** /**
* Return the current row in this worksheet * Return the current cell in this worksheet column
* *
* @return PHPExcel_Worksheet_Row * @return PHPExcel_Worksheet_Row
*/ */
@ -172,7 +157,11 @@ class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
* Set the iterator to its next value * Set the iterator to its next value
*/ */
public function next() { public function next() {
++$this->_position; do {
++$this->_position;
} while (($this->_onlyExistingCells) &&
(!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_position)) &&
($this->_position <= $this->_endRow));
} }
/** /**
@ -183,7 +172,11 @@ class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->_startRow} - {$this->_endRow})"); throw new PHPExcel_Exception("Row is already at the beginning of range ({$this->_startRow} - {$this->_endRow})");
} }
--$this->_position; do {
--$this->_position;
} while (($this->_onlyExistingCells) &&
(!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_position)) &&
($this->_position >= $this->_startRow));
} }
/** /**
@ -196,20 +189,27 @@ class PHPExcel_Worksheet_ColumnCellIterator implements Iterator
} }
/** /**
* Get loop only existing cells * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
* *
* @return boolean * @throws PHPExcel_Exception
*/ */
public function getIterateOnlyExistingCells() { protected function adjustForExistingOnlyRange() {
return $this->_onlyExistingCells; if ($this->_onlyExistingCells) {
while ((!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_startRow)) &&
($this->_startRow <= $this->_endRow)) {
++$this->_startRow;
}
if ($this->_startRow > $this->_endRow) {
throw new PHPExcel_Exception('No cells exist within the specified range');
}
while ((!$this->_subject->cellExistsByColumnAndRow($this->_columnIndex, $this->_endRow)) &&
($this->_endRow >= $this->_startRow)) {
--$this->_endRow;
}
if ($this->_endRow < $this->_startRow) {
throw new PHPExcel_Exception('No cells exist within the specified range');
}
}
} }
/**
* Set the iterator to loop only existing cells
*
* @param boolean $value
*/
public function setIterateOnlyExistingCells($value = true) {
$this->_onlyExistingCells = $value;
}
} }

View File

@ -35,49 +35,28 @@
* @package PHPExcel_Worksheet * @package PHPExcel_Worksheet
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/ */
class PHPExcel_Worksheet_RowCellIterator implements Iterator class PHPExcel_Worksheet_RowCellIterator extends PHPExcel_Worksheet_CellIterator implements Iterator
{ {
/**
* PHPExcel_Worksheet to iterate in
*
* @var PHPExcel_Worksheet
*/
private $_subject;
/** /**
* Row index * Row index
* *
* @var int * @var int
*/ */
private $_rowIndex; protected $_rowIndex;
/**
* Current iterator position
*
* @var int
*/
private $_position = 0;
/** /**
* Start position * Start position
* *
* @var int * @var int
*/ */
private $_startColumn = 0; protected $_startColumn = 0;
/** /**
* End position * End position
* *
* @var int * @var int
*/ */
private $_endColumn = 0; protected $_endColumn = 0;
/**
* Loop only existing cells
*
* @var boolean
*/
private $_onlyExistingCells = true;
/** /**
* Create a new column iterator * Create a new column iterator
@ -107,11 +86,13 @@ class PHPExcel_Worksheet_RowCellIterator implements Iterator
* *
* @param integer $startColumn The column address at which to start iterating * @param integer $startColumn The column address at which to start iterating
* @return PHPExcel_Worksheet_RowCellIterator * @return PHPExcel_Worksheet_RowCellIterator
* @throws PHPExcel_Exception
*/ */
public function resetStart($startColumn = 'A') { public function resetStart($startColumn = 'A') {
$startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1; $startColumnIndex = PHPExcel_Cell::columnIndexFromString($startColumn) - 1;
$this->_startColumn = $startColumnIndex; $this->_startColumn = $startColumnIndex;
$this->seek($startColumn); $this->adjustForExistingOnlyRange();
$this->seek(PHPExcel_Cell::stringFromColumnIndex($this->_startColumn));
return $this; return $this;
} }
@ -121,10 +102,12 @@ class PHPExcel_Worksheet_RowCellIterator implements Iterator
* *
* @param string $endColumn The column address at which to stop iterating * @param string $endColumn The column address at which to stop iterating
* @return PHPExcel_Worksheet_RowCellIterator * @return PHPExcel_Worksheet_RowCellIterator
* @throws PHPExcel_Exception
*/ */
public function resetEnd($endColumn = null) { public function resetEnd($endColumn = null) {
$endColumn = ($endColumn) ? $endColumn : $this->_subject->getHighestColumn(); $endColumn = ($endColumn) ? $endColumn : $this->_subject->getHighestColumn();
$this->_endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1; $this->_endColumn = PHPExcel_Cell::columnIndexFromString($endColumn) - 1;
$this->adjustForExistingOnlyRange();
return $this; return $this;
} }
@ -140,6 +123,8 @@ class PHPExcel_Worksheet_RowCellIterator implements Iterator
$column = PHPExcel_Cell::columnIndexFromString($column) - 1; $column = PHPExcel_Cell::columnIndexFromString($column) - 1;
if (($column < $this->_startColumn) || ($column > $this->_endColumn)) { if (($column < $this->_startColumn) || ($column > $this->_endColumn)) {
throw new PHPExcel_Exception("Column $column is out of range ({$this->_startColumn} - {$this->_endColumn})"); throw new PHPExcel_Exception("Column $column is out of range ({$this->_startColumn} - {$this->_endColumn})");
} elseif ($this->_onlyExistingCells && !($this->_subject->cellExistsByColumnAndRow($column, $this->_rowIndex))) {
throw new PHPExcel_Exception('In "IterateOnlyExistingCells" mode and Cell does not exist');
} }
$this->_position = $column; $this->_position = $column;
@ -175,7 +160,11 @@ class PHPExcel_Worksheet_RowCellIterator implements Iterator
* Set the iterator to its next value * Set the iterator to its next value
*/ */
public function next() { public function next() {
++$this->_position; do {
++$this->_position;
} while (($this->_onlyExistingCells) &&
(!$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) &&
($this->_position <= $this->_endColumn));
} }
/** /**
@ -192,7 +181,11 @@ class PHPExcel_Worksheet_RowCellIterator implements Iterator
); );
} }
--$this->_position; do {
--$this->_position;
} while (($this->_onlyExistingCells) &&
(!$this->_subject->cellExistsByColumnAndRow($this->_position, $this->_rowIndex)) &&
($this->_position >= $this->_startColumn));
} }
/** /**
@ -205,20 +198,27 @@ class PHPExcel_Worksheet_RowCellIterator implements Iterator
} }
/** /**
* Get loop only existing cells * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary
* *
* @return boolean * @throws PHPExcel_Exception
*/ */
public function getIterateOnlyExistingCells() { protected function adjustForExistingOnlyRange() {
return $this->_onlyExistingCells; if ($this->_onlyExistingCells) {
while ((!$this->_subject->cellExistsByColumnAndRow($this->_startColumn, $this->_rowIndex)) &&
($this->_startColumn <= $this->_endColumn)) {
++$this->_startColumn;
}
if ($this->_startColumn > $this->_endColumn) {
throw new PHPExcel_Exception('No cells exist within the specified range');
}
while ((!$this->_subject->cellExistsByColumnAndRow($this->_endColumn, $this->_rowIndex)) &&
($this->_endColumn >= $this->_startColumn)) {
--$this->_endColumn;
}
if ($this->_endColumn < $this->_startColumn) {
throw new PHPExcel_Exception('No cells exist within the specified range');
}
}
} }
/**
* Set the iterator to loop only existing cells
*
* @param boolean $value
*/
public function setIterateOnlyExistingCells($value = true) {
$this->_onlyExistingCells = $value;
}
} }

View File

@ -58,9 +58,10 @@ Planned for v1.8.1
Improved handling for next/prev Improved handling for next/prev
- Security: (MBaker) - XML filescan in XML-based Readers to prevent XML Entity Expansion (XEE) - Security: (MBaker) - XML filescan in XML-based Readers to prevent XML Entity Expansion (XEE)
(see http://projects.webappsec.org/w/page/13247002/XML%20Entity%20Expansion for an explanation of XEE injection) attacks (see http://projects.webappsec.org/w/page/13247002/XML%20Entity%20Expansion for an explanation of XEE injection) attacks
Reference CVE-2015-3542 - Identification of problem courtesy of Dawid Golunski (Pentest Ltd.)
2014-03-02 (v1.8.0): 2014-03-02 (v1.8.0):
- Bugfix: (MBaker) Work item CP19830 - Undefined variable: fileHandle in CSV Reader - Bugfix: (MBaker) Work item CP19830 - Undefined variable: fileHandle in CSV Reader
- Bugfix: (MBaker) Work item CP19968 - Out of memory in style/supervisor.php - Bugfix: (MBaker) Work item CP19968 - Out of memory in style/supervisor.php
- Bugfix: (MBaker) - Style error with merged cells in PDF Writer - Bugfix: (MBaker) - Style error with merged cells in PDF Writer