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