[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionMailer/ -> AkActionMailerQuoting.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package AkelosFramework
  13   * @subpackage AkActionMailer
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2008, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  
  20  defined('AK_ACTION_MAILER_CHARS_NEEDING_QUOTING_REGEX') ? null :
  21  define('AK_ACTION_MAILER_CHARS_NEEDING_QUOTING_REGEX', "/[\\000-\\011\\013\\014\\016-\\037\\177-\\377]/");
  22  ak_define('ACTION_MAILER_EMULATE_IMAP_8_BIT', true);
  23  
  24  class AkActionMailerQuoting
  25  {
  26  
  27      /**
  28       * Convert the given text into quoted printable format, with an instruction
  29       * that the text be eventually interpreted in the given charset.
  30       */
  31      function quotedPrintable($text, $charset = AK_ACTION_MAILER_DEFAULT_CHARSET)
  32      {
  33          $text = str_replace(' ','_', preg_replace('/[^a-z ]/ie', 'AkActionMailerQuoting::quotedPrintableEncode("$0")', $text));
  34          return "=?$charset?Q?$text?=";
  35      }
  36  
  37      /**
  38       * Convert the given character to quoted printable format, taking into
  39       * account multi-byte characters
  40       */
  41      function quotedPrintableEncode($character, $emulate_imap_8bit = AK_ACTION_MAILER_EMULATE_IMAP_8_BIT)
  42      {
  43          $lines = preg_split("/(?:\r\n|\r|\n)/", $character);
  44          $search_pattern = $emulate_imap_8bit ? '/[^\x20\x21-\x3C\x3E-\x7E]/e' : '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
  45          foreach ((array)$lines as $k=>$line){
  46              if (empty($line)){
  47                  continue;
  48              }
  49  
  50              $line = preg_replace($search_pattern, 'sprintf( "=%02X", ord ( "$0" ) ) ;', $line );
  51              $length = strlen($line);
  52  
  53              $last_char = ord($line[$length-1]);
  54              if (!($emulate_imap_8bit && ($k==count($lines)-1)) && ($last_char==0x09) || ($last_char==0x20)) {
  55                  $line[$length-1] = '=';
  56                  $line .= ($last_char==0x09) ? '09' : '20';
  57              }
  58              if ($emulate_imap_8bit) {
  59                  $line = str_replace(' =0D', '=20=0D', $line);
  60              }
  61              $lines[$k] = $line;
  62          }
  63          return implode(AK_ACTION_MAILER_EOL,$lines);
  64      }
  65  
  66  
  67      /**
  68      * Quote the given text if it contains any "illegal" characters
  69      */
  70      function quoteIfNecessary($text, $charset = AK_ACTION_MAILER_DEFAULT_CHARSET)
  71      {
  72          return preg_match(AK_ACTION_MAILER_CHARS_NEEDING_QUOTING_REGEX,$text) ? AkActionMailerQuoting::quotedPrintable($text,$charset) : $text;
  73      }
  74  
  75      /**
  76      * Quote any of the given strings if they contain any "illegal" characters
  77      */
  78      function quoteAnyIfNecessary($strings = array(), $charset = AK_ACTION_MAILER_DEFAULT_CHARSET)
  79      {
  80          foreach ($strings as $k=>$v){
  81              $strings[$k] = AkActionMailerQuoting::quoteIfNecessary($charset, $v);
  82          }
  83          return $strings;
  84      }
  85  
  86      /**
  87       *  Quote the given address if it needs to be. The address may be a
  88       * regular email address, or it can be a phrase followed by an address in
  89       * brackets. The phrase is the only part that will be quoted, and only if
  90       * it needs to be. This allows extended characters to be used in the
  91       * "to", "from", "cc", and "bcc" headers.
  92       */
  93      function quoteAddressIfNecessary($address, $charset = AK_ACTION_MAILER_DEFAULT_CHARSET)
  94      {
  95          if(is_array($address)){
  96              return join(", ".AK_ACTION_MAILER_EOL."     ",AkActionMailerQuoting::quoteAnyAddressIfNecessary($address, $charset));
  97          }elseif (preg_match('/^(\S.*)\s+(<?('.AK_ACTION_MAILER_EMAIL_REGULAR_EXPRESSION.')>?)$/i', $address, $match)){
  98              $address = $match[3];
  99              $quoted = AkActionMailerQuoting::quoteIfNecessary(trim($match[1],'\'"'), $charset);
 100              $phrase = str_replace('"','\"', $quoted);
 101              if($phrase[0] != '='){
 102                  return "\"$phrase\" <$address>";
 103              }else{
 104                  return "$phrase <$address>";
 105              }
 106          }else{
 107              return $address;
 108          }
 109      }
 110  
 111      /**
 112       *  Quote any of the given addresses, if they need to be.
 113       */
 114      function quoteAnyAddressIfNecessary($address = array(), $charset = AK_ACTION_MAILER_DEFAULT_CHARSET)
 115      {
 116          foreach ($address as $k=>$v){
 117              $address[$k] = is_string($k) ?
 118              AkActionMailerQuoting::quoteAddressIfNecessary('"'.$k.'" <'.$v.'>', $charset) :
 119              AkActionMailerQuoting::quoteAddressIfNecessary($v, $charset);
 120          }
 121          return $address;
 122      }
 123  
 124      function chunkQuoted($quoted_string, $max_length = 74)
 125      {
 126          if(empty($max_length) || !is_string($quoted_string)){
 127              return $quoted_string;
 128          }
 129  
 130          $lines= preg_split("/(?:\r\n|\r|\n)/", $quoted_string);
 131          foreach ((array)$lines as $k=>$line){
 132              if (empty($line)){
 133                  continue;
 134              }
 135              preg_match_all( '/.{1,'.($max_length - 2).'}([^=]{0,2})?/', $line, $match );
 136              $line = implode('='.AK_ACTION_MAILER_EOL.' ', $match[0] );
 137  
 138              $lines[$k] = $line;
 139          }
 140          return implode(AK_ACTION_MAILER_EOL,$lines);
 141      }
 142  }
 143  
 144  ?>


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6