Ticket #574: modifier.escape.patch

File modifier.escape.patch, 2.8 kB (added by sylvain261, 3 months ago)
  • build/manifests/jelix-lib.mn

    old new  
    510510  modifier.truncate.php 
    511511  modifier.wiki.php 
    512512  modifier.wordwrap.php 
     513  modifier.escape.php 
    513514 
    514515cd lib/jelix/plugins/tpl/html 
    515516  function.jlocale.php 
  • build/manifests/jtpl-standalone.mn

    old new  
    2929  modifier.strip.php 
    3030  modifier.truncate.php 
    3131  modifier.wordwrap.php 
     32  modifier.escape.php 
    3233 
    3334sd lib/jelix/plugins/tpl/html 
    3435dd plugins/html 
  • lib/jelix/plugins/tpl/common/modifier.escape.php

    old new  
     1<?php 
     2/** 
     3 * Plugin from smarty project and adapted for jtpl 
     4 * @package    jelix 
     5 * @subpackage jtpl_plugin 
     6 * @author      Njaka MISAHARISON 
     7 * @contributor Sylvain de Vathaire 
     8 * @copyright 2008 Neov 
     9 * @link http://smarty.php.net/ 
     10 * @link http://jelix.org/ 
     11 * @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
     12 */ 
     13 
     14/** 
     15 * Type:     modifier<br> 
     16 * Name:     escape<br> 
     17 * Purpose:  Escape the string according to escapement type 
     18 * @param string 
     19 * @param html|htmlall|url|quotes|hex|hexentity|javascript 
     20 * @return string 
     21 */ 
     22function jtpl_modifier_common_escape($string, $esc_type = 'html') 
     23{ 
     24    switch ($esc_type) { 
     25        case 'html': 
     26            return htmlspecialchars($string, ENT_QUOTES); 
     27 
     28        case 'htmlall': 
     29            return htmlentities($string, ENT_QUOTES); 
     30 
     31        case 'url': 
     32            return urlencode($string); 
     33 
     34        case 'quotes': 
     35            // escape unescaped single quotes 
     36            return preg_replace("%(?<!\\\\)'%", "\\'", $string); 
     37 
     38        case 'hex': 
     39            // escape every character into hex 
     40            $return = ''; 
     41            for ($x=0; $x < strlen($string); $x++) { 
     42                $return .= '%' . bin2hex($string[$x]); 
     43            } 
     44            return $return; 
     45             
     46        case 'hexentity': 
     47            $return = ''; 
     48            for ($x=0; $x < strlen($string); $x++) { 
     49                $return .= '&#x' . bin2hex($string[$x]) . ';'; 
     50            } 
     51            return $return; 
     52 
     53        case 'javascript': 
     54            // escape quotes and backslashes and newlines 
     55            return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n')); 
     56 
     57        default: 
     58            return $string; 
     59    } 
     60} 
     61 
     62 
     63?> 
Download in other formats: Original Format