js_escape.lib.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. } else {
  78. return 'false';
  79. }
  80. } elseif (is_int($value)) {
  81. return (int)$value;
  82. } else {
  83. return '"' . PMA_escapeJsString($value) . '"';
  84. }
  85. }
  86. /**
  87. * Formats an javascript assignment with proper escaping of a value
  88. * and support for assigning array of strings.
  89. *
  90. * @param string $key Name of value to set
  91. * @param mixed $value Value to set, can be either string or array of strings
  92. * @param bool $escape Whether to escape value or keep it as it is
  93. * (for inclusion of js code)
  94. *
  95. * @return string Javascript code.
  96. */
  97. function PMA_getJsValue($key, $value, $escape = true)
  98. {
  99. $result = $key . ' = ';
  100. if (!$escape) {
  101. $result .= $value;
  102. } elseif (is_array($value)) {
  103. $result .= '[';
  104. foreach ($value as $val) {
  105. $result .= PMA_formatJsVal($val) . ",";
  106. }
  107. $result .= "];\n";
  108. } else {
  109. $result .= PMA_formatJsVal($value) . ";\n";
  110. }
  111. return $result;
  112. }
  113. /**
  114. * Prints an javascript assignment with proper escaping of a value
  115. * and support for assigning array of strings.
  116. *
  117. * @param string $key Name of value to set
  118. * @param mixed $value Value to set, can be either string or array of strings
  119. *
  120. * @return void
  121. */
  122. function PMA_printJsValue($key, $value)
  123. {
  124. echo PMA_getJsValue($key, $value);
  125. }
  126. ?>