| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2003-04-20 21:12:13 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  |  * Smarty plugin | 
					
						
							| 
									
										
										
										
											2003-04-20 21:12:13 +00:00
										 |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage plugins | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Smarty {mailto} function plugin | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Type:     function<br> | 
					
						
							|  |  |  |  * Name:     mailto<br> | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  |  * Date:     May 21, 2002 | 
					
						
							|  |  |  |  * Purpose:  automate mailto address link creation, and optionally | 
					
						
							| 
									
										
										
										
											2003-04-20 21:12:13 +00:00
										 |  |  |  *           encode them.<br> | 
					
						
							|  |  |  |  * Input:<br> | 
					
						
							|  |  |  |  *         - address = e-mail address | 
					
						
							|  |  |  |  *         - text = (optional) text to display, default is address | 
					
						
							|  |  |  |  *         - encode = (optional) can be one of: | 
					
						
							|  |  |  |  *                * none : no encoding (default) | 
					
						
							|  |  |  |  *                * javascript : encode with javascript | 
					
						
							| 
									
										
										
										
											2005-02-25 17:08:28 +00:00
										 |  |  |  *                * javascript_charcode : encode with javascript charcode | 
					
						
							| 
									
										
										
										
											2003-04-20 21:12:13 +00:00
										 |  |  |  *                * hex : encode with hexidecimal (no javascript) | 
					
						
							|  |  |  |  *         - cc = (optional) address(es) to carbon copy | 
					
						
							|  |  |  |  *         - bcc = (optional) address(es) to blind carbon copy | 
					
						
							|  |  |  |  *         - subject = (optional) e-mail subject | 
					
						
							|  |  |  |  *         - newsgroups = (optional) newsgroup(s) to post to | 
					
						
							|  |  |  |  *         - followupto = (optional) address(es) to follow up to | 
					
						
							|  |  |  |  *         - extra = (optional) extra tags for the href link | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2003-04-20 21:12:13 +00:00
										 |  |  |  * Examples: | 
					
						
							|  |  |  |  * <pre> | 
					
						
							|  |  |  |  * {mailto address="me@domain.com"} | 
					
						
							|  |  |  |  * {mailto address="me@domain.com" encode="javascript"} | 
					
						
							|  |  |  |  * {mailto address="me@domain.com" encode="hex"} | 
					
						
							|  |  |  |  * {mailto address="me@domain.com" subject="Hello to you!"} | 
					
						
							|  |  |  |  * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"} | 
					
						
							|  |  |  |  * {mailto address="me@domain.com" extra='class="mailto"'} | 
					
						
							|  |  |  |  * </pre> | 
					
						
							|  |  |  |  * @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto} | 
					
						
							|  |  |  |  *          (Smarty online manual) | 
					
						
							|  |  |  |  * @version  1.2 | 
					
						
							| 
									
										
										
										
											2005-01-21 17:46:15 +00:00
										 |  |  |  * @author   Monte Ohrt <monte at ohrt dot com> | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |  * @author   credits to Jason Sweat (added cc, bcc and subject functionality) | 
					
						
							|  |  |  |  * @param    array | 
					
						
							|  |  |  |  * @param    Smarty | 
					
						
							|  |  |  |  * @return   string | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | function smarty_function_mailto($params, &$smarty) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2004-04-07 19:58:54 +00:00
										 |  |  |     $extra = ''; | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-11 09:38:18 +00:00
										 |  |  |     if (empty($params['address'])) { | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  |         $smarty->trigger_error("mailto: missing 'address' parameter"); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:38:18 +00:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         $address = $params['address']; | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-11 09:38:18 +00:00
										 |  |  |     $text = $address; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
 | 
					
						
							|  |  |  |     // so, don't encode it.
 | 
					
						
							|  |  |  |     $mail_parms = array(); | 
					
						
							| 
									
										
										
										
											2003-12-11 09:38:18 +00:00
										 |  |  |     foreach ($params as $var=>$value) { | 
					
						
							|  |  |  |         switch ($var) { | 
					
						
							|  |  |  |             case 'cc': | 
					
						
							|  |  |  |             case 'bcc': | 
					
						
							|  |  |  |             case 'followupto': | 
					
						
							|  |  |  |                 if (!empty($value)) | 
					
						
							|  |  |  |                     $mail_parms[] = $var.'='.str_replace('%40','@',rawurlencode($value)); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  |             case 'subject': | 
					
						
							|  |  |  |             case 'newsgroups': | 
					
						
							|  |  |  |                 $mail_parms[] = $var.'='.rawurlencode($value); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-11 09:38:18 +00:00
										 |  |  |             case 'extra': | 
					
						
							|  |  |  |             case 'text': | 
					
						
							|  |  |  |                 $$var = $value; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-11 09:38:18 +00:00
										 |  |  |             default: | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-09-02 10:29:10 +00:00
										 |  |  |     $mail_parm_vals = ''; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |     for ($i=0; $i<count($mail_parms); $i++) { | 
					
						
							|  |  |  |         $mail_parm_vals .= (0==$i) ? '?' : '&'; | 
					
						
							|  |  |  |         $mail_parm_vals .= $mail_parms[$i]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     $address .= $mail_parm_vals; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-12-11 09:38:18 +00:00
										 |  |  |     $encode = (empty($params['encode'])) ? 'none' : $params['encode']; | 
					
						
							| 
									
										
										
										
											2005-02-25 17:08:28 +00:00
										 |  |  |     if (!in_array($encode,array('javascript','javascript_charcode','hex','none')) ) { | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  |         $smarty->trigger_error("mailto: 'encode' parameter must be none, javascript or hex"); | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ($encode == 'javascript' ) { | 
					
						
							|  |  |  |         $string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-02-07 20:15:45 +00:00
										 |  |  |         $js_encode = ''; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |         for ($x=0; $x < strlen($string); $x++) { | 
					
						
							|  |  |  |             $js_encode .= '%' . bin2hex($string[$x]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-07-02 14:14:30 +00:00
										 |  |  |         return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>'; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-02-25 17:08:28 +00:00
										 |  |  |     } elseif ($encode == 'javascript_charcode' ) { | 
					
						
							|  |  |  |         $string = '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for($x = 0, $y = strlen($string); $x < $y; $x++ ) { | 
					
						
							|  |  |  |             $ord[] = ord($string[$x]);    | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n"; | 
					
						
							|  |  |  |         $_ret .= "<!--\n"; | 
					
						
							|  |  |  |         $_ret .= "{document.write(String.fromCharCode("; | 
					
						
							|  |  |  |         $_ret .= implode(',',$ord); | 
					
						
							|  |  |  |         $_ret .= "))"; | 
					
						
							|  |  |  |         $_ret .= "}\n"; | 
					
						
							|  |  |  |         $_ret .= "//-->\n"; | 
					
						
							|  |  |  |         $_ret .= "</script>\n"; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         return $_ret; | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |     } elseif ($encode == 'hex') { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         preg_match('!^(.*)(\?.*)$!',$address,$match); | 
					
						
							|  |  |  |         if(!empty($match[2])) { | 
					
						
							|  |  |  |             $smarty->trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript."); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2004-02-22 23:18:30 +00:00
										 |  |  |         $address_encode = ''; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |         for ($x=0; $x < strlen($address); $x++) { | 
					
						
							|  |  |  |             if(preg_match('!\w!',$address[$x])) { | 
					
						
							|  |  |  |                 $address_encode .= '%' . bin2hex($address[$x]); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $address_encode .= $address[$x]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2004-02-22 23:18:30 +00:00
										 |  |  |         $text_encode = ''; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  |         for ($x=0; $x < strlen($text); $x++) { | 
					
						
							|  |  |  |             $text_encode .= '&#x' . bin2hex($text[$x]).';'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-28 08:44:46 +00:00
										 |  |  |         $mailto = "mailto:"; | 
					
						
							|  |  |  |         return '<a href="'.$mailto.$address_encode.'" '.$extra.'>'.$text_encode.'</a>'; | 
					
						
							| 
									
										
										
										
											2003-12-11 09:25:23 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         // no encoding
 | 
					
						
							|  |  |  |         return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-09 19:43:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* vim: set expandtab: */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ?>
 |