/ext/imap/php_imap.c
C | 4653 lines | 3445 code | 639 blank | 569 comment | 984 complexity | fb2db4dbfd5ce1da0f43734fd7cba14b MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | Copyright (c) The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Rex Logan <veebert@dimensional.com> |
- | Mark Musone <musone@afterfive.com> |
- | Brian Wang <brian@vividnet.com> |
- | Kaj-Michael Lang <milang@tal.org> |
- | Antoni Pamies Olive <toni@readysoft.net> |
- | Rasmus Lerdorf <rasmus@php.net> |
- | Chuck Hagenbuch <chuck@horde.org> |
- | Andrew Skalski <askalski@chekinc.com> |
- | Hartmut Holzgraefe <hholzgra@php.net> |
- | Jani Taskinen <jani.taskinen@iki.fi> |
- | Daniel R. Kalowsky <kalowsky@php.net> |
- | PHP 4.0 updates: Zeev Suraski <zeev@php.net> |
- +----------------------------------------------------------------------+
- */
- #define IMAP41
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #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"
- #include "zend_smart_str.h"
- #include "ext/pcre/php_pcre.h"
- #ifdef ERROR
- #undef ERROR
- #endif
- #include "php_imap.h"
- #include "php_imap_arginfo.h"
- #include <time.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <signal.h>
- #ifdef PHP_WIN32
- #include <winsock2.h>
- #include <stdlib.h>
- #include "win32/sendmail.h"
- MAILSTREAM DEFAULTPROTO;
- #endif
- #define CRLF "\015\012"
- #define CRLF_LEN sizeof("\015\012") - 1
- #define PHP_EXPUNGE 32768
- #define PHP_IMAP_ADDRESS_SIZE_BUF 10
- #ifndef SENDBUFLEN
- #define SENDBUFLEN 16385
- #endif
- #if defined(__GNUC__) && __GNUC__ >= 4
- # define PHP_IMAP_EXPORT __attribute__ ((visibility("default")))
- #else
- # define PHP_IMAP_EXPORT
- #endif
- static void _php_make_header_object(zval *myzvalue, ENVELOPE *en);
- static void _php_imap_add_body(zval *arg, BODY *body);
- static zend_string* _php_imap_parse_address(ADDRESS *addresslist, zval *paddress);
- static zend_string* _php_rfc822_write_address(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);
- char *cpytxt(SIZEDTEXT *dst, char *text, unsigned long size);
- #ifndef HAVE_NEW_MIME2TEXT
- long utf8_mime2text(SIZEDTEXT *src, SIZEDTEXT *dst);
- #else
- long utf8_mime2text (SIZEDTEXT *src, SIZEDTEXT *dst, long flags);
- #endif
- unsigned long find_rightmost_bit(unsigned long *valptr);
- void fs_give(void **block);
- void *fs_get(size_t size);
- ZEND_DECLARE_MODULE_GLOBALS(imap)
- static PHP_GINIT_FUNCTION(imap);
- /* {{{ imap dependencies */
- static const zend_module_dep imap_deps[] = {
- ZEND_MOD_REQUIRED("standard")
- ZEND_MOD_END
- };
- /* }}} */
- /* {{{ PHP_INI
- */
- PHP_INI_BEGIN()
- STD_PHP_INI_BOOLEAN("imap.enable_insecure_rsh", "0", PHP_INI_SYSTEM, OnUpdateBool, enable_rsh, zend_imap_globals, imap_globals)
- PHP_INI_END()
- /* }}} */
- /* {{{ imap_module_entry
- */
- zend_module_entry imap_module_entry = {
- STANDARD_MODULE_HEADER_EX, NULL,
- imap_deps,
- "imap",
- ext_functions,
- PHP_MINIT(imap),
- NULL,
- PHP_RINIT(imap),
- PHP_RSHUTDOWN(imap),
- PHP_MINFO(imap),
- PHP_IMAP_VERSION,
- PHP_MODULE_GLOBALS(imap),
- PHP_GINIT(imap),
- NULL,
- NULL,
- STANDARD_MODULE_PROPERTIES_EX
- };
- /* }}} */
- #ifdef COMPILE_DL_IMAP
- #ifdef ZTS
- ZEND_TSRMLS_CACHE_DEFINE()
- #endif
- ZEND_GET_MODULE(imap)
- #endif
- /* True globals, no need for thread safety */
- static int le_imap;
- #define PHP_IMAP_CHECK_MSGNO(msgindex) \
- if ((msgindex < 1) || ((unsigned) msgindex > imap_le_struct->imap_stream->nmsgs)) { \
- php_error_docref(NULL, E_WARNING, "Bad message number"); \
- RETURN_FALSE; \
- } \
- /* {{{ mail_close_it
- */
- static void mail_close_it(zend_resource *rsrc)
- {
- pils *imap_le_struct = (pils *)rsrc->ptr;
- /* Do not try to close prototype streams */
- if (!(imap_le_struct->flags & OP_PROTOTYPE)) {
- mail_close_full(imap_le_struct->imap_stream, imap_le_struct->flags);
- }
- if (IMAPG(imap_user)) {
- efree(IMAPG(imap_user));
- IMAPG(imap_user) = 0;
- }
- if (IMAPG(imap_password)) {
- efree(IMAPG(imap_password));
- IMAPG(imap_password) = 0;
- }
- efree(imap_le_struct);
- }
- /* }}} */
- /* {{{ add_assoc_object
- */
- static zval *add_assoc_object(zval *arg, char *key, zval *tmp)
- {
- HashTable *symtable;
- if (Z_TYPE_P(arg) == IS_OBJECT) {
- symtable = Z_OBJPROP_P(arg);
- } else {
- symtable = Z_ARRVAL_P(arg);
- }
- return zend_hash_str_update(symtable, key, strlen(key), tmp);
- }
- /* }}} */
- /* {{{ add_next_index_object
- */
- static inline zval *add_next_index_object(zval *arg, zval *tmp)
- {
- HashTable *symtable;
- if (Z_TYPE_P(arg) == IS_OBJECT) {
- symtable = Z_OBJPROP_P(arg);
- } else {
- symtable = Z_ARRVAL_P(arg);
- }
- return zend_hash_next_index_insert(symtable, tmp);
- }
- /* }}} */
- /* {{{ mail_newfolderobjectlist
- *
- * Mail instantiate FOBJECTLIST
- * Returns: new FOBJECTLIST list
- * Author: CJH
- */
- FOBJECTLIST *mail_newfolderobjectlist(void)
- {
- return (FOBJECTLIST *) memset(fs_get(sizeof(FOBJECTLIST)), 0, sizeof(FOBJECTLIST));
- }
- /* }}} */
- /* {{{ mail_free_foblist
- *
- * Mail garbage collect FOBJECTLIST
- * Accepts: pointer to FOBJECTLIST pointer
- * Author: CJH
- */
- void mail_free_foblist(FOBJECTLIST **foblist, FOBJECTLIST **tail)
- {
- FOBJECTLIST *cur, *next;
- for (cur=*foblist, next=cur->next; cur; cur=next) {
- next = cur->next;
- if(cur->text.data)
- fs_give((void **)&(cur->text.data));
- fs_give((void **)&cur);
- }
- *tail = NIL;
- *foblist = NIL;
- }
- /* }}} */
- /* {{{ mail_newerrorlist
- *
- * Mail instantiate ERRORLIST
- * Returns: new ERRORLIST list
- * Author: CJH
- */
- ERRORLIST *mail_newerrorlist(void)
- {
- return (ERRORLIST *) memset(fs_get(sizeof(ERRORLIST)), 0, sizeof(ERRORLIST));
- }
- /* }}} */
- /* {{{ mail_free_errorlist
- *
- * Mail garbage collect FOBJECTLIST
- * Accepts: pointer to FOBJECTLIST pointer
- * Author: CJH
- */
- void mail_free_errorlist(ERRORLIST **errlist)
- {
- if (*errlist) { /* only free if exists */
- if ((*errlist)->text.data) {
- fs_give((void **) &(*errlist)->text.data);
- }
- mail_free_errorlist (&(*errlist)->next);
- fs_give((void **) errlist); /* return string to free storage */
- }
- }
- /* }}} */
- /* {{{ mail_newmessagelist
- *
- * Mail instantiate MESSAGELIST
- * Returns: new MESSAGELIST list
- * Author: CJH
- */
- MESSAGELIST *mail_newmessagelist(void)
- {
- return (MESSAGELIST *) memset(fs_get(sizeof(MESSAGELIST)), 0, sizeof(MESSAGELIST));
- }
- /* }}} */
- /* {{{ mail_free_messagelist
- *
- * Mail garbage collect MESSAGELIST
- * Accepts: pointer to MESSAGELIST pointer
- * Author: CJH
- */
- void mail_free_messagelist(MESSAGELIST **msglist, MESSAGELIST **tail)
- {
- MESSAGELIST *cur, *next;
- for (cur = *msglist, next = cur->next; cur; cur = next) {
- next = cur->next;
- fs_give((void **)&cur);
- }
- *tail = NIL;
- *msglist = NIL;
- }
- /* }}} */
- #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
- /* {{{ mail_getquota
- *
- * Mail GET_QUOTA callback
- * Called via the mail_parameter function in c-client:src/c-client/mail.c
- * Author DRK
- */
- void mail_getquota(MAILSTREAM *stream, char *qroot, QUOTALIST *qlist)
- {
- zval t_map, *return_value;
- return_value = *IMAPG(quota_return);
- /* put parsing code here */
- for(; qlist; qlist = qlist->next) {
- array_init(&t_map);
- if (strncmp(qlist->name, "STORAGE", 7) == 0)
- {
- /* this is to add backwards compatibility */
- add_assoc_long_ex(return_value, "usage", sizeof("usage") - 1, qlist->usage);
- add_assoc_long_ex(return_value, "limit", sizeof("limit") - 1, qlist->limit);
- }
- add_assoc_long_ex(&t_map, "usage", sizeof("usage") - 1, qlist->usage);
- add_assoc_long_ex(&t_map, "limit", sizeof("limit") - 1, qlist->limit);
- add_assoc_zval_ex(return_value, qlist->name, strlen(qlist->name), &t_map);
- }
- }
- /* }}} */
- /* {{{ mail_getquota
- *
- * Mail GET_ACL callback
- * Called via the mail_parameter function in c-client:src/c-client/mail.c
- */
- void mail_getacl(MAILSTREAM *stream, char *mailbox, ACLLIST *alist)
- {
- /* walk through the ACLLIST */
- for(; alist; alist = alist->next) {
- add_assoc_stringl(IMAPG(imap_acl_list), alist->identifier, alist->rights, strlen(alist->rights));
- }
- }
- /* }}} */
- #endif
- /* {{{ PHP_GINIT_FUNCTION
- */
- static PHP_GINIT_FUNCTION(imap)
- {
- #if defined(COMPILE_DL_IMAP) && defined(ZTS)
- ZEND_TSRMLS_CACHE_UPDATE();
- #endif
- imap_globals->imap_user = NIL;
- imap_globals->imap_password = NIL;
- imap_globals->imap_alertstack = NIL;
- imap_globals->imap_errorstack = NIL;
- imap_globals->imap_folders = NIL;
- imap_globals->imap_folders_tail = NIL;
- imap_globals->imap_sfolders = NIL;
- imap_globals->imap_sfolders_tail = NIL;
- imap_globals->imap_messages = NIL;
- imap_globals->imap_messages_tail = NIL;
- imap_globals->imap_folder_objects = NIL;
- imap_globals->imap_folder_objects_tail = NIL;
- imap_globals->imap_sfolder_objects = NIL;
- imap_globals->imap_sfolder_objects_tail = NIL;
- imap_globals->folderlist_style = FLIST_ARRAY;
- #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
- imap_globals->quota_return = NIL;
- imap_globals->imap_acl_list = NIL;
- #endif
- imap_globals->gets_stream = NIL;
- }
- /* }}} */
- /* {{{ PHP_MINIT_FUNCTION
- */
- PHP_MINIT_FUNCTION(imap)
- {
- unsigned long sa_all = SA_MESSAGES | SA_RECENT | SA_UNSEEN | SA_UIDNEXT | SA_UIDVALIDITY;
- REGISTER_INI_ENTRIES();
- #ifndef PHP_WIN32
- mail_link(&unixdriver); /* link in the unix driver */
- mail_link(&mhdriver); /* link in the mh driver */
- /* mail_link(&mxdriver); */ /* According to c-client docs (internal.txt) this shouldn't be used. */
- mail_link(&mmdfdriver); /* link in the mmdf driver */
- mail_link(&newsdriver); /* link in the news driver */
- mail_link(&philedriver); /* link in the phile driver */
- #endif
- mail_link(&imapdriver); /* link in the imap driver */
- mail_link(&nntpdriver); /* link in the nntp driver */
- mail_link(&pop3driver); /* link in the pop3 driver */
- mail_link(&mbxdriver); /* link in the mbx driver */
- mail_link(&tenexdriver); /* link in the tenex driver */
- mail_link(&mtxdriver); /* link in the mtx driver */
- mail_link(&dummydriver); /* link in the dummy driver */
- #ifndef PHP_WIN32
- auth_link(&auth_log); /* link in the log authenticator */
- auth_link(&auth_md5); /* link in the cram-md5 authenticator */
- #if HAVE_IMAP_KRB && defined(HAVE_IMAP_AUTH_GSS)
- auth_link(&auth_gss); /* link in the gss authenticator */
- #endif
- auth_link(&auth_pla); /* link in the plain authenticator */
- #endif
- #ifdef HAVE_IMAP_SSL
- ssl_onceonlyinit ();
- #endif
- /* lets allow NIL */
- REGISTER_LONG_CONSTANT("NIL", NIL, CONST_PERSISTENT | CONST_CS);
- /* plug in our gets */
- mail_parameters(NIL, SET_GETS, (void *) NIL);
- /* set default timeout values */
- mail_parameters(NIL, SET_OPENTIMEOUT, (void *) FG(default_socket_timeout));
- mail_parameters(NIL, SET_READTIMEOUT, (void *) FG(default_socket_timeout));
- mail_parameters(NIL, SET_WRITETIMEOUT, (void *) FG(default_socket_timeout));
- mail_parameters(NIL, SET_CLOSETIMEOUT, (void *) FG(default_socket_timeout));
- /* timeout constants */
- REGISTER_LONG_CONSTANT("IMAP_OPENTIMEOUT", 1, CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("IMAP_READTIMEOUT", 2, CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("IMAP_WRITETIMEOUT", 3, CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("IMAP_CLOSETIMEOUT", 4, CONST_PERSISTENT | CONST_CS);
- /* Open Options */
- REGISTER_LONG_CONSTANT("OP_DEBUG", OP_DEBUG, CONST_PERSISTENT | CONST_CS);
- /* debug protocol negotiations */
- REGISTER_LONG_CONSTANT("OP_READONLY", OP_READONLY, CONST_PERSISTENT | CONST_CS);
- /* read-only open */
- REGISTER_LONG_CONSTANT("OP_ANONYMOUS", OP_ANONYMOUS, CONST_PERSISTENT | CONST_CS);
- /* anonymous open of newsgroup */
- REGISTER_LONG_CONSTANT("OP_SHORTCACHE", OP_SHORTCACHE, CONST_PERSISTENT | CONST_CS);
- /* short (elt-only) caching */
- REGISTER_LONG_CONSTANT("OP_SILENT", OP_SILENT, CONST_PERSISTENT | CONST_CS);
- /* don't pass up events (internal use) */
- REGISTER_LONG_CONSTANT("OP_PROTOTYPE", OP_PROTOTYPE, CONST_PERSISTENT | CONST_CS);
- /* return driver prototype */
- REGISTER_LONG_CONSTANT("OP_HALFOPEN", OP_HALFOPEN, CONST_PERSISTENT | CONST_CS);
- /* half-open (IMAP connect but no select) */
- REGISTER_LONG_CONSTANT("OP_EXPUNGE", OP_EXPUNGE, CONST_PERSISTENT | CONST_CS);
- /* silently expunge recycle stream */
- REGISTER_LONG_CONSTANT("OP_SECURE", OP_SECURE, CONST_PERSISTENT | CONST_CS);
- /* don't do non-secure authentication */
- /*
- PHP re-assigns CL_EXPUNGE a custom value that can be used as part of the imap_open() bitfield
- because it seems like a good idea to be able to indicate that the mailbox should be
- automatically expunged during imap_open in case the script get interrupted and it doesn't get
- to the imap_close() where this option is normally placed. If the c-client library adds other
- options and the value for this one conflicts, simply make PHP_EXPUNGE higher at the top of
- this file
- */
- REGISTER_LONG_CONSTANT("CL_EXPUNGE", PHP_EXPUNGE, CONST_PERSISTENT | CONST_CS);
- /* expunge silently */
- /* Fetch options */
- REGISTER_LONG_CONSTANT("FT_UID", FT_UID, CONST_PERSISTENT | CONST_CS);
- /* argument is a UID */
- REGISTER_LONG_CONSTANT("FT_PEEK", FT_PEEK, CONST_PERSISTENT | CONST_CS);
- /* peek at data */
- REGISTER_LONG_CONSTANT("FT_NOT", FT_NOT, CONST_PERSISTENT | CONST_CS);
- /* NOT flag for header lines fetch */
- REGISTER_LONG_CONSTANT("FT_INTERNAL", FT_INTERNAL, CONST_PERSISTENT | CONST_CS);
- /* text can be internal strings */
- REGISTER_LONG_CONSTANT("FT_PREFETCHTEXT", FT_PREFETCHTEXT, CONST_PERSISTENT | CONST_CS);
- /* IMAP prefetch text when fetching header */
- /* Flagging options */
- REGISTER_LONG_CONSTANT("ST_UID", ST_UID, CONST_PERSISTENT | CONST_CS);
- /* argument is a UID sequence */
- REGISTER_LONG_CONSTANT("ST_SILENT", ST_SILENT, CONST_PERSISTENT | CONST_CS);
- /* don't return results */
- REGISTER_LONG_CONSTANT("ST_SET", ST_SET, CONST_PERSISTENT | CONST_CS);
- /* set vs. clear */
- /* Copy options */
- REGISTER_LONG_CONSTANT("CP_UID", CP_UID, CONST_PERSISTENT | CONST_CS);
- /* argument is a UID sequence */
- REGISTER_LONG_CONSTANT("CP_MOVE", CP_MOVE, CONST_PERSISTENT | CONST_CS);
- /* delete from source after copying */
- /* Search/sort options */
- REGISTER_LONG_CONSTANT("SE_UID", SE_UID, CONST_PERSISTENT | CONST_CS);
- /* return UID */
- REGISTER_LONG_CONSTANT("SE_FREE", SE_FREE, CONST_PERSISTENT | CONST_CS);
- /* free search program after finished */
- REGISTER_LONG_CONSTANT("SE_NOPREFETCH", SE_NOPREFETCH, CONST_PERSISTENT | CONST_CS);
- /* no search prefetching */
- REGISTER_LONG_CONSTANT("SO_FREE", SO_FREE, CONST_PERSISTENT | CONST_CS);
- /* free sort program after finished */
- REGISTER_LONG_CONSTANT("SO_NOSERVER", SO_NOSERVER, CONST_PERSISTENT | CONST_CS);
- /* don't do server-based sort */
- /* Status options */
- REGISTER_LONG_CONSTANT("SA_MESSAGES", SA_MESSAGES , CONST_PERSISTENT | CONST_CS);
- /* number of messages */
- REGISTER_LONG_CONSTANT("SA_RECENT", SA_RECENT, CONST_PERSISTENT | CONST_CS);
- /* number of recent messages */
- REGISTER_LONG_CONSTANT("SA_UNSEEN", SA_UNSEEN , CONST_PERSISTENT | CONST_CS);
- /* number of unseen messages */
- REGISTER_LONG_CONSTANT("SA_UIDNEXT", SA_UIDNEXT, CONST_PERSISTENT | CONST_CS);
- /* next UID to be assigned */
- REGISTER_LONG_CONSTANT("SA_UIDVALIDITY", SA_UIDVALIDITY , CONST_PERSISTENT | CONST_CS);
- /* UID validity value */
- REGISTER_LONG_CONSTANT("SA_ALL", sa_all, CONST_PERSISTENT | CONST_CS);
- /* get all status information */
- /* Bits for mm_list() and mm_lsub() */
- REGISTER_LONG_CONSTANT("LATT_NOINFERIORS", LATT_NOINFERIORS , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("LATT_NOSELECT", LATT_NOSELECT, CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("LATT_MARKED", LATT_MARKED, CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("LATT_UNMARKED", LATT_UNMARKED , CONST_PERSISTENT | CONST_CS);
- #ifdef LATT_REFERRAL
- REGISTER_LONG_CONSTANT("LATT_REFERRAL", LATT_REFERRAL, CONST_PERSISTENT | CONST_CS);
- #endif
- #ifdef LATT_HASCHILDREN
- REGISTER_LONG_CONSTANT("LATT_HASCHILDREN", LATT_HASCHILDREN, CONST_PERSISTENT | CONST_CS);
- #endif
- #ifdef LATT_HASNOCHILDREN
- REGISTER_LONG_CONSTANT("LATT_HASNOCHILDREN", LATT_HASNOCHILDREN, CONST_PERSISTENT | CONST_CS);
- #endif
- /* Sort functions */
- REGISTER_LONG_CONSTANT("SORTDATE", SORTDATE , CONST_PERSISTENT | CONST_CS);
- /* date */
- REGISTER_LONG_CONSTANT("SORTARRIVAL", SORTARRIVAL , CONST_PERSISTENT | CONST_CS);
- /* arrival date */
- REGISTER_LONG_CONSTANT("SORTFROM", SORTFROM , CONST_PERSISTENT | CONST_CS);
- /* from */
- REGISTER_LONG_CONSTANT("SORTSUBJECT", SORTSUBJECT , CONST_PERSISTENT | CONST_CS);
- /* subject */
- REGISTER_LONG_CONSTANT("SORTTO", SORTTO , CONST_PERSISTENT | CONST_CS);
- /* to */
- REGISTER_LONG_CONSTANT("SORTCC", SORTCC , CONST_PERSISTENT | CONST_CS);
- /* cc */
- REGISTER_LONG_CONSTANT("SORTSIZE", SORTSIZE , CONST_PERSISTENT | CONST_CS);
- /* size */
- REGISTER_LONG_CONSTANT("TYPETEXT", TYPETEXT , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEMULTIPART", TYPEMULTIPART , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEMESSAGE", TYPEMESSAGE , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEAPPLICATION", TYPEAPPLICATION , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEAUDIO", TYPEAUDIO , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEIMAGE", TYPEIMAGE , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEVIDEO", TYPEVIDEO , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEMODEL", TYPEMODEL , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("TYPEOTHER", TYPEOTHER , CONST_PERSISTENT | CONST_CS);
- /*
- TYPETEXT unformatted text
- TYPEMULTIPART multiple part
- TYPEMESSAGE encapsulated message
- TYPEAPPLICATION application data
- TYPEAUDIO audio
- TYPEIMAGE static image (GIF, JPEG, etc.)
- TYPEVIDEO video
- TYPEMODEL model
- TYPEOTHER unknown
- */
- REGISTER_LONG_CONSTANT("ENC7BIT", ENC7BIT , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("ENC8BIT", ENC8BIT , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("ENCBINARY", ENCBINARY , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("ENCBASE64", ENCBASE64, CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("ENCQUOTEDPRINTABLE", ENCQUOTEDPRINTABLE , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("ENCOTHER", ENCOTHER , CONST_PERSISTENT | CONST_CS);
- /*
- ENC7BIT 7 bit SMTP semantic data
- ENC8BIT 8 bit SMTP semantic data
- ENCBINARY 8 bit binary data
- ENCBASE64 base-64 encoded data
- ENCQUOTEDPRINTABLE human-readable 8-as-7 bit data
- ENCOTHER unknown
- */
- REGISTER_LONG_CONSTANT("IMAP_GC_ELT", GC_ELT , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("IMAP_GC_ENV", GC_ENV , CONST_PERSISTENT | CONST_CS);
- REGISTER_LONG_CONSTANT("IMAP_GC_TEXTS", GC_TEXTS , CONST_PERSISTENT | CONST_CS);
- /*
- GC_ELT message cache elements
- GC_ENV ENVELOPEs and BODYs
- GC_TEXTS texts
- */
- if (!IMAPG(enable_rsh)) {
- /* disable SSH and RSH, see https://bugs.php.net/bug.php?id=77153 */
- mail_parameters (NIL, SET_RSHTIMEOUT, 0);
- mail_parameters (NIL, SET_SSHTIMEOUT, 0);
- }
- le_imap = zend_register_list_destructors_ex(mail_close_it, NULL, "imap", module_number);
- return SUCCESS;
- }
- /* }}} */
- /* {{{ PHP_RINIT_FUNCTION
- */
- PHP_RINIT_FUNCTION(imap)
- {
- IMAPG(imap_errorstack) = NIL;
- IMAPG(imap_alertstack) = NIL;
- IMAPG(gets_stream) = NIL;
- return SUCCESS;
- }
- /* }}} */
- /* {{{ PHP_RSHUTDOWN_FUNCTION
- */
- PHP_RSHUTDOWN_FUNCTION(imap)
- {
- ERRORLIST *ecur = NIL;
- STRINGLIST *acur = NIL;
- if (IMAPG(imap_errorstack) != NIL) {
- /* output any remaining errors at their original error level */
- if (EG(error_reporting) & E_NOTICE) {
- ecur = IMAPG(imap_errorstack);
- while (ecur != NIL) {
- php_error_docref(NULL, E_NOTICE, "%s (errflg=%ld)", ecur->LTEXT, ecur->errflg);
- ecur = ecur->next;
- }
- }
- mail_free_errorlist(&IMAPG(imap_errorstack));
- IMAPG(imap_errorstack) = NIL;
- }
- if (IMAPG(imap_alertstack) != NIL) {
- /* output any remaining alerts at E_NOTICE level */
- if (EG(error_reporting) & E_NOTICE) {
- acur = IMAPG(imap_alertstack);
- while (acur != NIL) {
- php_error_docref(NULL, E_NOTICE, "%s", acur->LTEXT);
- acur = acur->next;
- }
- }
- mail_free_stringlist(&IMAPG(imap_alertstack));
- IMAPG(imap_alertstack) = NIL;
- }
- return SUCCESS;
- }
- /* }}} */
- #if !defined(CCLIENTVERSION)
- #if HAVE_IMAP2007e
- #define CCLIENTVERSION "2007e"
- #elif HAVE_IMAP2007d
- #define CCLIENTVERSION "2007d"
- #elif HAVE_IMAP2007b
- #define CCLIENTVERSION "2007b"
- #elif HAVE_IMAP2007a
- #define CCLIENTVERSION "2007a"
- #elif HAVE_IMAP2004
- #define CCLIENTVERSION "2004"
- #elif HAVE_IMAP2001
- #define CCLIENTVERSION "2001"
- #elif HAVE_IMAP2000
- #define CCLIENTVERSION "2000"
- #elif defined(IMAP41)
- #define CCLIENTVERSION "4.1"
- #else
- #define CCLIENTVERSION "4.0"
- #endif
- #endif
- /* {{{ PHP_MINFO_FUNCTION
- */
- PHP_MINFO_FUNCTION(imap)
- {
- php_info_print_table_start();
- php_info_print_table_row(2, "IMAP c-Client Version", CCLIENTVERSION);
- #if HAVE_IMAP_SSL
- php_info_print_table_row(2, "SSL Support", "enabled");
- #endif
- #if HAVE_IMAP_KRB && HAVE_IMAP_AUTH_GSS
- php_info_print_table_row(2, "Kerberos Support", "enabled");
- #endif
- php_info_print_table_end();
- DISPLAY_INI_ENTRIES();
- }
- /* }}} */
- /* {{{ imap_do_open
- */
- static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
- {
- zend_string *mailbox, *user, *passwd;
- zend_long retries = 0, flags = NIL, cl_flags = NIL;
- MAILSTREAM *imap_stream;
- pils *imap_le_struct;
- zval *params = NULL;
- int argc = ZEND_NUM_ARGS();
- if (zend_parse_parameters(argc, "PSS|lla", &mailbox, &user,
- &passwd, &flags, &retries, ¶ms) == FAILURE) {
- RETURN_THROWS();
- }
- if (argc >= 4) {
- if (flags & PHP_EXPUNGE) {
- cl_flags = CL_EXPUNGE;
- flags ^= PHP_EXPUNGE;
- }
- if (flags & OP_PROTOTYPE) {
- cl_flags |= OP_PROTOTYPE;
- }
- }
- if (params) {
- zval *disabled_auth_method;
- if ((disabled_auth_method = zend_hash_str_find(Z_ARRVAL_P(params), "DISABLE_AUTHENTICATOR", sizeof("DISABLE_AUTHENTICATOR") - 1)) != NULL) {
- switch (Z_TYPE_P(disabled_auth_method)) {
- case IS_STRING:
- if (Z_STRLEN_P(disabled_auth_method) > 1) {
- mail_parameters(NIL, DISABLE_AUTHENTICATOR, (void *)Z_STRVAL_P(disabled_auth_method));
- }
- break;
- case IS_ARRAY:
- {
- zval *z_auth_method;
- int i;
- int nelems = zend_hash_num_elements(Z_ARRVAL_P(disabled_auth_method));
- if (nelems == 0 ) {
- break;
- }
- for (i = 0; i < nelems; i++) {
- if ((z_auth_method = zend_hash_index_find(Z_ARRVAL_P(disabled_auth_method), i)) != NULL) {
- if (Z_TYPE_P(z_auth_method) == IS_STRING) {
- if (Z_STRLEN_P(z_auth_method) > 1) {
- mail_parameters(NIL, DISABLE_AUTHENTICATOR, (void *)Z_STRVAL_P(z_auth_method));
- }
- } else {
- php_error_docref(NULL, E_WARNING, "Invalid argument, expect string or array of strings");
- }
- }
- }
- }
- break;
- case IS_LONG:
- default:
- php_error_docref(NULL, E_WARNING, "Invalid argument, expect string or array of strings");
- break;
- }
- }
- }
- if (IMAPG(imap_user)) {
- efree(IMAPG(imap_user));
- IMAPG(imap_user) = 0;
- }
- if (IMAPG(imap_password)) {
- efree(IMAPG(imap_password));
- IMAPG(imap_password) = 0;
- }
- /* local filename, need to perform open_basedir check */
- if (ZSTR_VAL(mailbox)[0] != '{' && php_check_open_basedir(ZSTR_VAL(mailbox))) {
- RETURN_FALSE;
- }
- IMAPG(imap_user) = estrndup(ZSTR_VAL(user), ZSTR_LEN(user));
- IMAPG(imap_password) = estrndup(ZSTR_VAL(passwd), ZSTR_LEN(passwd));
- #ifdef SET_MAXLOGINTRIALS
- if (argc >= 5) {
- if (retries < 0) {
- php_error_docref(NULL, E_WARNING ,"Retries must be greater or equal to 0");
- } else {
- mail_parameters(NIL, SET_MAXLOGINTRIALS, (void *) retries);
- }
- }
- #endif
- imap_stream = mail_open(NIL, ZSTR_VAL(mailbox), flags);
- if (imap_stream == NIL) {
- php_error_docref(NULL, E_WARNING, "Couldn't open stream %s", ZSTR_VAL(mailbox));
- efree(IMAPG(imap_user)); IMAPG(imap_user) = 0;
- efree(IMAPG(imap_password)); IMAPG(imap_password) = 0;
- RETURN_FALSE;
- }
- imap_le_struct = emalloc(sizeof(pils));
- imap_le_struct->imap_stream = imap_stream;
- imap_le_struct->flags = cl_flags;
- RETURN_RES(zend_register_resource(imap_le_struct, le_imap));
- }
- /* }}} */
- /* {{{ proto resource imap_open(string mailbox, string user, string password [, int options [, int n_retries]])
- Open an IMAP stream to a mailbox */
- PHP_FUNCTION(imap_open)
- {
- php_imap_do_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- /* }}} */
- /* {{{ proto bool imap_reopen(resource stream_id, string mailbox [, int options [, int n_retries]])
- Reopen an IMAP stream to a new mailbox */
- PHP_FUNCTION(imap_reopen)
- {
- zval *streamind;
- zend_string *mailbox;
- zend_long options = 0, retries = 0;
- pils *imap_le_struct;
- long flags=NIL;
- long cl_flags=NIL;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rS|ll", &streamind, &mailbox, &options, &retries) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (options) {
- flags = options;
- if (flags & PHP_EXPUNGE) {
- cl_flags = CL_EXPUNGE;
- flags ^= PHP_EXPUNGE;
- }
- imap_le_struct->flags = cl_flags;
- }
- #ifdef SET_MAXLOGINTRIALS
- if (retries) {
- mail_parameters(NIL, SET_MAXLOGINTRIALS, (void *) retries);
- }
- #endif
- /* local filename, need to perform open_basedir check */
- if (ZSTR_VAL(mailbox)[0] != '{' && php_check_open_basedir(ZSTR_VAL(mailbox))) {
- RETURN_FALSE;
- }
- imap_le_struct->imap_stream = mail_open(imap_le_struct->imap_stream, ZSTR_VAL(mailbox), flags);
- if (imap_le_struct->imap_stream == NIL) {
- zend_list_delete(Z_RES_P(streamind));
- php_error_docref(NULL, E_WARNING, "Couldn't re-open stream");
- RETURN_FALSE;
- }
- RETURN_TRUE;
- }
- /* }}} */
- /* {{{ proto bool imap_append(resource stream_id, string folder, string message [, string options [, string internal_date]])
- Append a new message to a specified mailbox */
- PHP_FUNCTION(imap_append)
- {
- zval *streamind;
- zend_string *folder, *message, *internal_date = NULL, *flags = NULL;
- pils *imap_le_struct;
- STRING st;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS|SS", &streamind, &folder, &message, &flags, &internal_date) == FAILURE) {
- RETURN_THROWS();
- }
- if (internal_date) {
- zend_string *regex = zend_string_init("/[0-3][0-9]-((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))-[0-9]{4} [0-2][0-9]:[0-5][0-9]:[0-5][0-9] [+-][0-9]{4}/", sizeof("/[0-3][0-9]-((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))-[0-9]{4} [0-2][0-9]:[0-5][0-9]:[0-5][0-9] [+-][0-9]{4}/") - 1, 0);
- pcre_cache_entry *pce; /* Compiled regex */
- zval *subpats = NULL; /* Parts (not used) */
- int global = 0;
- /* Make sure the given internal_date string matches the RFC specifiedformat */
- if ((pce = pcre_get_compiled_regex_cache(regex))== NULL) {
- zend_string_release(regex);
- RETURN_FALSE;
- }
- zend_string_release(regex);
- php_pcre_match_impl(pce, internal_date, return_value, subpats, global,
- 0, Z_L(0), Z_L(0));
- if (!Z_LVAL_P(return_value)) {
- php_error_docref(NULL, E_WARNING, "Internal date not correctly formatted");
- internal_date = NULL;
- }
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- INIT (&st, mail_string, (void *) ZSTR_VAL(message), ZSTR_LEN(message));
- if (mail_append_full(imap_le_struct->imap_stream, ZSTR_VAL(folder), (flags ? ZSTR_VAL(flags) : NIL), (internal_date ? ZSTR_VAL(internal_date) : NIL), &st)) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto int imap_num_msg(resource stream_id)
- Gives the number of messages in the current mailbox */
- PHP_FUNCTION(imap_num_msg)
- {
- zval *streamind;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &streamind) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- RETURN_LONG(imap_le_struct->imap_stream->nmsgs);
- }
- /* }}} */
- /* {{{ proto bool imap_ping(resource stream_id)
- Check if the IMAP stream is still active */
- PHP_FUNCTION(imap_ping)
- {
- zval *streamind;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &streamind) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- RETURN_BOOL(mail_ping(imap_le_struct->imap_stream));
- }
- /* }}} */
- /* {{{ proto int imap_num_recent(resource stream_id)
- Gives the number of recent messages in current mailbox */
- PHP_FUNCTION(imap_num_recent)
- {
- zval *streamind;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &streamind) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- RETURN_LONG(imap_le_struct->imap_stream->recent);
- }
- /* }}} */
- #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
- /* {{{ proto array imap_get_quota(resource stream_id, string qroot)
- Returns the quota set to the mailbox account qroot */
- PHP_FUNCTION(imap_get_quota)
- {
- zval *streamind;
- zend_string *qroot;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rS", &streamind, &qroot) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- array_init(return_value);
- IMAPG(quota_return) = &return_value;
- /* set the callback for the GET_QUOTA function */
- mail_parameters(NIL, SET_QUOTA, (void *) mail_getquota);
- if (!imap_getquota(imap_le_struct->imap_stream, ZSTR_VAL(qroot))) {
- php_error_docref(NULL, E_WARNING, "C-client imap_getquota failed");
- zend_array_destroy(Z_ARR_P(return_value));
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto array imap_get_quotaroot(resource stream_id, string mbox)
- Returns the quota set to the mailbox account mbox */
- PHP_FUNCTION(imap_get_quotaroot)
- {
- zval *streamind;
- zend_string *mbox;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rS", &streamind, &mbox) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- array_init(return_value);
- IMAPG(quota_return) = &return_value;
- /* set the callback for the GET_QUOTAROOT function */
- mail_parameters(NIL, SET_QUOTA, (void *) mail_getquota);
- if (!imap_getquotaroot(imap_le_struct->imap_stream, ZSTR_VAL(mbox))) {
- php_error_docref(NULL, E_WARNING, "C-client imap_getquotaroot failed");
- zend_array_destroy(Z_ARR_P(return_value));
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto bool imap_set_quota(resource stream_id, string qroot, int mailbox_size)
- Will set the quota for qroot mailbox */
- PHP_FUNCTION(imap_set_quota)
- {
- zval *streamind;
- zend_string *qroot;
- zend_long mailbox_size;
- pils *imap_le_struct;
- STRINGLIST limits;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSl", &streamind, &qroot, &mailbox_size) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- limits.text.data = (unsigned char*)"STORAGE";
- limits.text.size = mailbox_size;
- limits.next = NIL;
- RETURN_BOOL(imap_setquota(imap_le_struct->imap_stream, ZSTR_VAL(qroot), &limits));
- }
- /* }}} */
- /* {{{ proto bool imap_setacl(resource stream_id, string mailbox, string id, string rights)
- Sets the ACL for a given mailbox */
- PHP_FUNCTION(imap_setacl)
- {
- zval *streamind;
- zend_string *mailbox, *id, *rights;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSSS", &streamind, &mailbox, &id, &rights) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- RETURN_BOOL(imap_setacl(imap_le_struct->imap_stream, ZSTR_VAL(mailbox), ZSTR_VAL(id), ZSTR_VAL(rights)));
- }
- /* }}} */
- /* {{{ proto array imap_getacl(resource stream_id, string mailbox)
- Gets the ACL for a given mailbox */
- PHP_FUNCTION(imap_getacl)
- {
- zval *streamind;
- zend_string *mailbox;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rS", &streamind, &mailbox) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- /* initializing the special array for the return values */
- array_init(return_value);
- IMAPG(imap_acl_list) = return_value;
- /* set the callback for the GET_ACL function */
- mail_parameters(NIL, SET_ACL, (void *) mail_getacl);
- if (!imap_getacl(imap_le_struct->imap_stream, ZSTR_VAL(mailbox))) {
- php_error(E_WARNING, "c-client imap_getacl failed");
- zend_array_destroy(Z_ARR_P(return_value));
- RETURN_FALSE;
- }
- IMAPG(imap_acl_list) = NIL;
- }
- /* }}} */
- #endif /* HAVE_IMAP2000 || HAVE_IMAP2001 */
- /* {{{ proto bool imap_expunge(resource stream_id)
- Permanently delete all messages marked for deletion */
- PHP_FUNCTION(imap_expunge)
- {
- zval *streamind;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &streamind) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- mail_expunge (imap_le_struct->imap_stream);
- RETURN_TRUE;
- }
- /* }}} */
- /* {{{ proto bool imap_gc(resource stream_id, int flags)
- This function garbage collects (purges) the cache of entries of a specific type. */
- PHP_FUNCTION(imap_gc)
- {
- zval *streamind;
- pils *imap_le_struct;
- zend_long flags;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &streamind, &flags) == FAILURE) {
- RETURN_THROWS();
- }
- if (flags && ((flags & ~(GC_TEXTS | GC_ELT | GC_ENV)) != 0)) {
- php_error_docref(NULL, E_WARNING, "Invalid value for the flags parameter");
- RETURN_FALSE;
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- mail_gc(imap_le_struct->imap_stream, flags);
- RETURN_TRUE;
- }
- /* }}} */
- /* {{{ proto bool imap_close(resource stream_id [, int options])
- Close an IMAP stream */
- PHP_FUNCTION(imap_close)
- {
- zval *streamind;
- pils *imap_le_struct=NULL;
- zend_long options = 0, flags = NIL;
- int argc = ZEND_NUM_ARGS();
- if (zend_parse_parameters(argc, "r|l", &streamind, &options) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (argc == 2) {
- flags = options;
- /* Check that flags is exactly equal to PHP_EXPUNGE or zero */
- if (flags && ((flags & ~PHP_EXPUNGE) != 0)) {
- php_error_docref(NULL, E_WARNING, "Invalid value for the flags parameter");
- RETURN_FALSE;
- }
- /* Do the translation from PHP's internal PHP_EXPUNGE define to c-client's CL_EXPUNGE */
- if (flags & PHP_EXPUNGE) {
- flags ^= PHP_EXPUNGE;
- flags |= CL_EXPUNGE;
- }
- imap_le_struct->flags = flags;
- }
- zend_list_close(Z_RES_P(streamind));
- RETURN_TRUE;
- }
- /* }}} */
- /* {{{ proto array imap_headers(resource stream_id)
- Returns headers for all messages in a mailbox */
- PHP_FUNCTION(imap_headers)
- {
- zval *streamind;
- pils *imap_le_struct;
- unsigned long i;
- char *t;
- unsigned int msgno;
- char tmp[MAILTMPLEN];
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &streamind) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- /* Initialize return array */
- array_init(return_value);
- for (msgno = 1; msgno <= imap_le_struct->imap_stream->nmsgs; msgno++) {
- MESSAGECACHE * cache = mail_elt (imap_le_struct->imap_stream, msgno);
- mail_fetchstructure(imap_le_struct->imap_stream, msgno, NIL);
- tmp[0] = cache->recent ? (cache->seen ? 'R': 'N') : ' ';
- tmp[1] = (cache->recent | cache->seen) ? ' ' : 'U';
- tmp[2] = cache->flagged ? 'F' : ' ';
- tmp[3] = cache->answered ? 'A' : ' ';
- tmp[4] = cache->deleted ? 'D' : ' ';
- tmp[5] = cache->draft ? 'X' : ' ';
- snprintf(tmp + 6, sizeof(tmp) - 6, "%4ld) ", cache->msgno);
- mail_date(tmp+11, cache);
- tmp[22] = ' ';
- tmp[23] = '\0';
- mail_fetchfrom(tmp+23, imap_le_struct->imap_stream, msgno, (long)20);
- strcat(tmp, " ");
- if ((i = cache->user_flags)) {
- strcat(tmp, "{");
- while (i) {
- strlcat(tmp, imap_le_struct->imap_stream->user_flags[find_rightmost_bit (&i)], sizeof(tmp));
- if (i) strlcat(tmp, " ", sizeof(tmp));
- }
- strlcat(tmp, "} ", sizeof(tmp));
- }
- mail_fetchsubject(t = tmp + strlen(tmp), imap_le_struct->imap_stream, msgno, (long)25);
- snprintf(t += strlen(t), sizeof(tmp) - strlen(tmp), " (%ld chars)", cache->rfc822_size);
- add_next_index_string(return_value, tmp);
- }
- }
- /* }}} */
- /* {{{ proto string imap_body(resource stream_id, int msg_no [, int options])
- Read the message body */
- PHP_FUNCTION(imap_body)
- {
- zval *streamind;
- zend_long msgno, flags = 0;
- pils *imap_le_struct;
- int msgindex, argc = ZEND_NUM_ARGS();
- char *body;
- unsigned long body_len = 0;
- if (zend_parse_parameters(argc, "rl|l", &streamind, &msgno, &flags) == FAILURE) {
- RETURN_THROWS();
- }
- if (flags && ((flags & ~(FT_UID|FT_PEEK|FT_INTERNAL)) != 0)) {
- php_error_docref(NULL, E_WARNING, "Invalid value for the options parameter");
- RETURN_FALSE;
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if ((argc == 3) && (flags & FT_UID)) {
- /* This should be cached; if it causes an extra RTT to the
- IMAP server, then that's the price we pay for making
- sure we don't crash. */
- msgindex = mail_msgno(imap_le_struct->imap_stream, msgno);
- } else {
- msgindex = msgno;
- }
- if ((msgindex < 1) || ((unsigned) msgindex > imap_le_struct->imap_stream->nmsgs)) {
- php_error_docref(NULL, E_WARNING, "Bad message number");
- RETURN_FALSE;
- }
- body = mail_fetchtext_full (imap_le_struct->imap_stream, msgno, &body_len, (argc == 3 ? flags : NIL));
- if (body_len == 0) {
- RETVAL_EMPTY_STRING();
- } else {
- RETVAL_STRINGL(body, body_len);
- }
- }
- /* }}} */
- /* {{{ proto bool imap_mail_copy(resource stream_id, string msglist, string mailbox [, int options])
- Copy specified message to a mailbox */
- PHP_FUNCTION(imap_mail_copy)
- {
- zval *streamind;
- zend_long options = 0;
- zend_string *seq, *folder;
- int argc = ZEND_NUM_ARGS();
- pils *imap_le_struct;
- if (zend_parse_parameters(argc, "rSS|l", &streamind, &seq, &folder, &options) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (mail_copy_full(imap_le_struct->imap_stream, ZSTR_VAL(seq), ZSTR_VAL(folder), (argc == 4 ? options : NIL)) == T) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto bool imap_mail_move(resource stream_id, string sequence, string mailbox [, int options])
- Move specified message to a mailbox */
- PHP_FUNCTION(imap_mail_move)
- {
- zval *streamind;
- zend_string *seq, *folder;
- zend_long options = 0;
- pils *imap_le_struct;
- int argc = ZEND_NUM_ARGS();
- if (zend_parse_parameters(argc, "rSS|l", &streamind, &seq, &folder, &options) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (mail_copy_full(imap_le_struct->imap_stream, ZSTR_VAL(seq), ZSTR_VAL(folder), (argc == 4 ? (options | CP_MOVE) : CP_MOVE)) == T) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto bool imap_createmailbox(resource stream_id, string mailbox)
- Create a new mailbox */
- PHP_FUNCTION(imap_createmailbox)
- {
- zval *streamind;
- zend_string *folder;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rS", &streamind, &folder) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (mail_create(imap_le_struct->imap_stream, ZSTR_VAL(folder)) == T) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto bool imap_renamemailbox(resource stream_id, string old_name, string new_name)
- Rename a mailbox */
- PHP_FUNCTION(imap_renamemailbox)
- {
- zval *streamind;
- zend_string *old_mailbox, *new_mailbox;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS", &streamind, &old_mailbox, &new_mailbox) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (mail_rename(imap_le_struct->imap_stream, ZSTR_VAL(old_mailbox), ZSTR_VAL(new_mailbox)) == T) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto bool imap_deletemailbox(resource stream_id, string mailbox)
- Delete a mailbox */
- PHP_FUNCTION(imap_deletemailbox)
- {
- zval *streamind;
- zend_string *folder;
- pils *imap_le_struct;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rS", &streamind, &folder) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (mail_delete(imap_le_struct->imap_stream, ZSTR_VAL(folder)) == T) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto array imap_list(resource stream_id, string ref, string pattern)
- Read the list of mailboxes */
- PHP_FUNCTION(imap_list)
- {
- zval *streamind;
- zend_string *ref, *pat;
- pils *imap_le_struct;
- STRINGLIST *cur=NIL;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS", &streamind, &ref, &pat) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- /* set flag for normal, old mailbox list */
- IMAPG(folderlist_style) = FLIST_ARRAY;
- IMAPG(imap_folders) = IMAPG(imap_folders_tail) = NIL;
- mail_list(imap_le_struct->imap_stream, ZSTR_VAL(ref), ZSTR_VAL(pat));
- if (IMAPG(imap_folders) == NIL) {
- RETURN_FALSE;
- }
- array_init(return_value);
- cur=IMAPG(imap_folders);
- while (cur != NIL) {
- add_next_index_string(return_value, (char*)cur->LTEXT);
- cur=cur->next;
- }
- mail_free_stringlist (&IMAPG(imap_folders));
- IMAPG(imap_folders) = IMAPG(imap_folders_tail) = NIL;
- }
- /* }}} */
- /* {{{ proto array imap_getmailboxes(resource stream_id, string ref, string pattern)
- Reads the list of mailboxes and returns a full array of objects containing name, attributes, and delimiter */
- /* Author: CJH */
- PHP_FUNCTION(imap_getmailboxes)
- {
- zval *streamind, mboxob;
- zend_string *ref, *pat;
- pils *imap_le_struct;
- FOBJECTLIST *cur=NIL;
- char *delim=NIL;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS", &streamind, &ref, &pat) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- /* set flag for new, improved array of objects mailbox list */
- IMAPG(folderlist_style) = FLIST_OBJECT;
- IMAPG(imap_folder_objects) = IMAPG(imap_folder_objects_tail) = NIL;
- mail_list(imap_le_struct->imap_stream, ZSTR_VAL(ref), ZSTR_VAL(pat));
- if (IMAPG(imap_folder_objects) == NIL) {
- RETURN_FALSE;
- }
- array_init(return_value);
- delim = safe_emalloc(2, sizeof(char), 0);
- cur=IMAPG(imap_folder_objects);
- while (cur != NIL) {
- object_init(&mboxob);
- add_property_string(&mboxob, "name", (char*)cur->LTEXT);
- add_property_long(&mboxob, "attributes", cur->attributes);
- #ifdef IMAP41
- delim[0] = (char)cur->delimiter;
- delim[1] = 0;
- add_property_string(&mboxob, "delimiter", delim);
- #else
- add_property_string(&mboxob, "delimiter", cur->delimiter);
- #endif
- add_next_index_object(return_value, &mboxob);
- cur=cur->next;
- }
- mail_free_foblist(&IMAPG(imap_folder_objects), &IMAPG(imap_folder_objects_tail));
- efree(delim);
- IMAPG(folderlist_style) = FLIST_ARRAY; /* reset to default */
- }
- /* }}} */
- /* {{{ proto array imap_listscan(resource stream_id, string ref, string pattern, string content)
- Read list of mailboxes containing a certain string */
- PHP_FUNCTION(imap_listscan)
- {
- zval *streamind;
- zend_string *ref, *pat, *content;
- pils *imap_le_struct;
- STRINGLIST *cur=NIL;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSSS", &streamind, &ref, &pat, &content) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- IMAPG(imap_folders) = NIL;
- mail_scan(imap_le_struct->imap_stream, ZSTR_VAL(ref), ZSTR_VAL(pat), ZSTR_VAL(content));
- if (IMAPG(imap_folders) == NIL) {
- RETURN_FALSE;
- }
- array_init(return_value);
- cur=IMAPG(imap_folders);
- while (cur != NIL) {
- add_next_index_string(return_value, (char*)cur->LTEXT);
- cur=cur->next;
- }
- mail_free_stringlist (&IMAPG(imap_folders));
- IMAPG(imap_folders) = IMAPG(imap_folders_tail) = NIL;
- }
- /* }}} */
- /* {{{ proto object imap_check(resource stream_id)
- Get mailbox properties */
- PHP_FUNCTION(imap_check)
- {
- zval *streamind;
- pils *imap_le_struct;
- char date[100];
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &streamind) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (mail_ping (imap_le_struct->imap_stream) == NIL) {
- RETURN_FALSE;
- }
- if (imap_le_struct->imap_stream && imap_le_struct->imap_stream->mailbox) {
- rfc822_date(date);
- object_init(return_value);
- add_property_string(return_value, "Date", date);
- add_property_string(return_value, "Driver", imap_le_struct->imap_stream->dtb->name);
- add_property_string(return_value, "Mailbox", imap_le_struct->imap_stream->mailbox);
- add_property_long(return_value, "Nmsgs", imap_le_struct->imap_stream->nmsgs);
- add_property_long(return_value, "Recent", imap_le_struct->imap_stream->recent);
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto bool imap_delete(resource stream_id, int msg_no [, int options])
- Mark a message for deletion */
- PHP_FUNCTION(imap_delete)
- {
- zval *streamind, *sequence;
- pils *imap_le_struct;
- zend_long flags = 0;
- int argc = ZEND_NUM_ARGS();
- if (zend_parse_parameters(argc, "rz|l", &streamind, &sequence, &flags) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (!try_convert_to_string(sequence)) {
- RETURN_THROWS();
- }
- mail_setflag_full(imap_le_struct->imap_stream, Z_STRVAL_P(sequence), "\\DELETED", (argc == 3 ? flags : NIL));
- RETVAL_TRUE;
- }
- /* }}} */
- /* {{{ proto bool imap_undelete(resource stream_id, int msg_no [, int flags])
- Remove the delete flag from a message */
- PHP_FUNCTION(imap_undelete)
- {
- zval *streamind, *sequence;
- zend_long flags = 0;
- pils *imap_le_struct;
- int argc = ZEND_NUM_ARGS();
- if (zend_parse_parameters(argc, "rz|l", &streamind, &sequence, &flags) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (!try_convert_to_string(sequence)) {
- RETURN_THROWS();
- }
- mail_clearflag_full(imap_le_struct->imap_stream, Z_STRVAL_P(sequence), "\\DELETED", (argc == 3 ? flags : NIL));
- RETVAL_TRUE;
- }
- /* }}} */
- /* {{{ proto object imap_headerinfo(resource stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
- Read the headers of the message */
- PHP_FUNCTION(imap_headerinfo)
- {
- zval *streamind;
- zend_string *defaulthost = NULL;
- int argc = ZEND_NUM_ARGS();
- zend_long msgno, fromlength, subjectlength;
- pils *imap_le_struct;
- MESSAGECACHE *cache;
- ENVELOPE *en;
- char dummy[2000], fulladdress[MAILTMPLEN + 1];
- if (zend_parse_parameters(argc, "rl|llS", &streamind, &msgno, &fromlength, &subjectlength, &defaulthost) == FAILURE) {
- RETURN_THROWS();
- }
- if ((imap_le_struct = (pils *)zend_fetch_resource(Z_RES_P(streamind), "imap", le_imap)) == NULL) {
- RETURN_THROWS();
- }
- if (argc >= 3) {
- if (fromlength < 0 || fromlength > MAILTMPLEN) {
- php_error_docref(NULL, E_WARNING, "From length has to be between 0 and %d", MAILTMPLEN);
- RETURN_FALSE;
- }
- } else {
- fromlength = 0x00;
- }
- if (argc >= 4) {
- if (subjectlength < 0 || subjectlength > MAILTMPLEN) {
- php_error_docref(NULL, E_WARNING, "Subject length has to be between 0 and %d", MAILTMPLEN);
- RETURN_FALSE;
- }
- } else {
- subjectlength = 0x00;
- }
- PHP_IMAP_CHECK_MSGNO(msgno);
- if (mail_fetchstructure(imap_le_struct->imap_stream, msgno, NIL)) {
- cache = mail_elt(imap_le_struct->imap_stream, msgno);
- } else {
- RETURN_FALSE;
- }
- en = mail_fetchenvelope(imap_le_struct->imap_stream, msgno);
- /* call a function to parse all the text, so that we can use the
- same function to parse text from other sources */
- _php_make_header_object(return_value, en);
- /* now run through properties th…
Large files files are truncated, but you can click here to view the full file