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