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