22
22
#include "php_globals.h"
23
23
#include "php_network.h"
24
24
#include "php_open_temporary_file.h"
25
+ #include "ext/standard/url.h"
25
26
#include "ext/standard/file.h"
26
27
#include "ext/standard/flock_compat.h"
27
28
#include <stddef.h>
@@ -86,22 +87,32 @@ PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
86
87
PHPAPI php_stream * _php_stream_fopen (const char * filename , const char * mode , char * * opened_path , int options STREAMS_DC TSRMLS_DC )
87
88
{
88
89
char * realpath = NULL ;
90
+ php_url * url = NULL ;
89
91
struct stat st ;
90
92
int open_flags ;
91
93
int fd ;
92
- php_stream * ret ;
94
+ php_stream * ret = NULL ;
93
95
int persistent = options & STREAM_OPEN_PERSISTENT ;
94
96
char * persistent_id = NULL ;
95
97
98
+ if (!filename ) {
99
+ return NULL ;
100
+ }
101
+
96
102
if (FAILURE == php_stream_parse_fopen_modes (mode , & open_flags )) {
97
103
if (options & REPORT_ERRORS ) {
98
104
php_error_docref (NULL TSRMLS_CC , E_WARNING , "`%s' is not a valid mode for fopen" , mode );
99
105
}
100
106
return NULL ;
101
107
}
102
108
109
+ if (!strncasecmp (filename , "file" , 4 )) {
110
+ url = php_url_parse ((char * )filename );
111
+ filename = url -> path ;
112
+ }
113
+
103
114
if ((realpath = expand_filepath (filename , NULL TSRMLS_CC )) == NULL ) {
104
- return NULL ;
115
+ goto stream_fopen_done ;
105
116
}
106
117
107
118
if (persistent ) {
@@ -112,14 +123,10 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
112
123
* opened_path = realpath ;
113
124
realpath = NULL ;
114
125
}
115
- if (realpath ) {
116
- efree (realpath );
117
- }
118
126
/* fall through */
119
127
120
128
case PHP_STREAM_PERSISTENT_FAILURE :
121
- efree (persistent_id );;
122
- return ret ;
129
+ goto stream_fopen_done ;
123
130
}
124
131
}
125
132
@@ -143,22 +150,22 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
143
150
* opened_path = realpath ;
144
151
realpath = NULL ;
145
152
}
146
- if (realpath ) {
147
- efree (realpath );
148
- }
149
- if (persistent_id ) {
150
- efree (persistent_id );
151
- }
152
- return ret ;
153
+ goto stream_fopen_done ;
153
154
}
154
155
err :
155
156
close (fd );
156
157
}
157
- efree (realpath );
158
+ stream_fopen_done :
159
+ if (realpath ) {
160
+ efree (realpath );
161
+ }
162
+ if (url ) {
163
+ efree (url );
164
+ }
158
165
if (persistent_id ) {
159
166
efree (persistent_id );
160
167
}
161
- return NULL ;
168
+ return ret ;
162
169
}
163
170
/* }}} */
164
171
@@ -959,6 +966,7 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
959
966
{
960
967
/* code ripped off from fopen_wrappers.c */
961
968
char * pathbuf , * ptr , * end ;
969
+ php_url * url = NULL ;
962
970
char * exec_fname ;
963
971
char trypath [MAXPATHLEN ];
964
972
struct stat sb ;
@@ -975,6 +983,11 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
975
983
return NULL ;
976
984
}
977
985
986
+ if (!strncasecmp (filename , "file" , 4 )) {
987
+ url = php_url_parse ((char * )filename );
988
+ filename = url -> path ;
989
+ }
990
+
978
991
filename_length = strlen (filename );
979
992
980
993
/* Relative path open */
@@ -990,13 +1003,16 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
990
1003
991
1004
992
1005
if (((options & STREAM_DISABLE_OPEN_BASEDIR ) == 0 ) && php_check_open_basedir (filename TSRMLS_CC )) {
993
- return NULL ;
1006
+ stream = NULL ;
1007
+ goto stream_done ;
994
1008
}
995
1009
996
1010
if (PG (safe_mode ) && (!php_checkuid (filename , mode , CHECKUID_CHECK_MODE_PARAM ))) {
997
- return NULL ;
1011
+ stream = NULL ;
1012
+ goto stream_done ;
998
1013
}
999
- return php_stream_fopen_rel (filename , mode , opened_path , options );
1014
+ stream = php_stream_fopen_rel (filename , mode , opened_path , options );
1015
+ goto stream_done ;
1000
1016
}
1001
1017
1002
1018
/*
@@ -1010,17 +1026,23 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
1010
1026
if (IS_ABSOLUTE_PATH (filename , filename_length )) {
1011
1027
1012
1028
if (((options & STREAM_DISABLE_OPEN_BASEDIR ) == 0 ) && php_check_open_basedir (filename TSRMLS_CC )) {
1013
- return NULL ;
1029
+ stream = NULL ;
1030
+ goto stream_done ;
1014
1031
}
1015
1032
1016
- if ((php_check_safe_mode_include_dir (filename TSRMLS_CC )) = = 0 )
1033
+ if ((php_check_safe_mode_include_dir (filename TSRMLS_CC )) = = 0 ) {
1017
1034
/* filename is in safe_mode_include_dir (or subdir) */
1018
- return php_stream_fopen_rel (filename , mode , opened_path , options );
1035
+ stream = php_stream_fopen_rel (filename , mode , opened_path , options );
1036
+ goto stream_done ;
1037
+ }
1019
1038
1020
- if (PG (safe_mode ) && (!php_checkuid (filename , mode , CHECKUID_CHECK_MODE_PARAM )))
1021
- return NULL ;
1039
+ if (PG (safe_mode ) && (!php_checkuid (filename , mode , CHECKUID_CHECK_MODE_PARAM ))) {
1040
+ stream = NULL ;
1041
+ goto stream_done ;
1042
+ }
1022
1043
1023
- return php_stream_fopen_rel (filename , mode , opened_path , options );
1044
+ stream = php_stream_fopen_rel (filename , mode , opened_path , options );
1045
+ goto stream_done ;
1024
1046
}
1025
1047
1026
1048
#ifdef PHP_WIN32
@@ -1036,29 +1058,36 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
1036
1058
free (cwd );
1037
1059
1038
1060
if (((options & STREAM_DISABLE_OPEN_BASEDIR ) == 0 ) && php_check_open_basedir (trypath TSRMLS_CC )) {
1039
- return NULL ;
1061
+ stream = NULL ;
1062
+ goto stream_done ;
1040
1063
}
1041
1064
if ((php_check_safe_mode_include_dir (trypath TSRMLS_CC )) = = 0 ) {
1042
- return php_stream_fopen_rel (trypath , mode , opened_path , options );
1065
+ stream = php_stream_fopen_rel (trypath , mode , opened_path , options );
1066
+ goto stream_done ;
1043
1067
}
1044
1068
if (PG (safe_mode ) && (!php_checkuid (trypath , mode , CHECKUID_CHECK_MODE_PARAM ))) {
1045
- return NULL ;
1069
+ stream = NULL ;
1070
+ goto stream_done ;
1046
1071
}
1047
1072
1048
- return php_stream_fopen_rel (trypath , mode , opened_path , options );
1073
+ stream = php_stream_fopen_rel (trypath , mode , opened_path , options );
1074
+ goto stream_done ;
1049
1075
}
1050
1076
#endif
1051
1077
1052
1078
if (!path || (path && !* path )) {
1053
1079
1054
1080
if (((options & STREAM_DISABLE_OPEN_BASEDIR ) == 0 ) && php_check_open_basedir (path TSRMLS_CC )) {
1055
- return NULL ;
1081
+ stream = NULL ;
1082
+ goto stream_done ;
1056
1083
}
1057
1084
1058
1085
if (PG (safe_mode ) && (!php_checkuid (filename , mode , CHECKUID_CHECK_MODE_PARAM ))) {
1059
- return NULL ;
1086
+ stream = NULL ;
1087
+ goto stream_done ;
1060
1088
}
1061
- return php_stream_fopen_rel (filename , mode , opened_path , options );
1089
+ stream = php_stream_fopen_rel (filename , mode , opened_path , options );
1090
+ goto stream_done ;
1062
1091
}
1063
1092
1064
1093
/* check in provided path */
@@ -1117,12 +1146,18 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
1117
1146
stream = php_stream_fopen_rel (trypath , mode , opened_path , options );
1118
1147
if (stream ) {
1119
1148
stream_done :
1149
+ if (url ) {
1150
+ efree (url );
1151
+ }
1120
1152
efree (pathbuf );
1121
1153
return stream ;
1122
1154
}
1123
1155
ptr = end ;
1124
1156
} /* end provided path */
1125
1157
1158
+ if (url ) {
1159
+ efree (url );
1160
+ }
1126
1161
efree (pathbuf );
1127
1162
return NULL ;
1128
1163
0 commit comments