/libraries/joomla/filter/filteroutput.php

https://github.com/MaBelleEcole/Main · PHP · 148 lines · 65 code · 11 blank · 72 comment · 13 complexity · 7758814ecf9145539af41f6eaee7f090 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id:output.php 6961 2007-03-15 16:06:53Z tcp $
  4. * @package Joomla.Framework
  5. * @subpackage Filter
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant to the
  9. * GNU General Public License, and as distributed it includes or is derivative
  10. * of works licensed under the GNU General Public License or other free or open
  11. * source software licenses. See COPYRIGHT.php for copyright notices and
  12. * details.
  13. */
  14. defined('JPATH_BASE') or die();
  15. /**
  16. * JFilterOutput
  17. *
  18. * @static
  19. * @package Joomla.Framework
  20. * @subpackage Filter
  21. * @since 1.5
  22. */
  23. class JFilterOutput
  24. {
  25. /**
  26. * Makes an object safe to display in forms
  27. *
  28. * Object parameters that are non-string, array, object or start with underscore
  29. * will be converted
  30. *
  31. * @static
  32. * @param object An object to be parsed
  33. * @param int The optional quote style for the htmlspecialchars function
  34. * @param string|array An optional single field name or array of field names not
  35. * to be parsed (eg, for a textarea)
  36. * @since 1.5
  37. */
  38. function objectHTMLSafe( &$mixed, $quote_style=ENT_QUOTES, $exclude_keys='' )
  39. {
  40. if (is_object( $mixed ))
  41. {
  42. foreach (get_object_vars( $mixed ) as $k => $v)
  43. {
  44. if (is_array( $v ) || is_object( $v ) || $v == NULL || substr( $k, 1, 1 ) == '_' ) {
  45. continue;
  46. }
  47. if (is_string( $exclude_keys ) && $k == $exclude_keys) {
  48. continue;
  49. } else if (is_array( $exclude_keys ) && in_array( $k, $exclude_keys )) {
  50. continue;
  51. }
  52. $mixed->$k = htmlspecialchars( $v, $quote_style, 'UTF-8' );
  53. }
  54. }
  55. }
  56. /**
  57. * This method processes a string and replaces all instances of & with &amp; in links only
  58. *
  59. * @static
  60. * @param string $input String to process
  61. * @return string Processed string
  62. * @since 1.5
  63. */
  64. function linkXHTMLSafe($input)
  65. {
  66. $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';
  67. return preg_replace_callback( "#$regex#i", array('JFilterOutput', '_ampReplaceCallback'), $input );
  68. }
  69. /**
  70. * This method processes a string and replaces all accented UTF-8 characters by unaccented
  71. * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercased.
  72. *
  73. * @static
  74. * @param string $input String to process
  75. * @return string Processed string
  76. * @since 1.5
  77. */
  78. function stringURLSafe($string)
  79. {
  80. //remove any '-' from the string they will be used as concatonater
  81. $str = str_replace('-', ' ', $string);
  82. $lang =& JFactory::getLanguage();
  83. $str = $lang->transliterate($str);
  84. // remove any duplicate whitespace, and ensure all characters are alphanumeric
  85. $str = preg_replace(array('/\s+/','/[^A-Za-z0-9\-]/'), array('-',''), $str);
  86. // lowercase and trim
  87. $str = trim(strtolower($str));
  88. return $str;
  89. }
  90. /**
  91. * Replaces &amp; with & for xhtml compliance
  92. *
  93. * @todo There must be a better way???
  94. *
  95. * @static
  96. * @since 1.5
  97. */
  98. function ampReplace( $text )
  99. {
  100. $text = str_replace( '&&', '*--*', $text );
  101. $text = str_replace( '&#', '*-*', $text );
  102. $text = str_replace( '&amp;', '&', $text );
  103. $text = preg_replace( '|&(?![\w]+;)|', '&amp;', $text );
  104. $text = str_replace( '*-*', '&#', $text );
  105. $text = str_replace( '*--*', '&&', $text );
  106. return $text;
  107. }
  108. /**
  109. * Callback method for replacing & with &amp; in a string
  110. *
  111. * @static
  112. * @param string $m String to process
  113. * @return string Replaced string
  114. * @since 1.5
  115. */
  116. function _ampReplaceCallback( $m )
  117. {
  118. $rx = '&(?!amp;)';
  119. return preg_replace( '#'.$rx.'#', '&amp;', $m[0] );
  120. }
  121. /**
  122. * Cleans text of all formating and scripting code
  123. */
  124. function cleanText ( &$text )
  125. {
  126. $text = preg_replace( "'<script[^>]*>.*?</script>'si", '', $text );
  127. $text = preg_replace( '/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is', '\2 (\1)', $text );
  128. $text = preg_replace( '/<!--.+?-->/', '', $text );
  129. $text = preg_replace( '/{.+?}/', '', $text );
  130. $text = preg_replace( '/&nbsp;/', ' ', $text );
  131. $text = preg_replace( '/&amp;/', ' ', $text );
  132. $text = preg_replace( '/&quot;/', ' ', $text );
  133. $text = strip_tags( $text );
  134. $text = htmlspecialchars( $text );
  135. return $text;
  136. }
  137. }