Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
output.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Filter
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * JFilterOutput
14  *
15  * @package Joomla.Platform
16  * @subpackage Filter
17  * @since 11.1
18  */
20 {
21  /**
22  * Makes an object safe to display in forms
23  *
24  * Object parameters that are non-string, array, object or start with underscore
25  * will be converted
26  *
27  * @param object &$mixed An object to be parsed
28  * @param integer $quote_style The optional quote style for the htmlspecialchars function
29  * @param mixed $exclude_keys An optional string single field name or array of field names not
30  * to be parsed (eg, for a textarea)
31  *
32  * @return void
33  *
34  * @since 11.1
35  */
36  public static function objectHTMLSafe(&$mixed, $quote_style = ENT_QUOTES, $exclude_keys = '')
37  {
38  if (is_object($mixed))
39  {
40  foreach (get_object_vars($mixed) as $k => $v)
41  {
42  if (is_array($v) || is_object($v) || $v == null || substr($k, 1, 1) == '_')
43  {
44  continue;
45  }
46 
47  if (is_string($exclude_keys) && $k == $exclude_keys)
48  {
49  continue;
50  }
51  elseif (is_array($exclude_keys) && in_array($k, $exclude_keys))
52  {
53  continue;
54  }
55 
56  $mixed->$k = htmlspecialchars($v, $quote_style, 'UTF-8');
57  }
58  }
59  }
60 
61  /**
62  * This method processes a string and replaces all instances of & with &amp; in links only.
63  *
64  * @param string $input String to process
65  *
66  * @return string Processed string
67  *
68  * @since 11.1
69  */
70  public static function linkXHTMLSafe($input)
71  {
72  $regex = 'href="([^"]*(&(amp;){0})[^"]*)*?"';
73 
74  return preg_replace_callback("#$regex#i", array('JFilterOutput', '_ampReplaceCallback'), $input);
75  }
76 
77  /**
78  * This method processes a string and replaces all accented UTF-8 characters by unaccented
79  * ASCII-7 "equivalents", whitespaces are replaced by hyphens and the string is lowercase.
80  *
81  * @param string $string String to process
82  *
83  * @return string Processed string
84  *
85  * @since 11.1
86  */
87  public static function stringURLSafe($string)
88  {
89  // Remove any '-' from the string since they will be used as concatenaters
90  $str = str_replace('-', ' ', $string);
91 
92  $lang = JFactory::getLanguage();
93  $str = $lang->transliterate($str);
94 
95  // Trim white spaces at beginning and end of alias and make lowercase
96  $str = trim(JString::strtolower($str));
97 
98  // Remove any duplicate whitespace, and ensure all characters are alphanumeric
99  $str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str);
100 
101  // Trim dashes at beginning and end of alias
102  $str = trim($str, '-');
103 
104  return $str;
105  }
106 
107  /**
108  * This method implements unicode slugs instead of transliteration.
109  *
110  * @param string $string String to process
111  *
112  * @return string Processed string
113  *
114  * @since 11.1
115  */
116  public static function stringURLUnicodeSlug($string)
117  {
118  // Replace double byte whitespaces by single byte (East Asian languages)
119  $str = preg_replace('/\xE3\x80\x80/', ' ', $string);
120 
121  // Remove any '-' from the string as they will be used as concatenator.
122  // Would be great to let the spaces in but only Firefox is friendly with this
123 
124  $str = str_replace('-', ' ', $str);
125 
126  // Replace forbidden characters by whitespaces
127  $str = preg_replace('#[:\#\*"@+=;!><&\.%()\]\/\'\\\\|\[]#', "\x20", $str);
128 
129  // Delete all '?'
130  $str = str_replace('?', '', $str);
131 
132  // Trim white spaces at beginning and end of alias and make lowercase
133  $str = trim(JString::strtolower($str));
134 
135  // Remove any duplicate whitespace and replace whitespaces by hyphens
136  $str = preg_replace('#\x20+#', '-', $str);
137 
138  return $str;
139  }
140 
141  /**
142  * Replaces &amp; with & for XHTML compliance
143  *
144  * @param string $text Text to process
145  *
146  * @return string Processed string.
147  *
148  * @since 11.1
149  *
150  * @todo There must be a better way???
151  */
152  public static function ampReplace($text)
153  {
154  $text = str_replace('&&', '*--*', $text);
155  $text = str_replace('&#', '*-*', $text);
156  $text = str_replace('&amp;', '&', $text);
157  $text = preg_replace('|&(?![\w]+;)|', '&amp;', $text);
158  $text = str_replace('*-*', '&#', $text);
159  $text = str_replace('*--*', '&&', $text);
160 
161  return $text;
162  }
163 
164  /**
165  * Callback method for replacing & with &amp; in a string
166  *
167  * @param string $m String to process
168  *
169  * @return string Replaced string
170  *
171  * @since 11.1
172  */
173  public static function _ampReplaceCallback($m)
174  {
175  $rx = '&(?!amp;)';
176 
177  return preg_replace('#' . $rx . '#', '&amp;', $m[0]);
178  }
179 
180  /**
181  * Cleans text of all formatting and scripting code
182  *
183  * @param string &$text Text to clean
184  *
185  * @return string Cleaned text.
186  *
187  * @since 11.1
188  */
189  public static function cleanText(&$text)
190  {
191  $text = preg_replace("'<script[^>]*>.*?</script>'si", '', $text);
192  $text = preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is', '\2 (\1)', $text);
193  $text = preg_replace('/<!--.+?-->/', '', $text);
194  $text = preg_replace('/{.+?}/', '', $text);
195  $text = preg_replace('/&nbsp;/', ' ', $text);
196  $text = preg_replace('/&amp;/', ' ', $text);
197  $text = preg_replace('/&quot;/', ' ', $text);
198  $text = strip_tags($text);
199  $text = htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
200 
201  return $text;
202  }
203 
204  /**
205  * Strip img-tags from string
206  *
207  * @param string $string Sting to be cleaned.
208  *
209  * @return string Cleaned string
210  *
211  * @since 11.1
212  */
213  public static function stripImages($string)
214  {
215  return preg_replace('#(<[/]?img.*>)#U', '', $string);
216  }
217 
218  /**
219  * Strip iframe-tags from string
220  *
221  * @param string $string Sting to be cleaned.
222  *
223  * @return string Cleaned string
224  *
225  * @since 12.2
226  */
227  public static function stripIframes($string)
228  {
229  return preg_replace('#(<[/]?iframe.*>)#U', '', $string);
230  }
231 }