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