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 escape modifier plugin
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * Type:     modifier
Chris PeBenito 696b41
 * Name:     escape
Chris PeBenito 696b41
 * Purpose:  Escape the string according to escapement type
Chris PeBenito 696b41
 * @link http://smarty.php.net/manual/en/language.modifier.escape.php
Chris PeBenito 696b41
 *          escape (Smarty online manual)
Chris PeBenito 696b41
 * @param string
Chris PeBenito 696b41
 * @param html|htmlall|url|quotes|hex|hexentity|javascript
Chris PeBenito 696b41
 * @return string
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
function smarty_modifier_escape($string, $esc_type = 'html')
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    switch ($esc_type) {
Chris PeBenito 696b41
        case 'html':
Chris PeBenito 696b41
            return htmlspecialchars($string, ENT_QUOTES);
Chris PeBenito 696b41
Chris PeBenito 696b41
        case 'htmlall':
Chris PeBenito 696b41
            return htmlentities($string, ENT_QUOTES);
Chris PeBenito 696b41
Chris PeBenito 696b41
        case 'url':
Chris PeBenito 696b41
            return rawurlencode($string);
Chris PeBenito 696b41
Chris PeBenito 696b41
        case 'quotes':
Chris PeBenito 696b41
            // escape unescaped single quotes
Chris PeBenito 696b41
            return preg_replace("%(?
Chris PeBenito 696b41
Chris PeBenito 696b41
        case 'hex':
Chris PeBenito 696b41
            // escape every character into hex
Chris PeBenito 696b41
            $return = '';
Chris PeBenito 696b41
            for ($x=0; $x < strlen($string); $x++) {
Chris PeBenito 696b41
                $return .= '%' . bin2hex($string[$x]);
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            return $return;
Chris PeBenito 696b41
            
Chris PeBenito 696b41
        case 'hexentity':
Chris PeBenito 696b41
            $return = '';
Chris PeBenito 696b41
            for ($x=0; $x < strlen($string); $x++) {
Chris PeBenito 696b41
                $return .= '&#x' . bin2hex($string[$x]) . ';';
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            return $return;
Chris PeBenito 696b41
Chris PeBenito 696b41
        case 'decentity':
Chris PeBenito 696b41
            $return = '';
Chris PeBenito 696b41
            for ($x=0; $x < strlen($string); $x++) {
Chris PeBenito 696b41
                $return .= '&#' . ord($string[$x]) . ';';
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            return $return;
Chris PeBenito 696b41
Chris PeBenito 696b41
        case 'javascript':
Chris PeBenito 696b41
            // escape quotes and backslashes, newlines, etc.
Chris PeBenito 696b41
            return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
Chris PeBenito 696b41
            
Chris PeBenito 696b41
        case 'mail':
Chris PeBenito 696b41
            // safe way to display e-mail address on a web page
Chris PeBenito 696b41
            return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string);
Chris PeBenito 696b41
            
Chris PeBenito 696b41
        case 'nonstd':
Chris PeBenito 696b41
           // escape non-standard chars, such as ms document quotes
Chris PeBenito 696b41
           $_res = '';
Chris PeBenito 696b41
           for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
Chris PeBenito 696b41
               $_ord = ord($string{$_i});
Chris PeBenito 696b41
               // non-standard char, escape it
Chris PeBenito 696b41
               if($_ord >= 126){
Chris PeBenito 696b41
                   $_res .= '&#' . $_ord . ';';
Chris PeBenito 696b41
               }
Chris PeBenito 696b41
               else {
Chris PeBenito 696b41
                   $_res .= $string{$_i};
Chris PeBenito 696b41
               }
Chris PeBenito 696b41
           }
Chris PeBenito 696b41
           return $_res;
Chris PeBenito 696b41
Chris PeBenito 696b41
        default:
Chris PeBenito 696b41
            return $string;
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>