Coverage Report

Created: 2026-10-28 06:20

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