diff options
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bz2/bz2.c | 4 | ||||
-rw-r--r-- | ext/curl/streams.c | 4 | ||||
-rw-r--r-- | ext/standard/file.c | 85 | ||||
-rw-r--r-- | ext/standard/ftp_fopen_wrapper.c | 148 | ||||
-rw-r--r-- | ext/standard/http_fopen_wrapper.c | 2 | ||||
-rw-r--r-- | ext/standard/php_fopen_wrapper.c | 4 | ||||
-rw-r--r-- | ext/zlib/zlib_fopen_wrapper.c | 4 |
7 files changed, 178 insertions, 73 deletions
diff --git a/ext/bz2/bz2.c b/ext/bz2/bz2.c index 0e4620e06a..f28f77c714 100644 --- a/ext/bz2/bz2.c +++ b/ext/bz2/bz2.c @@ -227,7 +227,9 @@ static php_stream_wrapper_ops bzip2_stream_wops = { NULL, /* opendir */ "BZip2", NULL, /* unlink */ - NULL /* rename */ + NULL, /* rename */ + NULL, /* mkdir */ + NULL /* rmdir */ }; php_stream_wrapper php_stream_bzip2_wrapper = { diff --git a/ext/curl/streams.c b/ext/curl/streams.c index 5468468806..188394b6a8 100644 --- a/ext/curl/streams.c +++ b/ext/curl/streams.c @@ -363,7 +363,9 @@ static php_stream_wrapper_ops php_curl_wrapper_ops = { NULL, /* opendir */ NULL, /* label */ NULL, /* unlink */ - NULL /* rename */ + NULL, /* rename */ + NULL, /* mkdir */ + NULL /* rmdir */ }; php_stream_wrapper php_curl_wrapper = { diff --git a/ext/standard/file.c b/ext/standard/file.c index 617a64ed5c..25a2a81333 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -1273,93 +1273,42 @@ PHPAPI int php_mkdir(char *dir, long mode TSRMLS_DC) } /* }}} */ -/* {{{ proto bool mkdir(string pathname [, int mode [, bool recursive]) +/* {{{ proto bool mkdir(string pathname [, int mode [, bool recursive [, resource context]]]) Create a directory */ PHP_FUNCTION(mkdir) { - int dir_len, ret; - long mode = 0777; - char *dir; + zval *zcontext = NULL; + long dir_len, mode = 0777; zend_bool recursive = 0; + char *dir; + php_stream_context *context; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lb", &dir, &dir_len, &mode, &recursive) == FAILURE) { - return; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lbr", &dir, &dir_len, &mode, &recursive, &zcontext) == FAILURE) { + RETURN_FALSE; } - if (!recursive) { - ret = php_mkdir(dir, mode TSRMLS_CC); - } else { - /* we look for directory separator from the end of string, thus hopefuly reducing our work load */ - char *p, *e, *buf; - struct stat sb; - - buf = estrndup(dir, dir_len); - e = buf + dir_len; - - /* find a top level directory we need to create */ - while ((p = strrchr(buf, DEFAULT_SLASH))) { - *p = '\0'; - if (VCWD_STAT(buf, &sb) == 0) { - *p = DEFAULT_SLASH; - break; - } - } - if (p == buf) { - ret = php_mkdir(dir, mode TSRMLS_CC); - } else if (!(ret = php_mkdir(buf, mode TSRMLS_CC))) { - if (!p) { - p = buf; - } - /* create any needed directories if the creation of the 1st directory worked */ - while (++p != e) { - if (*p == '\0' && *(p + 1) != '\0') { - *p = DEFAULT_SLASH; - if ((ret = VCWD_MKDIR(buf, (mode_t)mode)) < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); - break; - } - } - } - } - efree(buf); - } + context = php_stream_context_from_zval(zcontext, 0); - if (ret < 0) { - RETURN_FALSE; - } else { - RETURN_TRUE; - } + RETURN_BOOL(php_stream_mkdir(dir, mode, (recursive ? PHP_STREAM_MKDIR_RECURSIVE : 0) | REPORT_ERRORS, context)); } /* }}} */ -/* {{{ proto bool rmdir(string dirname) +/* {{{ proto bool rmdir(string dirname[, resource context]) Remove a directory */ PHP_FUNCTION(rmdir) { - zval **arg1; - int ret; - - if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg1) == FAILURE) { - WRONG_PARAM_COUNT; - } - - convert_to_string_ex(arg1); + char *dir; + long dir_len; + zval *zcontext = NULL; + php_stream_context *context; - if (PG(safe_mode) &&(!php_checkuid(Z_STRVAL_PP(arg1), NULL, CHECKUID_CHECK_FILE_AND_DIR))) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &dir, &dir_len, &zcontext) == FAILURE) { RETURN_FALSE; } - if (php_check_open_basedir(Z_STRVAL_PP(arg1) TSRMLS_CC)) { - RETURN_FALSE; - } - - ret = VCWD_RMDIR(Z_STRVAL_PP(arg1)); - if (ret < 0) { - php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_PP(arg1), E_WARNING, "%s", strerror(errno)); - RETURN_FALSE; - } + context = php_stream_context_from_zval(zcontext, 0); - RETURN_TRUE; + RETURN_BOOL(php_stream_rmdir(dir, REPORT_ERRORS, context)); } /* }}} */ diff --git a/ext/standard/ftp_fopen_wrapper.c b/ext/standard/ftp_fopen_wrapper.c index ca7e086f41..80cc620965 100644 --- a/ext/standard/ftp_fopen_wrapper.c +++ b/ext/standard/ftp_fopen_wrapper.c @@ -925,6 +925,150 @@ static int php_stream_ftp_rename(php_stream_wrapper *wrapper, char *url_from, ch } /* }}} */ +/* {{{ php_stream_ftp_mkdir + */ +static int php_stream_ftp_mkdir(php_stream_wrapper *wrapper, char *url, int mode, int options, php_stream_context *context TSRMLS_DC) +{ + php_stream *stream = NULL; + php_url *resource = NULL; + int result, recursive = options & PHP_STREAM_MKDIR_RECURSIVE; + char tmp_line[512]; + + stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, NULL, NULL, &resource, NULL, NULL TSRMLS_CC); + if (!stream) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to %s", url); + } + goto mkdir_errexit; + } + + if (resource->path == NULL) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path provided in %s", url); + } + goto mkdir_errexit; + } + + if (!recursive) { + php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", resource->path); + result = GET_FTP_RESULT(stream); + } else { + /* we look for directory separator from the end of string, thus hopefuly reducing our work load */ + char *p, *e, *buf; + + buf = estrdup(resource->path); + e = buf + strlen(buf); + + /* find a top level directory we need to create */ + while ((p = strrchr(buf, '/'))) { + *p = '\0'; + php_stream_printf(stream TSRMLS_CC, "CWD %s\r\n", buf); + result = GET_FTP_RESULT(stream); + if (result >= 200 && result <= 299) { + *p = '/'; + break; + } + } + if (p == buf) { + php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", resource->path); + result = GET_FTP_RESULT(stream); + } else { + php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", buf); + result = GET_FTP_RESULT(stream); + if (result >= 200 && result <= 299) { + if (!p) { + p = buf; + } + /* create any needed directories if the creation of the 1st directory worked */ + while (++p != e) { + if (*p == '\0' && *(p + 1) != '\0') { + *p = '/'; + php_stream_printf(stream TSRMLS_CC, "MKD %s\r\n", buf); + result = GET_FTP_RESULT(stream); + if (result < 200 || result > 299) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tmp_line); + } + break; + } + } + } + } + } + efree(buf); + } + + php_url_free(resource); + php_stream_close(stream); + + if (result < 200 || result > 299) { + /* Failure */ + return 0; + } + + return 1; + + mkdir_errexit: + if (resource) { + php_url_free(resource); + } + if (stream) { + php_stream_close(stream); + } + return 0; +} +/* }}} */ + +/* {{{ php_stream_ftp_rmdir + */ +static int php_stream_ftp_rmdir(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC) +{ + php_stream *stream = NULL; + php_url *resource = NULL; + int result; + char tmp_line[512]; + + stream = php_ftp_fopen_connect(wrapper, url, "r", 0, NULL, NULL, NULL, &resource, NULL, NULL TSRMLS_CC); + if (!stream) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to connect to %s", url); + } + goto rmdir_errexit; + } + + if (resource->path == NULL) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid path provided in %s", url); + } + goto rmdir_errexit; + } + + php_stream_printf(stream TSRMLS_CC, "RMD %s\r\n", resource->path); + result = GET_FTP_RESULT(stream); + + if (result < 200 || result > 299) { + if (options & REPORT_ERRORS) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tmp_line); + } + goto rmdir_errexit; + } + + php_url_free(resource); + php_stream_close(stream); + + return 1; + + rmdir_errexit: + if (resource) { + php_url_free(resource); + } + if (stream) { + php_stream_close(stream); + } + return 0; +} +/* }}} */ + static php_stream_wrapper_ops ftp_stream_wops = { php_stream_url_wrap_ftp, php_stream_ftp_stream_close, /* stream_close */ @@ -933,7 +1077,9 @@ static php_stream_wrapper_ops ftp_stream_wops = { php_stream_ftp_opendir, /* opendir */ "FTP", php_stream_ftp_unlink, /* unlink */ - php_stream_ftp_rename /* rename */ + php_stream_ftp_rename, /* rename */ + php_stream_ftp_mkdir, /* mkdir */ + php_stream_ftp_rmdir /* rmdir */ }; PHPAPI php_stream_wrapper php_stream_ftp_wrapper = { diff --git a/ext/standard/http_fopen_wrapper.c b/ext/standard/http_fopen_wrapper.c index 28c78fd0e9..b56ef670bd 100644 --- a/ext/standard/http_fopen_wrapper.c +++ b/ext/standard/http_fopen_wrapper.c @@ -544,6 +544,8 @@ static php_stream_wrapper_ops http_stream_wops = { "HTTP", NULL, /* unlink */ NULL, /* rename */ + NULL, /* mkdir */ + NULL /* rmdir */ }; PHPAPI php_stream_wrapper php_stream_http_wrapper = { diff --git a/ext/standard/php_fopen_wrapper.c b/ext/standard/php_fopen_wrapper.c index 9dc6eb1384..7f34847a05 100644 --- a/ext/standard/php_fopen_wrapper.c +++ b/ext/standard/php_fopen_wrapper.c @@ -234,7 +234,9 @@ static php_stream_wrapper_ops php_stdio_wops = { NULL, /* opendir */ "PHP", NULL, /* unlink */ - NULL /* rename */ + NULL, /* rename */ + NULL, /* mkdir */ + NULL /* rmdir */ }; php_stream_wrapper php_stream_php_wrapper = { diff --git a/ext/zlib/zlib_fopen_wrapper.c b/ext/zlib/zlib_fopen_wrapper.c index e5358e97e6..fa260feb92 100644 --- a/ext/zlib/zlib_fopen_wrapper.c +++ b/ext/zlib/zlib_fopen_wrapper.c @@ -161,7 +161,9 @@ static php_stream_wrapper_ops gzip_stream_wops = { NULL, /* opendir */ "ZLIB", NULL, /* unlink */ - NULL /* rename */ + NULL, /* rename */ + NULL, /* mkdir */ + NULL /* rmdir */ }; php_stream_wrapper php_stream_gzip_wrapper = { |