xref: /dokuwiki/inc/mail.php (revision d868eb89f182718a31113373a6272670bd7f8012)
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 */
23if (!defined('RFC2822_ATEXT')) define('RFC2822_ATEXT', "0-9a-zA-Z!#$%&'*+/=?^_`{|}~-");
24if (!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 */
37function mail_setup()
38{
39    global $conf;
40    global $USERINFO;
41    /** @var Input $INPUT */
42    global $INPUT;
43
44    // auto constructed address
45    $host = @parse_url(DOKU_URL, PHP_URL_HOST);
46    if(!$host) $host = 'example.com';
47    $noreply = 'noreply@'.$host;
48
49    $replace = [];
50    if(!empty($USERINFO['mail'])){
51        $replace['@MAIL@'] = $USERINFO['mail'];
52    }else{
53        $replace['@MAIL@'] = $noreply;
54    }
55
56    // use 'noreply' if no user
57    $replace['@USER@'] = $INPUT->server->str('REMOTE_USER', 'noreply', true);
58
59    if(!empty($USERINFO['name'])){
60        $replace['@NAME@'] = $USERINFO['name'];
61    }else{
62        $replace['@NAME@'] = '';
63    }
64
65    // apply replacements
66    $from = str_replace(array_keys($replace),
67                        array_values($replace),
68                        $conf['mailfrom']);
69
70    // any replacements done? set different mailfromnone
71    if($from != $conf['mailfrom']){
72        $conf['mailfromnobody'] = $noreply;
73    }else{
74        $conf['mailfromnobody'] = $from;
75    }
76    $conf['mailfrom'] = $from;
77}
78
79/**
80 * Check if a given mail address is valid
81 *
82 * @param   string $email the address to check
83 * @return  bool          true if address is valid
84 */
85function mail_isvalid($email)
86{
87    return EmailAddressValidator::checkEmailAddress($email, true);
88}
89
90/**
91 * Quoted printable encoding
92 *
93 * @author umu <umuAThrz.tu-chemnitz.de>
94 * @link   http://php.net/manual/en/function.imap-8bit.php#61216
95 *
96 * @param string $sText
97 * @param int $maxlen
98 * @param bool $bEmulate_imap_8bit
99 *
100 * @return string
101 */
102function mail_quotedprintable_encode($sText, $maxlen = 74, $bEmulate_imap_8bit = true)
103{
104    // split text into lines
105    $aLines= preg_split("/(?:\r\n|\r|\n)/", $sText);
106    $cnt = count($aLines);
107
108    for ($i=0;$i<$cnt;$i++) {
109        $sLine =& $aLines[$i];
110        if ($sLine === '') continue; // do nothing, if empty
111
112        $sRegExp = '/[^\x09\x20\x21-\x3C\x3E-\x7E]/e';
113
114        // imap_8bit encodes x09 everywhere, not only at lineends,
115        // for EBCDIC safeness encode !"#$@[\]^`{|}~,
116        // for complete safeness encode every character :)
117        if ($bEmulate_imap_8bit)
118            $sRegExp = '/[^\x20\x21-\x3C\x3E-\x7E]/';
119
120        $sLine = preg_replace_callback( $sRegExp, 'mail_quotedprintable_encode_callback', $sLine );
121
122        // encode x09,x20 at lineends
123        {
124            $iLength = strlen($sLine);
125            $iLastChar = ord($sLine[$iLength-1]);
126
127            //              !!!!!!!!
128            // imap_8_bit does not encode x20 at the very end of a text,
129            // here is, where I don't agree with imap_8_bit,
130            // please correct me, if I'm wrong,
131            // or comment next line for RFC2045 conformance, if you like
132            if (!($bEmulate_imap_8bit && ($i==count($aLines)-1))){
133            if (($iLastChar==0x09)||($iLastChar==0x20)) {
134                $sLine[$iLength-1]='=';
135                $sLine .= ($iLastChar==0x09)?'09':'20';
136                }
137            }
138        }    // imap_8bit encodes x20 before chr(13), too
139        // although IMHO not requested by RFC2045, why not do it safer :)
140        // and why not encode any x20 around chr(10) or chr(13)
141        if ($bEmulate_imap_8bit) {
142            $sLine=str_replace(' =0D', '=20=0D', $sLine);
143            //$sLine=str_replace(' =0A','=20=0A',$sLine);
144            //$sLine=str_replace('=0D ','=0D=20',$sLine);
145            //$sLine=str_replace('=0A ','=0A=20',$sLine);
146        }
147
148        // finally split into softlines no longer than $maxlen chars,
149        // for even more safeness one could encode x09,x20
150        // at the very first character of the line
151        // and after soft linebreaks, as well,
152        // but this wouldn't be caught by such an easy RegExp
153        if($maxlen){
154            preg_match_all( '/.{1,'.($maxlen - 2).'}([^=]{0,2})?/', $sLine, $aMatch );
155            $sLine = implode( '=' . MAILHEADER_EOL, $aMatch[0] ); // add soft crlf's
156        }
157    }
158
159    // join lines into text
160    return implode(MAILHEADER_EOL, $aLines);
161}
162
163function mail_quotedprintable_encode_callback($matches)
164{
165    return sprintf( "=%02X", ord ( $matches[0] ) ) ;
166}
167