Total Complexity | 101 |
Total Lines | 995 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like MyTextSanitizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MyTextSanitizer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
133 | class MyTextSanitizer |
||
134 | { |
||
135 | /** |
||
136 | * |
||
137 | * @var array |
||
138 | */ |
||
139 | public $smileys = array(); |
||
140 | |||
141 | /** |
||
142 | */ |
||
143 | public $censorConf; |
||
144 | |||
145 | /** |
||
146 | * |
||
147 | * @var holding reference to text |
||
148 | */ |
||
149 | public $text = ''; |
||
150 | public $patterns = array(); |
||
151 | public $replacements = array(); |
||
152 | |||
153 | //mb------------------------------ |
||
154 | public $callbackPatterns = array(); |
||
155 | public $callbacks = array(); |
||
156 | //mb------------------------------ |
||
157 | |||
158 | public $path_basic; |
||
159 | public $path_plugin; |
||
160 | |||
161 | public $config; |
||
162 | |||
163 | /** |
||
164 | * Constructor of this class |
||
165 | * |
||
166 | * Gets allowed html tags from admin config settings |
||
167 | * <br> should not be allowed since nl2br will be used |
||
168 | * when storing data. |
||
169 | * |
||
170 | * @access private |
||
171 | */ |
||
172 | |||
173 | public function __construct() |
||
174 | { |
||
175 | $this->path_basic = XOOPS_ROOT_PATH . '/class/textsanitizer'; |
||
176 | $this->path_plugin = XOOPS_ROOT_PATH . '/Frameworks/textsanitizer'; |
||
177 | $this->config = $this->loadConfig(); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Enter description here... |
||
182 | * |
||
183 | * @param string $name |
||
184 | * @return array |
||
185 | */ |
||
186 | public function loadConfig($name = null) |
||
187 | { |
||
188 | if (!empty($name)) { |
||
189 | return MyTextSanitizerExtension::loadConfig($name); |
||
190 | } |
||
191 | $config_default = include $this->path_basic . '/config.php'; |
||
192 | $config_custom = array(); |
||
193 | if (file_exists($file = $this->path_basic . '/config.custom.php')) { |
||
194 | $config_custom = include $file; |
||
195 | } |
||
196 | |||
197 | return $this->mergeConfig($config_default, $config_custom); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Enter description here... |
||
202 | * |
||
203 | * @param array $config_default |
||
204 | * @param array $config_custom |
||
205 | * @return unknown |
||
206 | */ |
||
207 | public function mergeConfig($config_default, $config_custom) |
||
208 | { |
||
209 | if (is_array($config_custom)) { |
||
210 | foreach ($config_custom as $key => $val) { |
||
211 | if (isset($config_default[$key]) && is_array($config_default[$key])) { |
||
212 | $config_default[$key] = $this->mergeConfig($config_default[$key], $config_custom[$key]); |
||
213 | } else { |
||
214 | $config_default[$key] = $val; |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | |||
219 | return $config_default; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Access the only instance of this class |
||
224 | * |
||
225 | * @return object |
||
226 | * @static |
||
227 | * @staticvar object |
||
228 | */ |
||
229 | public static function getInstance() |
||
230 | { |
||
231 | static $instance; |
||
232 | if (!isset($instance)) { |
||
233 | $instance = new MyTextSanitizer(); |
||
234 | } |
||
235 | |||
236 | return $instance; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get the smileys |
||
241 | * |
||
242 | * @param bool $isAll TRUE for all smileys, FALSE for smileys with display = 1 |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | public function getSmileys($isAll = true) |
||
247 | { |
||
248 | if (count($this->smileys) == 0) { |
||
249 | /* @var XoopsMySQLDatabase $xoopsDB */ |
||
250 | $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection(); |
||
251 | if ($getsmiles = $xoopsDB->query('SELECT * FROM ' . $xoopsDB->prefix('smiles'))) { |
||
252 | while (false !== ($smiles = $xoopsDB->fetchArray($getsmiles))) { |
||
253 | $this->smileys[] = $smiles; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | if ($isAll) { |
||
258 | return $this->smileys; |
||
259 | } |
||
260 | |||
261 | $smileys = array(); |
||
262 | foreach ($this->smileys as $smile) { |
||
263 | if (empty($smile['display'])) { |
||
264 | continue; |
||
265 | } |
||
266 | $smileys[] = $smile; |
||
267 | } |
||
268 | |||
269 | return $smileys; |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Replace emoticons in the message with smiley images |
||
274 | * |
||
275 | * @param string $message |
||
276 | * @return string |
||
277 | */ |
||
278 | public function smiley($message) |
||
279 | { |
||
280 | $smileys = $this->getSmileys(); |
||
281 | foreach ($smileys as $smile) { |
||
282 | $message = str_replace($smile['code'], '<img class="imgsmile" src="' . XOOPS_UPLOAD_URL . '/' . htmlspecialchars($smile['smile_url']) . '" alt="" />', $message); |
||
283 | } |
||
284 | |||
285 | return $message; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @param $match |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | public function makeClickableCallback01($match) |
||
294 | { |
||
295 | return $match[1] . "<a href=\"$match[2]://$match[3]\" title=\"$match[2]://$match[3]\" rel=\"noopener external\">$match[2]://" . $this->truncate($match[3]) . '</a>'; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @param $match |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function makeClickableCallback02($match) |
||
304 | { |
||
305 | return $match[1] . "<a href=\"http://www.$match[2]$match[6]\" title=\"www.$match[2]$match[6]\" rel=\"noopener external\">" . $this->truncate('www.' . $match[2] . $match[6]) . '</a>'; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param $match |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | public function makeClickableCallback03($match) |
||
314 | { |
||
315 | return $match[1] . "<a href=\"ftp://ftp.$match[2].$match[3]\" title=\"ftp.$match[2].$match[3]\" rel=\"external\">" . $this->truncate('ftp.' . $match[2] . $match[3]) . '</a>'; |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param $match |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | public function makeClickableCallback04($match) |
||
324 | { |
||
325 | return $match[1] . "<a href=\"mailto:$match[2]@$match[3]\" title=\"$match[2]@$match[3]\">" . $this->truncate($match[2] . '@' . $match[3]) . '</a>'; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Make links in the text clickable |
||
330 | * |
||
331 | * @param string $text |
||
332 | * @return string |
||
333 | */ |
||
334 | public function makeClickable(&$text) |
||
335 | { |
||
336 | $text1 = $text; |
||
337 | |||
338 | $valid_chars = "a-z0-9\/\-_+=.~!%@?#&;:$\|"; |
||
339 | $end_chars = "a-z0-9\/\-_+=~!%@?#&;:$\|"; |
||
340 | |||
341 | // $patterns = array(); |
||
342 | // $replacements = array(); |
||
343 | // |
||
344 | // $patterns[] = "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$valid_chars}]+[{$end_chars}])/ei"; |
||
345 | // $replacements[] = "'\\1<a href=\"\\2://\\3\" title=\"\\2://\\3\" rel=\"external\">\\2://'.MyTextSanitizer::truncate( '\\3' ).'</a>'"; |
||
346 | // |
||
347 | // |
||
348 | // $patterns[] = "/(^|[^]_a-z0-9-=\"'\/:\.])www\.((([a-zA-Z0-9\-]*\.){1,}){1}([a-zA-Z]{2,6}){1})((\/([a-zA-Z0-9\-\._\?\,\'\/\\+&%\$#\=~])*)*)/ei"; |
||
349 | // $replacements[] = "'\\1<a href=\"http://www.\\2\\6\" title=\"www.\\2\\6\" rel=\"external\">'.MyTextSanitizer::truncate( 'www.\\2\\6' ).'</a>'"; |
||
350 | // |
||
351 | // $patterns[] = "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$valid_chars}]+[{$end_chars}])/ei"; |
||
352 | // $replacements[] = "'\\1<a href=\"ftp://ftp.\\2.\\3\" title=\"ftp.\\2.\\3\" rel=\"external\">'.MyTextSanitizer::truncate( 'ftp.\\2.\\3' ).'</a>'"; |
||
353 | // |
||
354 | // $patterns[] = "/(^|[^]_a-z0-9-=\"'\/:\.])([-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+)@((?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?)/ei"; |
||
355 | // $replacements[] = "'\\1<a href=\"mailto:\\2@\\3\" title=\"\\2@\\3\">'.MyTextSanitizer::truncate( '\\2@\\3' ).'</a>'"; |
||
356 | // |
||
357 | // $text = preg_replace($patterns, $replacements, $text); |
||
358 | // |
||
359 | //---------------------------------------------------------------------------------- |
||
360 | |||
361 | $pattern = "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$valid_chars}]+[{$end_chars}])/i"; |
||
362 | $text1 = preg_replace_callback($pattern, 'self::makeClickableCallback01', $text1); |
||
363 | |||
364 | $pattern = "/(^|[^]_a-z0-9-=\"'\/:\.])www\.((([a-zA-Z0-9\-]*\.){1,}){1}([a-zA-Z]{2,6}){1})((\/([a-zA-Z0-9\-\._\?\,\'\/\\+&%\$#\=~])*)*)/i"; |
||
365 | $text1 = preg_replace_callback($pattern, 'self::makeClickableCallback02', $text1); |
||
366 | |||
367 | $pattern = "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$valid_chars}]+[{$end_chars}])/i"; |
||
368 | $text1 = preg_replace_callback($pattern, 'self::makeClickableCallback03', $text1); |
||
369 | |||
370 | $pattern = "/(^|[^]_a-z0-9-=\"'\/:\.])([-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+)@((?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?)/i"; |
||
371 | $text1 = preg_replace_callback($pattern, 'self::makeClickableCallback04', $text1); |
||
372 | |||
373 | return $text1; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * MyTextSanitizer::truncate() |
||
378 | * |
||
379 | * @param mixed $text |
||
380 | * @return mixed|string |
||
381 | */ |
||
382 | public function truncate($text) |
||
383 | { |
||
384 | $instance = MyTextSanitizer::getInstance(); |
||
385 | if (empty($text) || empty($instance->config['truncate_length']) || strlen($text) < $instance->config['truncate_length']) { |
||
386 | return $text; |
||
387 | } |
||
388 | $len = floor($instance->config['truncate_length'] / 2); |
||
389 | $ret = substr($text, 0, $len) . ' ... ' . substr($text, 5 - $len); |
||
390 | |||
391 | return $ret; |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Replace XoopsCodes with their equivalent HTML formatting |
||
396 | * |
||
397 | * @param string $text |
||
398 | * @param bool|int $allowimage Allow images in the text? |
||
399 | * On FALSE, uses links to images. |
||
400 | * @return string |
||
401 | */ |
||
402 | public function &xoopsCodeDecode(&$text, $allowimage = 1) |
||
403 | { |
||
404 | $patterns = array(); |
||
405 | $replacements = array(); |
||
406 | $patterns[] = "/\[siteurl=(['\"]?)([^\"'<>]*)\\1](.*)\[\/siteurl\]/sU"; |
||
407 | $replacements[] = '<a href="' . XOOPS_URL . '/\\2" title="">\\3</a>'; |
||
408 | $patterns[] = "/\[url=(['\"]?)(http[s]?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU"; |
||
409 | $replacements[] = '<a href="\\2" rel="noopener external" title="">\\3</a>'; |
||
410 | $patterns[] = "/\[url=(['\"]?)(ftp?:\/\/[^\"'<>]*)\\1](.*)\[\/url\]/sU"; |
||
411 | $replacements[] = '<a href="\\2" rel="external" title="">\\3</a>'; |
||
412 | $patterns[] = "/\[url=(['\"]?)([^'\"<>]*)\\1](.*)\[\/url\]/sU"; |
||
413 | $replacements[] = '<a href="http://\\2" rel="noopener external" title="">\\3</a>'; |
||
414 | $patterns[] = "/\[color=(['\"]?)([a-zA-Z0-9]*)\\1](.*)\[\/color\]/sU"; |
||
415 | $replacements[] = '<span style="color: #\\2;">\\3</span>'; |
||
416 | $patterns[] = "/\[size=(['\"]?)([a-z0-9-]*)\\1](.*)\[\/size\]/sU"; |
||
417 | $replacements[] = '<span style="font-size: \\2;">\\3</span>'; |
||
418 | $patterns[] = "/\[font=(['\"]?)([^;<>\*\(\)\"']*)\\1](.*)\[\/font\]/sU"; |
||
419 | $replacements[] = '<span style="font-family: \\2;">\\3</span>'; |
||
420 | $patterns[] = "/\[email]([^;<>\*\(\)\"']*)\[\/email\]/sU"; |
||
421 | $replacements[] = '<a href="mailto:\\1" title="">\\1</a>'; |
||
422 | |||
423 | $patterns[] = "/\[b](.*)\[\/b\]/sU"; |
||
424 | $replacements[] = '<strong>\\1</strong>'; |
||
425 | $patterns[] = "/\[i](.*)\[\/i\]/sU"; |
||
426 | $replacements[] = '<em>\\1</em>'; |
||
427 | $patterns[] = "/\[u](.*)\[\/u\]/sU"; |
||
428 | $replacements[] = '<span style="text-decoration: underline;">\\1</span>'; |
||
429 | $patterns[] = "/\[d](.*)\[\/d\]/sU"; |
||
430 | $replacements[] = '<del>\\1</del>'; |
||
431 | $patterns[] = "/\[center](.*)\[\/center\]/sU"; |
||
432 | $replacements[] = '<div style="text-align: center;">\\1</div>'; |
||
433 | $patterns[] = "/\[left](.*)\[\/left\]/sU"; |
||
434 | $replacements[] = '<div style="text-align: left;">\\1</div>'; |
||
435 | $patterns[] = "/\[right](.*)\[\/right\]/sU"; |
||
436 | $replacements[] = '<div style="text-align: right;">\\1</div>'; |
||
437 | |||
438 | $this->text = $text; |
||
439 | $this->patterns = $patterns; |
||
440 | $this->replacements = $replacements; |
||
441 | |||
442 | $this->config['allowimage'] = $allowimage; |
||
443 | $this->executeExtensions(); |
||
444 | |||
445 | $text = preg_replace($this->patterns, $this->replacements, $this->text); |
||
446 | //------------------------------------------------------------------------------- |
||
447 | $count = count($this->callbackPatterns); |
||
448 | |||
449 | for ($i = 0; $i < $count; ++$i) { |
||
450 | $text = preg_replace_callback($this->callbackPatterns[$i], $this->callbacks[$i], $text); |
||
451 | } |
||
452 | //------------------------------------------------------------------------------ |
||
453 | $text = $this->quoteConv($text); |
||
454 | |||
455 | return $text; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Convert quote tags |
||
460 | * |
||
461 | * @param string $text |
||
462 | * @return string |
||
463 | */ |
||
464 | public function quoteConv($text) |
||
465 | { |
||
466 | //look for both open and closing tags in the correct order |
||
467 | $pattern = "/\[quote](.*)\[\/quote\]/sU"; |
||
468 | $replacement = _QUOTEC . '<div class="xoopsQuote"><blockquote>\\1</blockquote></div>'; |
||
469 | |||
470 | $text = preg_replace($pattern, $replacement, $text, -1, $count); |
||
471 | //no more matches, return now |
||
472 | if (!$count) { |
||
473 | return $text; |
||
474 | } |
||
475 | |||
476 | //new matches could have been created, keep doing it until we have no matches |
||
477 | return $this->quoteConv($text); |
||
478 | } |
||
479 | |||
480 | /** |
||
481 | * A quick solution for filtering XSS scripts |
||
482 | * |
||
483 | * @TODO : To be improved |
||
484 | * @param $text |
||
485 | * @return mixed |
||
486 | */ |
||
487 | public function filterXss($text) |
||
488 | { |
||
489 | $patterns = array(); |
||
490 | $replacements = array(); |
||
491 | $text = str_replace("\x00", '', $text); |
||
492 | $c = "[\x01-\x1f]*"; |
||
493 | $patterns[] = "/\bj{$c}a{$c}v{$c}a{$c}s{$c}c{$c}r{$c}i{$c}p{$c}t{$c}[\s]*:/si"; |
||
494 | $replacements[] = 'javascript;'; |
||
495 | $patterns[] = "/\ba{$c}b{$c}o{$c}u{$c}t{$c}[\s]*:/si"; |
||
496 | $replacements[] = 'about;'; |
||
497 | $patterns[] = "/\bx{$c}s{$c}s{$c}[\s]*:/si"; |
||
498 | $replacements[] = 'xss;'; |
||
499 | $text = preg_replace($patterns, $replacements, $text); |
||
500 | |||
501 | return $text; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Convert linebreaks to <br> tags |
||
506 | * |
||
507 | * @param string $text |
||
508 | * @return string |
||
509 | */ |
||
510 | public function nl2Br($text) |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Add slashes to the text if magic_quotes_gpc is turned off. |
||
517 | * |
||
518 | * @param string $text |
||
519 | * @return string |
||
520 | */ |
||
521 | public function addSlashes($text) |
||
522 | { |
||
523 | if (!@get_magic_quotes_gpc()) { |
||
528 | } |
||
529 | |||
530 | /** |
||
531 | * Convert special characters to HTML entities |
||
532 | * |
||
533 | * @param string $text string being converted |
||
534 | * @param int|null $quote_style |
||
535 | * @param string $charset character set used in conversion |
||
536 | * @param bool $double_encode |
||
537 | * @return string |
||
538 | */ |
||
539 | public function htmlSpecialChars($text, $quote_style = NULL, $charset = null, $double_encode = true) |
||
540 | { |
||
541 | if ($quote_style === NULL) { |
||
542 | $quote_style = ENT_QUOTES; |
||
543 | } |
||
544 | |||
545 | if (version_compare(phpversion(), '5.2.3', '>=')) { |
||
546 | $text = htmlspecialchars($text, $quote_style, $charset ?: (defined('_CHARSET') ? _CHARSET : 'UTF-8'), $double_encode); |
||
547 | } else { |
||
548 | $text = htmlspecialchars($text, $quote_style); |
||
549 | } |
||
550 | |||
551 | return preg_replace(array('/&/i', '/ /i'), array('&', '&nbsp;'), $text); |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Reverses {@link htmlSpecialChars()} |
||
556 | * |
||
557 | * @param string $text |
||
558 | * @return string |
||
559 | */ |
||
560 | public function undoHtmlSpecialChars($text) |
||
561 | { |
||
562 | return preg_replace(array('/>/i', '/</i', '/"/i', '/'/i', '/&nbsp;/i'), array('>', '<', '"', '\'', ' '), $text); |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Filters textarea form data in DB for display |
||
567 | * |
||
568 | * @param string $text |
||
569 | * @param bool|int $html allow html? |
||
570 | * @param bool|int $smiley allow smileys? |
||
571 | * @param bool|int $xcode allow xoopscode? |
||
572 | * @param bool|int $image allow inline images? |
||
573 | * @param bool|int $br convert linebreaks? |
||
574 | * @return string |
||
575 | */ |
||
576 | public function &displayTarea($text, $html = 0, $smiley = 1, $xcode = 1, $image = 1, $br = 1) |
||
618 | } |
||
619 | |||
620 | /** |
||
621 | * Filters textarea form data submitted for preview |
||
622 | * |
||
623 | * @param string $text |
||
624 | * @param bool|int $html allow html? |
||
625 | * @param bool|int $smiley allow smileys? |
||
626 | * @param bool|int $xcode allow xoopscode? |
||
627 | * @param bool|int $image allow inline images? |
||
628 | * @param bool|int $br convert linebreaks? |
||
629 | * @return string |
||
630 | */ |
||
631 | public function &previewTarea($text, $html = 0, $smiley = 1, $xcode = 1, $image = 1, $br = 1) |
||
632 | { |
||
633 | $text = $this->stripSlashesGPC($text); |
||
634 | $text =& $this->displayTarea($text, $html, $smiley, $xcode, $image, $br); |
||
635 | |||
636 | return $text; |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * Replaces banned words in a string with their replacements |
||
641 | * |
||
642 | * @param string $text |
||
643 | * @return string |
||
644 | * @deprecated |
||
645 | */ |
||
646 | public function &censorString(&$text) |
||
647 | { |
||
648 | $ret = $this->executeExtension('censor', $text); |
||
649 | if ($ret === false) { |
||
650 | return $text; |
||
651 | } |
||
652 | |||
653 | return $ret; |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * MyTextSanitizer::codePreConv() |
||
658 | * |
||
659 | * @param mixed $text |
||
660 | * @param mixed $xcode |
||
661 | * @return mixed |
||
662 | */ |
||
663 | public function codePreConv($text, $xcode = 1) |
||
664 | { |
||
665 | if ($xcode != 0) { |
||
666 | // $patterns = "/\[code([^\]]*?)\](.*)\[\/code\]/esU"; |
||
667 | // $replacements = "'[code\\1]'.base64_encode('\\2').'[/code]'"; |
||
668 | |||
669 | $patterns = "/\[code([^\]]*?)\](.*)\[\/code\]/sU"; |
||
670 | $text = preg_replace_callback( |
||
671 | $patterns, |
||
672 | function ($matches) { |
||
673 | return '[code'. $matches[1] . ']' . base64_encode($matches[2]) . '[/code]'; |
||
674 | }, |
||
675 | $text |
||
676 | ); |
||
677 | } |
||
678 | |||
679 | return $text; |
||
680 | } |
||
681 | |||
682 | /** |
||
683 | * @param $match |
||
684 | * |
||
685 | * @return string |
||
686 | */ |
||
687 | public function codeConvCallback($match) |
||
688 | { |
||
689 | return '<div class="xoopsCode">' . $this->executeExtension('syntaxhighlight', str_replace('\\\"', '\"', base64_decode($match[2])), $match[1]) . '</div>'; |
||
690 | } |
||
691 | |||
692 | /** |
||
693 | * MyTextSanitizer::codeConv() |
||
694 | * |
||
695 | * @param mixed $text |
||
696 | * @param mixed $xcode |
||
697 | * @return mixed |
||
698 | */ |
||
699 | public function codeConv($text, $xcode = 1) |
||
700 | { |
||
701 | if (empty($xcode)) { |
||
702 | return $text; |
||
703 | } |
||
704 | $patterns = "/\[code([^\]]*?)\](.*)\[\/code\]/sU"; |
||
705 | $text1 = preg_replace_callback($patterns, array($this, 'codeConvCallback'), $text); |
||
706 | |||
707 | return $text1; |
||
708 | } |
||
709 | |||
710 | /** |
||
711 | * MyTextSanitizer::executeExtensions() |
||
712 | * |
||
713 | * @return bool |
||
714 | */ |
||
715 | public function executeExtensions() |
||
716 | { |
||
717 | $extensions = array_filter($this->config['extensions']); |
||
718 | if (empty($extensions)) { |
||
719 | return true; |
||
720 | } |
||
721 | foreach (array_keys($extensions) as $extension) { |
||
722 | $this->executeExtension($extension); |
||
723 | } |
||
724 | return null; |
||
725 | } |
||
726 | |||
727 | /** |
||
728 | * MyTextSanitizer::loadExtension() |
||
729 | * |
||
730 | * @param mixed $name |
||
731 | * @return MyTextSanitizerExtension|false |
||
732 | */ |
||
733 | public function loadExtension($name) |
||
734 | { |
||
735 | if (file_exists($file = $this->path_basic . '/' . $name . '/' . $name . '.php')) { |
||
736 | include_once $file; |
||
737 | } elseif (file_exists($file = $this->path_plugin . '/' . $name . '/' . $name . '.php')) { |
||
738 | include_once $file; |
||
739 | } else { |
||
740 | return false; |
||
741 | } |
||
742 | $class = 'Myts' . ucfirst($name); |
||
743 | if (!class_exists($class)) { |
||
744 | trigger_error("Extension '{$name}' does not exist", E_USER_WARNING); |
||
745 | |||
746 | return false; |
||
747 | } |
||
748 | return new $class($this); |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * MyTextSanitizer::executeExtension() |
||
753 | * |
||
754 | * @param mixed $name |
||
755 | * @return mixed |
||
756 | */ |
||
757 | public function executeExtension($name) |
||
758 | { |
||
759 | $extension = $this->loadExtension($name); |
||
760 | $args = array_slice(func_get_args(), 1); |
||
761 | array_unshift($args, $this); |
||
762 | |||
763 | return call_user_func_array(array($extension, 'load'), $args); |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * Filter out possible malicious text |
||
768 | * kses project at SF could be a good solution to check |
||
769 | * |
||
770 | * @param string $text text to filter |
||
771 | * @param bool $force force filtering |
||
772 | * @return string filtered text |
||
773 | */ |
||
774 | public function textFilter($text, $force = false) |
||
775 | { |
||
776 | $ret = $this->executeExtension('textfilter', $text, $force); |
||
777 | if ($ret === false) { |
||
778 | return $text; |
||
779 | } |
||
780 | |||
781 | return $ret; |
||
782 | } |
||
783 | |||
784 | // #################### Deprecated Methods ###################### |
||
785 | |||
786 | /** |
||
787 | * if magic_quotes_gpc is on, strip back slashes |
||
788 | * |
||
789 | * @param string $text |
||
790 | * @return string |
||
791 | * @deprecated as of XOOPS 2.5.11 and will be removed in next XOOPS version |
||
792 | * |
||
793 | * This remains here until we officially drop support for PHP 5.3 in next release |
||
794 | */ |
||
795 | public function stripSlashesGPC($text) |
||
796 | { |
||
797 | if (@get_magic_quotes_gpc()) { |
||
798 | $text = stripslashes($text); |
||
799 | } |
||
800 | |||
801 | return $text; |
||
802 | } |
||
803 | |||
804 | /** |
||
805 | * MyTextSanitizer::codeSanitizer() |
||
806 | * |
||
807 | * @param mixed $str |
||
808 | * @param mixed $image |
||
809 | * @return mixed|string |
||
810 | * @deprecated will be removed in next XOOPS version |
||
811 | */ |
||
812 | public function codeSanitizer($str, $image = 1) |
||
813 | { |
||
814 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
815 | $str = $this->htmlSpecialChars(str_replace('\"', '"', base64_decode($str))); |
||
816 | $str =& $this->xoopsCodeDecode($str, $image); |
||
817 | |||
818 | return $str; |
||
819 | } |
||
820 | |||
821 | /** |
||
822 | * MyTextSanitizer::sanitizeForDisplay() |
||
823 | * |
||
824 | * @param mixed $text |
||
825 | * @param integer $allowhtml |
||
826 | * @param integer $smiley |
||
827 | * @param mixed $bbcode |
||
828 | * @return mixed|string |
||
829 | * @deprecated will be removed in next XOOPS version |
||
830 | */ |
||
831 | public function sanitizeForDisplay($text, $allowhtml = 0, $smiley = 1, $bbcode = 1) |
||
832 | { |
||
833 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
834 | if ($allowhtml == 0) { |
||
835 | $text = $this->htmlSpecialChars($text); |
||
836 | } else { |
||
837 | // $config =& $GLOBALS['xoopsConfig']; |
||
838 | // $allowed = $config['allowed_html']; |
||
839 | // $text = strip_tags($text, $allowed); |
||
840 | $text = $this->makeClickable($text); |
||
841 | } |
||
842 | if ($smiley == 1) { |
||
843 | $text = $this->smiley($text); |
||
844 | } |
||
845 | if ($bbcode == 1) { |
||
846 | $text =& $this->xoopsCodeDecode($text); |
||
847 | } |
||
848 | $text = $this->nl2Br($text); |
||
849 | |||
850 | return $text; |
||
851 | } |
||
852 | |||
853 | /** |
||
854 | * MyTextSanitizer::sanitizeForPreview() |
||
855 | * |
||
856 | * @param mixed $text |
||
857 | * @param integer $allowhtml |
||
858 | * @param integer $smiley |
||
859 | * @param mixed $bbcode |
||
860 | * @return mixed|string |
||
861 | * @deprecated will be removed in next XOOPS version |
||
862 | */ |
||
863 | public function sanitizeForPreview($text, $allowhtml = 0, $smiley = 1, $bbcode = 1) |
||
864 | { |
||
865 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
866 | $text = $this->oopsStripSlashesGPC($text); |
||
867 | if ($allowhtml == 0) { |
||
868 | $text = $this->htmlSpecialChars($text); |
||
869 | } else { |
||
870 | // $config =& $GLOBALS['xoopsConfig']; |
||
871 | // $allowed = $config['allowed_html']; |
||
872 | // $text = strip_tags($text, $allowed); |
||
873 | $text = $this->makeClickable($text); |
||
874 | } |
||
875 | if ($smiley == 1) { |
||
876 | $text = $this->smiley($text); |
||
877 | } |
||
878 | if ($bbcode == 1) { |
||
879 | $text =& $this->xoopsCodeDecode($text); |
||
880 | } |
||
881 | $text = $this->nl2Br($text); |
||
882 | |||
883 | return $text; |
||
884 | } |
||
885 | |||
886 | /** |
||
887 | * MyTextSanitizer::makeTboxData4Save() |
||
888 | * |
||
889 | * @param mixed $text |
||
890 | * @return string |
||
891 | * @deprecated will be removed in next XOOPS version |
||
892 | */ |
||
893 | public function makeTboxData4Save($text) |
||
894 | { |
||
895 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
896 | |||
897 | // $text = $this->undoHtmlSpecialChars($text); |
||
898 | return $this->addSlashes($text); |
||
899 | } |
||
900 | |||
901 | /** |
||
902 | * MyTextSanitizer::makeTboxData4Show() |
||
903 | * |
||
904 | * @param mixed $text |
||
905 | * @param mixed $smiley |
||
906 | * @return mixed|string |
||
907 | * @deprecated will be removed in next XOOPS version |
||
908 | */ |
||
909 | public function makeTboxData4Show($text, $smiley = 0) |
||
910 | { |
||
911 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
912 | $text = $this->htmlSpecialChars($text); |
||
913 | |||
914 | return $text; |
||
915 | } |
||
916 | |||
917 | /** |
||
918 | * MyTextSanitizer::makeTboxData4Edit() |
||
919 | * |
||
920 | * @param mixed $text |
||
921 | * @return string |
||
922 | * @deprecated will be removed in next XOOPS version |
||
923 | */ |
||
924 | public function makeTboxData4Edit($text) |
||
925 | { |
||
926 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
927 | |||
928 | return $this->htmlSpecialChars($text); |
||
929 | } |
||
930 | |||
931 | /** |
||
932 | * MyTextSanitizer::makeTboxData4Preview() |
||
933 | * |
||
934 | * @param mixed $text |
||
935 | * @param mixed $smiley |
||
936 | * @return mixed|string |
||
937 | * @deprecated will be removed in next XOOPS version |
||
938 | */ |
||
939 | public function makeTboxData4Preview($text, $smiley = 0) |
||
946 | } |
||
947 | |||
948 | /** |
||
949 | * MyTextSanitizer::makeTboxData4PreviewInForm() |
||
950 | * |
||
951 | * @param mixed $text |
||
952 | * @return string |
||
953 | * @deprecated will be removed in next XOOPS version |
||
954 | */ |
||
955 | public function makeTboxData4PreviewInForm($text) |
||
956 | { |
||
957 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
958 | $text = $this->stripSlashesGPC($text); |
||
959 | |||
960 | return $this->htmlSpecialChars($text); |
||
961 | } |
||
962 | |||
963 | /** |
||
964 | * MyTextSanitizer::makeTareaData4Save() |
||
965 | * |
||
966 | * @param mixed $text |
||
967 | * @return string |
||
968 | * @deprecated will be removed in next XOOPS version |
||
969 | */ |
||
970 | public function makeTareaData4Save($text) |
||
971 | { |
||
972 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
973 | |||
974 | return $this->addSlashes($text); |
||
975 | } |
||
976 | |||
977 | /** |
||
978 | * MyTextSanitizer::makeTareaData4Show() |
||
979 | * |
||
980 | * @param mixed $text |
||
981 | * @param integer $html |
||
982 | * @param integer $smiley |
||
983 | * @param mixed $xcode |
||
984 | * @return mixed|string |
||
985 | * @deprecated will be removed in next XOOPS version |
||
986 | */ |
||
987 | public function &makeTareaData4Show(&$text, $html = 1, $smiley = 1, $xcode = 1) |
||
988 | { |
||
989 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
990 | $text =& $this->displayTarea($text, $html, $smiley, $xcode); |
||
991 | |||
992 | return $text; |
||
993 | } |
||
994 | |||
995 | /** |
||
996 | * MyTextSanitizer::makeTareaData4Edit() |
||
997 | * |
||
998 | * @param mixed $text |
||
999 | * @return string |
||
1000 | * @deprecated will be removed in next XOOPS version |
||
1001 | */ |
||
1002 | public function makeTareaData4Edit($text) |
||
1003 | { |
||
1004 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
1005 | |||
1006 | return $this->htmlSpecialChars($text); |
||
1007 | } |
||
1008 | |||
1009 | /** |
||
1010 | * MyTextSanitizer::makeTareaData4Preview() |
||
1011 | * |
||
1012 | * @param mixed $text |
||
1013 | * @param integer $html |
||
1014 | * @param integer $smiley |
||
1015 | * @param mixed $xcode |
||
1016 | * @return mixed|string |
||
1017 | * @deprecated will be removed in next XOOPS version |
||
1018 | */ |
||
1019 | public function &makeTareaData4Preview(&$text, $html = 1, $smiley = 1, $xcode = 1) |
||
1020 | { |
||
1021 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
1022 | $text =& $this->previewTarea($text, $html, $smiley, $xcode); |
||
1023 | |||
1024 | return $text; |
||
1025 | } |
||
1026 | |||
1027 | /** |
||
1028 | * MyTextSanitizer::makeTareaData4PreviewInForm() |
||
1029 | * |
||
1030 | * @param mixed $text |
||
1031 | * @return string |
||
1032 | * @deprecated will be removed in next XOOPS version |
||
1033 | */ |
||
1034 | public function makeTareaData4PreviewInForm($text) |
||
1035 | { |
||
1036 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
1037 | // if magic_quotes_gpc is on, do stipslashes |
||
1038 | $text = $this->stripSlashesGPC($text); |
||
1039 | |||
1040 | return $this->htmlSpecialChars($text); |
||
1041 | } |
||
1042 | |||
1043 | /** |
||
1044 | * MyTextSanitizer::makeTareaData4InsideQuotes() |
||
1045 | * |
||
1046 | * @param mixed $text |
||
1047 | * @return string |
||
1048 | * @deprecated will be removed in next XOOPS version |
||
1049 | */ |
||
1050 | public function makeTareaData4InsideQuotes($text) |
||
1051 | { |
||
1052 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
1053 | |||
1054 | return $this->htmlSpecialChars($text); |
||
1055 | } |
||
1056 | |||
1057 | /** |
||
1058 | * MyTextSanitizer::oopsStripSlashesGPC() |
||
1059 | * |
||
1060 | * @param mixed $text |
||
1061 | * @return string |
||
1062 | * @deprecated will be removed in next XOOPS version |
||
1063 | */ |
||
1064 | public function oopsStripSlashesGPC($text) |
||
1065 | { |
||
1066 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
1067 | |||
1068 | return $this->stripSlashesGPC($text); |
||
1069 | } |
||
1070 | |||
1071 | /** |
||
1072 | * MyTextSanitizer::oopsStripSlashesRT() |
||
1073 | * |
||
1074 | * @param mixed $text |
||
1075 | * @return mixed|string |
||
1076 | * @deprecated will be removed in next XOOPS version |
||
1077 | */ |
||
1078 | public function oopsStripSlashesRT($text) |
||
1079 | { |
||
1080 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
1081 | if (get_magic_quotes_runtime()) { |
||
1082 | $text = stripslashes($text); |
||
1083 | } |
||
1084 | |||
1085 | return $text; |
||
1086 | } |
||
1087 | |||
1088 | /** |
||
1089 | * MyTextSanitizer::oopsAddSlashes() |
||
1090 | * |
||
1091 | * @param mixed $text |
||
1092 | * @return string |
||
1093 | * @deprecated will be removed in next XOOPS version |
||
1094 | */ |
||
1095 | public function oopsAddSlashes($text) |
||
1100 | } |
||
1101 | |||
1102 | /** |
||
1103 | * MyTextSanitizer::oopsHtmlSpecialChars() |
||
1104 | * |
||
1105 | * @param mixed $text |
||
1106 | * @return string |
||
1107 | * @deprecated will be removed in next XOOPS version |
||
1108 | */ |
||
1109 | public function oopsHtmlSpecialChars($text) |
||
1110 | { |
||
1111 | $GLOBALS['xoopsLogger']->addDeprecated(__CLASS__ . '::' . __FUNCTION__ . ' is deprecated'); |
||
1112 | |||
1113 | return $this->htmlSpecialChars($text); |
||
1114 | } |
||
1115 | |||
1116 | /** |
||
1117 | * MyTextSanitizer::oopsNl2Br() |
||
1118 | * |
||
1119 | * @param mixed $text |
||
1120 | * @return string |
||
1121 | * @deprecated will be removed in next XOOPS version |
||
1122 | */ |
||
1123 | public function oopsNl2Br($text) |
||
1128 | } |
||
1129 | } |
||
1130 |