PageRenderTime 78ms CodeModel.GetById 46ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/js_escape.lib.php

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