Coverage Report

Created: 2025-07-23 06:33

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