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