Back to index

php5  5.3.10
Defines | Functions | Variables
php_stream_plain_wrapper.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define php_stream_fopen(filename, mode, opened)   _php_stream_fopen((filename), (mode), (opened), 0 STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_with_path(filename, mode, path, opened)   _php_stream_fopen_with_path((filename), (mode), (path), (opened) STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_from_file(file, mode)   _php_stream_fopen_from_file((file), (mode) STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_from_fd(fd, mode, persistent_id)   _php_stream_fopen_from_fd((fd), (mode), (persistent_id) STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_from_pipe(file, mode)   _php_stream_fopen_from_pipe((file), (mode) STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_tmpfile()   _php_stream_fopen_tmpfile(0 STREAMS_CC TSRMLS_CC)
#define php_stream_fopen_temporary_file(dir, pfx, opened_path)   _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_CC TSRMLS_CC)
#define php_stream_open_wrapper_as_file(path, mode, options, opened_path)   _php_stream_open_wrapper_as_file((path), (mode), (options), (opened_path) STREAMS_CC TSRMLS_CC)

Functions

PHPAPI php_stream * _php_stream_fopen (const char *filename, const char *mode, char **opened_path, int options STREAMS_DC TSRMLS_DC)
PHPAPI php_stream * _php_stream_fopen_with_path (char *filename, char *mode, char *path, char **opened_path, int options STREAMS_DC TSRMLS_DC)
PHPAPI php_stream * _php_stream_fopen_from_file (FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
PHPAPI php_stream * _php_stream_fopen_from_fd (int fd, const char *mode, const char *persistent_id STREAMS_DC TSRMLS_DC)
PHPAPI php_stream * _php_stream_fopen_from_pipe (FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
PHPAPI php_stream * _php_stream_fopen_tmpfile (int dummy STREAMS_DC TSRMLS_DC)
PHPAPI php_stream * _php_stream_fopen_temporary_file (const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC)
PHPAPI FILE * _php_stream_open_wrapper_as_file (char *path, char *mode, int options, char **opened_path STREAMS_DC TSRMLS_DC)

Variables

PHPAPI php_stream_ops php_stream_stdio_ops
PHPAPI php_stream_wrapper php_plain_files_wrapper

Define Documentation

#define php_stream_fopen (   filename,
  mode,
  opened 
)    _php_stream_fopen((filename), (mode), (opened), 0 STREAMS_CC TSRMLS_CC)

Definition at line 31 of file php_stream_plain_wrapper.h.

#define php_stream_fopen_from_fd (   fd,
  mode,
  persistent_id 
)    _php_stream_fopen_from_fd((fd), (mode), (persistent_id) STREAMS_CC TSRMLS_CC)

Definition at line 40 of file php_stream_plain_wrapper.h.

#define php_stream_fopen_from_file (   file,
  mode 
)    _php_stream_fopen_from_file((file), (mode) STREAMS_CC TSRMLS_CC)

Definition at line 37 of file php_stream_plain_wrapper.h.

#define php_stream_fopen_from_pipe (   file,
  mode 
)    _php_stream_fopen_from_pipe((file), (mode) STREAMS_CC TSRMLS_CC)

Definition at line 43 of file php_stream_plain_wrapper.h.

#define php_stream_fopen_temporary_file (   dir,
  pfx,
  opened_path 
)    _php_stream_fopen_temporary_file((dir), (pfx), (opened_path) STREAMS_CC TSRMLS_CC)

Definition at line 49 of file php_stream_plain_wrapper.h.

Definition at line 46 of file php_stream_plain_wrapper.h.

#define php_stream_fopen_with_path (   filename,
  mode,
  path,
  opened 
)    _php_stream_fopen_with_path((filename), (mode), (path), (opened) STREAMS_CC TSRMLS_CC)

Definition at line 34 of file php_stream_plain_wrapper.h.

#define php_stream_open_wrapper_as_file (   path,
  mode,
  options,
  opened_path 
)    _php_stream_open_wrapper_as_file((path), (mode), (options), (opened_path) STREAMS_CC TSRMLS_CC)

Definition at line 55 of file php_stream_plain_wrapper.h.


Function Documentation

PHPAPI php_stream* _php_stream_fopen ( const char *  filename,
const char *  mode,
char **  opened_path,
int options STREAMS_DC  TSRMLS_DC 
)

Definition at line 895 of file plain_wrapper.c.

