don't use curly braces for string index access
[php-utility-classes.git] / classes / email.php-class
index ff03a17363534d9083423d5e7538b9e0427f412e..3bbb5e940c3a60c35abb4602dc37d5fc4a840add 100644 (file)
@@ -1,23 +1,7 @@
 <?php
-/* ***** BEGIN LICENSE BLOCK *****
- *
- * The contents of this file are subject to Austrian copyright reegulations
- * ("Urheberrecht"); you may not use this file except in compliance with
- * those laws.
- * This contents and any derived work, if it gets distributed in any way,
- * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
- * either express or implied.
- *
- * The Original Code is KaiRo's E-Mail module.
- *
- * The Initial Developer of the Original Code is
- * KaiRo - Robert Kaiser.
- * Portions created by the Initial Developer are Copyright (C) 2003-2006
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s): Robert Kaiser <kairo@kairo.at>
- *
- * ***** END LICENSE BLOCK ***** */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 class email {
   // email PHP class
@@ -268,7 +252,7 @@ class email {
     if (count($this->attachments)) {
       // create random boundary, 20 chars, always beginning with KaiRo ;-)
       $boundary = 'KaiRo';
-      for ($i = 1; $i <= 15; $i++)     {
+      for ($i = 1; $i <= 15; $i++) {
         $r = rand(0, 61);
         if ($r < 10) { $boundary .= chr($r + 48); }
         elseif ($r < 36) { $boundary .= chr($r + 55); }
@@ -331,7 +315,12 @@ class email {
   }
 
   private function mimeencode($fieldtext, $stringescape = false) {
-    $mText = imap_8bit($fieldtext);
+    if (function_exists('imap_8bit')) {
+      $mText = imap_8bit($fieldtext);
+    }
+    else {
+      $mText = quoted_printable_encode($fieldtext);
+    }
     $is_qpformat = ($mText != $fieldtext);
     if ($stringescape && preg_match('/[^\w !#$%&\'*+\/=?^`{|}~-]/', $mText)) {
       // if needed, make this a quoted-string instead of an atom (to speak in RFC2822 language)
@@ -343,5 +332,81 @@ class email {
     }
   return $mText;
   }
+
+  private function quoted_printable_encode($sText, $bEmulate_imap_8bit=true) {
+    /* by ...deed.ztinmehc-ut.zrh@umuumu@hrz.tu-chemnitz.deed...
+       from https://secure.php.net/manual/en/function.imap-8bit.php#61216
+
+        I use the following function instead of imap_8bit
+        when using PHP without the IMAP module,
+        which is based on code found in
+        http://www.php.net/quoted_printable_decode,
+        and giving (supposedly) exactly the same results as imap_8bit,
+        (tested on thousands of random strings containing lots
+        of spaces, tabs, crlf, lfcr, lf, cr and so on,
+        no counterexample found SO FAR:)
+
+        AND you can force a trailing space to be encoded,
+        as opposed to what imap_8bit does,
+        which I consider is a violation of RFC2045,
+        (see http://bugs.php.net/bug.php?id=35290)
+        by commenting that one central line.
+    */
+    // split text into lines
+    $aLines=explode(chr(13).chr(10),$sText);
+
+    for ($i=0;$i<count($aLines);$i++) {
+      $sLine =& $aLines[$i];
+      if (strlen($sLine)===0) continue; // do nothing, if empty
+
+      $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
+
+      // imap_8bit encodes x09 everywhere, not only at lineends,
+      // for EBCDIC safeness encode !"#$@[\]^`{|}~,
+      // for complete safeness encode every character :)
+      if ($bEmulate_imap_8bit)
+        $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/e';
+
+      $sReplmt = 'sprintf( "=%02X", ord ( "$0" ) ) ;';
+      $sLine = preg_replace( $sRegExp, $sReplmt, $sLine );
+
+      // encode x09,x20 at lineends
+      {
+        $iLength = strlen($sLine);
+        $iLastChar = ord($sLine[$iLength-1]);
+
+        //              !!!!!!!!
+        // imap_8_bit does not encode x20 at the very end of a text,
+        // here is, where I don't agree with imap_8_bit,
+        // please correct me, if I'm wrong,
+        // or comment next line for RFC2045 conformance, if you like
+        if (!($bEmulate_imap_8bit && ($i==count($aLines)-1)))
+
+        if (($iLastChar==0x09)||($iLastChar==0x20)) {
+          $sLine[$iLength-1]='=';
+          $sLine .= ($iLastChar==0x09)?'09':'20';
+        }
+      }    // imap_8bit encodes x20 before chr(13), too
+      // although IMHO not requested by RFC2045, why not do it safer :)
+      // and why not encode any x20 around chr(10) or chr(13)
+      if ($bEmulate_imap_8bit) {
+        $sLine=str_replace(' =0D','=20=0D',$sLine);
+        //$sLine=str_replace(' =0A','=20=0A',$sLine);
+        //$sLine=str_replace('=0D ','=0D=20',$sLine);
+        //$sLine=str_replace('=0A ','=0A=20',$sLine);
+      }
+
+      // finally split into softlines no longer than 76 chars,
+      // for even more safeness one could encode x09,x20
+      // at the very first character of the line
+      // and after soft linebreaks, as well,
+      // but this wouldn't be caught by such an easy RegExp
+      preg_match_all( '/.{1,73}([^=]{0,2})?/', $sLine, $aMatch );
+      $sLine = implode( '=' . chr(13).chr(10), $aMatch[0] ); // add soft crlf's
+    }
+
+    // join lines into text
+    return implode(chr(13).chr(10),$aLines);
+  }
 }
 ?>