Skip to content

Commit

Permalink
Fix #27 MimeMessage::__construct() throws TypeError with $mode=stream
Browse files Browse the repository at this point in the history
  • Loading branch information
remicollet committed Jun 12, 2023
1 parent 59e040b commit 80133bc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
17 changes: 9 additions & 8 deletions mailparse.c
Expand Up @@ -249,21 +249,22 @@ PHP_METHOD(mimemessage, __construct)

/* now check the args */

if (zend_string_equals_literal(mode, "new"))
if (zend_string_equals_literal(mode, "new")) {
RETURN_TRUE;
}

if (source == NULL)
if (source == NULL) {
RETURN_FALSE;
}

if (zend_string_equals_literal(mode, "var") && Z_TYPE_P(source) == IS_STRING) {
/* source is the actual message */
part->source.kind = mpSTRING;

ZVAL_DUP(&part->source.zval, source);
convert_to_string_ex(&part->source.zval);
}

if (zend_string_equals_literal(mode, "file")) {
} else if (zend_string_equals_literal(mode, "file")) {
/* source is the name of a file */
php_stream *srcstream;

Expand All @@ -276,23 +277,23 @@ PHP_METHOD(mimemessage, __construct)
}

php_stream_to_zval(srcstream, &part->source.zval);
}
if (zend_string_equals_literal(mode, "stream")) {

} else if (zend_string_equals_literal(mode, "stream")) {

part->source.kind = mpSTREAM;

ZVAL_DUP(&part->source.zval, source);
convert_to_string_ex(&part->source.zval);
}

/* parse the data from the source */
if (part->source.kind == mpSTRING) {
php_mimepart_parse(part, Z_STRVAL_P(&part->source.zval), Z_STRLEN_P(&part->source.zval));

} else if (part->source.kind == mpSTREAM) {
php_stream *srcstream;
char buf[1024];

php_stream_from_zval(srcstream, &part->source.zval);

php_stream_rewind(srcstream);
while(!php_stream_eof(srcstream)) {
size_t n = php_stream_read(srcstream, buf, sizeof(buf));
Expand Down
3 changes: 3 additions & 0 deletions package.xml
Expand Up @@ -48,6 +48,7 @@ It can deal with rfc822 and rfc2045 (MIME) compliant messages.
<license uri="http://www.php.net/license">PHP</license>
<notes>
- drop usage of removed mbfl APIs in PHP 8.3
- fix GH-27 MimeMessage::__construct() throws TypeError with $mode=stream
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -80,6 +81,8 @@ It can deal with rfc822 and rfc2045 (MIME) compliant messages.
<file name="010.phpt" role="test" />
<file name="011.phpt" role="test" />
<file name="012.phpt" role="test" />
<file name="012-stream.phpt" role="test" />
<file name="012-var.phpt" role="test" />
<file name="013.phpt" role="test" />
<file name="bug001.phpt" role="test" />
<file name="bug73110.phpt" role="test" />
Expand Down
23 changes: 23 additions & 0 deletions tests/012-stream.phpt
@@ -0,0 +1,23 @@
--TEST--
Check mailparse_mimemessage_extract_uue (stream mode)
--SKIPIF--
<?php
/* vim600: sw=4 ts=4 fdm=marker syn=php
*/
if (!extension_loaded("mailparse")) print "skip"; ?>
--FILE--
<?php
$fp = fopen(dirname(__FILE__) . "/testdata/oeuue", "rb");
$msg = new MimeMessage("stream", $fp);
var_dump( $msg->extract_uue(0, MAILPARSE_EXTRACT_RETURN));
fclose($fp);
?>
--EXPECT--
string(88) "FooBar - Baaaaa

Requirements:
o php with mailparse
o virus scanner (optional)


"
22 changes: 22 additions & 0 deletions tests/012-var.phpt
@@ -0,0 +1,22 @@
--TEST--
Check mailparse_mimemessage_extract_uue (var mode)
--SKIPIF--
<?php
/* vim600: sw=4 ts=4 fdm=marker syn=php
*/
if (!extension_loaded("mailparse")) print "skip"; ?>
--FILE--
<?php
$var = file_get_contents(dirname(__FILE__) . "/testdata/oeuue");
$msg = new MimeMessage("var", $var);
var_dump( $msg->extract_uue(0, MAILPARSE_EXTRACT_RETURN));
?>
--EXPECT--
string(88) "FooBar - Baaaaa

Requirements:
o php with mailparse
o virus scanner (optional)


"
2 changes: 1 addition & 1 deletion tests/012.phpt
@@ -1,5 +1,5 @@
--TEST--
Check mailparse_mimemessage_extract_uue
Check mailparse_mimemessage_extract_uue (file mode)
--SKIPIF--
<?php
/* vim600: sw=4 ts=4 fdm=marker syn=php
Expand Down
2 changes: 1 addition & 1 deletion tests/parse_test_messages.phpt
Expand Up @@ -4,7 +4,7 @@ Parse messages in testdata dir
<?php
/* vim600: sw=4 ts=4 fdm=marker syn=php
*/
if (!extension_loaded("mailparse") || !extension_loaded("zlib")) print "skip"; ?>
if (!extension_loaded("mailparse") || !extension_loaded("zlib")) print "skip missing mailparse or zlib"; ?>
--FILE--
<?php
error_reporting(E_ALL ^ E_NOTICE);
Expand Down

0 comments on commit 80133bc

Please sign in to comment.