]> granicus.if.org Git - php/commitdiff
- The beginning of an attempt to cleanup fopen-wrappers.
authorAndi Gutmans <andi@php.net>
Wed, 16 Aug 2000 19:26:21 +0000 (19:26 +0000)
committerAndi Gutmans <andi@php.net>
Wed, 16 Aug 2000 19:26:21 +0000 (19:26 +0000)
- I started with trying to localize the V_FOPEN's so that we can have a
- version which won't really open the file for include_once/require_once to
- work faster and have less chance of a race which would cause a fd leak.
- What I did will, therefore, change but I want to do this step by step
- because the code is extremley messy so first of all I want to make sure
- that the isolating of the V_FOPEN code doesn't break anything.
- How about moving URL stuff out of this file?
- php_fopen_url_wrapper() copy and pasted the second part of
- php_fopen_wrapper() (incorrectly). Please try not to copy&paste code but
- centralize functionality. Need to think of a nice way to nuke one of the
- copies and have both functions use the same one.

main/fopen_wrappers.c
main/fopen_wrappers.h

index 6a111245a5a5a05a93d2bf3146313e1018d0ee34..5c3ec5e75246f45ab9e443bf4253be19d0d58754 100644 (file)
@@ -247,6 +247,17 @@ PHPAPI int php_check_open_basedir(char *path)
        return 0;
 }
 
+FILE *php_fopen_and_set_opened_path(const char *path, char *mode, char **opened_path)
+{
+               FILE *fp;
+
+               fp = V_FOPEN(path, mode);
+               if (fp && opened_path) {
+                       *opened_path = expand_filepath(path,NULL);
+               }
+               return fp;
+}
+
 PHPAPI FILE *php_fopen_wrapper(char *path, char *mode, int options, int *issock, int *socketd, char **opened_path)
 {
        PLS_FETCH();
@@ -266,19 +277,13 @@ PHPAPI FILE *php_fopen_wrapper(char *path, char *mode, int options, int *issock,
        if (options & USE_PATH && PG(include_path) != NULL) {
                return php_fopen_with_path(path, mode, PG(include_path), opened_path);
        } else {
-               FILE *fp;
-
                if (options & ENFORCE_SAFE_MODE && PG(safe_mode) && (!php_checkuid(path, mode, 0))) {
                        return NULL;
                }
                if (php_check_open_basedir(path)) {
                        return NULL;
                }
-               fp = V_FOPEN(path, mode);
-               if (fp && opened_path) {
-                       *opened_path = expand_filepath(path,NULL);
-               }
-               return fp;
+               return php_fopen_and_set_opened_path(path, mode, opened_path);
        }
 }
 
@@ -391,7 +396,6 @@ PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **
        char *pathbuf, *ptr, *end;
        char trypath[MAXPATHLEN + 1];
        struct stat sb;
-       FILE *fp;
        PLS_FETCH();
 
        if (opened_path) {
@@ -404,11 +408,8 @@ PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **
                        return NULL;
                }
                if (php_check_open_basedir(filename)) return NULL;
-               fp = V_FOPEN(filename, mode);
-               if (fp && opened_path) {
-                       *opened_path = expand_filepath(filename,NULL);
-               }
-               return fp;
+               
+               return php_fopen_and_set_opened_path(filename, mode, opened_path);
        }
        /* Absolute path open - prepend document_root in safe mode */
 #ifdef PHP_WIN32
@@ -426,20 +427,12 @@ PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **
                                return NULL;
                        }
                        if (php_check_open_basedir(trypath)) return NULL;
-                       fp = V_FOPEN(trypath, mode);
-                       if (fp && opened_path) {
-                               *opened_path = expand_filepath(trypath,NULL);
-                       }
-                       return fp;
+                       return php_fopen_and_set_opened_path(filename, mode, opened_path);
                } else {
                        if (php_check_open_basedir(filename)) {
                                return NULL;
                        }
-                       fp = V_FOPEN(filename, mode);
-                       if (fp && opened_path) {
-                               *opened_path = expand_filepath(filename,NULL);
-                       }
-                       return fp;
+                       return php_fopen_and_set_opened_path(filename, mode, opened_path);
                }
        }
        if (!path || (path && !*path)) {
@@ -449,11 +442,7 @@ PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **
                if (php_check_open_basedir(filename)) {
                        return NULL;
                }
-               fp = V_FOPEN(filename, mode);
-               if (fp && opened_path) {
-                       *opened_path = strdup(filename);
-               }
-               return fp;
+               return php_fopen_and_set_opened_path(filename, mode, opened_path);
        }
        pathbuf = estrdup(path);
 
@@ -476,17 +465,9 @@ PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **
                                return NULL;
                        }
                }
-               if ((fp = V_FOPEN(trypath, mode)) != NULL) {
-                       if (php_check_open_basedir(trypath)) {
-                               fclose(fp);
-                               efree(pathbuf);
-                               return NULL;
-                       }
-                       if (opened_path) {
-                               *opened_path = expand_filepath(trypath,NULL);
-                       }
+               if (!php_check_open_basedir(trypath)) {
                        efree(pathbuf);
-                       return fp;
+                       return php_fopen_and_set_opened_path(trypath, mode, opened_path);
                }
                ptr = end;
        }
@@ -1036,12 +1017,10 @@ static FILE *php_fopen_url_wrapper(const char *path, char *mode, int options, in
                                if (php_check_open_basedir((char *) path)) {
                                        fp = NULL;
                                } else {
-                                       fp = V_FOPEN(path, mode);
+                                       fp = php_fopen_and_set_opened_path(path, mode, opened_path);
                                }
                        }
                }
-
-
                return (fp);
        }
                        
@@ -1100,7 +1079,7 @@ PHPAPI char *php_strip_url_passwd(char *url)
 
 #if 1
 
-PHPAPI char *expand_filepath(char *filepath, char *real_path)
+PHPAPI char *expand_filepath(const char *filepath, char *real_path)
 {
        cwd_state new_state;
        char cwd[MAXPATHLEN+1];
index 919cf04010acb30f412367e692ef01053b5c7b92..7d119bd35a437e8ae991bf2c2dd076113d84bbb7 100644 (file)
@@ -22,9 +22,9 @@
 
 #include "php_globals.h"
 
-#define IGNORE_PATH    0
-#define USE_PATH       1
-#define IGNORE_URL     2
+#define IGNORE_PATH            0
+#define USE_PATH               1
+#define IGNORE_URL             2
 /* There's no USE_URL. */
 #ifdef PHP_WIN32
 # define IGNORE_URL_WIN 2
@@ -76,7 +76,7 @@ PHPAPI FILE *php_fopen_with_path(char *filename, char *mode, char *path, char **
 PHPAPI int php_is_url(char *path);
 PHPAPI char *php_strip_url_passwd(char *path);
 
-PHPAPI char *expand_filepath(char *filepath,char *real_path);
+PHPAPI char *expand_filepath(const char *filepath,char *real_path);
 
 int php_init_fopen_wrappers(void); 
 int php_shutdown_fopen_wrappers(void);