]> granicus.if.org Git - php/commitdiff
MFB: Bunch of streams related fixes.
authorWez Furlong <wez@php.net>
Mon, 24 Feb 2003 21:40:23 +0000 (21:40 +0000)
committerWez Furlong <wez@php.net>
Mon, 24 Feb 2003 21:40:23 +0000 (21:40 +0000)
ext/standard/file.c
main/php_streams.h
main/streams/cast.c
main/streams/plain_wrapper.c
main/streams/streams.c

index 810367f69494f01fbfb7447c6add75f7141ef8f4..b4f118b76cd3f22aea16e2bbf9cf8cb06784aa63 100644 (file)
@@ -2384,7 +2384,7 @@ PHPAPI int php_copy_file(char *src, char *dest TSRMLS_DC)
        int ret = FAILURE;
 
        srcstream = php_stream_open_wrapper(src, "rb", 
-                               ENFORCE_SAFE_MODE | REPORT_ERRORS,
+                               STREAM_DISABLE_OPEN_BASEDIR | REPORT_ERRORS,
                                NULL);
        
        if (!srcstream) {
index 5e22792682b9a1208db3b0da198280386113bcd1..194172c1c3e814aed7cbfe95524ffff722a52623 100755 (executable)
@@ -406,6 +406,9 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show
 /* this flag is used when only the headers from HTTP request are to be fetched */
 #define STREAM_ONLY_GET_HEADERS                512
 
+/* don't apply open_basedir checks */
+#define STREAM_DISABLE_OPEN_BASEDIR    1024
+
 /* Antique - no longer has meaning */
 #define IGNORE_URL_WIN 0
 
index 66acd141a3409a214cf14aaf40bce0167379aaea..aa6e6d12b254b12478d48ab5501230c9c5db07e8 100644 (file)
@@ -208,7 +208,12 @@ PHPAPI int _php_stream_cast(php_stream *stream, int castas, void **ret, int show
                return FAILURE;
 #endif
 
-               if (flags & PHP_STREAM_CAST_TRY_HARD) {
+               if (!php_stream_is_filtered(stream) && stream->ops->cast && stream->ops->cast(stream, castas, NULL TSRMLS_CC) == SUCCESS) {
+                       if (FAILURE == stream->ops->cast(stream, castas, ret TSRMLS_CC)) {
+                               return FAILURE;
+                       }
+                       goto exit_success;
+               } else if (flags & PHP_STREAM_CAST_TRY_HARD) {
                        php_stream *newstream;
 
                        newstream = php_stream_fopen_tmpfile();
index d45e6a1098c4ce7f714f8673c5b7736c95b90462..cf85586d78fcf167efc4106012c70ccaeb4ac1f4 100644 (file)
@@ -603,7 +603,7 @@ static php_stream *php_plain_files_dir_opener(php_stream_wrapper *wrapper, char
        DIR *dir = NULL;
        php_stream *stream = NULL;
 
-       if (php_check_open_basedir(path TSRMLS_CC)) {
+       if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(path TSRMLS_CC)) {
                return NULL;
        }
        
@@ -637,7 +637,7 @@ static php_stream *php_plain_files_stream_opener(php_stream_wrapper *wrapper, ch
                return php_stream_fopen_with_path_rel(path, mode, PG(include_path), opened_path, options);
        }
 
-       if (php_check_open_basedir(path TSRMLS_CC)) {
+       if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(path TSRMLS_CC)) {
                return NULL;
        }
 
@@ -702,7 +702,7 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
                }
 
 
-               if (php_check_open_basedir(filename TSRMLS_CC)) {
+               if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
                        return NULL;
                }
 
@@ -722,7 +722,7 @@ not_relative_path:
        /* Absolute path open */
        if (IS_ABSOLUTE_PATH(filename, filename_length)) {
 
-               if (php_check_open_basedir(filename TSRMLS_CC)) {
+               if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
                        return NULL;
                }
 
@@ -748,7 +748,7 @@ not_relative_path:
                
                free(cwd);
                
-               if (php_check_open_basedir(trypath TSRMLS_CC)) {
+               if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(trypath TSRMLS_CC)) {
                        return NULL;
                }
                if ((php_check_safe_mode_include_dir(trypath TSRMLS_CC)) == 0) {
@@ -764,7 +764,7 @@ not_relative_path:
 
        if (!path || (path && !*path)) {
 
-               if (php_check_open_basedir(path TSRMLS_CC)) {
+               if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(path TSRMLS_CC)) {
                        return NULL;
                }
 
@@ -809,7 +809,7 @@ not_relative_path:
                }
                snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename);
                
-               if (php_check_open_basedir(trypath TSRMLS_CC)) {
+               if ((options & STREAM_DISABLE_OPEN_BASEDIR == 0) && php_check_open_basedir(trypath TSRMLS_CC)) {
                        stream = NULL;
                        goto stream_done;
                }
index ae33818b79f1a3a061ea80c1474aac348969e1ce..e798684d182d0b04dbe65be82c60a4f7bac77ce7 100755 (executable)
@@ -801,6 +801,16 @@ static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size
 {
        size_t didwrite = 0, towrite, justwrote;
 
+       /* if we have a seekable stream we need to ensure that data is written at the
+        * current stream->position. This means invalidating the read buffer and then
+        * performing a low-level seek */
+       if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
+               stream->readpos = stream->writepos = 0;
+
+               stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position TSRMLS_CC);
+       }
+
        while (count > 0) {
                towrite = count;
                if (towrite > stream->chunk_size)
@@ -817,8 +827,6 @@ static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size
                         * buffered from fifos and sockets */
                        if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && !php_stream_is_filtered(stream)) {
                                stream->position += justwrote;
-                               stream->writepos = 0;
-                               stream->readpos = 0;
                        }
                } else {
                        break;
@@ -947,10 +955,6 @@ PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC)
 
 PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC)
 {
-       /* not moving anywhere */
-       if ((offset == 0 && whence == SEEK_CUR) || (offset == stream->position && whence == SEEK_SET))
-               return 0;
-
        /* handle the case where we are in the buffer */
        if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) {
                switch(whence) {
@@ -974,8 +978,6 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_
                }
        }
        
-       /* invalidate the buffer contents */
-       stream->readpos = stream->writepos = 0;
 
        if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
                int ret;
@@ -995,6 +997,10 @@ PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_
                if (((stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) || ret == 0) {
                        if (ret == 0)
                                stream->eof = 0;
+
+                       /* invalidate the buffer contents */
+                       stream->readpos = stream->writepos = 0;
+
                        return ret;
                }
                /* else the stream has decided that it can't support seeking after all;