PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/filter/filteroutput.php

https://bitbucket.org/joomla/joomla-platform/
PHP | 192 lines | 80 code | 27 blank | 85 comment | 13 complexity | a9de8822e5f6f9a05e3b12fbc413a282 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Filter
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * JFilterOutput
  12. *
  13. * @static
  14. * @package Joomla.Platform
  15. * @subpackage Filter
  16. * @since 11.1
  17. */
  18. class JFilterOutput
  19. {
  20. /**
  21. * Makes an object safe to display in forms
  22. *
  23. * Object parameters that are non-string, array, object or start with underscore
  24. * will be converted
  25. *
  26. * @param object An object to be parsed
  27. * @param int The optional quote style for the htmlspecialchars function
  28. * @param string|array An optional single field name or array of field names not
  29. * to be parsed (eg, for a textarea)
  30. * @since 11.1
  31. */
  32. public static function objectHTMLSafe(&$mixed, $quote_style=ENT_QUOTES, $exclude_keys='')
  33. {
  34. if (is_object($mixed))
  35. {
  36. foreach (get_object_vars($mixed) as $k => $v)
  37. {
  38. if (is_array($v) || is_object($v) || $v == NULL || substr($k, 1, 1) == '_') {
  39. continue;
  40. }
  41. if (is_string($exclude_keys) && $k == $exclude_keys) {
  42. continue;
  43. } else if (is_array($exclude_keys) && in_array($k, $exclude_keys)) {
  44. continue;
  45. }
  46. $mixed->$k = htmlspecialchars($v, $quote_style, 'UTF-8');
  47. }
  48. }
  49. }
  50. /**
  51. * This method processes a string and replaces all instances of & with &amp; in links only.
  52. *
  53. * @static
  54. * @param string $input String to process
  55. *
  56. * @return string Processed string
  57. * @since 11.1
  58. */
  59. public static function linkXHTMLSafe($input)
  60. {
  61. $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';
  62. return preg_replace_callback("#$regex#i", array('JFilterOutput', '_ampReplaceCallback'), $input);
  63. }
  64. /**
  65. * This method processes a string and replaces all accented UTF-8 characters by unaccented
  66. * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercased.
  67. *
  68. * @param string $input String to process
  69. * @return string Processed string
  70. * @since 11.1
  71. */
  72. public static function stringURLSafe($string)
  73. {
  74. //remove any '-' from the string since they will be used as concatenaters
  75. $str = str_replace('-', ' ', $string);
  76. $lang = JFactory::getLanguage();
  77. $str = $lang->transliterate($str);
  78. // Convert certain symbols to letter representation
  79. $str = str_replace(array('&', '"', '<', '>'), array('a', 'q', 'l', 'g'), $str);
  80. // Lowercase and trim
  81. $str = trim(strtolower($str));
  82. // Remove any duplicate whitespace, and ensure all characters are alphanumeric
  83. $str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);
  84. return $str;
  85. }
  86. /**
  87. * This method implements unicode slugs instead of transliteration.
  88. *
  89. * @static
  90. * @param string $input String to process
  91. * @return string Processed string
  92. * @since 11.1
  93. */
  94. public static function stringURLUnicodeSlug($string)
  95. {
  96. // Replace double byte whitespaces by single byte (East Asian languages)
  97. $str = preg_replace('/\xE3\x80\x80/', ' ', $string);
  98. // Remove any '-' from the string as they will be used as concatenator.
  99. // Would be great to let the spaces in but only Firefox is friendly with this
  100. $str = str_replace('-', ' ', $str);
  101. // Replace forbidden characters by whitespaces
  102. $str = preg_replace( '#[:\#\*"@+=;!&\.%()\]\/\'\\\\|\[]#',"\x20", $str );
  103. // Delete all '?'
  104. $str = str_replace('?', '', $str);
  105. // Trim white spaces at beginning and end of alias and make lowercase
  106. $str = trim(JString::strtolower($str));
  107. // Remove any duplicate whitespace and replace whitespaces by hyphens
  108. $str =preg_replace('#\x20+#','-', $str);
  109. return $str;
  110. }
  111. /**
  112. * Replaces &amp; with & for xhtml compliance
  113. *
  114. * @todo There must be a better way???
  115. *
  116. * @static
  117. * @since 11.1
  118. */
  119. public static function ampReplace($text)
  120. {
  121. $text = str_replace('&&', '*--*', $text);
  122. $text = str_replace('&#', '*-*', $text);
  123. $text = str_replace('&amp;', '&', $text);
  124. $text = preg_replace('|&(?![\w]+;)|', '&amp;', $text);
  125. $text = str_replace('*-*', '&#', $text);
  126. $text = str_replace('*--*', '&&', $text);
  127. return $text;
  128. }
  129. /**
  130. * Callback method for replacing & with &amp; in a string
  131. *
  132. * @param string $m String to process
  133. *
  134. * @return string Replaced string
  135. * @since 11.1
  136. */
  137. public static function _ampReplaceCallback($m)
  138. {
  139. $rx = '&(?!amp;)';
  140. return preg_replace('#'.$rx.'#', '&amp;', $m[0]);
  141. }
  142. /**
  143. * Cleans text of all formating and scripting code
  144. */
  145. public static function cleanText (&$text)
  146. {
  147. $text = preg_replace("'<script[^>]*>.*?</script>'si", '', $text);
  148. $text = preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is', '\2 (\1)', $text);
  149. $text = preg_replace('/<!--.+?-->/', '', $text);
  150. $text = preg_replace('/{.+?}/', '', $text);
  151. $text = preg_replace('/&nbsp;/', ' ', $text);
  152. $text = preg_replace('/&amp;/', ' ', $text);
  153. $text = preg_replace('/&quot;/', ' ', $text);
  154. $text = strip_tags($text);
  155. $text = htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
  156. return $text;
  157. }
  158. /**
  159. * Strip img-tags from string
  160. */
  161. public static function stripImages($string)
  162. {
  163. return preg_replace('#(<[/]?img.*>)#U', '', $string);
  164. }
  165. }