6 * @subpackage PluginsFunction
10 * Smarty {mailto} function plugin
14 * Purpose: automate mailto address link creation, and optionally encode them.<br>
17 * - address - (required) - e-mail address
18 * - text - (optional) - text to display, default is address
19 * - encode - (optional) - can be one of:
20 * * none : no encoding (default)
21 * * javascript : encode with javascript
22 * * javascript_charcode : encode with javascript charcode
23 * * hex : encode with hexidecimal (no javascript)
24 * - cc - (optional) - address(es) to carbon copy
25 * - bcc - (optional) - address(es) to blind carbon copy
26 * - subject - (optional) - e-mail subject
27 * - newsgroups - (optional) - newsgroup(s) to post to
28 * - followupto - (optional) - address(es) to follow up to
29 * - extra - (optional) - extra tags for the href link
33 * {mailto address="me@domain.com"}
34 * {mailto address="me@domain.com" encode="javascript"}
35 * {mailto address="me@domain.com" encode="hex"}
36 * {mailto address="me@domain.com" subject="Hello to you!"}
37 * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
38 * {mailto address="me@domain.com" extra='class="mailto"'}
41 * @link http://www.smarty.net/manual/en/language.function.mailto.php {mailto}
42 * (Smarty online manual)
44 * @author Monte Ohrt <monte at ohrt dot com>
45 * @author credits to Jason Sweat (added cc, bcc and subject functionality)
47 * @param array $params parameters
51 function smarty_function_mailto($params)
53 static $_allowed_encoding =
54 array('javascript' => true, 'javascript_charcode' => true, 'hex' => true, 'none' => true);
57 if (empty($params[ 'address' ])) {
58 trigger_error("mailto: missing 'address' parameter", E_USER_WARNING);
62 $address = $params[ 'address' ];
66 // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
67 // so, don't encode it.
68 $search = array('%40', '%2C');
69 $replace = array('@', ',');
70 $mail_parms = array();
71 foreach ($params as $var => $value) {
77 $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value));
83 $mail_parms[] = $var . '=' . rawurlencode($value);
95 $address .= '?' . join('&', $mail_parms);
98 $encode = (empty($params[ 'encode' ])) ? 'none' : $params[ 'encode' ];
99 if (!isset($_allowed_encoding[ $encode ])) {
100 trigger_error("mailto: 'encode' parameter must be none, javascript, javascript_charcode or hex",
105 // FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
106 if ($encode == 'javascript') {
107 $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
110 for ($x = 0, $_length = strlen($string); $x < $_length; $x ++) {
111 $js_encode .= '%' . bin2hex($string[ $x ]);
114 return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
115 } elseif ($encode == 'javascript_charcode') {
116 $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
118 for ($x = 0, $y = strlen($string); $x < $y; $x ++) {
119 $ord[] = ord($string[ $x ]);
122 $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n" . "{document.write(String.fromCharCode(" .
123 implode(',', $ord) . "))" . "}\n" . "</script>\n";
126 } elseif ($encode == 'hex') {
127 preg_match('!^(.*)(\?.*)$!', $address, $match);
128 if (!empty($match[ 2 ])) {
129 trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.", E_USER_WARNING);
133 $address_encode = '';
134 for ($x = 0, $_length = strlen($address); $x < $_length; $x ++) {
135 if (preg_match('!\w!' . Smarty::$_UTF8_MODIFIER, $address[ $x ])) {
136 $address_encode .= '%' . bin2hex($address[ $x ]);
138 $address_encode .= $address[ $x ];
142 for ($x = 0, $_length = strlen($text); $x < $_length; $x ++) {
143 $text_encode .= '&#x' . bin2hex($text[ $x ]) . ';';
146 $mailto = "mailto:";
148 return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
151 return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';