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.2, 2010-01-11
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../../');
  35. }
  36.  
  37. /** PHPExcel */
  38. require_once PHPEXCEL_ROOT 'PHPExcel.php';
  39.  
  40. /** PHPExcel_Reader_IReader */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/IReader.php';
  42.  
  43. /** PHPExcel_Shared_File */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Shared/File.php';
  45.  
  46.  
  47. /**
  48.  * PHPExcel_Reader_Serialized
  49.  *
  50.  * @category   PHPExcel
  51.  * @package    PHPExcel_Reader
  52.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  53.  */
  54. class PHPExcel_Reader_Serialized implements PHPExcel_Reader_IReader
  55. {
  56.     /**
  57.      * Can the current PHPExcel_Reader_IReader read the file?
  58.      *
  59.      * @param     string         $pFileName 
  60.      * @return     boolean 
  61.      */    
  62.     public function canRead($pFilename
  63.     {
  64.         // Check if file exists
  65.         if (!file_exists($pFilename)) {
  66.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  67.         }
  68.         
  69.         return $this->fileSupportsUnserializePHPExcel($pFilename);
  70.     }
  71.     
  72.     /**
  73.      * Loads PHPExcel Serialized file
  74.      *
  75.      * @param     string         $pFilename 
  76.      * @return     PHPExcel 
  77.      * @throws     Exception
  78.      */
  79.     public function load($pFilename)
  80.     {
  81.         // Check if file exists
  82.         if (!file_exists($pFilename)) {
  83.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  84.         }
  85.  
  86.         // Unserialize... First make sure the file supports it!
  87.         if (!$this->fileSupportsUnserializePHPExcel($pFilename)) {
  88.             throw new Exception("Invalid file format for PHPExcel_Reader_Serialized: " $pFilename ".");
  89.         }
  90.  
  91.         return $this->_loadSerialized($pFilename);
  92.     }
  93.  
  94.     /**
  95.      * Load PHPExcel Serialized file
  96.      *
  97.      * @param     string         $pFilename 
  98.      * @return     PHPExcel 
  99.      */
  100.     private function _loadSerialized($pFilename{
  101.         $xmlData simplexml_load_string(file_get_contents("zip://$pFilename#phpexcel.xml"));
  102.         $excel unserialize(base64_decode((string)$xmlData->data));
  103.  
  104.         // Update media links
  105.         for ($i 0$i $excel->getSheetCount()++$i{
  106.             for ($j 0$j $excel->getSheet($i)->getDrawingCollection()->count()++$j{
  107.                 if ($excel->getSheet($i)->getDrawingCollection()->offsetGet($jinstanceof PHPExcl_Worksheet_BaseDrawing{
  108.                     $imgTemp =$excel->getSheet($i)->getDrawingCollection()->offsetGet($j);
  109.                     $imgTemp->setPath('zip://' $pFilename '#media/' $imgTemp->getFilename()false);
  110.                 }
  111.             }
  112.         }
  113.  
  114.         return $excel;
  115.     }
  116.  
  117.     /**
  118.      * Does a file support UnserializePHPExcel ?
  119.      *
  120.      * @param     string         $pFilename 
  121.      * @throws     Exception
  122.      * @return     boolean 
  123.      */
  124.     public function fileSupportsUnserializePHPExcel($pFilename ''{
  125.         // Check if file exists
  126.         if (!file_exists($pFilename)) {
  127.             throw new Exception("Could not open " $pFilename " for reading! File does not exist.");
  128.         }
  129.  
  130.         // File exists, does it contain phpexcel.xml?
  131.         return PHPExcel_Shared_File::file_exists("zip://$pFilename#phpexcel.xml");
  132.     }
  133. }

Documentation generated on Mon, 11 Jan 2010 08:13:42 +0100 by phpDocumentor 1.4.1