Passed
Push — master ( 951518...df5075 )
by
unknown
09:13
created

MytsMp3::myCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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-2017 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 <[email protected]>
19
 */
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
/**
23
 * Class MytsMp3
24
 */
25
class MytsMp3 extends MyTextSanitizerExtension
26
{
27
    /**
28
     * @param string $textarea_id
29
     * @return array
30
     */
31
    public function encode($textarea_id)
32
    {
33
        $buttonHtml = "<button type='button' class='btn btn-default' onclick='xoopsCodeMp3(\"{$textarea_id}\");' title='"
34
                      . _XOOPS_FORM_ALTMP3 . "'>"
35
                      . "<span class='fa fa-fw fa-music' aria-hidden='true'></span></button>";
36
37
        $javascript = <<<EOF
38
function xoopsCodeMp3(id) {
39
    var text = prompt("Enter MP3 URL (e.g., https://example.com/audio.mp3)", xoopsGetSelect(id));
40
    while (text !== null && !text.trim().match(/^https?:\/\/[\w\-\.]+(\:\d+)?\/.+\.mp3(\?.*)?$/i)) {
41
        alert("Invalid MP3 URL. The URL must begin with http or https and end with .mp3");
42
        text = prompt("Enter MP3 URL (e.g., https://example.com/audio.mp3)", text);
43
    }
44
    if (text && text.trim().length > 0) {
45
        xoopsInsertText(document.getElementById(id), "[mp3]" + text.trim() + "[/mp3]");
46
    }
47
}
48
EOF;
49
50
51
        return [$buttonHtml, $javascript];
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function load(MyTextSanitizer $myts)
58
    {
59
        $myts->callbackPatterns[] = '/\[mp3\](.*?)\[\/mp3\]/s';
60
        $myts->callbacks[]        = __CLASS__ . '::decode';
61
        return true;
62
    }
63
64
    /**
65
     * @param string|array $url
66
     * @param string|int   $width
67
     * @param string|int   $height
68
     * @return string
69
     */
70
    public static function decode($url, $width = 0, $height = 0)
71
    {
72
        if (is_array($url)) {
73
            $url = htmlspecialchars($url[1], ENT_QUOTES, 'UTF-8', false); // Prevent double-encoding
74
        } else {
75
            $url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8', false); // Prevent double-encoding
76
        }
77
        return "<audio controls><source src='{$url}' type='audio/mpeg'>Your browser does not support the audio element.</audio>";
78
    }
79
}
80