| | 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 | */ |
|---|
| | 22 | function 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 | ?> |