PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/imap - php_imap.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1328 2078 63.9 %
Date: 2014-07-13 Functions: 79 106 74.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Rex Logan           <veebert@dimensional.com>               |
      16             :    |          Mark Musone         <musone@afterfive.com>                  |
      17             :    |          Brian Wang          <brian@vividnet.com>                    |
      18             :    |          Kaj-Michael Lang    <milang@tal.org>                        |
      19             :    |          Antoni Pamies Olive <toni@readysoft.net>                    |
      20             :    |          Rasmus Lerdorf      <rasmus@php.net>                        |
      21             :    |          Chuck Hagenbuch     <chuck@horde.org>                       |
      22             :    |          Andrew Skalski      <askalski@chekinc.com>                  |
      23             :    |          Hartmut Holzgraefe  <hholzgra@php.net>                      |
      24             :    |          Jani Taskinen       <jani.taskinen@iki.fi>                  |
      25             :    |          Daniel R. Kalowsky  <kalowsky@php.net>                      |
      26             :    | PHP 4.0 updates:  Zeev Suraski <zeev@zend.com>                       |
      27             :    +----------------------------------------------------------------------+
      28             :  */
      29             : /* $Id$ */
      30             : 
      31             : #define IMAP41
      32             : 
      33             : #ifdef HAVE_CONFIG_H
      34             : #include "config.h"
      35             : #endif
      36             : 
      37             : #include "php.h"
      38             : #include "php_ini.h"
      39             : #include "php_streams.h"
      40             : #include "ext/standard/php_string.h"
      41             : #include "ext/standard/info.h"
      42             : #include "ext/standard/file.h"
      43             : #include "ext/standard/php_smart_str.h"
      44             : #include "ext/pcre/php_pcre.h"
      45             : 
      46             : #ifdef ERROR
      47             : #undef ERROR
      48             : #endif
      49             : #include "php_imap.h"
      50             : 
      51             : #include <time.h>
      52             : #include <stdio.h>
      53             : #include <ctype.h>
      54             : #include <signal.h>
      55             : 
      56             : #ifdef PHP_WIN32
      57             : #include <winsock2.h>
      58             : #include <stdlib.h>
      59             : #include "win32/sendmail.h"
      60             : MAILSTREAM DEFAULTPROTO;
      61             : #endif
      62             : 
      63             : #define CRLF    "\015\012"
      64             : #define CRLF_LEN sizeof("\015\012") - 1
      65             : #define PHP_EXPUNGE 32768
      66             : #define PHP_IMAP_ADDRESS_SIZE_BUF 10
      67             : #ifndef SENDBUFLEN
      68             : #define SENDBUFLEN 16385
      69             : #endif
      70             : 
      71             : #if defined(__GNUC__) && __GNUC__ >= 4
      72             : # define PHP_IMAP_EXPORT __attribute__ ((visibility("default")))
      73             : #else
      74             : # define PHP_IMAP_EXPORT
      75             : #endif
      76             : 
      77             : static void _php_make_header_object(zval *myzvalue, ENVELOPE *en TSRMLS_DC);
      78             : static void _php_imap_add_body(zval *arg, BODY *body TSRMLS_DC);
      79             : static char* _php_imap_parse_address(ADDRESS *addresslist, zval *paddress TSRMLS_DC);
      80             : static char* _php_rfc822_write_address(ADDRESS *addresslist TSRMLS_DC);
      81             : 
      82             : /* the gets we use */
      83             : static char *php_mail_gets(readfn_t f, void *stream, unsigned long size, GETS_DATA *md);
      84             : 
      85             : /* These function declarations are missing from the IMAP header files... */
      86             : void rfc822_date(char *date);
      87             : char *cpystr(const char *str);
      88             : char *cpytxt(SIZEDTEXT *dst, char *text, unsigned long size);
      89             : #ifndef HAVE_NEW_MIME2TEXT
      90             : long utf8_mime2text(SIZEDTEXT *src, SIZEDTEXT *dst);
      91             : #else
      92             : long utf8_mime2text (SIZEDTEXT *src, SIZEDTEXT *dst, long flags);
      93             : #endif
      94             : unsigned long find_rightmost_bit(unsigned long *valptr);
      95             : void fs_give(void **block);
      96             : void *fs_get(size_t size);
      97             : 
      98             : ZEND_DECLARE_MODULE_GLOBALS(imap)
      99             : static PHP_GINIT_FUNCTION(imap);
     100             : 
     101             : /* {{{ arginfo */
     102             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_open, 0, 0, 3)
     103             :         ZEND_ARG_INFO(0, mailbox)
     104             :         ZEND_ARG_INFO(0, user)
     105             :         ZEND_ARG_INFO(0, password)
     106             :         ZEND_ARG_INFO(0, options)
     107             :         ZEND_ARG_INFO(0, n_retries)
     108             :         ZEND_ARG_INFO(0, params)
     109             : ZEND_END_ARG_INFO()
     110             : 
     111             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_reopen, 0, 0, 2)
     112             :         ZEND_ARG_INFO(0, stream_id)
     113             :         ZEND_ARG_INFO(0, mailbox)
     114             :         ZEND_ARG_INFO(0, options)
     115             :         ZEND_ARG_INFO(0, n_retries)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_append, 0, 0, 3)
     119             :         ZEND_ARG_INFO(0, stream_id)
     120             :         ZEND_ARG_INFO(0, folder)
     121             :         ZEND_ARG_INFO(0, message)
     122             :         ZEND_ARG_INFO(0, options)
     123             :         ZEND_ARG_INFO(0, date)
     124             : ZEND_END_ARG_INFO()
     125             : 
     126             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_num_msg, 0, 0, 1)
     127             :         ZEND_ARG_INFO(0, stream_id)
     128             : ZEND_END_ARG_INFO()
     129             : 
     130             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_ping, 0, 0, 1)
     131             :         ZEND_ARG_INFO(0, stream_id)
     132             : ZEND_END_ARG_INFO()
     133             : 
     134             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_num_recent, 0, 0, 1)
     135             :         ZEND_ARG_INFO(0, stream_id)
     136             : ZEND_END_ARG_INFO()
     137             : 
     138             : #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
     139             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_get_quota, 0, 0, 2)
     140             :         ZEND_ARG_INFO(0, stream_id)
     141             :         ZEND_ARG_INFO(0, qroot)
     142             : ZEND_END_ARG_INFO()
     143             : 
     144             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_get_quotaroot, 0, 0, 2)
     145             :         ZEND_ARG_INFO(0, stream_id)
     146             :         ZEND_ARG_INFO(0, mbox)
     147             : ZEND_END_ARG_INFO()
     148             : 
     149             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_set_quota, 0, 0, 3)
     150             :         ZEND_ARG_INFO(0, stream_id)
     151             :         ZEND_ARG_INFO(0, qroot)
     152             :         ZEND_ARG_INFO(0, mailbox_size)
     153             : ZEND_END_ARG_INFO()
     154             : 
     155             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_setacl, 0, 0, 4)
     156             :         ZEND_ARG_INFO(0, stream_id)
     157             :         ZEND_ARG_INFO(0, mailbox)
     158             :         ZEND_ARG_INFO(0, id)
     159             :         ZEND_ARG_INFO(0, rights)
     160             : ZEND_END_ARG_INFO()
     161             : 
     162             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_getacl, 0, 0, 2)
     163             :         ZEND_ARG_INFO(0, stream_id)
     164             :         ZEND_ARG_INFO(0, mailbox)
     165             : ZEND_END_ARG_INFO()
     166             : #endif
     167             : 
     168             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_expunge, 0, 0, 1)
     169             :         ZEND_ARG_INFO(0, stream_id)
     170             : ZEND_END_ARG_INFO()
     171             : 
     172             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_gc, 0, 0, 1)
     173             :         ZEND_ARG_INFO(0, stream_id)
     174             :         ZEND_ARG_INFO(0, flags)
     175             : ZEND_END_ARG_INFO()
     176             : 
     177             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_close, 0, 0, 1)
     178             :         ZEND_ARG_INFO(0, stream_id)
     179             :         ZEND_ARG_INFO(0, options)
     180             : ZEND_END_ARG_INFO()
     181             : 
     182             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_headers, 0, 0, 1)
     183             :         ZEND_ARG_INFO(0, stream_id)
     184             : ZEND_END_ARG_INFO()
     185             : 
     186             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_body, 0, 0, 2)
     187             :         ZEND_ARG_INFO(0, stream_id)
     188             :         ZEND_ARG_INFO(0, msg_no)
     189             :         ZEND_ARG_INFO(0, options)
     190             : ZEND_END_ARG_INFO()
     191             : 
     192             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_mail_copy, 0, 0, 3)
     193             :         ZEND_ARG_INFO(0, stream_id)
     194             :         ZEND_ARG_INFO(0, msglist)
     195             :         ZEND_ARG_INFO(0, mailbox)
     196             :         ZEND_ARG_INFO(0, options)
     197             : ZEND_END_ARG_INFO()
     198             : 
     199             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_mail_move, 0, 0, 3)
     200             :         ZEND_ARG_INFO(0, stream_id)
     201             :         ZEND_ARG_INFO(0, sequence)
     202             :         ZEND_ARG_INFO(0, mailbox)
     203             :         ZEND_ARG_INFO(0, options)
     204             : ZEND_END_ARG_INFO()
     205             : 
     206             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_createmailbox, 0, 0, 2)
     207             :         ZEND_ARG_INFO(0, stream_id)
     208             :         ZEND_ARG_INFO(0, mailbox)
     209             : ZEND_END_ARG_INFO()
     210             : 
     211             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_renamemailbox, 0, 0, 3)
     212             :         ZEND_ARG_INFO(0, stream_id)
     213             :         ZEND_ARG_INFO(0, old_name)
     214             :         ZEND_ARG_INFO(0, new_name)
     215             : ZEND_END_ARG_INFO()
     216             : 
     217             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_deletemailbox, 0, 0, 2)
     218             :         ZEND_ARG_INFO(0, stream_id)
     219             :         ZEND_ARG_INFO(0, mailbox)
     220             : ZEND_END_ARG_INFO()
     221             : 
     222             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_list, 0, 0, 3)
     223             :         ZEND_ARG_INFO(0, stream_id)
     224             :         ZEND_ARG_INFO(0, ref)
     225             :         ZEND_ARG_INFO(0, pattern)
     226             : ZEND_END_ARG_INFO()
     227             : 
     228             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_getmailboxes, 0, 0, 3)
     229             :         ZEND_ARG_INFO(0, stream_id)
     230             :         ZEND_ARG_INFO(0, ref)
     231             :         ZEND_ARG_INFO(0, pattern)
     232             : ZEND_END_ARG_INFO()
     233             : 
     234             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_listscan, 0, 0, 4)
     235             :         ZEND_ARG_INFO(0, stream_id)
     236             :         ZEND_ARG_INFO(0, ref)
     237             :         ZEND_ARG_INFO(0, pattern)
     238             :         ZEND_ARG_INFO(0, content)
     239             : ZEND_END_ARG_INFO()
     240             : 
     241             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_check, 0, 0, 1)
     242             :         ZEND_ARG_INFO(0, stream_id)
     243             : ZEND_END_ARG_INFO()
     244             : 
     245             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_delete, 0, 0, 2)
     246             :         ZEND_ARG_INFO(0, stream_id)
     247             :         ZEND_ARG_INFO(0, msg_no)
     248             :         ZEND_ARG_INFO(0, options)
     249             : ZEND_END_ARG_INFO()
     250             : 
     251             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_undelete, 0, 0, 2)
     252             :         ZEND_ARG_INFO(0, stream_id)
     253             :         ZEND_ARG_INFO(0, msg_no)
     254             :         ZEND_ARG_INFO(0, flags)
     255             : ZEND_END_ARG_INFO()
     256             : 
     257             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_headerinfo, 0, 0, 2)
     258             :         ZEND_ARG_INFO(0, stream_id)
     259             :         ZEND_ARG_INFO(0, msg_no)
     260             :         ZEND_ARG_INFO(0, from_length)
     261             :         ZEND_ARG_INFO(0, subject_length)
     262             :         ZEND_ARG_INFO(0, default_host)
     263             : ZEND_END_ARG_INFO()
     264             : 
     265             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_rfc822_parse_headers, 0, 0, 1)
     266             :         ZEND_ARG_INFO(0, headers)
     267             :         ZEND_ARG_INFO(0, default_host)
     268             : ZEND_END_ARG_INFO()
     269             : 
     270             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_lsub, 0, 0, 3)
     271             :         ZEND_ARG_INFO(0, stream_id)
     272             :         ZEND_ARG_INFO(0, ref)
     273             :         ZEND_ARG_INFO(0, pattern)
     274             : ZEND_END_ARG_INFO()
     275             : 
     276             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_getsubscribed, 0, 0, 3)
     277             :         ZEND_ARG_INFO(0, stream_id)
     278             :         ZEND_ARG_INFO(0, ref)
     279             :         ZEND_ARG_INFO(0, pattern)
     280             : ZEND_END_ARG_INFO()
     281             : 
     282             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_subscribe, 0, 0, 2)
     283             :         ZEND_ARG_INFO(0, stream_id)
     284             :         ZEND_ARG_INFO(0, mailbox)
     285             : ZEND_END_ARG_INFO()
     286             : 
     287             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_unsubscribe, 0, 0, 2)
     288             :         ZEND_ARG_INFO(0, stream_id)
     289             :         ZEND_ARG_INFO(0, mailbox)
     290             : ZEND_END_ARG_INFO()
     291             : 
     292             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_fetchstructure, 0, 0, 2)
     293             :         ZEND_ARG_INFO(0, stream_id)
     294             :         ZEND_ARG_INFO(0, msg_no)
     295             :         ZEND_ARG_INFO(0, options)
     296             : ZEND_END_ARG_INFO()
     297             : 
     298             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_fetchbody, 0, 0, 3)
     299             :         ZEND_ARG_INFO(0, stream_id)
     300             :         ZEND_ARG_INFO(0, msg_no)
     301             :         ZEND_ARG_INFO(0, section)
     302             :         ZEND_ARG_INFO(0, options)
     303             : ZEND_END_ARG_INFO()
     304             : 
     305             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_savebody, 0, 0, 3)
     306             :         ZEND_ARG_INFO(0, stream_id)
     307             :         ZEND_ARG_INFO(0, file)
     308             :         ZEND_ARG_INFO(0, msg_no)
     309             :         ZEND_ARG_INFO(0, section)
     310             :         ZEND_ARG_INFO(0, options)
     311             : ZEND_END_ARG_INFO()
     312             : 
     313             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_base64, 0, 0, 1)
     314             :         ZEND_ARG_INFO(0, text)
     315             : ZEND_END_ARG_INFO()
     316             : 
     317             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_qprint, 0, 0, 1)
     318             :         ZEND_ARG_INFO(0, text)
     319             : ZEND_END_ARG_INFO()
     320             : 
     321             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_8bit, 0, 0, 1)
     322             :         ZEND_ARG_INFO(0, text)
     323             : ZEND_END_ARG_INFO()
     324             : 
     325             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_binary, 0, 0, 1)
     326             :         ZEND_ARG_INFO(0, text)
     327             : ZEND_END_ARG_INFO()
     328             : 
     329             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_mailboxmsginfo, 0, 0, 1)
     330             :         ZEND_ARG_INFO(0, stream_id)
     331             : ZEND_END_ARG_INFO()
     332             : 
     333             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_rfc822_write_address, 0, 0, 3)
     334             :         ZEND_ARG_INFO(0, mailbox)
     335             :         ZEND_ARG_INFO(0, host)
     336             :         ZEND_ARG_INFO(0, personal)
     337             : ZEND_END_ARG_INFO()
     338             : 
     339             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_rfc822_parse_adrlist, 0, 0, 2)
     340             :         ZEND_ARG_INFO(0, address_string)
     341             :         ZEND_ARG_INFO(0, default_host)
     342             : ZEND_END_ARG_INFO()
     343             : 
     344             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_utf8, 0, 0, 1)
     345             :         ZEND_ARG_INFO(0, mime_encoded_text)
     346             : ZEND_END_ARG_INFO()
     347             : 
     348             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_utf7_decode, 0, 0, 1)
     349             :         ZEND_ARG_INFO(0, buf)
     350             : ZEND_END_ARG_INFO()
     351             : 
     352             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_utf7_encode, 0, 0, 1)
     353             :         ZEND_ARG_INFO(0, buf)
     354             : ZEND_END_ARG_INFO()
     355             : 
     356             : #ifdef HAVE_IMAP_MUTF7
     357             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_utf8_to_mutf7, 0, 0, 1)
     358             :         ZEND_ARG_INFO(0, in)
     359             : ZEND_END_ARG_INFO()
     360             : 
     361             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_mutf7_to_utf8, 0, 0, 1)
     362             :         ZEND_ARG_INFO(0, in)
     363             : ZEND_END_ARG_INFO()
     364             : #endif
     365             : 
     366             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_setflag_full, 0, 0, 3)
     367             :         ZEND_ARG_INFO(0, stream_id)
     368             :         ZEND_ARG_INFO(0, sequence)
     369             :         ZEND_ARG_INFO(0, flag)
     370             :         ZEND_ARG_INFO(0, options)
     371             : ZEND_END_ARG_INFO()
     372             : 
     373             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_clearflag_full, 0, 0, 3)
     374             :         ZEND_ARG_INFO(0, stream_id)
     375             :         ZEND_ARG_INFO(0, sequence)
     376             :         ZEND_ARG_INFO(0, flag)
     377             :         ZEND_ARG_INFO(0, options)
     378             : ZEND_END_ARG_INFO()
     379             : 
     380             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_sort, 0, 0, 3)
     381             :         ZEND_ARG_INFO(0, stream_id)
     382             :         ZEND_ARG_INFO(0, criteria)
     383             :         ZEND_ARG_INFO(0, reverse)
     384             :         ZEND_ARG_INFO(0, options)
     385             :         ZEND_ARG_INFO(0, search_criteria)
     386             :         ZEND_ARG_INFO(0, charset)
     387             : ZEND_END_ARG_INFO()
     388             : 
     389             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_fetchheader, 0, 0, 2)
     390             :         ZEND_ARG_INFO(0, stream_id)
     391             :         ZEND_ARG_INFO(0, msg_no)
     392             :         ZEND_ARG_INFO(0, options)
     393             : ZEND_END_ARG_INFO()
     394             : 
     395             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_uid, 0, 0, 2)
     396             :         ZEND_ARG_INFO(0, stream_id)
     397             :         ZEND_ARG_INFO(0, msg_no)
     398             : ZEND_END_ARG_INFO()
     399             : 
     400             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_msgno, 0, 0, 2)
     401             :         ZEND_ARG_INFO(0, stream_id)
     402             :         ZEND_ARG_INFO(0, unique_msg_id)
     403             : ZEND_END_ARG_INFO()
     404             : 
     405             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_status, 0, 0, 3)
     406             :         ZEND_ARG_INFO(0, stream_id)
     407             :         ZEND_ARG_INFO(0, mailbox)
     408             :         ZEND_ARG_INFO(0, options)
     409             : ZEND_END_ARG_INFO()
     410             : 
     411             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_bodystruct, 0, 0, 3)
     412             :         ZEND_ARG_INFO(0, stream_id)
     413             :         ZEND_ARG_INFO(0, msg_no)
     414             :         ZEND_ARG_INFO(0, section)
     415             : ZEND_END_ARG_INFO()
     416             : 
     417             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_fetch_overview, 0, 0, 2)
     418             :         ZEND_ARG_INFO(0, stream_id)
     419             :         ZEND_ARG_INFO(0, sequence)
     420             :         ZEND_ARG_INFO(0, options)
     421             : ZEND_END_ARG_INFO()
     422             : 
     423             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_mail_compose, 0, 0, 2)
     424             :         ZEND_ARG_INFO(0, envelope)
     425             :         ZEND_ARG_INFO(0, body)
     426             : ZEND_END_ARG_INFO()
     427             : 
     428             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_mail, 0, 0, 3)
     429             :         ZEND_ARG_INFO(0, to)
     430             :         ZEND_ARG_INFO(0, subject)
     431             :         ZEND_ARG_INFO(0, message)
     432             :         ZEND_ARG_INFO(0, additional_headers)
     433             :         ZEND_ARG_INFO(0, cc)
     434             :         ZEND_ARG_INFO(0, bcc)
     435             :         ZEND_ARG_INFO(0, rpath)
     436             : ZEND_END_ARG_INFO()
     437             : 
     438             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_search, 0, 0, 2)
     439             :         ZEND_ARG_INFO(0, stream_id)
     440             :         ZEND_ARG_INFO(0, criteria)
     441             :         ZEND_ARG_INFO(0, options)
     442             :         ZEND_ARG_INFO(0, charset)
     443             : ZEND_END_ARG_INFO()
     444             : 
     445             : ZEND_BEGIN_ARG_INFO(arginfo_imap_alerts, 0)
     446             : ZEND_END_ARG_INFO()
     447             : 
     448             : ZEND_BEGIN_ARG_INFO(arginfo_imap_errors, 0)
     449             : ZEND_END_ARG_INFO()
     450             : 
     451             : ZEND_BEGIN_ARG_INFO(arginfo_imap_last_error, 0)
     452             : ZEND_END_ARG_INFO()
     453             : 
     454             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_mime_header_decode, 0, 0, 1)
     455             :         ZEND_ARG_INFO(0, str)
     456             : ZEND_END_ARG_INFO()
     457             : 
     458             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_thread, 0, 0, 1)
     459             :         ZEND_ARG_INFO(0, stream_id)
     460             :         ZEND_ARG_INFO(0, options)
     461             : ZEND_END_ARG_INFO()
     462             : 
     463             : ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_timeout, 0, 0, 1)
     464             :         ZEND_ARG_INFO(0, timeout_type)
     465             :         ZEND_ARG_INFO(0, timeout)
     466             : ZEND_END_ARG_INFO()
     467             : /* }}} */
     468             : 
     469             : /* {{{ imap_functions[]
     470             :  */
     471             : const zend_function_entry imap_functions[] = {
     472             :         PHP_FE(imap_open,                                                               arginfo_imap_open)
     473             :         PHP_FE(imap_reopen,                                                             arginfo_imap_reopen)
     474             :         PHP_FE(imap_close,                                                              arginfo_imap_close)
     475             :         PHP_FE(imap_num_msg,                                                    arginfo_imap_num_msg)
     476             :         PHP_FE(imap_num_recent,                                                 arginfo_imap_num_recent)
     477             :         PHP_FE(imap_headers,                                                    arginfo_imap_headers)
     478             :         PHP_FE(imap_headerinfo,                                                 arginfo_imap_headerinfo)
     479             :         PHP_FE(imap_rfc822_parse_headers,                               arginfo_imap_rfc822_parse_headers)
     480             :         PHP_FE(imap_rfc822_write_address,                               arginfo_imap_rfc822_write_address)
     481             :         PHP_FE(imap_rfc822_parse_adrlist,                               arginfo_imap_rfc822_parse_adrlist)
     482             :         PHP_FE(imap_body,                                                               arginfo_imap_body)
     483             :         PHP_FE(imap_bodystruct,                                                 arginfo_imap_bodystruct)
     484             :         PHP_FE(imap_fetchbody,                                                  arginfo_imap_fetchbody)
     485             :         PHP_FE(imap_fetchmime,                                                  arginfo_imap_fetchbody)
     486             :         PHP_FE(imap_savebody,                                                   arginfo_imap_savebody)
     487             :         PHP_FE(imap_fetchheader,                                                arginfo_imap_fetchheader)
     488             :         PHP_FE(imap_fetchstructure,                                             arginfo_imap_fetchstructure)
     489             :         PHP_FE(imap_gc,                                                                         arginfo_imap_gc)
     490             :         PHP_FE(imap_expunge,                                                    arginfo_imap_expunge)
     491             :         PHP_FE(imap_delete,                                                             arginfo_imap_delete)
     492             :         PHP_FE(imap_undelete,                                                   arginfo_imap_undelete)
     493             :         PHP_FE(imap_check,                                                              arginfo_imap_check)
     494             :         PHP_FE(imap_listscan,                                                   arginfo_imap_listscan)
     495             :         PHP_FE(imap_mail_copy,                                                  arginfo_imap_mail_copy)
     496             :         PHP_FE(imap_mail_move,                                                  arginfo_imap_mail_move)
     497             :         PHP_FE(imap_mail_compose,                                               arginfo_imap_mail_compose)
     498             :         PHP_FE(imap_createmailbox,                                              arginfo_imap_createmailbox)
     499             :         PHP_FE(imap_renamemailbox,                                              arginfo_imap_renamemailbox)
     500             :         PHP_FE(imap_deletemailbox,                                              arginfo_imap_deletemailbox)
     501             :         PHP_FE(imap_subscribe,                                                  arginfo_imap_subscribe)
     502             :         PHP_FE(imap_unsubscribe,                                                arginfo_imap_unsubscribe)
     503             :         PHP_FE(imap_append,                                                             arginfo_imap_append)
     504             :         PHP_FE(imap_ping,                                                               arginfo_imap_ping)
     505             :         PHP_FE(imap_base64,                                                             arginfo_imap_base64)
     506             :         PHP_FE(imap_qprint,                                                             arginfo_imap_qprint)
     507             :         PHP_FE(imap_8bit,                                                               arginfo_imap_8bit)
     508             :         PHP_FE(imap_binary,                                                             arginfo_imap_binary)
     509             :         PHP_FE(imap_utf8,                                                               arginfo_imap_utf8)
     510             :         PHP_FE(imap_status,                                                             arginfo_imap_status)
     511             :         PHP_FE(imap_mailboxmsginfo,                                             arginfo_imap_mailboxmsginfo)
     512             :         PHP_FE(imap_setflag_full,                                               arginfo_imap_setflag_full)
     513             :         PHP_FE(imap_clearflag_full,                                             arginfo_imap_clearflag_full)
     514             :         PHP_FE(imap_sort,                                                               arginfo_imap_sort)
     515             :         PHP_FE(imap_uid,                                                                arginfo_imap_uid)
     516             :         PHP_FE(imap_msgno,                                                              arginfo_imap_msgno)
     517             :         PHP_FE(imap_list,                                                               arginfo_imap_list)
     518             :         PHP_FE(imap_lsub,                                                               arginfo_imap_lsub)
     519             :         PHP_FE(imap_fetch_overview,                                             arginfo_imap_fetch_overview)
     520             :         PHP_FE(imap_alerts,                                                             arginfo_imap_alerts)
     521             :         PHP_FE(imap_errors,                                                             arginfo_imap_errors)
     522             :         PHP_FE(imap_last_error,                                                 arginfo_imap_last_error)
     523             :         PHP_FE(imap_search,                                                             arginfo_imap_search)
     524             :         PHP_FE(imap_utf7_decode,                                                arginfo_imap_utf7_decode)
     525             :         PHP_FE(imap_utf7_encode,                                                arginfo_imap_utf7_encode)
     526             : #ifdef HAVE_IMAP_MUTF7
     527             :         PHP_FE(imap_utf8_to_mutf7,                                              arginfo_imap_utf8_to_mutf7)
     528             :         PHP_FE(imap_mutf7_to_utf8,                                              arginfo_imap_mutf7_to_utf8)
     529             : #endif
     530             :         PHP_FE(imap_mime_header_decode,                                 arginfo_imap_mime_header_decode)
     531             :         PHP_FE(imap_thread,                                                             arginfo_imap_thread)
     532             :         PHP_FE(imap_timeout,                                                            arginfo_imap_timeout)
     533             : 
     534             : #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
     535             :         PHP_FE(imap_get_quota,                                                  arginfo_imap_get_quota)
     536             :         PHP_FE(imap_get_quotaroot,                                              arginfo_imap_get_quotaroot)
     537             :         PHP_FE(imap_set_quota,                                                  arginfo_imap_set_quota)
     538             :         PHP_FE(imap_setacl,                                                             arginfo_imap_setacl)
     539             :         PHP_FE(imap_getacl,                                                             arginfo_imap_getacl)
     540             : #endif
     541             : 
     542             :         PHP_FE(imap_mail,                                                               arginfo_imap_mail)
     543             : 
     544             :         PHP_FALIAS(imap_header,                 imap_headerinfo,        arginfo_imap_headerinfo)
     545             :         PHP_FALIAS(imap_listmailbox,    imap_list,                      arginfo_imap_list)
     546             :         PHP_FALIAS(imap_getmailboxes,   imap_list_full,         arginfo_imap_getmailboxes)
     547             :         PHP_FALIAS(imap_scanmailbox,    imap_listscan,          arginfo_imap_listscan)
     548             :         PHP_FALIAS(imap_listsubscribed, imap_lsub,                      arginfo_imap_lsub)
     549             :         PHP_FALIAS(imap_getsubscribed,  imap_lsub_full,         arginfo_imap_getsubscribed)
     550             :         PHP_FALIAS(imap_fetchtext,              imap_body,                      arginfo_imap_body)
     551             :         PHP_FALIAS(imap_scan,                   imap_listscan,          arginfo_imap_listscan)
     552             :         PHP_FALIAS(imap_create,                 imap_createmailbox,     arginfo_imap_createmailbox)
     553             :         PHP_FALIAS(imap_rename,                 imap_renamemailbox,     arginfo_imap_renamemailbox)
     554             :         PHP_FE_END
     555             : };
     556             : /* }}} */
     557             : 
     558             : /* {{{ imap dependencies */
     559             : static const zend_module_dep imap_deps[] = {
     560             :         ZEND_MOD_REQUIRED("standard")
     561             :         ZEND_MOD_END
     562             : };
     563             : /* }}} */
     564             : 
     565             : /* {{{ imap_module_entry
     566             :  */
     567             : zend_module_entry imap_module_entry = {
     568             :         STANDARD_MODULE_HEADER_EX, NULL,
     569             :         imap_deps,
     570             :         "imap",
     571             :         imap_functions,
     572             :         PHP_MINIT(imap),
     573             :         NULL,
     574             :         PHP_RINIT(imap),
     575             :         PHP_RSHUTDOWN(imap),
     576             :         PHP_MINFO(imap),
     577             :         NO_VERSION_YET,
     578             :         PHP_MODULE_GLOBALS(imap),
     579             :         PHP_GINIT(imap),
     580             :         NULL,
     581             :         NULL,
     582             :         STANDARD_MODULE_PROPERTIES_EX
     583             : };
     584             : /* }}} */
     585             : 
     586             : #ifdef COMPILE_DL_IMAP
     587             : ZEND_GET_MODULE(imap)
     588             : #endif
     589             : 
     590             : /* True globals, no need for thread safety */
     591             : static int le_imap;
     592             : 
     593             : #define PHP_IMAP_CHECK_MSGNO(msgindex)  \
     594             :         if ((msgindex < 1) || ((unsigned) msgindex > imap_le_struct->imap_stream->nmsgs)) { \
     595             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad message number");    \
     596             :                 RETURN_FALSE;   \
     597             :         }       \
     598             : 
     599             : /* {{{ mail_close_it
     600             :  */
     601         177 : static void mail_close_it(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     602             : {
     603         177 :         pils *imap_le_struct = (pils *)rsrc->ptr;
     604             : 
     605             :         /* Do not try to close prototype streams */
     606         177 :         if (!(imap_le_struct->flags & OP_PROTOTYPE)) {
     607         177 :                 mail_close_full(imap_le_struct->imap_stream, imap_le_struct->flags);
     608             :         }
     609             : 
     610         177 :         if (IMAPG(imap_user)) {
     611         176 :                 efree(IMAPG(imap_user));
     612         176 :                 IMAPG(imap_user) = 0;
     613             :         }
     614         177 :         if (IMAPG(imap_password)) {
     615         176 :                 efree(IMAPG(imap_password));
     616         176 :                 IMAPG(imap_password) = 0;
     617             :         }
     618             : 
     619         177 :         efree(imap_le_struct);
     620         177 : }
     621             : /* }}} */
     622             : 
     623             : /* {{{ add_assoc_object
     624             :  */
     625          10 : static int add_assoc_object(zval *arg, char *key, zval *tmp TSRMLS_DC)
     626             : {
     627             :         HashTable *symtable;
     628             : 
     629          10 :         if (Z_TYPE_P(arg) == IS_OBJECT) {
     630          10 :                 symtable = Z_OBJPROP_P(arg);
     631             :         } else {
     632           0 :                 symtable = Z_ARRVAL_P(arg);
     633             :         }
     634          10 :         return zend_hash_update(symtable, key, strlen(key)+1, (void *) &tmp, sizeof(zval *), NULL);
     635             : }
     636             : /* }}} */
     637             : 
     638             : /* {{{ add_next_index_object
     639             :  */
     640         107 : static inline int add_next_index_object(zval *arg, zval *tmp TSRMLS_DC)
     641             : {
     642             :         HashTable *symtable;
     643             : 
     644         107 :         if (Z_TYPE_P(arg) == IS_OBJECT) {
     645           0 :                 symtable = Z_OBJPROP_P(arg);
     646             :         } else {
     647         107 :                 symtable = Z_ARRVAL_P(arg);
     648             :         }
     649             : 
     650         107 :         return zend_hash_next_index_insert(symtable, (void *) &tmp, sizeof(zval *), NULL);
     651             : }
     652             : /* }}} */
     653             : 
     654             : /* {{{ mail_newfolderobjectlist
     655             :  *
     656             :  * Mail instantiate FOBJECTLIST
     657             :  * Returns: new FOBJECTLIST list
     658             :  * Author: CJH
     659             :  */
     660          68 : FOBJECTLIST *mail_newfolderobjectlist(void)
     661             : {
     662          68 :         return (FOBJECTLIST *) memset(fs_get(sizeof(FOBJECTLIST)), 0, sizeof(FOBJECTLIST));
     663             : }
     664             : /* }}} */
     665             : 
     666             : /* {{{ mail_free_foblist
     667             :  *
     668             :  * Mail garbage collect FOBJECTLIST
     669             :  * Accepts: pointer to FOBJECTLIST pointer
     670             :  * Author: CJH
     671             :  */
     672          34 : void mail_free_foblist(FOBJECTLIST **foblist, FOBJECTLIST **tail)
     673             : {
     674             :         FOBJECTLIST *cur, *next;
     675             : 
     676         102 :         for (cur=*foblist, next=cur->next; cur; cur=next) {
     677          68 :                 next = cur->next;
     678             : 
     679          68 :                 if(cur->text.data)
     680          68 :                         fs_give((void **)&(cur->text.data));
     681             : 
     682          68 :                 fs_give((void **)&cur);
     683             :         }
     684             : 
     685          34 :         *tail = NIL;
     686          34 :         *foblist = NIL;
     687          34 : }
     688             : /* }}} */
     689             : 
     690             : /* {{{ mail_newerrorlist
     691             :  *
     692             :  * Mail instantiate ERRORLIST
     693             :  * Returns: new ERRORLIST list
     694             :  * Author: CJH
     695             :  */
     696          22 : ERRORLIST *mail_newerrorlist(void)
     697             : {
     698          22 :         return (ERRORLIST *) memset(fs_get(sizeof(ERRORLIST)), 0, sizeof(ERRORLIST));
     699             : }
     700             : /* }}} */
     701             : 
     702             : /* {{{ mail_free_errorlist
     703             :  *
     704             :  * Mail garbage collect FOBJECTLIST
     705             :  * Accepts: pointer to FOBJECTLIST pointer
     706             :  * Author: CJH
     707             :  */
     708          28 : void mail_free_errorlist(ERRORLIST **errlist)
     709             : {
     710          28 :         if (*errlist) {         /* only free if exists */
     711          22 :                 if ((*errlist)->text.data) {
     712          22 :                         fs_give((void **) &(*errlist)->text.data);
     713             :                 }
     714          22 :                 mail_free_errorlist (&(*errlist)->next);
     715          22 :                 fs_give((void **) errlist);     /* return string to free storage */
     716             :         }
     717          28 : }
     718             : /* }}} */
     719             : 
     720             : /* {{{ mail_newmessagelist
     721             :  *
     722             :  * Mail instantiate MESSAGELIST
     723             :  * Returns: new MESSAGELIST list
     724             :  * Author: CJH
     725             :  */
     726          21 : MESSAGELIST *mail_newmessagelist(void)
     727             : {
     728          21 :         return (MESSAGELIST *) memset(fs_get(sizeof(MESSAGELIST)), 0, sizeof(MESSAGELIST));
     729             : }
     730             : /* }}} */
     731             : 
     732             : /* {{{ mail_free_messagelist
     733             :  *
     734             :  * Mail garbage collect MESSAGELIST
     735             :  * Accepts: pointer to MESSAGELIST pointer
     736             :  * Author: CJH
     737             :  */
     738           7 : void mail_free_messagelist(MESSAGELIST **msglist, MESSAGELIST **tail)
     739             : {
     740             :         MESSAGELIST *cur, *next;
     741             : 
     742          28 :         for (cur = *msglist, next = cur->next; cur; cur = next) {
     743          21 :                 next = cur->next;
     744          21 :                 fs_give((void **)&cur);
     745             :         }
     746             : 
     747           7 :         *tail = NIL;
     748           7 :         *msglist = NIL;
     749           7 : }
     750             : /* }}} */
     751             : 
     752             : #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
     753             : /* {{{ mail_getquota
     754             :  *
     755             :  * Mail GET_QUOTA callback
     756             :  * Called via the mail_parameter function in c-client:src/c-client/mail.c
     757             :  * Author DRK
     758             :  */
     759             : 
     760           0 : void mail_getquota(MAILSTREAM *stream, char *qroot, QUOTALIST *qlist)
     761             : {
     762             :         zval *t_map, *return_value;
     763             :         TSRMLS_FETCH();
     764             : 
     765           0 :         return_value = *IMAPG(quota_return);
     766             : 
     767             : /* put parsing code here */
     768           0 :         for(; qlist; qlist = qlist->next) {
     769           0 :                 MAKE_STD_ZVAL(t_map);
     770           0 :                 array_init(t_map);
     771           0 :                 if (strncmp(qlist->name, "STORAGE", 7) == 0)
     772             :                 {
     773             :                         /* this is to add backwards compatibility */
     774           0 :                         add_assoc_long_ex(return_value, "usage", sizeof("usage"), qlist->usage);
     775           0 :                         add_assoc_long_ex(return_value, "limit", sizeof("limit"), qlist->limit);
     776             :                 }
     777             : 
     778           0 :                 add_assoc_long_ex(t_map, "usage", sizeof("usage"), qlist->usage);
     779           0 :                 add_assoc_long_ex(t_map, "limit", sizeof("limit"), qlist->limit);
     780           0 :                 add_assoc_zval_ex(return_value, qlist->name, strlen(qlist->name)+1, t_map);
     781             :         }
     782           0 : }
     783             : /* }}} */
     784             : 
     785             : /* {{{ mail_getquota
     786             :  *
     787             :  * Mail GET_ACL callback
     788             :  * Called via the mail_parameter function in c-client:src/c-client/mail.c
     789             :  */
     790           0 : void mail_getacl(MAILSTREAM *stream, char *mailbox, ACLLIST *alist)
     791             : {
     792             :         TSRMLS_FETCH();
     793             : 
     794             :         /* walk through the ACLLIST */
     795           0 :         for(; alist; alist = alist->next) {
     796           0 :                 add_assoc_stringl(IMAPG(imap_acl_list), alist->identifier, alist->rights, strlen(alist->rights), 1);
     797             :         }
     798           0 : }
     799             : /* }}} */
     800             : #endif
     801             : 
     802             : /* {{{ PHP_GINIT_FUNCTION
     803             :  */
     804       19341 : static PHP_GINIT_FUNCTION(imap)
     805             : {
     806       19341 :         imap_globals->imap_user = NIL;
     807       19341 :         imap_globals->imap_password = NIL;
     808             : 
     809       19341 :         imap_globals->imap_alertstack = NIL;
     810       19341 :         imap_globals->imap_errorstack = NIL;
     811             : 
     812       19341 :         imap_globals->imap_folders = NIL;
     813       19341 :         imap_globals->imap_folders_tail = NIL;
     814       19341 :         imap_globals->imap_sfolders = NIL;
     815       19341 :         imap_globals->imap_sfolders_tail = NIL;
     816       19341 :         imap_globals->imap_messages = NIL;
     817       19341 :         imap_globals->imap_messages_tail = NIL;
     818       19341 :         imap_globals->imap_folder_objects = NIL;
     819       19341 :         imap_globals->imap_folder_objects_tail = NIL;
     820       19341 :         imap_globals->imap_sfolder_objects = NIL;
     821       19341 :         imap_globals->imap_sfolder_objects_tail = NIL;
     822             : 
     823       19341 :         imap_globals->folderlist_style = FLIST_ARRAY;
     824             : #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
     825       19341 :         imap_globals->quota_return = NIL;
     826       19341 :         imap_globals->imap_acl_list = NIL;
     827             : #endif
     828       19341 :         imap_globals->gets_stream = NIL;
     829       19341 : }
     830             : /* }}} */
     831             : 
     832             : /* {{{ PHP_MINIT_FUNCTION
     833             :  */
     834       19341 : PHP_MINIT_FUNCTION(imap)
     835             : {
     836       19341 :         unsigned long sa_all =  SA_MESSAGES | SA_RECENT | SA_UNSEEN | SA_UIDNEXT | SA_UIDVALIDITY;
     837             : 
     838             : #ifndef PHP_WIN32
     839       19341 :         mail_link(&unixdriver);             /* link in the unix driver */
     840       19341 :         mail_link(&mhdriver);               /* link in the mh driver */
     841             :         /* mail_link(&mxdriver); */ /* According to c-client docs (internal.txt) this shouldn't be used. */
     842       19341 :         mail_link(&mmdfdriver);             /* link in the mmdf driver */
     843       19341 :         mail_link(&newsdriver);             /* link in the news driver */
     844       19341 :         mail_link(&philedriver);    /* link in the phile driver */
     845             : #endif
     846       19341 :         mail_link(&imapdriver);             /* link in the imap driver */
     847       19341 :         mail_link(&nntpdriver);             /* link in the nntp driver */
     848       19341 :         mail_link(&pop3driver);             /* link in the pop3 driver */
     849       19341 :         mail_link(&mbxdriver);              /* link in the mbx driver */
     850       19341 :         mail_link(&tenexdriver);    /* link in the tenex driver */
     851       19341 :         mail_link(&mtxdriver);              /* link in the mtx driver */
     852       19341 :         mail_link(&dummydriver);    /* link in the dummy driver */
     853             : 
     854             : #ifndef PHP_WIN32
     855       19341 :         auth_link(&auth_log);               /* link in the log authenticator */
     856       19341 :         auth_link(&auth_md5);               /* link in the cram-md5 authenticator */
     857             : #if HAVE_IMAP_KRB && defined(HAVE_IMAP_AUTH_GSS)
     858       19341 :         auth_link(&auth_gss);               /* link in the gss authenticator */
     859             : #endif
     860       19341 :         auth_link(&auth_pla);               /* link in the plain authenticator */
     861             : #endif
     862             : 
     863             : #ifdef HAVE_IMAP_SSL
     864       19341 :         ssl_onceonlyinit ();
     865             : #endif
     866             : 
     867             :         /* lets allow NIL */
     868       19341 :         REGISTER_LONG_CONSTANT("NIL", NIL, CONST_PERSISTENT | CONST_CS);
     869             : 
     870             :         /* plug in our gets */
     871       19341 :         mail_parameters(NIL, SET_GETS, (void *) NIL);
     872             : 
     873             :         /* set default timeout values */
     874       19341 :         mail_parameters(NIL, SET_OPENTIMEOUT, (void *) FG(default_socket_timeout));
     875       19341 :         mail_parameters(NIL, SET_READTIMEOUT, (void *) FG(default_socket_timeout));
     876       19341 :         mail_parameters(NIL, SET_WRITETIMEOUT, (void *) FG(default_socket_timeout));
     877       19341 :         mail_parameters(NIL, SET_CLOSETIMEOUT, (void *) FG(default_socket_timeout));
     878             : 
     879             :         /* timeout constants */
     880       19341 :         REGISTER_LONG_CONSTANT("IMAP_OPENTIMEOUT", 1, CONST_PERSISTENT | CONST_CS);
     881       19341 :         REGISTER_LONG_CONSTANT("IMAP_READTIMEOUT", 2, CONST_PERSISTENT | CONST_CS);
     882       19341 :         REGISTER_LONG_CONSTANT("IMAP_WRITETIMEOUT", 3, CONST_PERSISTENT | CONST_CS);
     883       19341 :         REGISTER_LONG_CONSTANT("IMAP_CLOSETIMEOUT", 4, CONST_PERSISTENT | CONST_CS);
     884             : 
     885             :         /* Open Options */
     886             : 
     887       19341 :         REGISTER_LONG_CONSTANT("OP_DEBUG", OP_DEBUG, CONST_PERSISTENT | CONST_CS);
     888             :         /* debug protocol negotiations */
     889       19341 :         REGISTER_LONG_CONSTANT("OP_READONLY", OP_READONLY, CONST_PERSISTENT | CONST_CS);
     890             :         /* read-only open */
     891       19341 :         REGISTER_LONG_CONSTANT("OP_ANONYMOUS", OP_ANONYMOUS, CONST_PERSISTENT | CONST_CS);
     892             :         /* anonymous open of newsgroup */
     893       19341 :         REGISTER_LONG_CONSTANT("OP_SHORTCACHE", OP_SHORTCACHE, CONST_PERSISTENT | CONST_CS);
     894             :         /* short (elt-only) caching */
     895       19341 :         REGISTER_LONG_CONSTANT("OP_SILENT", OP_SILENT, CONST_PERSISTENT | CONST_CS);
     896             :         /* don't pass up events (internal use) */
     897       19341 :         REGISTER_LONG_CONSTANT("OP_PROTOTYPE", OP_PROTOTYPE, CONST_PERSISTENT | CONST_CS);
     898             :         /* return driver prototype */
     899       19341 :         REGISTER_LONG_CONSTANT("OP_HALFOPEN", OP_HALFOPEN, CONST_PERSISTENT | CONST_CS);
     900             :         /* half-open (IMAP connect but no select) */
     901       19341 :         REGISTER_LONG_CONSTANT("OP_EXPUNGE", OP_EXPUNGE, CONST_PERSISTENT | CONST_CS);
     902             :         /* silently expunge recycle stream */
     903       19341 :         REGISTER_LONG_CONSTANT("OP_SECURE", OP_SECURE, CONST_PERSISTENT | CONST_CS);
     904             :         /* don't do non-secure authentication */
     905             : 
     906             :         /*
     907             :         PHP re-assigns CL_EXPUNGE a custom value that can be used as part of the imap_open() bitfield
     908             :         because it seems like a good idea to be able to indicate that the mailbox should be
     909             :         automatically expunged during imap_open in case the script get interrupted and it doesn't get
     910             :         to the imap_close() where this option is normally placed.  If the c-client library adds other
     911             :         options and the value for this one conflicts, simply make PHP_EXPUNGE higher at the top of
     912             :         this file
     913             :         */
     914       19341 :         REGISTER_LONG_CONSTANT("CL_EXPUNGE", PHP_EXPUNGE, CONST_PERSISTENT | CONST_CS);
     915             :         /* expunge silently */
     916             : 
     917             :         /* Fetch options */
     918             : 
     919       19341 :         REGISTER_LONG_CONSTANT("FT_UID", FT_UID, CONST_PERSISTENT | CONST_CS);
     920             :         /* argument is a UID */
     921       19341 :         REGISTER_LONG_CONSTANT("FT_PEEK", FT_PEEK, CONST_PERSISTENT | CONST_CS);
     922             :         /* peek at data */
     923       19341 :         REGISTER_LONG_CONSTANT("FT_NOT", FT_NOT, CONST_PERSISTENT | CONST_CS);
     924             :         /* NOT flag for header lines fetch */
     925       19341 :         REGISTER_LONG_CONSTANT("FT_INTERNAL", FT_INTERNAL, CONST_PERSISTENT | CONST_CS);
     926             :         /* text can be internal strings */
     927       19341 :         REGISTER_LONG_CONSTANT("FT_PREFETCHTEXT", FT_PREFETCHTEXT, CONST_PERSISTENT | CONST_CS);
     928             :         /* IMAP prefetch text when fetching header */
     929             : 
     930             :         /* Flagging options */
     931             : 
     932       19341 :         REGISTER_LONG_CONSTANT("ST_UID", ST_UID, CONST_PERSISTENT | CONST_CS);
     933             :         /* argument is a UID sequence */
     934       19341 :         REGISTER_LONG_CONSTANT("ST_SILENT", ST_SILENT, CONST_PERSISTENT | CONST_CS);
     935             :         /* don't return results */
     936       19341 :         REGISTER_LONG_CONSTANT("ST_SET", ST_SET, CONST_PERSISTENT | CONST_CS);
     937             :         /* set vs. clear */
     938             : 
     939             :         /* Copy options */
     940             : 
     941       19341 :         REGISTER_LONG_CONSTANT("CP_UID", CP_UID, CONST_PERSISTENT | CONST_CS);
     942             :         /* argument is a UID sequence */
     943       19341 :         REGISTER_LONG_CONSTANT("CP_MOVE", CP_MOVE, CONST_PERSISTENT | CONST_CS);
     944             :         /* delete from source after copying */
     945             : 
     946             :         /* Search/sort options */
     947             : 
     948       19341 :         REGISTER_LONG_CONSTANT("SE_UID", SE_UID, CONST_PERSISTENT | CONST_CS);
     949             :         /* return UID */
     950       19341 :         REGISTER_LONG_CONSTANT("SE_FREE", SE_FREE, CONST_PERSISTENT | CONST_CS);
     951             :         /* free search program after finished */
     952       19341 :         REGISTER_LONG_CONSTANT("SE_NOPREFETCH", SE_NOPREFETCH, CONST_PERSISTENT | CONST_CS);
     953             :         /* no search prefetching */
     954       19341 :         REGISTER_LONG_CONSTANT("SO_FREE", SO_FREE, CONST_PERSISTENT | CONST_CS);
     955             :         /* free sort program after finished */
     956       19341 :         REGISTER_LONG_CONSTANT("SO_NOSERVER", SO_NOSERVER, CONST_PERSISTENT | CONST_CS);
     957             :         /* don't do server-based sort */
     958             : 
     959             :         /* Status options */
     960             : 
     961       19341 :         REGISTER_LONG_CONSTANT("SA_MESSAGES", SA_MESSAGES , CONST_PERSISTENT | CONST_CS);
     962             :         /* number of messages */
     963       19341 :         REGISTER_LONG_CONSTANT("SA_RECENT", SA_RECENT, CONST_PERSISTENT | CONST_CS);
     964             :         /* number of recent messages */
     965       19341 :         REGISTER_LONG_CONSTANT("SA_UNSEEN", SA_UNSEEN , CONST_PERSISTENT | CONST_CS);
     966             :         /* number of unseen messages */
     967       19341 :         REGISTER_LONG_CONSTANT("SA_UIDNEXT", SA_UIDNEXT, CONST_PERSISTENT | CONST_CS);
     968             :         /* next UID to be assigned */
     969       19341 :         REGISTER_LONG_CONSTANT("SA_UIDVALIDITY", SA_UIDVALIDITY , CONST_PERSISTENT | CONST_CS);
     970             :         /* UID validity value */
     971       19341 :         REGISTER_LONG_CONSTANT("SA_ALL", sa_all, CONST_PERSISTENT | CONST_CS);
     972             :         /* get all status information */
     973             : 
     974             :         /* Bits for mm_list() and mm_lsub() */
     975             : 
     976       19341 :         REGISTER_LONG_CONSTANT("LATT_NOINFERIORS", LATT_NOINFERIORS , CONST_PERSISTENT | CONST_CS);
     977       19341 :         REGISTER_LONG_CONSTANT("LATT_NOSELECT", LATT_NOSELECT, CONST_PERSISTENT | CONST_CS);
     978       19341 :         REGISTER_LONG_CONSTANT("LATT_MARKED", LATT_MARKED, CONST_PERSISTENT | CONST_CS);
     979       19341 :         REGISTER_LONG_CONSTANT("LATT_UNMARKED", LATT_UNMARKED , CONST_PERSISTENT | CONST_CS);
     980             : 
     981             : #ifdef LATT_REFERRAL
     982       19341 :         REGISTER_LONG_CONSTANT("LATT_REFERRAL", LATT_REFERRAL, CONST_PERSISTENT | CONST_CS);
     983             : #endif
     984             : 
     985             : #ifdef LATT_HASCHILDREN
     986       19341 :         REGISTER_LONG_CONSTANT("LATT_HASCHILDREN", LATT_HASCHILDREN, CONST_PERSISTENT | CONST_CS);
     987             : #endif
     988             : 
     989             : #ifdef LATT_HASNOCHILDREN
     990       19341 :         REGISTER_LONG_CONSTANT("LATT_HASNOCHILDREN", LATT_HASNOCHILDREN, CONST_PERSISTENT | CONST_CS);
     991             : #endif
     992             : 
     993             :         /* Sort functions */
     994             : 
     995       19341 :         REGISTER_LONG_CONSTANT("SORTDATE", SORTDATE , CONST_PERSISTENT | CONST_CS);
     996             :         /* date */
     997       19341 :         REGISTER_LONG_CONSTANT("SORTARRIVAL", SORTARRIVAL , CONST_PERSISTENT | CONST_CS);
     998             :         /* arrival date */
     999       19341 :         REGISTER_LONG_CONSTANT("SORTFROM", SORTFROM , CONST_PERSISTENT | CONST_CS);
    1000             :         /* from */
    1001       19341 :         REGISTER_LONG_CONSTANT("SORTSUBJECT", SORTSUBJECT , CONST_PERSISTENT | CONST_CS);
    1002             :         /* subject */
    1003       19341 :         REGISTER_LONG_CONSTANT("SORTTO", SORTTO , CONST_PERSISTENT | CONST_CS);
    1004             :         /* to */
    1005       19341 :         REGISTER_LONG_CONSTANT("SORTCC", SORTCC , CONST_PERSISTENT | CONST_CS);
    1006             :         /* cc */
    1007       19341 :         REGISTER_LONG_CONSTANT("SORTSIZE", SORTSIZE , CONST_PERSISTENT | CONST_CS);
    1008             :         /* size */
    1009             : 
    1010       19341 :         REGISTER_LONG_CONSTANT("TYPETEXT", TYPETEXT , CONST_PERSISTENT | CONST_CS);
    1011       19341 :         REGISTER_LONG_CONSTANT("TYPEMULTIPART", TYPEMULTIPART , CONST_PERSISTENT | CONST_CS);
    1012       19341 :         REGISTER_LONG_CONSTANT("TYPEMESSAGE", TYPEMESSAGE , CONST_PERSISTENT | CONST_CS);
    1013       19341 :         REGISTER_LONG_CONSTANT("TYPEAPPLICATION", TYPEAPPLICATION , CONST_PERSISTENT | CONST_CS);
    1014       19341 :         REGISTER_LONG_CONSTANT("TYPEAUDIO", TYPEAUDIO , CONST_PERSISTENT | CONST_CS);
    1015       19341 :         REGISTER_LONG_CONSTANT("TYPEIMAGE", TYPEIMAGE , CONST_PERSISTENT | CONST_CS);
    1016       19341 :         REGISTER_LONG_CONSTANT("TYPEVIDEO", TYPEVIDEO , CONST_PERSISTENT | CONST_CS);
    1017       19341 :         REGISTER_LONG_CONSTANT("TYPEMODEL", TYPEMODEL , CONST_PERSISTENT | CONST_CS);
    1018       19341 :         REGISTER_LONG_CONSTANT("TYPEOTHER", TYPEOTHER , CONST_PERSISTENT | CONST_CS);
    1019             :         /*
    1020             :         TYPETEXT                unformatted text
    1021             :         TYPEMULTIPART           multiple part
    1022             :         TYPEMESSAGE             encapsulated message
    1023             :         TYPEAPPLICATION         application data
    1024             :         TYPEAUDIO               audio
    1025             :         TYPEIMAGE               static image (GIF, JPEG, etc.)
    1026             :         TYPEVIDEO               video
    1027             :         TYPEMODEL               model
    1028             :         TYPEOTHER               unknown
    1029             :         */
    1030             : 
    1031       19341 :         REGISTER_LONG_CONSTANT("ENC7BIT", ENC7BIT , CONST_PERSISTENT | CONST_CS);
    1032       19341 :         REGISTER_LONG_CONSTANT("ENC8BIT", ENC8BIT , CONST_PERSISTENT | CONST_CS);
    1033       19341 :         REGISTER_LONG_CONSTANT("ENCBINARY", ENCBINARY , CONST_PERSISTENT | CONST_CS);
    1034       19341 :         REGISTER_LONG_CONSTANT("ENCBASE64", ENCBASE64, CONST_PERSISTENT | CONST_CS);
    1035       19341 :         REGISTER_LONG_CONSTANT("ENCQUOTEDPRINTABLE", ENCQUOTEDPRINTABLE , CONST_PERSISTENT | CONST_CS);
    1036       19341 :         REGISTER_LONG_CONSTANT("ENCOTHER", ENCOTHER , CONST_PERSISTENT | CONST_CS);
    1037             :         /*
    1038             :         ENC7BIT                 7 bit SMTP semantic data
    1039             :         ENC8BIT                 8 bit SMTP semantic data
    1040             :         ENCBINARY               8 bit binary data
    1041             :         ENCBASE64               base-64 encoded data
    1042             :         ENCQUOTEDPRINTABLE      human-readable 8-as-7 bit data
    1043             :         ENCOTHER                unknown
    1044             :         */
    1045             : 
    1046       19341 :         REGISTER_LONG_CONSTANT("IMAP_GC_ELT", GC_ELT , CONST_PERSISTENT | CONST_CS);
    1047       19341 :         REGISTER_LONG_CONSTANT("IMAP_GC_ENV", GC_ENV , CONST_PERSISTENT | CONST_CS);
    1048       19341 :         REGISTER_LONG_CONSTANT("IMAP_GC_TEXTS", GC_TEXTS , CONST_PERSISTENT | CONST_CS);
    1049             :         /*
    1050             :         GC_ELT                 message cache elements
    1051             :         GC_ENV                 ENVELOPEs and BODYs
    1052             :         GC_TEXTS               texts
    1053             :         */
    1054             : 
    1055       19341 :         le_imap = zend_register_list_destructors_ex(mail_close_it, NULL, "imap", module_number);
    1056       19341 :         return SUCCESS;
    1057             : }
    1058             : /* }}} */
    1059             : 
    1060             : /* {{{ PHP_RINIT_FUNCTION
    1061             :  */
    1062       19327 : PHP_RINIT_FUNCTION(imap)
    1063             : {
    1064       19327 :         IMAPG(imap_errorstack) = NIL;
    1065       19327 :         IMAPG(imap_alertstack) = NIL;
    1066       19327 :         IMAPG(gets_stream) = NIL;
    1067       19327 :         return SUCCESS;
    1068             : }
    1069             : /* }}} */
    1070             : 
    1071             : /* {{{ PHP_RSHUTDOWN_FUNCTION
    1072             :  */
    1073       19362 : PHP_RSHUTDOWN_FUNCTION(imap)
    1074             : {
    1075       19362 :         ERRORLIST *ecur = NIL;
    1076       19362 :         STRINGLIST *acur = NIL;
    1077             : 
    1078       19362 :         if (IMAPG(imap_errorstack) != NIL) {
    1079             :                 /* output any remaining errors at their original error level */
    1080           2 :                 if (EG(error_reporting) & E_NOTICE) {
    1081           2 :                         ecur = IMAPG(imap_errorstack);
    1082           6 :                         while (ecur != NIL) {
    1083           2 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s (errflg=%ld)", ecur->LTEXT, ecur->errflg);
    1084           2 :                                 ecur = ecur->next;
    1085             :                         }
    1086             :                 }
    1087           2 :                 mail_free_errorlist(&IMAPG(imap_errorstack));
    1088             :         }
    1089             : 
    1090       19362 :         if (IMAPG(imap_alertstack) != NIL) {
    1091             :                 /* output any remaining alerts at E_NOTICE level */
    1092           0 :                 if (EG(error_reporting) & E_NOTICE) {
    1093           0 :                         acur = IMAPG(imap_alertstack);
    1094           0 :                         while (acur != NIL) {
    1095           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s", acur->LTEXT);
    1096           0 :                                 acur = acur->next;
    1097             :                         }
    1098             :                 }
    1099           0 :                 mail_free_stringlist(&IMAPG(imap_alertstack));
    1100           0 :                 IMAPG(imap_alertstack) = NIL;
    1101             :         }
    1102       19362 :         return SUCCESS;
    1103             : }
    1104             : /* }}} */
    1105             : 
    1106             : #if !defined(CCLIENTVERSION)
    1107             : #if HAVE_IMAP2007e
    1108             : #define CCLIENTVERSION "2007e"
    1109             : #elif HAVE_IMAP2007d
    1110             : #define CCLIENTVERSION "2007d"
    1111             : #elif HAVE_IMAP2007b
    1112             : #define CCLIENTVERSION "2007b"
    1113             : #elif HAVE_IMAP2007a
    1114             : #define CCLIENTVERSION "2007a"
    1115             : #elif HAVE_IMAP2004
    1116             : #define CCLIENTVERSION "2004"
    1117             : #elif HAVE_IMAP2001
    1118             : #define CCLIENTVERSION "2001"
    1119             : #elif HAVE_IMAP2000
    1120             : #define CCLIENTVERSION "2000"
    1121             : #elif defined(IMAP41)
    1122             : #define CCLIENTVERSION "4.1"
    1123             : #else
    1124             : #define CCLIENTVERSION "4.0"
    1125             : #endif
    1126             : #endif
    1127             : 
    1128             : /* {{{ PHP_MINFO_FUNCTION
    1129             :  */
    1130         148 : PHP_MINFO_FUNCTION(imap)
    1131             : {
    1132         148 :         php_info_print_table_start();
    1133         148 :         php_info_print_table_row(2, "IMAP c-Client Version", CCLIENTVERSION);
    1134             : #if HAVE_IMAP_SSL
    1135         148 :         php_info_print_table_row(2, "SSL Support", "enabled");
    1136             : #endif
    1137             : #if HAVE_IMAP_KRB && HAVE_IMAP_AUTH_GSS
    1138         148 :         php_info_print_table_row(2, "Kerberos Support", "enabled");
    1139             : #endif
    1140         148 :         php_info_print_table_end();
    1141         148 : }
    1142             : /* }}} */
    1143             : 
    1144             : /* {{{ imap_do_open
    1145             :  */
    1146         186 : static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
    1147             : {
    1148             :         char *mailbox, *user, *passwd;
    1149             :         int mailbox_len, user_len, passwd_len;
    1150         186 :         long retries = 0, flags = NIL, cl_flags = NIL;
    1151             :         MAILSTREAM *imap_stream;
    1152             :         pils *imap_le_struct;
    1153         186 :         zval *params = NULL;
    1154         186 :         int argc = ZEND_NUM_ARGS();
    1155             : 
    1156         186 :         if (zend_parse_parameters(argc TSRMLS_CC, "sss|lla", &mailbox, &mailbox_len, &user, &user_len,
    1157             :                 &passwd, &passwd_len, &flags, &retries, &params) == FAILURE) {
    1158           5 :                 return;
    1159             :         }
    1160             : 
    1161         181 :         if (argc >= 4) {
    1162          57 :                 if (flags & PHP_EXPUNGE) {
    1163           1 :                         cl_flags = CL_EXPUNGE;
    1164           1 :                         flags ^= PHP_EXPUNGE;
    1165             :                 }
    1166          57 :                 if (flags & OP_PROTOTYPE) {
    1167           1 :                         cl_flags |= OP_PROTOTYPE;
    1168             :                 }
    1169             :         }
    1170             : 
    1171         181 :         if (params) {
    1172             :                 zval **disabled_auth_method;
    1173             : 
    1174           0 :                 if (zend_hash_find(HASH_OF(params), "DISABLE_AUTHENTICATOR", sizeof("DISABLE_AUTHENTICATOR"), (void **)&disabled_auth_method) == SUCCESS) {
    1175           0 :                         switch (Z_TYPE_PP(disabled_auth_method)) {
    1176             :                                 case IS_STRING:
    1177           0 :                                         if (Z_STRLEN_PP(disabled_auth_method) > 1) {
    1178           0 :                                                 mail_parameters (NIL, DISABLE_AUTHENTICATOR, (void *)Z_STRVAL_PP(disabled_auth_method));
    1179             :                                         }
    1180           0 :                                         break;
    1181             :                                 case IS_ARRAY:
    1182             :                                         {
    1183             :                                                 zval **z_auth_method;
    1184             :                                                 int i;
    1185           0 :                                                 int nelems = zend_hash_num_elements(Z_ARRVAL_PP(disabled_auth_method));
    1186             : 
    1187           0 :                                                 if (nelems == 0 ) {
    1188           0 :                                                         break;
    1189             :                                                 }
    1190           0 :                                                 for (i = 0; i < nelems; i++) {
    1191           0 :                                                         if (zend_hash_index_find(Z_ARRVAL_PP(disabled_auth_method), i, (void **) &z_auth_method) == SUCCESS) {
    1192           0 :                                                                 if (Z_TYPE_PP(z_auth_method) == IS_STRING) {
    1193           0 :                                                                         if (Z_STRLEN_PP(z_auth_method) > 1) {
    1194           0 :                                                                                 mail_parameters (NIL, DISABLE_AUTHENTICATOR, (void *)Z_STRVAL_PP(z_auth_method));
    1195             :                                                                         }
    1196             :                                                                 } else {
    1197           0 :                                                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid argument, expect string or array of strings");
    1198             :                                                                 }
    1199             :                                                         }
    1200             :                                                 }
    1201             :                                         }
    1202           0 :                                         break;
    1203             :                                 case IS_LONG:
    1204             :                                 default:
    1205           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid argument, expect string or array of strings");
    1206             :                                         break;
    1207             :                         }
    1208             :                 }
    1209             :         }
    1210             : 
    1211         181 :         if (IMAPG(imap_user)) {
    1212           1 :                 efree(IMAPG(imap_user));
    1213           1 :                 IMAPG(imap_user) = 0;
    1214             :         }
    1215             : 
    1216         181 :         if (IMAPG(imap_password)) {
    1217           1 :                 efree(IMAPG(imap_password));
    1218           1 :                 IMAPG(imap_password) = 0;
    1219             :         }
    1220             : 
    1221             :         /* local filename, need to perform open_basedir and safe_mode checks */
    1222         181 :         if (mailbox[0] != '{') {
    1223           2 :                 if (strlen(mailbox) != mailbox_len) {
    1224           0 :                         RETURN_FALSE;
    1225             :                 }
    1226           4 :                 if (php_check_open_basedir(mailbox TSRMLS_CC) ||
    1227           2 :                         (PG(safe_mode) && !php_checkuid(mailbox, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
    1228           0 :                         RETURN_FALSE;
    1229             :                 }
    1230             :         }
    1231             : 
    1232         181 :         IMAPG(imap_user)     = estrndup(user, user_len);
    1233         181 :         IMAPG(imap_password) = estrndup(passwd, passwd_len);
    1234             : 
    1235             : #ifdef SET_MAXLOGINTRIALS
    1236         181 :         if (argc >= 5) {
    1237          56 :                 if (retries < 0) {
    1238           1 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING ,"Retries must be greater or equal to 0");
    1239             :                 } else {
    1240          55 :                         mail_parameters(NIL, SET_MAXLOGINTRIALS, (void *) retries);
    1241             :                 }
    1242             :         }
    1243             : #endif
    1244             : 
    1245         181 :         imap_stream = mail_open(NIL, mailbox, flags);
    1246             : 
    1247         181 :         if (imap_stream == NIL) {
    1248           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't open stream %s", mailbox);
    1249           4 :                 efree(IMAPG(imap_user)); IMAPG(imap_user) = 0;
    1250           4 :                 efree(IMAPG(imap_password)); IMAPG(imap_password) = 0;
    1251           4 :                 RETURN_FALSE;
    1252             :         }
    1253             : 
    1254         177 :         imap_le_struct = emalloc(sizeof(pils));
    1255         177 :         imap_le_struct->imap_stream = imap_stream;
    1256         177 :         imap_le_struct->flags = cl_flags;
    1257             : 
    1258         177 :         ZEND_REGISTER_RESOURCE(return_value, imap_le_struct, le_imap);
    1259             : }
    1260             : /* }}} */
    1261             : 
    1262             : /* {{{ proto resource imap_open(string mailbox, string user, string password [, int options [, int n_retries]])
    1263             :    Open an IMAP stream to a mailbox */
    1264         186 : PHP_FUNCTION(imap_open)
    1265             : {
    1266         186 :         php_imap_do_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    1267         186 : }
    1268             : /* }}} */
    1269             : 
    1270             : /* {{{ proto bool imap_reopen(resource stream_id, string mailbox [, int options [, int n_retries]])
    1271             :    Reopen an IMAP stream to a new mailbox */
    1272          31 : PHP_FUNCTION(imap_reopen)
    1273             : {
    1274             :         zval *streamind;
    1275             :         char *mailbox;
    1276             :         int mailbox_len;
    1277          31 :         long options = 0, retries = 0;
    1278             :         pils *imap_le_struct;
    1279             :         MAILSTREAM *imap_stream;
    1280          31 :         long flags=NIL;
    1281          31 :         long cl_flags=NIL;
    1282             : 
    1283          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|ll", &streamind, &mailbox, &mailbox_len, &options, &retries) == FAILURE) {
    1284           0 :                 return;
    1285             :         }
    1286             : 
    1287          31 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1288             : 
    1289          31 :         if (options) {
    1290           0 :                 flags = options;
    1291           0 :                 if (flags & PHP_EXPUNGE) {
    1292           0 :                         cl_flags = CL_EXPUNGE;
    1293           0 :                         flags ^= PHP_EXPUNGE;
    1294             :                 }
    1295           0 :                 imap_le_struct->flags = cl_flags;
    1296             :         }
    1297             : #ifdef SET_MAXLOGINTRIALS
    1298          31 :         if (retries) {
    1299           0 :                 mail_parameters(NIL, SET_MAXLOGINTRIALS, (void *) retries);
    1300             :         }
    1301             : #endif
    1302             :         /* local filename, need to perform open_basedir and safe_mode checks */
    1303          31 :         if (mailbox[0] != '{' &&
    1304           0 :                         (php_check_open_basedir(mailbox TSRMLS_CC) ||
    1305           0 :                         (PG(safe_mode) && !php_checkuid(mailbox, NULL, CHECKUID_CHECK_FILE_AND_DIR)))) {
    1306           0 :                 RETURN_FALSE;
    1307             :         }
    1308             : 
    1309          31 :         imap_stream = mail_open(imap_le_struct->imap_stream, mailbox, flags);
    1310          31 :         if (imap_stream == NIL) {
    1311           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't re-open stream");
    1312           0 :                 RETURN_FALSE;
    1313             :         }
    1314          31 :         imap_le_struct->imap_stream = imap_stream;
    1315          31 :         RETURN_TRUE;
    1316             : }
    1317             : /* }}} */
    1318             : 
    1319             : /* {{{ proto bool imap_append(resource stream_id, string folder, string message [, string options [, string internal_date]])
    1320             :    Append a new message to a specified mailbox */
    1321         141 : PHP_FUNCTION(imap_append)
    1322             : {
    1323             :         zval *streamind;
    1324         141 :         char *folder, *message, *internal_date = NULL, *flags = NULL;
    1325         141 :         int folder_len, message_len, internal_date_len = 0, flags_len = 0;
    1326             :         pils *imap_le_struct;
    1327             :         STRING st;
    1328         141 :         char* regex = "/[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}/";
    1329         141 :         const int regex_len = strlen(regex);
    1330             :         pcre_cache_entry *pce;                          /* Compiled regex */
    1331         141 :         zval *subpats = NULL;                           /* Parts (not used) */
    1332         141 :         long regex_flags = 0;                           /* Flags (not used) */
    1333         141 :         long start_offset = 0;                          /* Start offset (not used) */
    1334         141 :         int global = 0;
    1335             : 
    1336         141 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss|ss", &streamind, &folder, &folder_len, &message, &message_len, &flags, &flags_len, &internal_date, &internal_date_len) == FAILURE) {
    1337           0 :                 return;
    1338             :         }
    1339             : 
    1340         141 :         if (internal_date) {
    1341             :                 /* Make sure the given internal_date string matches the RFC specifiedformat */
    1342           0 :                 if ((pce = pcre_get_compiled_regex_cache(regex, regex_len TSRMLS_CC))== NULL) {
    1343           0 :                         RETURN_FALSE;
    1344             :                 }
    1345             : 
    1346           0 :                 php_pcre_match_impl(pce, internal_date, internal_date_len, return_value, subpats, global,
    1347             :                         0, regex_flags, start_offset TSRMLS_CC);
    1348             : 
    1349           0 :                 if (!Z_LVAL_P(return_value)) {
    1350           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "internal date not correctly formatted");
    1351           0 :                         internal_date = NULL;
    1352             :                 }
    1353             :         }
    1354             : 
    1355         141 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1356             : 
    1357         141 :         INIT (&st, mail_string, (void *) message, message_len);
    1358             : 
    1359         141 :         if (mail_append_full(imap_le_struct->imap_stream, folder, (flags ? flags : NIL), (internal_date ? internal_date : NIL), &st)) {
    1360         141 :                 RETURN_TRUE;
    1361             :         } else {
    1362           0 :                 RETURN_FALSE;
    1363             :         }
    1364             : }
    1365             : /* }}} */
    1366             : 
    1367             : /* {{{ proto int imap_num_msg(resource stream_id)
    1368             :    Gives the number of messages in the current mailbox */
    1369          22 : PHP_FUNCTION(imap_num_msg)
    1370             : {
    1371             :         zval *streamind;
    1372             :         pils *imap_le_struct;
    1373             : 
    1374          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &streamind) == FAILURE) {
    1375           3 :                 return;
    1376             :         }
    1377             : 
    1378          19 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1379             : 
    1380          19 :         RETURN_LONG(imap_le_struct->imap_stream->nmsgs);
    1381             : }
    1382             : /* }}} */
    1383             : 
    1384             : /* {{{ proto bool imap_ping(resource stream_id)
    1385             :    Check if the IMAP stream is still active */
    1386           3 : PHP_FUNCTION(imap_ping)
    1387             : {
    1388             :         zval *streamind;
    1389             :         pils *imap_le_struct;
    1390             : 
    1391           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &streamind) == FAILURE) {
    1392           3 :                 return;
    1393             :         }
    1394             : 
    1395           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1396             : 
    1397           0 :         RETURN_BOOL(mail_ping(imap_le_struct->imap_stream));
    1398             : }
    1399             : /* }}} */
    1400             : 
    1401             : /* {{{ proto int imap_num_recent(resource stream_id)
    1402             :    Gives the number of recent messages in current mailbox */
    1403           3 : PHP_FUNCTION(imap_num_recent)
    1404             : {
    1405             :         zval *streamind;
    1406             :         pils *imap_le_struct;
    1407             : 
    1408           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &streamind) == FAILURE) {
    1409           3 :                 return;
    1410             :         }
    1411             : 
    1412           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1413             : 
    1414           0 :         RETURN_LONG(imap_le_struct->imap_stream->recent);
    1415             : }
    1416             : /* }}} */
    1417             : 
    1418             : #if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
    1419             : /* {{{ proto array imap_get_quota(resource stream_id, string qroot)
    1420             :         Returns the quota set to the mailbox account qroot */
    1421           0 : PHP_FUNCTION(imap_get_quota)
    1422             : {
    1423             :         zval *streamind;
    1424             :         char *qroot;
    1425             :         int qroot_len;
    1426             :         pils *imap_le_struct;
    1427             : 
    1428           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &streamind, &qroot, &qroot_len) == FAILURE) {
    1429           0 :                 return;
    1430             :         }
    1431             : 
    1432           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1433             : 
    1434           0 :         array_init(return_value);
    1435           0 :         IMAPG(quota_return) = &return_value;
    1436             : 
    1437             :         /* set the callback for the GET_QUOTA function */
    1438           0 :         mail_parameters(NIL, SET_QUOTA, (void *) mail_getquota);
    1439           0 :         if (!imap_getquota(imap_le_struct->imap_stream, qroot)) {
    1440           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "c-client imap_getquota failed");
    1441           0 :                 zval_dtor(return_value);
    1442           0 :                 RETURN_FALSE;
    1443             :         }
    1444             : }
    1445             : /* }}} */
    1446             : 
    1447             : /* {{{ proto array imap_get_quotaroot(resource stream_id, string mbox)
    1448             :         Returns the quota set to the mailbox account mbox */
    1449           0 : PHP_FUNCTION(imap_get_quotaroot)
    1450             : {
    1451             :         zval *streamind;
    1452             :         char *mbox;
    1453             :         int mbox_len;
    1454             :         pils *imap_le_struct;
    1455             : 
    1456           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &streamind, &mbox, &mbox_len) == FAILURE) {
    1457           0 :                 return;
    1458             :         }
    1459             : 
    1460           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1461             : 
    1462           0 :         array_init(return_value);
    1463           0 :         IMAPG(quota_return) = &return_value;
    1464             : 
    1465             :         /* set the callback for the GET_QUOTAROOT function */
    1466           0 :         mail_parameters(NIL, SET_QUOTA, (void *) mail_getquota);
    1467           0 :         if (!imap_getquotaroot(imap_le_struct->imap_stream, mbox)) {
    1468           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "c-client imap_getquotaroot failed");
    1469           0 :                 zval_dtor(return_value);
    1470           0 :                 RETURN_FALSE;
    1471             :         }
    1472             : }
    1473             : /* }}} */
    1474             : 
    1475             : /* {{{ proto bool imap_set_quota(resource stream_id, string qroot, int mailbox_size)
    1476             :    Will set the quota for qroot mailbox */
    1477           0 : PHP_FUNCTION(imap_set_quota)
    1478             : {
    1479             :         zval *streamind;
    1480             :         char *qroot;
    1481             :         int qroot_len;
    1482             :         long mailbox_size;
    1483             :         pils *imap_le_struct;
    1484             :         STRINGLIST      limits;
    1485             : 
    1486           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsl", &streamind, &qroot, &qroot_len, &mailbox_size) == FAILURE) {
    1487           0 :                 return;
    1488             :         }
    1489             : 
    1490           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1491             : 
    1492           0 :         limits.text.data = "STORAGE";
    1493           0 :         limits.text.size = mailbox_size;
    1494           0 :         limits.next = NIL;
    1495             : 
    1496           0 :         RETURN_BOOL(imap_setquota(imap_le_struct->imap_stream, qroot, &limits));
    1497             : }
    1498             : /* }}} */
    1499             : 
    1500             : /* {{{ proto bool imap_setacl(resource stream_id, string mailbox, string id, string rights)
    1501             :         Sets the ACL for a given mailbox */
    1502           0 : PHP_FUNCTION(imap_setacl)
    1503             : {
    1504             :         zval *streamind;
    1505             :         char *mailbox, *id, *rights;
    1506             :         int mailbox_len, id_len, rights_len;
    1507             :         pils *imap_le_struct;
    1508             : 
    1509           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsss", &streamind, &mailbox, &mailbox_len, &id, &id_len, &rights, &rights_len) == FAILURE) {
    1510           0 :                 return;
    1511             :         }
    1512             : 
    1513           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1514             : 
    1515           0 :         RETURN_BOOL(imap_setacl(imap_le_struct->imap_stream, mailbox, id, rights));
    1516             : }
    1517             : /* }}} */
    1518             : 
    1519             : /* {{{ proto array imap_getacl(resource stream_id, string mailbox)
    1520             :         Gets the ACL for a given mailbox */
    1521           0 : PHP_FUNCTION(imap_getacl)
    1522             : {
    1523             :         zval *streamind;
    1524             :         char *mailbox;
    1525             :         int mailbox_len;
    1526             :         pils *imap_le_struct;
    1527             : 
    1528           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &streamind, &mailbox, &mailbox_len) == FAILURE) {
    1529           0 :                 return;
    1530             :         }
    1531             : 
    1532           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1533             : 
    1534             :         /* initializing the special array for the return values */
    1535           0 :         array_init(return_value);
    1536             : 
    1537           0 :         IMAPG(imap_acl_list) = return_value;
    1538             : 
    1539             :         /* set the callback for the GET_ACL function */
    1540           0 :         mail_parameters(NIL, SET_ACL, (void *) mail_getacl);
    1541           0 :         if (!imap_getacl(imap_le_struct->imap_stream, mailbox)) {
    1542           0 :                 php_error(E_WARNING, "c-client imap_getacl failed");
    1543           0 :                 zval_dtor(return_value);
    1544           0 :                 RETURN_FALSE;
    1545             :         }
    1546             : 
    1547           0 :         IMAPG(imap_acl_list) = NIL;
    1548             : }
    1549             : /* }}} */
    1550             : #endif /* HAVE_IMAP2000 || HAVE_IMAP2001 */
    1551             : 
    1552             : /* {{{ proto bool imap_expunge(resource stream_id)
    1553             :    Permanently delete all messages marked for deletion */
    1554           3 : PHP_FUNCTION(imap_expunge)
    1555             : {
    1556             :         zval *streamind;
    1557             :         pils *imap_le_struct;
    1558             : 
    1559           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &streamind) == FAILURE) {
    1560           3 :                 return;
    1561             :         }
    1562             : 
    1563           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1564             : 
    1565           0 :         mail_expunge (imap_le_struct->imap_stream);
    1566             : 
    1567           0 :         RETURN_TRUE;
    1568             : }
    1569             : /* }}} */
    1570             : 
    1571             : /* {{{ proto bool imap_gc(resource stream_id, int flags)
    1572             :    This function garbage collects (purges) the cache of entries of a specific type. */
    1573           4 : PHP_FUNCTION(imap_gc)
    1574             : {
    1575             :         zval *streamind;
    1576             :         pils *imap_le_struct;
    1577             :         long flags;
    1578             : 
    1579           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &streamind, &flags) == FAILURE) {
    1580           3 :                 return;
    1581             :         }
    1582             : 
    1583           1 :         if (flags && ((flags & ~(GC_TEXTS | GC_ELT | GC_ENV)) != 0)) {
    1584           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the flags parameter");
    1585           1 :                 RETURN_FALSE;
    1586             :         }
    1587             : 
    1588           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1589             : 
    1590           0 :         mail_gc(imap_le_struct->imap_stream, flags);
    1591             : 
    1592           0 :         RETURN_TRUE;
    1593             : }
    1594             : /* }}} */
    1595             : 
    1596             : /* {{{ proto bool imap_close(resource stream_id [, int options])
    1597             :    Close an IMAP stream */
    1598         194 : PHP_FUNCTION(imap_close)
    1599             : {
    1600             :         zval *streamind;
    1601         194 :         pils *imap_le_struct=NULL;
    1602         194 :         long options = 0, flags = NIL;
    1603         194 :         int argc = ZEND_NUM_ARGS();
    1604             : 
    1605         194 :         if (zend_parse_parameters(argc TSRMLS_CC, "r|l", &streamind, &options) == FAILURE) {
    1606          29 :                 return;
    1607             :         }
    1608             : 
    1609         165 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1610             : 
    1611         163 :         if (argc == 2) {
    1612          87 :                 flags = options;
    1613             : 
    1614             :                 /* Check that flags is exactly equal to PHP_EXPUNGE or zero */
    1615          87 :                 if (flags && ((flags & ~PHP_EXPUNGE) != 0)) {
    1616          10 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the flags parameter");
    1617          10 :                         RETURN_FALSE;
    1618             :                 }
    1619             : 
    1620             :                 /* Do the translation from PHP's internal PHP_EXPUNGE define to c-client's CL_EXPUNGE */
    1621          77 :                 if (flags & PHP_EXPUNGE) {
    1622          67 :                         flags ^= PHP_EXPUNGE;
    1623          67 :                         flags |= CL_EXPUNGE;
    1624             :                 }
    1625          77 :                 imap_le_struct->flags = flags;
    1626             :         }
    1627             : 
    1628         153 :         zend_list_delete(Z_RESVAL_P(streamind));
    1629             : 
    1630         153 :         RETURN_TRUE;
    1631             : }
    1632             : /* }}} */
    1633             : 
    1634             : /* {{{ proto array imap_headers(resource stream_id)
    1635             :    Returns headers for all messages in a mailbox */
    1636           4 : PHP_FUNCTION(imap_headers)
    1637             : {
    1638             :         zval *streamind;
    1639             :         pils *imap_le_struct;
    1640             :         unsigned long i;
    1641             :         char *t;
    1642             :         unsigned int msgno;
    1643             :         char tmp[MAILTMPLEN];
    1644             : 
    1645           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &streamind) == FAILURE) {
    1646           3 :                 return;
    1647             :         }
    1648             : 
    1649           1 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1650             : 
    1651             :         /* Initialize return array */
    1652           1 :         array_init(return_value);
    1653             : 
    1654           3 :         for (msgno = 1; msgno <= imap_le_struct->imap_stream->nmsgs; msgno++) {
    1655           2 :                 MESSAGECACHE * cache = mail_elt (imap_le_struct->imap_stream, msgno);
    1656           2 :                 mail_fetchstructure(imap_le_struct->imap_stream, msgno, NIL);
    1657           2 :                 tmp[0] = cache->recent ? (cache->seen ? 'R': 'N') : ' ';
    1658           2 :                 tmp[1] = (cache->recent | cache->seen) ? ' ' : 'U';
    1659           2 :                 tmp[2] = cache->flagged ? 'F' : ' ';
    1660           2 :                 tmp[3] = cache->answered ? 'A' : ' ';
    1661           2 :                 tmp[4] = cache->deleted ? 'D' : ' ';
    1662           2 :                 tmp[5] = cache->draft ? 'X' : ' ';
    1663           2 :                 snprintf(tmp + 6, sizeof(tmp) - 6, "%4ld) ", cache->msgno);
    1664           2 :                 mail_date(tmp+11, cache);
    1665           2 :                 tmp[22] = ' ';
    1666           2 :                 tmp[23] = '\0';
    1667           2 :                 mail_fetchfrom(tmp+23, imap_le_struct->imap_stream, msgno, (long)20);
    1668           2 :                 strcat(tmp, " ");
    1669           2 :                 if ((i = cache->user_flags)) {
    1670           0 :                         strcat(tmp, "{");
    1671           0 :                         while (i) {
    1672           0 :                                 strlcat(tmp, imap_le_struct->imap_stream->user_flags[find_rightmost_bit (&i)], sizeof(tmp));
    1673           0 :                                 if (i) strlcat(tmp, " ", sizeof(tmp));
    1674             :                         }
    1675           0 :                         strlcat(tmp, "} ", sizeof(tmp));
    1676             :                 }
    1677           2 :                 mail_fetchsubject(t = tmp + strlen(tmp), imap_le_struct->imap_stream, msgno, (long)25);
    1678           2 :                 snprintf(t += strlen(t), sizeof(tmp) - strlen(tmp), " (%ld chars)", cache->rfc822_size);
    1679           2 :                 add_next_index_string(return_value, tmp, 1);
    1680             :         }
    1681             : }
    1682             : /* }}} */
    1683             : 
    1684             : /* {{{ proto string imap_body(resource stream_id, int msg_no [, int options])
    1685             :    Read the message body */
    1686           9 : PHP_FUNCTION(imap_body)
    1687             : {
    1688             :         zval *streamind;
    1689           9 :         long msgno, flags = 0;
    1690             :         pils *imap_le_struct;
    1691           9 :         int msgindex, argc = ZEND_NUM_ARGS();
    1692             :         char *body;
    1693           9 :         unsigned long body_len = 0;
    1694             : 
    1695           9 :         if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &streamind, &msgno, &flags) == FAILURE) {
    1696           4 :                 return;
    1697             :         }
    1698             : 
    1699           5 :         if (flags && ((flags & ~(FT_UID|FT_PEEK|FT_INTERNAL)) != 0)) {
    1700           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the options parameter");
    1701           1 :                 RETURN_FALSE;
    1702             :         }
    1703             : 
    1704           4 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1705             : 
    1706           6 :         if ((argc == 3) && (flags & FT_UID)) {
    1707             :                 /* This should be cached; if it causes an extra RTT to the
    1708             :                    IMAP server, then that's the price we pay for making
    1709             :                    sure we don't crash. */
    1710           2 :                 msgindex = mail_msgno(imap_le_struct->imap_stream, msgno);
    1711             :         } else {
    1712           2 :                 msgindex = msgno;
    1713             :         }
    1714           4 :         if ((msgindex < 1) || ((unsigned) msgindex > imap_le_struct->imap_stream->nmsgs)) {
    1715           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad message number");
    1716           2 :                 RETURN_FALSE;
    1717             :         }
    1718             : 
    1719           2 :         body = mail_fetchtext_full (imap_le_struct->imap_stream, msgno, &body_len, (argc == 3 ? flags : NIL));
    1720           2 :         if (body_len == 0) {
    1721           0 :                 RETVAL_EMPTY_STRING();
    1722             :         } else {
    1723           2 :                 RETVAL_STRINGL(body, body_len, 1);
    1724             :         }
    1725             : }
    1726             : /* }}} */
    1727             : 
    1728             : /* {{{ proto bool imap_mail_copy(resource stream_id, string msglist, string mailbox [, int options])
    1729             :    Copy specified message to a mailbox */
    1730           7 : PHP_FUNCTION(imap_mail_copy)
    1731             : {
    1732             :         zval *streamind;
    1733           7 :         long options = 0;
    1734             :         char *seq, *folder;
    1735           7 :         int seq_len, folder_len, argc = ZEND_NUM_ARGS();
    1736             :         pils *imap_le_struct;
    1737             : 
    1738           7 :         if (zend_parse_parameters(argc TSRMLS_CC, "rss|l", &streamind, &seq, &seq_len, &folder, &folder_len, &options) == FAILURE) {
    1739           6 :                 return;
    1740             :         }
    1741             : 
    1742           1 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1743             : 
    1744           1 :         if (mail_copy_full(imap_le_struct->imap_stream, seq, folder, (argc == 4 ? options : NIL)) == T) {
    1745           1 :                 RETURN_TRUE;
    1746             :         } else {
    1747           0 :                 RETURN_FALSE;
    1748             :         }
    1749             : }
    1750             : /* }}} */
    1751             : 
    1752             : /* {{{ proto bool imap_mail_move(resource stream_id, string sequence, string mailbox [, int options])
    1753             :    Move specified message to a mailbox */
    1754           7 : PHP_FUNCTION(imap_mail_move)
    1755             : {
    1756             :         zval *streamind;
    1757             :         char *seq, *folder;
    1758             :         int seq_len, folder_len;
    1759           7 :         long options = 0;
    1760             :         pils *imap_le_struct;
    1761           7 :         int argc = ZEND_NUM_ARGS();
    1762             : 
    1763           7 :         if (zend_parse_parameters(argc TSRMLS_CC, "rss|l", &streamind, &seq, &seq_len, &folder, &folder_len, &options) == FAILURE) {
    1764           6 :                 return;
    1765             :         }
    1766             : 
    1767           1 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1768             : 
    1769           1 :         if (mail_copy_full(imap_le_struct->imap_stream, seq, folder, (argc == 4 ? (options | CP_MOVE) : CP_MOVE)) == T) {
    1770           1 :                 RETURN_TRUE;
    1771             :         } else {
    1772           0 :                 RETURN_FALSE;
    1773             :         }
    1774             : }
    1775             : /* }}} */
    1776             : 
    1777             : /* {{{ proto bool imap_createmailbox(resource stream_id, string mailbox)
    1778             :    Create a new mailbox */
    1779          35 : PHP_FUNCTION(imap_createmailbox)
    1780             : {
    1781             :         zval *streamind;
    1782             :         char *folder;
    1783             :         int folder_len;
    1784             :         pils *imap_le_struct;
    1785             : 
    1786          35 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &streamind, &folder, &folder_len) == FAILURE) {
    1787           0 :                 return;
    1788             :         }
    1789             : 
    1790          35 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1791             : 
    1792          35 :         if (mail_create(imap_le_struct->imap_stream, folder) == T) {
    1793          35 :                 RETURN_TRUE;
    1794             :         } else {
    1795           0 :                 RETURN_FALSE;
    1796             :         }
    1797             : }
    1798             : /* }}} */
    1799             : 
    1800             : /* {{{ proto bool imap_renamemailbox(resource stream_id, string old_name, string new_name)
    1801             :    Rename a mailbox */
    1802           6 : PHP_FUNCTION(imap_renamemailbox)
    1803             : {
    1804             :         zval *streamind;
    1805             :         char *old_mailbox, *new_mailbox;
    1806             :         int old_mailbox_len, new_mailbox_len;
    1807             :         pils *imap_le_struct;
    1808             : 
    1809           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &streamind, &old_mailbox, &old_mailbox_len, &new_mailbox, &new_mailbox_len) == FAILURE) {
    1810           5 :                 return;
    1811             :         }
    1812             : 
    1813           1 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1814             : 
    1815           1 :         if (mail_rename(imap_le_struct->imap_stream, old_mailbox, new_mailbox) == T) {
    1816           1 :                 RETURN_TRUE;
    1817             :         } else {
    1818           0 :                 RETURN_FALSE;
    1819             :         }
    1820             : }
    1821             : /* }}} */
    1822             : 
    1823             : /* {{{ proto bool imap_deletemailbox(resource stream_id, string mailbox)
    1824             :    Delete a mailbox */
    1825          35 : PHP_FUNCTION(imap_deletemailbox)
    1826             : {
    1827             :         zval *streamind;
    1828             :         char *folder;
    1829             :         int folder_len;
    1830             :         pils *imap_le_struct;
    1831             : 
    1832          35 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &streamind, &folder, &folder_len) == FAILURE) {
    1833           0 :                 return;
    1834             :         }
    1835             : 
    1836          35 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1837             : 
    1838          35 :         if (mail_delete(imap_le_struct->imap_stream, folder) == T) {
    1839          35 :                 RETURN_TRUE;
    1840             :         } else {
    1841           0 :                 RETURN_FALSE;
    1842             :         }
    1843             : }
    1844             : /* }}} */
    1845             : 
    1846             : /* {{{ proto array imap_list(resource stream_id, string ref, string pattern)
    1847             :    Read the list of mailboxes */
    1848           7 : PHP_FUNCTION(imap_list)
    1849             : {
    1850             :         zval *streamind;
    1851             :         char *ref, *pat;
    1852             :         int ref_len, pat_len;
    1853             :         pils *imap_le_struct;
    1854           7 :         STRINGLIST *cur=NIL;
    1855             : 
    1856           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &streamind, &ref, &ref_len, &pat, &pat_len) == FAILURE) {
    1857           5 :                 return;
    1858             :         }
    1859             : 
    1860           2 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1861             : 
    1862             :         /* set flag for normal, old mailbox list */
    1863           2 :         IMAPG(folderlist_style) = FLIST_ARRAY;
    1864             : 
    1865           2 :         IMAPG(imap_folders) = IMAPG(imap_folders_tail) = NIL;
    1866           2 :         mail_list(imap_le_struct->imap_stream, ref, pat);
    1867           2 :         if (IMAPG(imap_folders) == NIL) {
    1868           1 :                 RETURN_FALSE;
    1869             :         }
    1870             : 
    1871           1 :         array_init(return_value);
    1872           1 :         cur=IMAPG(imap_folders);
    1873           3 :         while (cur != NIL) {
    1874           1 :                 add_next_index_string(return_value, cur->LTEXT, 1);
    1875           1 :                 cur=cur->next;
    1876             :         }
    1877           1 :         mail_free_stringlist (&IMAPG(imap_folders));
    1878           1 :         IMAPG(imap_folders) = IMAPG(imap_folders_tail) = NIL;
    1879             : }
    1880             : 
    1881             : /* }}} */
    1882             : 
    1883             : /* {{{ proto array imap_getmailboxes(resource stream_id, string ref, string pattern)
    1884             :    Reads the list of mailboxes and returns a full array of objects containing name, attributes, and delimiter */
    1885             : /* Author: CJH */
    1886          64 : PHP_FUNCTION(imap_list_full)
    1887             : {
    1888             :         zval *streamind, *mboxob;
    1889             :         char *ref, *pat;
    1890             :         int ref_len, pat_len;
    1891             :         pils *imap_le_struct;
    1892          64 :         FOBJECTLIST *cur=NIL;
    1893          64 :         char *delim=NIL;
    1894             : 
    1895          64 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &streamind, &ref, &ref_len, &pat, &pat_len) == FAILURE) {
    1896           0 :                 return;
    1897             :         }
    1898             : 
    1899          64 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1900             : 
    1901             :         /* set flag for new, improved array of objects mailbox list */
    1902          64 :         IMAPG(folderlist_style) = FLIST_OBJECT;
    1903             : 
    1904          64 :         IMAPG(imap_folder_objects) = IMAPG(imap_folder_objects_tail) = NIL;
    1905          64 :         mail_list(imap_le_struct->imap_stream, ref, pat);
    1906          64 :         if (IMAPG(imap_folder_objects) == NIL) {
    1907          31 :                 RETURN_FALSE;
    1908             :         }
    1909             : 
    1910          33 :         array_init(return_value);
    1911          33 :         delim = safe_emalloc(2, sizeof(char), 0);
    1912          33 :         cur=IMAPG(imap_folder_objects);
    1913         133 :         while (cur != NIL) {
    1914          67 :                 MAKE_STD_ZVAL(mboxob);
    1915          67 :                 object_init(mboxob);
    1916          67 :                 add_property_string(mboxob, "name", cur->LTEXT, 1);
    1917          67 :                 add_property_long(mboxob, "attributes", cur->attributes);
    1918             : #ifdef IMAP41
    1919          67 :                 delim[0] = (char)cur->delimiter;
    1920          67 :                 delim[1] = 0;
    1921          67 :                 add_property_string(mboxob, "delimiter", delim, 1);
    1922             : #else
    1923             :                 add_property_string(mboxob, "delimiter", cur->delimiter, 1);
    1924             : #endif
    1925          67 :                 add_next_index_object(return_value, mboxob TSRMLS_CC);
    1926          67 :                 cur=cur->next;
    1927             :         }
    1928          33 :         mail_free_foblist(&IMAPG(imap_folder_objects), &IMAPG(imap_folder_objects_tail));
    1929          33 :         efree(delim);
    1930          33 :         IMAPG(folderlist_style) = FLIST_ARRAY;          /* reset to default */
    1931             : }
    1932             : /* }}} */
    1933             : 
    1934             : /* {{{ proto array imap_listscan(resource stream_id, string ref, string pattern, string content)
    1935             :    Read list of mailboxes containing a certain string */
    1936           0 : PHP_FUNCTION(imap_listscan)
    1937             : {
    1938             :         zval *streamind;
    1939             :         char *ref, *pat, *content;
    1940             :         int ref_len, pat_len, content_len;
    1941             :         pils *imap_le_struct;
    1942           0 :         STRINGLIST *cur=NIL;
    1943             : 
    1944           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsss", &streamind, &ref, &ref_len, &pat, &pat_len, &content, &content_len) == FAILURE) {
    1945           0 :                 return;
    1946             :         }
    1947             : 
    1948           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1949             : 
    1950           0 :         IMAPG(imap_folders) = NIL;
    1951           0 :         mail_scan(imap_le_struct->imap_stream, ref, pat, content);
    1952           0 :         if (IMAPG(imap_folders) == NIL) {
    1953           0 :                 RETURN_FALSE;
    1954             :         }
    1955             : 
    1956           0 :         array_init(return_value);
    1957           0 :         cur=IMAPG(imap_folders);
    1958           0 :         while (cur != NIL) {
    1959           0 :                 add_next_index_string(return_value, cur->LTEXT, 1);
    1960           0 :                 cur=cur->next;
    1961             :         }
    1962           0 :         mail_free_stringlist (&IMAPG(imap_folders));
    1963           0 :         IMAPG(imap_folders) = IMAPG(imap_folders_tail) = NIL;
    1964             : }
    1965             : 
    1966             : /* }}} */
    1967             : 
    1968             : /* {{{ proto object imap_check(resource stream_id)
    1969             :    Get mailbox properties */
    1970          42 : PHP_FUNCTION(imap_check)
    1971             : {
    1972             :         zval *streamind;
    1973             :         pils *imap_le_struct;
    1974             :         char date[100];
    1975             : 
    1976          42 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &streamind) == FAILURE) {
    1977           0 :                 return;
    1978             :         }
    1979             : 
    1980          42 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    1981             : 
    1982          42 :         if (mail_ping (imap_le_struct->imap_stream) == NIL) {
    1983           0 :                 RETURN_FALSE;
    1984             :         }
    1985             : 
    1986          84 :         if (imap_le_struct->imap_stream && imap_le_struct->imap_stream->mailbox) {
    1987          42 :                 rfc822_date(date);
    1988          42 :                 object_init(return_value);
    1989          42 :                 add_property_string(return_value, "Date", date, 1);
    1990          42 :                 add_property_string(return_value, "Driver", imap_le_struct->imap_stream->dtb->name, 1);
    1991          42 :                 add_property_string(return_value, "Mailbox", imap_le_struct->imap_stream->mailbox, 1);
    1992          42 :                 add_property_long(return_value, "Nmsgs", imap_le_struct->imap_stream->nmsgs);
    1993          42 :                 add_property_long(return_value, "Recent", imap_le_struct->imap_stream->recent);
    1994             :         } else {
    1995           0 :                 RETURN_FALSE;
    1996             :         }
    1997             : }
    1998             : /* }}} */
    1999             : 
    2000             : /* {{{ proto bool imap_delete(resource stream_id, int msg_no [, int options])
    2001             :    Mark a message for deletion */
    2002          88 : PHP_FUNCTION(imap_delete)
    2003             : {
    2004             :         zval *streamind, **sequence;
    2005             :         pils *imap_le_struct;
    2006          88 :         long flags = 0;
    2007          88 :         int argc = ZEND_NUM_ARGS();
    2008             : 
    2009          88 :         if (zend_parse_parameters(argc TSRMLS_CC, "rZ|l", &streamind, &sequence, &flags) == FAILURE) {
    2010           0 :                 return;
    2011             :         }
    2012             : 
    2013          88 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2014             : 
    2015         526 :         convert_to_string_ex(sequence);
    2016             : 
    2017          88 :         mail_setflag_full(imap_le_struct->imap_stream, Z_STRVAL_PP(sequence), "\\DELETED", (argc == 3 ? flags : NIL));
    2018          88 :         RETVAL_TRUE;
    2019             : }
    2020             : /* }}} */
    2021             : 
    2022             : /* {{{ proto bool imap_undelete(resource stream_id, int msg_no [, int flags])
    2023             :    Remove the delete flag from a message */
    2024           5 : PHP_FUNCTION(imap_undelete)
    2025             : {
    2026             :         zval *streamind, **sequence;
    2027           5 :         long flags = 0;
    2028             :         pils *imap_le_struct;
    2029           5 :         int argc = ZEND_NUM_ARGS();
    2030             : 
    2031           5 :         if (zend_parse_parameters(argc TSRMLS_CC, "rZ|l", &streamind, &sequence, &flags) == FAILURE) {
    2032           4 :                 return;
    2033             :         }
    2034             : 
    2035           1 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2036             : 
    2037           4 :         convert_to_string_ex(sequence);
    2038             : 
    2039           1 :         mail_clearflag_full(imap_le_struct->imap_stream, Z_STRVAL_PP(sequence), "\\DELETED", (argc == 3 ? flags : NIL));
    2040           1 :         RETVAL_TRUE;
    2041             : }
    2042             : /* }}} */
    2043             : 
    2044             : /* {{{ proto object imap_headerinfo(resource stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
    2045             :    Read the headers of the message */
    2046           6 : PHP_FUNCTION(imap_headerinfo)
    2047             : {
    2048             :         zval *streamind;
    2049           6 :         char *defaulthost = NULL;
    2050           6 :         int defaulthost_len = 0, argc = ZEND_NUM_ARGS();
    2051             :         long msgno, fromlength, subjectlength;
    2052             :         pils *imap_le_struct;
    2053             :         MESSAGECACHE *cache;
    2054             :         ENVELOPE *en;
    2055             :         char dummy[2000], fulladdress[MAILTMPLEN + 1];
    2056             : 
    2057           6 :         if (zend_parse_parameters(argc TSRMLS_CC, "rl|lls", &streamind, &msgno, &fromlength, &subjectlength, &defaulthost, &defaulthost_len) == FAILURE) {
    2058           4 :                 return;
    2059             :         }
    2060             : 
    2061           2 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2062             : 
    2063           2 :         if (argc >= 3) {
    2064           0 :                 if (fromlength < 0 || fromlength > MAILTMPLEN) {
    2065           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "From length has to be between 0 and %d", MAILTMPLEN);
    2066           0 :                         RETURN_FALSE;
    2067             :                 }
    2068             :         } else {
    2069           2 :                 fromlength = 0x00;
    2070             :         }
    2071           2 :         if (argc >= 4) {
    2072           0 :                 if (subjectlength < 0 || subjectlength > MAILTMPLEN) {
    2073           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Subject length has to be between 0 and %d", MAILTMPLEN);
    2074           0 :                         RETURN_FALSE;
    2075             :                 }
    2076             :         } else {
    2077           2 :                 subjectlength = 0x00;
    2078             :         }
    2079             : 
    2080           2 :         PHP_IMAP_CHECK_MSGNO(msgno);
    2081             : 
    2082           2 :         if (mail_fetchstructure(imap_le_struct->imap_stream, msgno, NIL)) {
    2083           2 :                 cache = mail_elt(imap_le_struct->imap_stream, msgno);
    2084             :         } else {
    2085           0 :                 RETURN_FALSE;
    2086             :         }
    2087             : 
    2088           2 :         en = mail_fetchenvelope(imap_le_struct->imap_stream, msgno);
    2089             : 
    2090             :         /* call a function to parse all the text, so that we can use the
    2091             :            same function to parse text from other sources */
    2092           2 :         _php_make_header_object(return_value, en TSRMLS_CC);
    2093             : 
    2094             :         /* now run through properties that are only going to be returned
    2095             :            from a server, not text headers */
    2096           4 :         add_property_string(return_value, "Recent", cache->recent ? (cache->seen ? "R": "N") : " ", 1);
    2097           2 :         add_property_string(return_value, "Unseen", (cache->recent | cache->seen) ? " " : "U", 1);
    2098           2 :         add_property_string(return_value, "Flagged", cache->flagged ? "F" : " ", 1);
    2099           2 :         add_property_string(return_value, "Answered", cache->answered ? "A" : " ", 1);
    2100           2 :         add_property_string(return_value, "Deleted", cache->deleted ? "D" : " ", 1);
    2101           2 :         add_property_string(return_value, "Draft", cache->draft ? "X" : " ", 1);
    2102             : 
    2103           2 :         snprintf(dummy, sizeof(dummy), "%4ld", cache->msgno);
    2104           2 :         add_property_string(return_value, "Msgno", dummy, 1);
    2105             : 
    2106           2 :         mail_date(dummy, cache);
    2107           2 :         add_property_string(return_value, "MailDate", dummy, 1);
    2108             : 
    2109           2 :         snprintf(dummy, sizeof(dummy), "%ld", cache->rfc822_size);
    2110           2 :         add_property_string(return_value, "Size", dummy, 1);
    2111             : 
    2112           2 :         add_property_long(return_value, "udate", mail_longdate(cache));
    2113             : 
    2114           2 :         if (en->from && fromlength) {
    2115           0 :                 fulladdress[0] = 0x00;
    2116           0 :                 mail_fetchfrom(fulladdress, imap_le_struct->imap_stream, msgno, fromlength);
    2117           0 :                 add_property_string(return_value, "fetchfrom", fulladdress, 1);
    2118             :         }
    2119           2 :         if (en->subject && subjectlength) {
    2120           0 :                 fulladdress[0] = 0x00;
    2121           0 :                 mail_fetchsubject(fulladdress, imap_le_struct->imap_stream, msgno, subjectlength);
    2122           0 :                 add_property_string(return_value, "fetchsubject", fulladdress, 1);
    2123             :         }
    2124             : }
    2125             : /* }}} */
    2126             : 
    2127             : /* {{{ proto object imap_rfc822_parse_headers(string headers [, string default_host])
    2128             :    Parse a set of mail headers contained in a string, and return an object similar to imap_headerinfo() */
    2129           0 : PHP_FUNCTION(imap_rfc822_parse_headers)
    2130             : {
    2131           0 :         char *headers, *defaulthost = NULL;
    2132             :         ENVELOPE *en;
    2133           0 :         int headers_len, defaulthost_len = 0, argc = ZEND_NUM_ARGS();
    2134             : 
    2135           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "s|s", &headers, &headers_len, &defaulthost, &defaulthost_len) == FAILURE) {
    2136           0 :                 return;
    2137             :         }
    2138             : 
    2139           0 :         if (argc == 2) {
    2140           0 :                 rfc822_parse_msg(&en, NULL, headers, headers_len, NULL, defaulthost, NIL);
    2141             :         } else {
    2142           0 :                 rfc822_parse_msg(&en, NULL, headers, headers_len, NULL, "UNKNOWN", NIL);
    2143             :         }
    2144             : 
    2145             :         /* call a function to parse all the text, so that we can use the
    2146             :            same function no matter where the headers are from */
    2147           0 :         _php_make_header_object(return_value, en TSRMLS_CC);
    2148           0 :         mail_free_envelope(&en);
    2149             : }
    2150             : /* }}} */
    2151             : 
    2152             : /* KMLANG */
    2153             : /* {{{ proto array imap_lsub(resource stream_id, string ref, string pattern)
    2154             :    Return a list of subscribed mailboxes */
    2155           7 : PHP_FUNCTION(imap_lsub)
    2156             : {
    2157             :         zval *streamind;
    2158             :         char *ref, *pat;
    2159             :         int ref_len, pat_len;
    2160             :         pils *imap_le_struct;
    2161           7 :         STRINGLIST *cur=NIL;
    2162             : 
    2163           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &streamind, &ref, &ref_len, &pat, &pat_len) == FAILURE) {
    2164           5 :                 return;
    2165             :         }
    2166             : 
    2167           2 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2168             : 
    2169             :         /* set flag for normal, old mailbox list */
    2170           2 :         IMAPG(folderlist_style) = FLIST_ARRAY;
    2171             : 
    2172           2 :         IMAPG(imap_sfolders) = NIL;
    2173           2 :         mail_lsub(imap_le_struct->imap_stream, ref, pat);
    2174           2 :         if (IMAPG(imap_sfolders) == NIL) {
    2175           1 :                 RETURN_FALSE;
    2176             :         }
    2177             : 
    2178           1 :         array_init(return_value);
    2179           1 :         cur=IMAPG(imap_sfolders);
    2180           3 :         while (cur != NIL) {
    2181           1 :                 add_next_index_string(return_value, cur->LTEXT, 1);
    2182           1 :                 cur=cur->next;
    2183             :         }
    2184           1 :         mail_free_stringlist (&IMAPG(imap_sfolders));
    2185           1 :         IMAPG(imap_sfolders) = IMAPG(imap_sfolders_tail) = NIL;
    2186             : }
    2187             : /* }}} */
    2188             : 
    2189             : /* {{{ proto array imap_getsubscribed(resource stream_id, string ref, string pattern)
    2190             :    Return a list of subscribed mailboxes, in the same format as imap_getmailboxes() */
    2191             : /* Author: CJH */
    2192           7 : PHP_FUNCTION(imap_lsub_full)
    2193             : {
    2194             :         zval *streamind, *mboxob;
    2195             :         char *ref, *pat;
    2196             :         int ref_len, pat_len;
    2197             :         pils *imap_le_struct;
    2198           7 :         FOBJECTLIST *cur=NIL;
    2199           7 :         char *delim=NIL;
    2200             : 
    2201           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &streamind, &ref, &ref_len, &pat, &pat_len) == FAILURE) {
    2202           5 :                 return;
    2203             :         }
    2204             : 
    2205           2 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2206             : 
    2207             :         /* set flag for new, improved array of objects list */
    2208           2 :         IMAPG(folderlist_style) = FLIST_OBJECT;
    2209             : 
    2210           2 :         IMAPG(imap_sfolder_objects) = IMAPG(imap_sfolder_objects_tail) = NIL;
    2211           2 :         mail_lsub(imap_le_struct->imap_stream, ref, pat);
    2212           2 :         if (IMAPG(imap_sfolder_objects) == NIL) {
    2213           1 :                 RETURN_FALSE;
    2214             :         }
    2215             : 
    2216           1 :         array_init(return_value);
    2217           1 :         delim = safe_emalloc(2, sizeof(char), 0);
    2218           1 :         cur=IMAPG(imap_sfolder_objects);
    2219           3 :         while (cur != NIL) {
    2220           1 :                 MAKE_STD_ZVAL(mboxob);
    2221           1 :                 object_init(mboxob);
    2222           1 :                 add_property_string(mboxob, "name", cur->LTEXT, 1);
    2223           1 :                 add_property_long(mboxob, "attributes", cur->attributes);
    2224             : #ifdef IMAP41
    2225           1 :                 delim[0] = (char)cur->delimiter;
    2226           1 :                 delim[1] = 0;
    2227           1 :                 add_property_string(mboxob, "delimiter", delim, 1);
    2228             : #else
    2229             :                 add_property_string(mboxob, "delimiter", cur->delimiter, 1);
    2230             : #endif
    2231           1 :                 add_next_index_object(return_value, mboxob TSRMLS_CC);
    2232           1 :                 cur=cur->next;
    2233             :         }
    2234           1 :         mail_free_foblist (&IMAPG(imap_sfolder_objects), &IMAPG(imap_sfolder_objects_tail));
    2235           1 :         efree(delim);
    2236           1 :         IMAPG(folderlist_style) = FLIST_ARRAY; /* reset to default */
    2237             : }
    2238             : /* }}} */
    2239             : 
    2240             : /* {{{ proto bool imap_subscribe(resource stream_id, string mailbox)
    2241             :    Subscribe to a mailbox */
    2242           2 : PHP_FUNCTION(imap_subscribe)
    2243             : {
    2244             :         zval *streamind;
    2245             :         char *folder;
    2246             :         int folder_len;
    2247             :         pils *imap_le_struct;
    2248             : 
    2249           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &streamind, &folder, &folder_len) == FAILURE) {
    2250           0 :                 return;
    2251             :         }
    2252             : 
    2253           2 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2254             : 
    2255           2 :         if (mail_subscribe(imap_le_struct->imap_stream, folder) == T) {
    2256           2 :                 RETURN_TRUE;
    2257             :         } else {
    2258           0 :                 RETURN_FALSE;
    2259             :         }
    2260             : }
    2261             : /* }}} */
    2262             : 
    2263             : /* {{{ proto bool imap_unsubscribe(resource stream_id, string mailbox)
    2264             :    Unsubscribe from a mailbox */
    2265           0 : PHP_FUNCTION(imap_unsubscribe)
    2266             : {
    2267             :         zval *streamind;
    2268             :         char *folder;
    2269             :         int folder_len;
    2270             :         pils *imap_le_struct;
    2271             : 
    2272           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &streamind, &folder, &folder_len) == FAILURE) {
    2273           0 :                 return;
    2274             :         }
    2275             : 
    2276           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2277             : 
    2278           0 :         if (mail_unsubscribe(imap_le_struct->imap_stream, folder) == T) {
    2279           0 :                 RETURN_TRUE;
    2280             :         } else {
    2281           0 :                 RETURN_FALSE;
    2282             :         }
    2283             : }
    2284             : /* }}} */
    2285             : 
    2286             : /* {{{ proto object imap_fetchstructure(resource stream_id, int msg_no [, int options])
    2287             :    Read the full structure of a message */
    2288           6 : PHP_FUNCTION(imap_fetchstructure)
    2289             : {
    2290             :         zval *streamind;
    2291           6 :         long msgno, flags = 0;
    2292             :         pils *imap_le_struct;
    2293             :         BODY *body;
    2294           6 :         int msgindex, argc = ZEND_NUM_ARGS();
    2295             : 
    2296           6 :         if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &streamind, &msgno, &flags) == FAILURE) {
    2297           4 :                 return;
    2298             :         }
    2299             : 
    2300           2 :         if (flags && ((flags & ~FT_UID) != 0)) {
    2301           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the options parameter");
    2302           0 :                 RETURN_FALSE;
    2303             :         }
    2304             : 
    2305           2 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2306             : 
    2307           2 :         if (msgno < 1) {
    2308           1 :                 RETURN_FALSE;
    2309             :         }
    2310             : 
    2311           1 :         object_init(return_value);
    2312             : 
    2313           1 :         if ((argc == 3) && (flags & FT_UID)) {
    2314             :                 /* This should be cached; if it causes an extra RTT to the
    2315             :                    IMAP server, then that's the price we pay for making
    2316             :                    sure we don't crash. */
    2317           0 :                 msgindex = mail_msgno(imap_le_struct->imap_stream, msgno);
    2318             :         } else {
    2319           1 :                 msgindex = msgno;
    2320             :         }
    2321           1 :         PHP_IMAP_CHECK_MSGNO(msgindex);
    2322             : 
    2323           1 :         mail_fetchstructure_full(imap_le_struct->imap_stream, msgno, &body , (argc == 3 ? flags : NIL));
    2324             : 
    2325           1 :         if (!body) {
    2326           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No body information available");
    2327           0 :                 RETURN_FALSE;
    2328             :         }
    2329             : 
    2330           1 :         _php_imap_add_body(return_value, body TSRMLS_CC);
    2331             : }
    2332             : /* }}} */
    2333             : 
    2334             : /* {{{ proto string imap_fetchbody(resource stream_id, int msg_no, string section [, int options])
    2335             :    Get a specific body section */
    2336          90 : PHP_FUNCTION(imap_fetchbody)
    2337             : {
    2338             :         zval *streamind;
    2339          90 :         long msgno, flags = 0;
    2340             :         pils *imap_le_struct;
    2341             :         char *body, *sec;
    2342             :         int sec_len;
    2343             :         unsigned long len;
    2344          90 :         int argc = ZEND_NUM_ARGS();
    2345             : 
    2346          90 :         if (zend_parse_parameters(argc TSRMLS_CC, "rls|l", &streamind, &msgno, &sec, &sec_len, &flags) == FAILURE) {
    2347          36 :                 return;
    2348             :         }
    2349             : 
    2350          54 :         if (flags && ((flags & ~(FT_UID|FT_PEEK|FT_INTERNAL)) != 0)) {
    2351           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the options parameter");
    2352           2 :                 RETURN_FALSE;
    2353             :         }
    2354             : 
    2355          52 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2356             : 
    2357          52 :         if (argc < 4 || !(flags & FT_UID)) {
    2358             :                 /* only perform the check if the msgno is a message number and not a UID */
    2359          47 :                 PHP_IMAP_CHECK_MSGNO(msgno);
    2360             :         }
    2361             : 
    2362          36 :         body = mail_fetchbody_full(imap_le_struct->imap_stream, msgno, sec, &len, (argc == 4 ? flags : NIL));
    2363             : 
    2364          36 :         if (!body) {
    2365           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No body information available");
    2366           0 :                 RETURN_FALSE;
    2367             :         }
    2368          36 :         RETVAL_STRINGL(body, len, 1);
    2369             : }
    2370             : 
    2371             : /* }}} */
    2372             : 
    2373             : 
    2374             : /* {{{ proto string imap_fetchmime(resource stream_id, int msg_no, string section [, int options])
    2375             :    Get a specific body section's MIME headers */
    2376           0 : PHP_FUNCTION(imap_fetchmime)
    2377             : {
    2378             :         zval *streamind;
    2379           0 :         long msgno, flags = 0;
    2380             :         pils *imap_le_struct;
    2381             :         char *body, *sec;
    2382             :         int sec_len;
    2383             :         unsigned long len;
    2384           0 :         int argc = ZEND_NUM_ARGS();
    2385             : 
    2386           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "rls|l", &streamind, &msgno, &sec, &sec_len, &flags) == FAILURE) {
    2387           0 :                 return;
    2388             :         }
    2389             : 
    2390           0 :         if (flags && ((flags & ~(FT_UID|FT_PEEK|FT_INTERNAL)) != 0)) {
    2391           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the options parameter");
    2392           0 :                 RETURN_FALSE;
    2393             :         }
    2394             : 
    2395           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2396             : 
    2397           0 :         if (argc < 4 || !(flags & FT_UID)) {
    2398             :                 /* only perform the check if the msgno is a message number and not a UID */
    2399           0 :                 PHP_IMAP_CHECK_MSGNO(msgno);
    2400             :         }
    2401             : 
    2402           0 :         body = mail_fetch_mime(imap_le_struct->imap_stream, msgno, sec, &len, (argc == 4 ? flags : NIL));
    2403             : 
    2404           0 :         if (!body) {
    2405           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No body MIME information available");
    2406           0 :                 RETURN_FALSE;
    2407             :         }
    2408           0 :         RETVAL_STRINGL(body, len, 1);
    2409             : }
    2410             : 
    2411             : /* }}} */
    2412             : 
    2413             : /* {{{ proto bool imap_savebody(resource stream_id, string|resource file, int msg_no[, string section = ""[, int options = 0]])
    2414             :         Save a specific body section to a file */
    2415           6 : PHP_FUNCTION(imap_savebody)
    2416             : {
    2417             :         zval *stream, **out;
    2418           6 :         pils *imap_ptr = NULL;
    2419           6 :         php_stream *writer = NULL;
    2420           6 :         char *section = "";
    2421           6 :         int section_len = 0, close_stream = 1;
    2422           6 :         long msgno, flags = 0;
    2423             : 
    2424           6 :         if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rZl|sl", &stream, &out, &msgno, &section, &section_len, &flags)) {
    2425           4 :                 RETURN_FALSE;
    2426             :         }
    2427             : 
    2428           2 :         ZEND_FETCH_RESOURCE(imap_ptr, pils *, &stream, -1, "imap", le_imap);
    2429             : 
    2430           2 :         if (!imap_ptr) {
    2431           0 :                 RETURN_FALSE;
    2432             :         }
    2433             : 
    2434           2 :         switch (Z_TYPE_PP(out))
    2435             :         {
    2436             :                 case IS_LONG:
    2437             :                 case IS_RESOURCE:
    2438           1 :                         close_stream = 0;
    2439           1 :                         php_stream_from_zval(writer, out);
    2440           1 :                 break;
    2441             : 
    2442             :                 default:
    2443           1 :                         convert_to_string_ex(out);
    2444           1 :                         writer = php_stream_open_wrapper(Z_STRVAL_PP(out), "wb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
    2445             :                 break;
    2446             :         }
    2447             : 
    2448           2 :         if (!writer) {
    2449           0 :                 RETURN_FALSE;
    2450             :         }
    2451             : 
    2452           2 :         IMAPG(gets_stream) = writer;
    2453           2 :         mail_parameters(NIL, SET_GETS, (void *) php_mail_gets);
    2454           2 :         mail_fetchbody_full(imap_ptr->imap_stream, msgno, section, NULL, flags);
    2455           2 :         mail_parameters(NIL, SET_GETS, (void *) NULL);
    2456           2 :         IMAPG(gets_stream) = NULL;
    2457             : 
    2458           2 :         if (close_stream) {
    2459           1 :                 php_stream_close(writer);
    2460             :         }
    2461             : 
    2462           2 :         RETURN_TRUE;
    2463             : }
    2464             : /* }}} */
    2465             : 
    2466             : /* {{{ proto string imap_base64(string text)
    2467             :    Decode BASE64 encoded text */
    2468           3 : PHP_FUNCTION(imap_base64)
    2469             : {
    2470             :         char *text, *decode;
    2471             :         int text_len;
    2472             :         unsigned long newlength;
    2473             : 
    2474           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &text_len) == FAILURE) {
    2475           0 :                 return;
    2476             :         }
    2477             : 
    2478           3 :         decode = (char *) rfc822_base64((unsigned char *) text, text_len, &newlength);
    2479             : 
    2480           3 :         if (decode == NULL) {
    2481           0 :                 RETURN_FALSE;
    2482             :         }
    2483             : 
    2484           3 :         RETVAL_STRINGL(decode, newlength, 1);
    2485           3 :         fs_give((void**) &decode);
    2486             : }
    2487             : /* }}} */
    2488             : 
    2489             : /* {{{ proto string imap_qprint(string text)
    2490             :    Convert a quoted-printable string to an 8-bit string */
    2491           0 : PHP_FUNCTION(imap_qprint)
    2492             : {
    2493             :         char *text, *decode;
    2494             :         int text_len;
    2495             :         unsigned long newlength;
    2496             : 
    2497           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &text_len) == FAILURE) {
    2498           0 :                 return;
    2499             :         }
    2500             : 
    2501           0 :         decode = (char *) rfc822_qprint((unsigned char *) text, text_len, &newlength);
    2502             : 
    2503           0 :         if (decode == NULL) {
    2504           0 :                 RETURN_FALSE;
    2505             :         }
    2506             : 
    2507           0 :         RETVAL_STRINGL(decode, newlength, 1);
    2508           0 :         fs_give((void**) &decode);
    2509             : }
    2510             : /* }}} */
    2511             : 
    2512             : /* {{{ proto string imap_8bit(string text)
    2513             :    Convert an 8-bit string to a quoted-printable string */
    2514           6 : PHP_FUNCTION(imap_8bit)
    2515             : {
    2516             :         char *text, *decode;
    2517             :         int text_len;
    2518             :         unsigned long newlength;
    2519             : 
    2520           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &text_len) == FAILURE) {
    2521           0 :                 return;
    2522             :         }
    2523             : 
    2524           6 :         decode = (char *) rfc822_8bit((unsigned char *) text, text_len, &newlength);
    2525             : 
    2526           6 :         if (decode == NULL) {
    2527           0 :                 RETURN_FALSE;
    2528             :         }
    2529             : 
    2530           6 :         RETVAL_STRINGL(decode, newlength, 1);
    2531           6 :         fs_give((void**) &decode);
    2532             : }
    2533             : /* }}} */
    2534             : 
    2535             : /* {{{ proto string imap_binary(string text)
    2536             :    Convert an 8bit string to a base64 string */
    2537           4 : PHP_FUNCTION(imap_binary)
    2538             : {
    2539             :         char *text, *decode;
    2540             :         int text_len;
    2541             :         unsigned long newlength;
    2542             : 
    2543           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &text, &text_len) == FAILURE) {
    2544           0 :                 return;
    2545             :         }
    2546             : 
    2547           4 :         decode = rfc822_binary(text, text_len, &newlength);
    2548             : 
    2549           4 :         if (decode == NULL) {
    2550           0 :                 RETURN_FALSE;
    2551             :         }
    2552             : 
    2553           4 :         RETVAL_STRINGL(decode, newlength, 1);
    2554           4 :         fs_give((void**) &decode);
    2555             : }
    2556             : /* }}} */
    2557             : 
    2558             : /* {{{ proto object imap_mailboxmsginfo(resource stream_id)
    2559             :    Returns info about the current mailbox */
    2560           1 : PHP_FUNCTION(imap_mailboxmsginfo)
    2561             : {
    2562             :         zval *streamind;
    2563             :         pils *imap_le_struct;
    2564             :         char date[100];
    2565             :         unsigned int msgno, unreadmsg, deletedmsg, msize;
    2566             : 
    2567           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &streamind) == FAILURE) {
    2568           0 :                 return;
    2569             :         }
    2570             : 
    2571           1 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    2572             : 
    2573             :         /* Initialize return object */
    2574           1 :         object_init(return_value);
    2575             : 
    2576           1 :         unreadmsg = 0;
    2577           1 :         deletedmsg = 0;
    2578           1 :         msize = 0;
    2579             : 
    2580           1 :         for (msgno = 1; msgno <= imap_le_struct->imap_stream->nmsgs; msgno++) {
    2581           0 :                 MESSAGECACHE * cache = mail_elt (imap_le_struct->imap_stream, msgno);
    2582           0 :                 mail_fetchstructure (imap_le_struct->imap_stream, msgno, NIL);
    2583             : 
    2584           0 :                 if (!cache->seen || cache->recent) {
    2585           0 :                         unreadmsg++;
    2586             :                 }
    2587             : 
    2588           0 :                 if (cache->deleted) {
    2589           0 :                         deletedmsg++;
    2590             :                 }
    2591           0 :                 msize = msize + cache->rfc822_size;
    2592             :         }
    2593           1 :         add_property_long(return_value, "Unread", unreadmsg);
    2594           1 :         add_property_long(return_value, "Deleted", deletedmsg);
    2595           1 :         add_property_long(return_value, "Nmsgs", imap_le_struct->imap_stream->nmsgs);
    2596           1 :         add_property_long(return_value, "Size", msize);
    2597           1 :         rfc822_date(date);
    2598           1 :         add_property_string(return_value, "Date", date, 1);
    2599           1 :         add_property_string(return_value, "Driver", imap_le_struct->imap_stream->dtb->name, 1);
    2600           1 :         add_property_string(return_value, "Mailbox", imap_le_struct->imap_stream->mailbox, 1);
    2601           1 :         add_property_long(return_value, "Recent", imap_le_struct->imap_stream->recent);
    2602             : }
    2603             : /* }}} */
    2604             : 
    2605             : /* {{{ proto string imap_rfc822_write_address(string mailbox, string host, string personal)
    2606             :    Returns a properly formatted email address given the mailbox, host, and personal info */
    2607           0 : PHP_FUNCTION(imap_rfc822_write_address)
    2608             : {
    2609             :         char *mailbox, *host, *personal;
    2610             :         int mailbox_len, host_len, personal_len;
    2611             :         ADDRESS *addr;
    2612             :         char *string;
    2613             : 
    2614           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &mailbox, &mailbox_len, &host, &host_len, &personal, &personal_len) == FAILURE) {
    2615           0 :                 return;
    2616             :         }
    2617             : 
    2618           0 :         addr=mail_newaddr();
    2619             : 
    2620           0 :         if (mailbox) {
    2621           0 :                 addr->mailbox = cpystr(mailbox);
    2622             :         }
    2623             : 
    2624           0 :         if (host) {
    2625           0 :                 addr->host = cpystr(host);
    2626             :         }
    2627             : 
    2628           0 :         if (personal) {
    2629           0 :                 addr->personal = cpystr(personal);
    2630             :         }
    2631             : 
    2632           0 :         addr->next=NIL;
    2633           0 :         addr->error=NIL;
    2634           0 :         addr->adl=NIL;
    2635             : 
    2636           0 :         string = _php_rfc822_write_address(addr TSRMLS_CC);
    2637           0 :         if (string) {
    2638           0 :                 RETVAL_STRING(string, 0);
    2639             :         } else {
    2640           0 :                 RETURN_FALSE;
    2641             :         }
    2642             : }
    2643             : /* }}} */
    2644             : 
    2645             : /* {{{ proto array imap_rfc822_parse_adrlist(string address_string, string default_host)
    2646             :    Parses an address string */
    2647           2 : PHP_FUNCTION(imap_rfc822_parse_adrlist)
    2648             : {
    2649             :         zval *tovals;
    2650             :         char *str, *defaulthost, *str_copy;
    2651             :         int str_len, defaulthost_len;
    2652             :         ADDRESS *addresstmp;
    2653             :         ENVELOPE *env;
    2654             : 
    2655           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &str, &str_len, &defaulthost, &defaulthost_len) == FAILURE) {
    2656           0 :                 return;
    2657             :         }
    2658             : 
    2659           2 :         env = mail_newenvelope();
    2660             : 
    2661             :         /* rfc822_parse_adrlist() modifies passed string. Copy it. */
    2662           2 :         str_copy = estrndup(str, str_len);
    2663           2 :         rfc822_parse_adrlist(&env->to, str_copy, defaulthost);
    2664           2 :         efree(str_copy);
    2665             : 
    2666           2 :         array_init(return_value);
    2667             : 
    2668           2 :         addresstmp = env->to;
    2669             : 
    2670           2 :         if (addresstmp) do {
    2671          10 :                 MAKE_STD_ZVAL(tovals);
    2672          10 :                 object_init(tovals);
    2673          10 :                 if (addresstmp->mailbox) {
    2674          10 :                         add_property_string(tovals, "mailbox", addresstmp->mailbox, 1);
    2675             :                 }
    2676          10 :                 if (addresstmp->host) {
    2677          10 :                         add_property_string(tovals, "host", addresstmp->host, 1);
    2678             :                 }
    2679          10 :                 if (addresstmp->personal) {
    2680           3 :                         add_property_string(tovals, "personal", addresstmp->personal, 1);
    2681             :                 }
    2682          10 :                 if (addresstmp->adl) {
    2683           1 :                         add_property_string(tovals, "adl", addresstmp->adl, 1);
    2684             :                 }
    2685          10 :                 add_next_index_object(return_value, tovals TSRMLS_CC);
    2686          10 :         } while ((addresstmp = addresstmp->next));
    2687             : 
    2688           2 :         mail_free_envelope(&env);
    2689             : }
    2690             : /* }}} */
    2691             : 
    2692             : /* {{{ proto string imap_utf8(string mime_encoded_text)
    2693             :    Convert a mime-encoded text to UTF-8 */
    2694           5 : PHP_FUNCTION(imap_utf8)
    2695             : {
    2696             :         char *str;
    2697             :         int str_len;
    2698             :         SIZEDTEXT src, dest;
    2699             : 
    2700           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
    2701           1 :                 return;
    2702             :         }
    2703             : 
    2704           4 :         src.data  = NULL;
    2705           4 :         src.size  = 0;
    2706           4 :         dest.data = NULL;
    2707           4 :         dest.size = 0;
    2708             : 
    2709           4 :         cpytxt(&src, str, str_len);
    2710             : 
    2711             : #ifndef HAVE_NEW_MIME2TEXT
    2712             :         utf8_mime2text(&src, &dest);
    2713             : #else
    2714           4 :         utf8_mime2text(&src, &dest, U8T_DECOMPOSE);
    2715             : #endif
    2716           4 :         RETVAL_STRINGL(dest.data, dest.size, 1);
    2717           4 :         if (dest.data) {
    2718           4 :                 free(dest.data);
    2719             :         }
    2720           4 :         if (src.data && src.data != dest.data) {
    2721           4 :                 free(src.data);
    2722             :         }
    2723             : }
    2724             : /* }}} */
    2725             : 
    2726             : /* {{{ macros for the modified utf7 conversion functions
    2727             :  *
    2728             :  * author: Andrew Skalski <askalski@chek.com>
    2729             :  */
    2730             : 
    2731             : /* tests `c' and returns true if it is a special character */
    2732             : #define SPECIAL(c) ((c) <= 0x1f || (c) >= 0x7f)
    2733             : 
    2734             : /* validate a modified-base64 character */
    2735             : #define B64CHAR(c) (isalnum(c) || (c) == '+' || (c) == ',')
    2736             : 
    2737             : /* map the low 64 bits of `n' to the modified-base64 characters */
    2738             : #define B64(n)  ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
    2739             :                                 "abcdefghijklmnopqrstuvwxyz0123456789+,"[(n) & 0x3f])
    2740             : 
    2741             : /* map the modified-base64 character `c' to its 64 bit value */
    2742             : #define UNB64(c)        ((c) == '+' ? 62 : (c) == ',' ? 63 : (c) >= 'a' ? \
    2743             :                                         (c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4)
    2744             : /* }}} */
    2745             : 
    2746             : /* {{{ proto string imap_utf7_decode(string buf)
    2747             :    Decode a modified UTF-7 string */
    2748           0 : PHP_FUNCTION(imap_utf7_decode)
    2749             : {
    2750             :         /* author: Andrew Skalski <askalski@chek.com> */
    2751             :         char *arg;
    2752             :         const unsigned char *in, *inp, *endp;
    2753             :         unsigned char *out, *outp;
    2754             :         unsigned char c;
    2755             :         int arg_len, inlen, outlen;
    2756             :         enum {
    2757             :                 ST_NORMAL,      /* printable text */
    2758             :                 ST_DECODE0,     /* encoded text rotation... */
    2759             :                 ST_DECODE1,
    2760             :                 ST_DECODE2,
    2761             :                 ST_DECODE3
    2762             :         } state;
    2763             : 
    2764           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
    2765           0 :                 return;
    2766             :         }
    2767             : 
    2768           0 :         in = (const unsigned char *) arg;
    2769           0 :         inlen = arg_len;
    2770             : 
    2771             :         /* validate and compute length of output string */
    2772           0 :         outlen = 0;
    2773           0 :         state = ST_NORMAL;
    2774           0 :         for (endp = (inp = in) + inlen; inp < endp; inp++) {
    2775           0 :                 if (state == ST_NORMAL) {
    2776             :                         /* process printable character */
    2777           0 :                         if (SPECIAL(*inp)) {
    2778           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid modified UTF-7 character: `%c'", *inp);
    2779           0 :                                 RETURN_FALSE;
    2780           0 :                         } else if (*inp != '&') {
    2781           0 :                                 outlen++;
    2782           0 :                         } else if (inp + 1 == endp) {
    2783           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected end of string");
    2784           0 :                                 RETURN_FALSE;
    2785           0 :                         } else if (inp[1] != '-') {
    2786           0 :                                 state = ST_DECODE0;
    2787             :                         } else {
    2788           0 :                                 outlen++;
    2789           0 :                                 inp++;
    2790             :                         }
    2791           0 :                 } else if (*inp == '-') {
    2792             :                         /* return to NORMAL mode */
    2793           0 :                         if (state == ST_DECODE1) {
    2794           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Stray modified base64 character: `%c'", *--inp);
    2795           0 :                                 RETURN_FALSE;
    2796             :                         }
    2797           0 :                         state = ST_NORMAL;
    2798           0 :                 } else if (!B64CHAR(*inp)) {
    2799           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid modified base64 character: `%c'", *inp);
    2800           0 :                         RETURN_FALSE;
    2801             :                 } else {
    2802           0 :                         switch (state) {
    2803             :                                 case ST_DECODE3:
    2804           0 :                                         outlen++;
    2805           0 :                                         state = ST_DECODE0;
    2806           0 :                                         break;
    2807             :                                 case ST_DECODE2:
    2808             :                                 case ST_DECODE1:
    2809           0 :                                         outlen++;
    2810             :                                 case ST_DECODE0:
    2811           0 :                                         state++;
    2812             :                                 case ST_NORMAL:
    2813             :                                         break;
    2814             :                         }
    2815             :                 }
    2816             :         }
    2817             : 
    2818             :         /* enforce end state */
    2819           0 :         if (state != ST_NORMAL) {
    2820           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unexpected end of string");
    2821           0 :                 RETURN_FALSE;
    2822             :         }
    2823             : 
    2824             :         /* allocate output buffer */
    2825           0 :         out = emalloc(outlen + 1);
    2826             : 
    2827             :         /* decode input string */
    2828           0 :         outp = out;
    2829           0 :         state = ST_NORMAL;
    2830           0 :         for (endp = (inp = in) + inlen; inp < endp; inp++) {
    2831           0 :                 if (state == ST_NORMAL) {
    2832           0 :                         if (*inp == '&' && inp[1] != '-') {
    2833           0 :                                 state = ST_DECODE0;
    2834             :                         }
    2835           0 :                         else if ((*outp++ = *inp) == '&') {
    2836           0 :                                 inp++;
    2837             :                         }
    2838             :                 }
    2839           0 :                 else if (*inp == '-') {
    2840           0 :                         state = ST_NORMAL;
    2841             :                 }
    2842             :                 else {
    2843             :                         /* decode input character */
    2844           0 :                         switch (state) {
    2845             :                         case ST_DECODE0:
    2846           0 :                                 *outp = UNB64(*inp) << 2;
    2847           0 :                                 state = ST_DECODE1;
    2848           0 :                                 break;
    2849             :                         case ST_DECODE1:
    2850           0 :                                 outp[1] = UNB64(*inp);
    2851           0 :                                 c = outp[1] >> 4;
    2852           0 :                                 *outp++ |= c;
    2853           0 :                                 *outp <<= 4;
    2854           0 :                                 state = ST_DECODE2;
    2855           0 :                                 break;
    2856             :                         case ST_DECODE2:
    2857           0 :                                 outp[1] = UNB64(*inp);
    2858           0 :                                 c = outp[1] >> 2;
    2859           0 :                                 *outp++ |= c;
    2860           0 :                                 *outp <<= 6;
    2861           0 :                                 state = ST_DECODE3;
    2862           0 :                                 break;
    2863             :                         case ST_DECODE3:
    2864           0 :                                 *outp++ |= UNB64(*inp);
    2865           0 :                                 state = ST_DECODE0;
    2866             :                         case ST_NORMAL:
    2867             :                                 break;
    2868             :                         }
    2869             :                 }
    2870             :         }
    2871             : 
    2872           0 :         *outp = 0;
    2873             : 
    2874             : #if PHP_DEBUG
    2875             :         /* warn if we computed outlen incorrectly */
    2876             :         if (outp - out != outlen) {
    2877             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "outp - out [%ld] != outlen [%d]", outp - out, outlen);
    2878             :         }
    2879             : #endif
    2880             : 
    2881           0 :         RETURN_STRINGL(out, outlen, 0);
    2882             : }
    2883             : /* }}} */
    2884             : 
    2885             : /* {{{ proto string imap_utf7_encode(string buf)
    2886             :    Encode a string in modified UTF-7 */
    2887           1 : PHP_FUNCTION(imap_utf7_encode)
    2888             : {
    2889             :         /* author: Andrew Skalski <askalski@chek.com> */
    2890             :         char *arg;
    2891             :         const unsigned char *in, *inp, *endp;
    2892             :         unsigned char *out, *outp;
    2893             :         unsigned char c;
    2894             :         int arg_len, inlen, outlen;
    2895             :         enum {
    2896             :                 ST_NORMAL,      /* printable text */
    2897             :                 ST_ENCODE0,     /* encoded text rotation... */
    2898             :                 ST_ENCODE1,
    2899             :                 ST_ENCODE2
    2900             :         } state;
    2901             : 
    2902           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
    2903           0 :                 return;
    2904             :         }
    2905             : 
    2906           1 :         in = (const unsigned char *) arg;
    2907           1 :         inlen = arg_len;
    2908             : 
    2909             :         /* compute the length of the result string */
    2910           1 :         outlen = 0;
    2911           1 :         state = ST_NORMAL;
    2912           1 :         endp = (inp = in) + inlen;
    2913          28 :         while (inp < endp) {
    2914          26 :                 if (state == ST_NORMAL) {
    2915          26 :                         if (SPECIAL(*inp)) {
    2916           0 :                                 state = ST_ENCODE0;
    2917           0 :                                 outlen++;
    2918          26 :                         } else if (*inp++ == '&') {
    2919           0 :                                 outlen++;
    2920             :                         }
    2921          26 :                         outlen++;
    2922           0 :                 } else if (!SPECIAL(*inp)) {
    2923           0 :                         state = ST_NORMAL;
    2924             :                 } else {
    2925             :                         /* ST_ENCODE0 -> ST_ENCODE1  - two chars
    2926             :                          * ST_ENCODE1 -> ST_ENCODE2  - one char
    2927             :                          * ST_ENCODE2 -> ST_ENCODE0  - one char
    2928             :                          */
    2929           0 :                         if (state == ST_ENCODE2) {
    2930           0 :                                 state = ST_ENCODE0;
    2931             :                         }
    2932           0 :                         else if (state++ == ST_ENCODE0) {
    2933           0 :                                 outlen++;
    2934             :                         }
    2935           0 :                         outlen++;
    2936           0 :                         inp++;
    2937             :                 }
    2938             :         }
    2939             : 
    2940             :         /* allocate output buffer */
    2941           1 :         out = emalloc(outlen + 1);
    2942             : 
    2943             :         /* encode input string */
    2944           1 :         outp = out;
    2945           1 :         state = ST_NORMAL;
    2946           1 :         endp = (inp = in) + inlen;
    2947          28 :         while (inp < endp || state != ST_NORMAL) {
    2948          26 :                 if (state == ST_NORMAL) {
    2949          26 :                         if (SPECIAL(*inp)) {
    2950             :                                 /* begin encoding */
    2951           0 :                                 *outp++ = '&';
    2952           0 :                                 state = ST_ENCODE0;
    2953          26 :                         } else if ((*outp++ = *inp++) == '&') {
    2954           0 :                                 *outp++ = '-';
    2955             :                         }
    2956           0 :                 } else if (inp == endp || !SPECIAL(*inp)) {
    2957             :                         /* flush overflow and terminate region */
    2958           0 :                         if (state != ST_ENCODE0) {
    2959           0 :                                 c = B64(*outp);
    2960           0 :                                 *outp++ = c;
    2961             :                         }
    2962           0 :                         *outp++ = '-';
    2963           0 :                         state = ST_NORMAL;
    2964             :                 } else {
    2965             :                         /* encode input character */
    2966           0 :                         switch (state) {
    2967             :                                 case ST_ENCODE0:
    2968           0 :                                         *outp++ = B64(*inp >> 2);
    2969           0 :                                         *outp = *inp++ << 4;
    2970           0 :                                         state = ST_ENCODE1;
    2971           0 :                                         break;
    2972             :                                 case ST_ENCODE1:
    2973           0 :                                         c = B64(*outp | *inp >> 4);
    2974           0 :                                         *outp++ = c;
    2975           0 :                                         *outp = *inp++ << 2;
    2976           0 :                                         state = ST_ENCODE2;
    2977           0 :                                         break;
    2978             :                                 case ST_ENCODE2:
    2979           0 :                                         c = B64(*outp | *inp >> 6);
    2980           0 :                                         *outp++ = c;
    2981           0 :                                         *outp++ = B64(*inp++);
    2982           0 :                                         state = ST_ENCODE0;
    2983             :                                 case ST_NORMAL:
    2984             :                                         break;
    2985             :                         }
    2986             :                 }
    2987             :         }
    2988             : 
    2989           1 :         *outp = 0;
    2990             : 
    2991             : #if PHP_DEBUG
    2992             :         /* warn if we computed outlen incorrectly */
    2993             :         if (outp - out != outlen) {
    2994             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "outp - out [%ld] != outlen [%d]", outp - out, outlen);
    2995             :         }
    2996             : #endif
    2997             : 
    2998           1 :         RETURN_STRINGL(out, outlen, 0);
    2999             : }
    3000             : /* }}} */
    3001             : 
    3002             : #undef SPECIAL
    3003             : #undef B64CHAR
    3004             : #undef B64
    3005             : #undef UNB64
    3006             : 
    3007             : #ifdef HAVE_IMAP_MUTF7
    3008             : static void php_imap_mutf7(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
    3009             : {
    3010             :         char *in;
    3011             :         int in_len;
    3012             :         unsigned char *out;
    3013             : 
    3014             :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &in, &in_len) == FAILURE) {
    3015             :                 return;
    3016             :         }
    3017             : 
    3018             :         if (in_len < 1) {
    3019             :                 RETURN_EMPTY_STRING();
    3020             :         }
    3021             : 
    3022             :         if (mode == 0) {
    3023             :                 out = utf8_to_mutf7((unsigned char *) in);
    3024             :         } else {
    3025             :                 out = utf8_from_mutf7((unsigned char *) in);
    3026             :         }
    3027             : 
    3028             :         if (out == NIL) {
    3029             :                 RETURN_FALSE;
    3030             :         } else {
    3031             :                 RETURN_STRING((char *)out, 1);
    3032             :         }
    3033             : }
    3034             : /* }}} */
    3035             : 
    3036             : /* {{{ proto string imap_utf8_to_mutf7(string in)
    3037             :    Encode a UTF-8 string to modified UTF-7 */
    3038             : PHP_FUNCTION(imap_utf8_to_mutf7)
    3039             : {
    3040             :         php_imap_mutf7(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    3041             : }
    3042             : /* }}} */
    3043             : 
    3044             : /* {{{ proto string imap_mutf7_to_utf8(string in)
    3045             :    Decode a modified UTF-7 string to UTF-8 */
    3046             : PHP_FUNCTION(imap_mutf7_to_utf8)
    3047             : {
    3048             :         php_imap_mutf7(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    3049             : }
    3050             : /* }}} */
    3051             : #endif
    3052             : 
    3053             : /* {{{ proto bool imap_setflag_full(resource stream_id, string sequence, string flag [, int options])
    3054             :    Sets flags on messages */
    3055           5 : PHP_FUNCTION(imap_setflag_full)
    3056             : {
    3057             :         zval *streamind;
    3058             :         char *sequence, *flag;
    3059             :         int sequence_len, flag_len;
    3060           5 :         long flags = 0;
    3061             :         pils *imap_le_struct;
    3062             : 
    3063           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss|l", &streamind, &sequence, &sequence_len, &flag, &flag_len, &flags) == FAILURE) {
    3064           0 :                 return;
    3065             :         }
    3066             : 
    3067           5 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3068             : 
    3069           5 :         mail_setflag_full(imap_le_struct->imap_stream, sequence, flag, (flags ? flags : NIL));
    3070           5 :         RETURN_TRUE;
    3071             : }
    3072             : /* }}} */
    3073             : 
    3074             : /* {{{ proto bool imap_clearflag_full(resource stream_id, string sequence, string flag [, int options])
    3075             :    Clears flags on messages */
    3076           3 : PHP_FUNCTION(imap_clearflag_full)
    3077             : {
    3078             :         zval *streamind;
    3079             :         char *sequence, *flag;
    3080             :         int sequence_len, flag_len;
    3081           3 :         long flags = 0;
    3082             :         pils *imap_le_struct;
    3083           3 :         int argc = ZEND_NUM_ARGS();
    3084             : 
    3085           3 :         if (zend_parse_parameters(argc TSRMLS_CC, "rss|l", &streamind, &sequence, &sequence_len, &flag, &flag_len, &flags) ==FAILURE) {
    3086           0 :                 return;
    3087             :         }
    3088             : 
    3089           3 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3090             : 
    3091           3 :         mail_clearflag_full(imap_le_struct->imap_stream, sequence, flag, (argc == 4 ? flags : NIL));
    3092           3 :         RETURN_TRUE;
    3093             : }
    3094             : /* }}} */
    3095             : 
    3096             : /* {{{ proto array imap_sort(resource stream_id, int criteria, int reverse [, int options [, string search_criteria [, string charset]]])
    3097             :    Sort an array of message headers, optionally including only messages that meet specified criteria. */
    3098           0 : PHP_FUNCTION(imap_sort)
    3099             : {
    3100             :         zval *streamind;
    3101           0 :         char *criteria = NULL, *charset = NULL;
    3102             :         int criteria_len, charset_len;
    3103           0 :         long pgm, rev, flags = 0;
    3104             :         pils *imap_le_struct;
    3105             :         unsigned long *slst, *sl;
    3106             :         char *search_criteria;
    3107           0 :         SORTPGM *mypgm=NIL;
    3108           0 :         SEARCHPGM *spg=NIL;
    3109           0 :         int argc = ZEND_NUM_ARGS();
    3110             : 
    3111           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "rll|lss", &streamind, &pgm, &rev, &flags, &criteria, &criteria_len, &charset, &charset_len) == FAILURE) {
    3112           0 :                 return;
    3113             :         }
    3114             : 
    3115           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3116             : 
    3117           0 :         if (pgm > SORTSIZE) {
    3118           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unrecognized sort criteria");
    3119           0 :                 RETURN_FALSE;
    3120             :         }
    3121           0 :         if (argc >= 4) {
    3122           0 :                 if (flags < 0) {
    3123           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Search options parameter has to be greater than or equal to 0");
    3124           0 :                         RETURN_FALSE;
    3125             :                 }
    3126             :         }
    3127           0 :         if (argc >= 5) {
    3128           0 :                 search_criteria = estrndup(criteria, criteria_len);
    3129           0 :                 spg = mail_criteria(search_criteria);
    3130           0 :                 efree(search_criteria);
    3131             :         } else {
    3132           0 :                 spg = mail_newsearchpgm();
    3133             :         }
    3134             : 
    3135           0 :         mypgm = mail_newsortpgm();
    3136           0 :         mypgm->reverse = rev;
    3137           0 :         mypgm->function = (short) pgm;
    3138           0 :         mypgm->next = NIL;
    3139             : 
    3140           0 :         slst = mail_sort(imap_le_struct->imap_stream, (argc == 6 ? charset : NIL), spg, mypgm, (argc >= 4 ? flags : NIL));
    3141             : 
    3142           0 :         if (spg && !(flags & SE_FREE)) {
    3143           0 :                 mail_free_searchpgm(&spg);
    3144             :         }
    3145             : 
    3146           0 :         array_init(return_value);
    3147           0 :         if (slst != NIL && slst != 0) {
    3148           0 :                 for (sl = slst; *sl; sl++) {
    3149           0 :                         add_next_index_long(return_value, *sl);
    3150             :                 }
    3151           0 :                 fs_give ((void **) &slst);
    3152             :         }
    3153             : }
    3154             : /* }}} */
    3155             : 
    3156             : /* {{{ proto string imap_fetchheader(resource stream_id, int msg_no [, int options])
    3157             :    Get the full unfiltered header for a message */
    3158          73 : PHP_FUNCTION(imap_fetchheader)
    3159             : {
    3160             :         zval *streamind;
    3161          73 :         long msgno, flags=0L;
    3162             :         pils *imap_le_struct;
    3163          73 :         int msgindex, argc = ZEND_NUM_ARGS();
    3164             : 
    3165          73 :         if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &streamind, &msgno, &flags) == FAILURE) {
    3166          40 :                 return;
    3167             :         }
    3168             : 
    3169          33 :         if (flags && ((flags & ~(FT_UID|FT_INTERNAL|FT_PREFETCHTEXT)) != 0)) {
    3170           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the options parameter");
    3171           2 :                 RETURN_FALSE;
    3172             :         }
    3173             : 
    3174          31 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3175             : 
    3176          34 :         if ((argc == 3) && (flags & FT_UID)) {
    3177             :                 /* This should be cached; if it causes an extra RTT to the
    3178             :                    IMAP server, then that's the price we pay for making sure
    3179             :                    we don't crash. */
    3180           5 :                 msgindex = mail_msgno(imap_le_struct->imap_stream, msgno);
    3181             :         } else {
    3182          24 :                 msgindex = msgno;
    3183             :         }
    3184             : 
    3185          29 :         PHP_IMAP_CHECK_MSGNO(msgindex);
    3186             : 
    3187          13 :         RETVAL_STRING(mail_fetchheader_full(imap_le_struct->imap_stream, msgno, NIL, NIL, (argc == 3 ? flags : NIL)), 1);
    3188             : }
    3189             : /* }}} */
    3190             : 
    3191             : /* {{{ proto int imap_uid(resource stream_id, int msg_no)
    3192             :    Get the unique message id associated with a standard sequential message number */
    3193           6 : PHP_FUNCTION(imap_uid)
    3194             : {
    3195             :         zval *streamind;
    3196             :         long msgno;
    3197             :         pils *imap_le_struct;
    3198             :         int msgindex;
    3199             : 
    3200           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &streamind, &msgno) == FAILURE) {
    3201           0 :                 return;
    3202             :         }
    3203             : 
    3204           6 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3205             : 
    3206           6 :         msgindex = msgno;
    3207           6 :         if ((msgindex < 1) || ((unsigned) msgindex > imap_le_struct->imap_stream->nmsgs)) {
    3208           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad message number");
    3209           0 :                 RETURN_FALSE;
    3210             :         }
    3211             : 
    3212           6 :         RETURN_LONG(mail_uid(imap_le_struct->imap_stream, msgno));
    3213             : }
    3214             : /* }}} */
    3215             : 
    3216             : /* {{{ proto int imap_msgno(resource stream_id, int unique_msg_id)
    3217             :    Get the sequence number associated with a UID */
    3218           0 : PHP_FUNCTION(imap_msgno)
    3219             : {
    3220             :         zval *streamind;
    3221             :         long msgno;
    3222             :         pils *imap_le_struct;
    3223             : 
    3224           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &streamind, &msgno) == FAILURE) {
    3225           0 :                 return;
    3226             :         }
    3227             : 
    3228           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3229             : 
    3230           0 :         RETURN_LONG(mail_msgno(imap_le_struct->imap_stream, msgno));
    3231             : }
    3232             : /* }}} */
    3233             : 
    3234             : /* {{{ proto object imap_status(resource stream_id, string mailbox, int options)
    3235             :    Get status info from a mailbox */
    3236           1 : PHP_FUNCTION(imap_status)
    3237             : {
    3238             :         zval *streamind;
    3239             :         char *mbx;
    3240             :         int mbx_len;
    3241             :         long flags;
    3242             :         pils *imap_le_struct;
    3243             : 
    3244           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsl", &streamind, &mbx, &mbx_len, &flags) == FAILURE) {
    3245           0 :                 return;
    3246             :         }
    3247             : 
    3248           1 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3249             : 
    3250           1 :         object_init(return_value);
    3251             : 
    3252           1 :         if (mail_status(imap_le_struct->imap_stream, mbx, flags)) {
    3253           1 :                 add_property_long(return_value, "flags", IMAPG(status_flags));
    3254           1 :                 if (IMAPG(status_flags) & SA_MESSAGES) {
    3255           1 :                         add_property_long(return_value, "messages", IMAPG(status_messages));
    3256             :                 }
    3257           1 :                 if (IMAPG(status_flags) & SA_RECENT) {
    3258           1 :                         add_property_long(return_value, "recent", IMAPG(status_recent));
    3259             :                 }
    3260           1 :                 if (IMAPG(status_flags) & SA_UNSEEN) {
    3261           1 :                         add_property_long(return_value, "unseen", IMAPG(status_unseen));
    3262             :                 }
    3263           1 :                 if (IMAPG(status_flags) & SA_UIDNEXT) {
    3264           1 :                         add_property_long(return_value, "uidnext", IMAPG(status_uidnext));
    3265             :                 }
    3266           1 :                 if (IMAPG(status_flags) & SA_UIDVALIDITY) {
    3267           1 :                         add_property_long(return_value, "uidvalidity", IMAPG(status_uidvalidity));
    3268             :                 }
    3269             :         } else {
    3270           0 :                 RETURN_FALSE;
    3271             :         }
    3272             : }
    3273             : /* }}} */
    3274             : 
    3275             : /* {{{ proto object imap_bodystruct(resource stream_id, int msg_no, string section)
    3276             :    Read the structure of a specified body section of a specific message */
    3277           2 : PHP_FUNCTION(imap_bodystruct)
    3278             : {
    3279             :         zval *streamind;
    3280             :         long msg;
    3281             :         char *section;
    3282             :         int section_len;
    3283             :         pils *imap_le_struct;
    3284             :         zval *parametres, *param, *dparametres, *dparam;
    3285             :         PARAMETER *par, *dpar;
    3286             :         BODY *body;
    3287             : 
    3288           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rls", &streamind, &msg, &section, &section_len) == FAILURE) {
    3289           0 :                 return;
    3290             :         }
    3291             : 
    3292           2 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3293             : 
    3294           2 :         if (!msg || msg < 1 || (unsigned) msg > imap_le_struct->imap_stream->nmsgs) {
    3295           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad message number");
    3296           0 :                 RETURN_FALSE;
    3297             :         }
    3298             : 
    3299           2 :         object_init(return_value);
    3300             : 
    3301           2 :         body=mail_body(imap_le_struct->imap_stream, msg, section);
    3302           2 :         if (body == NULL) {
    3303           1 :                 zval_dtor(return_value);
    3304           1 :                 RETURN_FALSE;
    3305             :         }
    3306           1 :         if (body->type <= TYPEMAX) {
    3307           1 :                 add_property_long(return_value, "type", body->type);
    3308             :         }
    3309           1 :         if (body->encoding <= ENCMAX) {
    3310           1 :                 add_property_long(return_value, "encoding", body->encoding);
    3311             :         }
    3312             : 
    3313           1 :         if (body->subtype) {
    3314           1 :                 add_property_long(return_value, "ifsubtype", 1);
    3315           1 :                 add_property_string(return_value, "subtype", body->subtype, 1);
    3316             :         } else {
    3317           0 :                 add_property_long(return_value, "ifsubtype", 0);
    3318             :         }
    3319             : 
    3320           1 :         if (body->description) {
    3321           1 :                 add_property_long(return_value, "ifdescription", 1);
    3322           1 :                 add_property_string(return_value, "description", body->description, 1);
    3323             :         } else {
    3324           0 :                 add_property_long(return_value, "ifdescription", 0);
    3325             :         }
    3326           1 :         if (body->id) {
    3327           0 :                 add_property_long(return_value, "ifid", 1);
    3328           0 :                 add_property_string(return_value, "id", body->id, 1);
    3329             :         } else {
    3330           1 :                 add_property_long(return_value, "ifid", 0);
    3331             :         }
    3332             : 
    3333           1 :         if (body->size.lines) {
    3334           0 :                 add_property_long(return_value, "lines", body->size.lines);
    3335             :         }
    3336           1 :         if (body->size.bytes) {
    3337           1 :                 add_property_long(return_value, "bytes", body->size.bytes);
    3338             :         }
    3339             : #ifdef IMAP41
    3340           1 :         if (body->disposition.type) {
    3341           0 :                 add_property_long(return_value, "ifdisposition", 1);
    3342           0 :                 add_property_string(return_value, "disposition", body->disposition.type, 1);
    3343             :         } else {
    3344           1 :                 add_property_long(return_value, "ifdisposition", 0);
    3345             :         }
    3346             : 
    3347           1 :         if (body->disposition.parameter) {
    3348           0 :                 dpar = body->disposition.parameter;
    3349           0 :                 add_property_long(return_value, "ifdparameters", 1);
    3350           0 :                 MAKE_STD_ZVAL(dparametres);
    3351           0 :                 array_init(dparametres);
    3352             :                 do {
    3353           0 :                         MAKE_STD_ZVAL(dparam);
    3354           0 :                         object_init(dparam);
    3355           0 :                         add_property_string(dparam, "attribute", dpar->attribute, 1);
    3356           0 :                         add_property_string(dparam, "value", dpar->value, 1);
    3357           0 :                         add_next_index_object(dparametres, dparam TSRMLS_CC);
    3358           0 :                 } while ((dpar = dpar->next));
    3359           0 :                 add_assoc_object(return_value, "dparameters", dparametres TSRMLS_CC);
    3360             :         } else {
    3361           1 :                 add_property_long(return_value, "ifdparameters", 0);
    3362             :         }
    3363             : #endif
    3364             : 
    3365           1 :         if ((par = body->parameter)) {
    3366           1 :                 add_property_long(return_value, "ifparameters", 1);
    3367             : 
    3368           1 :                 MAKE_STD_ZVAL(parametres);
    3369           1 :                 array_init(parametres);
    3370             :                 do {
    3371           1 :                         MAKE_STD_ZVAL(param);
    3372           1 :                         object_init(param);
    3373           1 :                         if (par->attribute) {
    3374           1 :                                 add_property_string(param, "attribute", par->attribute, 1);
    3375             :                         }
    3376           1 :                         if (par->value) {
    3377           1 :                                 add_property_string(param, "value", par->value, 1);
    3378             :                         }
    3379             : 
    3380           1 :                         add_next_index_object(parametres, param TSRMLS_CC);
    3381           1 :                 } while ((par = par->next));
    3382             :         } else {
    3383           0 :                 MAKE_STD_ZVAL(parametres);
    3384           0 :                 object_init(parametres);
    3385           0 :                 add_property_long(return_value, "ifparameters", 0);
    3386             :         }
    3387           1 :         add_assoc_object(return_value, "parameters", parametres TSRMLS_CC);
    3388             : }
    3389             : 
    3390             : /* }}} */
    3391             : 
    3392             : /* {{{ proto array imap_fetch_overview(resource stream_id, string sequence [, int options])
    3393             :    Read an overview of the information in the headers of the given message sequence */
    3394          71 : PHP_FUNCTION(imap_fetch_overview)
    3395             : {
    3396             :         zval *streamind;
    3397             :         char *sequence;
    3398             :         int sequence_len;
    3399             :         pils *imap_le_struct;
    3400             :         zval *myoverview;
    3401             :         char *address;
    3402          71 :         long status, flags = 0L;
    3403          71 :         int argc = ZEND_NUM_ARGS();
    3404             : 
    3405          71 :         if (zend_parse_parameters(argc TSRMLS_CC, "rs|l", &streamind, &sequence, &sequence_len, &flags) == FAILURE) {
    3406          28 :                 return;
    3407             :         }
    3408             : 
    3409          43 :         if (flags && ((flags & ~FT_UID) != 0)) {
    3410           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for the options parameter");
    3411           2 :                 RETURN_FALSE;
    3412             :         }
    3413             : 
    3414          41 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    3415             : 
    3416          39 :         array_init(return_value);
    3417             : 
    3418          78 :         status = (flags & FT_UID)
    3419           5 :                 ? mail_uid_sequence(imap_le_struct->imap_stream, sequence)
    3420          34 :                 : mail_sequence(imap_le_struct->imap_stream, sequence);
    3421             : 
    3422          39 :         if (status) {
    3423             :                 MESSAGECACHE *elt;
    3424             :                 ENVELOPE *env;
    3425             :                 unsigned long i;
    3426             : 
    3427          54 :                 for (i = 1; i <= imap_le_struct->imap_stream->nmsgs; i++) {
    3428          50 :                         if (((elt = mail_elt (imap_le_struct->imap_stream, i))->sequence) &&
    3429          19 :                                 (env = mail_fetch_structure (imap_le_struct->imap_stream, i, NIL, NIL))) {
    3430          19 :                                 MAKE_STD_ZVAL(myoverview);
    3431          19 :                                 object_init(myoverview);
    3432          19 :                                 if (env->subject) {
    3433          19 :                                         add_property_string(myoverview, "subject", env->subject, 1);
    3434             :                                 }
    3435          19 :                                 if (env->from) {
    3436          19 :                                         env->from->next=NULL;
    3437          19 :                                         address =_php_rfc822_write_address(env->from TSRMLS_CC);
    3438          19 :                                         if (address) {
    3439          19 :                                                 add_property_string(myoverview, "from", address, 0);
    3440             :                                         }
    3441             :                                 }
    3442          19 :                                 if (env->to) {
    3443          19 :                                         env->to->next = NULL;
    3444          19 :                                         address = _php_rfc822_write_address(env->to TSRMLS_CC);
    3445          19 :                                         if (address) {
    3446          19 :                                                 add_property_string(myoverview, "to", address, 0);
    3447             :                                         }
    3448             :                                 }
    3449          19 :                                 if (env->date) {
    3450           0 :                                         add_property_string(myoverview, "date", env->date, 1);
    3451             :                                 }
    3452          19 :                                 if (env->message_id) {
    3453           0 :                                         add_property_string(myoverview, "message_id", env->message_id, 1);
    3454             :                                 }
    3455          19 :                                 if (env->references) {
    3456           0 :                                         add_property_string(myoverview, "references", env->references, 1);
    3457             :                                 }
    3458          19 :                                 if (env->in_reply_to) {
    3459           0 :                                         add_property_string(myoverview, "in_reply_to", env->in_reply_to, 1);
    3460             :                                 }
    3461          19 :                                 add_property_long(myoverview, "size", elt->rfc822_size);
    3462          19 :                                 add_property_long(myoverview, "uid", mail_uid(imap_le_struct->imap_stream, i));
    3463          19 :                                 add_property_long(myoverview, "msgno", i);
    3464          19 :                                 add_property_long(myoverview, "recent", elt->recent);
    3465          19 :                                 add_property_long(myoverview, "flagged", elt->flagged);
    3466          19 :                                 add_property_long(myoverview, "answered", elt->answered);
    3467          19 :                                 add_property_long(myoverview, "deleted", elt->deleted);
    3468          19 :                                 add_property_long(myoverview, "seen", elt->seen);
    3469          19 :                                 add_property_long(myoverview, "draft", elt->draft);
    3470          19 :                                 add_property_long(myoverview, "udate", mail_longdate(elt));
    3471          19 :                                 add_next_index_object(return_value, myoverview TSRMLS_CC);
    3472             :                         }
    3473             :                 }
    3474             :         }
    3475             : }
    3476             : /* }}} */
    3477             : 
    3478             : /* {{{ proto string imap_mail_compose(array envelope, array body)
    3479             :    Create a MIME message based on given envelope and body sections */
    3480          22 : PHP_FUNCTION(imap_mail_compose)
    3481             : {
    3482             :         zval *envelope, *body;
    3483             :         char *key;
    3484             :         zval **data, **pvalue, **disp_data, **env_data;
    3485             :         ulong ind;
    3486          22 :         char *cookie = NIL;
    3487             :         ENVELOPE *env;
    3488          22 :         BODY *bod=NULL, *topbod=NULL;
    3489          22 :         PART *mypart=NULL, *part;
    3490          22 :         PARAMETER *param, *disp_param = NULL, *custom_headers_param = NULL, *tmp_param = NULL;
    3491          22 :         char *tmp=NULL, *mystring=NULL, *t=NULL, *tempstring=NULL, *str_copy = NULL;
    3492          22 :         int toppart = 0;
    3493             : 
    3494          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "aa", &envelope, &body) == FAILURE) {
    3495           0 :                 return;
    3496             :         }
    3497             : 
    3498             : #define PHP_RFC822_PARSE_ADRLIST(target, value) \
    3499             :         str_copy = estrndup(Z_STRVAL_PP(value), Z_STRLEN_PP(value)); \
    3500             :         rfc822_parse_adrlist(target, str_copy, "NO HOST"); \
    3501             :         efree(str_copy);
    3502             : 
    3503          22 :         env = mail_newenvelope();
    3504          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "remail", sizeof("remail"), (void **) &pvalue)== SUCCESS) {
    3505           0 :                 convert_to_string_ex(pvalue);
    3506           0 :                 env->remail = cpystr(Z_STRVAL_PP(pvalue));
    3507             :         }
    3508          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "return_path", sizeof("return_path"), (void **) &pvalue)== SUCCESS) {
    3509           2 :                 convert_to_string_ex(pvalue);
    3510           2 :                 PHP_RFC822_PARSE_ADRLIST(&env->return_path, pvalue);
    3511             :         }
    3512          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "date", sizeof("date"), (void **) &pvalue)== SUCCESS) {
    3513           1 :                 convert_to_string_ex(pvalue);
    3514           1 :                 env->date = cpystr(Z_STRVAL_PP(pvalue));
    3515             :         }
    3516          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "from", sizeof("from"), (void **) &pvalue)== SUCCESS) {
    3517          21 :                 convert_to_string_ex(pvalue);
    3518          21 :                 PHP_RFC822_PARSE_ADRLIST(&env->from, pvalue);
    3519             :         }
    3520          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "reply_to", sizeof("reply_to"), (void **) &pvalue)== SUCCESS) {
    3521           1 :                 convert_to_string_ex(pvalue);
    3522           1 :                 PHP_RFC822_PARSE_ADRLIST(&env->reply_to, pvalue);
    3523             :         }
    3524          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "in_reply_to", sizeof("in_reply_to"), (void **) &pvalue)== SUCCESS) {
    3525           0 :                 convert_to_string_ex(pvalue);
    3526           0 :                 env->in_reply_to = cpystr(Z_STRVAL_PP(pvalue));
    3527             :         }
    3528          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "subject", sizeof("subject"), (void **) &pvalue)== SUCCESS) {
    3529          16 :                 convert_to_string_ex(pvalue);
    3530          16 :                 env->subject = cpystr(Z_STRVAL_PP(pvalue));
    3531             :         }
    3532          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "to", sizeof("to"), (void **) &pvalue)== SUCCESS) {
    3533          20 :                 convert_to_string_ex(pvalue);
    3534          20 :                 PHP_RFC822_PARSE_ADRLIST(&env->to, pvalue);
    3535             :         }
    3536          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "cc", sizeof("cc"), (void **) &pvalue)== SUCCESS) {
    3537           3 :                 convert_to_string_ex(pvalue);
    3538           3 :                 PHP_RFC822_PARSE_ADRLIST(&env->cc, pvalue);
    3539             :         }
    3540          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "bcc", sizeof("bcc"), (void **) &pvalue)== SUCCESS) {
    3541           1 :                 convert_to_string_ex(pvalue);
    3542           1 :                 PHP_RFC822_PARSE_ADRLIST(&env->bcc, pvalue);
    3543             :         }
    3544          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "message_id", sizeof("message_id"), (void **) &pvalue)== SUCCESS) {
    3545           0 :                 convert_to_string_ex(pvalue);
    3546           0 :                 env->message_id=cpystr(Z_STRVAL_PP(pvalue));
    3547             :         }
    3548             : 
    3549          22 :         if (zend_hash_find(Z_ARRVAL_P(envelope), "custom_headers", sizeof("custom_headers"), (void **) &pvalue)== SUCCESS) {
    3550           0 :                 if (Z_TYPE_PP(pvalue) == IS_ARRAY) {
    3551           0 :                         custom_headers_param = tmp_param = NULL;
    3552           0 :                         while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &env_data) == SUCCESS) {
    3553           0 :                                 custom_headers_param = mail_newbody_parameter();
    3554           0 :                                 convert_to_string_ex(env_data);
    3555           0 :                                 custom_headers_param->value = (char *) fs_get(Z_STRLEN_PP(env_data) + 1);
    3556           0 :                                 custom_headers_param->attribute = NULL;
    3557           0 :                                 memcpy(custom_headers_param->value, Z_STRVAL_PP(env_data), Z_STRLEN_PP(env_data) + 1);
    3558           0 :                                 zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
    3559           0 :                                 custom_headers_param->next = tmp_param;
    3560           0 :                                 tmp_param = custom_headers_param;
    3561             :                         }
    3562             :                 }
    3563             :         }
    3564             : 
    3565          22 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(body));
    3566          22 :         if (zend_hash_get_current_data(Z_ARRVAL_P(body), (void **) &data) != SUCCESS || Z_TYPE_PP(data) != IS_ARRAY) {
    3567           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "body parameter must be a non-empty array");
    3568           0 :                 RETURN_FALSE;
    3569             :         }
    3570             : 
    3571          22 :         if (Z_TYPE_PP(data) == IS_ARRAY) {
    3572          22 :                 bod = mail_newbody();
    3573          22 :                 topbod = bod;
    3574             : 
    3575          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &pvalue)== SUCCESS) {
    3576          21 :                         convert_to_long_ex(pvalue);
    3577          21 :                         bod->type = (short) Z_LVAL_PP(pvalue);
    3578             :                 }
    3579          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "encoding", sizeof("encoding"), (void **) &pvalue)== SUCCESS) {
    3580           1 :                         convert_to_long_ex(pvalue);
    3581           1 :                         bod->encoding = (short) Z_LVAL_PP(pvalue);
    3582             :                 }
    3583          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "charset", sizeof("charset"), (void **) &pvalue)== SUCCESS) {
    3584           1 :                         convert_to_string_ex(pvalue);
    3585           1 :                         tmp_param = mail_newbody_parameter();
    3586           1 :                         tmp_param->value = cpystr(Z_STRVAL_PP(pvalue));
    3587           1 :                         tmp_param->attribute = cpystr("CHARSET");
    3588           1 :                         tmp_param->next = bod->parameter;
    3589           1 :                         bod->parameter = tmp_param;
    3590             :                 }
    3591          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "type.parameters", sizeof("type.parameters"), (void **) &pvalue)== SUCCESS) {
    3592           0 :                         if(Z_TYPE_PP(pvalue) == IS_ARRAY) {
    3593           0 :                                 disp_param = tmp_param = NULL;
    3594           0 :                                 while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
    3595           0 :                                         disp_param = mail_newbody_parameter();
    3596           0 :                                         zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
    3597           0 :                                         disp_param->attribute = cpystr(key);
    3598           0 :                                         convert_to_string_ex(disp_data);
    3599           0 :                                         disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
    3600           0 :                                         memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
    3601           0 :                                         zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
    3602           0 :                                         disp_param->next = tmp_param;
    3603           0 :                                         tmp_param = disp_param;
    3604             :                                 }
    3605           0 :                         bod->parameter = disp_param;
    3606             :                         }
    3607             :                 }
    3608          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "subtype", sizeof("subtype"), (void **) &pvalue)== SUCCESS) {
    3609          21 :                         convert_to_string_ex(pvalue);
    3610          21 :                         bod->subtype = cpystr(Z_STRVAL_PP(pvalue));
    3611             :                 }
    3612          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "id", sizeof("id"), (void **) &pvalue)== SUCCESS) {
    3613           0 :                         convert_to_string_ex(pvalue);
    3614           0 :                         bod->id = cpystr(Z_STRVAL_PP(pvalue));
    3615             :                 }
    3616          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "description", sizeof("description"), (void **) &pvalue)== SUCCESS) {
    3617           0 :                         convert_to_string_ex(pvalue);
    3618           0 :                         bod->description = cpystr(Z_STRVAL_PP(pvalue));
    3619             :                 }
    3620          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "disposition.type", sizeof("disposition.type"), (void **) &pvalue)== SUCCESS) {
    3621           0 :                         convert_to_string_ex(pvalue);
    3622           0 :                         bod->disposition.type = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
    3623           0 :                         memcpy(bod->disposition.type, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue)+1);
    3624             :                 }
    3625          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "disposition", sizeof("disposition"), (void **) &pvalue)== SUCCESS) {
    3626           0 :                         if (Z_TYPE_PP(pvalue) == IS_ARRAY) {
    3627           0 :                                 disp_param = tmp_param = NULL;
    3628           0 :                                 while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
    3629           0 :                                         disp_param = mail_newbody_parameter();
    3630           0 :                                         zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
    3631           0 :                                         disp_param->attribute = cpystr(key);
    3632           0 :                                         convert_to_string_ex(disp_data);
    3633           0 :                                         disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
    3634           0 :                                         memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
    3635           0 :                                         zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
    3636           0 :                                         disp_param->next = tmp_param;
    3637           0 :                                         tmp_param = disp_param;
    3638             :                                 }
    3639           0 :                                 bod->disposition.parameter = disp_param;
    3640             :                         }
    3641             :                 }
    3642          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "contents.data", sizeof("contents.data"), (void **) &pvalue)== SUCCESS) {
    3643           1 :                         convert_to_string_ex(pvalue);
    3644           1 :                         bod->contents.text.data = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
    3645           1 :                         memcpy(bod->contents.text.data, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue)+1);
    3646           1 :                         bod->contents.text.size = Z_STRLEN_PP(pvalue);
    3647             :                 } else {
    3648          21 :                         bod->contents.text.data = (char *) fs_get(1);
    3649          21 :                         memcpy(bod->contents.text.data, "", 1);
    3650          21 :                         bod->contents.text.size = 0;
    3651             :                 }
    3652          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "lines", sizeof("lines"), (void **) &pvalue)== SUCCESS) {
    3653           0 :                         convert_to_long_ex(pvalue);
    3654           0 :                         bod->size.lines = Z_LVAL_PP(pvalue);
    3655             :                 }
    3656          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "bytes", sizeof("bytes"), (void **) &pvalue)== SUCCESS) {
    3657           0 :                         convert_to_long_ex(pvalue);
    3658           0 :                         bod->size.bytes = Z_LVAL_PP(pvalue);
    3659             :                 }
    3660          22 :                 if (zend_hash_find(Z_ARRVAL_PP(data), "md5", sizeof("md5"), (void **) &pvalue)== SUCCESS) {
    3661           0 :                         convert_to_string_ex(pvalue);
    3662           0 :                         bod->md5 = cpystr(Z_STRVAL_PP(pvalue));
    3663             :                 }
    3664             :         }
    3665             : 
    3666          22 :         zend_hash_move_forward(Z_ARRVAL_P(body));
    3667             : 
    3668         100 :         while (zend_hash_get_current_data(Z_ARRVAL_P(body), (void **) &data) == SUCCESS) {
    3669          56 :                 if (Z_TYPE_PP(data) == IS_ARRAY) {
    3670          56 :                         short type = -1;
    3671          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &pvalue)== SUCCESS) {
    3672          56 :                                 convert_to_long_ex(pvalue);
    3673          56 :                                 type = (short) Z_LVAL_PP(pvalue);
    3674             :                         }
    3675             : 
    3676          56 :                         if (!toppart) {
    3677          20 :                                 bod->nested.part = mail_newbody_part();
    3678          20 :                                 mypart = bod->nested.part;
    3679          20 :                                 toppart = 1;
    3680             :                         } else {
    3681          36 :                                 mypart->next = mail_newbody_part();
    3682          36 :                                 mypart = mypart->next;
    3683             :                         }
    3684             : 
    3685          56 :                         bod = &mypart->body;
    3686             : 
    3687          56 :                         if (type != TYPEMULTIPART) {
    3688          55 :                                 bod->type = type;
    3689             :                         }
    3690             : 
    3691          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "encoding", sizeof("encoding"), (void **) &pvalue)== SUCCESS) {
    3692           5 :                                 convert_to_long_ex(pvalue);
    3693           5 :                                 bod->encoding = (short) Z_LVAL_PP(pvalue);
    3694             :                         }
    3695          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "charset", sizeof("charset"), (void **) &pvalue)== SUCCESS) {
    3696           3 :                                 convert_to_string_ex(pvalue);
    3697           3 :                                 tmp_param = mail_newbody_parameter();
    3698           3 :                                 tmp_param->value = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
    3699           3 :                                 memcpy(tmp_param->value, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue) + 1);
    3700           3 :                                 tmp_param->attribute = cpystr("CHARSET");
    3701           3 :                                 tmp_param->next = bod->parameter;
    3702           3 :                                 bod->parameter = tmp_param;
    3703             :                         }
    3704          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "type.parameters", sizeof("type.parameters"), (void **) &pvalue)== SUCCESS) {
    3705           1 :                                 if(Z_TYPE_PP(pvalue) == IS_ARRAY) {
    3706           1 :                                         disp_param = tmp_param = NULL;
    3707           3 :                                         while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
    3708           1 :                                                 disp_param = mail_newbody_parameter();
    3709           1 :                                                 zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
    3710           1 :                                                 disp_param->attribute = cpystr(key);
    3711           1 :                                                 convert_to_string_ex(disp_data);
    3712           1 :                                                 disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
    3713           1 :                                                 memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
    3714           1 :                                                 zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
    3715           1 :                                                 disp_param->next = tmp_param;
    3716           1 :                                                 tmp_param = disp_param;
    3717             :                                         }
    3718           1 :                                         bod->parameter = disp_param;
    3719             :                                 }
    3720             :                         }
    3721          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "subtype", sizeof("subtype"), (void **) &pvalue)== SUCCESS) {
    3722          56 :                                 convert_to_string_ex(pvalue);
    3723          56 :                                 bod->subtype = cpystr(Z_STRVAL_PP(pvalue));
    3724             :                         }
    3725          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "id", sizeof("id"), (void **) &pvalue)== SUCCESS) {
    3726           0 :                                 convert_to_string_ex(pvalue);
    3727           0 :                                 bod->id = cpystr(Z_STRVAL_PP(pvalue));
    3728             :                         }
    3729          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "description", sizeof("description"), (void **) &pvalue)== SUCCESS) {
    3730          55 :                                 convert_to_string_ex(pvalue);
    3731          55 :                                 bod->description = cpystr(Z_STRVAL_PP(pvalue));
    3732             :                         }
    3733          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "disposition.type", sizeof("disposition.type"), (void **) &pvalue)== SUCCESS) {
    3734           1 :                                 convert_to_string_ex(pvalue);
    3735           1 :                                 bod->disposition.type = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
    3736           1 :                                 memcpy(bod->disposition.type, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue)+1);
    3737             :                         }
    3738          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "disposition", sizeof("disposition"), (void **) &pvalue)== SUCCESS) {
    3739           3 :                                 if (Z_TYPE_PP(pvalue) == IS_ARRAY) {
    3740           1 :                                         disp_param = tmp_param = NULL;
    3741           3 :                                         while (zend_hash_get_current_data(Z_ARRVAL_PP(pvalue), (void **) &disp_data) == SUCCESS) {
    3742           1 :                                                 disp_param = mail_newbody_parameter();
    3743           1 :                                                 zend_hash_get_current_key(Z_ARRVAL_PP(pvalue), &key, &ind, 0);
    3744           1 :                                                 disp_param->attribute = cpystr(key);
    3745           1 :                                                 convert_to_string_ex(disp_data);
    3746           1 :                                                 disp_param->value = (char *) fs_get(Z_STRLEN_PP(disp_data) + 1);
    3747           1 :                                                 memcpy(disp_param->value, Z_STRVAL_PP(disp_data), Z_STRLEN_PP(disp_data) + 1);
    3748           1 :                                                 zend_hash_move_forward(Z_ARRVAL_PP(pvalue));
    3749           1 :                                                 disp_param->next = tmp_param;
    3750           1 :                                                 tmp_param = disp_param;
    3751             :                                         }
    3752           1 :                                         bod->disposition.parameter = disp_param;
    3753             :                                 }
    3754             :                         }
    3755          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "contents.data", sizeof("contents.data"), (void **) &pvalue)== SUCCESS) {
    3756          55 :                                 convert_to_string_ex(pvalue);
    3757          55 :                                 bod->contents.text.data = (char *) fs_get(Z_STRLEN_PP(pvalue) + 1);
    3758          55 :                                 memcpy(bod->contents.text.data, Z_STRVAL_PP(pvalue), Z_STRLEN_PP(pvalue) + 1);
    3759          55 :                                 bod->contents.text.size = Z_STRLEN_PP(pvalue);
    3760             :                         } else {
    3761           1 :                                 bod->contents.text.data = (char *) fs_get(1);
    3762           1 :                                 memcpy(bod->contents.text.data, "", 1);
    3763           1 :                                 bod->contents.text.size = 0;
    3764             :                         }
    3765          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "lines", sizeof("lines"), (void **) &pvalue)== SUCCESS) {
    3766           0 :                                 convert_to_long_ex(pvalue);
    3767           0 :                                 bod->size.lines = Z_LVAL_PP(pvalue);
    3768             :                         }
    3769          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "bytes", sizeof("bytes"), (void **) &pvalue)== SUCCESS) {
    3770           0 :                                 convert_to_long_ex(pvalue);
    3771           0 :                                 bod->size.bytes = Z_LVAL_PP(pvalue);
    3772             :                         }
    3773          56 :                         if (zend_hash_find(Z_ARRVAL_PP(data), "md5", sizeof("md5"), (void **) &pvalue)== SUCCESS) {
    3774           0 :                                 convert_to_string_ex(pvalue);
    3775           0 :                                 bod->md5 = cpystr(Z_STRVAL_PP(pvalue));
    3776             :                         }
    3777             :                 }
    3778          56 :                 zend_hash_move_forward(Z_ARRVAL_P(body));
    3779             :         }
    3780             : 
    3781          22 :         if (bod && bod->type == TYPEMULTIPART && (!bod->nested.part || !bod->nested.part->next)) {
    3782           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot generate multipart e-mail without components.");
    3783           0 :                 RETVAL_FALSE;
    3784           0 :                 goto done;
    3785             :         }
    3786             : 
    3787          22 :         rfc822_encode_body_7bit(env, topbod);
    3788             : 
    3789          22 :         tmp = emalloc(SENDBUFLEN + 1);
    3790             : 
    3791          22 :         rfc822_header(tmp, env, topbod);
    3792             : 
    3793             :         /* add custom envelope headers */
    3794          22 :         if (custom_headers_param) {
    3795           0 :                 int l = strlen(tmp) - 2, l2;
    3796           0 :                 PARAMETER *tp = custom_headers_param;
    3797             : 
    3798             :                 /* remove last CRLF from tmp */
    3799           0 :                 tmp[l] = '\0';
    3800           0 :                 tempstring = emalloc(l);
    3801           0 :                 memcpy(tempstring, tmp, l);
    3802             : 
    3803             :                 do {
    3804           0 :                         l2 = strlen(custom_headers_param->value);
    3805           0 :                         tempstring = erealloc(tempstring, l + l2 + CRLF_LEN + 1);
    3806           0 :                         memcpy(tempstring + l, custom_headers_param->value, l2);
    3807           0 :                         memcpy(tempstring + l + l2, CRLF, CRLF_LEN);
    3808           0 :                         l += l2 + CRLF_LEN;
    3809           0 :                 } while ((custom_headers_param = custom_headers_param->next));
    3810             : 
    3811           0 :                 mail_free_body_parameter(&tp);
    3812             : 
    3813           0 :                 mystring = emalloc(l + CRLF_LEN + 1);
    3814           0 :                 memcpy(mystring, tempstring, l);
    3815           0 :                 memcpy(mystring + l , CRLF, CRLF_LEN);
    3816           0 :                 mystring[l + CRLF_LEN] = '\0';
    3817             : 
    3818           0 :                 efree(tempstring);
    3819             :         } else {
    3820          22 :                 mystring = estrdup(tmp);
    3821             :         }
    3822             : 
    3823          22 :         bod = topbod;
    3824             : 
    3825          42 :         if (bod && bod->type == TYPEMULTIPART) {
    3826             : 
    3827             :                 /* first body part */
    3828          20 :                         part = bod->nested.part;
    3829             : 
    3830             :                 /* find cookie */
    3831          40 :                         for (param = bod->parameter; param && !cookie; param = param->next) {
    3832          20 :                                 if (!strcmp (param->attribute, "BOUNDARY")) {
    3833          20 :                                         cookie = param->value;
    3834             :                                 }
    3835             :                         }
    3836             : 
    3837             :                 /* yucky default */
    3838          20 :                         if (!cookie) {
    3839           0 :                                 cookie = "-";
    3840          20 :                         } else if (strlen(cookie) > (SENDBUFLEN - 2 - 2 - 2)) { /* validate cookie length -- + CRLF * 2 */
    3841           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The boundary should be no longer than 4kb");
    3842           0 :                                 RETVAL_FALSE;
    3843           0 :                                 goto done;
    3844             :                         }
    3845             : 
    3846             :                 /* for each part */
    3847             :                         do {
    3848          56 :                                 t = tmp;
    3849             : 
    3850             :                         /* append mini-header */
    3851          56 :                                 *t = '\0';
    3852          56 :                                 rfc822_write_body_header(&t, &part->body);
    3853             : 
    3854             :                         /* output cookie, mini-header, and contents */
    3855          56 :                                 spprintf(&tempstring, 0, "%s--%s%s%s%s", mystring, cookie, CRLF, tmp, CRLF);
    3856          56 :                                 efree(mystring);
    3857          56 :                                 mystring=tempstring;
    3858             : 
    3859          56 :                                 bod=&part->body;
    3860             : 
    3861          56 :                                 spprintf(&tempstring, 0, "%s%s%s", mystring, bod->contents.text.data, CRLF);
    3862          56 :                                 efree(mystring);
    3863          56 :                                 mystring=tempstring;
    3864          56 :                         } while ((part = part->next)); /* until done */
    3865             : 
    3866             :                         /* output trailing cookie */
    3867          20 :                         spprintf(&tempstring, 0, "%s--%s--%s", mystring, cookie, CRLF);
    3868          20 :                         efree(mystring);
    3869          20 :                         mystring=tempstring;
    3870           2 :         } else if (bod) {
    3871           2 :                 spprintf(&tempstring, 0, "%s%s%s", mystring, bod->contents.text.data, CRLF);
    3872           2 :                 efree(mystring);
    3873           2 :                 mystring=tempstring;
    3874             :         } else {
    3875           0 :                 efree(mystring);
    3876           0 :                 RETVAL_FALSE;
    3877           0 :                 goto done;
    3878             :         }
    3879             : 
    3880          22 :         RETVAL_STRING(tempstring, 0);
    3881             : done:
    3882          22 :         if (tmp) {
    3883          22 :                 efree(tmp);
    3884             :         }
    3885          22 :         mail_free_body(&topbod);
    3886          22 :         mail_free_envelope(&env);
    3887             : }
    3888             : /* }}} */
    3889             : 
    3890             : /* {{{ _php_imap_mail
    3891             :  */
    3892           0 : int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *cc, char *bcc, char* rpath TSRMLS_DC)
    3893             : {
    3894             : #ifdef PHP_WIN32
    3895             :         int tsm_err;
    3896             : #else
    3897             :         FILE *sendmail;
    3898             :         int ret;
    3899             : #endif
    3900             : 
    3901             : #ifdef PHP_WIN32
    3902             :         char *tempMailTo;
    3903             :         char *tsm_errmsg = NULL;
    3904             :         ADDRESS *addr;
    3905             :         char *bufferTo = NULL, *bufferCc = NULL, *bufferBcc = NULL, *bufferHeader = NULL;
    3906             :         int offset, bufferLen = 0;
    3907             :         size_t bt_len;
    3908             : 
    3909             :         if (headers) {
    3910             :                 bufferLen += strlen(headers);
    3911             :         }
    3912             :         if (to) {
    3913             :                 bufferLen += strlen(to) + 6;
    3914             :         }
    3915             :         if (cc) {
    3916             :                 bufferLen += strlen(cc) + 6;
    3917             :         }
    3918             : 
    3919             : #define PHP_IMAP_CLEAN  if (bufferTo) efree(bufferTo); if (bufferCc) efree(bufferCc); if (bufferBcc) efree(bufferBcc); if (bufferHeader) efree(bufferHeader);
    3920             : #define PHP_IMAP_BAD_DEST PHP_IMAP_CLEAN; efree(tempMailTo); return (BAD_MSG_DESTINATION);
    3921             : 
    3922             :         bufferHeader = (char *)emalloc(bufferLen + 1);
    3923             :         memset(bufferHeader, 0, bufferLen);
    3924             :         if (to && *to) {
    3925             :                 strlcat(bufferHeader, "To: ", bufferLen + 1);
    3926             :                 strlcat(bufferHeader, to, bufferLen + 1);
    3927             :                 strlcat(bufferHeader, "\r\n", bufferLen + 1);
    3928             :                 tempMailTo = estrdup(to);
    3929             :                 bt_len = strlen(to);
    3930             :                 bufferTo = (char *)safe_emalloc(bt_len, 1, 1);
    3931             :                 bt_len++;
    3932             :                 offset = 0;
    3933             :                 addr = NULL;
    3934             :                 rfc822_parse_adrlist(&addr, tempMailTo, NULL);
    3935             :                 while (addr) {
    3936             :                         if (addr->host == NULL || strcmp(addr->host, ERRHOST) == 0) {
    3937             :                                 PHP_IMAP_BAD_DEST;
    3938             :                         } else {
    3939             :                                 bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->mailbox));
    3940             :                                 bt_len += strlen(addr->mailbox);
    3941             :                                 bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->host));
    3942             :                                 bt_len += strlen(addr->host);
    3943             :                                 offset += slprintf(bufferTo + offset, bt_len - offset, "%s@%s,", addr->mailbox, addr->host);
    3944             :                         }
    3945             :                         addr = addr->next;
    3946             :                 }
    3947             :                 efree(tempMailTo);
    3948             :                 if (offset>0) {
    3949             :                         bufferTo[offset-1] = 0;
    3950             :                 }
    3951             :         }
    3952             : 
    3953             :         if (cc && *cc) {
    3954             :                 strlcat(bufferHeader, "Cc: ", bufferLen + 1);
    3955             :                 strlcat(bufferHeader, cc, bufferLen + 1);
    3956             :                 strlcat(bufferHeader, "\r\n", bufferLen + 1);
    3957             :                 tempMailTo = estrdup(cc);
    3958             :                 bt_len = strlen(cc);
    3959             :                 bufferCc = (char *)safe_emalloc(bt_len, 1, 1);
    3960             :                 bt_len++;
    3961             :                 offset = 0;
    3962             :                 addr = NULL;
    3963             :                 rfc822_parse_adrlist(&addr, tempMailTo, NULL);
    3964             :                 while (addr) {
    3965             :                         if (addr->host == NULL || strcmp(addr->host, ERRHOST) == 0) {
    3966             :                                 PHP_IMAP_BAD_DEST;
    3967             :                         } else {
    3968             :                                 bufferCc = safe_erealloc(bufferCc, bt_len, 1, strlen(addr->mailbox));
    3969             :                                 bt_len += strlen(addr->mailbox);
    3970             :                                 bufferCc = safe_erealloc(bufferCc, bt_len, 1, strlen(addr->host));
    3971             :                                 bt_len += strlen(addr->host);
    3972             :                                 offset += slprintf(bufferCc + offset, bt_len - offset, "%s@%s,", addr->mailbox, addr->host);
    3973             :                         }
    3974             :                         addr = addr->next;
    3975             :                 }
    3976             :                 efree(tempMailTo);
    3977             :                 if (offset>0) {
    3978             :                         bufferCc[offset-1] = 0;
    3979             :                 }
    3980             :         }
    3981             : 
    3982             :         if (bcc && *bcc) {
    3983             :                 tempMailTo = estrdup(bcc);
    3984             :                 bt_len = strlen(bcc);
    3985             :                 bufferBcc = (char *)safe_emalloc(bt_len, 1, 1);
    3986             :                 bt_len++;
    3987             :                 offset = 0;
    3988             :                 addr = NULL;
    3989             :                 rfc822_parse_adrlist(&addr, tempMailTo, NULL);
    3990             :                 while (addr) {
    3991             :                         if (addr->host == NULL || strcmp(addr->host, ERRHOST) == 0) {
    3992             :                                 PHP_IMAP_BAD_DEST;
    3993             :                         } else {
    3994             :                                 bufferBcc = safe_erealloc(bufferBcc, bt_len, 1, strlen(addr->mailbox));
    3995             :                                 bt_len += strlen(addr->mailbox);
    3996             :                                 bufferBcc = safe_erealloc(bufferBcc, bt_len, 1, strlen(addr->host));
    3997             :                                 bt_len += strlen(addr->host);
    3998             :                                 offset += slprintf(bufferBcc + offset, bt_len - offset, "%s@%s,", addr->mailbox, addr->host);
    3999             :                         }
    4000             :                         addr = addr->next;
    4001             :                 }
    4002             :                 efree(tempMailTo);
    4003             :                 if (offset>0) {
    4004             :                         bufferBcc[offset-1] = 0;
    4005             :                 }
    4006             :         }
    4007             : 
    4008             :         if (headers && *headers) {
    4009             :                 strlcat(bufferHeader, headers, bufferLen + 1);
    4010             :         }
    4011             : 
    4012             :         if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, bufferHeader, subject, bufferTo, message, bufferCc, bufferBcc, rpath TSRMLS_CC) != SUCCESS) {
    4013             :                 if (tsm_errmsg) {
    4014             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", tsm_errmsg);
    4015             :                         efree(tsm_errmsg);
    4016             :                 } else {
    4017             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", GetSMErrorText(tsm_err));
    4018             :                 }
    4019             :                 PHP_IMAP_CLEAN;
    4020             :                 return 0;
    4021             :         }
    4022             :         PHP_IMAP_CLEAN;
    4023             : #else
    4024           0 :         if (!INI_STR("sendmail_path")) {
    4025           0 :                 return 0;
    4026             :         }
    4027           0 :         sendmail = popen(INI_STR("sendmail_path"), "w");
    4028           0 :         if (sendmail) {
    4029           0 :                 if (rpath && rpath[0]) fprintf(sendmail, "From: %s\n", rpath);
    4030           0 :                 fprintf(sendmail, "To: %s\n", to);
    4031           0 :                 if (cc && cc[0]) fprintf(sendmail, "Cc: %s\n", cc);
    4032           0 :                 if (bcc && bcc[0]) fprintf(sendmail, "Bcc: %s\n", bcc);
    4033           0 :                 fprintf(sendmail, "Subject: %s\n", subject);
    4034           0 :                 if (headers != NULL) {
    4035           0 :                         fprintf(sendmail, "%s\n", headers);
    4036             :                 }
    4037           0 :                 fprintf(sendmail, "\n%s\n", message);
    4038           0 :                 ret = pclose(sendmail);
    4039           0 :                 if (ret == -1) {
    4040           0 :                         return 0;
    4041             :                 } else {
    4042           0 :                         return 1;
    4043             :                 }
    4044             :         } else {
    4045           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not execute mail delivery program");
    4046           0 :                 return 0;
    4047             :         }
    4048             : #endif
    4049             :         return 1;
    4050             : }
    4051             : /* }}} */
    4052             : 
    4053             : /* {{{ proto bool imap_mail(string to, string subject, string message [, string additional_headers [, string cc [, string bcc [, string rpath]]]])
    4054             :    Send an email message */
    4055           0 : PHP_FUNCTION(imap_mail)
    4056             : {
    4057           0 :         char *to=NULL, *message=NULL, *headers=NULL, *subject=NULL, *cc=NULL, *bcc=NULL, *rpath=NULL;
    4058           0 :         int to_len, message_len, headers_len, subject_len, cc_len, bcc_len, rpath_len, argc = ZEND_NUM_ARGS();
    4059             : 
    4060           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "sss|ssss", &to, &to_len, &subject, &subject_len, &message, &message_len,
    4061             :                 &headers, &headers_len, &cc, &cc_len, &bcc, &bcc_len, &rpath, &rpath_len) == FAILURE) {
    4062           0 :                 return;
    4063             :         }
    4064             : 
    4065             :         /* To: */
    4066           0 :         if (!to_len) {
    4067           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No to field in mail command");
    4068           0 :                 RETURN_FALSE;
    4069             :         }
    4070             : 
    4071             :         /* Subject: */
    4072           0 :         if (!subject_len) {
    4073           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No subject field in mail command");
    4074           0 :                 RETURN_FALSE;
    4075             :         }
    4076             : 
    4077             :         /* message body */
    4078           0 :         if (!message_len) {
    4079             :                 /* this is not really an error, so it is allowed. */
    4080           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "No message string in mail command");
    4081           0 :                 message = NULL;
    4082             :         }
    4083             : 
    4084           0 :         if (_php_imap_mail(to, subject, message, headers, cc, bcc, rpath TSRMLS_CC)) {
    4085           0 :                 RETURN_TRUE;
    4086             :         } else {
    4087           0 :                 RETURN_FALSE;
    4088             :         }
    4089             : }
    4090             : /* }}} */
    4091             : 
    4092             : /* {{{ proto array imap_search(resource stream_id, string criteria [, int options [, string charset]])
    4093             :    Return a list of messages matching the given criteria */
    4094           8 : PHP_FUNCTION(imap_search)
    4095             : {
    4096             :         zval *streamind;
    4097           8 :         char *criteria, *charset = NULL;
    4098           8 :         int criteria_len, charset_len = 0;
    4099           8 :         long flags = SE_FREE;
    4100             :         pils *imap_le_struct;
    4101             :         char *search_criteria;
    4102             :         MESSAGELIST *cur;
    4103           8 :         int argc = ZEND_NUM_ARGS();
    4104           8 :         SEARCHPGM *pgm = NIL;
    4105             : 
    4106           8 :         if (zend_parse_parameters(argc TSRMLS_CC, "rs|ls", &streamind, &criteria, &criteria_len, &flags, &charset, &charset_len) == FAILURE) {
    4107           0 :                 return;
    4108             :         }
    4109             : 
    4110           8 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    4111             : 
    4112           8 :         search_criteria = estrndup(criteria, criteria_len);
    4113             : 
    4114           8 :         IMAPG(imap_messages) = IMAPG(imap_messages_tail) = NIL;
    4115           8 :         pgm = mail_criteria(search_criteria);
    4116             : 
    4117           8 :         mail_search_full(imap_le_struct->imap_stream, (argc == 4 ? charset : NIL), pgm, flags);
    4118             : 
    4119           8 :         if (pgm && !(flags & SE_FREE)) {
    4120           0 :                 mail_free_searchpgm(&pgm);
    4121             :         }
    4122             : 
    4123           8 :         if (IMAPG(imap_messages) == NIL) {
    4124           1 :                 efree(search_criteria);
    4125           1 :                 RETURN_FALSE;
    4126             :         }
    4127             : 
    4128           7 :         array_init(return_value);
    4129             : 
    4130           7 :         cur = IMAPG(imap_messages);
    4131          35 :         while (cur != NIL) {
    4132          21 :                 add_next_index_long(return_value, cur->msgid);
    4133          21 :                 cur = cur->next;
    4134             :         }
    4135           7 :         mail_free_messagelist(&IMAPG(imap_messages), &IMAPG(imap_messages_tail));
    4136           7 :         efree(search_criteria);
    4137             : }
    4138             : /* }}} */
    4139             : 
    4140             : /* {{{ proto array imap_alerts(void)
    4141             :    Returns an array of all IMAP alerts that have been generated since the last page load or since the last imap_alerts() call, whichever came last. The alert stack is cleared after imap_alerts() is called. */
    4142             : /* Author: CJH */
    4143           1 : PHP_FUNCTION(imap_alerts)
    4144             : {
    4145           1 :         STRINGLIST *cur=NIL;
    4146             : 
    4147           1 :         if (zend_parse_parameters_none() == FAILURE) {
    4148           1 :                 return;
    4149             :         }
    4150             : 
    4151           0 :         if (IMAPG(imap_alertstack) == NIL) {
    4152           0 :                 RETURN_FALSE;
    4153             :         }
    4154             : 
    4155           0 :         array_init(return_value);
    4156             : 
    4157           0 :         cur = IMAPG(imap_alertstack);
    4158           0 :         while (cur != NIL) {
    4159           0 :                 add_next_index_string(return_value, cur->LTEXT, 1);
    4160           0 :                 cur = cur->next;
    4161             :         }
    4162           0 :         mail_free_stringlist(&IMAPG(imap_alertstack));
    4163           0 :         IMAPG(imap_alertstack) = NIL;
    4164             : }
    4165             : /* }}} */
    4166             : 
    4167             : /* {{{ proto array imap_errors(void)
    4168             :    Returns an array of all IMAP errors generated since the last page load, or since the last imap_errors() call, whichever came last. The error stack is cleared after imap_errors() is called. */
    4169             : /* Author: CJH */
    4170           6 : PHP_FUNCTION(imap_errors)
    4171             : {
    4172           6 :         ERRORLIST *cur=NIL;
    4173             : 
    4174           6 :         if (zend_parse_parameters_none() == FAILURE) {
    4175           0 :                 return;
    4176             :         }
    4177             : 
    4178           6 :         if (IMAPG(imap_errorstack) == NIL) {
    4179           2 :                 RETURN_FALSE;
    4180             :         }
    4181             : 
    4182           4 :         array_init(return_value);
    4183             : 
    4184           4 :         cur = IMAPG(imap_errorstack);
    4185          28 :         while (cur != NIL) {
    4186          20 :                 add_next_index_string(return_value, cur->LTEXT, 1);
    4187          20 :                 cur = cur->next;
    4188             :         }
    4189           4 :         mail_free_errorlist(&IMAPG(imap_errorstack));
    4190           4 :         IMAPG(imap_errorstack) = NIL;
    4191             : }
    4192             : /* }}} */
    4193             : 
    4194             : /* {{{ proto string imap_last_error(void)
    4195             :    Returns the last error that was generated by an IMAP function. The error stack is NOT cleared after this call. */
    4196             : /* Author: CJH */
    4197          30 : PHP_FUNCTION(imap_last_error)
    4198             : {
    4199          30 :         ERRORLIST *cur=NIL;
    4200             : 
    4201          30 :         if (zend_parse_parameters_none() == FAILURE) {
    4202           0 :                 return;
    4203             :         }
    4204             : 
    4205          30 :         if (IMAPG(imap_errorstack) == NIL) {
    4206           4 :                 RETURN_FALSE;
    4207             :         }
    4208             : 
    4209          26 :         cur = IMAPG(imap_errorstack);
    4210         206 :         while (cur != NIL) {
    4211         180 :                 if (cur->next == NIL) {
    4212          26 :                         RETURN_STRING(cur->LTEXT, 1);
    4213             :                 }
    4214         154 :                 cur = cur->next;
    4215             :         }
    4216             : }
    4217             : /* }}} */
    4218             : 
    4219             : /* {{{ proto array imap_mime_header_decode(string str)
    4220             :    Decode mime header element in accordance with RFC 2047 and return array of objects containing 'charset' encoding and decoded 'text' */
    4221           1 : PHP_FUNCTION(imap_mime_header_decode)
    4222             : {
    4223             :         /* Author: Ted Parnefors <ted@mtv.se> */
    4224             :         zval *myobject;
    4225             :         char *str, *string, *charset, encoding, *text, *decode;
    4226             :         int str_len;
    4227           1 :         long charset_token, encoding_token, end_token, end, offset=0, i;
    4228             :         unsigned long newlength;
    4229             : 
    4230           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
    4231           0 :                 return;
    4232             :         }
    4233             : 
    4234           1 :         array_init(return_value);
    4235             : 
    4236           1 :         string = str;
    4237           1 :         end = str_len;
    4238             : 
    4239           1 :         charset = (char *) safe_emalloc((end + 1), 2, 0);
    4240           1 :         text = &charset[end + 1];
    4241           5 :         while (offset < end) {       /* Reached end of the string? */
    4242           3 :                 if ((charset_token = (long)php_memnstr(&string[offset], "=?", 2, string + end))) {        /* Is there anything encoded in the string? */
    4243           3 :                         charset_token -= (long)string;
    4244           3 :                         if (offset != charset_token) {  /* Is there anything before the encoded data? */
    4245             :                                 /* Retrieve unencoded data that is found before encoded data */
    4246           0 :                                 memcpy(text, &string[offset], charset_token-offset);
    4247           0 :                                 text[charset_token - offset] = 0x00;
    4248           0 :                                 MAKE_STD_ZVAL(myobject);
    4249           0 :                                 object_init(myobject);
    4250           0 :                                 add_property_string(myobject, "charset", "default", 1);
    4251           0 :                                 add_property_string(myobject, "text", text, 1);
    4252           0 :                                 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), (void *)&myobject, sizeof(zval *), NULL);
    4253             :                         }
    4254           3 :                         if ((encoding_token = (long)php_memnstr(&string[charset_token+2], "?", 1, string+end))) {         /* Find token for encoding */
    4255           3 :                                 encoding_token -= (long)string;
    4256           3 :                                 if ((end_token = (long)php_memnstr(&string[encoding_token+3], "?=", 2, string+end))) {    /* Find token for end of encoded data */
    4257           3 :                                         end_token -= (long)string;
    4258           3 :                                         memcpy(charset, &string[charset_token + 2], encoding_token - (charset_token + 2));  /* Extract charset encoding */
    4259           3 :                                         charset[encoding_token-(charset_token + 2)] = 0x00;
    4260           3 :                                         encoding=string[encoding_token + 1];    /* Extract encoding from string */
    4261           3 :                                         memcpy(text, &string[encoding_token + 3], end_token - (encoding_token + 3));        /* Extract text */
    4262           3 :                                         text[end_token - (encoding_token + 3)] = 0x00;
    4263           3 :                                         decode = text;
    4264           6 :                                         if (encoding == 'q' || encoding == 'Q') {       /* Decode 'q' encoded data */
    4265           3 :                                                 for(i=0; text[i] != 0x00; i++) if (text[i] == '_') text[i] = ' ';       /* Replace all *_' with space. */
    4266           3 :                                                 decode = (char *)rfc822_qprint((unsigned char *) text, strlen(text), &newlength);
    4267           0 :                                         } else if (encoding == 'b' || encoding == 'B') {
    4268           0 :                                                 decode = (char *)rfc822_base64((unsigned char *) text, strlen(text), &newlength); /* Decode 'B' encoded data */
    4269             :                                         }
    4270           3 :                                         if (decode == NULL) {
    4271           0 :                                                 efree(charset);
    4272           0 :                                                 zval_dtor(return_value);
    4273           0 :                                                 RETURN_FALSE;
    4274             :                                         }
    4275           3 :                                         MAKE_STD_ZVAL(myobject);
    4276           3 :                                         object_init(myobject);
    4277           3 :                                         add_property_string(myobject, "charset", charset, 1);
    4278           3 :                                         add_property_string(myobject, "text", decode, 1);
    4279           3 :                                         zend_hash_next_index_insert(Z_ARRVAL_P(return_value), (void *)&myobject, sizeof(zval *), NULL);
    4280             : 
    4281             :                                         /* only free decode if it was allocated by rfc822_qprint or rfc822_base64 */
    4282           3 :                                         if (decode != text) {
    4283           3 :                                                 fs_give((void**)&decode);
    4284             :                                         }
    4285             : 
    4286           3 :                                         offset = end_token+2;
    4287           3 :                                         for (i = 0; (string[offset + i] == ' ') || (string[offset + i] == 0x0a) || (string[offset + i] == 0x0d) || (string[offset + i] == '\t'); i++);
    4288           3 :                                         if ((string[offset + i] == '=') && (string[offset + i + 1] == '?') && (offset + i < end)) {
    4289           2 :                                                 offset += i;
    4290             :                                         }
    4291           3 :                                         continue;       /*/ Iterate the loop again please. */
    4292             :                                 }
    4293             :                         }
    4294             :                 } else {
    4295             :                         /* Just some tweaking to optimize the code, and get the end statements work in a general manner.
    4296             :                          * If we end up here we didn't find a position for "charset_token",
    4297             :                          * so we need to set it to the start of the yet unextracted data.
    4298             :                          */
    4299           0 :                         charset_token = offset;
    4300             :                 }
    4301             :                 /* Return the rest of the data as unencoded, as it was either unencoded or was missing separators
    4302             :                    which rendered the remainder of the string impossible for us to decode. */
    4303           0 :                 memcpy(text, &string[charset_token], end - charset_token);  /* Extract unencoded text from string */
    4304           0 :                 text[end - charset_token] = 0x00;
    4305           0 :                 MAKE_STD_ZVAL(myobject);
    4306           0 :                 object_init(myobject);
    4307           0 :                 add_property_string(myobject, "charset", "default", 1);
    4308           0 :                 add_property_string(myobject, "text", text, 1);
    4309           0 :                 zend_hash_next_index_insert(Z_ARRVAL_P(return_value), (void *)&myobject, sizeof(zval *), NULL);
    4310             : 
    4311           0 :                 offset = end;   /* We have reached the end of the string. */
    4312             :         }
    4313           1 :         efree(charset);
    4314             : }
    4315             : /* }}} */
    4316             : 
    4317             : /* Support Functions */
    4318             : 
    4319             : #ifdef HAVE_RFC822_OUTPUT_ADDRESS_LIST
    4320             : /* {{{ _php_rfc822_soutr
    4321             :  */
    4322          46 : static long _php_rfc822_soutr (void *stream, char *string)
    4323             : {
    4324          46 :         smart_str *ret = (smart_str*)stream;
    4325          46 :         int len = strlen(string);
    4326             : 
    4327          46 :         smart_str_appendl(ret, string, len);
    4328          46 :         return LONGT;
    4329             : }
    4330             : /* }}} */
    4331             : 
    4332             : /* {{{ _php_rfc822_write_address
    4333             :  */
    4334          46 : static char* _php_rfc822_write_address(ADDRESS *addresslist TSRMLS_DC)
    4335             : {
    4336             :         char address[MAILTMPLEN];
    4337          46 :         smart_str ret = {0};
    4338             :         RFC822BUFFER buf;
    4339             : 
    4340          46 :         buf.beg = address;
    4341          46 :         buf.cur = buf.beg;
    4342          46 :         buf.end = buf.beg + sizeof(address) - 1;
    4343          46 :         buf.s = &ret;
    4344          46 :         buf.f = _php_rfc822_soutr;
    4345          46 :         rfc822_output_address_list(&buf, addresslist, 0, NULL);
    4346          46 :         rfc822_output_flush(&buf);
    4347          46 :         smart_str_0(&ret);
    4348          46 :         return ret.c;
    4349             : }
    4350             : /* }}} */
    4351             : 
    4352             : #else
    4353             : 
    4354             : /* {{{ _php_rfc822_len
    4355             :  * Calculate string length based on imap's rfc822_cat function.
    4356             :  */
    4357             : static int _php_rfc822_len(char *str)
    4358             : {
    4359             :         int len;
    4360             :         char *p;
    4361             : 
    4362             :         if (!str || !*str) {
    4363             :                 return 0;
    4364             :         }
    4365             : 
    4366             :         /* strings with special characters will need to be quoted, as a safety measure we
    4367             :          * add 2 bytes for the quotes just in case.
    4368             :          */
    4369             :         len = strlen(str) + 2;
    4370             :         p = str;
    4371             :         /* rfc822_cat() will escape all " and \ characters, therefor we need to increase
    4372             :          * our buffer length to account for these characters.
    4373             :          */
    4374             :         while ((p = strpbrk(p, "\\\""))) {
    4375             :                 p++;
    4376             :                 len++;
    4377             :         }
    4378             : 
    4379             :         return len;
    4380             : }
    4381             : /* }}} */
    4382             : 
    4383             : /* {{{ _php_imap_get_address_size
    4384             :  */
    4385             : static int _php_imap_address_size (ADDRESS *addresslist)
    4386             : {
    4387             :         ADDRESS *tmp;
    4388             :         int ret=0, num_ent=0;
    4389             : 
    4390             :         tmp = addresslist;
    4391             : 
    4392             :         if (tmp) do {
    4393             :                 ret += _php_rfc822_len(tmp->personal);
    4394             :                 ret += _php_rfc822_len(tmp->adl);
    4395             :                 ret += _php_rfc822_len(tmp->mailbox);
    4396             :                 ret += _php_rfc822_len(tmp->host);
    4397             :                 num_ent++;
    4398             :         } while ((tmp = tmp->next));
    4399             : 
    4400             :         /*
    4401             :          * rfc822_write_address_full() needs some extra space for '<>,', etc.
    4402             :          * for this perpouse we allocate additional PHP_IMAP_ADDRESS_SIZE_BUF bytes
    4403             :          * by default this buffer is 10 bytes long
    4404             :         */
    4405             :         ret += (ret) ? num_ent*PHP_IMAP_ADDRESS_SIZE_BUF : 0;
    4406             : 
    4407             :         return ret;
    4408             : }
    4409             : 
    4410             : /* }}} */
    4411             : 
    4412             : /* {{{ _php_rfc822_write_address
    4413             :  */
    4414             : static char* _php_rfc822_write_address(ADDRESS *addresslist TSRMLS_DC)
    4415             : {
    4416             :         char address[SENDBUFLEN];
    4417             : 
    4418             :         if (_php_imap_address_size(addresslist) >= SENDBUFLEN) {
    4419             :                 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Address buffer overflow");
    4420             :                 return NULL;
    4421             :         }
    4422             :         address[0] = 0;
    4423             :         rfc822_write_address(address, addresslist);
    4424             :         return estrdup(address);
    4425             : }
    4426             : /* }}} */
    4427             : #endif
    4428             : /* {{{ _php_imap_parse_address
    4429             :  */
    4430           8 : static char* _php_imap_parse_address (ADDRESS *addresslist, zval *paddress TSRMLS_DC)
    4431             : {
    4432             :         char *fulladdress;
    4433             :         ADDRESS *addresstmp;
    4434             :         zval *tmpvals;
    4435             : 
    4436           8 :         addresstmp = addresslist;
    4437             : 
    4438           8 :         fulladdress = _php_rfc822_write_address(addresstmp TSRMLS_CC);
    4439             : 
    4440           8 :         addresstmp = addresslist;
    4441             :         do {
    4442           8 :                 MAKE_STD_ZVAL(tmpvals);
    4443           8 :                 object_init(tmpvals);
    4444           8 :                 if (addresstmp->personal) add_property_string(tmpvals, "personal", addresstmp->personal, 1);
    4445           8 :                 if (addresstmp->adl) add_property_string(tmpvals, "adl", addresstmp->adl, 1);
    4446           8 :                 if (addresstmp->mailbox) add_property_string(tmpvals, "mailbox", addresstmp->mailbox, 1);
    4447           8 :                 if (addresstmp->host) add_property_string(tmpvals, "host", addresstmp->host, 1);
    4448           8 :                 add_next_index_object(paddress, tmpvals TSRMLS_CC);
    4449           8 :         } while ((addresstmp = addresstmp->next));
    4450           8 :         return fulladdress;
    4451             : }
    4452             : /* }}} */
    4453             : 
    4454             : /* {{{ _php_make_header_object
    4455             :  */
    4456           2 : static void _php_make_header_object(zval *myzvalue, ENVELOPE *en TSRMLS_DC)
    4457             : {
    4458             :         zval *paddress;
    4459           2 :         char *fulladdress=NULL;
    4460             : 
    4461           2 :         object_init(myzvalue);
    4462             : 
    4463           2 :         if (en->remail) add_property_string(myzvalue, "remail", en->remail, 1);
    4464           2 :         if (en->date) add_property_string(myzvalue, "date", en->date, 1);
    4465           2 :         if (en->date) add_property_string(myzvalue, "Date", en->date, 1);
    4466           2 :         if (en->subject) add_property_string(myzvalue, "subject", en->subject, 1);
    4467           2 :         if (en->subject) add_property_string(myzvalue, "Subject", en->subject, 1);
    4468           2 :         if (en->in_reply_to) add_property_string(myzvalue, "in_reply_to", en->in_reply_to, 1);
    4469           2 :         if (en->message_id) add_property_string(myzvalue, "message_id", en->message_id, 1);
    4470           2 :         if (en->newsgroups) add_property_string(myzvalue, "newsgroups", en->newsgroups, 1);
    4471           2 :         if (en->followup_to) add_property_string(myzvalue, "followup_to", en->followup_to, 1);
    4472           2 :         if (en->references) add_property_string(myzvalue, "references", en->references, 1);
    4473             : 
    4474           2 :         if (en->to) {
    4475           2 :                 MAKE_STD_ZVAL(paddress);
    4476           2 :                 array_init(paddress);
    4477           2 :                 fulladdress = _php_imap_parse_address(en->to, paddress TSRMLS_CC);
    4478           2 :                 if (fulladdress) {
    4479           2 :                         add_property_string(myzvalue, "toaddress", fulladdress, 0);
    4480             :                 }
    4481           2 :                 add_assoc_object(myzvalue, "to", paddress TSRMLS_CC);
    4482             :         }
    4483             : 
    4484           2 :         if (en->from) {
    4485           2 :                 MAKE_STD_ZVAL(paddress);
    4486           2 :                 array_init(paddress);
    4487           2 :                 fulladdress = _php_imap_parse_address(en->from, paddress TSRMLS_CC);
    4488           2 :                 if (fulladdress) {
    4489           2 :                         add_property_string(myzvalue, "fromaddress", fulladdress, 0);
    4490             :                 }
    4491           2 :                 add_assoc_object(myzvalue, "from", paddress TSRMLS_CC);
    4492             :         }
    4493             : 
    4494           2 :         if (en->cc) {
    4495           0 :                 MAKE_STD_ZVAL(paddress);
    4496           0 :                 array_init(paddress);
    4497           0 :                 fulladdress = _php_imap_parse_address(en->cc, paddress TSRMLS_CC);
    4498           0 :                 if (fulladdress) {
    4499           0 :                         add_property_string(myzvalue, "ccaddress", fulladdress, 0);
    4500             :                 }
    4501           0 :                 add_assoc_object(myzvalue, "cc", paddress TSRMLS_CC);
    4502             :         }
    4503             : 
    4504           2 :         if (en->bcc) {
    4505           0 :                 MAKE_STD_ZVAL(paddress);
    4506           0 :                 array_init(paddress);
    4507           0 :                 fulladdress = _php_imap_parse_address(en->bcc, paddress TSRMLS_CC);
    4508           0 :                 if (fulladdress) {
    4509           0 :                         add_property_string(myzvalue, "bccaddress", fulladdress, 0);
    4510             :                 }
    4511           0 :                 add_assoc_object(myzvalue, "bcc", paddress TSRMLS_CC);
    4512             :         }
    4513             : 
    4514           2 :         if (en->reply_to) {
    4515           2 :                 MAKE_STD_ZVAL(paddress);
    4516           2 :                 array_init(paddress);
    4517           2 :                 fulladdress = _php_imap_parse_address(en->reply_to, paddress TSRMLS_CC);
    4518           2 :                 if (fulladdress) {
    4519           2 :                         add_property_string(myzvalue, "reply_toaddress", fulladdress, 0);
    4520             :                 }
    4521           2 :                 add_assoc_object(myzvalue, "reply_to", paddress TSRMLS_CC);
    4522             :         }
    4523             : 
    4524           2 :         if (en->sender) {
    4525           2 :                 MAKE_STD_ZVAL(paddress);
    4526           2 :                 array_init(paddress);
    4527           2 :                 fulladdress = _php_imap_parse_address(en->sender, paddress TSRMLS_CC);
    4528           2 :                 if (fulladdress) {
    4529           2 :                         add_property_string(myzvalue, "senderaddress", fulladdress, 0);
    4530             :                 }
    4531           2 :                 add_assoc_object(myzvalue, "sender", paddress TSRMLS_CC);
    4532             :         }
    4533             : 
    4534           2 :         if (en->return_path) {
    4535           0 :                 MAKE_STD_ZVAL(paddress);
    4536           0 :                 array_init(paddress);
    4537           0 :                 fulladdress = _php_imap_parse_address(en->return_path, paddress TSRMLS_CC);
    4538           0 :                 if (fulladdress) {
    4539           0 :                         add_property_string(myzvalue, "return_pathaddress", fulladdress, 0);
    4540             :                 }
    4541           0 :                 add_assoc_object(myzvalue, "return_path", paddress TSRMLS_CC);
    4542             :         }
    4543           2 : }
    4544             : /* }}} */
    4545             : 
    4546             : /* {{{ _php_imap_add_body
    4547             :  */
    4548           1 : void _php_imap_add_body(zval *arg, BODY *body TSRMLS_DC)
    4549             : {
    4550             :         zval *parametres, *param, *dparametres, *dparam;
    4551             :         PARAMETER *par, *dpar;
    4552             :         PART *part;
    4553             : 
    4554           1 :         if (body->type <= TYPEMAX) {
    4555           1 :                 add_property_long(arg, "type", body->type);
    4556             :         }
    4557             : 
    4558           1 :         if (body->encoding <= ENCMAX) {
    4559           1 :                 add_property_long(arg, "encoding", body->encoding);
    4560             :         }
    4561             : 
    4562           1 :         if (body->subtype) {
    4563           1 :                 add_property_long(arg, "ifsubtype", 1);
    4564           1 :                 add_property_string(arg, "subtype", body->subtype, 1);
    4565             :         } else {
    4566           0 :                 add_property_long(arg, "ifsubtype", 0);
    4567             :         }
    4568             : 
    4569           1 :         if (body->description) {
    4570           0 :                 add_property_long(arg, "ifdescription", 1);
    4571           0 :                 add_property_string(arg, "description", body->description, 1);
    4572             :         } else {
    4573           1 :                 add_property_long(arg, "ifdescription", 0);
    4574             :         }
    4575             : 
    4576           1 :         if (body->id) {
    4577           0 :                 add_property_long(arg, "ifid", 1);
    4578           0 :                 add_property_string(arg, "id", body->id, 1);
    4579             :         } else {
    4580           1 :                 add_property_long(arg, "ifid", 0);
    4581             :         }
    4582             : 
    4583           1 :         if (body->size.lines) {
    4584           1 :                 add_property_long(arg, "lines", body->size.lines);
    4585             :         }
    4586             : 
    4587           1 :         if (body->size.bytes) {
    4588           1 :                 add_property_long(arg, "bytes", body->size.bytes);
    4589             :         }
    4590             : 
    4591             : #ifdef IMAP41
    4592           1 :         if (body->disposition.type) {
    4593           0 :                 add_property_long(arg, "ifdisposition", 1);
    4594           0 :                 add_property_string(arg, "disposition", body->disposition.type, 1);
    4595             :         } else {
    4596           1 :                 add_property_long(arg, "ifdisposition", 0);
    4597             :         }
    4598             : 
    4599           1 :         if (body->disposition.parameter) {
    4600           0 :                 dpar = body->disposition.parameter;
    4601           0 :                 add_property_long(arg, "ifdparameters", 1);
    4602           0 :                 MAKE_STD_ZVAL(dparametres);
    4603           0 :                 array_init(dparametres);
    4604             :                 do {
    4605           0 :                         MAKE_STD_ZVAL(dparam);
    4606           0 :                         object_init(dparam);
    4607           0 :                         add_property_string(dparam, "attribute", dpar->attribute, 1);
    4608           0 :                         add_property_string(dparam, "value", dpar->value, 1);
    4609           0 :                         add_next_index_object(dparametres, dparam TSRMLS_CC);
    4610           0 :                 } while ((dpar = dpar->next));
    4611           0 :                 add_assoc_object(arg, "dparameters", dparametres TSRMLS_CC);
    4612             :         } else {
    4613           1 :                 add_property_long(arg, "ifdparameters", 0);
    4614             :         }
    4615             : #endif
    4616             : 
    4617           1 :         if ((par = body->parameter)) {
    4618           1 :                 add_property_long(arg, "ifparameters", 1);
    4619             : 
    4620           1 :                 MAKE_STD_ZVAL(parametres);
    4621           1 :                 array_init(parametres);
    4622             :                 do {
    4623           1 :                         MAKE_STD_ZVAL(param);
    4624           1 :                         object_init(param);
    4625           1 :                         if (par->attribute) {
    4626           1 :                                 add_property_string(param, "attribute", par->attribute, 1);
    4627             :                         }
    4628           1 :                         if (par->value) {
    4629           1 :                                 add_property_string(param, "value", par->value, 1);
    4630             :                         }
    4631             : 
    4632           1 :                         add_next_index_object(parametres, param TSRMLS_CC);
    4633           1 :                 } while ((par = par->next));
    4634             :         } else {
    4635           0 :                 MAKE_STD_ZVAL(parametres);
    4636           0 :                 object_init(parametres);
    4637           0 :                 add_property_long(arg, "ifparameters", 0);
    4638             :         }
    4639           1 :         add_assoc_object(arg, "parameters", parametres TSRMLS_CC);
    4640             : 
    4641             :         /* multipart message ? */
    4642           1 :         if (body->type == TYPEMULTIPART) {
    4643           0 :                 MAKE_STD_ZVAL(parametres);
    4644           0 :                 array_init(parametres);
    4645           0 :                 for (part = body->CONTENT_PART; part; part = part->next) {
    4646           0 :                         MAKE_STD_ZVAL(param);
    4647           0 :                         object_init(param);
    4648           0 :                         _php_imap_add_body(param, &part->body TSRMLS_CC);
    4649           0 :                         add_next_index_object(parametres, param TSRMLS_CC);
    4650             :                 }
    4651           0 :                 add_assoc_object(arg, "parts", parametres TSRMLS_CC);
    4652             :         }
    4653             : 
    4654             :         /* encapsulated message ? */
    4655           1 :         if ((body->type == TYPEMESSAGE) && (!strcasecmp(body->subtype, "rfc822"))) {
    4656           0 :                 body = body->CONTENT_MSG_BODY;
    4657           0 :                 MAKE_STD_ZVAL(parametres);
    4658           0 :                 array_init(parametres);
    4659           0 :                 MAKE_STD_ZVAL(param);
    4660           0 :                 object_init(param);
    4661           0 :                 _php_imap_add_body(param, body TSRMLS_CC);
    4662           0 :                 add_next_index_object(parametres, param TSRMLS_CC);
    4663           0 :                 add_assoc_object(arg, "parts", parametres TSRMLS_CC);
    4664             :         }
    4665           1 : }
    4666             : /* }}} */
    4667             : 
    4668             : /* imap_thread, stealing this from header cclient -rjs3 */
    4669             : /* {{{ build_thread_tree_helper
    4670             :  */
    4671           0 : static void build_thread_tree_helper(THREADNODE *cur, zval *tree, long *numNodes, char *buf)
    4672             : {
    4673           0 :         unsigned long thisNode = *numNodes;
    4674             : 
    4675             :         /* define "#.num" */
    4676           0 :         snprintf(buf, 25, "%ld.num", thisNode);
    4677             : 
    4678           0 :         add_assoc_long(tree, buf, cur->num);
    4679             : 
    4680           0 :         snprintf(buf, 25, "%ld.next", thisNode);
    4681           0 :         if(cur->next) {
    4682           0 :                 (*numNodes)++;
    4683           0 :                 add_assoc_long(tree, buf, *numNodes);
    4684           0 :                 build_thread_tree_helper(cur->next, tree, numNodes, buf);
    4685             :         } else { /* "null pointer" */
    4686           0 :                 add_assoc_long(tree, buf, 0);
    4687             :         }
    4688             : 
    4689           0 :         snprintf(buf, 25, "%ld.branch", thisNode);
    4690           0 :         if(cur->branch) {
    4691           0 :                 (*numNodes)++;
    4692           0 :                 add_assoc_long(tree, buf, *numNodes);
    4693           0 :                 build_thread_tree_helper(cur->branch, tree, numNodes, buf);
    4694             :         } else { /* "null pointer" */
    4695           0 :                 add_assoc_long(tree, buf, 0);
    4696             :         }
    4697           0 : }
    4698             : /* }}} */
    4699             : 
    4700             : /* {{{ build_thread_tree
    4701             :  */
    4702           0 : static int build_thread_tree(THREADNODE *top, zval **tree)
    4703             : {
    4704           0 :         long numNodes = 0;
    4705             :         char buf[25];
    4706             : 
    4707           0 :         array_init(*tree);
    4708             : 
    4709           0 :         build_thread_tree_helper(top, *tree, &numNodes, buf);
    4710             : 
    4711           0 :         return SUCCESS;
    4712             : }
    4713             : /* }}} */
    4714             : 
    4715             : /* {{{ proto array imap_thread(resource stream_id [, int options])
    4716             :    Return threaded by REFERENCES tree */
    4717           0 : PHP_FUNCTION(imap_thread)
    4718             : {
    4719             :         zval *streamind;
    4720             :         pils *imap_le_struct;
    4721           0 :         long flags = SE_FREE;
    4722           0 :         char criteria[] = "ALL";
    4723             :         THREADNODE *top;
    4724           0 :         int argc = ZEND_NUM_ARGS();
    4725           0 :         SEARCHPGM *pgm = NIL;
    4726             : 
    4727           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "r|l", &streamind, &flags) == FAILURE) {
    4728           0 :                 return;
    4729             :         }
    4730             : 
    4731           0 :         ZEND_FETCH_RESOURCE(imap_le_struct, pils *, &streamind, -1, "imap", le_imap);
    4732             : 
    4733           0 :         pgm = mail_criteria(criteria);
    4734           0 :         top = mail_thread(imap_le_struct->imap_stream, "REFERENCES", NIL, pgm, flags);
    4735           0 :         if (pgm && !(flags & SE_FREE)) {
    4736           0 :                 mail_free_searchpgm(&pgm);
    4737             :         }
    4738             : 
    4739           0 :         if(top == NIL) {
    4740           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Function returned an empty tree");
    4741           0 :                 RETURN_FALSE;
    4742             :         }
    4743             : 
    4744             :         /* Populate our return value data structure here. */
    4745           0 :         if(build_thread_tree(top, &return_value) == FAILURE) {
    4746           0 :                 mail_free_threadnode(&top);
    4747           0 :                 RETURN_FALSE;
    4748             :         }
    4749           0 :         mail_free_threadnode(&top);
    4750             : }
    4751             : /* }}} */
    4752             : 
    4753             : /* {{{ proto mixed imap_timeout(int timeout_type [, int timeout])
    4754             :    Set or fetch imap timeout */
    4755          13 : PHP_FUNCTION(imap_timeout)
    4756             : {
    4757          13 :         long ttype, timeout=-1;
    4758             :         int timeout_type;
    4759             : 
    4760          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &ttype, &timeout) == FAILURE) {
    4761           2 :                 RETURN_FALSE;
    4762             :         }
    4763             : 
    4764          11 :         if (timeout == -1) {
    4765           8 :                 switch (ttype) {
    4766             :                         case 1:
    4767           2 :                                 timeout_type = GET_OPENTIMEOUT;
    4768           2 :                                 break;
    4769             :                         case 2:
    4770           2 :                                 timeout_type = GET_READTIMEOUT;
    4771           2 :                                 break;
    4772             :                         case 3:
    4773           2 :                                 timeout_type = GET_WRITETIMEOUT;
    4774           2 :                                 break;
    4775             :                         case 4:
    4776           1 :                                 timeout_type = GET_CLOSETIMEOUT;
    4777           1 :                                 break;
    4778             :                         default:
    4779           1 :                                 RETURN_FALSE;
    4780             :                                 break;
    4781             :                 }
    4782             : 
    4783           7 :                 timeout = (long) mail_parameters(NIL, timeout_type, NIL);
    4784           7 :                 RETURN_LONG(timeout);
    4785           3 :         } else if (timeout >= 0) {
    4786           3 :                 switch (ttype) {
    4787             :                         case 1:
    4788           1 :                                 timeout_type = SET_OPENTIMEOUT;
    4789           1 :                                 break;
    4790             :                         case 2:
    4791           1 :                                 timeout_type = SET_READTIMEOUT;
    4792           1 :                                 break;
    4793             :                         case 3:
    4794           1 :                                 timeout_type = SET_WRITETIMEOUT;
    4795           1 :                                 break;
    4796             :                         case 4:
    4797           0 :                                 timeout_type = SET_CLOSETIMEOUT;
    4798           0 :                                 break;
    4799             :                         default:
    4800           0 :                                 RETURN_FALSE;
    4801             :                                 break;
    4802             :                 }
    4803             : 
    4804           3 :                 timeout = (long) mail_parameters(NIL, timeout_type, (void *) timeout);
    4805           3 :                 RETURN_TRUE;
    4806             :         } else {
    4807           0 :                 RETURN_FALSE;
    4808             :         }
    4809             : }
    4810             : /* }}} */
    4811             : 
    4812             : #define GETS_FETCH_SIZE 8196LU
    4813           2 : static char *php_mail_gets(readfn_t f, void *stream, unsigned long size, GETS_DATA *md) /* {{{ */
    4814             : {
    4815             :         TSRMLS_FETCH();
    4816             : 
    4817             :         /*      write to the gets stream if it is set,
    4818             :                 otherwise forward to c-clients gets */
    4819           2 :         if (IMAPG(gets_stream)) {
    4820             :                 char buf[GETS_FETCH_SIZE];
    4821             : 
    4822           6 :                 while (size) {
    4823             :                         unsigned long read;
    4824             : 
    4825           2 :                         if (size > GETS_FETCH_SIZE) {
    4826           0 :                                 read = GETS_FETCH_SIZE;
    4827           0 :                                 size -=GETS_FETCH_SIZE;
    4828             :                         } else {
    4829           2 :                                 read = size;
    4830           2 :                                 size = 0;
    4831             :                         }
    4832             : 
    4833           2 :                         if (!f(stream, read, buf)) {
    4834           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read from socket");
    4835           0 :                                 break;
    4836           2 :                         } else if (read != php_stream_write(IMAPG(gets_stream), buf, read)) {
    4837           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write to stream");
    4838           0 :                                 break;
    4839             :                         }
    4840             :                 }
    4841           2 :                 return NULL;
    4842             :         } else {
    4843           0 :                 char *buf = pemalloc(size + 1, 1);
    4844             : 
    4845           0 :                 if (f(stream, size, buf)) {
    4846           0 :                         buf[size] = '\0';
    4847             :                 } else {
    4848           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to read from socket");
    4849           0 :                         free(buf);
    4850           0 :                         buf = NULL;
    4851             :                 }
    4852           0 :                 return buf;
    4853             :         }
    4854             : }
    4855             : /* }}} */
    4856             : 
    4857             : /* {{{ Interfaces to C-client
    4858             :  */
    4859          21 : PHP_IMAP_EXPORT void mm_searched(MAILSTREAM *stream, unsigned long number)
    4860             : {
    4861          21 :         MESSAGELIST *cur = NIL;
    4862             :         TSRMLS_FETCH();
    4863             : 
    4864          21 :         if (IMAPG(imap_messages) == NIL) {
    4865           7 :                 IMAPG(imap_messages) = mail_newmessagelist();
    4866           7 :                 IMAPG(imap_messages)->msgid = number;
    4867           7 :                 IMAPG(imap_messages)->next = NIL;
    4868           7 :                 IMAPG(imap_messages_tail) = IMAPG(imap_messages);
    4869             :         } else {
    4870          14 :                 cur = IMAPG(imap_messages_tail);
    4871          14 :                 cur->next = mail_newmessagelist();
    4872          14 :                 cur = cur->next;
    4873          14 :                 cur->msgid = number;
    4874          14 :                 cur->next = NIL;
    4875          14 :                 IMAPG(imap_messages_tail) = cur;
    4876             :         }
    4877          21 : }
    4878             : 
    4879         297 : PHP_IMAP_EXPORT void mm_exists(MAILSTREAM *stream, unsigned long number)
    4880             : {
    4881         297 : }
    4882             : 
    4883           0 : PHP_IMAP_EXPORT void mm_expunged(MAILSTREAM *stream, unsigned long number)
    4884             : {
    4885           0 : }
    4886             : 
    4887         134 : PHP_IMAP_EXPORT void mm_flags(MAILSTREAM *stream, unsigned long number)
    4888             : {
    4889         134 : }
    4890             : 
    4891             : /* Author: CJH */
    4892         550 : PHP_IMAP_EXPORT void mm_notify(MAILSTREAM *stream, char *str, long errflg)
    4893             : {
    4894         550 :         STRINGLIST *cur = NIL;
    4895             :         TSRMLS_FETCH();
    4896             : 
    4897         550 :         if (strncmp(str, "[ALERT] ", 8) == 0) {
    4898           0 :                 if (IMAPG(imap_alertstack) == NIL) {
    4899           0 :                         IMAPG(imap_alertstack) = mail_newstringlist();
    4900           0 :                         IMAPG(imap_alertstack)->LSIZE = strlen(IMAPG(imap_alertstack)->LTEXT = cpystr(str));
    4901           0 :                         IMAPG(imap_alertstack)->next = NIL;
    4902             :                 } else {
    4903           0 :                         cur = IMAPG(imap_alertstack);
    4904           0 :                         while (cur->next != NIL) {
    4905           0 :                                 cur = cur->next;
    4906             :                         }
    4907           0 :                         cur->next = mail_newstringlist ();
    4908           0 :                         cur = cur->next;
    4909           0 :                         cur->LSIZE = strlen(cur->LTEXT = cpystr(str));
    4910           0 :                         cur->next = NIL;
    4911             :                 }
    4912             :         }
    4913         550 : }
    4914             : 
    4915          68 : PHP_IMAP_EXPORT void mm_list(MAILSTREAM *stream, DTYPE delimiter, char *mailbox, long attributes)
    4916             : {
    4917          68 :         STRINGLIST *cur=NIL;
    4918          68 :         FOBJECTLIST *ocur=NIL;
    4919             :         TSRMLS_FETCH();
    4920             : 
    4921          68 :         if (IMAPG(folderlist_style) == FLIST_OBJECT) {
    4922             :                 /* build up a the new array of objects */
    4923             :                 /* Author: CJH */
    4924          67 :                 if (IMAPG(imap_folder_objects) == NIL) {
    4925          33 :                         IMAPG(imap_folder_objects) = mail_newfolderobjectlist();
    4926          33 :                         IMAPG(imap_folder_objects)->LSIZE=strlen(IMAPG(imap_folder_objects)->LTEXT=cpystr(mailbox));
    4927          33 :                         IMAPG(imap_folder_objects)->delimiter = delimiter;
    4928          33 :                         IMAPG(imap_folder_objects)->attributes = attributes;
    4929          33 :                         IMAPG(imap_folder_objects)->next = NIL;
    4930          33 :                         IMAPG(imap_folder_objects_tail) = IMAPG(imap_folder_objects);
    4931             :                 } else {
    4932          34 :                         ocur=IMAPG(imap_folder_objects_tail);
    4933          34 :                         ocur->next=mail_newfolderobjectlist();
    4934          34 :                         ocur=ocur->next;
    4935          34 :                         ocur->LSIZE = strlen(ocur->LTEXT = cpystr(mailbox));
    4936          34 :                         ocur->delimiter = delimiter;
    4937          34 :                         ocur->attributes = attributes;
    4938          34 :                         ocur->next = NIL;
    4939          34 :                         IMAPG(imap_folder_objects_tail) = ocur;
    4940             :                 }
    4941             : 
    4942             :         } else {
    4943             :                 /* build the old IMAPG(imap_folders) variable to allow old imap_listmailbox() to work */
    4944           1 :                 if (!(attributes & LATT_NOSELECT)) {
    4945           1 :                         if (IMAPG(imap_folders) == NIL) {
    4946           1 :                                 IMAPG(imap_folders)=mail_newstringlist();
    4947           1 :                                 IMAPG(imap_folders)->LSIZE=strlen(IMAPG(imap_folders)->LTEXT=cpystr(mailbox));
    4948           1 :                                 IMAPG(imap_folders)->next=NIL;
    4949           1 :                                 IMAPG(imap_folders_tail) = IMAPG(imap_folders);
    4950             :                         } else {
    4951           0 :                                 cur=IMAPG(imap_folders_tail);
    4952           0 :                                 cur->next=mail_newstringlist ();
    4953           0 :                                 cur=cur->next;
    4954           0 :                                 cur->LSIZE = strlen (cur->LTEXT = cpystr (mailbox));
    4955           0 :                                 cur->next = NIL;
    4956           0 :                                 IMAPG(imap_folders_tail) = cur;
    4957             :                         }
    4958             :                 }
    4959             :         }
    4960          68 : }
    4961             : 
    4962           2 : PHP_IMAP_EXPORT void mm_lsub(MAILSTREAM *stream, DTYPE delimiter, char *mailbox, long attributes)
    4963             : {
    4964           2 :         STRINGLIST *cur=NIL;
    4965           2 :         FOBJECTLIST *ocur=NIL;
    4966             :         TSRMLS_FETCH();
    4967             : 
    4968           2 :         if (IMAPG(folderlist_style) == FLIST_OBJECT) {
    4969             :                 /* build the array of objects */
    4970             :                 /* Author: CJH */
    4971           1 :                 if (IMAPG(imap_sfolder_objects) == NIL) {
    4972           1 :                         IMAPG(imap_sfolder_objects) = mail_newfolderobjectlist();
    4973           1 :                         IMAPG(imap_sfolder_objects)->LSIZE=strlen(IMAPG(imap_sfolder_objects)->LTEXT=cpystr(mailbox));
    4974           1 :                         IMAPG(imap_sfolder_objects)->delimiter = delimiter;
    4975           1 :                         IMAPG(imap_sfolder_objects)->attributes = attributes;
    4976           1 :                         IMAPG(imap_sfolder_objects)->next = NIL;
    4977           1 :                         IMAPG(imap_sfolder_objects_tail) = IMAPG(imap_sfolder_objects);
    4978             :                 } else {
    4979           0 :                         ocur=IMAPG(imap_sfolder_objects_tail);
    4980           0 :                         ocur->next=mail_newfolderobjectlist();
    4981           0 :                         ocur=ocur->next;
    4982           0 :                         ocur->LSIZE=strlen(ocur->LTEXT = cpystr(mailbox));
    4983           0 :                         ocur->delimiter = delimiter;
    4984           0 :                         ocur->attributes = attributes;
    4985           0 :                         ocur->next = NIL;
    4986           0 :                         IMAPG(imap_sfolder_objects_tail) = ocur;
    4987             :                 }
    4988             :         } else {
    4989             :                 /* build the old simple array for imap_listsubscribed() */
    4990           1 :                 if (IMAPG(imap_sfolders) == NIL) {
    4991           1 :                         IMAPG(imap_sfolders)=mail_newstringlist();
    4992           1 :                         IMAPG(imap_sfolders)->LSIZE=strlen(IMAPG(imap_sfolders)->LTEXT=cpystr(mailbox));
    4993           1 :                         IMAPG(imap_sfolders)->next=NIL;
    4994           1 :                         IMAPG(imap_sfolders_tail) = IMAPG(imap_sfolders);
    4995             :                 } else {
    4996           0 :                         cur=IMAPG(imap_sfolders_tail);
    4997           0 :                         cur->next=mail_newstringlist ();
    4998           0 :                         cur=cur->next;
    4999           0 :                         cur->LSIZE = strlen (cur->LTEXT = cpystr (mailbox));
    5000           0 :                         cur->next = NIL;
    5001           0 :                         IMAPG(imap_sfolders_tail) = cur;
    5002             :                 }
    5003             :         }
    5004           2 : }
    5005             : 
    5006           1 : PHP_IMAP_EXPORT void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS *status)
    5007             : {
    5008             :         TSRMLS_FETCH();
    5009             : 
    5010           1 :         IMAPG(status_flags)=status->flags;
    5011           1 :         if (IMAPG(status_flags) & SA_MESSAGES) {
    5012           1 :                 IMAPG(status_messages)=status->messages;
    5013             :         }
    5014           1 :         if (IMAPG(status_flags) & SA_RECENT) {
    5015           1 :                 IMAPG(status_recent)=status->recent;
    5016             :         }
    5017           1 :         if (IMAPG(status_flags) & SA_UNSEEN) {
    5018           1 :                 IMAPG(status_unseen)=status->unseen;
    5019             :         }
    5020           1 :         if (IMAPG(status_flags) & SA_UIDNEXT) {
    5021           1 :                 IMAPG(status_uidnext)=status->uidnext;
    5022             :         }
    5023           1 :         if (IMAPG(status_flags) & SA_UIDVALIDITY) {
    5024           1 :                 IMAPG(status_uidvalidity)=status->uidvalidity;
    5025             :         }
    5026           1 : }
    5027             : 
    5028         418 : PHP_IMAP_EXPORT void mm_log(char *str, long errflg)
    5029             : {
    5030         418 :         ERRORLIST *cur = NIL;
    5031             :         TSRMLS_FETCH();
    5032             : 
    5033             :         /* Author: CJH */
    5034         418 :         if (errflg != NIL) { /* CJH: maybe put these into a more comprehensive log for debugging purposes? */
    5035          22 :                 if (IMAPG(imap_errorstack) == NIL) {
    5036           6 :                         IMAPG(imap_errorstack) = mail_newerrorlist();
    5037           6 :                         IMAPG(imap_errorstack)->LSIZE = strlen(IMAPG(imap_errorstack)->LTEXT = cpystr(str));
    5038           6 :                         IMAPG(imap_errorstack)->errflg = errflg;
    5039           6 :                         IMAPG(imap_errorstack)->next = NIL;
    5040             :                 } else {
    5041          16 :                         cur = IMAPG(imap_errorstack);
    5042          91 :                         while (cur->next != NIL) {
    5043          59 :                                 cur = cur->next;
    5044             :                         }
    5045          16 :                         cur->next = mail_newerrorlist();
    5046          16 :                         cur = cur->next;
    5047          16 :                         cur->LSIZE = strlen(cur->LTEXT = cpystr(str));
    5048          16 :                         cur->errflg = errflg;
    5049          16 :                         cur->next = NIL;
    5050             :                 }
    5051             :         }
    5052         418 : }
    5053             : 
    5054           0 : PHP_IMAP_EXPORT void mm_dlog(char *str)
    5055             : {
    5056             :         /* CJH: this is for debugging; it might be useful to allow setting
    5057             :            the stream to debug mode and capturing this somewhere - syslog?
    5058             :            php debugger? */
    5059           0 : }
    5060             : 
    5061         181 : PHP_IMAP_EXPORT void mm_login(NETMBX *mb, char *user, char *pwd, long trial)
    5062             : {
    5063             :         TSRMLS_FETCH();
    5064             : 
    5065         181 :         if (*mb->user) {
    5066           0 :                 strlcpy (user, mb->user, MAILTMPLEN);
    5067             :         } else {
    5068         181 :                 strlcpy (user, IMAPG(imap_user), MAILTMPLEN);
    5069             :         }
    5070         181 :         strlcpy (pwd, IMAPG(imap_password), MAILTMPLEN);
    5071         181 : }
    5072             : 
    5073           0 : PHP_IMAP_EXPORT void mm_critical(MAILSTREAM *stream)
    5074             : {
    5075           0 : }
    5076             : 
    5077           0 : PHP_IMAP_EXPORT void mm_nocritical(MAILSTREAM *stream)
    5078             : {
    5079           0 : }
    5080             : 
    5081           0 : PHP_IMAP_EXPORT long mm_diskerror(MAILSTREAM *stream, long errcode, long serious)
    5082             : {
    5083           0 :         return 1;
    5084             : }
    5085             : 
    5086           0 : PHP_IMAP_EXPORT void mm_fatal(char *str)
    5087             : {
    5088           0 : }
    5089             : /* }}} */
    5090             : 
    5091             : /*
    5092             :  * Local variables:
    5093             :  * tab-width: 4
    5094             :  * c-basic-offset: 4
    5095             :  * End:
    5096             :  * vim600: sw=4 ts=4 fdm=marker
    5097             :  * vim<600: sw=4 ts=4
    5098             :  */

Generated by: LCOV version 1.10

Generated at Sun, 13 Jul 2014 23:57:50 +0000 (8 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.