sanitizing.lib.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. }
  60. /* Construct url */
  61. if (substr($found[1], 0, 4) == 'http') {
  62. $url = PMA_linkURL($found[1]);
  63. } else {
  64. $url = $found[1];
  65. }
  66. return '<a href="' . $url . '"' . $target . '>';
  67. }
  68. /**
  69. * Callback function for replacing [doc@anchor] links in bb code.
  70. *
  71. * @param array $found Array of preg matches
  72. *
  73. * @return string Replaced string
  74. */
  75. function PMA_replaceDocLink($found)
  76. {
  77. $anchor = $found[1];
  78. if (strncmp('faq', $anchor, 3) == 0) {
  79. $page = 'faq';
  80. } else if (strncmp('cfg', $anchor, 3) == 0) {
  81. $page = 'cfg';
  82. } else {
  83. /* Guess */
  84. $page = 'setup';
  85. }
  86. $link = PMA_Util::getDocuLink($page, $anchor);
  87. return '<a href="' . $link . '" target="documentation">';
  88. }
  89. /**
  90. * Sanitizes $message, taking into account our special codes
  91. * for formatting.
  92. *
  93. * If you want to include result in element attribute, you should escape it.
  94. *
  95. * Examples:
  96. *
  97. * <p><?php echo PMA_sanitize($foo); ?></p>
  98. *
  99. * <a title="<?php echo PMA_sanitize($foo, true); ?>">bar</a>
  100. *
  101. * @param string $message the message
  102. * @param boolean $escape whether to escape html in result
  103. * @param boolean $safe whether string is safe (can keep < and > chars)
  104. *
  105. * @return string the sanitized message
  106. */
  107. function PMA_sanitize($message, $escape = false, $safe = false)
  108. {
  109. if (!$safe) {
  110. $message = strtr($message, array('<' => '&lt;', '>' => '&gt;'));
  111. }
  112. /* Interpret bb code */
  113. $replace_pairs = array(
  114. '[em]' => '<em>',
  115. '[/em]' => '</em>',
  116. '[strong]' => '<strong>',
  117. '[/strong]' => '</strong>',
  118. '[code]' => '<code>',
  119. '[/code]' => '</code>',
  120. '[kbd]' => '<kbd>',
  121. '[/kbd]' => '</kbd>',
  122. '[br]' => '<br />',
  123. '[/a]' => '</a>',
  124. '[/doc]' => '</a>',
  125. '[sup]' => '<sup>',
  126. '[/sup]' => '</sup>',
  127. // used in common.inc.php:
  128. '[conferr]' => '<iframe src="show_config_errors.php" />',
  129. );
  130. $message = strtr($message, $replace_pairs);
  131. /* Match links in bb code ([a@url@target], where @target is options) */
  132. $pattern = '/\[a@([^]"@]*)(@([^]"]*))?\]/';
  133. /* Find and replace all links */
  134. $message = preg_replace_callback($pattern, 'PMA_replaceBBLink', $message);
  135. /* Replace documentation links */
  136. $message = preg_replace_callback(
  137. '/\[doc@([a-zA-Z0-9_-]+)\]/',
  138. 'PMA_replaceDocLink',
  139. $message
  140. );
  141. /* Possibly escape result */
  142. if ($escape) {
  143. $message = htmlspecialchars($message);
  144. }
  145. return $message;
  146. }
  147. /**
  148. * Sanitize a filename by removing anything besides legit characters
  149. *
  150. * Intended usecase:
  151. * When using a filename in a Content-Disposition header
  152. * the value should not contain ; or "
  153. *
  154. * When exporting, avoiding generation of an unexpected double-extension file
  155. *
  156. * @param string $filename The filename
  157. * @param boolean $replaceDots Whether to also replace dots
  158. *
  159. * @return string the sanitized filename
  160. *
  161. */
  162. function PMA_sanitizeFilename($filename, $replaceDots = false)
  163. {
  164. $pattern = '/[^A-Za-z0-9_';
  165. // if we don't have to replace dots
  166. if (! $replaceDots) {
  167. // then add the dot to the list of legit characters
  168. $pattern .= '.';
  169. }
  170. $pattern .= '-]/';
  171. $filename = preg_replace($pattern, '_', $filename);
  172. return $filename;
  173. }
  174. ?>