Completed
Pull Request — master (#541)
by Richard
07:06
created

Image   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 82
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
C registerExtensionProcessing() 0 60 10
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
 * TextSanitizer extension
19
 *
20
 * @category  Sanitizer\Extensions
21
 * @package   Xoops\Core\Text
22
 * @author    Taiwen Jiang <[email protected]>
23
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
24
 * @license   GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html)
25
 * @link      http://xoops.org
26
 */
27
class Image extends ExtensionAbstract
28
{
29
30
    protected static $jsLoaded;
31
32
    /**
33
     * @var array default configuration values
34
     */
35
    protected static $defaultConfiguration = [
36
        'enabled' => true,
37
        'clickable' => true,  // Click to open an image in a new window in full size
38
        'resize' => true,     // Resize the image down to max_width set below
39
        'max_width' => 300,   // Maximum width of an image displayed on page
40
        'allowimage' => true, // true to allow images, false to force links only
41
    ];
42
43
    /**
44
     * Register extension with the supplied sanitizer instance
45
     *
46
     * @return void
47
     */
48 1
    public function registerExtensionProcessing()
49
    {
50 1
        $config = $this->ts->getConfig('image'); // direct load to allow Sanitizer to change 'allowimage'
51
52 1
        $this->shortcodes->addShortcode(
53 1
            'img',
54 1
            function ($attributes, $content, $tagName) use ($config) {
0 ignored issues
show
Unused Code introduced by
The parameter $tagName is not used and could be removed.

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

Loading history...
55
                $xoops = \Xoops::getInstance();
56
                $defaults = [
57
                    'id' => 0,
58
                    'url' => trim($content),
59
                    'align' => '',
60
                    'width'  => $this->config['max_width'],
61
                ];
62
                $cleanAttributes = $this->shortcodes->shortcodeAttributes($defaults, $attributes);
63
                if (0 !== $cleanAttributes['id']) {
64
                    $cleanAttributes['url'] = $xoops->url('/image.php?id=' . $cleanAttributes['id']);
65
                }
66
                $url = $cleanAttributes['url'];
67
68
                $cleanAttributes['align'] = $this->ts->cleanEnum(
69
                    $cleanAttributes['align'],
70
                    ['right', 'left', 'center'],
71
                    '',
72
                    true
73
                );
74
                $class = '';
75
                if ($cleanAttributes['align']!= '') {
76
                    $class = ' class="' . $cleanAttributes['align'] . '"';
77
                }
78
                $width = $cleanAttributes['width'];
79
                if (preg_match('/[0-9]{1}$/', $width)) {
80
                    $width .= 'px';
81
                }
82
83
                if (!$config['allowimage']) {
84
                    $template = '<a href="%1$s" rel="external">%2$s</a>';
85
                    $alt = $this->ts->htmlSpecialChars($url);
86
                } elseif ($config['resize'] && !$config['clickable']) {
87
                    $alt = $this->ts->htmlSpecialChars(\XoopsLocale::RESIZED_IMAGE);
88
                    $template = '<img src="%1$s" alt="%2$s"%3$s style="max-width: %4$s;" />';
89
                } elseif ($config['resize'] && $config['clickable']) {
90
                    if (!self::$jsLoaded) {
91
                        self::$jsLoaded = true;
92
                        $xoops->theme()->addScript(
93
                            'media/xoops/image.js',
94
                            ['type' => 'text/javascript']
95
                        );
96
                    }
97
                    $alt = $this->ts->htmlSpecialChars(\XoopsLocale::CLICK_TO_SEE_ORIGINAL_IMAGE_IN_NEW_WINDOW);
98
                    $template = '<a href="javascript:loadImage(\'%1$s\');"><img src="%1$s" title="%2$s" alt="%2$s"%3$s style="max-width: %4$s;" /></a>';
99
                } else {
100
                    $alt = $this->ts->htmlSpecialChars(\XoopsLocale::ORIGINAL_IMAGE);
101
                    $template = '<img src="%1$s" alt="%2$s"%3$s />';
102
                }
103
                $newContent = sprintf($template, $url, $alt, $class, $width);
104
                return $newContent;
105 1
            }
106
        );
107 1
    }
108
}
109