]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Smarty/plugins/function.mailto.php
Release 6.5.0
[Github/sugarcrm.git] / include / Smarty / plugins / function.mailto.php
1 <?php
2
3 /*
4
5 Modification information for LGPL compliance
6
7 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
8
9 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
10
11 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
12
13 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
14
15 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
16
17 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
18
19 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
20
21 r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
22
23
24 */
25
26
27 /**
28  * Smarty plugin
29  * @package Smarty
30  * @subpackage plugins
31  */
32
33
34 /**
35  * Smarty {mailto} function plugin
36  *
37  * Type:     function<br>
38  * Name:     mailto<br>
39  * Date:     May 21, 2002
40  * Purpose:  automate mailto address link creation, and optionally
41  *           encode them.<br>
42  * Input:<br>
43  *         - address = e-mail address
44  *         - text = (optional) text to display, default is address
45  *         - encode = (optional) can be one of:
46  *                * none : no encoding (default)
47  *                * javascript : encode with javascript
48  *                * javascript_charcode : encode with javascript charcode
49  *                * hex : encode with hexidecimal (no javascript)
50  *         - cc = (optional) address(es) to carbon copy
51  *         - bcc = (optional) address(es) to blind carbon copy
52  *         - subject = (optional) e-mail subject
53  *         - newsgroups = (optional) newsgroup(s) to post to
54  *         - followupto = (optional) address(es) to follow up to
55  *         - extra = (optional) extra tags for the href link
56  *
57  * Examples:
58  * <pre>
59  * {mailto address="me@domain.com"}
60  * {mailto address="me@domain.com" encode="javascript"}
61  * {mailto address="me@domain.com" encode="hex"}
62  * {mailto address="me@domain.com" subject="Hello to you!"}
63  * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
64  * {mailto address="me@domain.com" extra='class="mailto"'}
65  * </pre>
66  * @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto}
67  *          (Smarty online manual)
68  * @version  1.2
69  * @author   Monte Ohrt <monte at ohrt dot com>
70  * @author   credits to Jason Sweat (added cc, bcc and subject functionality)
71  * @param    array
72  * @param    Smarty
73  * @return   string
74  */
75 function smarty_function_mailto($params, &$smarty)
76 {
77     $extra = '';
78
79     if (empty($params['address'])) {
80         $smarty->trigger_error("mailto: missing 'address' parameter");
81         return;
82     } else {
83         $address = $params['address'];
84     }
85
86     $text = $address;
87
88     // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
89     // so, don't encode it.
90     $mail_parms = array();
91     foreach ($params as $var=>$value) {
92         switch ($var) {
93             case 'cc':
94             case 'bcc':
95             case 'followupto':
96                 if (!empty($value))
97                     $mail_parms[] = $var.'='.str_replace('%40','@',rawurlencode($value));
98                 break;
99                 
100             case 'subject':
101             case 'newsgroups':
102                 $mail_parms[] = $var.'='.rawurlencode($value);
103                 break;
104
105             case 'extra':
106             case 'text':
107                 $$var = $value;
108
109             default:
110         }
111     }
112
113     $mail_parm_vals = '';
114     for ($i=0; $i<count($mail_parms); $i++) {
115         $mail_parm_vals .= (0==$i) ? '?' : '&';
116         $mail_parm_vals .= $mail_parms[$i];
117     }
118     $address .= $mail_parm_vals;
119
120     $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
121     if (!in_array($encode,array('javascript','javascript_charcode','hex','none')) ) {
122         $smarty->trigger_error("mailto: 'encode' parameter must be none, javascript or hex");
123         return;
124     }
125
126     if ($encode == 'javascript' ) {
127         $string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');';
128
129         $js_encode = '';
130         for ($x=0; $x < strlen($string); $x++) {
131             $js_encode .= '%' . bin2hex($string[$x]);
132         }
133
134         return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>';
135
136     } elseif ($encode == 'javascript_charcode' ) {
137         $string = '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>';
138
139         for($x = 0, $y = strlen($string); $x < $y; $x++ ) {
140             $ord[] = ord($string[$x]);   
141         }
142
143         $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n";
144         $_ret .= "<!--\n";
145         $_ret .= "{document.write(String.fromCharCode(";
146         $_ret .= implode(',',$ord);
147         $_ret .= "))";
148         $_ret .= "}\n";
149         $_ret .= "//-->\n";
150         $_ret .= "</script>\n";
151         
152         return $_ret;
153         
154         
155     } elseif ($encode == 'hex') {
156
157         preg_match('!^(.*)(\?.*)$!',$address,$match);
158         if(!empty($match[2])) {
159             $smarty->trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.");
160             return;
161         }
162         $address_encode = '';
163         for ($x=0; $x < strlen($address); $x++) {
164             if(preg_match('!\w!',$address[$x])) {
165                 $address_encode .= '%' . bin2hex($address[$x]);
166             } else {
167                 $address_encode .= $address[$x];
168             }
169         }
170         $text_encode = '';
171         for ($x=0; $x < strlen($text); $x++) {
172             $text_encode .= '&#x' . bin2hex($text[$x]).';';
173         }
174
175         $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
176         return '<a href="'.$mailto.$address_encode.'" '.$extra.'>'.$text_encode.'</a>';
177
178     } else {
179         // no encoding
180         return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>';
181
182     }
183
184 }
185
186 /* vim: set expandtab: */
187
188 ?>