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