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

Documentation generated on Wed, 22 Apr 2009 09:01:55 +0200 by phpDocumentor 1.4.1