Skip to content

Instantly share code, notes, and snippets.

@andrerom
Created October 2, 2013 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrerom/6792186 to your computer and use it in GitHub Desktop.
Save andrerom/6792186 to your computer and use it in GitHub Desktop.
Backport of Zeta patch: "Fixed Header Mime encoding failing using multi-byte characters" in issue EZP-21567 for eZ Publish 5.0 and lower Gist based on: https://github.com/zetacomponents/Mail/pull/8 Note: 5.0 needs to run composer update and apply this patch for mixed + pure legacy setup.
diff --git a/ezpublish_legacy/lib/ezc/Mail/src/interfaces/part.php b/ezpublish_legacy/lib/ezc/Mail/src/interfaces/part.php
index 6adcabf..8bdceb6 100644
--- a/src/interfaces/part.php
+++ b/src/interfaces/part.php
@@ -387,18 +387,9 @@ public function generateHeaders()
// break intentionally missing
default:
- $preferences = array(
- 'input-charset' => $charset,
- 'output-charset' => $charset,
- 'line-length' => ezcMailHeaderFolder::getLimit(),
- 'scheme' => 'Q',
- 'line-break-chars' => ezcMailTools::lineBreak()
- );
- $value = iconv_mime_encode( 'dummy', $value, $preferences );
- $value = substr( $value, 7 ); // "dummy: " + 1
-
// just to keep compatibility with code which might read
// the headers after generateHeaders() has been called
+ $value = ezcMailTools::mimeHeaderEncode( $value, $charset );
$this->setHeader( $header, $value, $charset );
break;
}
diff --git a/ezpublish_legacy/lib/ezc/Mail/src/tools.php b/ezpublish_legacy/lib/ezc/Mail/src/tools.php
index e99fddd..bf3ffa5 100644
--- a/src/tools.php
+++ b/src/tools.php
@@ -138,15 +138,7 @@ public static function composeEmailAddress( ezcMailAddress $item )
// break intentionally missing
default:
- $preferences = array(
- 'input-charset' => $item->charset,
- 'output-charset' => $item->charset,
- 'scheme' => 'Q',
- 'line-break-chars' => ezcMailTools::lineBreak()
- );
- $name = iconv_mime_encode( 'dummy', $name, $preferences );
- $name = substr( $name, 7 ); // "dummy: " + 1
- $text = $name . ' <' . $item->email . '>';
+ $text = self::mimeHeaderEncode( $name, $item->charset ) . ' <' . $item->email . '>';
break;
}
}
@@ -794,5 +786,40 @@ static function replaceContentIdRefs( $htmlText, $contentIdArray )
}
return $htmlText;
}
+
+ /**
+ * Encodes mime header value.
+ *
+ * @param string $value
+ * @param string $charset
+ *
+ * @return string
+ */
+ static public function mimeHeaderEncode( $value, $charset )
+ {
+ $preferences = array(
+ 'input-charset' => $charset,
+ 'output-charset' => $charset,
+ 'line-length' => ezcMailHeaderFolder::getLimit(),
+ 'scheme' => 'Q',
+ 'line-break-chars' => self::lineBreak()
+ );
+ // Mime encoding can fail with iconv when using multi-byte strings, generating a notice.
+ // See https://bugs.php.net/bug.php?id=53891
+ // I know, using the silent operator is usually evil, but we have no choice here...
+ $tmpValue = @iconv_mime_encode( 'dummy', $value, $preferences );
+ if ( $tmpValue !== false )
+ {
+ $value = substr( $tmpValue, 7 ); // "dummy: " + 1
+ unset( $tmpValue );
+ }
+ // Try to use mbstring extension if present.
+ else if ( extension_loaded( 'mbstring' ) )
+ {
+ $value = mb_encode_mimeheader( $value, $charset, 'Q', self::lineBreak() );
+ }
+
+ return $value;
+ }
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment