Skip to content

IMAP: Fix UNKNOWN default values #6179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 63 additions & 61 deletions ext/imap/php_imap.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,10 @@ static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
zend_long retries = 0, flags = NIL, cl_flags = NIL;
MAILSTREAM *imap_stream;
pils *imap_le_struct;
zval *params = NULL;
HashTable *params = NULL;
int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc, "PSS|lla", &mailbox, &user,
if (zend_parse_parameters(argc, "PSS|llh", &mailbox, &user,
&passwd, &flags, &retries, &params) == FAILURE) {
RETURN_THROWS();
}
Expand All @@ -723,7 +723,7 @@ static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
if (params) {
zval *disabled_auth_method;

if ((disabled_auth_method = zend_hash_str_find(Z_ARRVAL_P(params), "DISABLE_AUTHENTICATOR", sizeof("DISABLE_AUTHENTICATOR") - 1)) != NULL) {
if ((disabled_auth_method = zend_hash_str_find(params, "DISABLE_AUTHENTICATOR", sizeof("DISABLE_AUTHENTICATOR") - 1)) != NULL) {
switch (Z_TYPE_P(disabled_auth_method)) {
case IS_STRING:
if (Z_STRLEN_P(disabled_auth_method) > 1) {
Expand Down Expand Up @@ -866,7 +866,7 @@ PHP_FUNCTION(imap_append)
pils *imap_le_struct;
STRING st;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS|SS", &streamind, &folder, &message, &flags, &internal_date) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS|S!S!", &streamind, &folder, &message, &flags, &internal_date) == FAILURE) {
RETURN_THROWS();
}

Expand Down Expand Up @@ -1603,15 +1603,14 @@ PHP_FUNCTION(imap_undelete)
PHP_FUNCTION(imap_headerinfo)
{
zval *streamind;
zend_string *defaulthost = NULL;
int argc = ZEND_NUM_ARGS();
zend_long msgno, fromlength, subjectlength;
pils *imap_le_struct;
MESSAGECACHE *cache;
ENVELOPE *en;
char dummy[2000], fulladdress[MAILTMPLEN + 1];

if (zend_parse_parameters(argc, "rl|llS", &streamind, &msgno, &fromlength, &subjectlength, &defaulthost) == FAILURE) {
if (zend_parse_parameters(argc, "rl|ll", &streamind, &msgno, &fromlength, &subjectlength) == FAILURE) {
RETURN_THROWS();
}

Expand Down Expand Up @@ -2647,7 +2646,7 @@ PHP_FUNCTION(imap_sort)
SEARCHPGM *spg=NIL;
int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc, "rll|lSS", &streamind, &pgm, &rev, &flags, &criteria, &charset) == FAILURE) {
if (zend_parse_parameters(argc, "rll|lS!S!", &streamind, &pgm, &rev, &flags, &criteria, &charset) == FAILURE) {
RETURN_THROWS();
}

Expand All @@ -2665,7 +2664,7 @@ PHP_FUNCTION(imap_sort)
RETURN_FALSE;
}
}
if (argc >= 5) {
if (criteria) {
search_criteria = estrndup(ZSTR_VAL(criteria), ZSTR_LEN(criteria));
spg = mail_criteria(search_criteria);
efree(search_criteria);
Expand All @@ -2678,7 +2677,7 @@ PHP_FUNCTION(imap_sort)
mypgm->function = (short) pgm;
mypgm->next = NIL;

slst = mail_sort(imap_le_struct->imap_stream, (argc == 6 ? ZSTR_VAL(charset) : NIL), spg, mypgm, (argc >= 4 ? flags : NIL));
slst = mail_sort(imap_le_struct->imap_stream, (charset ? ZSTR_VAL(charset) : NIL), spg, mypgm, (argc >= 4 ? flags : NIL));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
slst = mail_sort(imap_le_struct->imap_stream, (charset ? ZSTR_VAL(charset) : NIL), spg, mypgm, (argc >= 4 ? flags : NIL));
slst = mail_sort(imap_le_struct->imap_stream, (charset ? ZSTR_VAL(charset) : NIL), spg, mypgm, flags;

to remove argc dependency. NIL is defined as 0.


if (spg && !(flags & SE_FREE)) {
mail_free_searchpgm(&spg);
Expand Down Expand Up @@ -3411,7 +3410,8 @@ PHP_FUNCTION(imap_mail_compose)
/* }}} */

/* {{{ _php_imap_mail */
int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *cc, char *bcc, char* rpath)
bool _php_imap_mail(zend_string *to, zend_string *subject, zend_string *message, zend_string *headers,
zend_string *cc, zend_string *bcc, zend_string* rpath)
{
#ifdef PHP_WIN32
int tsm_err;
Expand All @@ -3420,6 +3420,9 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
int ret;
#endif

ZEND_ASSERT(to && ZSTR_LEN(to) != 0);
ZEND_ASSERT(subject && ZSTR_LEN(subject) != 0);

#ifdef PHP_WIN32
char *tempMailTo;
char *tsm_errmsg = NULL;
Expand All @@ -3428,56 +3431,55 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
size_t offset, bufferLen = 0;
size_t bt_len;

/* Add "To" field's necessary buffer length */
bufferLen += ZSTR_LEN(to) + 6;
if (headers) {
bufferLen += strlen(headers);
}
if (to) {
bufferLen += strlen(to) + 6;
bufferLen += ZSTR_LEN(headers);
}
if (cc) {
bufferLen += strlen(cc) + 6;
bufferLen += ZSTR_LEN(cc) + 6;
}

#define PHP_IMAP_CLEAN if (bufferTo) efree(bufferTo); if (bufferCc) efree(bufferCc); if (bufferBcc) efree(bufferBcc); if (bufferHeader) efree(bufferHeader);
#define PHP_IMAP_BAD_DEST PHP_IMAP_CLEAN; efree(tempMailTo); return (BAD_MSG_DESTINATION);

bufferHeader = (char *)safe_emalloc(bufferLen, 1, 1);
memset(bufferHeader, 0, bufferLen);
if (to && *to) {
strlcat(bufferHeader, "To: ", bufferLen + 1);
strlcat(bufferHeader, to, bufferLen + 1);
strlcat(bufferHeader, "\r\n", bufferLen + 1);
tempMailTo = estrdup(to);
bt_len = strlen(to);
bufferTo = (char *)safe_emalloc(bt_len, 1, 1);
bt_len++;
offset = 0;
addr = NULL;
rfc822_parse_adrlist(&addr, tempMailTo, "NO HOST");
while (addr) {
if (addr->host == NULL || strcmp(addr->host, ERRHOST) == 0) {
PHP_IMAP_BAD_DEST;
} else {
bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->mailbox));
bt_len += strlen(addr->mailbox);
bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->host));
bt_len += strlen(addr->host);
offset += slprintf(bufferTo + offset, bt_len - offset, "%s@%s,", addr->mailbox, addr->host);
}
addr = addr->next;
}
efree(tempMailTo);
if (offset>0) {
bufferTo[offset-1] = 0;

/* Handle "To" Field */
strlcat(bufferHeader, "To: ", bufferLen + 1);
strlcat(bufferHeader, ZSTR_VAL(to), bufferLen + 1);
strlcat(bufferHeader, "\r\n", bufferLen + 1);
tempMailTo = estrdup(ZSTR_VAL(to));
bt_len = ZSTR_LEN(to);
bufferTo = (char *)safe_emalloc(bt_len, 1, 1);
bt_len++;
offset = 0;
addr = NULL;
rfc822_parse_adrlist(&addr, tempMailTo, "NO HOST");
while (addr) {
if (addr->host == NULL || strcmp(addr->host, ERRHOST) == 0) {
PHP_IMAP_BAD_DEST;
} else {
bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->mailbox));
bt_len += strlen(addr->mailbox);
bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->host));
bt_len += strlen(addr->host);
offset += slprintf(bufferTo + offset, bt_len - offset, "%s@%s,", addr->mailbox, addr->host);
}
addr = addr->next;
}
efree(tempMailTo);
if (offset>0) {
bufferTo[offset-1] = 0;
}

