web/enmi/Zend/Pdf/FileParserDataSource/File.php
changeset 19 1c2f13fd785c
parent 0 4eba9c11703f
equal deleted inserted replaced
18:bd595ad770fc 19:1c2f13fd785c
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Pdf
       
    17  * @subpackage FileParser
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: File.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /** Zend_Pdf_FileParserDataSource */
       
    24 require_once 'Zend/Pdf/FileParserDataSource.php';
       
    25 
       
    26 
       
    27 /**
       
    28  * Concrete subclass of {@link Zend_Pdf_FileParserDataSource} that provides an
       
    29  * interface to filesystem objects.
       
    30  *
       
    31  * Note that this class cannot be used for other sources that may be supported
       
    32  * by {@link fopen()} (through URL wrappers). It may be used for local
       
    33  * filesystem objects only.
       
    34  *
       
    35  * @package    Zend_Pdf
       
    36  * @subpackage FileParser
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_Pdf_FileParserDataSource_File extends Zend_Pdf_FileParserDataSource
       
    41 {
       
    42   /**** Instance Variables ****/
       
    43 
       
    44 
       
    45     /**
       
    46      * Fully-qualified path to the file.
       
    47      * @var string
       
    48      */
       
    49     protected $_filePath = '';
       
    50 
       
    51     /**
       
    52      * File resource handle .
       
    53      * @var resource
       
    54      */
       
    55     protected $_fileResource = null;
       
    56 
       
    57 
       
    58 
       
    59   /**** Public Interface ****/
       
    60 
       
    61 
       
    62   /* Concrete Class Implementation */
       
    63 
       
    64     /**
       
    65      * Object constructor.
       
    66      *
       
    67      * Validates the path to the file, ensures that it is readable, then opens
       
    68      * it for reading.
       
    69      *
       
    70      * Throws an exception if the file is missing or cannot be opened.
       
    71      *
       
    72      * @param string $filePath Fully-qualified path to the file.
       
    73      * @throws Zend_Pdf_Exception
       
    74      */
       
    75     public function __construct($filePath)
       
    76     {
       
    77         if (! (is_file($filePath) || is_link($filePath))) {
       
    78             require_once 'Zend/Pdf/Exception.php';
       
    79             throw new Zend_Pdf_Exception("Invalid file path: $filePath",
       
    80                                          Zend_Pdf_Exception::BAD_FILE_PATH);
       
    81         }
       
    82         if (! is_readable($filePath)) {
       
    83             require_once 'Zend/Pdf/Exception.php';
       
    84             throw new Zend_Pdf_Exception("File is not readable: $filePath",
       
    85                                          Zend_Pdf_Exception::NOT_READABLE);
       
    86         }
       
    87         if (($this->_size = @filesize($filePath)) === false) {
       
    88             require_once 'Zend/Pdf/Exception.php';
       
    89             throw new Zend_Pdf_Exception("Error while obtaining file size: $filePath",
       
    90                                          Zend_Pdf_Exception::CANT_GET_FILE_SIZE);
       
    91         }
       
    92         if (($this->_fileResource = @fopen($filePath, 'rb')) === false) {
       
    93             require_once 'Zend/Pdf/Exception.php';
       
    94             throw new Zend_Pdf_Exception("Cannot open file for reading: $filePath",
       
    95                                          Zend_Pdf_Exception::CANT_OPEN_FILE);
       
    96         }
       
    97         $this->_filePath = $filePath;
       
    98     }
       
    99 
       
   100     /**
       
   101      * Object destructor.
       
   102      *
       
   103      * Closes the file if it had been successfully opened.
       
   104      */
       
   105     public function __destruct()
       
   106     {
       
   107         if (is_resource($this->_fileResource)) {
       
   108             @fclose($this->_fileResource);
       
   109         }
       
   110     }
       
   111 
       
   112     /**
       
   113      * Returns the specified number of raw bytes from the file at the byte
       
   114      * offset of the current read position.
       
   115      *
       
   116      * Advances the read position by the number of bytes read.
       
   117      *
       
   118      * Throws an exception if an error was encountered while reading the file or
       
   119      * if there is insufficient data to completely fulfill the request.
       
   120      *
       
   121      * @param integer $byteCount Number of bytes to read.
       
   122      * @return string
       
   123      * @throws Zend_Pdf_Exception
       
   124      */
       
   125     public function readBytes($byteCount)
       
   126     {
       
   127         $bytes = @fread($this->_fileResource, $byteCount);
       
   128         if ($bytes === false) {
       
   129             require_once 'Zend/Pdf/Exception.php';
       
   130             throw new Zend_Pdf_Exception('Unexpected error while reading file',
       
   131                                          Zend_Pdf_Exception::ERROR_DURING_READ);
       
   132         }
       
   133         if (strlen($bytes) != $byteCount) {
       
   134             require_once 'Zend/Pdf/Exception.php';
       
   135             throw new Zend_Pdf_Exception("Insufficient data to read $byteCount bytes",
       
   136                                          Zend_Pdf_Exception::INSUFFICIENT_DATA);
       
   137         }
       
   138         $this->_offset += $byteCount;
       
   139         return $bytes;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Returns the entire contents of the file as a string.
       
   144      *
       
   145      * Preserves the current file seek position.
       
   146      *
       
   147      * @return string
       
   148      */
       
   149     public function readAllBytes()
       
   150     {
       
   151         return file_get_contents($this->_filePath);
       
   152     }
       
   153 
       
   154 
       
   155   /* Object Magic Methods */
       
   156 
       
   157     /**
       
   158      * Returns the full filesystem path of the file.
       
   159      *
       
   160      * @return string
       
   161      */
       
   162     public function __toString()
       
   163     {
       
   164         return $this->_filePath;
       
   165     }
       
   166 
       
   167 
       
   168   /* Primitive Methods */
       
   169 
       
   170     /**
       
   171      * Seeks the file read position to the specified byte offset.
       
   172      *
       
   173      * Throws an exception if the file pointer cannot be moved or if it is
       
   174      * moved beyond EOF (end of file).
       
   175      *
       
   176      * @param integer $offset Destination byte offset.
       
   177      * @throws Zend_Pdf_Exception
       
   178      */
       
   179     public function moveToOffset($offset)
       
   180     {
       
   181         if ($this->_offset == $offset) {
       
   182             return;    // Not moving; do nothing.
       
   183         }
       
   184         parent::moveToOffset($offset);
       
   185         $result = @fseek($this->_fileResource, $offset, SEEK_SET);
       
   186         if ($result !== 0) {
       
   187             require_once 'Zend/Pdf/Exception.php';
       
   188             throw new Zend_Pdf_Exception('Error while setting new file position',
       
   189                                          Zend_Pdf_Exception::CANT_SET_FILE_POSITION);
       
   190         }
       
   191         if (feof($this->_fileResource)) {
       
   192             require_once 'Zend/Pdf/Exception.php';
       
   193             throw new Zend_Pdf_Exception('Moved beyond the end of the file',
       
   194                                          Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE);
       
   195         }
       
   196     }
       
   197 
       
   198 }