From 5f301a72c1cde52c10f3d0b89bf0536de837c455 Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Fri, 25 May 2012 06:45:01 +0000 Subject: [PATCH] Additional validation on Worksheet name when adding/creating a new worksheet git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@90941 2327b42d-5241-43d6-9e2a-de5ac946f064 --- Classes/PHPExcel.php | 19 +++++++++++-- Classes/PHPExcel/Autoloader.php | 12 ++++---- Classes/PHPExcel/IOFactory.php | 39 ++++++++++++++----------- Classes/PHPExcel/Worksheet.php | 50 ++++++++++++++++++--------------- 4 files changed, 74 insertions(+), 46 deletions(-) diff --git a/Classes/PHPExcel.php b/Classes/PHPExcel.php index 70151f42..a78c5ce0 100644 --- a/Classes/PHPExcel.php +++ b/Classes/PHPExcel.php @@ -205,6 +205,17 @@ class PHPExcel return $newSheet; } + /** + * Chech if a sheet with a specified name already exists + * + * @param string $pSheetName Name of the worksheet to check + * @return boolean + */ + public function sheetNameExists($pSheetName) + { + return ($this->getSheetByName($pSheetName) !== NULL); + } + /** * Add sheet * @@ -213,8 +224,12 @@ class PHPExcel * @return PHPExcel_Worksheet * @throws Exception */ - public function addSheet(PHPExcel_Worksheet $pSheet = null, $iSheetIndex = null) + public function addSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) { + if ($this->sheetNameExists($pSheet->getTitle())) { + throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename this worksheet first."); + } + if($iSheetIndex === NULL) { $this->_workSheetCollection[] = $pSheet; } else { @@ -420,7 +435,7 @@ class PHPExcel * @return PHPExcel_Worksheet */ public function addExternalSheet(PHPExcel_Worksheet $pSheet, $iSheetIndex = null) { - if ($this->getSheetByName($pSheet->getTitle()) !== NULL) { + if ($this->sheetNameExists($pSheet->getTitle())) { throw new Exception("Workbook already contains a worksheet named '{$pSheet->getTitle()}'. Rename the external sheet first."); } diff --git a/Classes/PHPExcel/Autoloader.php b/Classes/PHPExcel/Autoloader.php index b11df94e..548994e8 100644 --- a/Classes/PHPExcel/Autoloader.php +++ b/Classes/PHPExcel/Autoloader.php @@ -26,6 +26,8 @@ */ PHPExcel_Autoloader::Register(); +// As we always try to run the autoloader before anything else, we can use it to do a few +// simple checks and initialisations PHPExcel_Shared_ZipStreamWrapper::register(); // check mbstring.func_overload if (ini_get('mbstring.func_overload') & 2) { @@ -68,16 +70,16 @@ class PHPExcel_Autoloader return FALSE; } - $pObjectFilePath = PHPEXCEL_ROOT . - str_replace('_',DIRECTORY_SEPARATOR,$pClassName) . - '.php'; + $pClassFilePath = PHPEXCEL_ROOT . + str_replace('_',DIRECTORY_SEPARATOR,$pClassName) . + '.php'; - if ((file_exists($pObjectFilePath) === false) || (is_readable($pObjectFilePath) === false)) { + if ((file_exists($pClassFilePath) === false) || (is_readable($pClassFilePath) === false)) { // Can't load return FALSE; } - require($pObjectFilePath); + require($pClassFilePath); } // function Load() } \ No newline at end of file diff --git a/Classes/PHPExcel/IOFactory.php b/Classes/PHPExcel/IOFactory.php index 369ed963..ad2d6fd4 100644 --- a/Classes/PHPExcel/IOFactory.php +++ b/Classes/PHPExcel/IOFactory.php @@ -70,6 +70,7 @@ class PHPExcel_IOFactory 'OOCalc', 'SYLK', 'Gnumeric', + 'HTML', 'CSV', ); @@ -136,7 +137,6 @@ class PHPExcel_IOFactory foreach (self::$_searchLocations as $searchLocation) { if ($searchLocation['type'] == $searchType) { $className = str_replace('{0}', $writerType, $searchLocation['class']); - $classFile = str_replace('{0}', $writerType, $searchLocation['path']); $instance = new $className($phpExcel); if ($instance !== NULL) { @@ -166,7 +166,6 @@ class PHPExcel_IOFactory foreach (self::$_searchLocations as $searchLocation) { if ($searchLocation['type'] == $searchType) { $className = str_replace('{0}', $readerType, $searchLocation['class']); - $classFile = str_replace('{0}', $readerType, $searchLocation['path']); $instance = new $className(); if ($instance !== NULL) { @@ -184,7 +183,7 @@ class PHPExcel_IOFactory * * @static * @access public - * @param string $pFileName + * @param string $pFileName The name of the spreadsheet file * @return PHPExcel * @throws Exception */ @@ -198,7 +197,7 @@ class PHPExcel_IOFactory * * @static * @access public - * @param string $pFileName + * @param string $pFileName The name of the spreadsheet file to identify * @return string * @throws Exception */ @@ -215,7 +214,7 @@ class PHPExcel_IOFactory * * @static * @access public - * @param string $pFileName + * @param string $pFileName The name of the spreadsheet file * @return PHPExcel_Reader_IReader * @throws Exception */ @@ -227,23 +226,27 @@ class PHPExcel_IOFactory if (isset($pathinfo['extension'])) { switch (strtolower($pathinfo['extension'])) { case 'xlsx': - $reader = self::createReader('Excel2007'); + $extensionType = 'Excel2007'; break; case 'xls': case 'xlsm': - $reader = self::createReader('Excel5'); + $extensionType = 'Excel5'; break; case 'ods': - $reader = self::createReader('OOCalc'); + $extensionType = 'OOCalc'; break; case 'slk': - $reader = self::createReader('SYLK'); + $extensionType = 'SYLK'; break; case 'xml': - $reader = self::createReader('Excel2003XML'); + $extensionType = 'Excel2003XML'; break; case 'gnumeric': - $reader = self::createReader('Gnumeric'); + $extensionType = 'Gnumeric'; + break; + case 'htm': + case 'html': + $extensionType = 'HTML'; break; case 'csv': // Do nothing @@ -254,6 +257,7 @@ class PHPExcel_IOFactory break; } + $reader = self::createReader($extensionType); // Let's see if we are lucky if (isset($reader) && $reader->canRead($pFilename)) { return $reader; @@ -262,14 +266,17 @@ class PHPExcel_IOFactory } // If we reach here then "lucky guess" didn't give any result - - // Try loading using self::$_autoResolveClasses + // Try walking through all the options in self::$_autoResolveClasses foreach (self::$_autoResolveClasses as $autoResolveClass) { - $reader = self::createReader($autoResolveClass); - if ($reader->canRead($pFilename)) { - return $reader; + // Ignore our original guess, we know that won't work + if ($reader !== $extensionType) { + $reader = self::createReader($autoResolveClass); + if ($reader->canRead($pFilename)) { + return $reader; + } } } + throw new Exception('Unable to identify a reader for this file'); } // function createReaderForFile() } diff --git a/Classes/PHPExcel/Worksheet.php b/Classes/PHPExcel/Worksheet.php index f54be5ad..eb90590f 100644 --- a/Classes/PHPExcel/Worksheet.php +++ b/Classes/PHPExcel/Worksheet.php @@ -790,39 +790,43 @@ class PHPExcel_Worksheet implements PHPExcel_IComparable // Old title $oldTitle = $this->getTitle(); - // Is there already such sheet name? - if ($this->getParent()->getSheetByName($pValue)) { - // Use name, but append with lowest possible integer + if ($this->getParent()) { + // Is there already such sheet name? + if ($this->getParent()->sheetNameExists($pValue)) { + // Use name, but append with lowest possible integer - if (PHPExcel_Shared_String::CountCharacters($pValue) > 29) { - $pValue = PHPExcel_Shared_String::Substring($pValue,0,29); - } - $i = 1; - while ($this->getParent()->getSheetByName($pValue . ' ' . $i)) { - ++$i; - if ($i == 10) { - if (PHPExcel_Shared_String::CountCharacters($pValue) > 28) { - $pValue = PHPExcel_Shared_String::Substring($pValue,0,28); - } - } elseif ($i == 100) { - if (PHPExcel_Shared_String::CountCharacters($pValue) > 27) { - $pValue = PHPExcel_Shared_String::Substring($pValue,0,27); + if (PHPExcel_Shared_String::CountCharacters($pValue) > 29) { + $pValue = PHPExcel_Shared_String::Substring($pValue,0,29); + } + $i = 1; + while ($this->getParent()->sheetNameExists($pValue . ' ' . $i)) { + ++$i; + if ($i == 10) { + if (PHPExcel_Shared_String::CountCharacters($pValue) > 28) { + $pValue = PHPExcel_Shared_String::Substring($pValue,0,28); + } + } elseif ($i == 100) { + if (PHPExcel_Shared_String::CountCharacters($pValue) > 27) { + $pValue = PHPExcel_Shared_String::Substring($pValue,0,27); + } } } - } - $altTitle = $pValue . ' ' . $i; - return $this->setTitle($altTitle,$updateFormulaCellReferences); + $altTitle = $pValue . ' ' . $i; + return $this->setTitle($altTitle,$updateFormulaCellReferences); + } } // Set title $this->_title = $pValue; $this->_dirty = true; - // New title - $newTitle = $this->getTitle(); - if ($updateFormulaCellReferences) - PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle); + if ($this->getParent()) { + // New title + $newTitle = $this->getTitle(); + if ($updateFormulaCellReferences) + PHPExcel_ReferenceHelper::getInstance()->updateNamedFormulas($this->getParent(), $oldTitle, $newTitle); + } return $this; }