| 1 |
joko |
1.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 |
|
|
* * 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 |
| 30 |
|
|
* |
| 31 |
|
|
* Examples: |
| 32 |
|
|
* <pre> |
| 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"'} |
| 39 |
|
|
* </pre> |
| 40 |
|
|
* @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto} |
| 41 |
|
|
* (Smarty online manual) |
| 42 |
|
|
* @version 1.2 |
| 43 |
|
|
* @author Monte Ohrt <monte@ispi.net> |
| 44 |
|
|
* @author credits to Jason Sweat (added cc, bcc and subject functionality) |
| 45 |
|
|
* @param array |
| 46 |
|
|
* @param Smarty |
| 47 |
|
|
* @return string |
| 48 |
|
|
*/ |
| 49 |
|
|
function smarty_function_mailto($params, &$smarty) |
| 50 |
|
|
{ |
| 51 |
|
|
$extra = ''; |
| 52 |
|
|
|
| 53 |
|
|
if (empty($params['address'])) { |
| 54 |
|
|
$smarty->trigger_error("mailto: missing 'address' parameter"); |
| 55 |
|
|
return; |
| 56 |
|
|
} else { |
| 57 |
|
|
$address = $params['address']; |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
$text = $address; |
| 61 |
|
|
|
| 62 |
|
|
// netscape and mozilla do not decode %40 (@) in BCC field (bug?) |
| 63 |
|
|
// so, don't encode it. |
| 64 |
|
|
$mail_parms = array(); |
| 65 |
|
|
foreach ($params as $var=>$value) { |
| 66 |
|
|
switch ($var) { |
| 67 |
|
|
case 'cc': |
| 68 |
|
|
case 'bcc': |
| 69 |
|
|
case 'followupto': |
| 70 |
|
|
if (!empty($value)) |
| 71 |
|
|
$mail_parms[] = $var.'='.str_replace('%40','@',rawurlencode($value)); |
| 72 |
|
|
break; |
| 73 |
|
|
|
| 74 |
|
|
case 'subject': |
| 75 |
|
|
case 'newsgroups': |
| 76 |
|
|
$mail_parms[] = $var.'='.rawurlencode($value); |
| 77 |
|
|
break; |
| 78 |
|
|
|
| 79 |
|
|
case 'extra': |
| 80 |
|
|
case 'text': |
| 81 |
|
|
$$var = $value; |
| 82 |
|
|
|
| 83 |
|
|
default: |
| 84 |
|
|
} |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
|
|
$mail_parm_vals = ''; |
| 88 |
|
|
for ($i=0; $i<count($mail_parms); $i++) { |
| 89 |
|
|
$mail_parm_vals .= (0==$i) ? '?' : '&'; |
| 90 |
|
|
$mail_parm_vals .= $mail_parms[$i]; |
| 91 |
|
|
} |
| 92 |
|
|
$address .= $mail_parm_vals; |
| 93 |
|
|
|
| 94 |
|
|
$encode = (empty($params['encode'])) ? 'none' : $params['encode']; |
| 95 |
|
|
if (!in_array($encode,array('javascript','hex','none')) ) { |
| 96 |
|
|
$smarty->trigger_error("mailto: 'encode' parameter must be none, javascript or hex"); |
| 97 |
|
|
return; |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
if ($encode == 'javascript' ) { |
| 101 |
|
|
$string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');'; |
| 102 |
|
|
|
| 103 |
|
|
$js_encode = ''; |
| 104 |
|
|
for ($x=0; $x < strlen($string); $x++) { |
| 105 |
|
|
$js_encode .= '%' . bin2hex($string[$x]); |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
|
|
return '<script type="text/javascript" language="javascript">eval(unescape(\''.$js_encode.'\'))</script>'; |
| 109 |
|
|
|
| 110 |
|
|
} elseif ($encode == 'hex') { |
| 111 |
|
|
|
| 112 |
|
|
preg_match('!^(.*)(\?.*)$!',$address,$match); |
| 113 |
|
|
if(!empty($match[2])) { |
| 114 |
|
|
$smarty->trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript."); |
| 115 |
|
|
return; |
| 116 |
|
|
} |
| 117 |
|
|
$address_encode = ''; |
| 118 |
|
|
for ($x=0; $x < strlen($address); $x++) { |
| 119 |
|
|
if(preg_match('!\w!',$address[$x])) { |
| 120 |
|
|
$address_encode .= '%' . bin2hex($address[$x]); |
| 121 |
|
|
} else { |
| 122 |
|
|
$address_encode .= $address[$x]; |
| 123 |
|
|
} |
| 124 |
|
|
} |
| 125 |
|
|
$text_encode = ''; |
| 126 |
|
|
for ($x=0; $x < strlen($text); $x++) { |
| 127 |
|
|
$text_encode .= '&#x' . bin2hex($text[$x]).';'; |
| 128 |
|
|
} |
| 129 |
|
|
|
| 130 |
|
|
$mailto = "mailto:"; |
| 131 |
|
|
return '<a href="'.$mailto.$address_encode.'" '.$extra.'>'.$text_encode.'</a>'; |
| 132 |
|
|
|
| 133 |
|
|
} else { |
| 134 |
|
|
// no encoding |
| 135 |
|
|
return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; |
| 136 |
|
|
|
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
|
} |
| 140 |
|
|
|
| 141 |
|
|
/* vim: set expandtab: */ |
| 142 |
|
|
|
| 143 |
|
|
?> |