| 1 | <?php |
| 2 | /** |
| 3 | * Smarty plugin |
| 4 | * @package Smarty |
| 5 | * @subpackage plugins |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * Smarty {mailto} function plugin |
| 11 | * |
| 12 | * Type: function<br> |
| 13 | * Name: mailto<br> |
| 14 | * Date: May 21, 2002 |
| 15 | * Purpose: automate mailto address link creation, and optionally |
| 16 | * encode them.<br> |
| 17 | * Input:<br> |
| 18 | * - address = 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 |
| 31 | * |
| 32 | * Examples: |
| 33 | * <pre> |
| 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"'} |
| 40 | * </pre> |
| 41 | * @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto} |
| 42 | * (Smarty online manual) |
| 43 | * @version 1.2 |
| 44 | * @author Monte Ohrt <monte at ohrt dot com> |
| 45 | * @author credits to Jason Sweat (added cc, bcc and subject functionality) |
| 46 | * @param array |
| 47 | * @param Smarty |
| 48 | * @return string |
| 49 | */ |
| 50 | function smarty_function_mailto($params, &$smarty) |
| 51 | { |
| 52 | $extra = ''; |
| 53 | |
| 54 | if (empty($params['address'])) { |
| 55 | $smarty->trigger_error("mailto: missing 'address' parameter"); |
| 56 | return; |
| 57 | } else { |
| 58 | $address = $params['address']; |
| 59 | } |
| 60 | |
| 61 | $text = $address; |
| 62 | |
| 63 | // netscape and mozilla do not decode %40 (@) in BCC field (bug?) |
| 64 | // so, don't encode it. |
| 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('%40','@',rawurlencode($value)); |
| 73 | break; |
| 74 | |
| 75 | case 'subject': |
| 76 | case 'newsgroups': |
| 77 | $mail_parms[] = $var.'='.rawurlencode($value); |
| 78 | break; |
| 79 | |
| 80 | case 'extra': |
| 81 | case 'text': |
| 82 | $$var = $value; |
| 83 | |
| 84 | default: |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | $mail_parm_vals = ''; |
| 89 | for ($i=0; $i<count($mail_parms); $i++) { |
| 90 | $mail_parm_vals .= (0==$i) ? '?' : '&'; |
| 91 | $mail_parm_vals .= $mail_parms[$i]; |
| 92 | } |
| 93 | $address .= $mail_parm_vals; |
| 94 | |
| 95 | $encode = (empty($params['encode'])) ? 'none' : $params['encode']; |
| 96 | if (!in_array($encode,array('javascript','javascript_charcode','hex','none')) ) { |
| 97 | $smarty->trigger_error("mailto: 'encode' parameter must be none, javascript or hex"); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | if ($encode == 'javascript' ) { |
| 102 | $string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');'; |
| 103 | |
| 104 | $js_encode = ''; |
| 105 | for ($x=0; $x < strlen($string); $x++) { |
| 106 | $js_encode .= '%' . bin2hex($string[$x]); |
| 107 | } |
| 108 | |
| 109 | return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>'; |
| 110 | |
| 111 | } elseif ($encode == 'javascript_charcode' ) { |
| 112 | $string = '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; |
| 113 | |
| 114 | for($x = 0, $y = strlen($string); $x < $y; $x++ ) { |
| 115 | $ord[] = ord($string[$x]); |
| 116 | } |
| 117 | |
| 118 | $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n"; |
| 119 | $_ret .= "<!--\n"; |
| 120 | $_ret .= "{document.write(String.fromCharCode("; |
| 121 | $_ret .= implode(',',$ord); |
| 122 | $_ret .= "))"; |
| 123 | $_ret .= "}\n"; |
| 124 | $_ret .= "//-->\n"; |
| 125 | $_ret .= "</script>\n"; |
| 126 | |
| 127 | return $_ret; |
| 128 | |
| 129 | |
| 130 | } elseif ($encode == 'hex') { |
| 131 | |
| 132 | preg_match('!^(.*)(\?.*)$!',$address,$match); |
| 133 | if(!empty($match[2])) { |
| 134 | $smarty->trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript."); |
| 135 | return; |
| 136 | } |
| 137 | $address_encode = ''; |
| 138 | for ($x=0; $x < strlen($address); $x++) { |
| 139 | if(preg_match('!\w!',$address[$x])) { |
| 140 | $address_encode .= '%' . bin2hex($address[$x]); |
| 141 | } else { |
| 142 | $address_encode .= $address[$x]; |
| 143 | } |
| 144 | } |
| 145 | $text_encode = ''; |
| 146 | for ($x=0; $x < strlen($text); $x++) { |
| 147 | $text_encode .= '&#x' . bin2hex($text[$x]).';'; |
| 148 | } |
| 149 | |
| 150 | $mailto = "mailto:"; |
| 151 | return '<a href="'.$mailto.$address_encode.'" '.$extra.'>'.$text_encode.'</a>'; |
| 152 | |
| 153 | } else { |
| 154 | // no encoding |
| 155 | return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; |
| 156 | |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | |
| 161 | /* vim: set expandtab: */ |
| 162 | |
| 163 | ?> |