Revert "Do not assume that DefaultLang is escaped."
[phpmyadmin/madhuracj.git] / libraries / sanitizing.lib.php
blobd17fc50dd92be9f641a17512c7798cfe3ee393ed
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * This is in a separate script because it's called from a number of scripts
6 * @version $Id$
7 * @package phpMyAdmin
8 */
10 /**
11 * Sanitizes $message, taking into account our special codes
12 * for formatting.
14 * If you want to include result in element attribute, you should escape it.
16 * Examples:
18 * <p><?php echo PMA_sanitize($foo); ?></p>
20 * <a title="<?php echo PMA_sanitize($foo, true); ?>">bar</a>
22 * @uses preg_replace()
23 * @uses strtr()
24 * @param string the message
25 * @param boolean whether to escape html in result
27 * @return string the sanitized message
29 * @access public
31 function PMA_sanitize($message, $escape = false)
33 $replace_pairs = array(
34 '<' => '&lt;',
35 '>' => '&gt;',
36 '[i]' => '<em>', // deprecated by em
37 '[/i]' => '</em>', // deprecated by em
38 '[em]' => '<em>',
39 '[/em]' => '</em>',
40 '[b]' => '<strong>', // deprecated by strong
41 '[/b]' => '</strong>', // deprecated by strong
42 '[strong]' => '<strong>',
43 '[/strong]' => '</strong>',
44 '[tt]' => '<code>', // deprecated by CODE or KBD
45 '[/tt]' => '</code>', // deprecated by CODE or KBD
46 '[code]' => '<code>',
47 '[/code]' => '</code>',
48 '[kbd]' => '<kbd>',
49 '[/kbd]' => '</kbd>',
50 '[br]' => '<br />',
51 '[/a]' => '</a>',
52 '[sup]' => '<sup>',
53 '[/sup]' => '</sup>',
55 $message = strtr($message, $replace_pairs);
57 $pattern = '/\[a@([^"@]*)@([^]"]*)\]/';
59 if (preg_match_all($pattern, $message, $founds, PREG_SET_ORDER)) {
60 $valid_links = array(
61 'http', // default http:// links (and https://)
62 './Do', // ./Documentation
65 foreach ($founds as $found) {
66 // only http... and ./Do... allowed
67 if (! in_array(substr($found[1], 0, 4), $valid_links)) {
68 return $message;
70 // a-z and _ allowed in target
71 if (! empty($found[2]) && preg_match('/[^a-z_]+/i', $found[2])) {
72 return $message;
76 $message = preg_replace($pattern, '<a href="\1" target="\2">', $message);
79 if ($escape) {
80 $message = htmlspecialchars($message);
83 return $message;