| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHPExcel |
| 4 |
* |
| 5 |
* Copyright (c) 2006 - 2014 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 |
| 23 |
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
| 24 |
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
| 25 |
* @version ##VERSION##, ##DATE## |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
/** PHPExcel root directory */ |
| 30 |
if (!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_IOFactory |
| 40 |
* |
| 41 |
* @category PHPExcel |
| 42 |
* @package PHPExcel |
| 43 |
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel) |
| 44 |
*/ |
| 45 |
class PHPExcel_IOFactory |
| 46 |
{ |
| 47 |
/** |
| 48 |
* Search locations |
| 49 |
* |
| 50 |
* @var array |
| 51 |
* @access private |
| 52 |
* @static |
| 53 |
*/ |
| 54 |
private static $_searchLocations = array( |
| 55 |
array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ), |
| 56 |
array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' ) |
| 57 |
); |
| 58 |
|
| 59 |
/** |
| 60 |
* Autoresolve classes |
| 61 |
* |
| 62 |
* @var array |
| 63 |
* @access private |
| 64 |
* @static |
| 65 |
*/ |
| 66 |
private static $_autoResolveClasses = array( |
| 67 |
'Excel2007', |
| 68 |
'Excel5', |
| 69 |
'Excel2003XML', |
| 70 |
'OOCalc', |
| 71 |
'SYLK', |
| 72 |
'Gnumeric', |
| 73 |
'HTML', |
| 74 |
'CSV', |
| 75 |
); |
| 76 |
|
| 77 |
/** |
| 78 |
* Private constructor for PHPExcel_IOFactory |
| 79 |
*/ |
| 80 |
private function __construct() { } |
| 81 |
|
| 82 |
/** |
| 83 |
* Get search locations |
| 84 |
* |
| 85 |
* @static |
| 86 |
* @access public |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public static function getSearchLocations() { |
| 90 |
return self::$_searchLocations; |
| 91 |
} // function getSearchLocations() |
| 92 |
|
| 93 |
/** |
| 94 |
* Set search locations |
| 95 |
* |
| 96 |
* @static |
| 97 |
* @access public |
| 98 |
* @param array $value |
| 99 |
* @throws PHPExcel_Reader_Exception |
| 100 |
*/ |
| 101 |
public static function setSearchLocations($value) { |
| 102 |
if (is_array($value)) { |
| 103 |
self::$_searchLocations = $value; |
| 104 |
} else { |
| 105 |
throw new PHPExcel_Reader_Exception('Invalid parameter passed.'); |
| 106 |
} |
| 107 |
} // function setSearchLocations() |
| 108 |
|
| 109 |
/** |
| 110 |
* Add search location |
| 111 |
* |
| 112 |
* @static |
| 113 |
* @access public |
| 114 |
* @param string $type Example: IWriter |
| 115 |
* @param string $location Example: PHPExcel/Writer/{0}.php |
| 116 |
* @param string $classname Example: PHPExcel_Writer_{0} |
| 117 |
*/ |
| 118 |
public static function addSearchLocation($type = '', $location = '', $classname = '') { |
| 119 |
self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname ); |
| 120 |
} // function addSearchLocation() |
| 121 |
|
| 122 |
/** |
| 123 |
* Create PHPExcel_Writer_IWriter |
| 124 |
* |
| 125 |
* @static |
| 126 |
* @access public |
| 127 |
* @param PHPExcel $phpExcel |
| 128 |
* @param string $writerType Example: Excel2007 |
| 129 |
* @return PHPExcel_Writer_IWriter |
| 130 |
* @throws PHPExcel_Reader_Exception |
| 131 |
*/ |
| 132 |
public static function createWriter(PHPExcel $phpExcel, $writerType = '') { |
| 133 |
// Search type |
| 134 |
$searchType = 'IWriter'; |
| 135 |
|
| 136 |
// Include class |
| 137 |
foreach (self::$_searchLocations as $searchLocation) { |
| 138 |
if ($searchLocation['type'] == $searchType) { |
| 139 |
$className = str_replace('{0}', $writerType, $searchLocation['class']); |
| 140 |
|
| 141 |
$instance = new $className($phpExcel); |
| 142 |
if ($instance !== NULL) { |
| 143 |
return $instance; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
// Nothing found... |
| 149 |
throw new PHPExcel_Reader_Exception("No $searchType found for type $writerType"); |
| 150 |
} // function createWriter() |
| 151 |
|
| 152 |
/** |
| 153 |
* Create PHPExcel_Reader_IReader |
| 154 |
* |
| 155 |
* @static |
| 156 |
* @access public |
| 157 |
* @param string $readerType Example: Excel2007 |
| 158 |
* @return PHPExcel_Reader_IReader |
| 159 |
* @throws PHPExcel_Reader_Exception |
| 160 |
*/ |
| 161 |
public static function createReader($readerType = '') { |
| 162 |
// Search type |
| 163 |
$searchType = 'IReader'; |
| 164 |
|
| 165 |
// Include class |
| 166 |
foreach (self::$_searchLocations as $searchLocation) { |
| 167 |
if ($searchLocation['type'] == $searchType) { |
| 168 |
$className = str_replace('{0}', $readerType, $searchLocation['class']); |
| 169 |
|
| 170 |
$instance = new $className(); |
| 171 |
if ($instance !== NULL) { |
| 172 |
return $instance; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// Nothing found... |
| 178 |
throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType"); |
| 179 |
} // function createReader() |
| 180 |
|
| 181 |
/** |
| 182 |
* Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution |
| 183 |
* |
| 184 |
* @static |
| 185 |
* @access public |
| 186 |
* @param string $pFilename The name of the spreadsheet file |
| 187 |
* @return PHPExcel |
| 188 |
* @throws PHPExcel_Reader_Exception |
| 189 |
*/ |
| 190 |
public static function load($pFilename) { |
| 191 |
$reader = self::createReaderForFile($pFilename); |
| 192 |
return $reader->load($pFilename); |
| 193 |
} // function load() |
| 194 |
|
| 195 |
/** |
| 196 |
* Identify file type using automatic PHPExcel_Reader_IReader resolution |
| 197 |
* |
| 198 |
* @static |
| 199 |
* @access public |
| 200 |
* @param string $pFilename The name of the spreadsheet file to identify |
| 201 |
* @return string |
| 202 |
* @throws PHPExcel_Reader_Exception |
| 203 |
*/ |
| 204 |
public static function identify($pFilename) { |
| 205 |
$reader = self::createReaderForFile($pFilename); |
| 206 |
$className = get_class($reader); |
| 207 |
$classType = explode('_',$className); |
| 208 |
unset($reader); |
| 209 |
return array_pop($classType); |
| 210 |
} // function identify() |
| 211 |
|
| 212 |
/** |
| 213 |
* Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution |
| 214 |
* |
| 215 |
* @static |
| 216 |
* @access public |
| 217 |
* @param string $pFilename The name of the spreadsheet file |
| 218 |
* @return PHPExcel_Reader_IReader |
| 219 |
* @throws PHPExcel_Reader_Exception |
| 220 |
*/ |
| 221 |
public static function createReaderForFile($pFilename) { |
| 222 |
|
| 223 |
// First, lucky guess by inspecting file extension |
| 224 |
$pathinfo = pathinfo($pFilename); |
| 225 |
|
| 226 |
$extensionType = NULL; |
| 227 |
if (isset($pathinfo['extension'])) { |
| 228 |
switch (strtolower($pathinfo['extension'])) { |
| 229 |
case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet |
| 230 |
case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded) |
| 231 |
case 'xltx': // Excel (OfficeOpenXML) Template |
| 232 |
case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded) |
| 233 |
$extensionType = 'Excel2007'; |
| 234 |
break; |
| 235 |
case 'xls': // Excel (BIFF) Spreadsheet |
| 236 |
case 'xlt': // Excel (BIFF) Template |
| 237 |
$extensionType = 'Excel5'; |
| 238 |
break; |
| 239 |
case 'ods': // Open/Libre Offic Calc |
| 240 |
case 'ots': // Open/Libre Offic Calc Template |
| 241 |
$extensionType = 'OOCalc'; |
| 242 |
break; |
| 243 |
case 'slk': |
| 244 |
$extensionType = 'SYLK'; |
| 245 |
break; |
| 246 |
case 'xml': // Excel 2003 SpreadSheetML |
| 247 |
$extensionType = 'Excel2003XML'; |
| 248 |
break; |
| 249 |
case 'gnumeric': |
| 250 |
$extensionType = 'Gnumeric'; |
| 251 |
break; |
| 252 |
case 'htm': |
| 253 |
case 'html': |
| 254 |
$extensionType = 'HTML'; |
| 255 |
break; |
| 256 |
case 'csv': |
| 257 |
// Do nothing |
| 258 |
// We must not try to use CSV reader since it loads |
| 259 |
// all files including Excel files etc. |
| 260 |
break; |
| 261 |
default: |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
if ($extensionType !== NULL) { |
| 266 |
$reader = self::createReader($extensionType); |
| 267 |
// Let's see if we are lucky |
| 268 |
if (isset($reader) && $reader->canRead($pFilename)) { |
| 269 |
return $reader; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
// If we reach here then "lucky guess" didn't give any result |
| 275 |
// Try walking through all the options in self::$_autoResolveClasses |
| 276 |
foreach (self::$_autoResolveClasses as $autoResolveClass) { |
| 277 |
// Ignore our original guess, we know that won't work |
| 278 |
if ($autoResolveClass !== $extensionType) { |
| 279 |
$reader = self::createReader($autoResolveClass); |
| 280 |
if ($reader->canRead($pFilename)) { |
| 281 |
return $reader; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
throw new PHPExcel_Reader_Exception('Unable to identify a reader for this file'); |
| 287 |
} // function createReaderForFile() |
| 288 |
} |
| 289 |
|