/src/php-src/ext/standard/streamsfuncs.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright (c) The PHP Group | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to version 3.01 of the PHP license, | |
6 | | | that is bundled with this package in the file LICENSE, and is | |
7 | | | available through the world-wide-web at the following url: | |
8 | | | http://www.php.net/license/3_01.txt | |
9 | | | If you did not receive a copy of the PHP license and are unable to | |
10 | | | obtain it through the world-wide-web, please send a note to | |
11 | | | license@php.net so we can mail you a copy immediately. | |
12 | | +----------------------------------------------------------------------+ |
13 | | | Authors: Wez Furlong <wez@thebrainroom.com> | |
14 | | | Sara Golemon <pollita@php.net> | |
15 | | +----------------------------------------------------------------------+ |
16 | | */ |
17 | | |
18 | | #include "php.h" |
19 | | #include "php_globals.h" |
20 | | #include "ext/standard/flock_compat.h" |
21 | | #include "ext/standard/file.h" |
22 | | #include "ext/standard/php_filestat.h" |
23 | | #include "php_open_temporary_file.h" |
24 | | #include "ext/standard/basic_functions.h" |
25 | | #include "php_ini.h" |
26 | | #include "streamsfuncs.h" |
27 | | #include "php_network.h" |
28 | | #include "php_string.h" |
29 | | #if HAVE_UNISTD_H |
30 | | #include <unistd.h> |
31 | | #endif |
32 | | |
33 | | #ifndef PHP_WIN32 |
34 | 0 | #define php_select(m, r, w, e, t) select(m, r, w, e, t) |
35 | | typedef unsigned long long php_timeout_ull; |
36 | | #else |
37 | | #include "win32/select.h" |
38 | | #include "win32/sockets.h" |
39 | | #include "win32/console.h" |
40 | | typedef unsigned __int64 php_timeout_ull; |
41 | | #endif |
42 | | |
43 | 0 | #define GET_CTX_OPT(stream, wrapper, name, val) (PHP_STREAM_CONTEXT(stream) && NULL != (val = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), wrapper, name))) |
44 | | |
45 | | static php_stream_context *decode_context_param(zval *contextresource); |
46 | | |
47 | | /* Streams based network functions */ |
48 | | |
49 | | #if HAVE_SOCKETPAIR |
50 | | /* {{{ proto array|false stream_socket_pair(int domain, int type, int protocol) |
51 | | Creates a pair of connected, indistinguishable socket streams */ |
52 | | PHP_FUNCTION(stream_socket_pair) |
53 | 0 | { |
54 | 0 | zend_long domain, type, protocol; |
55 | 0 | php_stream *s1, *s2; |
56 | 0 | php_socket_t pair[2]; |
57 | |
|
58 | 0 | ZEND_PARSE_PARAMETERS_START(3, 3) |
59 | 0 | Z_PARAM_LONG(domain) |
60 | 0 | Z_PARAM_LONG(type) |
61 | 0 | Z_PARAM_LONG(protocol) |
62 | 0 | ZEND_PARSE_PARAMETERS_END(); |
63 | |
|
64 | 0 | if (0 != socketpair((int)domain, (int)type, (int)protocol, pair)) { |
65 | 0 | char errbuf[256]; |
66 | 0 | php_error_docref(NULL, E_WARNING, "Failed to create sockets: [%d]: %s", |
67 | 0 | php_socket_errno(), php_socket_strerror(php_socket_errno(), errbuf, sizeof(errbuf))); |
68 | 0 | RETURN_FALSE; |
69 | 0 | } |
70 | |
|
71 | 0 | array_init(return_value); |
72 | |
|
73 | 0 | s1 = php_stream_sock_open_from_socket(pair[0], 0); |
74 | 0 | s2 = php_stream_sock_open_from_socket(pair[1], 0); |
75 | | |
76 | | /* set the __exposed flag. |
77 | | * php_stream_to_zval() does, add_next_index_resource() does not */ |
78 | 0 | php_stream_auto_cleanup(s1); |
79 | 0 | php_stream_auto_cleanup(s2); |
80 | |
|
81 | 0 | add_next_index_resource(return_value, s1->res); |
82 | 0 | add_next_index_resource(return_value, s2->res); |
83 | 0 | } |
84 | | /* }}} */ |
85 | | #endif |
86 | | |
87 | | /* {{{ proto resource|false stream_socket_client(string remoteaddress [, int &errcode [, string &errstring [, double timeout [, int flags [, resource context]]]]]) |
88 | | Open a client connection to a remote address */ |
89 | | PHP_FUNCTION(stream_socket_client) |
90 | 0 | { |
91 | 0 | zend_string *host; |
92 | 0 | zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL; |
93 | 0 | double timeout = (double)FG(default_socket_timeout); |
94 | 0 | php_timeout_ull conv; |
95 | 0 | struct timeval tv; |
96 | 0 | char *hashkey = NULL; |
97 | 0 | php_stream *stream = NULL; |
98 | 0 | int err; |
99 | 0 | zend_long flags = PHP_STREAM_CLIENT_CONNECT; |
100 | 0 | zend_string *errstr = NULL; |
101 | 0 | php_stream_context *context = NULL; |
102 | |
|
103 | 0 | RETVAL_FALSE; |
104 | |
|
105 | 0 | ZEND_PARSE_PARAMETERS_START(1, 6) |
106 | 0 | Z_PARAM_STR(host) |
107 | 0 | Z_PARAM_OPTIONAL |
108 | 0 | Z_PARAM_ZVAL(zerrno) |
109 | 0 | Z_PARAM_ZVAL(zerrstr) |
110 | 0 | Z_PARAM_DOUBLE(timeout) |
111 | 0 | Z_PARAM_LONG(flags) |
112 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
113 | 0 | ZEND_PARSE_PARAMETERS_END(); |
114 | |
|
115 | 0 | context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); |
116 | |
|
117 | 0 | if (flags & PHP_STREAM_CLIENT_PERSISTENT) { |
118 | 0 | spprintf(&hashkey, 0, "stream_socket_client__%s", ZSTR_VAL(host)); |
119 | 0 | } |
120 | | |
121 | | /* prepare the timeout value for use */ |
122 | 0 | conv = (php_timeout_ull) (timeout * 1000000.0); |
123 | | #ifdef PHP_WIN32 |
124 | | tv.tv_sec = (long)(conv / 1000000); |
125 | | tv.tv_usec =(long)(conv % 1000000); |
126 | | #else |
127 | 0 | tv.tv_sec = conv / 1000000; |
128 | 0 | tv.tv_usec = conv % 1000000; |
129 | 0 | #endif |
130 | 0 | if (zerrno) { |
131 | 0 | ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0); |
132 | 0 | } |
133 | 0 | if (zerrstr) { |
134 | 0 | ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr); |
135 | 0 | } |
136 | |
|
137 | 0 | stream = php_stream_xport_create(ZSTR_VAL(host), ZSTR_LEN(host), REPORT_ERRORS, |
138 | 0 | STREAM_XPORT_CLIENT | (flags & PHP_STREAM_CLIENT_CONNECT ? STREAM_XPORT_CONNECT : 0) | |
139 | 0 | (flags & PHP_STREAM_CLIENT_ASYNC_CONNECT ? STREAM_XPORT_CONNECT_ASYNC : 0), |
140 | 0 | hashkey, &tv, context, &errstr, &err); |
141 | | |
142 | |
|
143 | 0 | if (stream == NULL) { |
144 | | /* host might contain binary characters */ |
145 | 0 | zend_string *quoted_host = php_addslashes(host); |
146 | |
|
147 | 0 | php_error_docref(NULL, E_WARNING, "Unable to connect to %s (%s)", ZSTR_VAL(quoted_host), errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr)); |
148 | 0 | zend_string_release_ex(quoted_host, 0); |
149 | 0 | } |
150 | |
|
151 | 0 | if (hashkey) { |
152 | 0 | efree(hashkey); |
153 | 0 | } |
154 | |
|
155 | 0 | if (stream == NULL) { |
156 | 0 | if (zerrno) { |
157 | 0 | ZEND_TRY_ASSIGN_REF_LONG(zerrno, err); |
158 | 0 | } |
159 | 0 | if (zerrstr && errstr) { |
160 | 0 | ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr); |
161 | 0 | } else if (errstr) { |
162 | 0 | zend_string_release_ex(errstr, 0); |
163 | 0 | } |
164 | 0 | RETURN_FALSE; |
165 | 0 | } |
166 | |
|
167 | 0 | if (errstr) { |
168 | 0 | zend_string_release_ex(errstr, 0); |
169 | 0 | } |
170 | |
|
171 | 0 | php_stream_to_zval(stream, return_value); |
172 | |
|
173 | 0 | } |
174 | | /* }}} */ |
175 | | |
176 | | /* {{{ proto resource|false stream_socket_server(string localaddress [, int &errcode [, string &errstring [, int flags [, resource context]]]]) |
177 | | Create a server socket bound to localaddress */ |
178 | | PHP_FUNCTION(stream_socket_server) |
179 | 0 | { |
180 | 0 | char *host; |
181 | 0 | size_t host_len; |
182 | 0 | zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL; |
183 | 0 | php_stream *stream = NULL; |
184 | 0 | int err = 0; |
185 | 0 | zend_long flags = STREAM_XPORT_BIND | STREAM_XPORT_LISTEN; |
186 | 0 | zend_string *errstr = NULL; |
187 | 0 | php_stream_context *context = NULL; |
188 | |
|
189 | 0 | RETVAL_FALSE; |
190 | |
|
191 | 0 | ZEND_PARSE_PARAMETERS_START(1, 5) |
192 | 0 | Z_PARAM_STRING(host, host_len) |
193 | 0 | Z_PARAM_OPTIONAL |
194 | 0 | Z_PARAM_ZVAL(zerrno) |
195 | 0 | Z_PARAM_ZVAL(zerrstr) |
196 | 0 | Z_PARAM_LONG(flags) |
197 | 0 | Z_PARAM_RESOURCE_OR_NULL(zcontext) |
198 | 0 | ZEND_PARSE_PARAMETERS_END(); |
199 | |
|
200 | 0 | context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT); |
201 | |
|
202 | 0 | if (context) { |
203 | 0 | GC_ADDREF(context->res); |
204 | 0 | } |
205 | |
|
206 | 0 | if (zerrno) { |
207 | 0 | ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0); |
208 | 0 | } |
209 | 0 | if (zerrstr) { |
210 | 0 | ZEND_TRY_ASSIGN_REF_EMPTY_STRING(zerrstr); |
211 | 0 | } |
212 | |
|
213 | 0 | stream = php_stream_xport_create(host, host_len, REPORT_ERRORS, |
214 | 0 | STREAM_XPORT_SERVER | (int)flags, |
215 | 0 | NULL, NULL, context, &errstr, &err); |
216 | |
|
217 | 0 | if (stream == NULL) { |
218 | 0 | php_error_docref(NULL, E_WARNING, "Unable to connect to %s (%s)", host, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr)); |
219 | 0 | } |
220 | |
|
221 | 0 | if (stream == NULL) { |
222 | 0 | if (zerrno) { |
223 | 0 | ZEND_TRY_ASSIGN_REF_LONG(zerrno, err); |
224 | 0 | } |
225 | 0 | if (zerrstr && errstr) { |
226 | 0 | ZEND_TRY_ASSIGN_REF_STR(zerrstr, errstr); |
227 | 0 | } else if (errstr) { |
228 | 0 | zend_string_release_ex(errstr, 0); |
229 | 0 | } |
230 | 0 | RETURN_FALSE; |
231 | 0 | } |
232 | |
|
233 | 0 | if (errstr) { |
234 | 0 | zend_string_release_ex(errstr, 0); |
235 | 0 | } |
236 | |
|
237 | 0 | php_stream_to_zval(stream, return_value); |
238 | 0 | } |
239 | | /* }}} */ |
240 | | |
241 | | /* {{{ proto resource|false stream_socket_accept(resource serverstream, [ double timeout [, string &peername ]]) |
242 | | Accept a client connection from a server socket */ |
243 | | PHP_FUNCTION(stream_socket_accept) |
244 | 0 | { |
245 | 0 | double timeout = (double)FG(default_socket_timeout); |
246 | 0 | zval *zpeername = NULL; |
247 | 0 | zend_string *peername = NULL; |
248 | 0 | php_timeout_ull conv; |
249 | 0 | struct timeval tv; |
250 | 0 | php_stream *stream = NULL, *clistream = NULL; |
251 | 0 | zval *zstream; |
252 | 0 | zend_string *errstr = NULL; |
253 | |
|
254 | 0 | ZEND_PARSE_PARAMETERS_START(1, 3) |
255 | 0 | Z_PARAM_RESOURCE(zstream) |
256 | 0 | Z_PARAM_OPTIONAL |
257 | 0 | Z_PARAM_DOUBLE(timeout) |
258 | 0 | Z_PARAM_ZVAL(zpeername) |
259 | 0 | ZEND_PARSE_PARAMETERS_END(); |
260 | |
|
261 | 0 | php_stream_from_zval(stream, zstream); |
262 | | |
263 | | /* prepare the timeout value for use */ |
264 | 0 | conv = (php_timeout_ull) (timeout * 1000000.0); |
265 | | #ifdef PHP_WIN32 |
266 | | tv.tv_sec = (long)(conv / 1000000); |
267 | | tv.tv_usec = (long)(conv % 1000000); |
268 | | #else |
269 | 0 | tv.tv_sec = conv / 1000000; |
270 | 0 | tv.tv_usec = conv % 1000000; |
271 | 0 | #endif |
272 | |
|
273 | 0 | if (0 == php_stream_xport_accept(stream, &clistream, |
274 | 0 | zpeername ? &peername : NULL, |
275 | 0 | NULL, NULL, |
276 | 0 | &tv, &errstr |
277 | 0 | ) && clistream) { |
278 | |
|
279 | 0 | if (peername) { |
280 | 0 | ZEND_TRY_ASSIGN_REF_STR(zpeername, peername); |
281 | 0 | } |
282 | 0 | php_stream_to_zval(clistream, return_value); |
283 | 0 | } else { |
284 | 0 | if (peername) { |
285 | 0 | zend_string_release(peername); |
286 | 0 | } |
287 | 0 | php_error_docref(NULL, E_WARNING, "Accept failed: %s", errstr ? ZSTR_VAL(errstr) : "Unknown error"); |
288 | 0 | RETVAL_FALSE; |
289 | 0 | } |
290 | |
|
291 | 0 | if (errstr) { |
292 | 0 | zend_string_release_ex(errstr, 0); |
293 | 0 | } |
294 | 0 | } |
295 | | /* }}} */ |
296 | | |
297 | | /* {{{ proto string|false stream_socket_get_name(resource stream, bool want_peer) |
298 | | Returns either the locally bound or remote name for a socket stream */ |
299 | | PHP_FUNCTION(stream_socket_get_name) |
300 | 0 | { |
301 | 0 | php_stream *stream; |
302 | 0 | zval *zstream; |
303 | 0 | zend_bool want_peer; |
304 | 0 | zend_string *name = NULL; |
305 | |
|
306 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
307 | 0 | Z_PARAM_RESOURCE(zstream) |
308 | 0 | Z_PARAM_BOOL(want_peer) |
309 | 0 | ZEND_PARSE_PARAMETERS_END(); |
310 | |
|
311 | 0 | php_stream_from_zval(stream, zstream); |
312 | |
|
313 | 0 | if (0 != php_stream_xport_get_name(stream, want_peer, |
314 | 0 | &name, |
315 | 0 | NULL, NULL |
316 | 0 | ) || !name) { |
317 | 0 | RETURN_FALSE; |
318 | 0 | } |
319 | |
|
320 | 0 | if ((ZSTR_LEN(name) == 0) || (ZSTR_VAL(name)[0] == 0)) { |
321 | 0 | zend_string_release_ex(name, 0); |
322 | 0 | RETURN_FALSE; |
323 | 0 | } |
324 | |
|
325 | 0 | RETVAL_STR(name); |
326 | 0 | } |
327 | | /* }}} */ |
328 | | |
329 | | /* {{{ proto int|false stream_socket_sendto(resource stream, string data [, int flags [, string target_addr]]) |
330 | | Send data to a socket stream. If target_addr is specified it must be in dotted quad (or [ipv6]) format */ |
331 | | PHP_FUNCTION(stream_socket_sendto) |
332 | 0 | { |
333 | 0 | php_stream *stream; |
334 | 0 | zval *zstream; |
335 | 0 | zend_long flags = 0; |
336 | 0 | char *data, *target_addr = NULL; |
337 | 0 | size_t datalen, target_addr_len = 0; |
338 | 0 | php_sockaddr_storage sa; |
339 | 0 | socklen_t sl = 0; |
340 | |
|
341 | 0 | ZEND_PARSE_PARAMETERS_START(2, 4) |
342 | 0 | Z_PARAM_RESOURCE(zstream) |
343 | 0 | Z_PARAM_STRING(data, datalen) |
344 | 0 | Z_PARAM_OPTIONAL |
345 | 0 | Z_PARAM_LONG(flags) |
346 | 0 | Z_PARAM_STRING(target_addr, target_addr_len) |
347 | 0 | ZEND_PARSE_PARAMETERS_END(); |
348 | 0 | php_stream_from_zval(stream, zstream); |
349 | |
|
350 | 0 | if (target_addr_len) { |
351 | | /* parse the address */ |
352 | 0 | if (FAILURE == php_network_parse_network_address_with_port(target_addr, target_addr_len, (struct sockaddr*)&sa, &sl)) { |
353 | 0 | php_error_docref(NULL, E_WARNING, "Failed to parse `%s' into a valid network address", target_addr); |
354 | 0 | RETURN_FALSE; |
355 | 0 | } |
356 | 0 | } |
357 | |
|
358 | 0 | RETURN_LONG(php_stream_xport_sendto(stream, data, datalen, (int)flags, target_addr_len ? &sa : NULL, sl)); |
359 | 0 | } |
360 | | /* }}} */ |
361 | | |
362 | | /* {{{ proto string|false stream_socket_recvfrom(resource stream, int amount [, int flags [, string &remote_addr]]) |
363 | | Receives data from a socket stream */ |
364 | | PHP_FUNCTION(stream_socket_recvfrom) |
365 | 0 | { |
366 | 0 | php_stream *stream; |
367 | 0 | zval *zstream, *zremote = NULL; |
368 | 0 | zend_string *remote_addr = NULL; |
369 | 0 | zend_long to_read = 0; |
370 | 0 | zend_string *read_buf; |
371 | 0 | zend_long flags = 0; |
372 | 0 | int recvd; |
373 | |
|
374 | 0 | ZEND_PARSE_PARAMETERS_START(2, 4) |
375 | 0 | Z_PARAM_RESOURCE(zstream) |
376 | 0 | Z_PARAM_LONG(to_read) |
377 | 0 | Z_PARAM_OPTIONAL |
378 | 0 | Z_PARAM_LONG(flags) |
379 | 0 | Z_PARAM_ZVAL(zremote) |
380 | 0 | ZEND_PARSE_PARAMETERS_END(); |
381 | |
|
382 | 0 | php_stream_from_zval(stream, zstream); |
383 | |
|
384 | 0 | if (zremote) { |
385 | 0 | ZEND_TRY_ASSIGN_REF_NULL(zremote); |
386 | 0 | } |
387 | |
|
388 | 0 | if (to_read <= 0) { |
389 | 0 | zend_argument_value_error(2, "must be greater than 0"); |
390 | 0 | RETURN_THROWS(); |
391 | 0 | } |
392 | |
|
393 | 0 | read_buf = zend_string_alloc(to_read, 0); |
394 | |
|
395 | 0 | recvd = php_stream_xport_recvfrom(stream, ZSTR_VAL(read_buf), to_read, (int)flags, NULL, NULL, |
396 | 0 | zremote ? &remote_addr : NULL |
397 | 0 | ); |
398 | |
|
399 | 0 | if (recvd >= 0) { |
400 | 0 | if (zremote && remote_addr) { |
401 | 0 | ZEND_TRY_ASSIGN_REF_STR(zremote, remote_addr); |
402 | 0 | } |
403 | 0 | ZSTR_VAL(read_buf)[recvd] = '\0'; |
404 | 0 | ZSTR_LEN(read_buf) = recvd; |
405 | 0 | RETURN_NEW_STR(read_buf); |
406 | 0 | } |
407 | |
|
408 | 0 | zend_string_efree(read_buf); |
409 | 0 | RETURN_FALSE; |
410 | 0 | } |
411 | | /* }}} */ |
412 | | |
413 | | /* {{{ proto string|false stream_get_contents(resource source [, int maxlen [, int offset]]) |
414 | | Reads all remaining bytes (or up to maxlen bytes) from a stream and returns them as a string. */ |
415 | | PHP_FUNCTION(stream_get_contents) |
416 | 0 | { |
417 | 0 | php_stream *stream; |
418 | 0 | zval *zsrc; |
419 | 0 | zend_long maxlen = (ssize_t) PHP_STREAM_COPY_ALL, |
420 | 0 | desiredpos = -1L; |
421 | 0 | zend_string *contents; |
422 | |
|
423 | 0 | ZEND_PARSE_PARAMETERS_START(1, 3) |
424 | 0 | Z_PARAM_RESOURCE(zsrc) |
425 | 0 | Z_PARAM_OPTIONAL |
426 | 0 | Z_PARAM_LONG(maxlen) |
427 | 0 | Z_PARAM_LONG(desiredpos) |
428 | 0 | ZEND_PARSE_PARAMETERS_END(); |
429 | |
|
430 | 0 | php_stream_from_zval(stream, zsrc); |
431 | |
|
432 | 0 | if (desiredpos >= 0) { |
433 | 0 | int seek_res = 0; |
434 | 0 | zend_off_t position; |
435 | |
|
436 | 0 | position = php_stream_tell(stream); |
437 | 0 | if (position >= 0 && desiredpos > position) { |
438 | | /* use SEEK_CUR to allow emulation in streams that don't support seeking */ |
439 | 0 | seek_res = php_stream_seek(stream, desiredpos - position, SEEK_CUR); |
440 | 0 | } else if (desiredpos < position) { |
441 | | /* desired position before position or error on tell */ |
442 | 0 | seek_res = php_stream_seek(stream, desiredpos, SEEK_SET); |
443 | 0 | } |
444 | |
|
445 | 0 | if (seek_res != 0) { |
446 | 0 | php_error_docref(NULL, E_WARNING, |
447 | 0 | "Failed to seek to position " ZEND_LONG_FMT " in the stream", desiredpos); |
448 | 0 | RETURN_FALSE; |
449 | 0 | } |
450 | 0 | } |
451 | |
|
452 | 0 | if (maxlen > INT_MAX) { |
453 | 0 | php_error_docref(NULL, E_WARNING, "maxlen truncated from " ZEND_LONG_FMT " to %d bytes", maxlen, INT_MAX); |
454 | 0 | maxlen = INT_MAX; |
455 | 0 | } |
456 | 0 | if ((contents = php_stream_copy_to_mem(stream, maxlen, 0))) { |
457 | 0 | RETURN_STR(contents); |
458 | 0 | } else { |
459 | 0 | RETURN_EMPTY_STRING(); |
460 | 0 | } |
461 | 0 | } |
462 | | /* }}} */ |
463 | | |
464 | | /* {{{ proto int|false stream_copy_to_stream(resource source, resource dest [, int maxlen [, int pos]]) |
465 | | Reads up to maxlen bytes from source stream and writes them to dest stream. */ |
466 | | PHP_FUNCTION(stream_copy_to_stream) |
467 | 0 | { |
468 | 0 | php_stream *src, *dest; |
469 | 0 | zval *zsrc, *zdest; |
470 | 0 | zend_long maxlen = PHP_STREAM_COPY_ALL, pos = 0; |
471 | 0 | size_t len; |
472 | 0 | int ret; |
473 | |
|
474 | 0 | ZEND_PARSE_PARAMETERS_START(2, 4) |
475 | 0 | Z_PARAM_RESOURCE(zsrc) |
476 | 0 | Z_PARAM_RESOURCE(zdest) |
477 | 0 | Z_PARAM_OPTIONAL |
478 | 0 | Z_PARAM_LONG(maxlen) |
479 | 0 | Z_PARAM_LONG(pos) |
480 | 0 | ZEND_PARSE_PARAMETERS_END(); |
481 | |
|
482 | 0 | php_stream_from_zval(src, zsrc); |
483 | 0 | php_stream_from_zval(dest, zdest); |
484 | |
|
485 | 0 | if (pos > 0 && php_stream_seek(src, pos, SEEK_SET) < 0) { |
486 | 0 | php_error_docref(NULL, E_WARNING, "Failed to seek to position " ZEND_LONG_FMT " in the stream", pos); |
487 | 0 | RETURN_FALSE; |
488 | 0 | } |
489 | |
|
490 | 0 | ret = php_stream_copy_to_stream_ex(src, dest, maxlen, &len); |
491 | |
|
492 | 0 | if (ret != SUCCESS) { |
493 | 0 | RETURN_FALSE; |
494 | 0 | } |
495 | 0 | RETURN_LONG(len); |
496 | 0 | } |
497 | | /* }}} */ |
498 | | |
499 | | /* {{{ proto array stream_get_meta_data(resource fp) |
500 | | Retrieves header/meta data from streams/file pointers */ |
501 | | PHP_FUNCTION(stream_get_meta_data) |
502 | 0 | { |
503 | 0 | zval *zstream; |
504 | 0 | php_stream *stream; |
505 | |
|
506 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
507 | 0 | Z_PARAM_RESOURCE(zstream) |
508 | 0 | ZEND_PARSE_PARAMETERS_END(); |
509 | |
|
510 | 0 | php_stream_from_zval(stream, zstream); |
511 | |
|
512 | 0 | array_init(return_value); |
513 | |
|
514 | 0 | if (!php_stream_populate_meta_data(stream, return_value)) { |
515 | 0 | add_assoc_bool(return_value, "timed_out", 0); |
516 | 0 | add_assoc_bool(return_value, "blocked", 1); |
517 | 0 | add_assoc_bool(return_value, "eof", php_stream_eof(stream)); |
518 | 0 | } |
519 | |
|
520 | 0 | if (!Z_ISUNDEF(stream->wrapperdata)) { |
521 | 0 | Z_ADDREF_P(&stream->wrapperdata); |
522 | 0 | add_assoc_zval(return_value, "wrapper_data", &stream->wrapperdata); |
523 | 0 | } |
524 | 0 | if (stream->wrapper) { |
525 | 0 | add_assoc_string(return_value, "wrapper_type", (char *)stream->wrapper->wops->label); |
526 | 0 | } |
527 | 0 | add_assoc_string(return_value, "stream_type", (char *)stream->ops->label); |
528 | |
|
529 | 0 | add_assoc_string(return_value, "mode", stream->mode); |
530 | |
|
531 | | #if 0 /* TODO: needs updating for new filter API */ |
532 | | if (stream->filterhead) { |
533 | | php_stream_filter *filter; |
534 | | |
535 | | MAKE_STD_ZVAL(newval); |
536 | | array_init(newval); |
537 | | |
538 | | for (filter = stream->filterhead; filter != NULL; filter = filter->next) { |
539 | | add_next_index_string(newval, (char *)filter->fops->label); |
540 | | } |
541 | | |
542 | | add_assoc_zval(return_value, "filters", newval); |
543 | | } |
544 | | #endif |
545 | |
|
546 | 0 | add_assoc_long(return_value, "unread_bytes", stream->writepos - stream->readpos); |
547 | |
|
548 | 0 | add_assoc_bool(return_value, "seekable", (stream->ops->seek) && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0); |
549 | 0 | if (stream->orig_path) { |
550 | 0 | add_assoc_string(return_value, "uri", stream->orig_path); |
551 | 0 | } |
552 | |
|
553 | 0 | } |
554 | | /* }}} */ |
555 | | |
556 | | /* {{{ proto array|false stream_get_transports() |
557 | | Retrieves list of registered socket transports */ |
558 | | PHP_FUNCTION(stream_get_transports) |
559 | 0 | { |
560 | 0 | HashTable *stream_xport_hash; |
561 | 0 | zend_string *stream_xport; |
562 | |
|
563 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
564 | |
|
565 | 0 | if ((stream_xport_hash = php_stream_xport_get_hash())) { |
566 | 0 | array_init(return_value); |
567 | 0 | ZEND_HASH_FOREACH_STR_KEY(stream_xport_hash, stream_xport) { |
568 | 0 | add_next_index_str(return_value, zend_string_copy(stream_xport)); |
569 | 0 | } ZEND_HASH_FOREACH_END(); |
570 | 0 | } else { |
571 | 0 | RETURN_FALSE; |
572 | 0 | } |
573 | 0 | } |
574 | | /* }}} */ |
575 | | |
576 | | /* {{{ proto array|false stream_get_wrappers() |
577 | | Retrieves list of registered stream wrappers */ |
578 | | PHP_FUNCTION(stream_get_wrappers) |
579 | 0 | { |
580 | 0 | HashTable *url_stream_wrappers_hash; |
581 | 0 | zend_string *stream_protocol; |
582 | |
|
583 | 0 | ZEND_PARSE_PARAMETERS_NONE(); |
584 | |
|
585 | 0 | if ((url_stream_wrappers_hash = php_stream_get_url_stream_wrappers_hash())) { |
586 | 0 | array_init(return_value); |
587 | 0 | ZEND_HASH_FOREACH_STR_KEY(url_stream_wrappers_hash, stream_protocol) { |
588 | 0 | if (stream_protocol) { |
589 | 0 | add_next_index_str(return_value, zend_string_copy(stream_protocol)); |
590 | 0 | } |
591 | 0 | } ZEND_HASH_FOREACH_END(); |
592 | 0 | } else { |
593 | 0 | RETURN_FALSE; |
594 | 0 | } |
595 | |
|
596 | 0 | } |
597 | | /* }}} */ |
598 | | |
599 | | /* {{{ stream_select related functions */ |
600 | | static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t *max_fd) |
601 | 0 | { |
602 | 0 | zval *elem; |
603 | 0 | php_stream *stream; |
604 | 0 | int cnt = 0; |
605 | |
|
606 | 0 | if (Z_TYPE_P(stream_array) != IS_ARRAY) { |
607 | 0 | return 0; |
608 | 0 | } |
609 | | |
610 | 0 | ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stream_array), elem) { |
611 | | /* Temporary int fd is needed for the STREAM data type on windows, passing this_fd directly to php_stream_cast() |
612 | | would eventually bring a wrong result on x64. php_stream_cast() casts to int internally, and this will leave |
613 | | the higher bits of a SOCKET variable uninitialized on systems with little endian. */ |
614 | 0 | php_socket_t this_fd; |
615 | |
|
616 | 0 | ZVAL_DEREF(elem); |
617 | 0 | php_stream_from_zval_no_verify(stream, elem); |
618 | 0 | if (stream == NULL) { |
619 | 0 | continue; |
620 | 0 | } |
621 | | /* get the fd. |
622 | | * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag |
623 | | * when casting. It is only used here so that the buffered data warning |
624 | | * is not displayed. |
625 | | * */ |
626 | 0 | if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) { |
627 | |
|
628 | 0 | PHP_SAFE_FD_SET(this_fd, fds); |
629 | |
|
630 | 0 | if (this_fd > *max_fd) { |
631 | 0 | *max_fd = this_fd; |
632 | 0 | } |
633 | 0 | cnt++; |
634 | 0 | } |
635 | 0 | } ZEND_HASH_FOREACH_END(); |
636 | 0 | return cnt ? 1 : 0; |
637 | 0 | } |
638 | | |
639 | | static int stream_array_from_fd_set(zval *stream_array, fd_set *fds) |
640 | 0 | { |
641 | 0 | zval *elem, *dest_elem; |
642 | 0 | HashTable *ht; |
643 | 0 | php_stream *stream; |
644 | 0 | int ret = 0; |
645 | 0 | zend_string *key; |
646 | 0 | zend_ulong num_ind; |
647 | |
|
648 | 0 | if (Z_TYPE_P(stream_array) != IS_ARRAY) { |
649 | 0 | return 0; |
650 | 0 | } |
651 | 0 | ht = zend_new_array(zend_hash_num_elements(Z_ARRVAL_P(stream_array))); |
652 | |
|
653 | 0 | ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) { |
654 | 0 | php_socket_t this_fd; |
655 | |
|
656 | 0 | ZVAL_DEREF(elem); |
657 | 0 | php_stream_from_zval_no_verify(stream, elem); |
658 | 0 | if (stream == NULL) { |
659 | 0 | continue; |
660 | 0 | } |
661 | | /* get the fd |
662 | | * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag |
663 | | * when casting. It is only used here so that the buffered data warning |
664 | | * is not displayed. |
665 | | */ |
666 | 0 | if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != SOCK_ERR) { |
667 | 0 | if (PHP_SAFE_FD_ISSET(this_fd, fds)) { |
668 | 0 | if (!key) { |
669 | 0 | dest_elem = zend_hash_index_update(ht, num_ind, elem); |
670 | 0 | } else { |
671 | 0 | dest_elem = zend_hash_update(ht, key, elem); |
672 | 0 | } |
673 | |
|
674 | 0 | zval_add_ref(dest_elem); |
675 | 0 | ret++; |
676 | 0 | continue; |
677 | 0 | } |
678 | 0 | } |
679 | 0 | } ZEND_HASH_FOREACH_END(); |
680 | | |
681 | | /* destroy old array and add new one */ |
682 | 0 | zval_ptr_dtor(stream_array); |
683 | 0 | ZVAL_ARR(stream_array, ht); |
684 | |
|
685 | 0 | return ret; |
686 | 0 | } |
687 | | |
688 | | static int stream_array_emulate_read_fd_set(zval *stream_array) |
689 | 0 | { |
690 | 0 | zval *elem, *dest_elem; |
691 | 0 | HashTable *ht; |
692 | 0 | php_stream *stream; |
693 | 0 | int ret = 0; |
694 | 0 | zend_ulong num_ind; |
695 | 0 | zend_string *key; |
696 | |
|
697 | 0 | if (Z_TYPE_P(stream_array) != IS_ARRAY) { |
698 | 0 | return 0; |
699 | 0 | } |
700 | 0 | ht = zend_new_array(zend_hash_num_elements(Z_ARRVAL_P(stream_array))); |
701 | |
|
702 | 0 | ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) { |
703 | 0 | ZVAL_DEREF(elem); |
704 | 0 | php_stream_from_zval_no_verify(stream, elem); |
705 | 0 | if (stream == NULL) { |
706 | 0 | continue; |
707 | 0 | } |
708 | 0 | if ((stream->writepos - stream->readpos) > 0) { |
709 | | /* allow readable non-descriptor based streams to participate in stream_select. |
710 | | * Non-descriptor streams will only "work" if they have previously buffered the |
711 | | * data. Not ideal, but better than nothing. |
712 | | * This branch of code also allows blocking streams with buffered data to |
713 | | * operate correctly in stream_select. |
714 | | * */ |
715 | 0 | if (!key) { |
716 | 0 | dest_elem = zend_hash_index_update(ht, num_ind, elem); |
717 | 0 | } else { |
718 | 0 | dest_elem = zend_hash_update(ht, key, elem); |
719 | 0 | } |
720 | 0 | zval_add_ref(dest_elem); |
721 | 0 | ret++; |
722 | 0 | continue; |
723 | 0 | } |
724 | 0 | } ZEND_HASH_FOREACH_END(); |
725 | |
|
726 | 0 | if (ret > 0) { |
727 | | /* destroy old array and add new one */ |
728 | 0 | zval_ptr_dtor(stream_array); |
729 | 0 | ZVAL_ARR(stream_array, ht); |
730 | 0 | } else { |
731 | 0 | zend_array_destroy(ht); |
732 | 0 | } |
733 | |
|
734 | 0 | return ret; |
735 | 0 | } |
736 | | /* }}} */ |
737 | | |
738 | | /* {{{ proto int|false stream_select(array &read_streams, array &write_streams, array &except_streams, int tv_sec[, int tv_usec]) |
739 | | Runs the select() system call on the sets of streams with a timeout specified by tv_sec and tv_usec */ |
740 | | PHP_FUNCTION(stream_select) |
741 | 0 | { |
742 | 0 | zval *r_array, *w_array, *e_array; |
743 | 0 | struct timeval tv, *tv_p = NULL; |
744 | 0 | fd_set rfds, wfds, efds; |
745 | 0 | php_socket_t max_fd = 0; |
746 | 0 | int retval, sets = 0; |
747 | 0 | zend_long sec, usec = 0; |
748 | 0 | zend_bool secnull; |
749 | 0 | int set_count, max_set_count = 0; |
750 | |
|
751 | 0 | ZEND_PARSE_PARAMETERS_START(4, 5) |
752 | 0 | Z_PARAM_ARRAY_EX2(r_array, 1, 1, 0) |
753 | 0 | Z_PARAM_ARRAY_EX2(w_array, 1, 1, 0) |
754 | 0 | Z_PARAM_ARRAY_EX2(e_array, 1, 1, 0) |
755 | 0 | Z_PARAM_LONG_OR_NULL(sec, secnull) |
756 | 0 | Z_PARAM_OPTIONAL |
757 | 0 | Z_PARAM_LONG(usec) |
758 | 0 | ZEND_PARSE_PARAMETERS_END(); |
759 | |
|
760 | 0 | FD_ZERO(&rfds); |
761 | 0 | FD_ZERO(&wfds); |
762 | 0 | FD_ZERO(&efds); |
763 | |
|
764 | 0 | if (r_array != NULL) { |
765 | 0 | set_count = stream_array_to_fd_set(r_array, &rfds, &max_fd); |
766 | 0 | if (set_count > max_set_count) |
767 | 0 | max_set_count = set_count; |
768 | 0 | sets += set_count; |
769 | 0 | } |
770 | |
|
771 | 0 | if (w_array != NULL) { |
772 | 0 | set_count = stream_array_to_fd_set(w_array, &wfds, &max_fd); |
773 | 0 | if (set_count > max_set_count) |
774 | 0 | max_set_count = set_count; |
775 | 0 | sets += set_count; |
776 | 0 | } |
777 | |
|
778 | 0 | if (e_array != NULL) { |
779 | 0 | set_count = stream_array_to_fd_set(e_array, &efds, &max_fd); |
780 | 0 | if (set_count > max_set_count) |
781 | 0 | max_set_count = set_count; |
782 | 0 | sets += set_count; |
783 | 0 | } |
784 | |
|
785 | 0 | if (!sets) { |
786 | 0 | zend_value_error("No stream arrays were passed"); |
787 | 0 | RETURN_THROWS(); |
788 | 0 | } |
789 | |
|
790 | 0 | PHP_SAFE_MAX_FD(max_fd, max_set_count); |
791 | | |
792 | | /* If seconds is not set to null, build the timeval, else we wait indefinitely */ |
793 | 0 | if (!secnull) { |
794 | 0 | if (sec < 0) { |
795 | 0 | zend_argument_value_error(4, "must be greater than or equal to 0"); |
796 | 0 | RETURN_THROWS(); |
797 | 0 | } else if (usec < 0) { |
798 | 0 | zend_argument_value_error(4, "must be greater than or equal to 0"); |
799 | 0 | RETURN_THROWS(); |
800 | 0 | } |
801 | | |
802 | | /* Windows, Solaris and BSD do not like microsecond values which are >= 1 sec */ |
803 | 0 | tv.tv_sec = (long)(sec + (usec / 1000000)); |
804 | 0 | tv.tv_usec = (long)(usec % 1000000); |
805 | 0 | tv_p = &tv; |
806 | 0 | } |
807 | | |
808 | | /* slight hack to support buffered data; if there is data sitting in the |
809 | | * read buffer of any of the streams in the read array, let's pretend |
810 | | * that we selected, but return only the readable sockets */ |
811 | 0 | if (r_array != NULL) { |
812 | 0 | retval = stream_array_emulate_read_fd_set(r_array); |
813 | 0 | if (retval > 0) { |
814 | 0 | if (w_array != NULL) { |
815 | 0 | zval_ptr_dtor(w_array); |
816 | 0 | ZVAL_EMPTY_ARRAY(w_array); |
817 | 0 | } |
818 | 0 | if (e_array != NULL) { |
819 | 0 | zval_ptr_dtor(e_array); |
820 | 0 | ZVAL_EMPTY_ARRAY(e_array); |
821 | 0 | } |
822 | 0 | RETURN_LONG(retval); |
823 | 0 | } |
824 | 0 | } |
825 | |
|
826 | 0 | retval = php_select(max_fd+1, &rfds, &wfds, &efds, tv_p); |
827 | |
|
828 | 0 | if (retval == -1) { |
829 | 0 | php_error_docref(NULL, E_WARNING, "Unable to select [%d]: %s (max_fd=%d)", |
830 | 0 | errno, strerror(errno), max_fd); |
831 | 0 | RETURN_FALSE; |
832 | 0 | } |
833 | |
|
834 | 0 | if (r_array != NULL) stream_array_from_fd_set(r_array, &rfds); |
835 | 0 | if (w_array != NULL) stream_array_from_fd_set(w_array, &wfds); |
836 | 0 | if (e_array != NULL) stream_array_from_fd_set(e_array, &efds); |
837 | |
|
838 | 0 | RETURN_LONG(retval); |
839 | 0 | } |
840 | | /* }}} */ |
841 | | |
842 | | /* {{{ stream_context related functions */ |
843 | | static void user_space_stream_notifier(php_stream_context *context, int notifycode, int severity, |
844 | | char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr) |
845 | 0 | { |
846 | 0 | zval *callback = &context->notifier->ptr; |
847 | 0 | zval retval; |
848 | 0 | zval zvs[6]; |
849 | 0 | int i; |
850 | |
|
851 | 0 | ZVAL_LONG(&zvs[0], notifycode); |
852 | 0 | ZVAL_LONG(&zvs[1], severity); |
853 | 0 | if (xmsg) { |
854 | 0 | ZVAL_STRING(&zvs[2], xmsg); |
855 | 0 | } else { |
856 | 0 | ZVAL_NULL(&zvs[2]); |
857 | 0 | } |
858 | 0 | ZVAL_LONG(&zvs[3], xcode); |
859 | 0 | ZVAL_LONG(&zvs[4], bytes_sofar); |
860 | 0 | ZVAL_LONG(&zvs[5], bytes_max); |
861 | |
|
862 | 0 | if (FAILURE == call_user_function_ex(NULL, NULL, callback, &retval, 6, zvs, 0, NULL)) { |
863 | 0 | php_error_docref(NULL, E_WARNING, "Failed to call user notifier"); |
864 | 0 | } |
865 | 0 | for (i = 0; i < 6; i++) { |
866 | 0 | zval_ptr_dtor(&zvs[i]); |
867 | 0 | } |
868 | 0 | zval_ptr_dtor(&retval); |
869 | 0 | } |
870 | | |
871 | | static void user_space_stream_notifier_dtor(php_stream_notifier *notifier) |
872 | 0 | { |
873 | 0 | if (notifier && Z_TYPE(notifier->ptr) != IS_UNDEF) { |
874 | 0 | zval_ptr_dtor(¬ifier->ptr); |
875 | 0 | ZVAL_UNDEF(¬ifier->ptr); |
876 | 0 | } |
877 | 0 | } |
878 | | |
879 | | static int parse_context_options(php_stream_context *context, zval *options) |
880 | 0 | { |
881 | 0 | zval *wval, *oval; |
882 | 0 | zend_string *wkey, *okey; |
883 | 0 | int ret = SUCCESS; |
884 | |
|
885 | 0 | ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), wkey, wval) { |
886 | 0 | ZVAL_DEREF(wval); |
887 | 0 | if (wkey && Z_TYPE_P(wval) == IS_ARRAY) { |
888 | 0 | ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(wval), okey, oval) { |
889 | 0 | if (okey) { |
890 | 0 | php_stream_context_set_option(context, ZSTR_VAL(wkey), ZSTR_VAL(okey), oval); |
891 | 0 | } |
892 | 0 | } ZEND_HASH_FOREACH_END(); |
893 | 0 | } else { |
894 | 0 | zend_value_error("Options should have the form [\"wrappername\"][\"optionname\"] = $value"); |
895 | 0 | return FAILURE; |
896 | 0 | } |
897 | 0 | } ZEND_HASH_FOREACH_END(); |
898 | |
|
899 | 0 | return ret; |
900 | 0 | } |
901 | | |
902 | | static int parse_context_params(php_stream_context *context, zval *params) |
903 | 0 | { |
904 | 0 | int ret = SUCCESS; |
905 | 0 | zval *tmp; |
906 | |
|
907 | 0 | if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(params), "notification", sizeof("notification")-1))) { |
908 | |
|
909 | 0 | if (context->notifier) { |
910 | 0 | php_stream_notification_free(context->notifier); |
911 | 0 | context->notifier = NULL; |
912 | 0 | } |
913 | |
|
914 | 0 | context->notifier = php_stream_notification_alloc(); |
915 | 0 | context->notifier->func = user_space_stream_notifier; |
916 | 0 | ZVAL_COPY(&context->notifier->ptr, tmp); |
917 | 0 | context->notifier->dtor = user_space_stream_notifier_dtor; |
918 | 0 | } |
919 | 0 | if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(params), "options", sizeof("options")-1))) { |
920 | 0 | if (Z_TYPE_P(tmp) == IS_ARRAY) { |
921 | 0 | return parse_context_options(context, tmp); |
922 | 0 | } else { |
923 | 0 | zend_type_error("Invalid stream/context parameter"); |
924 | 0 | return FAILURE; |
925 | 0 | } |
926 | 0 | } |
927 | | |
928 | 0 | return ret; |
929 | 0 | } |
930 | | |
931 | | /* given a zval which is either a stream or a context, return the underlying |
932 | | * stream_context. If it is a stream that does not have a context assigned, it |
933 | | * will create and assign a context and return that. */ |
934 | | static php_stream_context *decode_context_param(zval *contextresource) |
935 | 0 | { |
936 | 0 | php_stream_context *context = NULL; |
937 | |
|
938 | 0 | context = zend_fetch_resource_ex(contextresource, NULL, php_le_stream_context()); |
939 | 0 | if (context == NULL) { |
940 | 0 | php_stream *stream; |
941 | |
|
942 | 0 | stream = zend_fetch_resource2_ex(contextresource, NULL, php_file_le_stream(), php_file_le_pstream()); |
943 | |
|
944 | 0 | if (stream) { |
945 | 0 | context = PHP_STREAM_CONTEXT(stream); |
946 | 0 | if (context == NULL) { |
947 | | /* Only way this happens is if file is opened with NO_DEFAULT_CONTEXT |
948 | | param, but then something is called which requires a context. |
949 | | Don't give them the default one though since they already said they |
950 | | didn't want it. */ |
951 | 0 | context = php_stream_context_alloc(); |
952 | 0 | stream->ctx = context->res; |
953 | 0 | } |
954 | 0 | } |
955 | 0 | } |
956 | |
|
957 | 0 | return context; |
958 | 0 | } |
959 | | /* }}} */ |
960 | | |
961 | | /* {{{ proto array stream_context_get_options(resource context|resource stream) |
962 | | Retrieve options for a stream/wrapper/context */ |
963 | | PHP_FUNCTION(stream_context_get_options) |
964 | 0 | { |
965 | 0 | zval *zcontext; |
966 | 0 | php_stream_context *context; |
967 | |
|
968 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
969 | 0 | Z_PARAM_RESOURCE(zcontext) |
970 | 0 | ZEND_PARSE_PARAMETERS_END(); |
971 | |
|
972 | 0 | context = decode_context_param(zcontext); |
973 | 0 | if (!context) { |
974 | 0 | zend_argument_type_error(1, "must be a valid stream/context"); |
975 | 0 | RETURN_THROWS(); |
976 | 0 | } |
977 | |
|
978 | 0 | ZVAL_COPY(return_value, &context->options); |
979 | 0 | } |
980 | | /* }}} */ |
981 | | |
982 | | /* {{{ proto bool stream_context_set_option(resource context|resource stream, string wrappername, string optionname, mixed value) |
983 | | Set an option for a wrapper */ |
984 | | PHP_FUNCTION(stream_context_set_option) |
985 | 0 | { |
986 | 0 | zval *zcontext = NULL; |
987 | 0 | php_stream_context *context; |
988 | |
|
989 | 0 | if (ZEND_NUM_ARGS() == 2) { |
990 | 0 | zval *options; |
991 | |
|
992 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
993 | 0 | Z_PARAM_RESOURCE(zcontext) |
994 | 0 | Z_PARAM_ARRAY(options) |
995 | 0 | ZEND_PARSE_PARAMETERS_END(); |
996 | | |
997 | | /* figure out where the context is coming from exactly */ |
998 | 0 | if (!(context = decode_context_param(zcontext))) { |
999 | 0 | zend_argument_type_error(1, "must be a valid stream/context"); |
1000 | 0 | RETURN_THROWS(); |
1001 | 0 | } |
1002 | |
|
1003 | 0 | RETURN_BOOL(parse_context_options(context, options) == SUCCESS); |
1004 | 0 | } else { |
1005 | 0 | zval *zvalue; |
1006 | 0 | char *wrappername, *optionname; |
1007 | 0 | size_t wrapperlen, optionlen; |
1008 | |
|
1009 | 0 | ZEND_PARSE_PARAMETERS_START(4, 4) |
1010 | 0 | Z_PARAM_RESOURCE(zcontext) |
1011 | 0 | Z_PARAM_STRING(wrappername, wrapperlen) |
1012 | 0 | Z_PARAM_STRING(optionname, optionlen) |
1013 | 0 | Z_PARAM_ZVAL(zvalue) |
1014 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1015 | | |
1016 | | /* figure out where the context is coming from exactly */ |
1017 | 0 | if (!(context = decode_context_param(zcontext))) { |
1018 | 0 | zend_argument_type_error(1, "must be a valid stream/context"); |
1019 | 0 | RETURN_THROWS(); |
1020 | 0 | } |
1021 | |
|
1022 | 0 | RETURN_BOOL(php_stream_context_set_option(context, wrappername, optionname, zvalue) == SUCCESS); |
1023 | 0 | } |
1024 | 0 | } |
1025 | | /* }}} */ |
1026 | | |
1027 | | /* {{{ proto bool stream_context_set_params(resource context|resource stream, array options) |
1028 | | Set parameters for a file context */ |
1029 | | PHP_FUNCTION(stream_context_set_params) |
1030 | 0 | { |
1031 | 0 | zval *params, *zcontext; |
1032 | 0 | php_stream_context *context; |
1033 | |
|
1034 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1035 | 0 | Z_PARAM_RESOURCE(zcontext) |
1036 | 0 | Z_PARAM_ARRAY(params) |
1037 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1038 | |
|
1039 | 0 | context = decode_context_param(zcontext); |
1040 | 0 | if (!context) { |
1041 | 0 | zend_argument_type_error(1, "must be a valid stream/context"); |
1042 | 0 | RETURN_THROWS(); |
1043 | 0 | } |
1044 | |
|
1045 | 0 | RETVAL_BOOL(parse_context_params(context, params) == SUCCESS); |
1046 | 0 | } |
1047 | | /* }}} */ |
1048 | | |
1049 | | /* {{{ proto array stream_context_get_params(resource context|resource stream) |
1050 | | Get parameters of a file context */ |
1051 | | PHP_FUNCTION(stream_context_get_params) |
1052 | 0 | { |
1053 | 0 | zval *zcontext; |
1054 | 0 | php_stream_context *context; |
1055 | |
|
1056 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1057 | 0 | Z_PARAM_RESOURCE(zcontext) |
1058 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1059 | |
|
1060 | 0 | context = decode_context_param(zcontext); |
1061 | 0 | if (!context) { |
1062 | 0 | zend_argument_type_error(1, "must be a valid stream/context"); |
1063 | 0 | RETURN_THROWS(); |
1064 | 0 | } |
1065 | |
|
1066 | 0 | array_init(return_value); |
1067 | 0 | if (context->notifier && Z_TYPE(context->notifier->ptr) != IS_UNDEF && context->notifier->func == user_space_stream_notifier) { |
1068 | 0 | Z_TRY_ADDREF(context->notifier->ptr); |
1069 | 0 | add_assoc_zval_ex(return_value, "notification", sizeof("notification")-1, &context->notifier->ptr); |
1070 | 0 | } |
1071 | 0 | Z_TRY_ADDREF(context->options); |
1072 | 0 | add_assoc_zval_ex(return_value, "options", sizeof("options")-1, &context->options); |
1073 | 0 | } |
1074 | | /* }}} */ |
1075 | | |
1076 | | /* {{{ proto resource stream_context_get_default([array options]) |
1077 | | Get a handle on the default file/stream context and optionally set parameters */ |
1078 | | PHP_FUNCTION(stream_context_get_default) |
1079 | 0 | { |
1080 | 0 | zval *params = NULL; |
1081 | 0 | php_stream_context *context; |
1082 | |
|
1083 | 0 | ZEND_PARSE_PARAMETERS_START(0, 1) |
1084 | 0 | Z_PARAM_OPTIONAL |
1085 | 0 | Z_PARAM_ARRAY(params) |
1086 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1087 | |
|
1088 | 0 | if (FG(default_context) == NULL) { |
1089 | 0 | FG(default_context) = php_stream_context_alloc(); |
1090 | 0 | } |
1091 | 0 | context = FG(default_context); |
1092 | |
|
1093 | 0 | if (params) { |
1094 | 0 | if (parse_context_options(context, params) == FAILURE) { |
1095 | 0 | RETURN_THROWS(); |
1096 | 0 | } |
1097 | 0 | } |
1098 | |
|
1099 | 0 | php_stream_context_to_zval(context, return_value); |
1100 | 0 | } |
1101 | | /* }}} */ |
1102 | | |
1103 | | /* {{{ proto resource stream_context_set_default(array options) |
1104 | | Set default file/stream context, returns the context as a resource */ |
1105 | | PHP_FUNCTION(stream_context_set_default) |
1106 | 0 | { |
1107 | 0 | zval *options = NULL; |
1108 | 0 | php_stream_context *context; |
1109 | |
|
1110 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1111 | 0 | Z_PARAM_ARRAY(options) |
1112 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1113 | |
|
1114 | 0 | if (FG(default_context) == NULL) { |
1115 | 0 | FG(default_context) = php_stream_context_alloc(); |
1116 | 0 | } |
1117 | 0 | context = FG(default_context); |
1118 | |
|
1119 | 0 | if (parse_context_options(context, options) == FAILURE) { |
1120 | 0 | RETURN_THROWS(); |
1121 | 0 | } |
1122 | |
|
1123 | 0 | php_stream_context_to_zval(context, return_value); |
1124 | 0 | } |
1125 | | /* }}} */ |
1126 | | |
1127 | | /* {{{ proto resource stream_context_create([array options[, array params]]) |
1128 | | Create a file context and optionally set parameters */ |
1129 | | PHP_FUNCTION(stream_context_create) |
1130 | 0 | { |
1131 | 0 | zval *options = NULL, *params = NULL; |
1132 | 0 | php_stream_context *context; |
1133 | |
|
1134 | 0 | ZEND_PARSE_PARAMETERS_START(0, 2) |
1135 | 0 | Z_PARAM_OPTIONAL |
1136 | 0 | Z_PARAM_ARRAY_OR_NULL(options) |
1137 | 0 | Z_PARAM_ARRAY_OR_NULL(params) |
1138 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1139 | |
|
1140 | 0 | context = php_stream_context_alloc(); |
1141 | |
|
1142 | 0 | if (options) { |
1143 | 0 | parse_context_options(context, options); |
1144 | 0 | } |
1145 | |
|
1146 | 0 | if (params) { |
1147 | 0 | parse_context_params(context, params); |
1148 | 0 | } |
1149 | |
|
1150 | 0 | RETURN_RES(context->res); |
1151 | 0 | } |
1152 | | /* }}} */ |
1153 | | |
1154 | | /* {{{ streams filter functions */ |
1155 | | static void apply_filter_to_stream(int append, INTERNAL_FUNCTION_PARAMETERS) |
1156 | 0 | { |
1157 | 0 | zval *zstream; |
1158 | 0 | php_stream *stream; |
1159 | 0 | char *filtername; |
1160 | 0 | size_t filternamelen; |
1161 | 0 | zend_long read_write = 0; |
1162 | 0 | zval *filterparams = NULL; |
1163 | 0 | php_stream_filter *filter = NULL; |
1164 | 0 | int ret; |
1165 | |
|
1166 | 0 | ZEND_PARSE_PARAMETERS_START(2, 4) |
1167 | 0 | Z_PARAM_RESOURCE(zstream) |
1168 | 0 | Z_PARAM_STRING(filtername, filternamelen) |
1169 | 0 | Z_PARAM_OPTIONAL |
1170 | 0 | Z_PARAM_LONG(read_write) |
1171 | 0 | Z_PARAM_ZVAL(filterparams) |
1172 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1173 | |
|
1174 | 0 | php_stream_from_zval(stream, zstream); |
1175 | |
|
1176 | 0 | if ((read_write & PHP_STREAM_FILTER_ALL) == 0) { |
1177 | | /* Chain not specified. |
1178 | | * Examine stream->mode to determine which filters are needed |
1179 | | * There's no harm in attaching a filter to an unused chain, |
1180 | | * but why waste the memory and clock cycles? |
1181 | | */ |
1182 | 0 | if (strchr(stream->mode, 'r') || strchr(stream->mode, '+')) { |
1183 | 0 | read_write |= PHP_STREAM_FILTER_READ; |
1184 | 0 | } |
1185 | 0 | if (strchr(stream->mode, 'w') || strchr(stream->mode, '+') || strchr(stream->mode, 'a')) { |
1186 | 0 | read_write |= PHP_STREAM_FILTER_WRITE; |
1187 | 0 | } |
1188 | 0 | } |
1189 | |
|
1190 | 0 | if (read_write & PHP_STREAM_FILTER_READ) { |
1191 | 0 | filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream)); |
1192 | 0 | if (filter == NULL) { |
1193 | 0 | RETURN_FALSE; |
1194 | 0 | } |
1195 | |
|
1196 | 0 | if (append) { |
1197 | 0 | ret = php_stream_filter_append_ex(&stream->readfilters, filter); |
1198 | 0 | } else { |
1199 | 0 | ret = php_stream_filter_prepend_ex(&stream->readfilters, filter); |
1200 | 0 | } |
1201 | 0 | if (ret != SUCCESS) { |
1202 | 0 | php_stream_filter_remove(filter, 1); |
1203 | 0 | RETURN_FALSE; |
1204 | 0 | } |
1205 | 0 | } |
1206 | |
|
1207 | 0 | if (read_write & PHP_STREAM_FILTER_WRITE) { |
1208 | 0 | filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream)); |
1209 | 0 | if (filter == NULL) { |
1210 | 0 | RETURN_FALSE; |
1211 | 0 | } |
1212 | |
|
1213 | 0 | if (append) { |
1214 | 0 | ret = php_stream_filter_append_ex(&stream->writefilters, filter); |
1215 | 0 | } else { |
1216 | 0 | ret = php_stream_filter_prepend_ex(&stream->writefilters, filter); |
1217 | 0 | } |
1218 | 0 | if (ret != SUCCESS) { |
1219 | 0 | php_stream_filter_remove(filter, 1); |
1220 | 0 | RETURN_FALSE; |
1221 | 0 | } |
1222 | 0 | } |
1223 | |
|
1224 | 0 | if (filter) { |
1225 | 0 | filter->res = zend_register_resource(filter, php_file_le_stream_filter()); |
1226 | 0 | GC_ADDREF(filter->res); |
1227 | 0 | RETURN_RES(filter->res); |
1228 | 0 | } else { |
1229 | 0 | RETURN_FALSE; |
1230 | 0 | } |
1231 | 0 | } |
1232 | | /* }}} */ |
1233 | | |
1234 | | /* {{{ proto resource stream_filter_prepend(resource stream, string filtername[, int read_write[, string filterparams]]) |
1235 | | Prepend a filter to a stream */ |
1236 | | PHP_FUNCTION(stream_filter_prepend) |
1237 | 0 | { |
1238 | 0 | apply_filter_to_stream(0, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
1239 | 0 | } |
1240 | | /* }}} */ |
1241 | | |
1242 | | /* {{{ proto resource stream_filter_append(resource stream, string filtername[, int read_write[, string filterparams]]) |
1243 | | Append a filter to a stream */ |
1244 | | PHP_FUNCTION(stream_filter_append) |
1245 | 0 | { |
1246 | 0 | apply_filter_to_stream(1, INTERNAL_FUNCTION_PARAM_PASSTHRU); |
1247 | 0 | } |
1248 | | /* }}} */ |
1249 | | |
1250 | | /* {{{ proto bool stream_filter_remove(resource stream_filter) |
1251 | | Flushes any data in the filter's internal buffer, removes it from the chain, and frees the resource */ |
1252 | | PHP_FUNCTION(stream_filter_remove) |
1253 | 0 | { |
1254 | 0 | zval *zfilter; |
1255 | 0 | php_stream_filter *filter; |
1256 | |
|
1257 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1258 | 0 | Z_PARAM_RESOURCE(zfilter) |
1259 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1260 | |
|
1261 | 0 | filter = zend_fetch_resource(Z_RES_P(zfilter), "stream filter", php_file_le_stream_filter()); |
1262 | 0 | if (!filter) { |
1263 | 0 | RETURN_THROWS(); |
1264 | 0 | } |
1265 | |
|
1266 | 0 | if (php_stream_filter_flush(filter, 1) == FAILURE) { |
1267 | 0 | php_error_docref(NULL, E_WARNING, "Unable to flush filter, not removing"); |
1268 | 0 | RETURN_FALSE; |
1269 | 0 | } |
1270 | |
|
1271 | 0 | if (zend_list_close(Z_RES_P(zfilter)) == FAILURE) { |
1272 | 0 | php_error_docref(NULL, E_WARNING, "Could not invalidate filter, not removing"); |
1273 | 0 | RETURN_FALSE; |
1274 | 0 | } else { |
1275 | 0 | php_stream_filter_remove(filter, 1); |
1276 | 0 | RETURN_TRUE; |
1277 | 0 | } |
1278 | 0 | } |
1279 | | /* }}} */ |
1280 | | |
1281 | | /* {{{ proto string stream_get_line(resource stream, int maxlen [, string ending]) |
1282 | | Read up to maxlen bytes from a stream or until the ending string is found */ |
1283 | | PHP_FUNCTION(stream_get_line) |
1284 | 0 | { |
1285 | 0 | char *str = NULL; |
1286 | 0 | size_t str_len = 0; |
1287 | 0 | zend_long max_length; |
1288 | 0 | zval *zstream; |
1289 | 0 | zend_string *buf; |
1290 | 0 | php_stream *stream; |
1291 | |
|
1292 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
1293 | 0 | Z_PARAM_RESOURCE(zstream) |
1294 | 0 | Z_PARAM_LONG(max_length) |
1295 | 0 | Z_PARAM_OPTIONAL |
1296 | 0 | Z_PARAM_STRING(str, str_len) |
1297 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1298 | |
|
1299 | 0 | if (max_length < 0) { |
1300 | 0 | zend_argument_value_error(2, "must be greater than or equal to 0"); |
1301 | 0 | RETURN_THROWS(); |
1302 | 0 | } |
1303 | 0 | if (!max_length) { |
1304 | 0 | max_length = PHP_SOCK_CHUNK_SIZE; |
1305 | 0 | } |
1306 | |
|
1307 | 0 | php_stream_from_zval(stream, zstream); |
1308 | |
|
1309 | 0 | if ((buf = php_stream_get_record(stream, max_length, str, str_len))) { |
1310 | 0 | RETURN_STR(buf); |
1311 | 0 | } else { |
1312 | 0 | RETURN_FALSE; |
1313 | 0 | } |
1314 | 0 | } |
1315 | | |
1316 | | /* }}} */ |
1317 | | |
1318 | | /* {{{ proto bool stream_set_blocking(resource socket, bool mode) |
1319 | | Set blocking/non-blocking mode on a socket or stream */ |
1320 | | PHP_FUNCTION(stream_set_blocking) |
1321 | 0 | { |
1322 | 0 | zval *zstream; |
1323 | 0 | zend_bool block; |
1324 | 0 | php_stream *stream; |
1325 | |
|
1326 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1327 | 0 | Z_PARAM_RESOURCE(zstream) |
1328 | 0 | Z_PARAM_BOOL(block) |
1329 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1330 | |
|
1331 | 0 | php_stream_from_zval(stream, zstream); |
1332 | |
|
1333 | 0 | if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, block, NULL) == -1) { |
1334 | 0 | RETURN_FALSE; |
1335 | 0 | } |
1336 | |
|
1337 | 0 | RETURN_TRUE; |
1338 | 0 | } |
1339 | | |
1340 | | /* }}} */ |
1341 | | |
1342 | | /* {{{ proto bool stream_set_timeout(resource stream, int seconds [, int microseconds]) |
1343 | | Set timeout on stream read to seconds + microseonds */ |
1344 | | #if HAVE_SYS_TIME_H || defined(PHP_WIN32) |
1345 | | PHP_FUNCTION(stream_set_timeout) |
1346 | 0 | { |
1347 | 0 | zval *socket; |
1348 | 0 | zend_long seconds, microseconds = 0; |
1349 | 0 | struct timeval t; |
1350 | 0 | php_stream *stream; |
1351 | 0 | int argc = ZEND_NUM_ARGS(); |
1352 | |
|
1353 | 0 | ZEND_PARSE_PARAMETERS_START(2, 3) |
1354 | 0 | Z_PARAM_RESOURCE(socket) |
1355 | 0 | Z_PARAM_LONG(seconds) |
1356 | 0 | Z_PARAM_OPTIONAL |
1357 | 0 | Z_PARAM_LONG(microseconds) |
1358 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1359 | |
|
1360 | 0 | php_stream_from_zval(stream, socket); |
1361 | |
|
1362 | | #ifdef PHP_WIN32 |
1363 | | t.tv_sec = (long)seconds; |
1364 | | |
1365 | | if (argc == 3) { |
1366 | | t.tv_usec = (long)(microseconds % 1000000); |
1367 | | t.tv_sec +=(long)(microseconds / 1000000); |
1368 | | } else { |
1369 | | t.tv_usec = 0; |
1370 | | } |
1371 | | #else |
1372 | 0 | t.tv_sec = seconds; |
1373 | |
|
1374 | 0 | if (argc == 3) { |
1375 | 0 | t.tv_usec = microseconds % 1000000; |
1376 | 0 | t.tv_sec += microseconds / 1000000; |
1377 | 0 | } else { |
1378 | 0 | t.tv_usec = 0; |
1379 | 0 | } |
1380 | 0 | #endif |
1381 | |
|
1382 | 0 | if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &t)) { |
1383 | 0 | RETURN_TRUE; |
1384 | 0 | } |
1385 | |
|
1386 | 0 | RETURN_FALSE; |
1387 | 0 | } |
1388 | | #endif /* HAVE_SYS_TIME_H || defined(PHP_WIN32) */ |
1389 | | /* }}} */ |
1390 | | |
1391 | | /* {{{ proto int stream_set_write_buffer(resource fp, int buffer) |
1392 | | Set file write buffer */ |
1393 | | PHP_FUNCTION(stream_set_write_buffer) |
1394 | 0 | { |
1395 | 0 | zval *arg1; |
1396 | 0 | int ret; |
1397 | 0 | zend_long arg2; |
1398 | 0 | size_t buff; |
1399 | 0 | php_stream *stream; |
1400 | |
|
1401 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1402 | 0 | Z_PARAM_RESOURCE(arg1) |
1403 | 0 | Z_PARAM_LONG(arg2) |
1404 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1405 | |
|
1406 | 0 | php_stream_from_zval(stream, arg1); |
1407 | |
|
1408 | 0 | buff = arg2; |
1409 | | |
1410 | | /* if buff is 0 then set to non-buffered */ |
1411 | 0 | if (buff == 0) { |
1412 | 0 | ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_NONE, NULL); |
1413 | 0 | } else { |
1414 | 0 | ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buff); |
1415 | 0 | } |
1416 | |
|
1417 | 0 | RETURN_LONG(ret == 0 ? 0 : EOF); |
1418 | 0 | } |
1419 | | /* }}} */ |
1420 | | |
1421 | | /* {{{ proto int stream_set_chunk_size(resource fp, int chunk_size) |
1422 | | Set the stream chunk size */ |
1423 | | PHP_FUNCTION(stream_set_chunk_size) |
1424 | 0 | { |
1425 | 0 | int ret; |
1426 | 0 | zend_long csize; |
1427 | 0 | zval *zstream; |
1428 | 0 | php_stream *stream; |
1429 | |
|
1430 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1431 | 0 | Z_PARAM_RESOURCE(zstream) |
1432 | 0 | Z_PARAM_LONG(csize) |
1433 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1434 | |
|
1435 | 0 | if (csize <= 0) { |
1436 | 0 | zend_argument_value_error(2, "must be greater than 0"); |
1437 | 0 | RETURN_THROWS(); |
1438 | 0 | } |
1439 | | /* stream.chunk_size is actually a size_t, but php_stream_set_option |
1440 | | * can only use an int to accept the new value and return the old one. |
1441 | | * In any case, values larger than INT_MAX for a chunk size make no sense. |
1442 | | */ |
1443 | 0 | if (csize > INT_MAX) { |
1444 | 0 | zend_argument_value_error(2, "is too large"); |
1445 | 0 | RETURN_THROWS(); |
1446 | 0 | } |
1447 | |
|
1448 | 0 | php_stream_from_zval(stream, zstream); |
1449 | |
|
1450 | 0 | ret = php_stream_set_option(stream, PHP_STREAM_OPTION_SET_CHUNK_SIZE, (int)csize, NULL); |
1451 | |
|
1452 | 0 | RETURN_LONG(ret > 0 ? (zend_long)ret : (zend_long)EOF); |
1453 | 0 | } |
1454 | | /* }}} */ |
1455 | | |
1456 | | /* {{{ proto int stream_set_read_buffer(resource fp, int buffer) |
1457 | | Set file read buffer */ |
1458 | | PHP_FUNCTION(stream_set_read_buffer) |
1459 | 0 | { |
1460 | 0 | zval *arg1; |
1461 | 0 | int ret; |
1462 | 0 | zend_long arg2; |
1463 | 0 | size_t buff; |
1464 | 0 | php_stream *stream; |
1465 | |
|
1466 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1467 | 0 | Z_PARAM_RESOURCE(arg1) |
1468 | 0 | Z_PARAM_LONG(arg2) |
1469 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1470 | |
|
1471 | 0 | php_stream_from_zval(stream, arg1); |
1472 | |
|
1473 | 0 | buff = arg2; |
1474 | | |
1475 | | /* if buff is 0 then set to non-buffered */ |
1476 | 0 | if (buff == 0) { |
1477 | 0 | ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL); |
1478 | 0 | } else { |
1479 | 0 | ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_FULL, &buff); |
1480 | 0 | } |
1481 | |
|
1482 | 0 | RETURN_LONG(ret == 0 ? 0 : EOF); |
1483 | 0 | } |
1484 | | /* }}} */ |
1485 | | |
1486 | | /* {{{ proto int stream_socket_enable_crypto(resource stream, bool enable [, int cryptokind [, resource sessionstream]]) |
1487 | | Enable or disable a specific kind of crypto on the stream */ |
1488 | | PHP_FUNCTION(stream_socket_enable_crypto) |
1489 | 0 | { |
1490 | 0 | zend_long cryptokind = 0; |
1491 | 0 | zval *zstream, *zsessstream = NULL; |
1492 | 0 | php_stream *stream, *sessstream = NULL; |
1493 | 0 | zend_bool enable, cryptokindnull = 1; |
1494 | 0 | int ret; |
1495 | |
|
1496 | 0 | ZEND_PARSE_PARAMETERS_START(2, 4) |
1497 | 0 | Z_PARAM_RESOURCE(zstream) |
1498 | 0 | Z_PARAM_BOOL(enable) |
1499 | 0 | Z_PARAM_OPTIONAL |
1500 | 0 | Z_PARAM_LONG_OR_NULL(cryptokind, cryptokindnull) |
1501 | 0 | Z_PARAM_RESOURCE_OR_NULL(zsessstream) |
1502 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1503 | |
|
1504 | 0 | php_stream_from_zval(stream, zstream); |
1505 | |
|
1506 | 0 | if (enable) { |
1507 | 0 | if (cryptokindnull) { |
1508 | 0 | zval *val; |
1509 | |
|
1510 | 0 | if (!GET_CTX_OPT(stream, "ssl", "crypto_method", val)) { |
1511 | 0 | zend_argument_value_error(3, "must be specified when enabling encryption"); |
1512 | 0 | RETURN_THROWS(); |
1513 | 0 | } |
1514 | |
|
1515 | 0 | cryptokind = Z_LVAL_P(val); |
1516 | 0 | } |
1517 | |
|
1518 | 0 | if (zsessstream) { |
1519 | 0 | php_stream_from_zval(sessstream, zsessstream); |
1520 | 0 | } |
1521 | |
|
1522 | 0 | if (php_stream_xport_crypto_setup(stream, cryptokind, sessstream) < 0) { |
1523 | 0 | RETURN_FALSE; |
1524 | 0 | } |
1525 | 0 | } |
1526 | |
|
1527 | 0 | ret = php_stream_xport_crypto_enable(stream, enable); |
1528 | 0 | switch (ret) { |
1529 | 0 | case -1: |
1530 | 0 | RETURN_FALSE; |
1531 | |
|
1532 | 0 | case 0: |
1533 | 0 | RETURN_LONG(0); |
1534 | |
|
1535 | 0 | default: |
1536 | 0 | RETURN_TRUE; |
1537 | 0 | } |
1538 | 0 | } |
1539 | | /* }}} */ |
1540 | | |
1541 | | /* {{{ proto string|false stream_resolve_include_path(string filename) |
1542 | | Determine what file will be opened by calls to fopen() with a relative path */ |
1543 | | PHP_FUNCTION(stream_resolve_include_path) |
1544 | 0 | { |
1545 | 0 | char *filename; |
1546 | 0 | size_t filename_len; |
1547 | 0 | zend_string *resolved_path; |
1548 | |
|
1549 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1550 | 0 | Z_PARAM_PATH(filename, filename_len) |
1551 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1552 | |
|
1553 | 0 | resolved_path = zend_resolve_path(filename, filename_len); |
1554 | |
|
1555 | 0 | if (resolved_path) { |
1556 | 0 | RETURN_STR(resolved_path); |
1557 | 0 | } |
1558 | 0 | RETURN_FALSE; |
1559 | 0 | } |
1560 | | /* }}} */ |
1561 | | |
1562 | | /* {{{ proto bool stream_is_local(resource stream|string url) U |
1563 | | */ |
1564 | | PHP_FUNCTION(stream_is_local) |
1565 | 0 | { |
1566 | 0 | zval *zstream; |
1567 | 0 | php_stream *stream = NULL; |
1568 | 0 | php_stream_wrapper *wrapper = NULL; |
1569 | |
|
1570 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1571 | 0 | Z_PARAM_ZVAL(zstream) |
1572 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1573 | |
|
1574 | 0 | if (Z_TYPE_P(zstream) == IS_RESOURCE) { |
1575 | 0 | php_stream_from_zval(stream, zstream); |
1576 | 0 | if (stream == NULL) { |
1577 | 0 | RETURN_FALSE; |
1578 | 0 | } |
1579 | 0 | wrapper = stream->wrapper; |
1580 | 0 | } else { |
1581 | 0 | if (!try_convert_to_string(zstream)) { |
1582 | 0 | RETURN_THROWS(); |
1583 | 0 | } |
1584 | |
|
1585 | 0 | wrapper = php_stream_locate_url_wrapper(Z_STRVAL_P(zstream), NULL, 0); |
1586 | 0 | } |
1587 | |
|
1588 | 0 | if (!wrapper) { |
1589 | 0 | RETURN_FALSE; |
1590 | 0 | } |
1591 | |
|
1592 | 0 | RETURN_BOOL(wrapper->is_url==0); |
1593 | 0 | } |
1594 | | /* }}} */ |
1595 | | |
1596 | | /* {{{ proto bool stream_supports_lock(resource stream) |
1597 | | Tells whether the stream supports locking through flock(). */ |
1598 | | PHP_FUNCTION(stream_supports_lock) |
1599 | 0 | { |
1600 | 0 | php_stream *stream; |
1601 | 0 | zval *zsrc; |
1602 | |
|
1603 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1604 | 0 | Z_PARAM_RESOURCE(zsrc) |
1605 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1606 | |
|
1607 | 0 | php_stream_from_zval(stream, zsrc); |
1608 | |
|
1609 | 0 | if (!php_stream_supports_lock(stream)) { |
1610 | 0 | RETURN_FALSE; |
1611 | 0 | } |
1612 | |
|
1613 | 0 | RETURN_TRUE; |
1614 | 0 | } |
1615 | | |
1616 | | /* {{{ proto bool stream_isatty(resource stream) |
1617 | | Check if a stream is a TTY. |
1618 | | */ |
1619 | | PHP_FUNCTION(stream_isatty) |
1620 | 0 | { |
1621 | 0 | zval *zsrc; |
1622 | 0 | php_stream *stream; |
1623 | 0 | php_socket_t fileno; |
1624 | |
|
1625 | 0 | ZEND_PARSE_PARAMETERS_START(1, 1) |
1626 | 0 | Z_PARAM_RESOURCE(zsrc) |
1627 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1628 | |
|
1629 | 0 | php_stream_from_zval(stream, zsrc); |
1630 | |
|
1631 | 0 | if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) { |
1632 | 0 | php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0); |
1633 | 0 | } else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) { |
1634 | 0 | php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0); |
1635 | 0 | } else { |
1636 | 0 | RETURN_FALSE; |
1637 | 0 | } |
1638 | |
|
1639 | | #ifdef PHP_WIN32 |
1640 | | /* Check if the Windows standard handle is redirected to file */ |
1641 | | RETVAL_BOOL(php_win32_console_fileno_is_console(fileno)); |
1642 | | #elif HAVE_UNISTD_H |
1643 | | /* Check if the file descriptor identifier is a terminal */ |
1644 | 0 | RETVAL_BOOL(isatty(fileno)); |
1645 | | #else |
1646 | | { |
1647 | | zend_stat_t stat = {0}; |
1648 | | RETVAL_BOOL(zend_fstat(fileno, &stat) == 0 && (stat.st_mode & /*S_IFMT*/0170000) == /*S_IFCHR*/0020000); |
1649 | | } |
1650 | | #endif |
1651 | 0 | } |
1652 | | |
1653 | | #ifdef PHP_WIN32 |
1654 | | /* {{{ proto bool sapi_windows_vt100_support(resource stream[, bool enable]) |
1655 | | Get or set VT100 support for the specified stream associated to an |
1656 | | output buffer of a Windows console. |
1657 | | */ |
1658 | | PHP_FUNCTION(sapi_windows_vt100_support) |
1659 | | { |
1660 | | zval *zsrc; |
1661 | | php_stream *stream; |
1662 | | zend_bool enable; |
1663 | | zend_long fileno; |
1664 | | |
1665 | | int argc = ZEND_NUM_ARGS(); |
1666 | | |
1667 | | ZEND_PARSE_PARAMETERS_START(1, 2) |
1668 | | Z_PARAM_RESOURCE(zsrc) |
1669 | | Z_PARAM_OPTIONAL |
1670 | | Z_PARAM_BOOL(enable) |
1671 | | ZEND_PARSE_PARAMETERS_END(); |
1672 | | |
1673 | | php_stream_from_zval(stream, zsrc); |
1674 | | |
1675 | | if (php_stream_can_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT) == SUCCESS) { |
1676 | | php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT, (void*)&fileno, 0); |
1677 | | } |
1678 | | else if (php_stream_can_cast(stream, PHP_STREAM_AS_FD) == SUCCESS) { |
1679 | | php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fileno, 0); |
1680 | | } |
1681 | | else { |
1682 | | zend_type_error( |
1683 | | "%s() was not able to analyze the specified stream", |
1684 | | get_active_function_name() |
1685 | | ); |
1686 | | RETURN_THROWS(); |
1687 | | } |
1688 | | |
1689 | | /* Check if the file descriptor is a console */ |
1690 | | if (!php_win32_console_fileno_is_console(fileno)) { |
1691 | | RETURN_FALSE; |
1692 | | } |
1693 | | |
1694 | | if (argc == 1) { |
1695 | | /* Check if the Windows standard handle has VT100 control codes enabled */ |
1696 | | if (php_win32_console_fileno_has_vt100(fileno)) { |
1697 | | RETURN_TRUE; |
1698 | | } |
1699 | | else { |
1700 | | RETURN_FALSE; |
1701 | | } |
1702 | | } |
1703 | | else { |
1704 | | /* Enable/disable VT100 control codes support for the specified Windows standard handle */ |
1705 | | if (php_win32_console_fileno_set_vt100(fileno, enable ? TRUE : FALSE)) { |
1706 | | RETURN_TRUE; |
1707 | | } |
1708 | | else { |
1709 | | RETURN_FALSE; |
1710 | | } |
1711 | | } |
1712 | | } |
1713 | | #endif |
1714 | | |
1715 | | #ifdef HAVE_SHUTDOWN |
1716 | | /* {{{ proto int stream_socket_shutdown(resource stream, int how) |
1717 | | causes all or part of a full-duplex connection on the socket associated |
1718 | | with stream to be shut down. If how is SHUT_RD, further receptions will |
1719 | | be disallowed. If how is SHUT_WR, further transmissions will be disallowed. |
1720 | | If how is SHUT_RDWR, further receptions and transmissions will be |
1721 | | disallowed. */ |
1722 | | PHP_FUNCTION(stream_socket_shutdown) |
1723 | 0 | { |
1724 | 0 | zend_long how; |
1725 | 0 | zval *zstream; |
1726 | 0 | php_stream *stream; |
1727 | |
|
1728 | 0 | ZEND_PARSE_PARAMETERS_START(2, 2) |
1729 | 0 | Z_PARAM_RESOURCE(zstream) |
1730 | 0 | Z_PARAM_LONG(how) |
1731 | 0 | ZEND_PARSE_PARAMETERS_END(); |
1732 | |
|
1733 | 0 | if (how != STREAM_SHUT_RD && |
1734 | 0 | how != STREAM_SHUT_WR && |
1735 | 0 | how != STREAM_SHUT_RDWR) { |
1736 | 0 | php_error_docref(NULL, E_WARNING, "Second parameter $how needs to be one of STREAM_SHUT_RD, STREAM_SHUT_WR or STREAM_SHUT_RDWR"); |
1737 | 0 | RETURN_FALSE; |
1738 | 0 | } |
1739 | |
|
1740 | 0 | php_stream_from_zval(stream, zstream); |
1741 | |
|
1742 | 0 | RETURN_BOOL(php_stream_xport_shutdown(stream, (stream_shutdown_t)how) == 0); |
1743 | 0 | } |
1744 | | /* }}} */ |
1745 | | #endif |