Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty {mailto} function plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     function
Chris PeBenito 696b41
 * Name:     mailto
Chris PeBenito 696b41
 * Date:     May 21, 2002
Chris PeBenito 696b41
 * Purpose:  automate mailto address link creation, and optionally
Chris PeBenito 696b41
 *           encode them.
Chris PeBenito 696b41
 * Input:
Chris PeBenito 696b41
 *         - address = e-mail address
Chris PeBenito 696b41
 *         - text = (optional) text to display, default is address
Chris PeBenito 696b41
 *         - encode = (optional) can be one of:
Chris PeBenito 696b41
 *                * none : no encoding (default)
Chris PeBenito 696b41
 *                * javascript : encode with javascript
Chris PeBenito 696b41
 *                * javascript_charcode : encode with javascript charcode
Chris PeBenito 696b41
 *                * hex : encode with hexidecimal (no javascript)
Chris PeBenito 696b41
 *         - cc = (optional) address(es) to carbon copy
Chris PeBenito 696b41
 *         - bcc = (optional) address(es) to blind carbon copy
Chris PeBenito 696b41
 *         - subject = (optional) e-mail subject
Chris PeBenito 696b41
 *         - newsgroups = (optional) newsgroup(s) to post to
Chris PeBenito 696b41
 *         - followupto = (optional) address(es) to follow up to
Chris PeBenito 696b41
 *         - extra = (optional) extra tags for the href link
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Examples:
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * {mailto address="me@domain.com"}
Chris PeBenito 696b41
 * {mailto address="me@domain.com" encode="javascript"}
Chris PeBenito 696b41
 * {mailto address="me@domain.com" encode="hex"}
Chris PeBenito 696b41
 * {mailto address="me@domain.com" subject="Hello to you!"}
Chris PeBenito 696b41
 * {mailto address="me@domain.com" cc="you@domain.com,they@domain.com"}
Chris PeBenito 696b41
 * {mailto address="me@domain.com" extra='class="mailto"'}
Chris PeBenito 696b41
 * 
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.function.mailto.php {mailto}
Chris PeBenito 696b41
 *          (Smarty online manual)
Chris PeBenito 696b41
 * @version  1.2
Chris PeBenito 696b41
 * @author   Monte Ohrt <monte at ohrt dot com>
Chris PeBenito 696b41
 * @author   credits to Jason Sweat (added cc, bcc and subject functionality)
Chris PeBenito 696b41
 * @param    array
Chris PeBenito 696b41
 * @param    Smarty
