js_escape.lib.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Javascript escaping functions.
  5. *
  6. * @package PhpMyAdmin
  7. *
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /**
  13. * Format a string so it can be a string inside JavaScript code inside an
  14. * eventhandler (onclick, onchange, on..., ).
  15. * This function is used to displays a javascript confirmation box for
  16. * "DROP/DELETE/ALTER" queries.
  17. *
  18. * @param string $a_string the string to format
  19. * @param boolean $add_backquotes whether to add backquotes to the string or not
  20. *
  21. * @return string the formatted string
  22. *
  23. * @access public
  24. */
  25. function PMA_jsFormat($a_string = '', $add_backquotes = true)
  26. {
  27. if (is_string($a_string)) {
  28. $a_string = htmlspecialchars($a_string);
  29. $a_string = PMA_escapeJsString($a_string);
  30. // Needed for inline javascript to prevent some browsers
  31. // treating it as a anchor
  32. $a_string = str_replace('#', '\\#', $a_string);
  33. }
  34. return (($add_backquotes) ? PMA_Util::backquote($a_string) : $a_string);
  35. } // end of the 'PMA_jsFormat()' function
  36. /**
  37. * escapes a string to be inserted as string a JavaScript block
  38. * enclosed by <![CDATA[ ... ]]>
  39. * this requires only to escape ' with \' and end of script block
  40. *
  41. * We also remove NUL byte as some browsers (namely MSIE) ignore it and
  42. * inserting it anywhere inside </script would allow to bypass this check.
  43. *
  44. * @param string $string the string to be escaped
  45. *
  46. * @return string the escaped string
  47. */
  48. function PMA_escapeJsString($string)
  49. {
  50. return preg_replace(
  51. '@</script@i', '</\' + \'script',
  52. strtr(
  53. $string,
  54. array(
  55. "\000" => '',
  56. '\\' => '\\\\',
  57. '\'' => '\\\'',
  58. '"' => '\"',
  59. "\n" => '\n',
  60. "\r" => '\r'
  61. )
  62. )
  63. );
  64. }
  65. /**
  66. * Formats a value for javascript code.
  67. *
  68. * @param string $value String to be formatted.
  69. *
  70. * @return string formatted value.
  71. */
  72. function PMA_formatJsVal($value)
  73. {
  74. if (is_bool($value)) {
  75. if ($value) {
  76. return 'true';
  77. }
  78. return 'false';
  79. }
  80. if (is_int($value)) {
  81. return (int)$value;
  82. }
  83. return '"' . PMA_escapeJsString($value) . '"';
  84. }
  85. /**
  86. * Formats an javascript assignment with proper escaping of a value
  87. * and support for assigning array of strings.
  88. *
  89. * @param string $key Name of value to set
  90. * @param mixed $value Value to set, can be either string or array of strings
  91. * @param bool $escape Whether to escape value or keep it as it is
  92. * (for inclusion of js code)
  93. *
  94. * @return string Javascript code.
  95. */
  96. function PMA_getJsValue($key, $value, $escape = true)
  97. {
  98. $result = $key . ' = ';
  99. if (!$escape) {
  100. $result .= $value;
  101. } elseif (is_array($value)) {
  102. $result .= '[';
  103. foreach ($value as $val) {
  104. $result .= PMA_formatJsVal($val) . ",";
  105. }
  106. $result .= "];\n";
  107. } else {
  108. $result .= PMA_formatJsVal($value) . ";\n";
  109. }
  110. return $result;
  111. }
  112. /**
  113. * Prints an javascript assignment with proper escaping of a value
  114. * and support for assigning array of strings.
  115. *
  116. * @param string $key Name of value to set
  117. * @param mixed $value Value to set, can be either string or array of strings
  118. *
  119. * @return void
  120. */
  121. function PMA_printJsValue($key, $value)
  122. {
  123. echo PMA_getJsValue($key, $value);
  124. }
  125. ?>