{
       char *realpath = NULL;
       int open_flags;
       int fd;
       php_stream *ret;
       int persistent = options & STREAM_OPEN_PERSISTENT;
       char *persistent_id = NULL;

       if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) {
              if (options & REPORT_ERRORS) {
                     php_error_docref(NULL TSRMLS_CC, E_WARNING, "`%s' is not a valid mode for fopen", mode);
              }
              return NULL;
       }

       if (options & STREAM_ASSUME_REALPATH) {
              realpath = estrdup(filename);
       } else {
              if ((realpath = expand_filepath(filename, NULL TSRMLS_CC)) == NULL) {
                     return NULL;
              }
       }

       if (persistent) {
              spprintf(&persistent_id, 0, "streams_stdio_%d_%s", open_flags, realpath);
              switch (php_stream_from_persistent_id(persistent_id, &ret TSRMLS_CC)) {
                     case PHP_STREAM_PERSISTENT_SUCCESS:
                            if (opened_path) {
                                   *opened_path = realpath;
                                   realpath = NULL;
                            }
                            /* fall through */

                     case PHP_STREAM_PERSISTENT_FAILURE:
                            if (realpath) {
                                   efree(realpath);
                            }
                            efree(persistent_id);;
                            return ret;
              }
       }
       
       fd = open(realpath, open_flags, 0666);

       if (fd != -1) {

              if (options & STREAM_OPEN_FOR_INCLUDE) {
                     ret = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);
              } else {
                     ret = php_stream_fopen_from_fd_rel(fd, mode, persistent_id);
              }

              if (ret)      {
                     if (opened_path) {
                            *opened_path = realpath;
                            realpath = NULL;
                     }
                     if (realpath) {
                            efree(realpath);
                     }
                     if (persistent_id) {
                            efree(persistent_id);
                     }

                     /* WIN32 always set ISREG flag */
#ifndef PHP_WIN32
                     /* sanity checks for include/require.
                      * We check these after opening the stream, so that we save
                      * on fstat() syscalls */
                     if (options & STREAM_OPEN_FOR_INCLUDE) {
                            php_stdio_stream_data *self = (php_stdio_stream_data*)ret->abstract;
                            int r;

                            r = do_fstat(self, 0);
                            if ((r == 0 && !S_ISREG(self->sb.st_mode))) {
                                   if (opened_path) {
                                          efree(*opened_path);
                                          *opened_path = NULL;
                                   }
                                   php_stream_close(ret);
                                   return NULL;
                            }
                     }
#endif

                     return ret;
              }
              close(fd);
       }
       efree(realpath);
       if (persistent_id) {
              efree(persistent_id);
       }
       return NULL;
}

Here is the call graph for this function:

PHPAPI php_stream* _php_stream_fopen_from_fd ( int  fd,
const char *  mode,
const char *persistent_id STREAMS_DC  TSRMLS_DC 
)

Definition at line 223 of file plain_wrapper.c.

{
       php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id);

       if (stream) {
              php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;

#ifdef S_ISFIFO
              /* detect if this is a pipe */
              if (self->fd >= 0) {
                     self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
              }
#elif defined(PHP_WIN32)
              {
                     zend_uintptr_t handle = _get_osfhandle(self->fd);

                     if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
                            self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
                     }
              }
#endif
       
              if (self->is_pipe) {
                     stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
              } else {
                     stream->position = lseek(self->fd, 0, SEEK_CUR);
#ifdef ESPIPE
                     if (stream->position == (off_t)-1 && errno == ESPIPE) {
                            stream->position = 0;
                            stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
                            self->is_pipe = 1;
                     }
#endif
              }
       }

       return stream;
}

Here is the call graph for this function:

PHPAPI php_stream* _php_stream_fopen_from_file ( FILE *  file,
const char *mode STREAMS_DC  TSRMLS_DC 
)

Definition at line 262 of file plain_wrapper.c.

{
       php_stream *stream = php_stream_fopen_from_file_int_rel(file, mode);

       if (stream) {
              php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;

#ifdef S_ISFIFO
              /* detect if this is a pipe */
              if (self->fd >= 0) {
                     self->is_pipe = (do_fstat(self, 0) == 0 && S_ISFIFO(self->sb.st_mode)) ? 1 : 0;
              }
#elif defined(PHP_WIN32)
              {
                     zend_uintptr_t handle = _get_osfhandle(self->fd);

                     if (handle != (zend_uintptr_t)INVALID_HANDLE_VALUE) {
                            self->is_pipe = GetFileType((HANDLE)handle) == FILE_TYPE_PIPE;
                     }
              }
#endif
       
              if (self->is_pipe) {
                     stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
              } else {
                     stream->position = ftell(file);
              }
       }

       return stream;
}

