PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/plugins/modifier.escape.php

http://zoop.googlecode.com/
PHP | 111 lines | 78 code | 10 blank | 23 comment | 6 complexity | 4126123f6b6023e9d0b0dd233eaacde9 MD5 | raw file
  1<?php
  2
  3/**
  4* Smarty plugin
  5* 
  6* @package Smarty
  7* @subpackage PluginsModifier
  8*/
  9
 10/**
 11* Smarty escape modifier plugin
 12* 
 13* Type:     modifier<br>
 14* Name:     escape<br>
 15* Purpose:  escape string for output
 16* 
 17* @link http://smarty.php.net/manual/en/language.modifier.count.characters.php count_characters (Smarty online manual)
 18* @author Monte Ohrt <monte at ohrt dot com> 
 19* @param string $string input string
 20* @param string $esc_type escape type
 21* @param string $char_set character set
 22* @return string escaped input string
 23*/
 24function smarty_modifier_escape($string, $esc_type = 'html', $char_set = SMARTY_RESOURCE_CHAR_SET)
 25{
 26    if (!function_exists('mb_str_replace') && function_exists('mb_strlen')) {
 27        // simulate the missing PHP mb_str_replace function
 28        function mb_str_replace($needle, $replacement, $haystack)
 29        {
 30            $needle_len = mb_strlen($needle);
 31            $replacement_len = mb_strlen($replacement);
 32            $pos = mb_strpos($haystack, $needle, 0);
 33            while ($pos !== false) {
 34                $haystack = mb_substr($haystack, 0, $pos) . $replacement
 35                 . mb_substr($haystack, $pos + $needle_len);
 36                $pos = mb_strpos($haystack, $needle, $pos + $replacement_len);
 37            } 
 38            return $haystack;
 39        } 
 40    } 
 41    switch ($esc_type) {
 42        case 'html':
 43            return htmlspecialchars($string, ENT_QUOTES, $char_set);
 44
 45        case 'htmlall':
 46            return htmlentities($string, ENT_QUOTES, $char_set);
 47
 48        case 'url':
 49            return rawurlencode($string);
 50
 51        case 'urlpathinfo':
 52            return str_replace('%2F', '/', rawurlencode($string));
 53
 54        case 'quotes': 
 55            // escape unescaped single quotes
 56            return preg_replace("%(?<!\\\\)'%", "\\'", $string);
 57
 58        case 'hex': 
 59            // escape every character into hex
 60            $return = '';
 61            for ($x = 0; $x < strlen($string); $x++) {
 62                $return .= '%' . bin2hex($string[$x]);
 63            } 
 64            return $return;
 65
 66        case 'hexentity':
 67            $return = '';
 68            for ($x = 0; $x < strlen($string); $x++) {
 69                $return .= '&#x' . bin2hex($string[$x]) . ';';
 70            } 
 71            return $return;
 72
 73        case 'decentity':
 74            $return = '';
 75            for ($x = 0; $x < strlen($string); $x++) {
 76                $return .= '&#' . ord($string[$x]) . ';';
 77            } 
 78            return $return;
 79
 80        case 'javascript': 
 81            // escape quotes and backslashes, newlines, etc.
 82            return strtr($string, array('\\' => '\\\\', "'" => "\\'", '"' => '\\"', "\r" => '\\r', "\n" => '\\n', '</' => '<\/'));
 83
 84        case 'mail': 
 85            // safe way to display e-mail address on a web page
 86            if (function_exists('mb_str_replace')) {
 87                return mb_str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
 88            } else {
 89                return str_replace(array('@', '.'), array(' [AT] ', ' [DOT] '), $string);
 90            } 
 91
 92        case 'nonstd': 
 93            // escape non-standard chars, such as ms document quotes
 94            $_res = '';
 95            for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
 96                $_ord = ord(substr($string, $_i, 1)); 
 97                // non-standard char, escape it
 98                if ($_ord >= 126) {
 99                    $_res .= '&#' . $_ord . ';';
100                } else {
101                    $_res .= substr($string, $_i, 1);
102                } 
103            } 
104            return $_res;
105
106        default:
107            return $string;
108    } 
109} 
110
111?>