if (cc && *cc) {
if (cc && ZSTR_LEN(cc) != 0) {
strlcat(bufferHeader, "Cc: ", bufferLen + 1);
strlcat(bufferHeader, cc, bufferLen + 1);
strlcat(bufferHeader, ZSTR_VAL(cc), bufferLen + 1);
strlcat(bufferHeader, "\r\n", bufferLen + 1);
tempMailTo = estrdup(cc);
bt_len = strlen(cc);
tempMailTo = estrdup(ZSTR_VAL(cc));
bt_len = ZSTR_LEN(cc);
bufferCc = (char *)safe_emalloc(bt_len, 1, 1);
bt_len++;
offset = 0;
Expand All @@ -3501,9 +3503,9 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
}
}

if (bcc && *bcc) {
tempMailTo = estrdup(bcc);
bt_len = strlen(bcc);
if (bcc && ZSTR_LEN(bcc)) {
tempMailTo = estrdup(ZSTR_VAL(bcc));
bt_len = ZSTR_LEN(bcc);
bufferBcc = (char *)safe_emalloc(bt_len, 1, 1);
bt_len++;
offset = 0;
Expand All @@ -3527,11 +3529,12 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
}
}

if (headers && *headers) {
strlcat(bufferHeader, headers, bufferLen + 1);
if (headers && ZSTR_LEN(headers)) {
strlcat(bufferHeader, ZSTR_VAL(headers), bufferLen + 1);
}

if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, bufferHeader, subject, bufferTo, message, bufferCc, bufferBcc, rpath) != SUCCESS) {
if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, bufferHeader, ZSTR_VAL(subject),
bufferTo, ZSTR_VAL(message), bufferCc, bufferBcc, ZSTR_VAL(rpath)) != SUCCESS) {
if (tsm_errmsg) {
php_error_docref(NULL, E_WARNING, "%s", tsm_errmsg);
efree(tsm_errmsg);
Expand All @@ -3548,15 +3551,15 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
}
sendmail = popen(INI_STR("sendmail_path"), "w");
if (sendmail) {
if (rpath && rpath[0]) fprintf(sendmail, "From: %s\n", rpath);
fprintf(sendmail, "To: %s\n", to);
if (cc && cc[0]) fprintf(sendmail, "Cc: %s\n", cc);
if (bcc && bcc[0]) fprintf(sendmail, "Bcc: %s\n", bcc);
fprintf(sendmail, "Subject: %s\n", subject);
if (ZSTR_LEN(rpath) != 0) fprintf(sendmail, "From: %s\n", ZSTR_VAL(rpath));
fprintf(sendmail, "To: %s\n", ZSTR_VAL(to));
if (ZSTR_LEN(cc) != 0) fprintf(sendmail, "Cc: %s\n", ZSTR_VAL(cc));
if (ZSTR_LEN(bcc) != 0) fprintf(sendmail, "Bcc: %s\n", ZSTR_VAL(bcc));
fprintf(sendmail, "Subject: %s\n", ZSTR_VAL(subject));
if (headers != NULL) {
fprintf(sendmail, "%s\n", headers);
fprintf(sendmail, "%s\n", ZSTR_VAL(headers));
}
fprintf(sendmail, "\n%s\n", message);
fprintf(sendmail, "\n%s\n", ZSTR_VAL(message));
ret = pclose(sendmail);

return ret != -1;
Expand All @@ -3575,7 +3578,7 @@ PHP_FUNCTION(imap_mail)
zend_string *to=NULL, *message=NULL, *headers=NULL, *subject=NULL, *cc=NULL, *bcc=NULL, *rpath=NULL;
int argc = ZEND_NUM_ARGS();

if (zend_parse_parameters(argc, "SSS|SSSS", &to, &subject, &message,
if (zend_parse_parameters(argc, "PPP|P!P!P!P!", &to, &subject, &message,
&headers, &cc, &bcc, &rpath) == FAILURE) {
RETURN_THROWS();
}
Expand All @@ -3598,8 +3601,7 @@ PHP_FUNCTION(imap_mail)
php_error_docref(NULL, E_WARNING, "No message string in mail command");
}

if (_php_imap_mail(ZSTR_VAL(to), ZSTR_VAL(subject), ZSTR_VAL(message), headers?ZSTR_VAL(headers):NULL, cc?ZSTR_VAL(cc):NULL,
bcc?ZSTR_VAL(bcc):NULL, rpath?ZSTR_VAL(rpath):NULL)) {
if (_php_imap_mail(to, subject, message, headers, cc, bcc, rpath)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
Expand Down
16 changes: 5 additions & 11 deletions ext/imap/php_imap.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* @return resource|false
*/
function imap_open(string $mailbox, string $user, string $password, int $options = 0, int $n_retries = 0, array $params = UNKNOWN) {}
function imap_open(string $mailbox, string $user, string $password, int $options = 0, int $n_retries = 0, array $params = []) {}

/**
* @param resource $stream_id
Expand All @@ -27,13 +27,7 @@ function imap_num_recent($stream_id): int|false {}
function imap_headers($stream_id): array|false {}

/** @param resource $stream_id */
function imap_headerinfo($stream_id, int $msg_no, int $from_length = 0, int $subject_length = 0, string $default_host = UNKNOWN): stdClass|false {}

/**
* @param resource $stream_id
* @alias imap_headerinfo
*/
function imap_header($stream_id, int $msg_no, int $from_length = 0, int $subject_length = 0, string $default_host = UNKNOWN): stdClass|false {}
function imap_headerinfo($stream_id, int $msg_no, int $from_length = 0, int $subject_length = 0): stdClass|false {}

function imap_rfc822_parse_headers(string $headers, string $default_host = "UNKNOWN"): stdClass {}

Expand Down Expand Up @@ -148,7 +142,7 @@ function imap_subscribe($stream_id, string $mailbox): bool {}
function imap_unsubscribe($stream_id, string $mailbox): bool {}

/** @param resource $stream_id */
function imap_append($stream_id, string $folder, string $message, string $options = UNKNOWN, string $internal_date = UNKNOWN): bool {}
function imap_append($stream_id, string $folder, string $message, ?string $options = null, ?string $internal_date = null): bool {}

/** @param resource $stream_id */
function imap_ping($stream_id): bool {}
Expand Down Expand Up @@ -180,7 +174,7 @@ function imap_setflag_full($stream_id, string $sequence, string $flag, int $opti
function imap_clearflag_full($stream_id, string $sequence, string $flag, int $options = 0): bool {}

/** @param resource $stream_id */
function imap_sort($stream_id, int $criteria, int $reverse, int $options = 0, string $search_criteria = UNKNOWN, string $charset = UNKNOWN): array|false {}
function imap_sort($stream_id, int $criteria, int $reverse, int $options = 0, ?string $search_criteria = null, ?string $charset = null): array|false {}

/** @param resource $stream_id */
function imap_uid($stream_id, int $msg_no): int|false {}
Expand Down Expand Up @@ -258,4 +252,4 @@ function imap_setacl($stream_id, string $mailbox, string $id, string $rights): b
function imap_getacl($stream_id, string $mailbox): array|false {}
#endif

function imap_mail(string $to, string $subject, string $message, string $additional_headers = UNKNOWN, string $cc = UNKNOWN, string $bcc = UNKNOWN, string $rpath = UNKNOWN): bool {}
function imap_mail(string $to, string $subject, string $message, ?string $additional_headers = null, ?string $cc = null, ?string $bcc = null, ?string $rpath = null): bool {}
24 changes: 10 additions & 14 deletions ext/imap/php_imap_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 4991f82d78672ab23044864fa6dd75851952965c */
* Stub hash: e501d6869d721ad720a1a7c8b597b96e9591d5ed */

ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_open, 0, 0, 3)
ZEND_ARG_TYPE_INFO(0, mailbox, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, user, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, password, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, n_retries, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO(0, params, IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, params, IS_ARRAY, 0, "[]")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imap_reopen, 0, 2, _IS_BOOL, 0)
Expand Down Expand Up @@ -37,11 +37,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_imap_headerinfo, 0, 2, stdCl
ZEND_ARG_TYPE_INFO(0, msg_no, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, from_length, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, subject_length, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO(0, default_host, IS_STRING, 0)
ZEND_END_ARG_INFO()

#define arginfo_imap_header arginfo_imap_headerinfo

ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_imap_rfc822_parse_headers, 0, 1, stdClass, 0)
ZEND_ARG_TYPE_INFO(0, headers, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, default_host, IS_STRING, 0, "\"UNKNOWN\"")
Expand Down Expand Up @@ -173,8 +170,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imap_append, 0, 3, _IS_BOOL, 0)
ZEND_ARG_INFO(0, stream_id)
ZEND_ARG_TYPE_INFO(0, folder, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, internal_date, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, internal_date, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()

#define arginfo_imap_ping arginfo_imap_expunge
Expand Down Expand Up @@ -217,8 +214,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_imap_sort, 0, 3, MAY_BE_ARRAY|MA
ZEND_ARG_TYPE_INFO(0, criteria, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, reverse, IS_LONG, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_LONG, 0, "0")
ZEND_ARG_TYPE_INFO(0, search_criteria, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, charset, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, search_criteria, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, charset, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_imap_uid, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
Expand Down Expand Up @@ -342,10 +339,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imap_mail, 0, 3, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, to, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, subject, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, additional_headers, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, cc, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, bcc, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, rpath, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, additional_headers, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, cc, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, bcc, IS_STRING, 1, "null")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, rpath, IS_STRING, 1, "null")
ZEND_END_ARG_INFO()


Expand Down Expand Up @@ -440,7 +437,6 @@ static const zend_function_entry ext_functions[] = {
ZEND_FE(imap_num_recent, arginfo_imap_num_recent)
ZEND_FE(imap_headers, arginfo_imap_headers)
ZEND_FE(imap_headerinfo, arginfo_imap_headerinfo)
ZEND_FALIAS(imap_header, imap_headerinfo, arginfo_imap_header)
ZEND_FE(imap_rfc822_parse_headers, arginfo_imap_rfc822_parse_headers)
ZEND_FE(imap_rfc822_write_address, arginfo_imap_rfc822_write_address)
ZEND_FE(imap_rfc822_parse_adrlist, arginfo_imap_rfc822_parse_adrlist)
Expand Down