PageRenderTime 74ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/imap/php_imap.c

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