Skip to content

Commit e5103d7

Browse files
author
Shane Caraveo
committed
make file uri's work with streams
1 parent 4505a61 commit e5103d7

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

ext/standard/basic_functions.c

+1
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,7 @@ PHP_MINIT_FUNCTION(basic)
11471147
PHP_MINIT(imagetypes)(INIT_FUNC_ARGS_PASSTHRU);
11481148

11491149
php_register_url_stream_wrapper("php", &php_stream_php_wrapper TSRMLS_CC);
1150+
php_register_url_stream_wrapper("file", &php_plain_files_wrapper TSRMLS_CC);
11501151
#ifndef PHP_CURL_URL_WRAPPERS
11511152
php_register_url_stream_wrapper("http", &php_stream_http_wrapper TSRMLS_CC);
11521153
php_register_url_stream_wrapper("ftp", &php_stream_ftp_wrapper TSRMLS_CC);

ext/standard/php_fopen_wrappers.h

+1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ php_stream *php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, cha
2828
extern PHPAPI php_stream_wrapper php_stream_http_wrapper;
2929
extern PHPAPI php_stream_wrapper php_stream_ftp_wrapper;
3030
extern php_stream_wrapper php_stream_php_wrapper;
31+
extern php_stream_wrapper php_plain_files_wrapper;
3132

3233
#endif

main/streams/plain_wrapper.c

+67-32
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "php_globals.h"
2323
#include "php_network.h"
2424
#include "php_open_temporary_file.h"
25+
#include "ext/standard/url.h"
2526
#include "ext/standard/file.h"
2627
#include "ext/standard/flock_compat.h"
2728
#include <stddef.h>
@@ -86,22 +87,32 @@ PHPAPI int php_stream_parse_fopen_modes(const char *mode, int *open_flags)
8687
PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, char **opened_path, int options STREAMS_DC TSRMLS_DC)
8788
{
8889
char *realpath = NULL;
90+
php_url *url = NULL;
8991
struct stat st;
9092
int open_flags;
9193
int fd;
92-
php_stream *ret;
94+
php_stream *ret = NULL;
9395
int persistent = options & STREAM_OPEN_PERSISTENT;
9496
char *persistent_id = NULL;
9597

98+
if(!filename) {
99+
return NULL;
100+
}
101+
96102
if (FAILURE == php_stream_parse_fopen_modes(mode, &open_flags)) {
97103
if (options & REPORT_ERRORS) {
98104
php_error_docref(NULL TSRMLS_CC, E_WARNING, "`%s' is not a valid mode for fopen", mode);
99105
}
100106
return NULL;
101107
}
102108

109+
if (!strncasecmp(filename, "file", 4)) {
110+
url = php_url_parse((char *)filename);
111+
filename = url->path;
112+
}
113+
103114
if ((realpath = expand_filepath(filename, NULL TSRMLS_CC)) == NULL) {
104-
return NULL;
115+
goto stream_fopen_done;
105116
}
106117

107118
if (persistent) {
@@ -112,14 +123,10 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
112123
*opened_path = realpath;
113124
realpath = NULL;
114125
}
115-
if (realpath) {
116-
efree(realpath);
117-
}
118126
/* fall through */
119127

120128
case PHP_STREAM_PERSISTENT_FAILURE:
121-
efree(persistent_id);;
122-
return ret;
129+
goto stream_fopen_done;
123130
}
124131
}
125132

@@ -143,22 +150,22 @@ PHPAPI php_stream *_php_stream_fopen(const char *filename, const char *mode, cha
143150
*opened_path = realpath;
144151
realpath = NULL;
145152
}
146-
if (realpath) {
147-
efree(realpath);
148-
}
149-
if (persistent_id) {
150-
efree(persistent_id);
151-
}
152-
return ret;
153+
goto stream_fopen_done;
153154
}
154155
err:
155156
close(fd);
156157
}
157-
efree(realpath);
158+
stream_fopen_done:
159+
if (realpath) {
160+
efree(realpath);
161+
}
162+
if (url) {
163+
efree(url);
164+
}
158165
if (persistent_id) {
159166
efree(persistent_id);
160167
}
161-
return NULL;
168+
return ret;
162169
}
163170
/* }}} */
164171

@@ -959,6 +966,7 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
959966
{
960967
/* code ripped off from fopen_wrappers.c */
961968
char *pathbuf, *ptr, *end;
969+
php_url *url = NULL;
962970
char *exec_fname;
963971
char trypath[MAXPATHLEN];
964972
struct stat sb;
@@ -975,6 +983,11 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
975983
return NULL;
976984
}
977985

986+
if (!strncasecmp(filename, "file", 4)) {
987+
url = php_url_parse((char *)filename);
988+
filename = url->path;
989+
}
990+
978991
filename_length = strlen(filename);
979992

980993
/* Relative path open */
@@ -990,13 +1003,16 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
9901003

9911004

9921005
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
993-
return NULL;
1006+
stream = NULL;
1007+
goto stream_done;
9941008
}
9951009

9961010
if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
997-
return NULL;
1011+
stream = NULL;
1012+
goto stream_done;
9981013
}
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;
10001016
}
10011017

10021018
/*
@@ -1010,17 +1026,23 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
10101026
if (IS_ABSOLUTE_PATH(filename, filename_length)) {
10111027

10121028
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(filename TSRMLS_CC)) {
1013-
return NULL;
1029+
stream = NULL;
1030+
goto stream_done;
10141031
}
10151032

1016-
if ((php_check_safe_mode_include_dir(filename TSRMLS_CC)) == 0)
1033+
if ((php_check_safe_mode_include_dir(filename TSRMLS_CC)) == 0) {
10171034
/* 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+
}
10191038

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+
}
10221043

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;
10241046
}
10251047

10261048
#ifdef PHP_WIN32
@@ -1036,29 +1058,36 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
10361058
free(cwd);
10371059

10381060
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(trypath TSRMLS_CC)) {
1039-
return NULL;
1061+
stream = NULL;
1062+
goto stream_done;
10401063
}
10411064
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;
10431067
}
10441068
if (PG(safe_mode) && (!php_checkuid(trypath, mode, CHECKUID_CHECK_MODE_PARAM))) {
1045-
return NULL;
1069+
stream = NULL;
1070+
goto stream_done;
10461071
}
10471072

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;
10491075
}
10501076
#endif
10511077

10521078
if (!path || (path && !*path)) {
10531079

10541080
if (((options & STREAM_DISABLE_OPEN_BASEDIR) == 0) && php_check_open_basedir(path TSRMLS_CC)) {
1055-
return NULL;
1081+
stream = NULL;
1082+
goto stream_done;
10561083
}
10571084

10581085
if (PG(safe_mode) && (!php_checkuid(filename, mode, CHECKUID_CHECK_MODE_PARAM))) {
1059-
return NULL;
1086+
stream = NULL;
1087+
goto stream_done;
10601088
}
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;
10621091
}
10631092

10641093
/* check in provided path */
@@ -1117,12 +1146,18 @@ PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char
11171146
stream = php_stream_fopen_rel(trypath, mode, opened_path, options);
11181147
if (stream) {
11191148
stream_done:
1149+
if (url) {
1150+
efree(url);
1151+
}
11201152
efree(pathbuf);
11211153
return stream;
11221154
}
11231155
ptr = end;
11241156
} /* end provided path */
11251157

1158+
if (url) {
1159+
efree(url);
1160+
}
11261161
efree(pathbuf);
11271162
return NULL;
11281163

0 commit comments

Comments
 (0)