376 lines
11 KiB
PHP
376 lines
11 KiB
PHP
<?php
|
|
/* vim: set expandtab sw=4 ts=4 sts=4: */
|
|
/**
|
|
* This class includes various sanitization methods that can be called statically
|
|
*
|
|
* @package PhpMyAdmin
|
|
*/
|
|
namespace PMA\libraries;
|
|
|
|
use PMA\libraries\Util;
|
|
|
|
/**
|
|
* This class includes various sanitization methods that can be called statically
|
|
*
|
|
* @package PhpMyAdmin
|
|
*/
|
|
class Sanitize
|
|
{
|
|
/**
|
|
* Checks whether given link is valid
|
|
*
|
|
* @param string $url URL to check
|
|
*
|
|
* @return boolean True if string can be used as link
|
|
*/
|
|
public static function checkLink($url)
|
|
{
|
|
$valid_starts = array(
|
|
'http://',
|
|
'https://',
|
|
'./url.php?url=http%3A%2F%2F',
|
|
'./url.php?url=https%3A%2F%2F',
|
|
'./doc/html/',
|
|
);
|
|
if (defined('PMA_SETUP')) {
|
|
$valid_starts[] = '?page=form&';
|
|
$valid_starts[] = '?page=servers&';
|
|
}
|
|
foreach ($valid_starts as $val) {
|
|
if (mb_substr($url, 0, mb_strlen($val)) == $val) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Callback function for replacing [a@link@target] links in bb code.
|
|
*
|
|
* @param array $found Array of preg matches
|
|
*
|
|
* @return string Replaced string
|
|
*/
|
|
public static function replaceBBLink($found)
|
|
{
|
|
/* Check for valid link */
|
|
if (! Sanitize::checkLink($found[1])) {
|
|
return $found[0];
|
|
}
|
|
/* a-z and _ allowed in target */
|
|
if (! empty($found[3]) && preg_match('/[^a-z_]+/i', $found[3])) {
|
|
return $found[0];
|
|
}
|
|
|
|
/* Construct target */
|
|
$target = '';
|
|
if (! empty($found[3])) {
|
|
$target = ' target="' . $found[3] . '"';
|
|
}
|
|
|
|
/* Construct url */
|
|
if (substr($found[1], 0, 4) == 'http') {
|
|
$url = PMA_linkURL($found[1]);
|
|
} else {
|
|
$url = $found[1];
|
|
}
|
|
|
|
return '<a href="' . $url . '"' . $target . '>';
|
|
}
|
|
|
|
/**
|
|
* Callback function for replacing [doc@anchor] links in bb code.
|
|
*
|
|
* @param array $found Array of preg matches
|
|
*
|
|
* @return string Replaced string
|
|
*/
|
|
public static function replaceDocLink($found)
|
|
{
|
|
if (count($found) >= 4) {
|
|
$page = $found[1];
|
|
$anchor = $found[3];
|
|
} else {
|
|
$anchor = $found[1];
|
|
if (strncmp('faq', $anchor, 3) == 0) {
|
|
$page = 'faq';
|
|
} else if (strncmp('cfg', $anchor, 3) == 0) {
|
|
$page = 'config';
|
|
} else {
|
|
/* Guess */
|
|
$page = 'setup';
|
|
}
|
|
}
|
|
$link = Util::getDocuLink($page, $anchor);
|
|
return '<a href="' . $link . '" target="documentation">';
|
|
}
|
|
|
|
/**
|
|
* 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 Sanitize::sanitize($foo); ?></p>
|
|
*
|
|
* <a title="<?php echo Sanitize::sanitize($foo, true); ?>">bar</a>
|
|
*
|
|
* @param string $message the message
|
|
* @param boolean $escape whether to escape html in result
|
|
* @param boolean $safe whether string is safe (can keep < and > chars)
|
|
*
|
|
* @return string the sanitized message
|
|
*/
|
|
public static function sanitize($message, $escape = false, $safe = false)
|
|
{
|
|
if (!$safe) {
|
|
$message = strtr($message, array('<' => '<', '>' => '>'));
|
|
}
|
|
|
|
/* Interpret bb code */
|
|
$replace_pairs = array(
|
|
'[em]' => '<em>',
|
|
'[/em]' => '</em>',
|
|
'[strong]' => '<strong>',
|
|
'[/strong]' => '</strong>',
|
|
'[code]' => '<code>',
|
|
'[/code]' => '</code>',
|
|
'[kbd]' => '<kbd>',
|
|
'[/kbd]' => '</kbd>',
|
|
'[br]' => '<br />',
|
|
'[/a]' => '</a>',
|
|
'[/doc]' => '</a>',
|
|
'[sup]' => '<sup>',
|
|
'[/sup]' => '</sup>',
|
|
// used in common.inc.php:
|
|
'[conferr]' => '<iframe src="show_config_errors.php" />',
|
|
// used in libraries/Util.php
|
|
'[dochelpicon]' => Util::getImage('b_help.png', __('Documentation')),
|
|
);
|
|
|
|
$message = strtr($message, $replace_pairs);
|
|
|
|
/* Match links in bb code ([a@url@target], where @target is options) */
|
|
$pattern = '/\[a@([^]"@]*)(@([^]"]*))?\]/';
|
|
|
|
/* Find and replace all links */
|
|
$message = preg_replace_callback($pattern, function($match){
|
|
return Sanitize::replaceBBLink($match);
|
|
}, $message);
|
|
|
|
/* Replace documentation links */
|
|
$message = preg_replace_callback(
|
|
'/\[doc@([a-zA-Z0-9_-]+)(@([a-zA-Z0-9_-]*))?\]/',
|
|
function($match){
|
|
return Sanitize::replaceDocLink($match);
|
|
},
|
|
$message
|
|
);
|
|
|
|
/* Possibly escape result */
|
|
if ($escape) {
|
|
$message = htmlspecialchars($message);
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
|
|
/**
|
|
* Sanitize a filename by removing anything besides legit characters
|
|
*
|
|
* Intended usecase:
|
|
* When using a filename in a Content-Disposition header
|
|
* the value should not contain ; or "
|
|
*
|
|
* When exporting, avoiding generation of an unexpected double-extension file
|
|
*
|
|
* @param string $filename The filename
|
|
* @param boolean $replaceDots Whether to also replace dots
|
|
*
|
|
* @return string the sanitized filename
|
|
*
|
|
*/
|
|
public static function sanitizeFilename($filename, $replaceDots = false)
|
|
{
|
|
$pattern = '/[^A-Za-z0-9_';
|
|
// if we don't have to replace dots
|
|
if (! $replaceDots) {
|
|
// then add the dot to the list of legit characters
|
|
$pattern .= '.';
|
|
}
|
|
$pattern .= '-]/';
|
|
$filename = preg_replace($pattern, '_', $filename);
|
|
return $filename;
|
|
}
|
|
|
|
/**
|
|
* Format a string so it can be a string inside JavaScript code inside an
|
|
* eventhandler (onclick, onchange, on..., ).
|
|
* This function is used to displays a javascript confirmation box for
|
|
* "DROP/DELETE/ALTER" queries.
|
|
*
|
|
* @param string $a_string the string to format
|
|
* @param boolean $add_backquotes whether to add backquotes to the string or not
|
|
*
|
|
* @return string the formatted string
|
|
*
|
|
* @access public
|
|
*/
|
|
public static function jsFormat($a_string = '', $add_backquotes = true)
|
|
{
|
|
if (is_string($a_string)) {
|
|
$a_string = htmlspecialchars($a_string);
|
|
$a_string = Sanitize::escapeJsString($a_string);
|
|
// Needed for inline javascript to prevent some browsers
|
|
// treating it as a anchor
|
|
$a_string = str_replace('#', '\\#', $a_string);
|
|
}
|
|
|
|
return $add_backquotes
|
|
? Util::backquote($a_string)
|
|
: $a_string;
|
|
} // end of the 'Sanitize::jsFormat()' function
|
|
|
|
/**
|
|
* escapes a string to be inserted as string a JavaScript block
|
|
* enclosed by <![CDATA[ ... ]]>
|
|
* this requires only to escape ' with \' and end of script block
|
|
*
|
|
* We also remove NUL byte as some browsers (namely MSIE) ignore it and
|
|
* inserting it anywhere inside </script would allow to bypass this check.
|
|
*
|
|
* @param string $string the string to be escaped
|
|
*
|
|
* @return string the escaped string
|
|
*/
|
|
public static function escapeJsString($string)
|
|
{
|
|
return preg_replace(
|
|
'@</script@i', '</\' + \'script',
|
|
strtr(
|
|
$string,
|
|
array(
|
|
"\000" => '',
|
|
'\\' => '\\\\',
|
|
'\'' => '\\\'',
|
|
'"' => '\"',
|
|
"\n" => '\n',
|
|
"\r" => '\r'
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Formats a value for javascript code.
|
|
*
|
|
* @param string $value String to be formatted.
|
|
*
|
|
* @return string formatted value.
|
|
*/
|
|
public static function formatJsVal($value)
|
|
{
|
|
if (is_bool($value)) {
|
|
if ($value) {
|
|
return 'true';
|
|
}
|
|
|
|
return 'false';
|
|
}
|
|
|
|
if (is_int($value)) {
|
|
return (int)$value;
|
|
}
|
|
|
|
return '"' . Sanitize::escapeJsString($value) . '"';
|
|
}
|
|
|
|
/**
|
|
* Formats an javascript assignment with proper escaping of a value
|
|
* and support for assigning array of strings.
|
|
*
|
|
* @param string $key Name of value to set
|
|
* @param mixed $value Value to set, can be either string or array of strings
|
|
* @param bool $escape Whether to escape value or keep it as it is
|
|
* (for inclusion of js code)
|
|
*
|
|
* @return string Javascript code.
|
|
*/
|
|
public static function getJsValue($key, $value, $escape = true)
|
|
{
|
|
$result = $key . ' = ';
|
|
if (!$escape) {
|
|
$result .= $value;
|
|
} elseif (is_array($value)) {
|
|
$result .= '[';
|
|
foreach ($value as $val) {
|
|
$result .= Sanitize::formatJsVal($val) . ",";
|
|
}
|
|
$result .= "];\n";
|
|
} else {
|
|
$result .= Sanitize::formatJsVal($value) . ";\n";
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Prints an javascript assignment with proper escaping of a value
|
|
* and support for assigning array of strings.
|
|
*
|
|
* @param string $key Name of value to set
|
|
* @param mixed $value Value to set, can be either string or array of strings
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function printJsValue($key, $value)
|
|
{
|
|
echo Sanitize::getJsValue($key, $value);
|
|
}
|
|
|
|
/**
|
|
* Formats javascript assignment for form validation api
|
|
* with proper escaping of a value.
|
|
*
|
|
* @param string $key Name of value to set
|
|
* @param string $value Value to set
|
|
* @param boolean $addOn Check if $.validator.format is required or not
|
|
* @param boolean $comma Check if comma is required
|
|
*
|
|
* @return string Javascript code.
|
|
*/
|
|
public static function getJsValueForFormValidation($key, $value, $addOn, $comma)
|
|
{
|
|
$result = $key . ': ';
|
|
if ($addOn) {
|
|
$result .= '$.validator.format(';
|
|
}
|
|
$result .= Sanitize::formatJsVal($value);
|
|
if ($addOn) {
|
|
$result .= ')';
|
|
}
|
|
if ($comma) {
|
|
$result .= ', ';
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Prints javascript assignment for form validation api
|
|
* with proper escaping of a value.
|
|
*
|
|
* @param string $key Name of value to set
|
|
* @param string $value Value to set
|
|
* @param boolean $addOn Check if $.validator.format is required or not
|
|
* @param boolean $comma Check if comma is required
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function printJsValueForFormValidation($key, $value, $addOn=false, $comma=true)
|
|
{
|
|
echo Sanitize::getJsValueForFormValidation($key, $value, $addOn, $comma);
|
|
}
|
|
}
|