PHPExcel_Reader
[ class tree: PHPExcel_Reader ] [ index: PHPExcel_Reader ] [ all elements ]

Source for file Serialized.php

Documentation is available at Serialized.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.4, 2010-08-26
  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.     // check mbstring.func_overload
  37.     if (ini_get('mbstring.func_overload'2{
  38.         throw new Exception('Multibyte function overloading in PHP must be disabled for string functions (2).');
  39.     }
  40. }
  41.  
  42. /**
  43.  * PHPExcel_Reader_Serialized
  44.  *
  45.  * @category   PHPExcel
  46.  * @package    PHPExcel_Reader
  47.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  48.  */
  49. class PHPExcel_Reader_Serialized implements PHPExcel_Reader_IReader
  50. {
  51.     /**
  52.      * Can the current PHPExcel_Reader_IReader read the file?
  53.      *
  54.      * @param     string         $pFileName 
  55.      * @return     boolean 
  56.      */
  57.     public function canRead($pFilename)
  58.     {
  59.         // Check if file exists
  60.         if (!file_exists($pFilename)) {
  61.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  62.         }
  63.  
  64.         return $this->fileSupportsUnserializePHPExcel($pFilename);
  65.     }
  66.  
  67.     /**
  68.      * Loads PHPExcel Serialized file
  69.      *
  70.      * @param     string         $pFilename 
  71.      * @return     PHPExcel 
  72.      * @throws     Exception
  73.      */
  74.     public function load($pFilename)
  75.     {
  76.         // Check if file exists
  77.         if (!file_exists($pFilename)) {
  78.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  79.         }
  80.  
  81.         // Unserialize... First make sure the file supports it!
  82.         if (!$this->fileSupportsUnserializePHPExcel($pFilename)) {
  83.             throw new Exception("Invalid file format for PHPExcel_Reader_Serialized: " $pFilename ".");
  84.         }
  85.  
  86.         return $this->_loadSerialized($pFilename);
  87.     }
  88.  
  89.     /**
  90.      * Load PHPExcel Serialized file
  91.      *
  92.      * @param     string         $pFilename 
  93.      * @return     PHPExcel 
  94.      */
  95.     private function _loadSerialized($pFilename{
  96.         $xmlData simplexml_load_string(file_get_contents("zip://$pFilename#phpexcel.xml"));
  97.         $excel unserialize(base64_decode((string)$xmlData->data));
  98.  
  99.         // Update media links
  100.         for ($i 0$i $excel->getSheetCount()++$i{
  101.             for ($j 0$j $excel->getSheet($i)->getDrawingCollection()->count()++$j{
  102.                 if ($excel->getSheet($i)->getDrawingCollection()->offsetGet($jinstanceof PHPExcl_Worksheet_BaseDrawing{
  103.                     $imgTemp =$excel->getSheet($i)->getDrawingCollection()->offsetGet($j);
  104.                     $imgTemp->setPath('zip://' $pFilename '#media/' $imgTemp->getFilename()false);
  105.                 }
  106.             }
  107.         }
  108.  
  109.         return $excel;
  110.     }
  111.  
  112.     /**
  113.      * Does a file support UnserializePHPExcel ?
  114.      *
  115.      * @param     string         $pFilename 
  116.      * @throws     Exception
  117.      * @return     boolean 
  118.      */
  119.     public function fileSupportsUnserializePHPExcel($pFilename ''{
  120.         // Check if file exists
  121.         if (!file_exists($pFilename)) {
  122.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  123.         }
  124.  
  125.         // File exists, does it contain phpexcel.xml?
  126.         return PHPExcel_Shared_File::file_exists("zip://$pFilename#phpexcel.xml");
  127.     }
  128. }

Documentation generated on Thu, 26 Aug 2010 17:44:18 +0200 by phpDocumentor 1.4.3