~hexmode/+junk/main

« back to all changes in this revision

Viewing changes to install-files/apps/phpmyadmin2.10.1/libraries/js_escape.lib.php

  • Committer: Mark A. Hershberger
  • Date: 2008-01-05 19:38:56 UTC
  • Revision ID: hershberger@spawn-xp-20080105193856-6rnzgwa4nehue3qj
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/* $Id: common.lib.php 9832 2007-01-09 09:50:49Z nijel $ */
 
3
// vim: expandtab sw=4 ts=4 sts=4:
 
4
 
 
5
/**
 
6
 * Javascript escaping functions.
 
7
 *
 
8
 * @author Michal Čihař <michal@cihar.com>
 
9
 * @package phpMyAdmin
 
10
 */
 
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
 * @uses    PMA_escapeJsString()
 
19
 * @uses    PMA_backquote()
 
20
 * @uses    is_string()
 
21
 * @uses    htmlspecialchars()
 
22
 * @uses    str_replace()
 
23
 * @param   string   $a_string          the string to format
 
24
 * @param   boolean  $add_backquotes    whether to add backquotes to the string or not
 
25
 *
 
26
 * @return  string   the formatted string
 
27
 *
 
28
 * @access  public
 
29
 */
 
30
function PMA_jsFormat($a_string = '', $add_backquotes = true)
 
31
{
 
32
        if (is_string($a_string)) {
 
33
                $a_string = htmlspecialchars($a_string);
 
34
                $a_string = PMA_escapeJsString($a_string);
 
35
                /**
 
36
                 * @todo what is this good for?
 
37
                 */
 
38
                $a_string = str_replace('#', '\\#', $a_string);
 
39
        }
 
40
 
 
41
        return (($add_backquotes) ? PMA_backquote($a_string) : $a_string);
 
42
} // end of the 'PMA_jsFormat()' function
 
43
 
 
44
/**
 
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
 
48
 *
 
49
 * @uses    strtr()
 
50
 * @uses    preg_replace()
 
51
 * @param   string  $string the string to be escaped
 
52
 * @return  string  the escaped string
 
53
 */
 
54
function PMA_escapeJsString($string)
 
55
{
 
56
        return preg_replace('@</script@i', '</\' + \'script',
 
57
                                                strtr($string, array(
 
58
                                                                '\\' => '\\\\',
 
59
                                                                '\'' => '\\\'',
 
60
                                                                "\n" => '\n',
 
61
                                                                "\r" => '\r')));
 
62
}
 
63
 
 
64
?>