sanitizing.lib.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Checks whether given link is valid
  13. *
  14. * @param string $url URL to check
  15. *
  16. * @return boolean True if string can be used as link
  17. */
  18. function PMA_checkLink($url)
  19. {
  20. $valid_starts = array(
  21. 'http://',
  22. 'https://',
  23. './url.php?url=http%3A%2F%2F',
  24. './url.php?url=https%3A%2F%2F',
  25. './doc/html/',
  26. );
  27. if (defined('PMA_SETUP')) {
  28. $valid_starts[] = '?page=form&';
  29. $valid_starts[] = '?page=servers&';
  30. }
  31. foreach ($valid_starts as $val) {
  32. if (substr($url, 0, strlen($val)) == $val) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38. /**
  39. * Callback function for replacing [a@link@target] links in bb code.
  40. *
  41. * @param array $found Array of preg matches
  42. *
  43. * @return string Replaced string
  44. */
  45. function PMA_replaceBBLink($found)
  46. {
  47. /* Check for valid link */
  48. if (! PMA_checkLink($found[1])) {
  49. return $found[0];
  50. }
  51. /* a-z and _ allowed in target */
  52. if (! empty($found[3]) && preg_match('/[^a-z_]+/i', $found[3])) {
  53. return $found[0];
  54. }
  55. /* Construct target */
  56. $target = '';
  57. if (! empty($found[3])) {
  58. $target = ' target="' . $found[3] . '"';
  59. if ($found[3] == '_blank') {
  60. $target .= ' rel="noopener noreferrer"';
  61. }
  62. }
  63. /* Construct url */
  64. if (substr($found[1], 0, 4) == 'http') {
  65. $url = PMA_linkURL($found[1]);
  66. } else {
  67. $url = $found[1];
  68. }
  69. return '<a href="' . $url . '"' . $target . '>';
  70. }
  71. /**
  72. * Callback function for replacing [doc@anchor] links in bb code.
  73. *
  74. * @param array $found Array of preg matches
  75. *
  76. * @return string Replaced string
  77. */
  78. function PMA_replaceDocLink($found)
  79. {
  80. $anchor = $found[1];
  81. if (strncmp('faq', $anchor, 3) == 0) {
  82. $page = 'faq';
  83. } else if (strncmp('cfg', $anchor, 3) == 0) {
  84. $page = 'cfg';
  85. } else {
  86. /* Guess */
  87. $page = 'setup';
  88. }
  89. $link = PMA_Util::getDocuLink($page, $anchor);
  90. return '<a href="' . $link . '" target="documentation">';
  91. }
  92. /**
  93. * Sanitizes $message, taking into account our special codes
  94. * for formatting.
  95. *
  96. * If you want to include result in element attribute, you should escape it.
  97. *
  98. * Examples:
  99. *
  100. * <p><?php echo PMA_sanitize($foo); ?></p>
  101. *
  102. * <a title="<?php echo PMA_sanitize($foo, true); ?>">bar</a>
  103. *
  104. * @param string $message the message
  105. * @param boolean $escape whether to escape html in result
  106. * @param boolean $safe whether string is safe (can keep < and > chars)
  107. *
  108. * @return string the sanitized message
  109. */
  110. function PMA_sanitize($message, $escape = false, $safe = false)
  111. {
  112. if (!$safe) {
  113. $message = strtr($message, array('<' => '&lt;', '>' => '&gt;'));
  114. }
  115. /* Interpret bb code */
  116. $replace_pairs = array(
  117. '[em]' => '<em>',
  118. '[/em]' => '</em>',
  119. '[strong]' => '<strong>',
  120. '[/strong]' => '</strong>',
  121. '[code]' => '<code>',
  122. '[/code]' => '</code>',
  123. '[kbd]' => '<kbd>',
  124. '[/kbd]' => '</kbd>',
  125. '[br]' => '<br />',
  126. '[/a]' => '</a>',
  127. '[/doc]' => '</a>',
  128. '[sup]' => '<sup>',
  129. '[/sup]' => '</sup>',
  130. // used in common.inc.php:
  131. '[conferr]' => '<iframe src="show_config_errors.php" />',
  132. );
  133. $message = strtr($message, $replace_pairs);
  134. /* Match links in bb code ([a@url@target], where @target is options) */
  135. $pattern = '/\[a@([^]"@]*)(@([^]"]*))?\]/';
  136. /* Find and replace all links */
  137. $message = preg_replace_callback($pattern, 'PMA_replaceBBLink', $message);
  138. /* Replace documentation links */
  139. $message = preg_replace_callback(
  140. '/\[doc@([a-zA-Z0-9_-]+)\]/',
  141. 'PMA_replaceDocLink',
  142. $message
  143. );
  144. /* Possibly escape result */
  145. if ($escape) {
  146. $message = htmlspecialchars($message);
  147. }
  148. return $message;
  149. }
  150. /**
  151. * Sanitize a filename by removing anything besides legit characters
  152. *
  153. * Intended usecase:
  154. * When using a filename in a Content-Disposition header
  155. * the value should not contain ; or "
  156. *
  157. * When exporting, avoiding generation of an unexpected double-extension file
  158. *
  159. * @param string $filename The filename
  160. * @param boolean $replaceDots Whether to also replace dots
  161. *
  162. * @return string the sanitized filename
  163. *
  164. */
  165. function PMA_sanitizeFilename($filename, $replaceDots = false)
  166. {
  167. $pattern = '/[^A-Za-z0-9_';
  168. // if we don't have to replace dots
  169. if (! $replaceDots) {
  170. // then add the dot to the list of legit characters
  171. $pattern .= '.';
  172. }
  173. $pattern .= '-]/';
  174. $filename = preg_replace($pattern, '_', $filename);
  175. return $filename;
  176. }
  177. ?>