Completed
Pull Request — master (#628)
by Richard
19:02 queued 04:58
created

Clickable::truncate()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 9.2876

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 3
nop 1
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
ccs 4
cts 9
cp 0.4444
crap 9.2876
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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
 * TextSanitizer filter - clean up HTML text
19
 *
20
 * @category  Sanitizer
21
 * @package   Xoops\Core\Text
22
 * @author    Taiwen Jiang <[email protected]>
23
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 */
27
class Clickable extends FilterAbstract
28
{
29
    /**
30
     * @var array default configuration values
31
     */
32
    protected static $defaultConfiguration = [
33
        'enabled' => true,
34
        'truncate_length' => 60,
35
    ];
36
37
    /**
38
     * Make and URL's in the text clickable links
39
     *
40
     * @param string $text text string to filter
41
     *
42
     * @return mixed
43
     */
44 18
    public function applyFilter($text)
45
    {
46 18
        if (!$this->config['enabled']) {
47
            return $text;
48
        }
49
50 18
        $valid_chars = "a-z0-9\/\-_+=.~!%@?#&;:$\|";
51 18
        $end_chars   = "a-z0-9\/\-_+=~!%@?#&;:$\|";
52
53 18
        $pattern     = "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$valid_chars}]+[{$end_chars}])/i";
54 18
        $text = preg_replace_callback(
55 18
            $pattern,
56
            function ($match) {
57 2
                return $match[1] . "<a href=\"$match[2]://$match[3]\" title=\"$match[2]://$match[3]\""
58 2
                . "rel=\"external\">$match[2]://".$this->truncate($match[3]).'</a>';
59 18
            },
60 18
            $text
61
        );
62
63 18
        $pattern     = "/(^|[^]_a-z0-9-=\"'\/:\.])www\.((([a-zA-Z0-9\-]*\.){1,}){1}([a-zA-Z]{2,6}){1})((\/([a-zA-Z0-9\-\._\?\,\'\/\\+&%\$#\=~])*)*)/i";
64 18
        $text = preg_replace_callback(
65 18
            $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 18
            },
71 18
            $text
72
        );
73
74 18
        $pattern     = "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$valid_chars}]+[{$end_chars}])/i";
75 18
        $text = preg_replace_callback(
76 18
            $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 18
            },
82 18
            $text
83
        );
84
85 18
        $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 18
        $text = preg_replace_callback(
87 18
            $pattern,
88
            function ($match) {
89 1
                return $match[1]. "<a href=\"mailto:$match[2]@$match[3]\" title=\"$match[2]@$match[3]\">"
90 1
                . $this->truncate($match[2] . "@" . $match[3]) . '</a>';
91 18
            },
92 18
            $text
93
        );
94
95
96 18
        return $text;
97
    }
98
99
    /**
100
     * truncate string in context of
101
     *
102
     * @param string $text string to be truncated
103
     *
104
     * @return string
105
     */
106 2
    protected function truncate($text)
107
    {
108 2
        $config = $this->config;
109 2
        if (empty($text) || empty($config['truncate_length']) || mb_strlen($text) < $config['truncate_length']) {
110 2
            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