Here is the call graph for this function:

PHPAPI php_stream* _php_stream_fopen_from_pipe ( FILE *  file,
const char *mode STREAMS_DC  TSRMLS_DC 
)

Definition at line 294 of file plain_wrapper.c.

{
       php_stdio_stream_data *self;
       php_stream *stream;

       self = emalloc_rel_orig(sizeof(*self));
       memset(self, 0, sizeof(*self));
       self->file = file;
       self->is_pipe = 1;
       self->lock_flag = LOCK_UN;
       self->is_process_pipe = 1;
       self->fd = fileno(file);
       self->temp_file_name = NULL;

       stream = php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
       stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
       return stream;
}
PHPAPI php_stream* _php_stream_fopen_temporary_file ( const char *  dir,
const char *  pfx,
char **opened_path STREAMS_DC  TSRMLS_DC 
)

Definition at line 179 of file plain_wrapper.c.

{
       int fd = php_open_temporary_fd(dir, pfx, opened_path TSRMLS_CC);

       if (fd != -1) {
              php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
              if (stream) {
                     return stream;
              }
              close(fd);

              php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");

              return NULL;
       }
       return NULL;
}

Here is the call graph for this function:

PHPAPI php_stream* _php_stream_fopen_tmpfile ( int dummy STREAMS_DC  TSRMLS_DC)

Definition at line 197 of file plain_wrapper.c.

{
       char *opened_path = NULL;
       int fd = php_open_temporary_fd(NULL, "php", &opened_path TSRMLS_CC);

       if (fd != -1) {
              php_stream *stream = php_stream_fopen_from_fd_int_rel(fd, "r+b", NULL);
              if (stream) {
                     php_stdio_stream_data *self = (php_stdio_stream_data*)stream->abstract;
                     stream->wrapper = &php_plain_files_wrapper;
                     stream->orig_path = estrdup(opened_path);

                     self->temp_file_name = opened_path;
                     self->lock_flag = LOCK_UN;
                     
                     return stream;
              }
              close(fd);

              php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");

              return NULL;
       }
       return NULL;
}

Here is the call graph for this function:

PHPAPI php_stream* _php_stream_fopen_with_path ( char *  filename,
char *  mode,
char *  path,
char **  opened_path,
int options STREAMS_DC  TSRMLS_DC 
)

Definition at line 1309 of file plain_wrapper.c.

{
       /* code ripped off from fopen_wrappers.c */
       char *pathbuf, *ptr, *end;
       char *exec_fname;
       char trypath[MAXPATHLEN];
       php_stream *stream;
       int path_length;
       int filename_length;
       int exec_fname_length;

       if (opened_path) {
              *opened_path = NULL;
       }

       if(!filename) {
              return NULL;
       }

       filename_length = strlen(filename);

       /* Relative path open */
       if (*filename == '.' && (IS_SLASH(filename[1]) || filename[1] == '.')) {
              /* further checks, we could have ....... filenames */
              ptr = filename + 1;
              if (*ptr == '.') {
                     while (*(++ptr) == '.');
                     if (!IS_SLASH(*ptr)) { /* not a relative path after all */
                            goto not_relative_path;
                     }
              }


              if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
                     return NULL;
              }

              if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
                     return NULL;
              }
              return php_stream_fopen_rel(filename, mode, opened_path, options);
       }

       /*
        * files in safe_mode_include_dir (or subdir) are excluded from
        * safe mode GID/UID checks
        */

not_relative_path:

       /* Absolute path open */
       if (IS_ABSOLUTE_PATH(filename, filename_length)) {

              if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
                     return NULL;
              }

              if ((php_check_safe_mode_include_dir(filename TSRMLS_CC)) == 0)
                     /* filename is in safe_mode_include_dir (or subdir) */
                     return php_stream_fopen_rel(filename, mode, opened_path, options);

              if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM)))
                     return NULL;

              return php_stream_fopen_rel(filename, mode, opened_path, options);
       }
       
