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: $config = parent::loadConfig(__DIR__);
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, enterFlashPhrase, enterFlashHeightPhrase, enterFlashWidthPhrase)
38: {
39: var selection = xoopsGetSelect(id);
40: if (selection.length > 0) {
41: var text = selection;
42: } else {
43: var text = prompt(enterFlashPhrase, "");
44: }
45: var domobj = xoopsGetElementById(id);
46: if (text.length > 0) {
47: var text2 = prompt(enterFlashWidthPhrase, "16x9");
48: var text3 = prompt(enterFlashHeightPhrase, "");
49: var result = "[youtube="+text2+","+text3+"]" + text + "[/youtube]";
50: xoopsInsertText(domobj, result);
51: }
52: domobj.focus();
53: }
54: EOH;
55:
56: return array($code, $javascript);
57: }
58:
59: 60: 61: 62: 63:
64: public static function myCallback($match)
65: {
66: return self::decode($match[4], $match[2], $match[3]);
67: }
68:
69: 70: 71:
72: public function load($ts)
73: {
74:
75:
76:
77:
78: $ts->callbackPatterns[] = "/\[youtube=(['\"]?)([^\"']*),([^\"']*)\\1]([^\"]*)\[\/youtube\]/sU";
79: $ts->callbacks[] = __CLASS__ . '::myCallback';
80:
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: public static function decode($url, $width, $height)
91: {
92:
93:
94:
95:
96:
97: $youtubeRegex = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)'
98: .'([^"&?/ ]{11})%i';
99:
100: if (preg_match($youtubeRegex, $url, $match)) {
101: $videoId = $match[1];
102: } elseif (preg_match('%^[^"&?/ ]{11}$%', $url)) {
103: $videoId = $url;
104: } else {
105: trigger_error("Not matched: {$url} {$width} {$height}", E_USER_WARNING);
106: return '';
107: }
108:
109: $width = empty($width) ? 426 : (int) $width;
110: switch ($width) {
111: case 4:
112: $height = 3;
113: break;
114: case 16:
115: $height = 9;
116: break;
117: default:
118: $height = empty($height) ? 240 : (int) $height;
119: break;
120: }
121:
122: $aspectRatio = $width/$height;
123: $responsiveAspect = ($aspectRatio < 1.4) ? 'embed-responsive-4by3' : 'embed-responsive-16by9';
124: if ($width < 17 && $height < 10) {
125: $scale = (int) 450 / $width;
126: $width = $width * $scale;
127: $height = $height * $scale;
128: }
129:
130: $template = <<<'EOD'
131: <div class="embed-responsive %4$s">
132: <iframe class="embed-responsive-item" width="%2$d" height="%3$d" src="https://www.youtube.com/embed/%1$s" frameborder="0" allowfullscreen></iframe>
133: </div>
134: EOD;
135:
136: $code = sprintf($template, $videoId, $width, $height, $responsiveAspect);
137: return $code;
138: }
139: }
140: