PageRenderTime 23ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/22222/htdocs/db/pma/libraries/sanitizing.lib.php

https://gitlab.com/jmcdowall/transactions
PHP | 195 lines | 104 code | 17 blank | 74 comment | 23 complexity | 5e2d4390abe8021e44b551cad49b6630 MD5 | raw file
  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. /**
  9. * Checks whether given link is valid
  10. *
  11. * @param string $url URL to check
  12. *
  13. * @return boolean True if string can be used as link
  14. */
  15. function PMA_checkLink($url)
  16. {
  17. $valid_starts = array(
  18. 'https://',
  19. './url.php?url=https%3A%2F%2F',
  20. './doc/html/',
  21. );
  22. if (defined('PMA_SETUP')) {
  23. $valid_starts[] = '?page=form&';
  24. $valid_starts[] = '?page=servers&';
  25. }
  26. foreach ($valid_starts as $val) {
  27. if (mb_substr($url, 0, mb_strlen($val)) == $val) {
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33. /**
  34. * Callback function for replacing [a@link@target] links in bb code.
  35. *
  36. * @param array $found Array of preg matches
  37. *
  38. * @return string Replaced string
  39. */
  40. function PMA_replaceBBLink($found)
  41. {
  42. /* Check for valid link */
  43. if (! PMA_checkLink($found[1])) {
  44. return $found[0];
  45. }
  46. /* a-z and _ allowed in target */
  47. if (! empty($found[3]) && preg_match('/[^a-z_]+/i', $found[3])) {
  48. return $found[0];
  49. }
  50. /* Construct target */
  51. $target = '';
  52. if (! empty($found[3])) {
  53. $target = ' target="' . $found[3] . '"';
  54. if ($found[3] == '_blank') {
  55. $target .= ' rel="noopener noreferrer"';
  56. }
  57. }
  58. /* Construct url */
  59. if (substr($found[1], 0, 4) == 'http') {
  60. $url = PMA_linkURL($found[1]);
  61. } else {
  62. $url = $found[1];
  63. }
  64. return '<a href="' . $url . '"' . $target . '>';
  65. }
  66. /**
  67. * Callback function for replacing [doc@anchor] links in bb code.
  68. *
  69. * @param array $found Array of preg matches
  70. *
  71. * @return string Replaced string
  72. */
  73. function PMA_replaceDocLink($found)
  74. {
  75. if (count($found) >= 4) {
  76. $page = $found[1];
  77. $anchor = $found[3];
  78. } else {
  79. $anchor = $found[1];
  80. if (strncmp('faq', $anchor, 3) == 0) {
  81. $page = 'faq';
  82. } else if (strncmp('cfg', $anchor, 3) == 0) {
  83. $page = 'config';
  84. } else {
  85. /* Guess */
  86. $page = 'setup';
  87. }
  88. }
  89. $link = PMA\libraries\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. // used in libraries/Util.php
  133. '[dochelpicon]' => PMA\libraries\Util::getImage('b_help.png', __('Documentation')),
  134. );
  135. $message = strtr($message, $replace_pairs);
  136. /* Match links in bb code ([a@url@target], where @target is options) */
  137. $pattern = '/\[a@([^]"@]*)(@([^]"]*))?\]/';
  138. /* Find and replace all links */
  139. $message = preg_replace_callback($pattern, 'PMA_replaceBBLink', $message);
  140. /* Replace documentation links */
  141. $message = preg_replace_callback(
  142. '/\[doc@([a-zA-Z0-9_-]+)(@([a-zA-Z0-9_-]*))?\]/',
  143. 'PMA_replaceDocLink',
  144. $message
  145. );
  146. /* Possibly escape result */
  147. if ($escape) {
  148. $message = htmlspecialchars($message);
  149. }
  150. return $message;
  151. }
  152. /**
  153. * Sanitize a filename by removing anything besides legit characters
  154. *
  155. * Intended usecase:
  156. * When using a filename in a Content-Disposition header
  157. * the value should not contain ; or "
  158. *
  159. * When exporting, avoiding generation of an unexpected double-extension file
  160. *
  161. * @param string $filename The filename
  162. * @param boolean $replaceDots Whether to also replace dots
  163. *
  164. * @return string the sanitized filename
  165. *
  166. */
  167. function PMA_sanitizeFilename($filename, $replaceDots = false)
  168. {
  169. $pattern = '/[^A-Za-z0-9_';
  170. // if we don't have to replace dots
  171. if (! $replaceDots) {
  172. // then add the dot to the list of legit characters
  173. $pattern .= '.';
  174. }
  175. $pattern .= '-]/';
  176. $filename = preg_replace($pattern, '_', $filename);
  177. return $filename;
  178. }