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