1: | <?php
|
2: |
|
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: |
|
20: | class MytsYoutube extends MyTextSanitizerExtension
|
21: | {
|
22: | |
23: | |
24: | |
25: | |
26: |
|
27: | public function encode($textarea_id)
|
28: | {
|
29: |
|
30: | $code = "<button type='button' class='btn btn-default btn-sm' onclick='xoopsCodeYoutube(\"{$textarea_id}\",\""
|
31: | . htmlspecialchars(_XOOPS_FORM_ENTERYOUTUBEURL, ENT_QUOTES) . "\",\""
|
32: | . htmlspecialchars(_XOOPS_FORM_ALT_ENTERHEIGHT, ENT_QUOTES) . "\",\""
|
33: | . htmlspecialchars(_XOOPS_FORM_ALT_ENTERWIDTH, ENT_QUOTES)
|
34: | . "\");' onmouseover='style.cursor=\"hand\"' title='" . _XOOPS_FORM_ALTYOUTUBE
|
35: | . "'><span class='fa fa-fw fa-youtube' aria-hidden='true'></span></button>";
|
36: | $javascript = <<<EOH
|
37: | function xoopsCodeYoutube(id, enterYouTubePhrase, enterYouTubeHeightPhrase, enterYouTubeWidthPhrase)
|
38: | {
|
39: | var selection = xoopsGetSelect(id);
|
40: | if (selection.length > 0) {
|
41: | var text = selection;
|
42: | } else {
|
43: | var text = prompt(enterYouTubePhrase, "");
|
44: | if (text === null) {
|
45: | text = '';
|
46: | }
|
47: | }
|
48: | var domobj = xoopsGetElementById(id);
|
49: | if (text.length > 0) {
|
50: | var text2 = prompt(enterYouTubeWidthPhrase, "16x9");
|
51: | var text3 = prompt(enterYouTubeHeightPhrase, "");
|
52: | var result = "[youtube="+text2+","+text3+"]" + text + "[/youtube]";
|
53: | xoopsInsertText(domobj, result);
|
54: | }
|
55: | domobj.focus();
|
56: | }
|
57: | EOH;
|
58: |
|
59: | return array($code, $javascript);
|
60: | }
|
61: |
|
62: | |
63: | |
64: | |
65: | |
66: |
|
67: | public static function myCallback($match)
|
68: | {
|
69: | return self::decode($match[4], $match[2], $match[3]);
|
70: | }
|
71: |
|
72: | |
73: | |
74: |
|
75: | public function load(MyTextSanitizer $myts)
|
76: | {
|
77: | $myts->callbackPatterns[] = "/\[youtube=(['\"]?)([^\"']*),([^\"']*)\\1]([^\"]*)\[\/youtube\]/sU";
|
78: | $myts->callbacks[] = __CLASS__ . '::myCallback';
|
79: | }
|
80: |
|
81: | |
82: | |
83: | |
84: | |
85: | |
86: | |
87: |
|
88: | public static function decode($url, $width, $height)
|
89: | {
|
90: |
|
91: |
|
92: |
|
93: |
|
94: |
|
95: | $youtubeRegex = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)'
|
96: | .'([^"&?/ ]{11})%i';
|
97: |
|
98: | if (preg_match($youtubeRegex, $url, $match)) {
|
99: | $videoId = $match[1];
|
100: | } elseif (preg_match('%^[^"&?/ ]{11}$%', $url)) {
|
101: | $videoId = $url;
|
102: | } else {
|
103: | trigger_error("Not matched: {$url} {$width} {$height}", E_USER_WARNING);
|
104: | return '';
|
105: | }
|
106: |
|
107: | $width = empty($width) ? 426 : (int) $width;
|
108: | switch ($width) {
|
109: | case 4:
|
110: | $height = 3;
|
111: | break;
|
112: | case 16:
|
113: | $height = 9;
|
114: | break;
|
115: | default:
|
116: | $height = empty($height) ? 240 : (int) $height;
|
117: | break;
|
118: | }
|
119: |
|
120: | $aspectRatio = $width/$height;
|
121: | $responsiveAspect = ($aspectRatio < 1.4) ? 'embed-responsive-4by3' : 'embed-responsive-16by9';
|
122: | if ($width < 17 && $height < 10) {
|
123: | $scale = (int) 450 / $width;
|
124: | $width = $width * $scale;
|
125: | $height = $height * $scale;
|
126: | }
|
127: |
|
128: | $template = <<<'EOD'
|
129: | <div class="embed-responsive %4$s">
|
130: | <iframe class="embed-responsive-item" width="%2$d" height="%3$d" src="https://www.youtube.com/embed/%1$s" frameborder="0" allowfullscreen></iframe>
|
131: | </div>
|
132: | EOD;
|
133: |
|
134: | $code = sprintf($template, $videoId, $width, $height, $responsiveAspect);
|
135: | return $code;
|
136: | }
|
137: | }
|
138: | |