1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Text\Sanitizer\Extensions;
13:
14: use Xoops\Core\Text\Sanitizer;
15: use Xoops\Core\Text\Sanitizer\FilterAbstract;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26:
27: class Clickable extends FilterAbstract
28: {
29: 30: 31:
32: protected static $defaultConfiguration = [
33: 'enabled' => true,
34: 'truncate_length' => 60,
35: ];
36:
37: 38: 39: 40: 41: 42: 43:
44: public function applyFilter($text)
45: {
46: if (!$this->config['enabled']) {
47: return $text;
48: }
49:
50: $valid_chars = "a-z0-9\/\-_+=.~!%@?#&;:$\|";
51: $end_chars = "a-z0-9\/\-_+=~!%@?#&;:$\|";
52:
53: $pattern = "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$valid_chars}]+[{$end_chars}])/i";
54: $text = preg_replace_callback(
55: $pattern,
56: function ($match) {
57: return $match[1] . "<a href=\"$match[2]://$match[3]\" title=\"$match[2]://$match[3]\""
58: . "rel=\"external\">$match[2]://".$this->truncate($match[3]).'</a>';
59: },
60: $text
61: );
62:
63: $pattern = "/(^|[^]_a-z0-9-=\"'\/:\.])www\.((([a-zA-Z0-9\-]*\.){1,}){1}([a-zA-Z]{2,6}){1})((\/([a-zA-Z0-9\-\._\?\,\'\/\\+&%\$#\=~])*)*)/i";
64: $text = preg_replace_callback(
65: $pattern,
66: function ($match) {
67: return $match[1] ."<a href=\"http://www.$match[2]$match[6]\" "
68: . "title=\"www.$match[2]$match[6]\" rel=\"external\">" .
69: $this->truncate('www.'.$match[2].$match[6]) .'</a>';
70: },
71: $text
72: );
73:
74: $pattern = "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$valid_chars}]+[{$end_chars}])/i";
75: $text = preg_replace_callback(
76: $pattern,
77: function ($match) {
78: return $match[1]."<a href=\"ftp://ftp.$match[2].$match[3]\" "
79: . "title=\"ftp.$match[2].$match[3]\" rel=\"external\">"
80: . $this->truncate('ftp.'.$match[2].$match[3]) .'</a>';
81: },
82: $text
83: );
84:
85: $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";
86: $text = preg_replace_callback(
87: $pattern,
88: function ($match) {
89: return $match[1]. "<a href=\"mailto:$match[2]@$match[3]\" title=\"$match[2]@$match[3]\">"
90: . $this->truncate($match[2] . "@" . $match[3]) . '</a>';
91: },
92: $text
93: );
94:
95:
96: return $text;
97: }
98:
99: 100: 101: 102: 103: 104: 105:
106: protected function truncate($text)
107: {
108: $config = $this->config;
109: if (empty($text) || empty($config['truncate_length']) || mb_strlen($text) < $config['truncate_length']) {
110: return $text;
111: }
112: $len = (((mb_strlen($text) - $config['truncate_length']) - 5) / 2);
113: if ($len < 5) {
114: $ret = mb_substr($text, 0, $len) . ' ... ' . mb_substr($text, -$len);
115: } else {
116: $ret = mb_substr($text, 0, $config['truncate_length']);
117: }
118: return $ret;
119: }
120: }
121: