1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 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: 19: 20: 21: 22: 23: 24: 25: 26:
27: class YouTube extends ExtensionAbstract
28: {
29: 30: 31:
32: protected static $defaultConfiguration = [
33: 'enabled' => true,
34: 'template' => '<iframe width="%2$d" height="%3$d" src="https://www.youtube.com/embed/%1$s" frameborder="0" allowfullscreen></iframe>',
35: 'width' => "640",
36: 'height' => "385",
37: ];
38:
39: 40: 41: 42: 43: 44: 45:
46: public function getDhtmlEditorSupport($textAreaId)
47: {
48: $buttonCode = $this->getEditorButtonHtml(
49: $textAreaId,
50: 'youtube.gif',
51: \XoopsLocale::YOUTUBE,
52: 'xoopsCodeYoutube',
53: \XoopsLocale::YOUTUBE_URL,
54: \XoopsLocale::HEIGHT,
55: \XoopsLocale::WIDTH
56: );
57:
58: $javascript = <<<EOH
59:
60: function xoopsCodeYoutube(id, enterFlashPhrase, enterFlashHeightPhrase, enterFlashWidthPhrase)
61: {
62: var selection = xoopsGetSelect(id);
63: if (selection.length > 0) {
64: var text = selection;
65: } else {
66: var text = prompt(enterFlashPhrase, "");
67: }
68: var domobj = xoopsGetElementById(id);
69: if ( text.length > 0 ) {
70: var text2 = prompt(enterFlashWidthPhrase, "640");
71: var text3 = prompt(enterFlashHeightPhrase, "385");
72: var result = '[youtube url="' + text + '"';
73: if ( text2.length > 0) {
74: result += ' width="' + text2 + '" height="' + text3 + '"';
75: }
76: result += " /]";
77: xoopsInsertText(domobj, result);
78: }
79: domobj.focus();
80: }
81:
82: EOH;
83:
84: return [$buttonCode, $javascript];
85: }
86:
87: 88: 89: 90: 91:
92: public function registerExtensionProcessing()
93: {
94: $this->shortcodes->addShortcode(
95: 'youtube',
96: function ($attributes, $content, $tagName) {
97: if (array_key_exists(0, $attributes) && '=' === substr($attributes[0], 0, 1)) {
98: $args = ltrim($attributes[0], '=');
99: list($width, $height) = explode(',', $args);
100: $url = $content;
101: } else {
102: $defaults = [
103: 'url' => trim($content),
104: 'width' => $this->config['width'],
105: 'height' => $this->config['height'],
106: ];
107: $cleanAttributes = $this->shortcodes->shortcodeAttributes($defaults, $attributes);
108: $url = $cleanAttributes['url'];
109: $width = $cleanAttributes['width'];
110: $height = $cleanAttributes['height'];
111: }
112:
113:
114: $youtubeRegex = '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)'
115: .'([^"&?/ ]{11})%i';
116:
117: if (preg_match($youtubeRegex, $url, $match)) {
118: $videoId = $match[1];
119: } elseif (preg_match('%^[^"&?/ ]{11}$%', $url)) {
120: $videoId = $url;
121: } else {
122: return '';
123: }
124: $template = $this->config['template'];
125: $newContent = sprintf($template, $videoId, $width, $height);
126: return $newContent;
127: }
128: );
129: }
130: }
131: