[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/ -> mail.php (source)

   1  <?php
   2  /**
   3   * Mail functions
   4   *
   5   * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
   6   * @author     Andreas Gohr <andi@splitbrain.org>
   7   */
   8  
   9  
  10  /**
  11   * Patterns for use in email detection and validation
  12   *
  13   * NOTE: there is an unquoted '/' in RFC2822_ATEXT, it must remain unquoted to be used in the parser
  14   * the pattern uses non-capturing groups as captured groups aren't allowed in the parser
  15   * select pattern delimiters with care!
  16   *
  17   * May not be completly RFC conform!
  18   * @link http://www.faqs.org/rfcs/rfc2822.html (paras 3.4.1 & 3.2.4)
  19   *
  20   * @author Chris Smith <chris@jalakai.co.uk>
  21   * Check if a given mail address is valid
  22   */
  23  if (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT',"0-9a-zA-Z!#$%&'*+/=?^_`{|}~-");
  24  if (!defined('PREG_PATTERN_VALID_EMAIL')) define(
  25      'PREG_PATTERN_VALID_EMAIL',
  26      '['.RFC2822_ATEXT.']+(?:\.['.RFC2822_ATEXT.']+)*@(?i:[0-9a-z][0-9a-z-]*\.)+(?i:[a-z]{2,63})'
  27  );
  28  
  29  /**
  30   * Prepare mailfrom replacement patterns
  31   *
  32   * Also prepares a mailfromnobody config that contains an autoconstructed address
  33   * if the mailfrom one is userdependent and this might not be wanted (subscriptions)
  34   *
  35   * @author Andreas Gohr <andi@splitbrain.org>
  36   */
  37  function mail_setup(){
  38      global $conf;
  39      global $USERINFO;
  40      /** @var Input $INPUT */
  41      global $INPUT;
  42  
  43      // auto constructed address
  44      $host = @parse_url(DOKU_URL,PHP_URL_HOST);
  45      if(!$host) $host = 'example.com';
  46      $noreply = 'noreply@'.$host;
  47  
  48      $replace = array();
  49      if(!empty($USERINFO['mail'])){
  50          $replace['@MAIL@'] = $USERINFO['mail'];
  51      }else{
  52          $replace['@MAIL@'] = $noreply;
  53      }
  54  
  55      // use 'noreply' if no user
  56      $replace['@USER@'] = $INPUT->server->str('REMOTE_USER', 'noreply', true);
  57  
  58      if(!empty($USERINFO['name'])){
  59          $replace['@NAME@'] = $USERINFO['name'];
  60      }else{
  61          $replace['@NAME@'] = '';
  62      }
  63  
  64      // apply replacements
  65      $from = str_replace(array_keys($replace),
  66                          array_values($replace),
  67                          $conf['mailfrom']);
  68  
  69      // any replacements done? set different mailfromnone
  70      if($from != $conf['mailfrom']){
  71          $conf['mailfromnobody'] = $noreply;
  72      }else{
  73          $conf['mailfromnobody'] = $from;
  74      }
  75      $conf['mailfrom'] = $from;
  76  }
  77  
  78  /**
  79   * Check if a given mail address is valid
  80   *
  81   * @param   string $email the address to check
  82   * @return  bool          true if address is valid
  83   */
  84  function mail_isvalid($email) {
  85      return EmailAddressValidator::checkEmailAddress($email, true);
  86  }
  87  
  88  /**
  89   * Quoted printable encoding
  90   *
  91   * @author umu <umuAThrz.tu-chemnitz.de>
  92   * @link   http://php.net/manual/en/function.imap-8bit.php#61216
  93   *
  94   * @param string $sText
  95   * @param int $maxlen
  96   * @param bool $bEmulate_imap_8bit
  97   *
  98   * @return string
  99   */
 100  function mail_quotedprintable_encode($sText,$maxlen=74,$bEmulate_imap_8bit=true) {
 101      // split text into lines
 102      $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText);
 103      $cnt = count($aLines);
 104  
 105      for ($i=0;$i<$cnt;$i++) {
 106          $sLine =& $aLines[$i];
 107          if (strlen($sLine)===0) continue; // do nothing, if empty
 108  
 109          $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
 110  
 111          // imap_8bit encodes x09 everywhere, not only at lineends,
 112          // for EBCDIC safeness encode !"#$@[\]^`{|}~,
 113          // for complete safeness encode every character :)
 114          if ($bEmulate_imap_8bit)
 115              $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/';
 116  
 117          $sLine = preg_replace_callback( $sRegExp, 'mail_quotedprintable_encode_callback', $sLine );
 118  
 119          // encode x09,x20 at lineends
 120          {
 121              $iLength = strlen($sLine);
 122              $iLastChar = ord($sLine[$iLength-1]);
 123  
 124              //              !!!!!!!!
 125              // imap_8_bit does not encode x20 at the very end of a text,
 126              // here is, where I don't agree with imap_8_bit,
 127              // please correct me, if I'm wrong,
 128              // or comment next line for RFC2045 conformance, if you like
 129              if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))){
 130                  if (($iLastChar==0x09)||($iLastChar==0x20)) {
 131                      $sLine[$iLength-1]='=';
 132                      $sLine .= ($iLastChar==0x09)?'09':'20';
 133                  }
 134              }
 135          }    // imap_8bit encodes x20 before chr(13), too
 136          // although IMHO not requested by RFC2045, why not do it safer :)
 137          // and why not encode any x20 around chr(10) or chr(13)
 138          if ($bEmulate_imap_8bit) {
 139              $sLine=str_replace(' =0D','=20=0D',$sLine);
 140              //$sLine=str_replace(' =0A','=20=0A',$sLine);
 141              //$sLine=str_replace('=0D ','=0D=20',$sLine);
 142              //$sLine=str_replace('=0A ','=0A=20',$sLine);
 143          }
 144  
 145          // finally split into softlines no longer than $maxlen chars,
 146          // for even more safeness one could encode x09,x20
 147          // at the very first character of the line
 148          // and after soft linebreaks, as well,
 149          // but this wouldn't be caught by such an easy RegExp
 150          if($maxlen){
 151              preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch );
 152              $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's
 153          }
 154      }
 155  
 156      // join lines into text
 157      return implode(MAILHEADER_EOL,$aLines);
 158  }
 159  
 160  function mail_quotedprintable_encode_callback($matches){
 161      return sprintf( "=%02X", ord ( $matches[0] ) ) ;
 162  }