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