Completed
Push — fixDeprecatedCalls ( 656246 )
by Andreas
05:29 queued 03:04
created

mail.php ➔ mail_encode_address()   C

Complexity

Conditions 15
Paths 147

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
nc 147
nop 3
dl 0
loc 72
rs 5.525
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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