Use PMA_DBI_get_columns in odt export
[phpmyadmin.git] / libraries / sanitizing.lib.php
blob95a6ec087091e5d45801e9a000b369aef629539b
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 * @package phpMyAdmin
7 */
9 /**
10 * Sanitizes $message, taking into account our special codes
11 * for formatting.
13 * If you want to include result in element attribute, you should escape it.
15 * Examples:
17 * <p><?php echo PMA_sanitize($foo); ?></p>
19 * <a title="<?php echo PMA_sanitize($foo, true); ?>">bar</a>
21 * @param string the message
22 * @param boolean whether to escape html in result
24 * @return string the sanitized message
26 * @access public
28 function PMA_sanitize($message, $escape = false, $safe = false)
30 if (!$safe) {
31 $message = strtr($message, array('<' => '&lt;', '>' => '&gt;'));
33 $replace_pairs = array(
34 '[i]' => '<em>', // deprecated by em
35 '[/i]' => '</em>', // deprecated by em
36 '[em]' => '<em>',
37 '[/em]' => '</em>',
38 '[b]' => '<strong>', // deprecated by strong
39 '[/b]' => '</strong>', // deprecated by strong
40 '[strong]' => '<strong>',
41 '[/strong]' => '</strong>',
42 '[tt]' => '<code>', // deprecated by CODE or KBD
43 '[/tt]' => '</code>', // deprecated by CODE or KBD
44 '[code]' => '<code>',
45 '[/code]' => '</code>',
46 '[kbd]' => '<kbd>',
47 '[/kbd]' => '</kbd>',
48 '[br]' => '<br />',
49 '[/a]' => '</a>',
50 '[sup]' => '<sup>',
51 '[/sup]' => '</sup>',
53 $message = strtr($message, $replace_pairs);
55 $pattern = '/\[a@([^"@]*)@([^]"]*)\]/';
57 if (preg_match_all($pattern, $message, $founds, PREG_SET_ORDER)) {
58 $valid_links = array(
59 'http', // default http:// links (and https://)
60 './Do', // ./Documentation
63 foreach ($founds as $found) {
64 // only http... and ./Do... allowed
65 if (! in_array(substr($found[1], 0, 4), $valid_links)) {
66 return $message;
68 // a-z and _ allowed in target
69 if (! empty($found[2]) && preg_match('/[^a-z_]+/i', $found[2])) {
70 return $message;
74 if (substr($found[1], 0, 4) == 'http') {
75 $message = preg_replace($pattern, '<a href="' . PMA_linkURL($found[1]) . '" target="\2">', $message);
76 } else {
77 $message = preg_replace($pattern, '<a href="\1" target="\2">', $message);
81 if ($escape) {
82 $message = htmlspecialchars($message);
85 return $message;