PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/quicky/classes/plugins/modifier.escape.php

https://bitbucket.org/seyar/parshin.local
PHP | 58 lines | 56 code | 0 blank | 2 comment | 20 complexity | 9fa6839449874f5aa297528dbfd362b3 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. function quicky_modifier_escape($string, $esc_type = 'html', $char_set = 'ISO-8859-1')
  3. {
  4. if ($esc_type == 'html') {return htmlspecialchars($string,ENT_QUOTES,$char_set);}
  5. if ($esc_type == 'htmlall') {return htmlentities($string,ENT_QUOTES,$char_set);}
  6. if ($esc_type == 'url') {return rawurlencode($string);}
  7. if ($esc_type == 'urlencode') {return urlencode($string);}
  8. if ($esc_type == 'urldecode') {return urldecode($string);}
  9. if ($esc_type == 'urlpathinfo') {return str_replace('%2F','/',rawurlencode($string));}
  10. if ($esc_type == 'quotes')
  11. {
  12. // escape unescaped single quotes
  13. return preg_replace("%(?<!\\\\)'%", "\\'", $string);
  14. }
  15. if ($esc_type == 'hex')
  16. {
  17. // escape every character into hex
  18. $return = '';
  19. for ($x=0; $x < strlen($string); $x++) {$return .= '%' . bin2hex($string[$x]);}
  20. return $return;
  21. }
  22. if ($esc_type == 'hexentity')
  23. {
  24. $return = '';
  25. for ($x=0; $x < strlen($string); $x++) {$return .= '&#x' . bin2hex($string[$x]) . ';';}
  26. return $return;
  27. }
  28. if ($esc_type == 'decentity')
  29. {
  30. $return = '';
  31. for ($x=0; $x < strlen($string); $x++) {$return .= '&#' . ord($string[$x]) . ';'; }
  32. return $return;
  33. }
  34. if ($esc_type == 'javascript')
  35. {
  36. // escape quotes and backslashes, newlines, etc.
  37. return strtr($string, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
  38. }
  39. if ($esc_type == 'mail')
  40. {
  41. // safe way to display e-mail address on a web page
  42. return str_replace(array('@', '.'),array(' [AT] ', ' [DOT] '), $string);
  43. }
  44. if ($esc_type == 'nonstd')
  45. {
  46. // escape non-standard chars, such as ms document quotes
  47. $_res = '';
  48. for($_i = 0, $_len = strlen($string); $_i < $_len; $_i++)
  49. {
  50. $_ord = ord(substr($string, $_i, 1));
  51. // non-standard char, escape it
  52. if ($_ord >= 126){$_res .= '&#' . $_ord . ';';}
  53. else {$_res .= substr($string, $_i, 1);}
  54. }
  55. return $_res;
  56. }
  57. return $string;
  58. }