? imap_savebody.patch.txt Index: php_imap.c =================================================================== RCS file: /repository/php-src/ext/imap/php_imap.c,v retrieving revision 1.208.2.1 diff -u -p -d -r1.208.2.1 php_imap.c --- php_imap.c 30 Aug 2005 22:03:28 -0000 1.208.2.1 +++ php_imap.c 23 Sep 2005 05:52:48 -0000 @@ -36,6 +36,7 @@ #include "php.h" #include "php_ini.h" +#include "php_streams.h" #include "ext/standard/php_string.h" #include "ext/standard/info.h" #include "ext/standard/file.h" @@ -93,6 +94,9 @@ function_entry imap_functions[] = { PHP_FE(imap_body, NULL) PHP_FE(imap_bodystruct, NULL) PHP_FE(imap_fetchbody, NULL) +#ifndef ZTS + PHP_FE(imap_savebody, NULL) +#endif PHP_FE(imap_fetchheader, NULL) PHP_FE(imap_fetchstructure, NULL) PHP_FE(imap_expunge, NULL) @@ -1845,6 +1849,86 @@ PHP_FUNCTION(imap_fetchbody) /* }}} */ +#ifndef ZTS +/* {{{ php_mail_gets */ +static char *php_mail_gets(readfn_t f, void *stream, unsigned long size, GETS_DATA *md) +{ +#define GETS_FETCH_SIZE 8196 + unsigned long i, rest = size % GETS_FETCH_SIZE; + char *buf = emalloc(GETS_FETCH_SIZE+1); + + for (i = GETS_FETCH_SIZE; i < size; i += GETS_FETCH_SIZE) { + memset(buf, 0, GETS_FETCH_SIZE+1); + f(stream, GETS_FETCH_SIZE, buf); + php_stream_write_string(IMAPG(gets), buf); + } + if (rest) { + memset(buf, 0, GETS_FETCH_SIZE+1); + f(stream, rest, buf); + php_stream_write_string(IMAPG(gets), buf); + } + efree(buf); + + return NULL; +} +/* }}} */ + +/* {{{ proto bool imap_savebody(resource stream_id, string|resource file, int msg_no, string section[, int options]) + Save a specific body section to a file */ +PHP_FUNCTION(imap_savebody) +{ + void *old_gets; + zval *stream, *out; + pils *imap_ptr = NULL; + php_stream *writer = NULL; + char *section = ""; + int section_len = 0, close_stream = 1; + long msgno, flags = 0; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rzl|sl", &stream, &out, &msgno, §ion, §ion_len, &flags)) { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE(imap_ptr, pils *, &stream, -1, "imap", le_imap); + if (!imap_ptr) { + RETURN_FALSE; + } + + switch (Z_TYPE_P(out)) + { + case IS_LONG: + case IS_RESOURCE: + close_stream = 0; + php_stream_from_zval(writer, &out); + break; + + default: + convert_to_string_ex(&out); + writer = php_stream_open_wrapper(Z_STRVAL_P(out), "wb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); + break; + } + + if (writer) { + IMAPG(gets) = writer; + } else { + RETURN_FALSE; + } + + old_gets = mail_parameters(imap_ptr->imap_stream, GET_GETS, NULL); + mail_parameters(imap_ptr->imap_stream, SET_GETS, php_mail_gets); + mail_fetchbody_full(imap_ptr->imap_stream, msgno, section, NULL, flags); + mail_parameters(imap_ptr->imap_stream, SET_GETS, old_gets); + + if (close_stream) { + php_stream_close(writer); + } + IMAPG(gets) = NULL; + + RETURN_TRUE; +} +/* }}} */ +#endif /* ZTS */ + /* {{{ proto string imap_base64(string text) Decode BASE64 encoded text */ PHP_FUNCTION(imap_base64) Index: php_imap.h =================================================================== RCS file: /repository/php-src/ext/imap/php_imap.h,v retrieving revision 1.32 diff -u -p -d -r1.32 php_imap.h --- php_imap.h 3 Aug 2005 14:07:17 -0000 1.32 +++ php_imap.h 23 Sep 2005 05:52:48 -0000 @@ -114,6 +114,9 @@ PHP_FUNCTION(imap_rfc822_parse_headers); PHP_FUNCTION(imap_body); PHP_FUNCTION(imap_fetchstructure); PHP_FUNCTION(imap_fetchbody); +#ifndef ZTS +PHP_FUNCTION(imap_savebody); +#endif PHP_FUNCTION(imap_expunge); PHP_FUNCTION(imap_delete); PHP_FUNCTION(imap_undelete); @@ -205,6 +208,7 @@ ZEND_BEGIN_MODULE_GLOBALS(imap) zval **quota_return; zval *imap_acl_list; #endif + php_stream *gets; ZEND_END_MODULE_GLOBALS(imap) #ifdef ZTS