init
[garradin.git] / include / libs / template_lite / plugins / function.mailto.php
1 <?php
2
3 /*
4 * Template Lite plugin
5 * -------------------------------------------------------------
6 * Type: function
7 * Name: mailto
8 * Version: 1.2
9 * Date: May 21, 2002
10 * Author: Monte Ohrt <monte@ispi.net>
11 * Credits: Jason Sweat (added cc, bcc and subject functionality)
12 * Purpose: automate mailto address link creation, and optionally
13 * encode them.
14 * Input: address = e-mail address
15 * text = (optional) text to display, default is address
16 * encode = (optional) can be one of:
17 * none : no encoding (default)
18 * javascript : encode with javascript
19 * hex : encode with hexidecimal (no javascript)
20 * cc = (optional) address(es) to carbon copy
21 * bcc = (optional) address(es) to blind carbon copy
22 * subject = (optional) e-mail subject
23 * newsgroups = (optional) newsgroup(s) to post to
24 * followupto = (optional) address(es) to follow up to
25 * extra = (optional) extra tags for the href link
26 *
27 * Examples: {mailto address="me@domain.com"}
28 * {mailto address="me@domain.com" encode="javascript"}
29 * {mailto address="me@domain.com" encode="hex"}
30 * {mailto address="me@domain.com" subject="Hello to you!"}
31 * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
32 * {mailto address="me@domain.com" extra='class="mailto"'}
33 * Taken from the original Smarty
34 * http://smarty.php.net
35 * -------------------------------------------------------------
36 */
37 function tpl_function_mailto($params, &$template_object)
38 {
39 extract($params);
40
41 if (empty($address))
42 {
43 throw new Template_Exception("mailto: missing 'address' parameter", $template_object);
44 return;
45 }
46
47 if (empty($text))
48 {
49 $text = $address;
50 }
51
52 if (empty($extra))
53 {
54 $extra = "";
55 }
56
57 // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
58 // so, don't encode it.
59
60 $mail_parms = array();
61 if (!empty($cc))
62 {
63 $mail_parms[] = 'cc='.str_replace('%40','@',rawurlencode($cc));
64 }
65
66 if (!empty($bcc))
67 {
68 $mail_parms[] = 'bcc='.str_replace('%40','@',rawurlencode($bcc));
69 }
70
71 if (!empty($subject))
72 {
73 $mail_parms[] = 'subject='.rawurlencode($subject);
74 }
75
76 if (!empty($newsgroups))
77 {
78 $mail_parms[] = 'newsgroups='.rawurlencode($newsgroups);
79 }
80
81 if (!empty($followupto))
82 {
83 $mail_parms[] = 'followupto='.str_replace('%40','@',rawurlencode($followupto));
84 }
85
86 $mail_parm_vals = "";
87 for ($i=0; $i<count($mail_parms); $i++)
88 {
89 $mail_parm_vals .= (0==$i) ? '?' : '&';
90 $mail_parm_vals .= $mail_parms[$i];
91 }
92 $address .= $mail_parm_vals;
93
94 if (empty($encode))
95 {
96 $encode = 'none';
97 }
98 elseif (!in_array($encode,array('javascript','hex','none')) )
99 {
100 throw new Template_Exception("mailto: 'encode' parameter must be none, javascript or hex", $template_object);
101 return;
102 }
103
104 if ($encode == 'javascript' )
105 {
106 $string = 'document.write(\'<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>\');';
107 $js_encode = '';
108 for ($x=0; $x < strlen($string); $x++)
109 {
110 $js_encode .= '%' . bin2hex($string[$x]);
111 }
112 return '<script type="text/javascript" language="javascript">eval(unescape(\''.$js_encode.'\'))</script>';
113 }
114 elseif ($encode == 'hex')
115 {
116 preg_match('!^(.*)(\?.*)$!',$address,$match);
117 if(!empty($match[2]))
118 {
119 throw new Template_Exception("mailto: hex encoding does not work with extra attributes. Try javascript.", $template_object);
120 return;
121 }
122 $address_encode = "";
123 for ($x=0; $x < strlen($address); $x++)
124 {
125 if(preg_match('!\w!',$address[$x]))
126 {
127 $address_encode .= '%' . bin2hex($address[$x]);
128 }
129 else
130 {
131 $address_encode .= $address[$x];
132 }
133 }
134 $text_encode = "";
135 for ($x=0; $x < strlen($text); $x++)
136 {
137 $text_encode .= '&#x' . bin2hex($text[$x]).';';
138 }
139 return '<a href="mailto:'.$address_encode.'" '.$extra.'>'.$text_encode.'</a>';
140 }
141 else
142 {
143 // no encoding
144 return '<a href="mailto:'.$address.'" '.$extra.'>'.$text.'</a>';
145 }
146 }
147
148 ?>