PageRenderTime 31ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/include/Smarty/plugins/modifier.escape.php

https://gitlab.com/tjaafar/SuiteCRM
PHP | 125 lines | 63 code | 15 blank | 47 comment | 3 complexity | aae08c2908d653176d5cdcdae0a8cd97 MD5 | raw file
  1. <?php
  2. /*
  3. Modification information for LGPL compliance
  4. r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
  5. r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
  6. r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
  7. r52439 - 2009-11-12 17:05:52 -0800 (Thu, 12 Nov 2009) - clee - Updated to allow Rich Text Editor to resize and render HTML content on detailview.
  8. r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
  9. r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
  10. r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
  11. r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
  12. r10971 - 2006-01-12 14:58:30 -0800 (Thu, 12 Jan 2006) - chris - Bug 4128: updating Smarty templates to 2.6.11, a version supposedly that plays better with PHP 5.1
  13. r8230 - 2005-10-03 17:47:19 -0700 (Mon, 03 Oct 2005) - majed - Added Sugar_Smarty to the code tree.
  14. */
  15. /**
  16. * Smarty plugin
  17. * @package Smarty
  18. * @subpackage plugins
  19. */
  20. /**
  21. * Smarty escape modifier plugin
  22. *
  23. * Type: modifier<br>
  24. * Name: escape<br>
  25. * Purpose: Escape the string according to escapement type
  26. * @link http://smarty.php.net/manual/en/language.modifier.escape.php
  27. * escape (Smarty online manual)
  28. * @author Monte Ohrt <monte at ohrt dot com>
  29. * @param string
  30. * @param html|htmlall|url|quotes|hex|hexentity|javascript
  31. * @return string
  32. */
  33. function smarty_modifier_escape($string, $esc_type = 'html', $char_set = 'ISO-8859-1')
  34. {
  35. switch ($esc_type) {
  36. case 'html':
  37. return htmlspecialchars($string, ENT_QUOTES, $char_set);
  38. case 'htmlall':
  39. return htmlentities($string, ENT_QUOTES, $char_set);
  40. case 'htmlentitydecode':
  41. return html_entity_decode($string, ENT_QUOTES, $char_set);
  42. case 'url':
  43. return rawurlencode($string);
  44. case 'urlpathinfo':
  45. return str_replace('%2F','/',rawurlencode($string));
  46. case 'quotes':
  47. // escape unescaped single quotes
  48. return preg_replace("%(?<!\\\\)'%", "\\'", $string);
  49. case 'hex':
  50. // escape every character into hex
  51. $return = '';
  52. for ($x=0; $x < strlen($string); $x++) {
  53. $return .= '%' . bin2hex($string[$x]);
  54. }
  55. return $return;
  56. case 'hexentity':
  57. $return = '';
  58. for ($x=0; $x < strlen($string); $x++) {
  59. $return .= '&#x' . bin2hex($string[$x]) . ';';
  60. }
  61. return $return;
  62. case 'decentity':
  63. $return = '';
  64. for ($x=0; $x < strlen($string); $x++) {
  65. $return .= '&#' . ord($string[$x]) . ';';
  66. }
  67. return $return;
  68. case 'javascript':
  69. // escape quotes and backslashes, newlines, etc.
  70. return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
  71. case 'mail':
  72. // safe way to display e-mail address on a web page
  73. return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string);
  74. case 'nonstd':
  75. // escape non-standard chars, such as ms document quotes
  76. $_res = '';
  77. for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++) {
  78. $_ord = ord(substr($string, $_i, 1));
  79. // non-standard char, escape it
  80. if($_ord >= 126){
  81. $_res .= '&#' . $_ord . ';';
  82. }
  83. else {
  84. $_res .= substr($string, $_i, 1);
  85. }
  86. }
  87. return $_res;
  88. default:
  89. return $string;
  90. }
  91. }
  92. /* vim: set expandtab: */
  93. ?>