Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 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    http://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
        array('javascript' => true, 'javascript_charcode' => true, 'hex' => true, 'none' => true);
53
    $extra = '';
54
    if (empty($params[ 'address' ])) {
55
        trigger_error("mailto: missing 'address' parameter", E_USER_WARNING);
56
        return;
57
    } else {
58
        $address = $params[ 'address' ];
59
    }
60
    $text = $address;
61
    // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
62
    // so, don't encode it.
63
    $search = array('%40', '%2C');
64
    $replace = array('@', ',');
65
    $mail_parms = array();
66
    foreach ($params as $var => $value) {
67
        switch ($var) {
68
            case 'cc':
69
            case 'bcc':
70
            case 'followupto':
71
                if (!empty($value)) {
72
                    $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value));
73
                }
74
                break;
75
            case 'subject':
76
            case 'newsgroups':
77
                $mail_parms[] = $var . '=' . rawurlencode($value);
78
                break;
79
            case 'extra':
80
            case 'text':
81
                $$var = $value;
82
            // no break
83
            default:
84
        }
85
    }
86
    if ($mail_parms) {
87
        $address .= '?' . join('&', $mail_parms);
88
    }
89
    $encode = (empty($params[ 'encode' ])) ? 'none' : $params[ 'encode' ];
90
    if (!isset($_allowed_encoding[ $encode ])) {
91
        trigger_error(
92
            "mailto: 'encode' parameter must be none, javascript, javascript_charcode or hex",
93
            E_USER_WARNING
94
        );
95
        return;
96
    }
97
    // FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
98
    if ($encode === 'javascript') {
99
        $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
100
        $js_encode = '';
101
        for ($x = 0, $_length = strlen($string); $x < $_length; $x++) {
102
            $js_encode .= '%' . bin2hex($string[ $x ]);
103
        }
104
        return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
105
    } elseif ($encode === 'javascript_charcode') {
106
        $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
107
        for ($x = 0, $y = strlen($string); $x < $y; $x++) {
108
            $ord[] = ord($string[ $x ]);
109
        }
110
        $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n" . "{document.write(String.fromCharCode(" .
111
                implode(',', $ord) . "))" . "}\n" . "</script>\n";
112
        return $_ret;
113
    } elseif ($encode === 'hex') {
114
        preg_match('!^(.*)(\?.*)$!', $address, $match);
115
        if (!empty($match[ 2 ])) {
116
            trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.", E_USER_WARNING);
117
            return;
118
        }
119
        $address_encode = '';
120
        for ($x = 0, $_length = strlen($address); $x < $_length; $x++) {
121
            if (preg_match('!\w!' . Smarty::$_UTF8_MODIFIER, $address[ $x ])) {
122
                $address_encode .= '%' . bin2hex($address[ $x ]);
123
            } else {
124
                $address_encode .= $address[ $x ];
125
            }
126
        }
127
        $text_encode = '';
128
        for ($x = 0, $_length = strlen($text); $x < $_length; $x++) {
129
            $text_encode .= '&#x' . bin2hex($text[ $x ]) . ';';
130
        }
131
        $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
132
        return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
133
    } else {
134
        // no encoding
135
        return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
136
    }
137
}