library/smarty/libs/plugins/function.mailto.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage PluginsFunction
       
     7  */
       
     8 
       
     9 /**
       
    10  * Smarty {mailto} function plugin
       
    11  * Type:     function<br>
       
    12  * Name:     mailto<br>
       
    13  * Date:     May 21, 2002
       
    14  * Purpose:  automate mailto address link creation, and optionally encode them.<br>
       
    15  * Params:
       
    16  * <pre>
       
    17  * - address    - (required) - e-mail address
       
    18  * - text       - (optional) - text to display, default is address
       
    19  * - encode     - (optional) - can be one of:
       
    20  *                             * none : no encoding (default)
       
    21  *                             * javascript : encode with javascript
       
    22  *                             * javascript_charcode : encode with javascript charcode
       
    23  *                             * hex : encode with hexidecimal (no javascript)
       
    24  * - cc         - (optional) - address(es) to carbon copy
       
    25  * - bcc        - (optional) - address(es) to blind carbon copy
       
    26  * - subject    - (optional) - e-mail subject
       
    27  * - newsgroups - (optional) - newsgroup(s) to post to
       
    28  * - followupto - (optional) - address(es) to follow up to
       
    29  * - extra      - (optional) - extra tags for the href link
       
    30  * </pre>
       
    31  * Examples:
       
    32  * <pre>
       
    33  * {mailto address="me@domain.com"}
       
    34  * {mailto address="me@domain.com" encode="javascript"}
       
    35  * {mailto address="me@domain.com" encode="hex"}
       
    36  * {mailto address="me@domain.com" subject="Hello to you!"}
       
    37  * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
       
    38  * {mailto address="me@domain.com" extra='class="mailto"'}
       
    39  * </pre>
       
    40  *
       
    41  * @link     http://www.smarty.net/manual/en/language.function.mailto.php {mailto}
       
    42  *           (Smarty online manual)
       
    43  * @version  1.2
       
    44  * @author   Monte Ohrt <monte at ohrt dot com>
       
    45  * @author   credits to Jason Sweat (added cc, bcc and subject functionality)
       
    46  *
       
    47  * @param array $params parameters
       
    48  *
       
    49  * @return string
       
    50  */
       
    51 function smarty_function_mailto($params) {
       
    52     static $_allowed_encoding = array('javascript' => true, 'javascript_charcode' => true, 'hex' => true, 'none' => true);
       
    53     $extra = '';
       
    54 
       
    55     if (empty($params['address'])) {
       
    56         trigger_error("mailto: missing 'address' parameter", E_USER_WARNING);
       
    57 
       
    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                 }
       
    77                 break;
       
    78 
       
    79             case 'subject':
       
    80             case 'newsgroups':
       
    81                 $mail_parms[] = $var . '=' . rawurlencode($value);
       
    82                 break;
       
    83 
       
    84             case 'extra':
       
    85             case 'text':
       
    86                 $$var = $value;
       
    87 
       
    88             default:
       
    89         }
       
    90     }
       
    91 
       
    92     if ($mail_parms) {
       
    93         $address .= '?' . join('&', $mail_parms);
       
    94     }
       
    95 
       
    96     $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
       
    97     if (!isset($_allowed_encoding[$encode])) {
       
    98         trigger_error("mailto: 'encode' parameter must be none, javascript, javascript_charcode or hex", E_USER_WARNING);
       
    99 
       
   100         return;
       
   101     }
       
   102     // FIXME: (rodneyrehm) document.write() excues me what? 1998 has passed!
       
   103     if ($encode == 'javascript') {
       
   104         $string = 'document.write(\'<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>\');';
       
   105 
       
   106         $js_encode = '';
       
   107         for ($x = 0, $_length = strlen($string); $x < $_length; $x++) {
       
   108             $js_encode .= '%' . bin2hex($string[$x]);
       
   109         }
       
   110 
       
   111         return '<script type="text/javascript">eval(unescape(\'' . $js_encode . '\'))</script>';
       
   112     } elseif ($encode == 'javascript_charcode') {
       
   113         $string = '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
       
   114 
       
   115         for ($x = 0, $y = strlen($string); $x < $y; $x++) {
       
   116             $ord[] = ord($string[$x]);
       
   117         }
       
   118 
       
   119         $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n"
       
   120             . "{document.write(String.fromCharCode("
       
   121             . implode(',', $ord)
       
   122             . "))"
       
   123             . "}\n"
       
   124             . "</script>\n";
       
   125 
       
   126         return $_ret;
       
   127     } elseif ($encode == 'hex') {
       
   128         preg_match('!^(.*)(\?.*)$!', $address, $match);
       
   129         if (!empty($match[2])) {
       
   130             trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.", E_USER_WARNING);
       
   131 
       
   132             return;
       
   133         }
       
   134         $address_encode = '';
       
   135         for ($x = 0, $_length = strlen($address); $x < $_length; $x++) {
       
   136             if (preg_match('!\w!' . Smarty::$_UTF8_MODIFIER, $address[$x])) {
       
   137                 $address_encode .= '%' . bin2hex($address[$x]);
       
   138             } else {
       
   139                 $address_encode .= $address[$x];
       
   140             }
       
   141         }
       
   142         $text_encode = '';
       
   143         for ($x = 0, $_length = strlen($text); $x < $_length; $x++) {
       
   144             $text_encode .= '&#x' . bin2hex($text[$x]) . ';';
       
   145         }
       
   146 
       
   147         $mailto = "&#109;&#97;&#105;&#108;&#116;&#111;&#58;";
       
   148 
       
   149         return '<a href="' . $mailto . $address_encode . '" ' . $extra . '>' . $text_encode . '</a>';
       
   150     } else {
       
   151         // no encoding
       
   152         return '<a href="mailto:' . $address . '" ' . $extra . '>' . $text . '</a>';
       
   153     }
       
   154 }