Skip to content

Commit 17861b9

Browse files
author
Ilia Alshanetsky
committed
Fixed bug #24161 (No timeout value for imap functions)
Added imap_timeout() that allows the user to specify as well as retrieve timeout values and set default timeout values based on the default_socket_timeout ini setting.
1 parent 1d008db commit 17861b9

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

ext/imap/php_imap.c

+70-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "php_ini.h"
3939
#include "ext/standard/php_string.h"
4040
#include "ext/standard/info.h"
41+
#include "ext/standard/file.h"
4142

4243
#ifdef ERROR
4344
#undef ERROR
@@ -130,6 +131,7 @@ function_entry imap_functions[] = {
130131
PHP_FE(imap_utf7_encode, NULL)
131132
PHP_FE(imap_mime_header_decode, NULL)
132133
PHP_FE(imap_thread, NULL)
134+
PHP_FE(imap_timeout, NULL)
133135

134136
#if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
135137
PHP_FE(imap_get_quota, NULL)
@@ -337,6 +339,7 @@ void mail_free_messagelist(MESSAGELIST **msglist, MESSAGELIST **tail)
337339
* Called via the mail_parameter function in c-client:src/c-client/mail.c
338340
* Author DRK
339341
*/
342+
340343
void mail_getquota(MAILSTREAM *stream, char *qroot, QUOTALIST *qlist)
341344
{
342345
zval *t_map, *return_value;
@@ -450,6 +453,17 @@ PHP_MINIT_FUNCTION(imap)
450453
/* lets allow NIL */
451454
REGISTER_LONG_CONSTANT("NIL", NIL, CONST_PERSISTENT | CONST_CS);
452455

456+
/* set default timeout values */
457+
mail_parameters(NIL, SET_OPENTIMEOUT, (void *) FG(default_socket_timeout));
458+
mail_parameters(NIL, SET_READTIMEOUT, (void *) FG(default_socket_timeout));
459+
mail_parameters(NIL, SET_WRITETIMEOUT, (void *) FG(default_socket_timeout));
460+
mail_parameters(NIL, SET_CLOSETIMEOUT, (void *) FG(default_socket_timeout));
461+
462+
/* timeout constants */
463+
REGISTER_LONG_CONSTANT("IMAP_OPENTIMEOUT", 1, CONST_PERSISTENT | CONST_CS);
464+
REGISTER_LONG_CONSTANT("IMAP_READTIMEOUT", 2, CONST_PERSISTENT | CONST_CS);
465+
REGISTER_LONG_CONSTANT("IMAP_WRITETIMEOUT", 3, CONST_PERSISTENT | CONST_CS);
466+
REGISTER_LONG_CONSTANT("IMAP_CLOSETIMEOUT", 4, CONST_PERSISTENT | CONST_CS);
453467

454468
/* Open Options */
455469

@@ -717,7 +731,7 @@ static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
717731

718732
IMAPG(imap_user) = estrndup(Z_STRVAL_PP(user), Z_STRLEN_PP(user));
719733
IMAPG(imap_password) = estrndup(Z_STRVAL_PP(passwd), Z_STRLEN_PP(passwd));
720-
734+
721735
imap_stream = mail_open(NIL, Z_STRVAL_PP(mailbox), flags);
722736

723737
if (imap_stream == NIL) {
@@ -4009,6 +4023,61 @@ PHP_FUNCTION (imap_thread)
40094023
}
40104024
/* }}} */
40114025

4026+
PHP_FUNCTION (imap_timeout)
4027+
{
4028+
long ttype, timeout=-1;
4029+
int timeout_type;
4030+
4031+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &ttype, &timeout) == FAILURE) {
4032+
RETURN_FALSE;
4033+
}
4034+
4035+
if (timeout == -1) {
4036+
switch (ttype) {
4037+
case 1:
4038+
timeout_type = GET_OPENTIMEOUT;
4039+
break;
4040+
case 2:
4041+
timeout_type = GET_READTIMEOUT;
4042+
break;
4043+
case 3:
4044+
timeout_type = GET_WRITETIMEOUT;
4045+
break;
4046+
case 4:
4047+
timeout_type = GET_CLOSETIMEOUT;
4048+
break;
4049+
default:
4050+
RETURN_FALSE;
4051+
break;
4052+
}
4053+
4054+
timeout = (long) mail_parameters(NIL, timeout_type, NIL);
4055+
RETURN_LONG(timeout);
4056+
} else if (timeout >= 0) {
4057+
switch (ttype) {
4058+
case 1:
4059+
timeout_type = SET_OPENTIMEOUT;
4060+
break;
4061+
case 2:
4062+
timeout_type = SET_READTIMEOUT;
4063+
break;
4064+
case 3:
4065+
timeout_type = SET_WRITETIMEOUT;
4066+
break;
4067+
case 4:
4068+
timeout_type = SET_CLOSETIMEOUT;
4069+
break;
4070+
default:
4071+
RETURN_FALSE;
4072+
break;
4073+
}
4074+
4075+
timeout = (long) mail_parameters(NIL, timeout_type, (void *) timeout);
4076+
RETURN_TRUE;
4077+
} else {
4078+
RETURN_FALSE;
4079+
}
4080+
}
40124081

40134082
/* {{{ Interfaces to C-client
40144083
*/

ext/imap/php_imap.h

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ PHP_FUNCTION(imap_utf7_decode);
165165
PHP_FUNCTION(imap_utf7_encode);
166166
PHP_FUNCTION(imap_mime_header_decode);
167167
PHP_FUNCTION(imap_thread);
168+
PHP_FUNCTION(imap_timeout);
168169

169170
#if defined(HAVE_IMAP2000) || defined(HAVE_IMAP2001)
170171
PHP_FUNCTION(imap_get_quota);

0 commit comments

Comments
 (0)