| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Smarty plugin | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * @package Smarty | 
					
						
							|  |  |  |  * @subpackage PluginsFunction | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Smarty {mailto} function plugin | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  * Type:     function<br> | 
					
						
							|  |  |  |  * Name:     mailto<br> | 
					
						
							|  |  |  |  * Date:     May 21, 2002 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * Purpose:  automate mailto address link creation, and optionally encode them.<br> | 
					
						
							|  |  |  |  * Params: | 
					
						
							|  |  |  |  * <pre> | 
					
						
							|  |  |  |  * - address    - (required) - 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 | 
					
						
							|  |  |  |  *                             * javascript_charcode : encode with javascript charcode | 
					
						
							|  |  |  |  *                             * 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 | 
					
						
							|  |  |  |  * </pre> | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +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> | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @link http://www.smarty.net/manual/en/language.function.mailto.php {mailto} | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  *          (Smarty online manual) | 
					
						
							|  |  |  |  * @version 1.2 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |  * @author Monte Ohrt <monte at ohrt dot com> | 
					
						
							|  |  |  |  * @author credits to Jason Sweat (added cc, bcc and subject functionality) | 
					
						
							|  |  |  |  * @param array                    $params   parameters | 
					
						
							|  |  |  |  * @param Smarty_Internal_Template $template template object | 
					
						
							|  |  |  |  * @return string | 
					
						
							| 
									
										
										
										
											2010-08-17 15:39:51 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-11-12 23:42:32 +00:00
										 |  |  | function smarty_function_mailto($params, $template) | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-01 18:10:48 +00:00
										 |  |  |     static $_allowed_encoding = array('javascript' => true, 'javascript_charcode' => true, 'hex' => true, 'none' => true); | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $extra = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (empty($params['address'])) { | 
					
						
							| 
									
										
										
										
											2009-12-01 20:34:32 +00:00
										 |  |  |         trigger_error("mailto: missing 'address' parameter",E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         $address = $params['address']; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     $text = $address; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
 | 
					
						
							|  |  |  |     // so, don't encode it.
 | 
					
						
							|  |  |  |     $search = array('%40', '%2C'); | 
					
						
							|  |  |  |     $replace = array('@', ','); | 
					
						
							|  |  |  |     $mail_parms = array(); | 
					
						
							|  |  |  |     foreach ($params as $var => $value) { | 
					
						
							|  |  |  |         switch ($var) { | 
					
						
							|  |  |  |             case 'cc': | 
					
						
							|  |  |  |             case 'bcc': | 
					
						
							|  |  |  |             case 'followupto': | 
					
						
							|  |  |  |                 if (!empty($value)) | 
					
						
							|  |  |  |                     $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value)); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'subject': | 
					
						
							|  |  |  |             case 'newsgroups': | 
					
						
							|  |  |  |                 $mail_parms[] = $var . '=' . rawurlencode($value); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             case 'extra': | 
					
						
							|  |  |  |             case 'text': | 
					
						
							|  |  |  |                 $$var = $value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             default: | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-07 14:05:26 +00:00
										 |  |  |     if ($mail_parms) { | 
					
						
							|  |  |  |         $address .= '?' . join('&', $mail_parms); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     $encode = (empty($params['encode'])) ? 'none' : $params['encode']; | 
					
						
							| 
									
										
										
										
											2011-10-01 18:10:48 +00:00
										 |  |  |     if (!isset($_allowed_encoding[$encode])) { | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         trigger_error("mailto: 'encode' parameter must be none, javascript, javascript_charcode or hex", E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     // FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |     if ($encode == 'javascript') { | 
					
						
							|  |  |  |         $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $js_encode = ''; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         for ($x = 0, $_length = strlen($string); $x < $_length; $x++) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             $js_encode .= '%' . bin2hex($string[$x]); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>'; | 
					
						
							|  |  |  |     } elseif ($encode == 'javascript_charcode') { | 
					
						
							|  |  |  |         $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  |         for ($x = 0, $y = strlen($string); $x < $y; $x++) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             $ord[] = ord($string[$x]); | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-07 08:55:12 +00:00
										 |  |  |         $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n" | 
					
						
							|  |  |  |             . "{document.write(String.fromCharCode(" | 
					
						
							|  |  |  |             . implode(',', $ord) | 
					
						
							|  |  |  |             . "))" | 
					
						
							|  |  |  |             . "}\n" | 
					
						
							|  |  |  |             . "</script>\n"; | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $_ret; | 
					
						
							|  |  |  |     } elseif ($encode == 'hex') { | 
					
						
							|  |  |  |         preg_match('!^(.*)(\?.*)$!', $address, $match); | 
					
						
							|  |  |  |         if (!empty($match[2])) { | 
					
						
							| 
									
										
										
										
											2009-12-01 20:34:32 +00:00
										 |  |  |             trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.",E_USER_WARNING); | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         $address_encode = ''; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         for ($x = 0, $_length = strlen($address); $x < $_length; $x++) { | 
					
						
							| 
									
										
										
										
											2011-12-18 18:48:07 +00:00
										 |  |  |             if (preg_match('!\w!' . Smarty::$_UTF8_MODIFIER, $address[$x])) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |                 $address_encode .= '%' . bin2hex($address[$x]); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 $address_encode .= $address[$x]; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         $text_encode = ''; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         for ($x = 0, $_length = strlen($text); $x < $_length; $x++) { | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |             $text_encode .= '&#x' . bin2hex($text[$x]) . ';'; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $mailto = "mailto:"; | 
					
						
							| 
									
										
										
										
											2013-07-14 22:15:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-22 16:09:05 +00:00
										 |  |  |         return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>'; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         // no encoding
 | 
					
						
							|  |  |  |         return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>'; | 
					
						
							| 
									
										
										
										
											2011-09-16 14:19:56 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } |