File indexing completed on 2025-01-19 05:21:21
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Pdf 0017 * @subpackage FileParser 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** Zend_Pdf_FileParserDataSource */ 0024 // require_once 'Zend/Pdf/FileParserDataSource.php'; 0025 0026 0027 /** 0028 * Concrete subclass of {@link Zend_Pdf_FileParserDataSource} that provides an 0029 * interface to filesystem objects. 0030 * 0031 * Note that this class cannot be used for other sources that may be supported 0032 * by {@link fopen()} (through URL wrappers). It may be used for local 0033 * filesystem objects only. 0034 * 0035 * @package Zend_Pdf 0036 * @subpackage FileParser 0037 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0038 * @license http://framework.zend.com/license/new-bsd New BSD License 0039 */ 0040 class Zend_Pdf_FileParserDataSource_File extends Zend_Pdf_FileParserDataSource 0041 { 0042 /**** Instance Variables ****/ 0043 0044 0045 /** 0046 * Fully-qualified path to the file. 0047 * @var string 0048 */ 0049 protected $_filePath = ''; 0050 0051 /** 0052 * File resource handle . 0053 * @var resource 0054 */ 0055 protected $_fileResource = null; 0056 0057 0058 0059 /**** Public Interface ****/ 0060 0061 0062 /* Concrete Class Implementation */ 0063 0064 /** 0065 * Object constructor. 0066 * 0067 * Validates the path to the file, ensures that it is readable, then opens 0068 * it for reading. 0069 * 0070 * Throws an exception if the file is missing or cannot be opened. 0071 * 0072 * @param string $filePath Fully-qualified path to the file. 0073 * @throws Zend_Pdf_Exception 0074 */ 0075 public function __construct($filePath) 0076 { 0077 if (! (is_file($filePath) || is_link($filePath))) { 0078 // require_once 'Zend/Pdf/Exception.php'; 0079 throw new Zend_Pdf_Exception("Invalid file path: $filePath", 0080 Zend_Pdf_Exception::BAD_FILE_PATH); 0081 } 0082 if (! is_readable($filePath)) { 0083 // require_once 'Zend/Pdf/Exception.php'; 0084 throw new Zend_Pdf_Exception("File is not readable: $filePath", 0085 Zend_Pdf_Exception::NOT_READABLE); 0086 } 0087 if (($this->_size = @filesize($filePath)) === false) { 0088 // require_once 'Zend/Pdf/Exception.php'; 0089 throw new Zend_Pdf_Exception("Error while obtaining file size: $filePath", 0090 Zend_Pdf_Exception::CANT_GET_FILE_SIZE); 0091 } 0092 if (($this->_fileResource = @fopen($filePath, 'rb')) === false) { 0093 // require_once 'Zend/Pdf/Exception.php'; 0094 throw new Zend_Pdf_Exception("Cannot open file for reading: $filePath", 0095 Zend_Pdf_Exception::CANT_OPEN_FILE); 0096 } 0097 $this->_filePath = $filePath; 0098 } 0099 0100 /** 0101 * Object destructor. 0102 * 0103 * Closes the file if it had been successfully opened. 0104 */ 0105 public function __destruct() 0106 { 0107 if (is_resource($this->_fileResource)) { 0108 @fclose($this->_fileResource); 0109 } 0110 } 0111 0112 /** 0113 * Returns the specified number of raw bytes from the file at the byte 0114 * offset of the current read position. 0115 * 0116 * Advances the read position by the number of bytes read. 0117 * 0118 * Throws an exception if an error was encountered while reading the file or 0119 * if there is insufficient data to completely fulfill the request. 0120 * 0121 * @param integer $byteCount Number of bytes to read. 0122 * @return string 0123 * @throws Zend_Pdf_Exception 0124 */ 0125 public function readBytes($byteCount) 0126 { 0127 $bytes = @fread($this->_fileResource, $byteCount); 0128 if ($bytes === false) { 0129 // require_once 'Zend/Pdf/Exception.php'; 0130 throw new Zend_Pdf_Exception('Unexpected error while reading file', 0131 Zend_Pdf_Exception::ERROR_DURING_READ); 0132 } 0133 if (strlen($bytes) != $byteCount) { 0134 // require_once 'Zend/Pdf/Exception.php'; 0135 throw new Zend_Pdf_Exception("Insufficient data to read $byteCount bytes", 0136 Zend_Pdf_Exception::INSUFFICIENT_DATA); 0137 } 0138 $this->_offset += $byteCount; 0139 return $bytes; 0140 } 0141 0142 /** 0143 * Returns the entire contents of the file as a string. 0144 * 0145 * Preserves the current file seek position. 0146 * 0147 * @return string 0148 */ 0149 public function readAllBytes() 0150 { 0151 return file_get_contents($this->_filePath); 0152 } 0153 0154 0155 /* Object Magic Methods */ 0156 0157 /** 0158 * Returns the full filesystem path of the file. 0159 * 0160 * @return string 0161 */ 0162 public function __toString() 0163 { 0164 return $this->_filePath; 0165 } 0166 0167 0168 /* Primitive Methods */ 0169 0170 /** 0171 * Seeks the file read position to the specified byte offset. 0172 * 0173 * Throws an exception if the file pointer cannot be moved or if it is 0174 * moved beyond EOF (end of file). 0175 * 0176 * @param integer $offset Destination byte offset. 0177 * @throws Zend_Pdf_Exception 0178 */ 0179 public function moveToOffset($offset) 0180 { 0181 if ($this->_offset == $offset) { 0182 return; // Not moving; do nothing. 0183 } 0184 parent::moveToOffset($offset); 0185 $result = @fseek($this->_fileResource, $offset, SEEK_SET); 0186 if ($result !== 0) { 0187 // require_once 'Zend/Pdf/Exception.php'; 0188 throw new Zend_Pdf_Exception('Error while setting new file position', 0189 Zend_Pdf_Exception::CANT_SET_FILE_POSITION); 0190 } 0191 if (feof($this->_fileResource)) { 0192 // require_once 'Zend/Pdf/Exception.php'; 0193 throw new Zend_Pdf_Exception('Moved beyond the end of the file', 0194 Zend_Pdf_Exception::MOVE_BEYOND_END_OF_FILE); 0195 } 0196 } 0197 0198 }