Skip to content

Commit

Permalink
Merge pull request #2504 from splitbrain/issue1569
Browse files Browse the repository at this point in the history
Auth/Mailer: properly handle usernames including a comma
  • Loading branch information
splitbrain committed May 19, 2019
2 parents cbdd2bc + 743792d commit 8902b37
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
23 changes: 23 additions & 0 deletions _test/tests/inc/mailer.test.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function cleanHeaders() {

}

/**
* @group mailer_class
*/
class mailer_test extends DokuWikiTest {


Expand Down Expand Up @@ -94,11 +97,21 @@ function test_addresses(){
$headers = $mail->prop('headers');
$this->assertEquals('Andreas Gohr <andi@splitbrain.org>', $headers['To']);

$mail->to('"Andreas Gohr" <andi@splitbrain.org>');
$mail->cleanHeaders();
$headers = $mail->prop('headers');
$this->assertEquals('"Andreas Gohr" <andi@splitbrain.org>', $headers['To']);

$mail->to('Andreas Gohr <andi@splitbrain.org> , foo <foo@example.com>');
$mail->cleanHeaders();
$headers = $mail->prop('headers');
$this->assertEquals('Andreas Gohr <andi@splitbrain.org>, foo <foo@example.com>', $headers['To']);

$mail->to('"Foo, Dr." <foo@example.com> , foo <foo@example.com>');
$mail->cleanHeaders();
$headers = $mail->prop('headers');
$this->assertEquals('=?UTF-8?B?IkZvbywgRHIuIg==?= <foo@example.com>, foo <foo@example.com>', $headers['To']);

$mail->to('Möp <moep@example.com> , foo <foo@example.com>');
$mail->cleanHeaders();
$headers = $mail->prop('headers');
Expand Down Expand Up @@ -334,5 +347,15 @@ function test_htmlmailsignaturecustom() {
$this->assertRegexp('/' . preg_quote($expected_mail_body, '/') . '/', $dump);

}

function test_getCleanName() {
$mail = new TestMailer();
$name = $mail->getCleanName('Foo Bar');
$this->assertEquals('Foo Bar', $name);
$name = $mail->getCleanName('Foo, Bar');
$this->assertEquals('"Foo, Bar"', $name);
$name = $mail->getCleanName('Foo" Bar');
$this->assertEquals('"Foo\" Bar"', $name);
}
}
//Setup VIM: ex: et ts=4 :
31 changes: 30 additions & 1 deletion inc/Mailer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,22 +321,51 @@ public function subject($subject) {
$this->headers['Subject'] = $subject;
}

/**
* Return a clean name which can be safely used in mail address
* fields. That means the name will be enclosed in '"' if it includes
* a '"' or a ','. Also a '"' will be escaped as '\"'.
*
* @param string $name the name to clean-up
* @see cleanAddress
*/
public function getCleanName($name) {
$name = trim($name, ' \t"');
$name = str_replace('"', '\"', $name, $count);
if ($count > 0 || strpos($name, ',') !== false) {
$name = '"'.$name.'"';
}
return $name;
}

/**
* Sets an email address header with correct encoding
*
* Unicode characters will be deaccented and encoded base64
* for headers. Addresses may not contain Non-ASCII data!
*
* If @$addresses is a string then it will be split into multiple
* addresses. Addresses must be separated by a comma. If the display
* name includes a comma then it MUST be properly enclosed by '"' to
* prevent spliting at the wrong point.
*
* Example:
* cc("föö <foo@bar.com>, me@somewhere.com","TBcc");
* to("foo, Dr." <foo@bar.com>, me@somewhere.com");
*
* @param string|string[] $addresses Multiple adresses separated by commas or as array
* @return false|string the prepared header (can contain multiple lines)
*/
public function cleanAddress($addresses) {
$headers = '';
if(!is_array($addresses)){
$addresses = explode(',', $addresses);
$count = preg_match_all('/\s*(?:("[^"]*"[^,]+),*)|([^,]+)\s*,*/', $addresses, $matches, PREG_SET_ORDER);
$addresses = array();
if ($count !== false && is_array($matches)) {
foreach ($matches as $match) {
array_push($addresses, $match[0]);
}
}
}

foreach($addresses as $part) {
Expand Down
2 changes: 1 addition & 1 deletion inc/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ function auth_sendPassword($user, $password) {
);

$mail = new Mailer();
$mail->to($userinfo['name'].' <'.$userinfo['mail'].'>');
$mail->to($mail->getCleanName($userinfo['name']).' <'.$userinfo['mail'].'>');
$mail->subject($lang['regpwmail']);
$mail->setBody($text, $trep);
return $mail->send();
Expand Down

0 comments on commit 8902b37

Please sign in to comment.