Completed
Pull Request — master (#591)
by Richard
16:26
created

Flash   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 58.33%

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 109
ccs 21
cts 36
cp 0.5833
rs 10
c 0
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDhtmlEditorSupport() 0 45 2
B registerExtensionProcessing() 0 36 9
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
 * Sanitizer extension for flash content
19
 *
20
 * @category  Sanitizer
21
 * @package   Xoops\Core\Text
22
 * @author    Taiwen Jiang <[email protected]>
23
 * @copyright 2000-2019 XOOPS Project (https://xoops.org)
24
 * @license   GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
25
 */
26
class Flash extends ExtensionAbstract
27
{
28
    /**
29
     * @var array default configuration values
30
     */
31
    protected static $defaultConfiguration = [
32
        'enabled' => false,
33
        'detect_dimension' => '1',
34
        'template' => '<object type="application/x-shockwave-flash" data="%1$s" width="%2$d" height="%3$d"></object>',
35
        'fallback_width'  => "320",
36
        'fallback_height' => "240",
37
        'enable_flash_entry' => false,  // false to disable entry button in editor, existing content will still play
38
    ];
39
40
    /**
41
     * Provide button and javascript code used by the DhtmlTextArea
42
     *
43
     * @param string $textAreaId dom element id
44
     *
45
     * @return string[] editor button as HTML, supporting javascript
46
     */
47 3
    public function getDhtmlEditorSupport($textAreaId)
48
    {
49 3
        if (false === $this->config['enable_flash_entry']) {
50 3
            return parent::getDhtmlEditorSupport($textAreaId);
51
        }
52
53
        $buttonCode = $this->getEditorButtonHtml(
54
            $textAreaId,
55
            'fa fa-fw fa-flash',
56
            \XoopsLocale::FLASH,
57
            'xoopsCodeFlash',
58
            \XoopsLocale::FLASH_URL,
59
            \XoopsLocale::HEIGHT,
60
            \XoopsLocale::WIDTH,
61
            $this->config['detect_dimension']
62
        );
63
64
        $javascript = <<<EOF
65
66
            function xoopsCodeFlash(id, enterFlashPhrase, enterFlashHeightPhrase, enterFlashWidthPhrase, enableDimensionDetect)
67
            {
68
                enableDimensionDetect = Boolean(parseInt(enableDimensionDetect));
69
                var selection = xoopsGetSelect(id);
70
                if (selection.length > 0) {
71
                    var text = selection;
72
                } else {
73
                    var text = prompt(enterFlashPhrase, "");
74
                }
75
                var domobj = xoopsGetElementById(id);
76
                if ( text.length > 0 ) {
77
                    var text2 = enableDimensionDetect ? "" : prompt(enterFlashWidthPhrase, "");
78
                    var text3 = enableDimensionDetect ? "" : prompt(enterFlashHeightPhrase, "");
79
                    if (text2.length == 0 && text2.length == 0) {
80
                        var result = '[flash url="'+text+'" /]';
81
                    } else {
82
                        var result = '[flash url="'+text+'" width='+text2+' height='+text3+' /]';
83
                    }
84
                    xoopsInsertText(domobj, result);
85
                }
86
                domobj.focus();
87
            }
88
89
EOF;
90
91
        return [$buttonCode, $javascript];
92
    }
93
94
    /**
95
     * Register extension with the supplied sanitizer instance
96
     *
97
     * @return void
98
     */
99 16
    public function registerExtensionProcessing()
100
    {
101
        $function = function ($attributes, $content, $tagName) {
0 ignored issues
show
Unused Code introduced by
The parameter $tagName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
        $function = function ($attributes, $content, /** @scrutinizer ignore-unused */ $tagName) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102 1
            if (array_key_exists(0, $attributes) && '=' === substr($attributes[0], 0, 1)) {
103 1
                $args = ltrim($attributes[0], '=');
104 1
                list($width, $height) = explode(',', $args);
105 1
                $url = $content;
106
            } else {
107
                $defaults = [
108 1
                    'url'    => trim($content),
109
                    'width'  => null,
110
                    'height' => null,
111
                ];
112 1
                $cleanAttributes = $this->shortcodes->shortcodeAttributes($defaults, $attributes);
113 1
                $url = $cleanAttributes['url'];
114 1
                $width = $cleanAttributes['width'];
115 1
                $height = $cleanAttributes['height'];
116
            }
117 1
            if ((empty($width) || empty($height)) && (bool)$this->config['detect_dimension']) {
118
                $dimension = @getimagesize($content);
119
                if ($dimension !== false) {
120
                    list($width, $height) = $dimension;
121
                }
122
            }
123 1
            if (empty($width) || empty($height)) {
124
                $width = $this->config['fallback_width'];
125
                $height = $this->config['fallback_height'];
126
            }
127
128 1
            $template = $this->config['template'];
129 1
            $newcontent = sprintf($template, $url, $width, $height);
130 1
            return $newcontent;
131 16
        };
132
133 16
        $this->shortcodes->addShortcode('flash', $function);
134 16
        $this->shortcodes->addShortcode('swf', $function);
135 16
    }
136
}
137