mike            Fri Jan 27 19:44:40 2006 UTC

  Modified files:              
    /php-src/ext/imap   php_imap.c php_imap.h 
  Log:
  - add imap_savebody()
  
http://cvs.php.net/viewcvs.cgi/php-src/ext/imap/php_imap.c?r1=1.212&r2=1.213&diff_format=u
Index: php-src/ext/imap/php_imap.c
diff -u php-src/ext/imap/php_imap.c:1.212 php-src/ext/imap/php_imap.c:1.213
--- php-src/ext/imap/php_imap.c:1.212   Thu Jan  5 00:51:34 2006
+++ php-src/ext/imap/php_imap.c Fri Jan 27 19:44:40 2006
@@ -26,7 +26,7 @@
    | PHP 4.0 updates:  Zeev Suraski <[EMAIL PROTECTED]>                       |
    +----------------------------------------------------------------------+
  */
-/* $Id: php_imap.c,v 1.212 2006/01/05 00:51:34 iliaa Exp $ */
+/* $Id: php_imap.c,v 1.213 2006/01/27 19:44:40 mike 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,11 @@
 static void _php_imap_parse_address(ADDRESS *addresslist, char **fulladdress, 
zval *paddress TSRMLS_DC);
 static int _php_imap_address_size(ADDRESS *addresslist);
 
+/* c-clients gets */
+static mailgets_t old_mail_gets;
+/* 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 +99,7 @@
        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 +425,7 @@
        imap_globals->quota_return = NIL;
        imap_globals->imap_acl_list = NIL;
 #endif
+       imap_globals->gets_stream = NIL;
 }
 /* }}} */
 
@@ -460,6 +468,10 @@
        /* lets allow NIL */
        REGISTER_LONG_CONSTANT("NIL", NIL, CONST_PERSISTENT | CONST_CS);
 
+       /* plug in our gets */
+       old_mail_gets = mail_parameters(NIL, GET_GETS, NIL);
+       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 +662,7 @@
 {
        IMAPG(imap_errorstack) = NIL;
        IMAPG(imap_alertstack) = NIL;
+       IMAPG(gets_stream) = NIL;
        return SUCCESS;
 }
 /* }}} */
@@ -1845,6 +1858,57 @@
 
 /* }}} */
 
+/* {{{ 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 +4211,43 @@
 }
 /* }}} */
 
+#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 {
+               return old_mail_gets(f, stream, size, md);
+       }
+}
+/* }}} */
+
 /* {{{ Interfaces to C-client 
  */
 void mm_searched(MAILSTREAM *stream, unsigned long number)
http://cvs.php.net/viewcvs.cgi/php-src/ext/imap/php_imap.h?r1=1.33&r2=1.34&diff_format=u
Index: php-src/ext/imap/php_imap.h
diff -u php-src/ext/imap/php_imap.h:1.33 php-src/ext/imap/php_imap.h:1.34
--- php-src/ext/imap/php_imap.h:1.33    Sun Jan  1 13:09:50 2006
+++ php-src/ext/imap/php_imap.h Fri Jan 27 19:44:40 2006
@@ -27,7 +27,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: php_imap.h,v 1.33 2006/01/01 13:09:50 sniper Exp $ */
+/* $Id: php_imap.h,v 1.34 2006/01/27 19:44:40 mike Exp $ */
 
 #ifndef PHP_IMAP_H
 #define PHP_IMAP_H
@@ -114,6 +114,7 @@
 PHP_FUNCTION(imap_body);
 PHP_FUNCTION(imap_fetchstructure);
 PHP_FUNCTION(imap_fetchbody);
+PHP_FUNCTION(imap_savebody);
 PHP_FUNCTION(imap_expunge);
 PHP_FUNCTION(imap_delete);
 PHP_FUNCTION(imap_undelete);
@@ -205,6 +206,8 @@
        zval **quota_return;
        zval *imap_acl_list;
 #endif
+       /* php_stream for php_mail_gets() */
+       php_stream *gets_stream;
 ZEND_END_MODULE_GLOBALS(imap)
 
 #ifdef ZTS

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to