zend_string_release_ex(tmp_headers, 0);
break;
case IS_ARRAY:
- str_headers = php_mail_build_headers(headers);
+ str_headers = php_mail_build_headers(Z_ARRVAL_P(headers));
break;
default:
php_error_docref(NULL, E_WARNING, "headers parameter must be string or array");
function ucwords(string $str, string $delimiters = " \t\r\n\f\v"): string {}
-/**
- * @param string|array $from
- */
-function strtr(string $str, $from, string $to = UNKNOWN): string {}
+function strtr(string $str, string|array $from, string $to = UNKNOWN): string {}
function strrev(string $str): string {}
/* mail.c */
-/** @param string|array $additional_headers */
-function mail(string $to, string $subject, string $message, $additional_headers = UNKNOWN, string $additional_parameters = ""): bool {}
+function mail(string $to, string $subject, string $message, string|array $additional_headers = UNKNOWN, string $additional_parameters = ""): bool {}
/* math.c */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_strtr, 0, 2, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
- ZEND_ARG_INFO(0, from)
+ ZEND_ARG_TYPE_MASK(0, from, MAY_BE_STRING|MAY_BE_ARRAY)
ZEND_ARG_TYPE_INFO(0, to, IS_STRING, 0)
ZEND_END_ARG_INFO()
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_INFO(0, additional_headers)
+ ZEND_ARG_TYPE_MASK(0, additional_headers, MAY_BE_STRING|MAY_BE_ARRAY)
ZEND_ARG_TYPE_INFO(0, additional_parameters, IS_STRING, 0)
ZEND_END_ARG_INFO()
}
-PHPAPI zend_string *php_mail_build_headers(zval *headers)
+PHPAPI zend_string *php_mail_build_headers(HashTable *headers)
{
zend_ulong idx;
zend_string *key;
zval *val;
smart_str s = {0};
- ZEND_ASSERT(Z_TYPE_P(headers) == IS_ARRAY);
-
- ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(headers), idx, key, val) {
+ ZEND_HASH_FOREACH_KEY_VAL(headers, idx, key, val) {
if (!key) {
php_error_docref(NULL, E_WARNING, "Found numeric header (" ZEND_LONG_FMT ")", idx);
continue;
{
char *to=NULL, *message=NULL;
char *subject=NULL;
- zend_string *extra_cmd=NULL, *str_headers=NULL, *tmp_headers;
- zval *headers = NULL;
+ zend_string *extra_cmd=NULL;
+ zend_string *headers_str = NULL;
+ HashTable *headers_ht = NULL;
size_t to_len, message_len;
size_t subject_len, i;
char *force_extra_parameters = INI_STR("mail.force_extra_parameters");
Z_PARAM_STRING(subject, subject_len)
Z_PARAM_STRING(message, message_len)
Z_PARAM_OPTIONAL
- Z_PARAM_ZVAL(headers)
+ Z_PARAM_STR_OR_ARRAY_HT(headers_str, headers_ht)
Z_PARAM_STR(extra_cmd)
ZEND_PARSE_PARAMETERS_END();
MAIL_ASCIIZ_CHECK(to, to_len);
MAIL_ASCIIZ_CHECK(subject, subject_len);
MAIL_ASCIIZ_CHECK(message, message_len);
- if (headers) {
- switch(Z_TYPE_P(headers)) {
- case IS_STRING:
- tmp_headers = zend_string_init(Z_STRVAL_P(headers), Z_STRLEN_P(headers), 0);
- MAIL_ASCIIZ_CHECK(ZSTR_VAL(tmp_headers), ZSTR_LEN(tmp_headers));
- str_headers = php_trim(tmp_headers, NULL, 0, 2);
- zend_string_release_ex(tmp_headers, 0);
- break;
- case IS_ARRAY:
- str_headers = php_mail_build_headers(headers);
- break;
- default:
- php_error_docref(NULL, E_WARNING, "headers parameter must be string or array");
- RETURN_FALSE;
- }
+ if (headers_str) {
+ MAIL_ASCIIZ_CHECK(ZSTR_VAL(headers_str), ZSTR_LEN(headers_str));
+ headers_str = php_trim(headers_str, NULL, 0, 2);
+ } else if (headers_ht) {
+ headers_str = php_mail_build_headers(headers_ht);
}
+
if (extra_cmd) {
MAIL_ASCIIZ_CHECK(ZSTR_VAL(extra_cmd), ZSTR_LEN(extra_cmd));
}
extra_cmd = php_escape_shell_cmd(ZSTR_VAL(extra_cmd));
}
- if (php_mail(to_r, subject_r, message, str_headers ? ZSTR_VAL(str_headers) : NULL, extra_cmd ? ZSTR_VAL(extra_cmd) : NULL)) {
+ if (php_mail(to_r, subject_r, message, headers_str ? ZSTR_VAL(headers_str) : NULL, extra_cmd ? ZSTR_VAL(extra_cmd) : NULL)) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
}
- if (str_headers) {
- zend_string_release_ex(str_headers, 0);
+ if (headers_str) {
+ zend_string_release_ex(headers_str, 0);
}
if (extra_cmd) {
PHP_MINFO_FUNCTION(mail);
-PHPAPI zend_string *php_mail_build_headers(zval *headers);
+PHPAPI zend_string *php_mail_build_headers(HashTable *headers);
PHPAPI extern int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd);
#define PHP_MAIL_BUILD_HEADER_CHECK(target, s, key, val) \
Translates characters in str using given translation tables */
PHP_FUNCTION(strtr)
{
- zval *from;
- zend_string *str;
+ zend_string *str, *from_str = NULL;
+ HashTable *from_ht = NULL;
char *to = NULL;
size_t to_len = 0;
int ac = ZEND_NUM_ARGS();
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(str)
- Z_PARAM_ZVAL(from)
+ Z_PARAM_STR_OR_ARRAY_HT(from_str, from_ht)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(to, to_len)
ZEND_PARSE_PARAMETERS_END();
- if (ac == 2 && Z_TYPE_P(from) != IS_ARRAY) {
- zend_type_error("The second argument is not an array");
+ if (ac == 2 && from_ht == NULL) {
+ zend_type_error("If two arguments are passed, the second argument must be an array");
+ return;
+ } else if (ac != 2 && from_str == NULL) {
+ zend_type_error("If three arguments are passed, the second argument must be a string");
return;
}
}
if (ac == 2) {
- HashTable *pats = Z_ARRVAL_P(from);
-
- if (zend_hash_num_elements(pats) < 1) {
+ if (zend_hash_num_elements(from_ht) < 1) {
RETURN_STR_COPY(str);
- } else if (zend_hash_num_elements(pats) == 1) {
+ } else if (zend_hash_num_elements(from_ht) == 1) {
zend_long num_key;
zend_string *str_key, *tmp_str, *replace, *tmp_replace;
zval *entry;
- ZEND_HASH_FOREACH_KEY_VAL(pats, num_key, str_key, entry) {
+ ZEND_HASH_FOREACH_KEY_VAL(from_ht, num_key, str_key, entry) {
tmp_str = NULL;
if (UNEXPECTED(!str_key)) {
str_key = tmp_str = zend_long_to_str(num_key);
return;
} ZEND_HASH_FOREACH_END();
} else {
- php_strtr_array(return_value, str, pats);
+ php_strtr_array(return_value, str, from_ht);
}
} else {
- if (!try_convert_to_string(from)) {
- return;
- }
-
RETURN_STR(php_strtr_ex(str,
- Z_STRVAL_P(from),
+ ZSTR_VAL(from_str),
to,
- MIN(Z_STRLEN_P(from), to_len)));
+ MIN(ZSTR_LEN(from_str), to_len)));
}
}
/* }}} */
for($index = 0; $index < count($from_arr); $index++) {
echo "-- Iteration $count --\n";
$from = $from_arr[$index];
- var_dump( strtr($str, $from, $to) );
- $count ++;
+ try {
+ var_dump(strtr($str, $from, $to));
+ } catch (TypeError $exception) {
+ echo $exception->getMessage() . "\n";
+ }
+ $count++;
}
fclose($file_handle); //closing the file handle
-- Iteration 6 --
string(6) "tm0atm"
-- Iteration 7 --
-
-Warning: Array to string conversion in %s on line %d
-string(6) "0120tm"
+If three arguments are passed, the second argument must be a string
-- Iteration 8 --
-
-Warning: Array to string conversion in %s on line %d
-string(6) "0120tm"
+If three arguments are passed, the second argument must be a string
-- Iteration 9 --
-
-Warning: Array to string conversion in %s on line %d
-string(6) "0120tm"
+If three arguments are passed, the second argument must be a string
-- Iteration 10 --
string(6) "0a2atm"
-- Iteration 11 --
-- Iteration 16 --
string(6) "012ttm"
-- Iteration 17 --
-string(6) "012atm"
+strtr() expects parameter 2 to be string or array, resource given
-- Iteration 18 --
string(6) "012atm"
-- Iteration 19 --
echo "\n-- Iteration $count --\n";
$replace_pairs = $replace_pairs_arr[$index];
try {
- var_dump( strtr($str, $replace_pairs) );
- } catch (\TypeError $e) {
+ var_dump(strtr($str, $replace_pairs));
+ } catch (TypeError $e) {
echo $e->getMessage() . "\n";
}
*** Testing strtr() function: with unexpected inputs for 'replace_pairs' ***
-- Iteration 1 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 2 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 3 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 4 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 5 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 6 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 7 --
string(6) "012atm"
string(6) "122atm"
-- Iteration 10 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 11 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 12 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 13 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 14 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 15 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 16 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 17 --
-The second argument is not an array
+strtr() expects parameter 2 to be string or array, resource given
-- Iteration 18 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
-- Iteration 19 --
-The second argument is not an array
+If two arguments are passed, the second argument must be an array
*** Done ***