1: <?php
2:
3: /**
4: * TextSanitizer extension
5: *
6: * You may not change or alter any portion of this comment or credits
7: * of supporting developers from this source code or any supporting source code
8: * which is considered copyrighted (c) material of the original comment or credit authors.
9: * This program is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12: *
13: * @copyright (c) 2000-2021 XOOPS Project (www.xoops.org)
14: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
15: * @package class
16: * @subpackage textsanitizer
17: * @since 2.3.0
18: * @author Taiwen Jiang <phppp@users.sourceforge.net>
19: */
20: class MytsYoutube extends MyTextSanitizerExtension
21: {
22: /**
23: * @param $textarea_id
24: *
25: * @return array
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, 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: * @param $match
64: *
65: * @return string
66: */
67: public static function myCallback($match)
68: {
69: return self::decode($match[4], $match[2], $match[3]);
70: }
71:
72: /**
73: * @param MyTextSanitizer $myts
74: */
75: public function load(MyTextSanitizer $myts)
76: {
77: $myts->callbackPatterns[] = "/\[youtube=(['\"]?)([^\"']*),([^\"']*)\\1]([^\"]*)\[\/youtube\]/sU";
78: $myts->callbacks[] = __CLASS__ . '::myCallback';
79: }
80:
81: /**
82: * @param $url
83: * @param $width
84: * @param $height
85: *
86: * @return string
87: */
88: public static function decode($url, $width, $height)
89: {
90: // modernized responsive YouTube handling suggested by XOOPS user xd9527 -- thanks!
91: // https://xoops.org/modules/newbb/viewtopic.php?post_id=359913
92:
93: // match known youtube urls
94: // from: https://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match/6382259#6382259
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]; // extract just the video id from a URL
100: } elseif (preg_match('%^[^"&?/ ]{11}$%', $url)) {
101: $videoId = $url; // have a bare video id
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; // 16x9 = 1.777777778, 4x3 = 1.333333333
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: