Coverage Report

Created: 2026-06-02 06:36

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