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 Embed extends FilterAbstract |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array default configuration values |
31
|
|
|
*/ |
32
|
|
|
protected static $defaultConfiguration = [ |
33
|
|
|
'enabled' => true, |
34
|
|
|
'cache_time' => 15552000, // 180 days in seconds |
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
|
3 |
|
public function applyFilter($text) |
45
|
|
|
{ |
46
|
3 |
|
if (!$this->config['enabled']) { |
47
|
|
|
return $text; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
$pattern = '/^(https?:\/\/([\p{L}\p{N}]{1,}\.){1,}[\p{L}\p{N}]{2,}[\/\?\.\-_&=:#\p{L}\p{N}]{0,})$/m'; |
51
|
|
|
|
52
|
3 |
|
$text = preg_replace_callback( |
53
|
|
|
$pattern, |
54
|
3 |
|
[$this, 'decorateUrl'], |
55
|
|
|
$text |
56
|
|
|
); |
57
|
|
|
|
58
|
3 |
|
return $text; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* decorate a bare url with the help of embed/embed |
63
|
|
|
* |
64
|
|
|
* @param string $match string to be truncated |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
1 |
|
protected function decorateUrl($match) { |
69
|
1 |
|
$url = $match[1]; |
70
|
1 |
|
$decorated = null; |
|
|
|
|
71
|
1 |
|
$xoops = \Xoops::getInstance(); |
72
|
1 |
|
$md5 = md5($url); |
73
|
1 |
|
$crc = hash("crc32b", $url); |
74
|
1 |
|
$key = ['embed', substr($crc, -2), $md5]; |
75
|
|
|
//$xoops->cache()->delete($key); |
76
|
1 |
|
$decorated = $xoops->cache()->cacheRead( |
77
|
|
|
$key, |
78
|
1 |
|
function ($url) { |
79
|
1 |
|
$return = null; |
80
|
|
|
try { |
81
|
1 |
|
$info = \Embed\Embed::create($url); |
82
|
|
|
} catch (\Exception $e) { |
83
|
|
|
$info = null; |
84
|
|
|
} |
85
|
1 |
|
if (is_object($info)) { |
86
|
1 |
|
$return = $info->code; |
87
|
1 |
|
if (empty($return)) { |
88
|
1 |
|
$return = $this->mediaBox($info->url, $info->image, $info->title, $info->description); |
89
|
|
|
} |
90
|
|
|
} |
91
|
1 |
|
if (empty($return)) { |
92
|
|
|
$return = $url; |
93
|
|
|
} |
94
|
1 |
|
return $return; |
95
|
1 |
|
}, |
96
|
1 |
|
$this->config['cache_time'], |
97
|
|
|
$url |
98
|
|
|
); |
99
|
1 |
|
return $decorated; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
protected function mediaBox($link, $imageSrc, $title, $description) |
103
|
|
|
{ |
104
|
|
|
$htmlTemplate = <<<'EOT' |
105
|
|
|
<div class="media"> |
106
|
|
|
<a class="pull-left" href="%1$s" rel="external"> |
107
|
|
|
<img src="%2$s" class="media-object" style="max-height: 128px; max-width: 128px;"> |
108
|
|
|
</a> |
109
|
|
|
<div class="media-body"> |
110
|
|
|
<h4 class="media-heading">%3$s</h4> |
111
|
|
|
%4$s |
112
|
|
|
</div> |
113
|
1 |
|
</div> |
114
|
|
|
EOT; |
115
|
|
|
|
116
|
1 |
|
if(empty($imageSrc)) { |
117
|
|
|
$imageSrc = \Xoops::getInstance()->url('media/xoops/images/icons/link-ext.svg'); |
118
|
|
|
} |
119
|
1 |
|
$box = sprintf($htmlTemplate, $link, $imageSrc, $title, $description); |
120
|
1 |
|
return $box; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.