Chris PeBenito 696b41
 * @return   string
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_function_mailto($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    $extra = '';
Chris PeBenito 696b41
Chris PeBenito 696b41
    if (empty($params['address'])) {
Chris PeBenito 696b41
        $smarty->trigger_error("mailto: missing 'address' parameter");
Chris PeBenito 696b41
        return;
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        $address = $params['address'];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $text = $address;
Chris PeBenito 696b41
Chris PeBenito 696b41
    // netscape and mozilla do not decode %40 (@) in BCC field (bug?)
Chris PeBenito 696b41
    // so, don't encode it.
Chris PeBenito 696b41
    $mail_parms = array();
Chris PeBenito 696b41
    foreach ($params as $var=>$value) {
Chris PeBenito 696b41
        switch ($var) {
Chris PeBenito 696b41
            case 'cc':
Chris PeBenito 696b41
            case 'bcc':
Chris PeBenito 696b41
            case 'followupto':
Chris PeBenito 696b41
                if (!empty($value))
Chris PeBenito 696b41
                    $mail_parms[] = $var.'='.str_replace('%40','@',rawurlencode($value));
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
                
Chris PeBenito 696b41
            case 'subject':
Chris PeBenito 696b41
            case 'newsgroups':
Chris PeBenito 696b41
                $mail_parms[] = $var.'='.rawurlencode($value);
Chris PeBenito 696b41
                break;
Chris PeBenito 696b41
Chris PeBenito 696b41
            case 'extra':
Chris PeBenito 696b41
            case 'text':
Chris PeBenito 696b41
                $$var = $value;
Chris PeBenito 696b41
Chris PeBenito 696b41
            default:
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    $mail_parm_vals = '';
Chris PeBenito 696b41
    for ($i=0; $i
Chris PeBenito 696b41
        $mail_parm_vals .= (0==$i) ? '?' : '&';
Chris PeBenito 696b41
        $mail_parm_vals .= $mail_parms[$i];
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
    $address .= $mail_parm_vals;
Chris PeBenito 696b41
Chris PeBenito 696b41
    $encode = (empty($params['encode'])) ? 'none' : $params['encode'];
Chris PeBenito 696b41
    if (!in_array($encode,array('javascript','javascript_charcode','hex','none')) ) {
Chris PeBenito 696b41
        $smarty->trigger_error("mailto: 'encode' parameter must be none, javascript or hex");
Chris PeBenito 696b41
        return;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
    if ($encode == 'javascript' ) {
Chris PeBenito 696b41
        $string = 'document.write(\''.$text.'\');';
Chris PeBenito 696b41
Chris PeBenito 696b41
        $js_encode = '';
Chris PeBenito 696b41
        for ($x=0; $x < strlen($string); $x++) {
Chris PeBenito 696b41
            $js_encode .= '%' . bin2hex($string[$x]);
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        return '<script type="text/javascript">eval(unescape(\''.$js_encode.'\'))</script>';
Chris PeBenito 696b41
Chris PeBenito 696b41
    } elseif ($encode == 'javascript_charcode' ) {
Chris PeBenito 696b41
        $string = ''.$text.'';
Chris PeBenito 696b41
Chris PeBenito 696b41
        for($x = 0, $y = strlen($string); $x < $y; $x++ ) {
Chris PeBenito 696b41
            $ord[] = ord($string[$x]);   
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $_ret = "<script type=\"text/javascript\" language=\"javascript\">\n";
Chris PeBenito 696b41
        $_ret .= "
Chris PeBenito 696b41
        $_ret .= "{document.write(String.fromCharCode(";
Chris PeBenito 696b41
        $_ret .= implode(',',$ord);
Chris PeBenito 696b41
        $_ret .= "))";
Chris PeBenito 696b41
        $_ret .= "}\n";
Chris PeBenito 696b41
        $_ret .= "//-->\n";
Chris PeBenito 696b41
        $_ret .= "</script>\n";
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        return $_ret;
Chris PeBenito 696b41
        
Chris PeBenito 696b41
        
Chris PeBenito 696b41
    } elseif ($encode == 'hex') {
Chris PeBenito 696b41
Chris PeBenito 696b41
        preg_match('!^(.*)(\?.*)$!',$address,$match);
Chris PeBenito 696b41
        if(!empty($match[2])) {
Chris PeBenito 696b41
            $smarty->trigger_error("mailto: hex encoding does not work with extra attributes. Try javascript.");
Chris PeBenito 696b41
            return;
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $address_encode = '';
Chris PeBenito 696b41
        for ($x=0; $x < strlen($address); $x++) {
Chris PeBenito 696b41
            if(preg_match('!\w!',$address[$x])) {
Chris PeBenito 696b41
                $address_encode .= '%' . bin2hex($address[$x]);
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                $address_encode .= $address[$x];
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
        $text_encode = '';
Chris PeBenito 696b41
        for ($x=0; $x < strlen($text); $x++) {
Chris PeBenito 696b41
            $text_encode .= '&#x' . bin2hex($text[$x]).';';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        $mailto = "mailto:";
Chris PeBenito 696b41
        return ''.$text_encode.'';
Chris PeBenito 696b41
Chris PeBenito 696b41
    } else {
Chris PeBenito 696b41
        // no encoding
Chris PeBenito 696b41
        return ''.$text.'';
Chris PeBenito 696b41
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>