/home/mjc1/public_html/ej_sql/libraries/sanitizing.lib.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 *
 * @version $Id$
 */

/**
 * Sanitizes $message, taking into account our special codes
 * for formatting.
 *
 * If you want to include result in element attribute, you should escape it.
 *
 * Examples:
 *
 * <p><?php echo PMA_sanitize($foo); ?></p>
 *
 * <a title="<?php echo PMA_sanitize($foo, true); ?>">bar</a>
 *
 * @uses    preg_replace()
 * @uses    strtr()
 * @param   string   the message
 * @param   boolean  whether to escape html in result
 *
 * @return  string   the sanitized message
 *
 * @access  public
 */
function PMA_sanitize($message$escape false)
{
    
$replace_pairs = array(
        
'<'         => '&lt;',
        
'>'         => '&gt;',
        
'[i]'       => '<em>',      // deprecated by em
        
'[/i]'      => '</em>',     // deprecated by em
        
'[em]'      => '<em>',
        
'[/em]'     => '</em>',
        
'[b]'       => '<strong>',  // deprecated by strong
        
'[/b]'      => '</strong>'// deprecated by strong
        
'[strong]'  => '<strong>',
        
'[/strong]' => '</strong>',
        
'[tt]'      => '<code>',    // deprecated by CODE or KBD
        
'[/tt]'     => '</code>',   // deprecated by CODE or KBD
        
'[code]'    => '<code>',
        
'[/code]'   => '</code>',
        
'[kbd]'     => '<kbd>',
        
'[/kbd]'    => '</kbd>',
        
'[br]'      => '<br />',
        
'[/a]'      => '</a>',
        
'[sup]'      => '<sup>',
        
'[/sup]'      => '</sup>',
    );
    
$message strtr($message$replace_pairs);

    
$pattern '/\[a@([^"@]*)@([^]"]*)\]/';

    if (
preg_match_all($pattern$message$foundsPREG_SET_ORDER)) {
        
$valid_links = array(
            
'http',  // default http:// links (and https://)
            
'./Do',  // ./Documentation
        
);

        foreach (
$founds as $found) {
            
// only http... and ./Do... allowed
            
if (! in_array(substr($found[1], 04), $valid_links)) {
                return 
$message;
            }
            
// a-z and _ allowed in target
            
if (! empty($found[2]) && preg_match('/[^a-z_]+/i'$found[2])) {
                return 
$message;
            }
        }

        
$message preg_replace($pattern'<a href="\1" target="\2">'$message);
    }

    if (
$escape) {
        
$message htmlspecialchars($message);
    }

    return 
$message;
}
?>