1 | <?php |
||||
2 | /* |
||||
3 | Copyright (c) 2003, 2005, 2006, 2009 Danilo Segan <[email protected]>. |
||||
4 | |||||
5 | This file is part of PHP-gettext. |
||||
6 | |||||
7 | PHP-gettext is free software; you can redistribute it and/or modify |
||||
8 | it under the terms of the GNU General Public License as published by |
||||
9 | the Free Software Foundation; either version 2 of the License, or |
||||
10 | (at your option) any later version. |
||||
11 | |||||
12 | PHP-gettext 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 |
||||
15 | GNU General Public License for more details. |
||||
16 | |||||
17 | You should have received a copy of the GNU General Public License |
||||
18 | along with PHP-gettext; if not, write to the Free Software |
||||
19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||||
20 | |||||
21 | */ |
||||
22 | |||||
23 | |||||
24 | // Simple class to wrap file streams, string streams, etc. |
||||
25 | // seek is essential, and it should be byte stream |
||||
26 | class StreamReader { |
||||
27 | // should return a string [FIXME: perhaps return array of bytes?] |
||||
28 | function read($bytes) { |
||||
0 ignored issues
–
show
The parameter
$bytes is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
29 | return false; |
||||
30 | } |
||||
31 | |||||
32 | // should return new position |
||||
33 | function seekto($position) { |
||||
0 ignored issues
–
show
The parameter
$position is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
34 | return false; |
||||
35 | } |
||||
36 | |||||
37 | // returns current position |
||||
38 | function currentpos() { |
||||
0 ignored issues
–
show
|
|||||
39 | return false; |
||||
40 | } |
||||
41 | |||||
42 | // returns length of entire stream (limit for seekto()s) |
||||
43 | function length() { |
||||
0 ignored issues
–
show
|
|||||
44 | return false; |
||||
45 | } |
||||
46 | }; |
||||
47 | |||||
48 | class StringReader { |
||||
49 | var $_pos; |
||||
50 | var $_str; |
||||
51 | |||||
52 | function StringReader($str = '') { |
||||
0 ignored issues
–
show
|
|||||
53 | $this->_str = $str; |
||||
54 | $this->_pos = 0; |
||||
55 | } |
||||
56 | |||||
57 | function read($bytes) { |
||||
0 ignored issues
–
show
|
|||||
58 | $data = substr($this->_str, $this->_pos, $bytes); |
||||
59 | $this->_pos += $bytes; |
||||
60 | if (strlen($this->_str) < $this->_pos) { |
||||
61 | $this->_pos = strlen($this->_str); |
||||
62 | } |
||||
63 | |||||
64 | return $data; |
||||
65 | } |
||||
66 | |||||
67 | function seekto($pos) { |
||||
0 ignored issues
–
show
|
|||||
68 | $this->_pos = $pos; |
||||
69 | if (strlen($this->_str) < $this->_pos) { |
||||
70 | $this->_pos = strlen($this->_str); |
||||
71 | } |
||||
72 | return $this->_pos; |
||||
73 | } |
||||
74 | |||||
75 | function currentpos() { |
||||
0 ignored issues
–
show
|
|||||
76 | return $this->_pos; |
||||
77 | } |
||||
78 | |||||
79 | function length() { |
||||
0 ignored issues
–
show
|
|||||
80 | return strlen($this->_str); |
||||
81 | } |
||||
82 | |||||
83 | }; |
||||
84 | |||||
85 | |||||
86 | class FileReader { |
||||
87 | var $_pos; |
||||
88 | var $_fd; |
||||
89 | var $_length; |
||||
90 | |||||
91 | function FileReader($filename) { |
||||
0 ignored issues
–
show
|
|||||
92 | if (file_exists($filename)) { |
||||
93 | |||||
94 | $this->_length = filesize($filename); |
||||
95 | $this->_pos = 0; |
||||
96 | $this->_fd = fopen($filename, 'rb'); |
||||
97 | if (!$this->_fd) { |
||||
98 | $this->error = 3; // Cannot read file, probably permissions |
||||
0 ignored issues
–
show
|
|||||
99 | return false; |
||||
100 | } |
||||
101 | } else { |
||||
102 | $this->error = 2; // File doesn't exist |
||||
103 | return false; |
||||
104 | } |
||||
105 | } |
||||
106 | |||||
107 | function read($bytes) { |
||||
0 ignored issues
–
show
|
|||||
108 | if ($bytes) { |
||||
109 | fseek($this->_fd, $this->_pos); |
||||
110 | |||||
111 | // PHP 5.1.1 does not read more than 8192 bytes in one fread() |
||||
112 | // the discussions at PHP Bugs suggest it's the intended behaviour |
||||
113 | $data = ''; |
||||
114 | while ($bytes > 0) { |
||||
115 | $chunk = fread($this->_fd, $bytes); |
||||
116 | $data .= $chunk; |
||||
117 | $bytes -= strlen($chunk); |
||||
118 | } |
||||
119 | $this->_pos = ftell($this->_fd); |
||||
120 | |||||
121 | return $data; |
||||
122 | } else { |
||||
123 | return ''; |
||||
124 | } |
||||
125 | } |
||||
126 | |||||
127 | function seekto($pos) { |
||||
0 ignored issues
–
show
|
|||||
128 | fseek($this->_fd, $pos); |
||||
129 | $this->_pos = ftell($this->_fd); |
||||
130 | return $this->_pos; |
||||
131 | } |
||||
132 | |||||
133 | function currentpos() { |
||||
0 ignored issues
–
show
|
|||||
134 | return $this->_pos; |
||||
135 | } |
||||
136 | |||||
137 | function length() { |
||||
0 ignored issues
–
show
|
|||||
138 | return $this->_length; |
||||
139 | } |
||||
140 | |||||
141 | function close() { |
||||
0 ignored issues
–
show
|
|||||
142 | fclose($this->_fd); |
||||
143 | } |
||||
144 | |||||
145 | }; |
||||
146 | |||||
147 | // Preloads entire file in memory first, then creates a StringReader |
||||
148 | // over it (it assumes knowledge of StringReader internals) |
||||
149 | class CachedFileReader extends StringReader { |
||||
150 | function CachedFileReader($filename) { |
||||
0 ignored issues
–
show
|
|||||
151 | if (file_exists($filename)) { |
||||
152 | |||||
153 | $length = filesize($filename); |
||||
154 | $fd = fopen($filename, 'rb'); |
||||
155 | |||||
156 | if (!$fd) { |
||||
0 ignored issues
–
show
|
|||||
157 | $this->error = 3; // Cannot read file, probably permissions |
||||
0 ignored issues
–
show
|
|||||
158 | return false; |
||||
159 | } |
||||
160 | $this->_str = fread($fd, $length); |
||||
161 | fclose($fd); |
||||
162 | |||||
163 | } else { |
||||
164 | $this->error = 2; // File doesn't exist |
||||
165 | return false; |
||||
166 | } |
||||
167 | } |
||||
168 | }; |
||||
169 | |||||
170 | |||||
171 | ?> |
||||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |
|||||
172 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.