PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - streamsfuncs.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 0 693 0.0 %
Date: 2014-04-16 Functions: 0 43 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:57 +0000 (6 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.