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