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: 1301 2029 64.1 %
Date: 2015-01-26 Functions: 79 106 74.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:45 +0000 (3 days ago)

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