2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Javascript escaping functions.
6 * @author Michal Čihař <michal@cihar.com>
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.
18 * @uses PMA_escapeJsString()
19 * @uses PMA_backquote()
21 * @uses htmlspecialchars()
23 * @param string $a_string the string to format
24 * @param boolean $add_backquotes whether to add backquotes to the string or not
26 * @return string the formatted string
30 function PMA_jsFormat($a_string = '', $add_backquotes = true)
32 if (is_string($a_string)) {
33 $a_string = htmlspecialchars($a_string);
34 $a_string = PMA_escapeJsString($a_string);
36 * @todo what is this good for?
38 $a_string = str_replace('#', '\\#', $a_string);
41 return (($add_backquotes) ?
PMA_backquote($a_string) : $a_string);
42 } // end of the 'PMA_jsFormat()' function
45 * escapes a string to be inserted as string a JavaScript block
46 * enclosed by <![CDATA[ ... ]]>
47 * this requires only to escape ' with \' and end of script block
49 * We also remove NUL byte as some browsers (namely MSIE) ignore it and
50 * inserting it anywhere inside </script would allow to bypass this check.
53 * @uses preg_replace()
54 * @param string $string the string to be escaped
55 * @return string the escaped string
57 function PMA_escapeJsString($string)
59 return preg_replace('@</script@i', '</\' + \'script',