PageRenderTime 73ms CodeModel.GetById 66ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/function.mailto.php

http://zoop.googlecode.com/
PHP | 157 lines | 89 code | 16 blank | 52 comment | 19 complexity | 7de31693523b404f77d1e7fd1978307b MD5 | raw file
  1<?php
  2/**
  3* Smarty plugin
  4* 
  5* @package Smarty
  6* @subpackage PluginsFunction
  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* 
 18* Examples:
 19* <pre>
 20* {mailto address="me@domain.com"}
 21* {mailto address="me@domain.com" encode="javascript"}
 22* {mailto address="me@domain.com" encode="hex"}
 23* {mailto address="me@domain.com" subject="Hello to you!"}
 24* {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
 25* {mailto address="me@domain.com" extra='class="mailto"'}
 26* </pre>
 27* 
 28* @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto}
 29          (Smarty online manual)
 30* @version 1.2
 31* @author Monte Ohrt <monte at ohrt dot com> 
 32* @author credits to Jason Sweat (added cc, bcc and subject functionality) 
 33* @param array $params parameters
 34* Input:<br>
 35*          - address = e-mail address
 36*          - text = (optional) text to display, default is address
 37*          - encode = (optional) can be one of:
 38*                 * none : no encoding (default)
 39*                 * javascript : encode with javascript
 40*                 * javascript_charcode : encode with javascript charcode
 41*                 * hex : encode with hexidecimal (no javascript)
 42*          - cc = (optional) address(es) to carbon copy
 43*          - bcc = (optional) address(es) to blind carbon copy
 44*          - subject = (optional) e-mail subject
 45*          - newsgroups = (optional) newsgroup(s) to post to
 46*          - followupto = (optional) address(es) to follow up to
 47*          - extra = (optional) extra tags for the href link
 48* @param object $smarty Smarty object
 49* @param object $template template object
 50* @return string 
 51*/
 52function smarty_function_mailto($params, $smarty, $template)
 53{
 54    $extra = '';
 55
 56    if (empty($params['address'])) {
 57        trigger_error("mailto: missing 'address' parameter",E_USER_WARNING);
 58        return;
 59    } else {
 60        $address = $params['address'];
 61    } 
 62
 63    $text = $address; 
 64    // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
 65    // so, don't encode it.
 66    $search = array('%40', '%2C');
 67    $replace = array('@', ',');
 68    $mail_parms = array();
 69    foreach ($params as $var => $value) {
 70        switch ($var) {
 71            case 'cc':
 72            case 'bcc':
 73            case 'followupto':
 74                if (!empty($value))
 75                    $mail_parms[] = $var . '=' . str_replace($search, $replace, rawurlencode($value));
 76                break;
 77
 78            case 'subject':
 79            case 'newsgroups':
 80                $mail_parms[] = $var . '=' . rawurlencode($value);
 81                break;
 82
 83            case 'extra':
 84            case 'text':
 85                $$var = $value;
 86
 87            default:
 88        } 
 89    } 
 90
 91    $mail_parm_vals = '';
 92    for ($i = 0; $i < count($mail_parms); $i++) {
 93        $mail_parm_vals .= (0 == $i) ? '?' : '&';
 94        $mail_parm_vals .= $mail_parms[$i];
 95    } 
 96    $address .= $mail_parm_vals;
 97
 98    $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
 99    if (!in_array($encode, array('javascript', 'javascript_charcode', 'hex', 'none'))) {
100        trigger_error("mailto: 'encode' parameter must be none, javascript or hex",E_USER_WARNING);
101        return;
102    } 
103
104    if ($encode == 'javascript') {
105        $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
106
107        $js_encode = '';
108        for ($x = 0; $x < strlen($string); $x++) {
109            $js_encode .= '%' . bin2hex($string[$x]);
110        } 
111
112        return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
113    } elseif ($encode == 'javascript_charcode') {
114        $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
115
116        for($x = 0, $y = strlen($string); $x < $y; $x++) {
117            $ord[] = ord($string[$x]);
118        } 
119
120        $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n";
121        $_ret .= "<!--\n";
122        $_ret .= "{document.write(String.fromCharCode(";
123        $_ret .= implode(',', $ord);
124        $_ret .= "))";
125        $_ret .= "}\n";
126        $_ret .= "//-->\n";
127        $_ret .= "</script>\n";
128
129        return $_ret;
130    } elseif ($encode == 'hex') {
131        preg_match('!^(.*)(\?.*)$!', $address, $match);
132        if (!empty($match[2])) {
133            trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.",E_USER_WARNING);
134            return;
135        } 
136        $address_encode = '';
137        for ($x = 0; $x < strlen($address); $x++) {
138            if (preg_match('!\w!', $address[$x])) {
139                $address_encode .= '%' . bin2hex($address[$x]);
140            } else {
141                $address_encode .= $address[$x];
142            } 
143        } 
144        $text_encode = '';
145        for ($x = 0; $x < strlen($text); $x++) {
146            $text_encode .= '&#x' . bin2hex($text[$x]) . ';';
147        } 
148
149        $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
150        return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
151    } else {
152        // no encoding
153        return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
154    } 
155} 
156
157?>