Back to index

php5  5.3.10
streamsfuncs.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Authors: Wez Furlong <wez@thebrainroom.com>                          |
00016   |          Sara Golemon <pollita@php.net>                              |
00017   +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: streamsfuncs.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #include "php.h"
00023 #include "php_globals.h"
00024 #include "ext/standard/flock_compat.h"
00025 #include "ext/standard/file.h"
00026 #include "ext/standard/php_filestat.h"
00027 #include "php_open_temporary_file.h"
00028 #include "ext/standard/basic_functions.h"
00029 #include "php_ini.h"
00030 #include "streamsfuncs.h"
00031 #include "php_network.h"
00032 #include "php_string.h"
00033 
00034 #ifndef PHP_WIN32
00035 #define php_select(m, r, w, e, t)  select(m, r, w, e, t)
00036 typedef unsigned long long php_timeout_ull;
00037 #else
00038 #include "win32/select.h"
00039 #include "win32/sockets.h"
00040 typedef unsigned __int64 php_timeout_ull;
00041 #endif
00042 
00043 static php_stream_context *decode_context_param(zval *contextresource TSRMLS_DC);
00044 
00045 /* Streams based network functions */
00046 
00047 #if HAVE_SOCKETPAIR
00048 /* {{{ proto array stream_socket_pair(int domain, int type, int protocol)
00049    Creates a pair of connected, indistinguishable socket streams */
00050 PHP_FUNCTION(stream_socket_pair)
00051 {
00052        long domain, type, protocol;
00053        php_stream *s1, *s2;
00054        int pair[2];
00055 
00056        if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll",
00057                      &domain, &type, &protocol)) {
00058               RETURN_FALSE;
00059        }
00060 
00061        if (0 != socketpair(domain, type, protocol, pair)) {
00062               char errbuf[256];
00063               php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to create sockets: [%d]: %s",
00064                      php_socket_errno(), php_socket_strerror(php_socket_errno(), errbuf, sizeof(errbuf)));
00065               RETURN_FALSE;
00066        }
00067 
00068        array_init(return_value);
00069 
00070        s1 = php_stream_sock_open_from_socket(pair[0], 0);
00071        s2 = php_stream_sock_open_from_socket(pair[1], 0);
00072 
00073        /* set the __exposed flag.
00074         * php_stream_to_zval() does, add_next_index_resource() does not */
00075        php_stream_auto_cleanup(s1);
00076        php_stream_auto_cleanup(s2);
00077 
00078        add_next_index_resource(return_value, php_stream_get_resource_id(s1));
00079        add_next_index_resource(return_value, php_stream_get_resource_id(s2));
00080 }
00081 /* }}} */
00082 #endif
00083 
00084 /* {{{ proto resource stream_socket_client(string remoteaddress [, long &errcode [, string &errstring [, double timeout [, long flags [, resource context]]]]])
00085    Open a client connection to a remote address */
00086 PHP_FUNCTION(stream_socket_client)
00087 {
00088        char *host;
00089        int host_len;
00090        zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL;
00091        double timeout = FG(default_socket_timeout);
00092        php_timeout_ull conv;
00093        struct timeval tv;
00094        char *hashkey = NULL;
00095        php_stream *stream = NULL;
00096        int err;
00097        long flags = PHP_STREAM_CLIENT_CONNECT;
00098        char *errstr = NULL;
00099        php_stream_context *context = NULL;
00100 
00101        RETVAL_FALSE;
00102 
00103        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zzdlr", &host, &host_len, &zerrno, &zerrstr, &timeout, &flags, &zcontext) == FAILURE) {
00104               RETURN_FALSE;
00105        }
00106 
00107        context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
00108 
00109        if (context) {
00110               zend_list_addref(context->rsrc_id);
00111        }
00112 
00113        if (flags & PHP_STREAM_CLIENT_PERSISTENT) {
00114               spprintf(&hashkey, 0, "stream_socket_client__%s", host);
00115        }
00116 
00117        /* prepare the timeout value for use */
00118        conv = (php_timeout_ull) (timeout * 1000000.0);
00119 #ifdef PHP_WIN32
00120        tv.tv_sec = (long)(conv / 1000000);
00121        tv.tv_usec =(long)(conv % 1000000);
00122 #else
00123        tv.tv_sec = conv / 1000000;
00124        tv.tv_usec = conv % 1000000;
00125 #endif
00126        if (zerrno)   {
00127               zval_dtor(zerrno);
00128               ZVAL_LONG(zerrno, 0);
00129        }
00130        if (zerrstr) {
00131               zval_dtor(zerrstr);
00132               ZVAL_STRING(zerrstr, "", 1);
00133        }
00134 
00135        stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE | REPORT_ERRORS,
00136                      STREAM_XPORT_CLIENT | (flags & PHP_STREAM_CLIENT_CONNECT ? STREAM_XPORT_CONNECT : 0) |
00137                      (flags & PHP_STREAM_CLIENT_ASYNC_CONNECT ? STREAM_XPORT_CONNECT_ASYNC : 0),
00138                      hashkey, &tv, context, &errstr, &err);
00139 
00140 
00141        if (stream == NULL) {
00142               /* host might contain binary characters */
00143               char *quoted_host = php_addslashes(host, host_len, NULL, 0 TSRMLS_CC);
00144 
00145               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", quoted_host, errstr == NULL ? "Unknown error" : errstr);
00146               efree(quoted_host);
00147        }
00148 
00149        if (hashkey) {
00150               efree(hashkey);
00151        }
00152 
00153        if (stream == NULL)  {
00154               if (zerrno) {
00155                      zval_dtor(zerrno);
00156                      ZVAL_LONG(zerrno, err);
00157               }
00158               if (zerrstr && errstr) {
00159                      /* no need to dup; we need to efree buf anyway */
00160                      zval_dtor(zerrstr);
00161                      ZVAL_STRING(zerrstr, errstr, 0);
00162               } else if (errstr) {
00163                      efree(errstr);
00164               }
00165               RETURN_FALSE;
00166        }
00167 
00168        if (errstr) {
00169               efree(errstr);
00170        }
00171 
00172        php_stream_to_zval(stream, return_value);
00173 
00174 }
00175 /* }}} */
00176 
00177 /* {{{ proto resource stream_socket_server(string localaddress [, long &errcode [, string &errstring [, long flags [, resource context]]]])
00178    Create a server socket bound to localaddress */
00179 PHP_FUNCTION(stream_socket_server)
00180 {
00181        char *host;
00182        int host_len;
00183        zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL;
00184        php_stream *stream = NULL;
00185        int err = 0;
00186        long flags = STREAM_XPORT_BIND | STREAM_XPORT_LISTEN;
00187        char *errstr = NULL;
00188        php_stream_context *context = NULL;
00189 
00190        RETVAL_FALSE;
00191 
00192        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zzlr", &host, &host_len, &zerrno, &zerrstr, &flags, &zcontext) == FAILURE) {
00193               RETURN_FALSE;
00194        }
00195 
00196        context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
00197 
00198        if (context) {
00199               zend_list_addref(context->rsrc_id);
00200        }
00201 
00202        if (zerrno)   {
00203               zval_dtor(zerrno);
00204               ZVAL_LONG(zerrno, 0);
00205        }
00206        if (zerrstr) {
00207               zval_dtor(zerrstr);
00208               ZVAL_STRING(zerrstr, "", 1);
00209        }
00210 
00211        stream = php_stream_xport_create(host, host_len, ENFORCE_SAFE_MODE | REPORT_ERRORS,
00212                      STREAM_XPORT_SERVER | flags,
00213                      NULL, NULL, context, &errstr, &err);
00214 
00215        if (stream == NULL) {
00216               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", host, errstr == NULL ? "Unknown error" : errstr);
00217        }
00218 
00219        if (stream == NULL)  {
00220               if (zerrno) {
00221                      zval_dtor(zerrno);
00222                      ZVAL_LONG(zerrno, err);
00223               }
00224               if (zerrstr && errstr) {
00225                      /* no need to dup; we need to efree buf anyway */
00226                      zval_dtor(zerrstr);
00227                      ZVAL_STRING(zerrstr, errstr, 0);
00228               } else if (errstr) {
00229                      efree(errstr);
00230               }
00231               RETURN_FALSE;
00232        }
00233 
00234        if (errstr) {
00235               efree(errstr);
00236        }
00237 
00238        php_stream_to_zval(stream, return_value);
00239 }
00240 /* }}} */
00241 
00242 /* {{{ proto resource stream_socket_accept(resource serverstream, [ double timeout [, string &peername ]])
00243    Accept a client connection from a server socket */
00244 PHP_FUNCTION(stream_socket_accept)
00245 {
00246        double timeout = FG(default_socket_timeout);
00247        zval *zpeername = NULL;
00248        char *peername = NULL;
00249        int peername_len;
00250        php_timeout_ull conv;
00251        struct timeval tv;
00252        php_stream *stream = NULL, *clistream = NULL;
00253        zval *zstream;
00254 
00255        char *errstr = NULL;
00256 
00257        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|dz", &zstream, &timeout, &zpeername) == FAILURE) {
00258               RETURN_FALSE;
00259        }
00260 
00261        php_stream_from_zval(stream, &zstream);
00262 
00263        /* prepare the timeout value for use */
00264        conv = (php_timeout_ull) (timeout * 1000000.0);
00265 #ifdef PHP_WIN32
00266        tv.tv_sec = (long)(conv / 1000000);
00267        tv.tv_usec = (long)(conv % 1000000);
00268 #else
00269        tv.tv_sec = conv / 1000000;
00270        tv.tv_usec = conv % 1000000;
00271 #endif
00272        if (zpeername) {
00273               zval_dtor(zpeername);
00274               ZVAL_NULL(zpeername);
00275        }
00276 
00277        if (0 == php_stream_xport_accept(stream, &clistream,
00278                             zpeername ? &peername : NULL,
00279                             zpeername ? &peername_len : NULL,
00280                             NULL, NULL,
00281                             &tv, &errstr
00282                             TSRMLS_CC) && clistream) {
00283 
00284               if (peername) {
00285                      ZVAL_STRINGL(zpeername, peername, peername_len, 0);
00286               }
00287               php_stream_to_zval(clistream, return_value);
00288        } else {
00289               php_error_docref(NULL TSRMLS_CC, E_WARNING, "accept failed: %s", errstr ? errstr : "Unknown error");
00290               RETVAL_FALSE;
00291        }
00292 
00293        if (errstr) {
00294               efree(errstr);
00295        }
00296 }
00297 /* }}} */
00298 
00299 /* {{{ proto string stream_socket_get_name(resource stream, bool want_peer)
00300    Returns either the locally bound or remote name for a socket stream */
00301 PHP_FUNCTION(stream_socket_get_name)
00302 {
00303        php_stream *stream;
00304        zval *zstream;
00305        zend_bool want_peer;
00306        char *name = NULL;
00307        int name_len;
00308 
00309        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb", &zstream, &want_peer) == FAILURE) {
00310               RETURN_FALSE;
00311        }
00312 
00313        php_stream_from_zval(stream, &zstream);
00314 
00315        if (0 != php_stream_xport_get_name(stream, want_peer,
00316                             &name,
00317                             &name_len,
00318                             NULL, NULL
00319                             TSRMLS_CC)) {
00320               RETURN_FALSE;
00321        }
00322 
00323        RETURN_STRINGL(name, name_len, 0);
00324 }
00325 /* }}} */
00326 
00327 /* {{{ proto long stream_socket_sendto(resouce stream, string data [, long flags [, string target_addr]])
00328    Send data to a socket stream.  If target_addr is specified it must be in dotted quad (or [ipv6]) format */
00329 PHP_FUNCTION(stream_socket_sendto)
00330 {
00331        php_stream *stream;
00332        zval *zstream;
00333        long flags = 0;
00334        char *data, *target_addr = NULL;
00335        int datalen, target_addr_len = 0;
00336        php_sockaddr_storage sa;
00337        socklen_t sl = 0;
00338 
00339        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|ls", &zstream, &data, &datalen, &flags, &target_addr, &target_addr_len) == FAILURE) {
00340               RETURN_FALSE;
00341        }
00342        php_stream_from_zval(stream, &zstream);
00343 
00344        if (target_addr_len) {
00345               /* parse the address */
00346               if (FAILURE == php_network_parse_network_address_with_port(target_addr, target_addr_len, (struct sockaddr*)&sa, &sl TSRMLS_CC)) {
00347                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse `%s' into a valid network address", target_addr);
00348                      RETURN_FALSE;
00349               }
00350        }
00351 
00352        RETURN_LONG(php_stream_xport_sendto(stream, data, datalen, flags, target_addr ? &sa : NULL, sl TSRMLS_CC));
00353 }
00354 /* }}} */
00355 
00356 /* {{{ proto string stream_socket_recvfrom(resource stream, long amount [, long flags [, string &remote_addr]])
00357    Receives data from a socket stream */
00358 PHP_FUNCTION(stream_socket_recvfrom)
00359 {
00360        php_stream *stream;
00361        zval *zstream, *zremote = NULL;
00362        char *remote_addr = NULL;
00363        int remote_addr_len;
00364        long to_read = 0;
00365        char *read_buf;
00366        long flags = 0;
00367        int recvd;
00368 
00369        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|lz", &zstream, &to_read, &flags, &zremote) == FAILURE) {
00370               RETURN_FALSE;
00371        }
00372 
00373        php_stream_from_zval(stream, &zstream);
00374 
00375        if (zremote) {
00376               zval_dtor(zremote);
00377               ZVAL_NULL(zremote);
00378        }
00379 
00380        if (to_read <= 0) {
00381               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
00382               RETURN_FALSE;
00383        }
00384 
00385        read_buf = safe_emalloc(1, to_read, 1);
00386 
00387        recvd = php_stream_xport_recvfrom(stream, read_buf, to_read, flags, NULL, NULL,
00388                      zremote ? &remote_addr : NULL,
00389                      zremote ? &remote_addr_len : NULL
00390                      TSRMLS_CC);
00391 
00392        if (recvd >= 0) {
00393               if (zremote) {
00394                      ZVAL_STRINGL(zremote, remote_addr, remote_addr_len, 0);
00395               }
00396               read_buf[recvd] = '\0';
00397 
00398               if (PG(magic_quotes_runtime)) {
00399                      Z_TYPE_P(return_value) = IS_STRING;
00400                      Z_STRVAL_P(return_value) = php_addslashes(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), &Z_STRLEN_P(return_value), 1 TSRMLS_CC);
00401                      return;
00402               } else {
00403                      RETURN_STRINGL(read_buf, recvd, 0);
00404               }
00405        }
00406 
00407        efree(read_buf);
00408        RETURN_FALSE;
00409 }
00410 /* }}} */
00411 
00412 /* {{{ proto string stream_get_contents(resource source [, long maxlen [, long offset]])
00413    Reads all remaining bytes (or up to maxlen bytes) from a stream and returns them as a string. */
00414 PHP_FUNCTION(stream_get_contents)
00415 {
00416        php_stream    *stream;
00417        zval          *zsrc;
00418        long          maxlen        = PHP_STREAM_COPY_ALL,
00419                             desiredpos    = -1L;
00420        int                  len,
00421                             newlen;
00422        char          *contents     = NULL;
00423 
00424        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zsrc, &maxlen, &desiredpos) == FAILURE) {
00425               RETURN_FALSE;
00426        }
00427 
00428        php_stream_from_zval(stream, &zsrc);
00429 
00430        if (desiredpos >= 0) {
00431               int           seek_res = 0;
00432               off_t  position;
00433 
00434               position = php_stream_tell(stream);
00435               if (position >= 0 && desiredpos > position) {
00436                      /* use SEEK_CUR to allow emulation in streams that don't support seeking */
00437                      seek_res = php_stream_seek(stream, desiredpos - position, SEEK_CUR);
00438               } else if (desiredpos < position)  {
00439                      /* desired position before position or error on tell */
00440                      seek_res = php_stream_seek(stream, desiredpos, SEEK_SET);
00441               }
00442 
00443               if (seek_res != 0) {
00444                      php_error_docref(NULL TSRMLS_CC, E_WARNING,
00445                             "Failed to seek to position %ld in the stream", desiredpos);
00446                      RETURN_FALSE;
00447               }
00448        }
00449 
00450        len = php_stream_copy_to_mem(stream, &contents, maxlen, 0);
00451 
00452        if (contents) {
00453               if (len && PG(magic_quotes_runtime)) {
00454                      contents = php_addslashes(contents, len, &newlen, 1 TSRMLS_CC); /* 1 = free source string */
00455                      len = newlen;
00456               }
00457               RETVAL_STRINGL(contents, len, 0);
00458        } else {
00459               RETVAL_EMPTY_STRING();
00460        }
00461 }
00462 /* }}} */
00463 
00464 /* {{{ proto long stream_copy_to_stream(resource source, resource dest [, long maxlen [, long pos]])
00465    Reads up to maxlen bytes from source stream and writes them to dest stream. */
00466 PHP_FUNCTION(stream_copy_to_stream)
00467 {
00468        php_stream *src, *dest;
00469        zval *zsrc, *zdest;
00470        long maxlen = PHP_STREAM_COPY_ALL, pos = 0;
00471        size_t len;
00472        int ret;
00473 
00474        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|ll", &zsrc, &zdest, &maxlen, &pos) == FAILURE) {
00475               RETURN_FALSE;
00476        }
00477 
00478        php_stream_from_zval(src, &zsrc);
00479        php_stream_from_zval(dest, &zdest);
00480 
00481        if (pos > 0 && php_stream_seek(src, pos, SEEK_SET) < 0) {
00482               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek to position %ld in the stream", pos);
00483               RETURN_FALSE;
00484        }
00485 
00486        ret = php_stream_copy_to_stream_ex(src, dest, maxlen, &len);
00487 
00488        if (ret != SUCCESS) {
00489               RETURN_FALSE;
00490        }
00491        RETURN_LONG(len);
00492 }
00493 /* }}} */
00494 
00495 /* {{{ proto array stream_get_meta_data(resource fp)
00496     Retrieves header/meta data from streams/file pointers */
00497 PHP_FUNCTION(stream_get_meta_data)
00498 {
00499        zval *arg1;
00500        php_stream *stream;
00501        zval *newval;
00502 
00503        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
00504               return;
00505        }
00506        php_stream_from_zval(stream, &arg1);
00507 
00508        array_init(return_value);
00509 
00510        if (stream->wrapperdata) {
00511               MAKE_STD_ZVAL(newval);
00512               MAKE_COPY_ZVAL(&stream->wrapperdata, newval);
00513 
00514               add_assoc_zval(return_value, "wrapper_data", newval);
00515        }
00516        if (stream->wrapper) {
00517               add_assoc_string(return_value, "wrapper_type", (char *)stream->wrapper->wops->label, 1);
00518        }
00519        add_assoc_string(return_value, "stream_type", (char *)stream->ops->label, 1);
00520 
00521        add_assoc_string(return_value, "mode", stream->mode, 1);
00522 
00523 #if 0  /* TODO: needs updating for new filter API */
00524        if (stream->filterhead) {
00525               php_stream_filter *filter;
00526 
00527               MAKE_STD_ZVAL(newval);
00528               array_init(newval);
00529 
00530               for (filter = stream->filterhead; filter != NULL; filter = filter->next) {
00531                      add_next_index_string(newval, (char *)filter->fops->label, 1);
00532               }
00533 
00534               add_assoc_zval(return_value, "filters", newval);
00535        }
00536 #endif
00537 
00538        add_assoc_long(return_value, "unread_bytes", stream->writepos - stream->readpos);
00539 
00540        add_assoc_bool(return_value, "seekable", (stream->ops->seek) && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0);
00541        if (stream->orig_path) {
00542               add_assoc_string(return_value, "uri", stream->orig_path, 1);
00543        }
00544 
00545        if (!php_stream_populate_meta_data(stream, return_value)) {
00546               add_assoc_bool(return_value, "timed_out", 0);
00547               add_assoc_bool(return_value, "blocked", 1);
00548               add_assoc_bool(return_value, "eof", php_stream_eof(stream));
00549        }
00550 
00551 }
00552 /* }}} */
00553 
00554 /* {{{ proto array stream_get_transports()
00555    Retrieves list of registered socket transports */
00556 PHP_FUNCTION(stream_get_transports)
00557 {
00558        HashTable *stream_xport_hash;
00559        char *stream_xport;
00560        int stream_xport_len;
00561        ulong num_key;
00562 
00563        if (zend_parse_parameters_none() == FAILURE) {
00564               return;
00565        }
00566 
00567        if ((stream_xport_hash = php_stream_xport_get_hash())) {
00568               HashPosition pos;
00569               array_init(return_value);
00570               zend_hash_internal_pointer_reset_ex(stream_xport_hash, &pos);
00571               while (zend_hash_get_current_key_ex(stream_xport_hash,
00572                                    &stream_xport, &stream_xport_len,
00573                                    &num_key, 0, &pos) == HASH_KEY_IS_STRING) {
00574                      add_next_index_stringl(return_value, stream_xport, stream_xport_len - 1, 1);
00575                      zend_hash_move_forward_ex(stream_xport_hash, &pos);
00576               }
00577        } else {
00578               RETURN_FALSE;
00579        }
00580 }
00581 /* }}} */
00582 
00583 /* {{{ proto array stream_get_wrappers()
00584     Retrieves list of registered stream wrappers */
00585 PHP_FUNCTION(stream_get_wrappers)
00586 {
00587        HashTable *url_stream_wrappers_hash;
00588        char *stream_protocol;
00589        int key_flags, stream_protocol_len = 0;
00590        ulong num_key;
00591 
00592        if (zend_parse_parameters_none() == FAILURE) {
00593               return;
00594        }
00595 
00596        if ((url_stream_wrappers_hash = php_stream_get_url_stream_wrappers_hash())) {
00597               HashPosition pos;
00598               array_init(return_value);
00599               for (zend_hash_internal_pointer_reset_ex(url_stream_wrappers_hash, &pos);
00600                      (key_flags = zend_hash_get_current_key_ex(url_stream_wrappers_hash, &stream_protocol, &stream_protocol_len, &num_key, 0, &pos)) != HASH_KEY_NON_EXISTANT;
00601                      zend_hash_move_forward_ex(url_stream_wrappers_hash, &pos)) {
00602                             if (key_flags == HASH_KEY_IS_STRING) {
00603                                    add_next_index_stringl(return_value, stream_protocol, stream_protocol_len - 1, 1);
00604                             }
00605               }
00606        } else {
00607               RETURN_FALSE;
00608        }
00609 
00610 }
00611 /* }}} */
00612 
00613 /* {{{ stream_select related functions */
00614 static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t *max_fd TSRMLS_DC)
00615 {
00616        zval **elem;
00617        php_stream *stream;
00618        php_socket_t this_fd;
00619        int cnt = 0;
00620 
00621        if (Z_TYPE_P(stream_array) != IS_ARRAY) {
00622               return 0;
00623        }
00624        for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream_array));
00625                zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == SUCCESS;
00626                zend_hash_move_forward(Z_ARRVAL_P(stream_array))) {
00627 
00628               php_stream_from_zval_no_verify(stream, elem);
00629               if (stream == NULL) {
00630                      continue;
00631               }
00632               /* get the fd.
00633                * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
00634                * when casting.  It is only used here so that the buffered data warning
00635                * is not displayed.
00636                * */
00637               if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) {
00638 
00639                      PHP_SAFE_FD_SET(this_fd, fds);
00640 
00641                      if (this_fd > *max_fd) {
00642                             *max_fd = this_fd;
00643                      }
00644                      cnt++;
00645               }
00646        }
00647        return cnt ? 1 : 0;
00648 }
00649 
00650 static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
00651 {
00652        zval **elem, **dest_elem;
00653        php_stream *stream;
00654        HashTable *new_hash;
00655        php_socket_t this_fd;
00656        int ret = 0;
00657 
00658        if (Z_TYPE_P(stream_array) != IS_ARRAY) {
00659               return 0;
00660        }
00661        ALLOC_HASHTABLE(new_hash);
00662        zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
00663 
00664        for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream_array));
00665                zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == SUCCESS;
00666                zend_hash_move_forward(Z_ARRVAL_P(stream_array))) {
00667 
00668               php_stream_from_zval_no_verify(stream, elem);
00669               if (stream == NULL) {
00670                      continue;
00671               }
00672               /* get the fd
00673                * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
00674                * when casting.  It is only used here so that the buffered data warning
00675                * is not displayed.
00676                */
00677               if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) {
00678                      if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
00679                             zend_hash_next_index_insert(new_hash, (void *)elem, sizeof(zval *), (void **)&dest_elem);
00680                             if (dest_elem) {
00681                                    zval_add_ref(dest_elem);
00682                             }
00683                             ret++;
00684                             continue;
00685                      }
00686               }
00687        }
00688 
00689        /* destroy old array and add new one */
00690        zend_hash_destroy(Z_ARRVAL_P(stream_array));
00691        efree(Z_ARRVAL_P(stream_array));
00692 
00693        zend_hash_internal_pointer_reset(new_hash);
00694        Z_ARRVAL_P(stream_array) = new_hash;
00695 
00696        return ret;
00697 }
00698 
00699 static int stream_array_emulate_read_fd_set(zval *stream_array TSRMLS_DC)
00700 {
00701        zval **elem, **dest_elem;
00702        php_stream *stream;
00703        HashTable *new_hash;
00704        int ret = 0;
00705 
00706        if (Z_TYPE_P(stream_array) != IS_ARRAY) {
00707               return 0;
00708        }
00709        ALLOC_HASHTABLE(new_hash);
00710        zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
00711 
00712        for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream_array));
00713                zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == SUCCESS;
00714                zend_hash_move_forward(Z_ARRVAL_P(stream_array))) {
00715 
00716               php_stream_from_zval_no_verify(stream, elem);
00717               if (stream == NULL) {
00718                      continue;
00719               }
00720               if ((stream->writepos - stream->readpos) > 0) {
00721                      /* allow readable non-descriptor based streams to participate in stream_select.
00722                       * Non-descriptor streams will only "work" if they have previously buffered the
00723                       * data.  Not ideal, but better than nothing.
00724                       * This branch of code also allows blocking streams with buffered data to
00725                       * operate correctly in stream_select.
00726                       * */
00727                      zend_hash_next_index_insert(new_hash, (void *)elem, sizeof(zval *), (void **)&dest_elem);
00728                      if (dest_elem) {
00729                             zval_add_ref(dest_elem);
00730                      }
00731                      ret++;
00732                      continue;
00733               }
00734        }
00735 
00736        if (ret > 0) {
00737               /* destroy old array and add new one */
00738               zend_hash_destroy(Z_ARRVAL_P(stream_array));
00739               efree(Z_ARRVAL_P(stream_array));
00740 
00741               zend_hash_internal_pointer_reset(new_hash);
00742               Z_ARRVAL_P(stream_array) = new_hash;
00743        } else {
00744               zend_hash_destroy(new_hash);
00745               FREE_HASHTABLE(new_hash);
00746        }
00747 
00748        return ret;
00749 }
00750 /* }}} */
00751 
00752 /* {{{ proto int stream_select(array &read_streams, array &write_streams, array &except_streams, int tv_sec[, int tv_usec])
00753    Runs the select() system call on the sets of streams with a timeout specified by tv_sec and tv_usec */
00754 PHP_FUNCTION(stream_select)
00755 {
00756        zval                 *r_array, *w_array, *e_array, **sec = NULL;
00757        struct timeval       tv;
00758        struct timeval *tv_p = NULL;
00759        fd_set               rfds, wfds, efds;
00760        php_socket_t  max_fd = 0;
00761        int                         retval, sets = 0;
00762        long                 usec = 0;
00763        int                         set_count, max_set_count = 0;
00764 
00765        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!a!Z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE)
00766               return;
00767 
00768        FD_ZERO(&rfds);
00769        FD_ZERO(&wfds);
00770        FD_ZERO(&efds);
00771 
00772        if (r_array != NULL) {
00773               set_count = stream_array_to_fd_set(r_array, &rfds, &max_fd TSRMLS_CC);
00774               if (set_count > max_set_count)
00775                      max_set_count = set_count;
00776               sets += set_count;
00777        }
00778 
00779        if (w_array != NULL) {
00780               set_count = stream_array_to_fd_set(w_array, &wfds, &max_fd TSRMLS_CC);
00781               if (set_count > max_set_count)
00782                      max_set_count = set_count;
00783               sets += set_count;
00784        }
00785 
00786        if (e_array != NULL) {
00787               set_count = stream_array_to_fd_set(e_array, &efds, &max_fd TSRMLS_CC);
00788               if (set_count > max_set_count)
00789                      max_set_count = set_count;
00790               sets += set_count;
00791        }
00792 
00793        if (!sets) {
00794               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No stream arrays were passed");
00795               RETURN_FALSE;
00796        }
00797 
00798        PHP_SAFE_MAX_FD(max_fd, max_set_count);
00799 
00800        /* If seconds is not set to null, build the timeval, else we wait indefinitely */
00801        if (sec != NULL) {
00802               convert_to_long_ex(sec);
00803 
00804               if (Z_LVAL_PP(sec) < 0) {
00805                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "The seconds parameter must be greater than 0");
00806                      RETURN_FALSE;
00807               } else if (usec < 0) {
00808                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "The microseconds parameter must be greater than 0");
00809                      RETURN_FALSE;
00810               }
00811 
00812               /* Solaris + BSD do not like microsecond values which are >= 1 sec */
00813               if (usec > 999999) {
00814                      tv.tv_sec = Z_LVAL_PP(sec) + (usec / 1000000);
00815                      tv.tv_usec = usec % 1000000;
00816               } else {
00817                      tv.tv_sec = Z_LVAL_PP(sec);
00818                      tv.tv_usec = usec;
00819               }
00820 
00821               tv_p = &tv;
00822        }
00823 
00824        /* slight hack to support buffered data; if there is data sitting in the
00825         * read buffer of any of the streams in the read array, let's pretend
00826         * that we selected, but return only the readable sockets */
00827        if (r_array != NULL) {
00828 
00829               retval = stream_array_emulate_read_fd_set(r_array TSRMLS_CC);
00830               if (retval > 0) {
00831                      if (w_array != NULL) {
00832                             zend_hash_clean(Z_ARRVAL_P(w_array));
00833                      }
00834                      if (e_array != NULL) {
00835                             zend_hash_clean(Z_ARRVAL_P(e_array));
00836                      }
00837                      RETURN_LONG(retval);
00838               }
00839        }
00840 
00841        retval = php_select(max_fd+1, &rfds, &wfds, &efds, tv_p);
00842 
00843        if (retval == -1) {
00844               php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to select [%d]: %s (max_fd=%d)",
00845                             errno, strerror(errno), max_fd);
00846               RETURN_FALSE;
00847        }
00848 
00849        if (r_array != NULL) stream_array_from_fd_set(r_array, &rfds TSRMLS_CC);
00850        if (w_array != NULL) stream_array_from_fd_set(w_array, &wfds TSRMLS_CC);
00851        if (e_array != NULL) stream_array_from_fd_set(e_array, &efds TSRMLS_CC);
00852 
00853        RETURN_LONG(retval);
00854 }
00855 /* }}} */
00856 
00857 /* {{{ stream_context related functions */
00858 static void user_space_stream_notifier(php_stream_context *context, int notifycode, int severity,
00859               char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr TSRMLS_DC)
00860 {
00861        zval *callback = (zval*)context->notifier->ptr;
00862        zval *retval = NULL;
00863        zval zvs[6];
00864        zval *ps[6];
00865        zval **ptps[6];
00866        int i;
00867 
00868        for (i = 0; i < 6; i++) {
00869               INIT_ZVAL(zvs[i]);
00870               ps[i] = &zvs[i];
00871               ptps[i] = &ps[i];
00872               MAKE_STD_ZVAL(ps[i]);
00873        }
00874 
00875        ZVAL_LONG(ps[0], notifycode);
00876        ZVAL_LONG(ps[1], severity);
00877        if (xmsg) {
00878               ZVAL_STRING(ps[2], xmsg, 1);
00879        } else {
00880               ZVAL_NULL(ps[2]);
00881        }
00882        ZVAL_LONG(ps[3], xcode);
00883        ZVAL_LONG(ps[4], bytes_sofar);
00884        ZVAL_LONG(ps[5], bytes_max);
00885 
00886        if (FAILURE == call_user_function_ex(EG(function_table), NULL, callback, &retval, 6, ptps, 0, NULL TSRMLS_CC)) {
00887               php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to call user notifier");
00888        }
00889        for (i = 0; i < 6; i++) {
00890               zval_ptr_dtor(&ps[i]);
00891        }
00892        if (retval) {
00893               zval_ptr_dtor(&retval);
00894        }
00895 }
00896 
00897 static void user_space_stream_notifier_dtor(php_stream_notifier *notifier)
00898 {
00899        if (notifier && notifier->ptr) {
00900               zval_ptr_dtor((zval **)&(notifier->ptr));
00901               notifier->ptr = NULL;
00902        }
00903 }
00904 
00905 static int parse_context_options(php_stream_context *context, zval *options TSRMLS_DC)
00906 {
00907        HashPosition pos, opos;
00908        zval **wval, **oval;
00909        char *wkey, *okey;
00910        int wkey_len, okey_len;
00911        int ret = SUCCESS;
00912        ulong num_key;
00913 
00914        zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(options), &pos);
00915        while (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(options), (void**)&wval, &pos)) {
00916               if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(options), &wkey, &wkey_len, &num_key, 0, &pos)
00917                             && Z_TYPE_PP(wval) == IS_ARRAY) {
00918 
00919                      zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(wval), &opos);
00920                      while (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(wval), (void**)&oval, &opos)) {
00921 
00922                             if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_PP(wval), &okey, &okey_len, &num_key, 0, &opos)) {
00923                                    php_stream_context_set_option(context, wkey, okey, *oval);
00924                             }
00925                             zend_hash_move_forward_ex(Z_ARRVAL_PP(wval), &opos);
00926                      }
00927 
00928               } else {
00929                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "options should have the form [\"wrappername\"][\"optionname\"] = $value");
00930               }
00931               zend_hash_move_forward_ex(Z_ARRVAL_P(options), &pos);
00932        }
00933 
00934        return ret;
00935 }
00936 
00937 static int parse_context_params(php_stream_context *context, zval *params TSRMLS_DC)
00938 {
00939        int ret = SUCCESS;
00940        zval **tmp;
00941 
00942        if (SUCCESS == zend_hash_find(Z_ARRVAL_P(params), "notification", sizeof("notification"), (void**)&tmp)) {
00943 
00944               if (context->notifier) {
00945                      php_stream_notification_free(context->notifier);
00946                      context->notifier = NULL;
00947               }
00948 
00949               context->notifier = php_stream_notification_alloc();
00950               context->notifier->func = user_space_stream_notifier;
00951               context->notifier->ptr = *tmp;
00952               Z_ADDREF_P(*tmp);
00953               context->notifier->dtor = user_space_stream_notifier_dtor;
00954        }
00955        if (SUCCESS == zend_hash_find(Z_ARRVAL_P(params), "options", sizeof("options"), (void**)&tmp)) {
00956               if (Z_TYPE_PP(tmp) == IS_ARRAY) {
00957                      parse_context_options(context, *tmp TSRMLS_CC);
00958               } else {
00959                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid stream/context parameter");
00960               }
00961        }
00962 
00963        return ret;
00964 }
00965 
00966 /* given a zval which is either a stream or a context, return the underlying
00967  * stream_context.  If it is a stream that does not have a context assigned, it
00968  * will create and assign a context and return that.  */
00969 static php_stream_context *decode_context_param(zval *contextresource TSRMLS_DC)
00970 {
00971        php_stream_context *context = NULL;
00972 
00973        context = zend_fetch_resource(&contextresource TSRMLS_CC, -1, NULL, NULL, 1, php_le_stream_context());
00974        if (context == NULL) {
00975               php_stream *stream;
00976 
00977               stream = zend_fetch_resource(&contextresource TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream);
00978 
00979               if (stream) {
00980                      context = stream->context;
00981                      if (context == NULL) {
00982                             /* Only way this happens is if file is opened with NO_DEFAULT_CONTEXT
00983                                param, but then something is called which requires a context.
00984                                Don't give them the default one though since they already said they
00985                                didn't want it. */
00986                             context = stream->context = php_stream_context_alloc();
00987                      }
00988               }
00989        }
00990 
00991        return context;
00992 }
00993 /* }}} */
00994 
00995 /* {{{ proto array stream_context_get_options(resource context|resource stream)
00996    Retrieve options for a stream/wrapper/context */
00997 PHP_FUNCTION(stream_context_get_options)
00998 {
00999        zval *zcontext;
01000        php_stream_context *context;
01001 
01002        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zcontext) == FAILURE) {
01003               RETURN_FALSE;
01004        }
01005        context = decode_context_param(zcontext TSRMLS_CC);
01006        if (!context) {
01007               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid stream/context parameter");
01008               RETURN_FALSE;
01009        }
01010 
01011        RETURN_ZVAL(context->options, 1, 0);
01012 }
01013 /* }}} */
01014 
01015 /* {{{ proto bool stream_context_set_option(resource context|resource stream, string wrappername, string optionname, mixed value)
01016    Set an option for a wrapper */
01017 PHP_FUNCTION(stream_context_set_option)
01018 {
01019        zval *options = NULL, *zcontext = NULL, *zvalue = NULL;
01020        php_stream_context *context;
01021        char *wrappername, *optionname;
01022        int wrapperlen, optionlen;
01023 
01024        if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
01025                             "rssz", &zcontext, &wrappername, &wrapperlen,
01026                             &optionname, &optionlen, &zvalue) == FAILURE) {
01027               if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC,
01028                                    "ra", &zcontext, &options) == FAILURE) {
01029                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "called with wrong number or type of parameters; please RTM");
01030                      RETURN_FALSE;
01031               }
01032        }
01033 
01034        /* figure out where the context is coming from exactly */
01035        context = decode_context_param(zcontext TSRMLS_CC);
01036        if (!context) {
01037               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid stream/context parameter");
01038               RETURN_FALSE;
01039        }
01040 
01041        if (options) {
01042               /* handle the array syntax */
01043               RETVAL_BOOL(parse_context_options(context, options TSRMLS_CC) == SUCCESS);
01044        } else {
01045               php_stream_context_set_option(context, wrappername, optionname, zvalue);
01046               RETVAL_TRUE;
01047        }
01048 }
01049 /* }}} */
01050 
01051 /* {{{ proto bool stream_context_set_params(resource context|resource stream, array options)
01052    Set parameters for a file context */
01053 PHP_FUNCTION(stream_context_set_params)
01054 {
01055        zval *params, *zcontext;
01056        php_stream_context *context;
01057 
01058        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ra", &zcontext, &params) == FAILURE) {
01059               RETURN_FALSE;
01060        }
01061 
01062        context = decode_context_param(zcontext TSRMLS_CC);
01063        if (!context) {
01064               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid stream/context parameter");
01065               RETURN_FALSE;
01066        }
01067 
01068        RETVAL_BOOL(parse_context_params(context, params TSRMLS_CC) == SUCCESS);
01069 }
01070 /* }}} */
01071 
01072 /* {{{ proto array stream_context_get_params(resource context|resource stream)
01073    Get parameters of a file context */
01074 PHP_FUNCTION(stream_context_get_params)
01075 {
01076        zval *zcontext, *options;
01077        php_stream_context *context;
01078 
01079        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zcontext) == FAILURE) {
01080               RETURN_FALSE;
01081        }
01082 
01083        context = decode_context_param(zcontext TSRMLS_CC);
01084        if (!context) {
01085               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid stream/context parameter");
01086               RETURN_FALSE;
01087        }
01088 
01089        array_init(return_value);
01090        if (context->notifier && context->notifier->ptr && context->notifier->func == user_space_stream_notifier) {
01091               add_assoc_zval_ex(return_value, ZEND_STRS("notification"), context->notifier->ptr);
01092               Z_ADDREF_P(context->notifier->ptr);
01093        }
01094        ALLOC_INIT_ZVAL(options);
01095        ZVAL_ZVAL(options, context->options, 1, 0);
01096        add_assoc_zval_ex(return_value, ZEND_STRS("options"), options);
01097 }
01098 /* }}} */
01099 
01100 /* {{{ proto resource stream_context_get_default([array options])
01101    Get a handle on the default file/stream context and optionally set parameters */
01102 PHP_FUNCTION(stream_context_get_default)
01103 {
01104        zval *params = NULL;
01105        php_stream_context *context;
01106 
01107        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a", &params) == FAILURE) {
01108               RETURN_FALSE;
01109        }
01110 
01111        if (FG(default_context) == NULL) {
01112               FG(default_context) = php_stream_context_alloc();
01113        }
01114        context = FG(default_context);
01115 
01116        if (params) {
01117               parse_context_options(context, params TSRMLS_CC);
01118        }
01119 
01120        php_stream_context_to_zval(context, return_value);
01121 }
01122 /* }}} */
01123 
01124 /* {{{ proto resource stream_context_set_default(array options)
01125    Set default file/stream context, returns the context as a resource */
01126 PHP_FUNCTION(stream_context_set_default)
01127 {
01128        zval *options = NULL;
01129        php_stream_context *context;
01130 
01131        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &options) == FAILURE) {
01132               return;
01133        }
01134 
01135        if (FG(default_context) == NULL) {
01136               FG(default_context) = php_stream_context_alloc();
01137        }
01138        context = FG(default_context);
01139 
01140        parse_context_options(context, options TSRMLS_CC);
01141 
01142        php_stream_context_to_zval(context, return_value);
01143 }
01144 /* }}} */
01145 
01146 /* {{{ proto resource stream_context_create([array options[, array params]])
01147    Create a file context and optionally set parameters */
01148 PHP_FUNCTION(stream_context_create)
01149 {
01150        zval *options = NULL, *params = NULL;
01151        php_stream_context *context;
01152 
01153        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!a!", &options, &params) == FAILURE) {
01154               RETURN_FALSE;
01155        }
01156 
01157        context = php_stream_context_alloc();
01158 
01159        if (options) {
01160               parse_context_options(context, options TSRMLS_CC);
01161        }
01162 
01163        if (params) {
01164               parse_context_params(context, params TSRMLS_CC);
01165        }
01166 
01167        RETURN_RESOURCE(context->rsrc_id);
01168 }
01169 /* }}} */
01170 
01171 /* {{{ streams filter functions */
01172 static void apply_filter_to_stream(int append, INTERNAL_FUNCTION_PARAMETERS)
01173 {
01174        zval *zstream;
01175        php_stream *stream;
01176        char *filtername;
01177        int filternamelen;
01178        long read_write = 0;
01179        zval *filterparams = NULL;
01180        php_stream_filter *filter = NULL;
01181        int ret;
01182 
01183        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|lz", &zstream,
01184                             &filtername, &filternamelen, &read_write, &filterparams) == FAILURE) {
01185               RETURN_FALSE;
01186        }
01187 
01188        php_stream_from_zval(stream, &zstream);
01189 
01190        if ((read_write & PHP_STREAM_FILTER_ALL) == 0) {
01191               /* Chain not specified.
01192                * Examine stream->mode to determine which filters are needed
01193                * There's no harm in attaching a filter to an unused chain,
01194                * but why waste the memory and clock cycles?
01195                */
01196               if (strchr(stream->mode, 'r') || strchr(stream->mode, '+')) {
01197                      read_write |= PHP_STREAM_FILTER_READ;
01198               }
01199               if (strchr(stream->mode, 'w') || strchr(stream->mode, '+') || strchr(stream->mode, 'a')) {
01200                      read_write |= PHP_STREAM_FILTER_WRITE;
01201               }
01202        }
01203 
01204        if (read_write & PHP_STREAM_FILTER_READ) {
01205               filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream) TSRMLS_CC);
01206               if (filter == NULL) {
01207                      RETURN_FALSE;
01208               }
01209 
01210               if (append) {
01211                      ret = php_stream_filter_append_ex(&stream->readfilters, filter TSRMLS_CC);
01212               } else {
01213                      ret = php_stream_filter_prepend_ex(&stream->readfilters, filter TSRMLS_CC);
01214               }
01215               if (ret != SUCCESS) {
01216                      php_stream_filter_remove(filter, 1 TSRMLS_CC);
01217                      RETURN_FALSE;
01218               }
01219        }
01220 
01221        if (read_write & PHP_STREAM_FILTER_WRITE) {
01222               filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream) TSRMLS_CC);
01223               if (filter == NULL) {
01224                      RETURN_FALSE;
01225               }
01226 
01227               if (append) {
01228                      ret = php_stream_filter_append_ex(&stream->writefilters, filter TSRMLS_CC);
01229               } else {
01230                      ret = php_stream_filter_prepend_ex(&stream->writefilters, filter TSRMLS_CC);
01231               }
01232               if (ret != SUCCESS) {
01233                      php_stream_filter_remove(filter, 1 TSRMLS_CC);
01234                      RETURN_FALSE;
01235               }
01236        }
01237 
01238        if (filter) {
01239               RETURN_RESOURCE(filter->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, filter, php_file_le_stream_filter()));
01240        } else {
01241               RETURN_FALSE;
01242        }
01243 }
01244 /* }}} */
01245 
01246 /* {{{ proto resource stream_filter_prepend(resource stream, string filtername[, int read_write[, string filterparams]])
01247    Prepend a filter to a stream */
01248 PHP_FUNCTION(stream_filter_prepend)
01249 {
01250        apply_filter_to_stream(0, INTERNAL_FUNCTION_PARAM_PASSTHRU);
01251 }
01252 /* }}} */
01253 
01254 /* {{{ proto resource stream_filter_append(resource stream, string filtername[, int read_write[, string filterparams]])
01255    Append a filter to a stream */
01256 PHP_FUNCTION(stream_filter_append)
01257 {
01258        apply_filter_to_stream(1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
01259 }
01260 /* }}} */
01261 
01262 /* {{{ proto bool stream_filter_remove(resource stream_filter)
01263        Flushes any data in the filter's internal buffer, removes it from the chain, and frees the resource */
01264 PHP_FUNCTION(stream_filter_remove)
01265 {
01266        zval *zfilter;
01267        php_stream_filter *filter;
01268 
01269        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zfilter) == FAILURE) {
01270               RETURN_FALSE;
01271        }
01272 
01273        filter = zend_fetch_resource(&zfilter TSRMLS_CC, -1, NULL, NULL, 1, php_file_le_stream_filter());
01274        if (!filter) {
01275               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid resource given, not a stream filter");
01276               RETURN_FALSE;
01277        }
01278 
01279        if (php_stream_filter_flush(filter, 1) == FAILURE) {
01280               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to flush filter, not removing");
01281               RETURN_FALSE;
01282        }
01283 
01284        if (zend_list_delete(Z_LVAL_P(zfilter)) == FAILURE) {
01285               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not invalidate filter, not removing");
01286               RETURN_FALSE;
01287        } else {
01288               php_stream_filter_remove(filter, 1 TSRMLS_CC);
01289               RETURN_TRUE;
01290        }
01291 }
01292 /* }}} */
01293 
01294 /* {{{ proto string stream_get_line(resource stream, int maxlen [, string ending])
01295    Read up to maxlen bytes from a stream or until the ending string is found */
01296 PHP_FUNCTION(stream_get_line)
01297 {
01298        char *str = NULL;
01299        int str_len = 0;
01300        long max_length;
01301        zval *zstream;
01302        char *buf;
01303        size_t buf_size;
01304        php_stream *stream;
01305 
01306        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|s", &zstream, &max_length, &str, &str_len) == FAILURE) {
01307               RETURN_FALSE;
01308        }
01309 
01310        if (max_length < 0) {
01311               php_error_docref(NULL TSRMLS_CC, E_WARNING, "The maximum allowed length must be greater than or equal to zero");
01312               RETURN_FALSE;
01313        }
01314        if (!max_length) {
01315               max_length = PHP_SOCK_CHUNK_SIZE;
01316        }
01317 
01318        php_stream_from_zval(stream, &zstream);
01319 
01320        if ((buf = php_stream_get_record(stream, max_length, &buf_size, str, str_len TSRMLS_CC))) {
01321               RETURN_STRINGL(buf, buf_size, 0);
01322        } else {
01323               RETURN_FALSE;
01324        }
01325 }
01326 
01327 /* }}} */
01328 
01329 /* {{{ proto bool stream_set_blocking(resource socket, int mode)
01330    Set blocking/non-blocking mode on a socket or stream */
01331 PHP_FUNCTION(stream_set_blocking)
01332 {
01333        zval *arg1;
01334        int block;
01335        long arg2;
01336        php_stream *stream;
01337 
01338        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &arg1, &arg2) == FAILURE) {
01339               return;
01340        }
01341 
01342        php_stream_from_zval(stream, &arg1);
01343 
01344        block = arg2;
01345 
01346        if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, block == 0 ? 0 : 1, NULL) == -1) {
01347               RETURN_FALSE;
01348        }
01349 
01350        RETURN_TRUE;
01351 }
01352 
01353 /* }}} */
01354 
01355 /* {{{ proto bool stream_set_timeout(resource stream, int seconds [, int microseconds])
01356    Set timeout on stream read to seconds + microseonds */
01357 #if HAVE_SYS_TIME_H || defined(PHP_WIN32)
01358 PHP_FUNCTION(stream_set_timeout)
01359 {
01360        zval *socket;
01361        long seconds, microseconds = 0;
01362        struct timeval t;
01363        php_stream *stream;
01364        int argc = ZEND_NUM_ARGS();
01365 
01366        if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &socket, &seconds, &microseconds) == FAILURE) {
01367               return;
01368        }
01369 
01370        php_stream_from_zval(stream, &socket);
01371 
01372        t.tv_sec = seconds;
01373 
01374        if (argc == 3) {
01375               t.tv_usec = microseconds % 1000000;
01376               t.tv_sec += microseconds / 1000000;
01377        } else {
01378               t.tv_usec = 0;
01379        }
01380 
01381        if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &t)) {
01382               RETURN_TRUE;
01383        }
01384 
01385        RETURN_FALSE;
01386 }
01387 #endif /* HAVE_SYS_TIME_H || defined(PHP_WIN32) */
01388 /* }}} */
01389 
01390 /* {{{ proto int stream_set_write_buffer(resource fp, int buffer)
01391    Set file write buffer */
01392 PHP_FUNCTION(stream_set_write_buffer)
01393 {
01394        zval *arg1;
01395        int ret;
01396        long arg2;
01397        size_t buff;
01398        php_stream *stream;
01399 
01400        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &arg1, &arg2) == FAILURE) {
01401               RETURN_FALSE;
01402        }
01403 
01404        php_stream_from_zval(stream, &arg1);
01405 
01406        buff = arg2;
01407 
01408        /* if buff is 0 then set to non-buffered */
01409        if (buff == 0) {
01410               ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
01411        } else {
01412               ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
01413        }
01414 
01415        RETURN_LONG(ret == 0 ? 0 : EOF);
01416 }
01417 /* }}} */
01418 
01419 /* {{{ proto int stream_set_read_buffer(resource fp, int buffer)
01420    Set file read buffer */
01421 PHP_FUNCTION(stream_set_read_buffer)
01422 {
01423        zval *arg1;
01424        int ret;
01425        long arg2;
01426        size_t buff;
01427        php_stream *stream;
01428 
01429        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &arg1, &arg2) == FAILURE) {
01430               RETURN_FALSE;
01431        }
01432 
01433        php_stream_from_zval(stream, &arg1);
01434 
01435        buff = arg2;
01436 
01437        /* if buff is 0 then set to non-buffered */
01438        if (buff == 0) {
01439               ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
01440        } else {
01441               ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
01442        }
01443 
01444        RETURN_LONG(ret == 0 ? 0 : EOF);
01445 }
01446 /* }}} */
01447 
01448 /* {{{ proto int stream_socket_enable_crypto(resource stream, bool enable [, int cryptokind [, resource sessionstream]])
01449    Enable or disable a specific kind of crypto on the stream */
01450 PHP_FUNCTION(stream_socket_enable_crypto)
01451 {
01452        long cryptokind = 0;
01453        zval *zstream, *zsessstream = NULL;
01454        php_stream *stream, *sessstream = NULL;
01455        zend_bool enable;
01456        int ret;
01457 
01458        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rb|lr", &zstream, &enable, &cryptokind, &zsessstream) == FAILURE) {
01459               RETURN_FALSE;
01460        }
01461 
01462        php_stream_from_zval(stream, &zstream);
01463 
01464        if (ZEND_NUM_ARGS() >= 3) {
01465               if (zsessstream) {
01466                      php_stream_from_zval(sessstream, &zsessstream);
01467               }
01468 
01469               if (php_stream_xport_crypto_setup(stream, cryptokind, sessstream TSRMLS_CC) < 0) {
01470                      RETURN_FALSE;
01471               }
01472        } else if (enable) {
01473               php_error_docref(NULL TSRMLS_CC, E_WARNING, "When enabling encryption you must specify the crypto type");
01474               RETURN_FALSE;
01475        }
01476 
01477        ret = php_stream_xport_crypto_enable(stream, enable TSRMLS_CC);
01478        switch (ret) {
01479               case -1:
01480                      RETURN_FALSE;
01481 
01482               case 0:
01483                      RETURN_LONG(0);
01484 
01485               default:
01486                      RETURN_TRUE;
01487        }
01488 }
01489 /* }}} */
01490 
01491 /* {{{ proto string stream_resolve_include_path(string filename)
01492 Determine what file will be opened by calls to fopen() with a relative path */
01493 PHP_FUNCTION(stream_resolve_include_path)
01494 {
01495        char *filename, *resolved_path;
01496        int filename_len;
01497 
01498        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
01499               return;
01500        }
01501 
01502        resolved_path = zend_resolve_path(filename, filename_len TSRMLS_CC);
01503 
01504        if (resolved_path) {
01505               RETURN_STRING(resolved_path, 0);
01506        }
01507        RETURN_FALSE;
01508 }
01509 /* }}} */
01510 
01511 /* {{{ proto bool stream_is_local(resource stream|string url) U
01512 */
01513 PHP_FUNCTION(stream_is_local)
01514 {
01515        zval **zstream;
01516        php_stream *stream = NULL;
01517        php_stream_wrapper *wrapper = NULL;
01518 
01519        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &zstream) == FAILURE) {
01520               RETURN_FALSE;
01521        }
01522 
01523        if (Z_TYPE_PP(zstream) == IS_RESOURCE) {
01524               php_stream_from_zval(stream, zstream);
01525               if (stream == NULL) {
01526                      RETURN_FALSE;
01527               }
01528               wrapper = stream->wrapper;
01529        } else {
01530               convert_to_string_ex(zstream);
01531 
01532               wrapper = php_stream_locate_url_wrapper(Z_STRVAL_PP(zstream), NULL, 0 TSRMLS_CC);
01533        }
01534 
01535        if (!wrapper) {
01536               RETURN_FALSE;
01537        }
01538 
01539        RETURN_BOOL(wrapper->is_url==0);
01540 }
01541 /* }}} */
01542 
01543 /* {{{ proto bool stream_supports_lock(resource stream)
01544    Tells wether the stream supports locking through flock(). */
01545 PHP_FUNCTION(stream_supports_lock)
01546 {
01547        php_stream *stream;
01548        zval *zsrc;
01549 
01550        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zsrc) == FAILURE) {
01551               RETURN_FALSE;
01552        }
01553 
01554        php_stream_from_zval(stream, &zsrc);
01555 
01556        if (!php_stream_supports_lock(stream)) {
01557               RETURN_FALSE;
01558        }
01559 
01560        RETURN_TRUE;
01561 }
01562 
01563 #ifdef HAVE_SHUTDOWN
01564 /* {{{ proto int stream_socket_shutdown(resource stream, int how)
01565        causes all or part of a full-duplex connection on the socket associated
01566        with stream to be shut down.  If how is SHUT_RD,  further receptions will
01567        be disallowed. If how is SHUT_WR, further transmissions will be disallowed.
01568        If how is SHUT_RDWR,  further  receptions and transmissions will be
01569        disallowed. */
01570 PHP_FUNCTION(stream_socket_shutdown)
01571 {
01572        long how;
01573        zval *zstream;
01574        php_stream *stream;
01575 
01576        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zstream, &how) == FAILURE) {
01577               RETURN_FALSE;
01578        }
01579 
01580        if (how != STREAM_SHUT_RD &&
01581            how != STREAM_SHUT_WR &&
01582            how != STREAM_SHUT_RDWR) {
01583               RETURN_FALSE;
01584        }
01585 
01586        php_stream_from_zval(stream, &zstream);
01587 
01588        RETURN_BOOL(php_stream_xport_shutdown(stream, (stream_shutdown_t)how TSRMLS_CC) == 0);
01589 }
01590 /* }}} */
01591 #endif
01592 
01593 /*
01594  * Local variables:
01595  * tab-width: 4
01596  * c-basic-offset: 4
01597  * End:
01598  * vim600: noet sw=4 ts=4 fdm=marker
01599  * vim<600: noet sw=4 ts=4
01600  */
01601