-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Typo.class.php
329 lines (285 loc) · 9.96 KB
/
Typo.class.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
defined('GX_LIB') or die('Direct Access Not Allowed!');
/**
* GeniXCMS - Content Management System.
*
* PHP Based Content Management System and Framework
*
* @since 0.0.1 build date 20140925
*
* @version 1.1.12
*
* @link https://github.com/semplon/GeniXCMS
*
*
* @author Puguh Wijayanto <metalgenix@gmail.com>
* @copyright 2014-2021 Puguh Wijayanto
* @license http://www.opensource.org/licenses/mit-license.php MIT
*/
/**
* Typo Class.
*
* This class will process text modifier, including sanitizing, slug, strip
* tags, create random characters.
*
* @author Puguh Wijayanto <metalgenix@gmail.com>
*
* @since 0.0.1
*/
class Typo
{
public function __construct()
{
}
public static function cleanX($c)
{
// $c = urldecode($c);
// echo $c;
$val = self::strip_tags_content($c, '<script>', true);
$val = preg_replace_callback(
'#\<pre\>(.+?)\<\/pre\>#',
function($matches) {
return "<pre>".str_replace('"', '"', $matches[1])."</pre>";
},
$val
);
Vendor::loadonce("ezyang/htmlpurifier/library/HTMLPurifier.auto.php");
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
$val = $purifier->purify($val);
// $val = self::filterXss($val);
$val = htmlspecialchars(
$val,
ENT_QUOTES | ENT_HTML5,
'utf-8'
);
$val = str_replace('\\', "\\\\", $val);
// echo $val;
// $val = htmlentities(
// $c,
// ENT_QUOTES | ENT_IGNORE, "UTF-8");
return $val;
}
public static function Xclean($vars)
{
$var = htmlspecialchars_decode($vars, ENT_QUOTES | ENT_HTML5);
// $var = html_entity_decode($vars);
$var = str_replace('\\\\', '\\', $var);
return $var;
}
public static function slugify($text)
{
// strip tags
$text = strip_tags($text);
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
setlocale(LC_CTYPE, Options::v('country').'.utf8');
$text = iconv('utf-8', 'utf-8//TRANSLIT', $text);
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
/**
* Remove Tags.
*
* @link http://php.net/manual/es/function.strip-tags.php#86964
*/
public static function strip($text, $tags = '', $invert = false)
{
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if (is_array($tags) and count($tags) > 0) {
if ($invert == false) {
/*return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); */
$text = preg_replace('@<(?!(?:'.implode('|', $tags).')\b)(\w+)\b.*?>@si', '', $text);
$text = preg_replace('@</(?!(?:'.implode('|', $tags).')\b)(\w+)\b.*?>@si', '', $text);
} else {
/*return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); */
$text = preg_replace('@<('.implode('|', $tags).')\b.*?>@si', '', $text);
$text = preg_replace('@</('.implode('|', $tags).')\b.*?>@si', '', $text);
}
} elseif ($invert == false) {
/*return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); */
$text = preg_replace('@<(\w+)\b.*?>@si', '', $text);
$text = preg_replace('@</(\w+)\b.*?>@si', '', $text);
}
return $text;
}
/**
* Remove Tags and Content inside tags.
*
* @link http://php.net/manual/es/function.strip-tags.php#86964
* @since 0.0.4
*/
public static function strip_tags_content($text, $tags = '', $invert = false)
{
preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
$tags = array_unique($tags[1]);
if (is_array($tags) and count($tags) > 0) {
if ($invert == false) {
return preg_replace('@<(?!(?:'.implode('|', $tags).')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
} else {
return preg_replace('@<('.implode('|', $tags).')\b.*?>.*?</\1>@si', '', $text);
}
} elseif ($invert == false) {
return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
}
return $text;
}
/**
* Cryptography random characters.
*
* @link http://stackoverflow.com/a/13733588
*/
public static function crypto_rand_secure($min, $max)
{
$range = $max - $min;
if ($range < 0) {
return $min;
} // not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd >= $range);
return $min + $rnd;
}
public static function getToken($length)
{
$token = '';
$codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz';
$codeAlphabet .= '0123456789';
for ($i = 0; $i < $length; ++$i) {
$token .= $codeAlphabet[self::crypto_rand_secure(0, strlen($codeAlphabet))];
}
return $token;
}
public static function int($var)
{
$var = sprintf('%d', $var);
return $var;
}
public static function float($var)
{
$var = number_format(sprintf('%2f', $var), 2);
return $var;
}
public static function escape($vars)
{
return Db::escape($vars);
}
/**
* Change New Line (nl) to Paragraph.
*
* @since 0.0.4
*/
public static function nl2p($string, $line_breaks = true, $xml = true)
{
$string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string);
// It is conceivable that people might still want single line-breaks
// without breaking into a new paragraph.
if ($line_breaks == true) {
return '<p>'.preg_replace(
array("/([\n]{2,})/i", "/([^>])\n([^<])/i"),
array("</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'),
trim($string)
).'</p>';
} else {
return '<p>'.preg_replace(
array("/([\n]{2,})/i", "/([\r\n]{3,})/i", "/([^>])\n([^<])/i"),
array("</p>\n<p>", "</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'),
trim($string)
).'</p>';
}
}
public static function url2link($text)
{
// The Regular Expression filter
$reg_exUrl = preg_replace(
'@((https?://)(www\.|[-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@',
'<a href="$1" target="_blank" rel="nofollow">$1</a>',
$text
);
return $reg_exUrl;
}
public static function p2nl($string)
{
$string = str_replace(array('<p>', '<br>', '<br />'), '', $string);
$string = str_replace('</p>', "\n", $string);
return $string;
}
public static function p2br($string)
{
$string = str_replace(array('<p>'), '', $string);
$string = str_replace('</p>', '<br />', $string);
return $string;
}
public static function jsonFormat($var)
{
// $var = self::cleanX($var);
$var = str_replace("\r\n", "\n", $var);
$var = str_replace("\r", "\n", $var);
// // // JSON requires new line characters be escaped
$var = str_replace("\n", '\\n', $var);
$var = str_replace("'", '\\u0027', $var);
// $var = str_replace('"', '\\u0022', $var);
$var = preg_replace_callback(
'/<([^<>]+)>/',
function ($matches) {
return str_replace('"', '\"', $matches[0]);
},
$var
);
// $var = preg_replace_callback(
// '/([^<>]+)/',
// function ($matches) {
// return str_replace("'", ''', $matches[0]);
// },
// $var
// );
$var = str_replace('/>', ' />', $var);
$var = str_replace('</', '<\/', $var);
$var = str_replace('\&', '&', $var);
return $var;
}
public static function jsonDeFormat($var)
{
return mb_convert_encoding($var, 'UTF-8', mb_list_encodings());
}
public static function validateEmail($email)
{
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
return true;
}
else {
return false;
}
}
public static function filterXss($str)
{
// $str = preg_replace('#on.*=["|\'](.*)["|\']#', '', $str);
$str = preg_replace('#(?!<pre>.*?)(onload|onerror|onblur|onchange|onscroll|oninput|
onfocus|onbeforescriptexecute|ontoggle|onratechange|onreadystatechange|onpropertychange|
onqt_error|onpageshow|onclick|onmouseover|onunload|event|formaction|actiontype|background|oncut)=("|\')(.*)("|\')(?!.*?</pre>)#', '', $str);
$str = preg_replace('#(.*?)(javascript:.*)(.*?)#', '', $str);
$str = preg_replace('#(.*?)(onload|onerror|onblur|onchange|onscroll|oninput|
onfocus|onbeforescriptexecute|ontoggle|onratechange|onreadystatechange|onpropertychange|
onqt_error|onpageshow|onclick|onmouseover|onunload|event|formaction|actiontype|background|oncut)=("|\')(.*)("|\')(.*?)#', '', $str);
//$str = preg_replace('#<(.*?)script>#', '', $str);
return $str;
}
}
/* End of file Typo.class.php */
/* Location: ./inc/lib/Typo.class.php */