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 Image extends ExtensionAbstract
28: {
29:
30: protected static $jsLoaded;
31:
32: 33: 34:
35: protected static $defaultConfiguration = [
36: 'enabled' => true,
37: 'clickable' => true,
38: 'resize' => true,
39: 'max_width' => 300,
40: 'allowimage' => true,
41: ];
42:
43: 44: 45: 46: 47:
48: public function registerExtensionProcessing()
49: {
50: $config = $this->ts->getConfig('image');
51:
52: $this->shortcodes->addShortcode(
53: 'img',
54: function ($attributes, $content, $tagName) use ($config) {
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" alt="%2$s"%3$s style="max-width: %4$s;" />';
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: }
106: );
107: }
108: }
109: