summaryrefslogtreecommitdiff
path: root/ext/imap/php_imap.c
diff options
context:
space:
mode:
authorMark A. Hershberger <mah@debian.(none)>2009-03-25 00:35:28 -0400
committerMark A. Hershberger <mah@debian.(none)>2009-03-25 00:35:28 -0400
commitba50031707469046407a35b77a3cd81351e951b3 (patch)
tree5c03e723bdbfabae09d41a3ab1253dff41eeed4a /ext/imap/php_imap.c
parent0a36161e13484a99ccf69bb38f206462d27cc6d6 (diff)
downloadphp-ba50031707469046407a35b77a3cd81351e951b3.tar.gz
Imported Upstream version 5.1.5upstream/5.1.5
Diffstat (limited to 'ext/imap/php_imap.c')
-rw-r--r--ext/imap/php_imap.c124
1 files changed, 123 insertions, 1 deletions
diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c
index 8c9841add..9b07924f6 100644
--- a/ext/imap/php_imap.c
+++ b/ext/imap/php_imap.c
@@ -26,7 +26,7 @@
| PHP 4.0 updates: Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
-/* $Id: php_imap.c,v 1.208.2.4 2006/01/05 00:47:16 iliaa Exp $ */
+/* $Id: php_imap.c,v 1.208.2.9 2006/08/11 15:07:13 iliaa Exp $ */
#define IMAP41
@@ -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"
@@ -67,6 +68,9 @@ static void _php_imap_add_body(zval *arg, BODY *body TSRMLS_DC);
static void _php_imap_parse_address(ADDRESS *addresslist, char **fulladdress, zval *paddress TSRMLS_DC);
static int _php_imap_address_size(ADDRESS *addresslist);
+/* the gets we use */
+static char *php_mail_gets(readfn_t f, void *stream, unsigned long size, GETS_DATA *md);
+
/* These function declarations are missing from the IMAP header files... */
void rfc822_date(char *date);
char *cpystr(const char *str);
@@ -93,6 +97,7 @@ zend_function_entry imap_functions[] = {
PHP_FE(imap_body, NULL)
PHP_FE(imap_bodystruct, NULL)
PHP_FE(imap_fetchbody, NULL)
+ PHP_FE(imap_savebody, NULL)
PHP_FE(imap_fetchheader, NULL)
PHP_FE(imap_fetchstructure, NULL)
PHP_FE(imap_expunge, NULL)
@@ -418,6 +423,7 @@ static void php_imap_init_globals(zend_imap_globals *imap_globals)
imap_globals->quota_return = NIL;
imap_globals->imap_acl_list = NIL;
#endif
+ imap_globals->gets_stream = NIL;
}
/* }}} */
@@ -460,6 +466,9 @@ PHP_MINIT_FUNCTION(imap)
/* lets allow NIL */
REGISTER_LONG_CONSTANT("NIL", NIL, CONST_PERSISTENT | CONST_CS);
+ /* plug in our gets */
+ mail_parameters(NIL, SET_GETS, (void *) php_mail_gets);
+
/* set default timeout values */
mail_parameters(NIL, SET_OPENTIMEOUT, (void *) FG(default_socket_timeout));
mail_parameters(NIL, SET_READTIMEOUT, (void *) FG(default_socket_timeout));
@@ -650,6 +659,7 @@ PHP_RINIT_FUNCTION(imap)
{
IMAPG(imap_errorstack) = NIL;
IMAPG(imap_alertstack) = NIL;
+ IMAPG(gets_stream) = NIL;
return SUCCESS;
}
/* }}} */
@@ -751,6 +761,13 @@ static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
efree(IMAPG(imap_password));
}
+ /* local filename, need to perform open_basedir and safe_mode checks */
+ if (Z_STRVAL_PP(mailbox)[0] != '{' &&
+ (php_check_open_basedir(Z_STRVAL_PP(mailbox) TSRMLS_CC) ||
+ (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(mailbox), NULL, CHECKUID_CHECK_FILE_AND_DIR)))) {
+ RETURN_FALSE;
+ }
+
IMAPG(imap_user) = estrndup(Z_STRVAL_PP(user), Z_STRLEN_PP(user));
IMAPG(imap_password) = estrndup(Z_STRVAL_PP(passwd), Z_STRLEN_PP(passwd));
@@ -807,6 +824,14 @@ PHP_FUNCTION(imap_reopen)
}
imap_le_struct->flags = cl_flags;
}
+
+ /* local filename, need to perform open_basedir and safe_mode checks */
+ if (Z_STRVAL_PP(mailbox)[0] != '{' &&
+ (php_check_open_basedir(Z_STRVAL_PP(mailbox) TSRMLS_CC) ||
+ (PG(safe_mode) && !php_checkuid(Z_STRVAL_PP(mailbox), NULL, CHECKUID_CHECK_FILE_AND_DIR)))) {
+ RETURN_FALSE;
+ }
+
imap_stream = mail_open(imap_le_struct->imap_stream, Z_STRVAL_PP(mailbox), flags);
if (imap_stream == NIL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't re-open stream");
@@ -1845,6 +1870,57 @@ PHP_FUNCTION(imap_fetchbody)
/* }}} */
+/* {{{ proto bool imap_savebody(resource stream_id, string|resource file, int msg_no[, string section = ""[, int options = 0]])
+ Save a specific body section to a file */
+PHP_FUNCTION(imap_savebody)
+{
+ 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, &section, &section_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) {
+ RETURN_FALSE;
+ }
+
+ IMAPG(gets_stream) = writer;
+ mail_fetchbody_full(imap_ptr->imap_stream, msgno, section, NULL, flags);
+ IMAPG(gets_stream) = NULL;
+
+ if (close_stream) {
+ php_stream_close(writer);
+ }
+
+ RETURN_TRUE;
+}
+/* }}} */
+
/* {{{ proto string imap_base64(string text)
Decode BASE64 encoded text */
PHP_FUNCTION(imap_base64)
@@ -4147,6 +4223,52 @@ PHP_FUNCTION(imap_timeout)
}
/* }}} */
+#define GETS_FETCH_SIZE 8196LU
+/* {{{ php_mail_gets */
+static char *php_mail_gets(readfn_t f, void *stream, unsigned long size, GETS_DATA *md)
+{
+ TSRMLS_FETCH();
+
+ /* write to the gets stream if it is set,
+ otherwise forward to c-clients gets */
+ if (IMAPG(gets_stream)) {
+ char buf[GETS_FETCH_SIZE];
+
+ while (size) {
+ unsigned long read;
+
+ if (size > GETS_FETCH_SIZE) {
+ read = GETS_FETCH_SIZE;
+ size -=GETS_FETCH_SIZE;
+ } else {
+ read = size;
+ size = 0;
+ }
+
+ if (!f(stream, read, buf)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read from socket");
+ break;
+ } else if (read != php_stream_write(IMAPG(gets_stream), buf, read)) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write to stream");
+ break;
+ }
+ }
+ return NULL;
+ } else {
+ char *buf = malloc(size + 1);
+
+ if (f(stream, size, buf)) {
+ buf[size] = '\0';
+ } else {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read from socket");
+ free(buf);
+ buf = NULL;
+ }
+ return buf;
+ }
+}
+/* }}} */
+
/* {{{ Interfaces to C-client
*/
void mm_searched(MAILSTREAM *stream, unsigned long number)