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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:43 +0000 (4 days ago)

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