PluginMailto   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 23
eloc 58
c 0
b 0
f 0
dl 0
loc 105
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F process() 0 90 23
1
<?php
2
/**
3
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Functions
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.2
13
 * @date      2017-01-06
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Plugin;
20
21
/**
22
 * Outputs a mailto link with optional spam-proof (okay probably not) encoding
23
 * <pre>
24
 *  * address : target email address
25
 *  * text : display text to show for the link, defaults to the address if not provided
26
 *  * subject : the email subject
27
 *  * encode : one of the available encoding (none, js, jscharcode or hex)
28
 *  * cc : address(es) to carbon copy, comma separated
29
 *  * bcc : address(es) to blind carbon copy, comma separated
30
 *  * newsgroups : newsgroup(s) to post to, comma separated
31
 *  * followupto : address(es) to follow up, comma separated
32
 *  * extra : additional attributes to add to the &lt;a&gt; tag
33
 * </pre>
34
 * This software is provided 'as-is', without any express or implied warranty.
35
 * In no event will the authors be held liable for any damages arising from the use of this software.
36
 */
37
class PluginMailto extends Plugin
38
{
39
    /**
40
     * @param      $address
41
     * @param null $text
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $text is correct as it would always require null to be passed?
Loading history...
42
     * @param null $subject
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $subject is correct as it would always require null to be passed?
Loading history...
43
     * @param null $encode
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $encode is correct as it would always require null to be passed?
Loading history...
44
     * @param null $cc
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $cc is correct as it would always require null to be passed?
Loading history...
45
     * @param null $bcc
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $bcc is correct as it would always require null to be passed?
Loading history...
46
     * @param null $newsgroups
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $newsgroups is correct as it would always require null to be passed?
Loading history...
47
     * @param null $followupto
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $followupto is correct as it would always require null to be passed?
Loading history...
48
     * @param null $extra
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $extra is correct as it would always require null to be passed?
Loading history...
49
     *
50
     * @return string
51
     */
52
    public function process($address, $text = null, $subject = null, $encode = null, $cc = null, $bcc = null, $newsgroups = null, $followupto = null, $extra = null)
53
    {
54
        if (empty($address)) {
55
            return '';
56
        }
57
        if (empty($text)) {
58
            $text = $address;
59
        }
60
61
        // build address string
62
        $address .= '?';
63
64
        if (!empty($subject)) {
65
            $address .= 'subject=' . rawurlencode($subject) . '&';
66
        }
67
        if (!empty($cc)) {
68
            $address .= 'cc=' . rawurlencode($cc) . '&';
69
        }
70
        if (!empty($bcc)) {
71
            $address .= 'bcc=' . rawurlencode($bcc) . '&';
72
        }
73
        if (!empty($newsgroups)) {
74
            $address .= 'newsgroups=' . rawurlencode($newsgroups) . '&';
75
        }
76
        if (!empty($followupto)) {
77
            $address .= 'followupto=' . rawurlencode($followupto) . '&';
78
        }
79
80
        $address = rtrim($address, '?&');
81
82
        // output
83
        switch ($encode) {
84
85
            case 'none':
86
            case null:
87
                return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
88
89
            case 'js':
90
            case 'javascript':
91
                $str = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
92
                $len = strlen($str);
93
94
                $out = '';
95
                for ($i = 0; $i < $len; ++ $i) {
96
                    $out .= '%' . bin2hex($str[$i]);
97
                }
98
99
                return '<script type="text/javascript">eval(unescape(\'' . $out . '\'));</script>';
100
101
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
102
            case 'javascript_charcode':
103
            case 'js_charcode':
104
            case 'jscharcode':
105
            case 'jschar':
106
                $str = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
107
                $len = strlen($str);
108
109
                $out = '<script type="text/javascript">' . "\n<!--\ndocument.write(Str.fromCharCode(";
110
                for ($i = 0; $i < $len; ++ $i) {
111
                    $out .= ord($str[$i]) . ',';
112
                }
113
114
                return rtrim($out, ',') . "));\n-->\n</script>\n";
115
116
                break;
117
118
            case 'hex':
119
                if (strpos($address, '?') !== false) {
120
                    $this->core->triggerError('Mailto: Hex encoding is not possible with extra attributes, use one of : <em>js, jscharcode or none</em>.', E_USER_WARNING);
121
                }
122
123
                $out = '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;';
124
                $len = strlen($address);
125
                for ($i = 0; $i < $len; ++ $i) {
126
                    if (preg_match('#\w#', $address[$i])) {
127
                        $out .= '%' . bin2hex($address[$i]);
128
                    } else {
129
                        $out .= $address[$i];
130
                    }
131
                }
132
                $out .= '" ' . $extra . '>';
133
                $len = strlen($text);
134
                for ($i = 0; $i < $len; ++ $i) {
135
                    $out .= '&#x' . bin2hex($text[$i]);
136
                }
137
138
                return $out . '</a>';
139
140
            default:
141
                $this->core->triggerError('Mailto: <em>encode</em> argument is invalid, it must be one of : <em>none (= no value), js, js_charcode or hex</em>', E_USER_WARNING);
142
        }
143
    }
144
}