PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ReferenceCode/phpmyadmin/libraries/sanitizing.lib.php

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