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