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\ExtensionAbstract; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* YouTube extension |
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 YouTube extends ExtensionAbstract |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array default configuration values |
31
|
|
|
*/ |
32
|
|
|
protected static $defaultConfiguration = [ |
33
|
|
|
'enabled' => true, |
34
|
|
|
'template' => '<div class="embed-responsive %4$s"> |
35
|
|
|
<iframe class="embed-responsive-item" width="%2$d" height="%3$d" src="https://www.youtube.com/embed/%1$s" frameborder="0" allowfullscreen></iframe> |
36
|
|
|
</div>', |
37
|
|
|
'width' => "640", |
38
|
|
|
'height' => "385", |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Provide button and javascript code used by the DhtmlTextArea |
43
|
|
|
* |
44
|
|
|
* @param string $textAreaId dom element id |
45
|
|
|
* |
46
|
|
|
* @return string[] editor button as HTML, supporting javascript |
47
|
|
|
*/ |
48
|
1 |
|
public function getDhtmlEditorSupport($textAreaId) |
49
|
|
|
{ |
50
|
1 |
|
$buttonCode = $this->getEditorButtonHtml( |
51
|
1 |
|
$textAreaId, |
52
|
1 |
|
'youtube.gif', |
53
|
1 |
|
\XoopsLocale::YOUTUBE, |
54
|
1 |
|
'xoopsCodeYoutube', |
55
|
1 |
|
\XoopsLocale::YOUTUBE_URL, |
56
|
1 |
|
\XoopsLocale::HEIGHT, |
57
|
1 |
|
\XoopsLocale::WIDTH |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$javascript = <<<EOH |
61
|
|
|
|
62
|
|
|
function xoopsCodeYoutube(id, enterFlashPhrase, enterFlashHeightPhrase, enterFlashWidthPhrase) |
63
|
|
|
{ |
64
|
|
|
var selection = xoopsGetSelect(id); |
65
|
|
|
if (selection.length > 0) { |
66
|
|
|
var text = selection; |
67
|
|
|
} else { |
68
|
|
|
var text = prompt(enterFlashPhrase, ""); |
69
|
|
|
} |
70
|
|
|
var domobj = xoopsGetElementById(id); |
71
|
|
|
if ( text.length > 0 ) { |
72
|
|
|
var text2 = prompt(enterFlashWidthPhrase, "640"); |
73
|
|
|
var text3 = prompt(enterFlashHeightPhrase, "385"); |
74
|
|
|
var result = '[youtube url="' + text + '"'; |
75
|
|
|
if ( text2.length > 0) { |
76
|
|
|
result += ' width="' + text2 + '" height="' + text3 + '"'; |
77
|
|
|
} |
78
|
|
|
result += " /]"; |
79
|
|
|
xoopsInsertText(domobj, result); |
80
|
|
|
} |
81
|
|
|
domobj.focus(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
EOH; |
85
|
|
|
|
86
|
1 |
|
return [$buttonCode, $javascript]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Register extension with the supplied sanitizer instance |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
2 |
|
public function registerExtensionProcessing() |
95
|
|
|
{ |
96
|
2 |
|
$this->shortcodes->addShortcode( |
97
|
2 |
|
'youtube', |
98
|
2 |
|
function ($attributes, $content, $tagName) { |
|
|
|
|
99
|
1 |
|
if (array_key_exists(0, $attributes) && '=' === substr($attributes[0], 0, 1)) { |
100
|
1 |
|
$args = ltrim($attributes[0], '='); |
101
|
1 |
|
list($width, $height) = explode(',', $args); |
102
|
1 |
|
$url = $content; |
103
|
|
|
} else { |
104
|
|
|
$defaults = [ |
105
|
1 |
|
'url' => trim($content), |
106
|
1 |
|
'width' => $this->config['width'], |
107
|
1 |
|
'height' => $this->config['height'], |
108
|
|
|
]; |
109
|
1 |
|
$cleanAttributes = $this->shortcodes->shortcodeAttributes($defaults, $attributes); |
110
|
1 |
|
$url = $cleanAttributes['url']; |
111
|
1 |
|
$width = (int) $cleanAttributes['width']; |
112
|
1 |
|
$height = (int) $cleanAttributes['height']; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// from: http://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match/6382259#6382259 |
116
|
|
|
$youtubeRegex = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)' |
117
|
1 |
|
.'([^"&?/ ]{11})%i'; |
118
|
|
|
|
119
|
1 |
|
if (preg_match($youtubeRegex, $url, $match)) { |
120
|
|
|
$videoId = $match[1]; |
121
|
1 |
|
} elseif (preg_match('%^[^"&?/ ]{11}$%', $url)) { |
122
|
1 |
|
$videoId = $url; |
123
|
|
|
} else { |
124
|
|
|
return ''; // giveup |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
switch ($width) { |
128
|
1 |
|
case 4: |
129
|
|
|
$height = 3; |
130
|
|
|
break; |
131
|
1 |
|
case 16: |
132
|
|
|
$height = 9; |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$aspectRatio = $width/$height; // 16x9 = 1.777777778, 4x3 = 1.333333333 |
137
|
1 |
|
$responsiveAspect = ($aspectRatio < 1.4) ? 'embed-responsive-4by3' : 'embed-responsive-16by9'; |
138
|
1 |
|
if ($width < 17 && $height < 10) { |
139
|
|
|
$scale = (int) 640 / $width; |
140
|
|
|
$width = $width * $scale; |
141
|
|
|
$height = $height * $scale; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
$template = $this->config['template']; |
145
|
1 |
|
$newContent = sprintf($template, $videoId, $width, $height, $responsiveAspect); |
146
|
1 |
|
return $newContent; |
147
|
2 |
|
} |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.