#ifdef PHP_WIN32
       if (IS_SLASH(filename[0])) {
              size_t cwd_len;
              char *cwd;
              cwd = virtual_getcwd_ex(&cwd_len TSRMLS_CC);
              /* getcwd() will return always return [DRIVE_LETTER]:/) on windows. */
              *(cwd+3) = '\0';
       
              if (snprintf(trypath, MAXPATHLEN, "%s%s", cwd, filename) >= MAXPATHLEN) {
                     php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", cwd, filename, MAXPATHLEN);
              }
              
              free(cwd);
              
              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) {
                     return php_stream_fopen_rel(trypath, mode, opened_path, options);
              }      
              if (PG(safe_mode) && (!php_checkuid(trypath, mode, CHECKUID_CHECK_MODE_PARAM))) {
                     return NULL;
              }
              
              return php_stream_fopen_rel(trypath, mode, opened_path, options);
       }
#endif

       if (!path || (path && !*path)) {
              if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
                     return NULL;
              }
              return php_stream_fopen_rel(filename, mode, opened_path, options);
       }

       /* check in provided path */
       /* append the calling scripts' current working directory
        * as a fall back case
        */
       if (zend_is_executing(TSRMLS_C)) {
              exec_fname = zend_get_executed_filename(TSRMLS_C);
              exec_fname_length = strlen(exec_fname);
              path_length = strlen(path);

              while ((--exec_fname_length >= 0) && !IS_SLASH(exec_fname[exec_fname_length]));
              if ((exec_fname && exec_fname[0] == '[')
                            || exec_fname_length<=0) {
                     /* [no active file] or no path */
                     pathbuf = estrdup(path);
              } else {
                     pathbuf = (char *) emalloc(exec_fname_length + path_length +1 +1);
                     memcpy(pathbuf, path, path_length);
                     pathbuf[path_length] = DEFAULT_DIR_SEPARATOR;
                     memcpy(pathbuf+path_length+1, exec_fname, exec_fname_length);
                     pathbuf[path_length + exec_fname_length +1] = '\0';
              }
       } else {
              pathbuf = estrdup(path);
       }

       ptr = pathbuf;

       while (ptr && *ptr) {
              end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
              if (end != NULL) {
                     *end = '\0';
                     end++;
              }
              if (*ptr == '\0') {
                     goto stream_skip;
              }
              if (snprintf(trypath, MAXPATHLEN, "%s/%s", ptr, filename) >= MAXPATHLEN) {
                     php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s/%s path was truncated to %d", ptr, filename, MAXPATHLEN);
              }

              if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir_ex(trypath, 0 TSRMLS_CC)) {
                     goto stream_skip;
              }
              
              if (PG(safe_mode)) {
                     struct stat sb;

                     if (VCWD_STAT(trypath, &sb) == 0) {
                            /* file exists ... check permission */
                            if ((php_check_safe_mode_include_dir(trypath TSRMLS_CC) == 0) ||
                                          php_checkuid_ex(trypath, mode, CHECKUID_CHECK_MODE_PARAM, CHECKUID_NO_ERRORS)) {
                                   /* UID ok, or trypath is in safe_mode_include_dir */
                                   stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
                                   goto stream_done;
                            }
                     }
                     goto stream_skip;
              }
              stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
              if (stream) {
stream_done:
                     efree(pathbuf);
                     return stream;
              }
stream_skip:
              ptr = end;
       } /* end provided path */

       efree(pathbuf);
       return NULL;

}

Here is the call graph for this function:

PHPAPI FILE* _php_stream_open_wrapper_as_file ( char *  path,
char *  mode,
int  options,
char **opened_path STREAMS_DC  TSRMLS_DC 
)

Definition at line 346 of file cast.c.

{
       FILE *fp = NULL;
       php_stream *stream = NULL;

       stream = php_stream_open_wrapper_rel(path, mode, options|STREAM_WILL_CAST, opened_path);

       if (stream == NULL) {
              return NULL;
       }

       if (php_stream_cast(stream, PHP_STREAM_AS_STDIO|PHP_STREAM_CAST_TRY_HARD|PHP_STREAM_CAST_RELEASE, (void**)&fp, REPORT_ERRORS) == FAILURE) {
              php_stream_close(stream);
              if (opened_path && *opened_path) {
                     efree(*opened_path);
              }
              return NULL;
       }
       return fp;
}

Variable Documentation

PHPAPI php_stream_wrapper php_plain_files_wrapper

Definition at line 1302 of file plain_wrapper.c.

Definition at line 800 of file plain_wrapper.c.