Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsFunction
7
 */
8
/**
9
 * Smarty {mailto} function plugin
10
 * Type:     function
11
 * Name:     mailto
12
 * Date:     May 21, 2002
13
 * Purpose:  automate mailto address link creation, and optionally encode them.
14
 * Params:
15
 *
16
 * - address    - (required) - e-mail address
17
 * - text       - (optional) - text to display, default is address
18
 * - encode     - (optional) - can be one of:
19
 *                             * none : no encoding (default)
20
 *                             * javascript : encode with javascript
21
 *                             * javascript_charcode : encode with javascript charcode
22
 *                             * hex : encode with hexadecimal (no javascript)
23
 * - cc         - (optional) - address(es) to carbon copy
24
 * - bcc        - (optional) - address(es) to blind carbon copy
25
 * - subject    - (optional) - e-mail subject
26
 * - newsgroups - (optional) - newsgroup(s) to post to
27
 * - followupto - (optional) - address(es) to follow up to
28
 * - extra      - (optional) - extra tags for the href link
29
 *
30
 * Examples:
31
 *
32
 * {mailto address="me@domain.com"}
33
 * {mailto address="me@domain.com" encode="javascript"}
34
 * {mailto address="me@domain.com" encode="hex"}
35
 * {mailto address="me@domain.com" subject="Hello to you!"}
36
 * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
37
 * {mailto address="me@domain.com" extra='class="mailto"'}
38
 *
39
 * @link    https://www.smarty.net/manual/en/language.function.mailto.php {mailto}
40
 *           (Smarty online manual)
41
 * @version 1.2
42
 * @author  Monte Ohrt <monte at ohrt dot com>
43
 * @author  credits to Jason Sweat (added cc, bcc and subject functionality)
44
 *
45
 * @param array $params parameters
46
 *
47
 * @return string
48
 */
49
function smarty_function_mailto($params)
50
{
51
    static $_allowed_encoding = [
52
        'javascript' => true,
53
        'javascript_charcode' => true,
54
        'hex' => true,
55
        'none' => true
56
    ];
57
 
58
    $extra = '';
59
    if (empty($params[ 'address' ])) {
60
        trigger_error("mailto: missing 'address' parameter", E_USER_WARNING);
61
        return;
62
    } else {
63
        $address = $params[ 'address' ];
64
    }
65
 
66
    $text = $address;
67
 
68
    // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
69
    // so, don't encode it.
70
    $mail_parms = [];
71
    foreach ($params as $var => $value) {
72
        switch ($var) {
73
            case 'cc':
74
            case 'bcc':
75
            case 'followupto':
76
                if (!empty($value)) {
77
                    $mail_parms[] = $var . '=' . str_replace(['%40', '%2C'], ['@', ','], rawurlencode($value));
78
                }
79
                break;
80
            case 'subject':
81
            case 'newsgroups':
82
                $mail_parms[] = $var . '=' . rawurlencode($value);
83
                break;
84
            case 'extra':
85
            case 'text':
86
                $$var = $value;
87
            // no break
88
            default:
89
        }
90
    }
91
 
92
    if ($mail_parms) {
93
        $address .= '?' . join('&', $mail_parms);
94
    }
95
    $encode = (empty($params[ 'encode' ])) ? 'none' : $params[ 'encode' ];
96
    if (!isset($_allowed_encoding[ $encode ])) {
97
        trigger_error(
98
            "mailto: 'encode' parameter must be none, javascript, javascript_charcode or hex",
99
            E_USER_WARNING
100
        );
101
        return;
102
    }
103
 
104
    $string = '<a href="mailto:' . htmlspecialchars($address, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, Smarty::$_CHARSET) .
105
        '" ' . $extra . '>' . htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, Smarty::$_CHARSET) . '</a>';
106
 
107
    if ($encode === 'javascript') {
108
        $js_encode = '';
109
        for ($x = 0, $_length = strlen($string); $x < $_length; $x++) {
110
            $js_encode .= '%' . bin2hex($string[ $x ]);
111
        }
112
        return '<script type="text/javascript">document.write(unescape(\'' . $js_encode . '\'))</script>';
113
    } elseif ($encode === 'javascript_charcode') {
114
        for ($x = 0, $_length = strlen($string); $x < $_length; $x++) {
115
            $ord[] = ord($string[ $x ]);
116
        }
117
        return '<script type="text/javascript">document.write(String.fromCharCode(' . implode(',', $ord) . '))</script>';
118
    } elseif ($encode === 'hex') {
119
        preg_match('!^(.*)(\?.*)$!', $address, $match);
120
        if (!empty($match[ 2 ])) {
121
            trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.", E_USER_WARNING);
122
            return;
123
        }
124
        $address_encode = '';
125
        for ($x = 0, $_length = strlen($address); $x < $_length; $x++) {
126
            if (preg_match('!\w!' . Smarty::$_UTF8_MODIFIER, $address[ $x ])) {
127
                $address_encode .= '%' . bin2hex($address[ $x ]);
128
            } else {
129
                $address_encode .= $address[ $x ];
130
            }
131
        }
132
        $text_encode = '';
133
        for ($x = 0, $_length = strlen($text); $x < $_length; $x++) {
134
            $text_encode .= '&#x' . bin2hex($text[ $x ]) . ';';
135
        }
136
        $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
137
        return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
138
    } else {
139
        // no encoding
140
        return $string;
141
    }
142
}