mediajce.php
1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* @package Joomla.Plugin
* @subpackage Fields.MediaJce
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @copyright Copyright (C) 2020 Ryan Demmer. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
jimport('joomla.filesystem.path');
if ($field->value == '') {
return;
}
$data = json_decode($field->value);
if (!$data) {
$data = (object) array('src' => $field->value, 'alt' => '');
}
$class = (string) $fieldParams->get('media_class', '');
$type = (string) $fieldParams->get('mediatype', 'images');
$text = (string) $fieldParams->get('media_description', '');
if ($class) {
$class = ' class="' . htmlentities($class, ENT_COMPAT, 'UTF-8', true) . '"';
}
if ($text) {
$text = htmlentities($text, ENT_COMPAT, 'UTF-8', true);
}
$value = (array) $data->src;
$buffer = '';
$element = '<img src="%s"%s alt="%s" />';
if ($type !== "images") {
$element = '<a href="%s"%s>%s</a>';
} else {
$text = $data->alt;
}
foreach ($value as $path) {
if (!$path) {
continue;
}
// remove some common characters
$path = preg_replace('#[\+\\\?\#%&<>"\'=\[\]\{\},;@\^\(\)£€$]#', '', $path);
// trim
$path = trim($path);
// check for valid path after clean
if (!$path) {
continue;
}
// clean path
$path = JPath::clean($path);
// create full path
$fullpath = JPATH_SITE . '/' . trim($path, '/');
// check path is valid
if (!is_file($fullpath)) {
continue;
}
// set text as basename if not an image
if (!$text && $type !== "images") {
$text = basename($path);
}
$buffer .= sprintf($element,
htmlentities($path, ENT_COMPAT, 'UTF-8', true),
$class,
$text
);
}
echo $buffer;