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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
<?php
namespace Courseware\BlockTypes;
/**
* This class represents the content of a Courseware text block.
*
* @author Ron Lucke <lucke@elan-ev.de>
* @license GPL2 or any later version
*
* @since Stud.IP 5.0
*/
class Text extends BlockType
{
public static function getType(): string
{
return 'text';
}
public static function getTitle(): string
{
return _('Text');
}
public static function getDescription(): string
{
return _('Erstellen von Inhalten mit dem WYSIWYG-Editor.');
}
public function initialPayload(): array
{
return ['text' => ''];
}
public function getPayload()
{
$payload = parent::getPayload();
$payload['text'] = \Studip\Markup::purifyHtml(\Studip\Markup::markAsHtml($payload['text']));
return $payload;
}
public function setPayload($payload): void
{
$document = new \DOMDocument();
$old_libxml_error = libxml_use_internal_errors(true);
if (!empty($payload['text'])) {
$document->loadHTML($payload['text']);
libxml_use_internal_errors($old_libxml_error);
$imageElements = $document->getElementsByTagName('img');
foreach ($imageElements as $element) {
if (!$element instanceof \DOMElement || !$element->hasAttribute('src')) {
continue;
}
$src = $element->getAttribute('src');
if (str_contains($src, 'sendfile.php') && !str_contains($src, $GLOBALS['ABSOLUTE_URI_STUDIP'])) {
$find = explode('sendfile.php', $src)[0];
$payload['text'] = str_replace($find, $GLOBALS['ABSOLUTE_URI_STUDIP'], $payload['text']);
}
}
}
$payload['text'] = \Studip\Markup::purifyHtml(\Studip\Markup::markAsHtml($payload['text']));
parent::setPayload($payload);
}
public static function getJsonSchema(): string
{
$schemaFile = __DIR__.'/Text.json';
return file_get_contents($schemaFile);
}
/**
* get all files related to this block.
*
* @return \FileRef[] list of file references realted to this block
*/
public function getFiles(): array
{
$payload = $this->getPayload();
$document = new \DOMDocument();
$files = [];
if ($payload['text']) {
$old_libxml_error = libxml_use_internal_errors(true);
$document->loadHTML($payload['text']);
libxml_use_internal_errors($old_libxml_error);
$imageElements = $document->getElementsByTagName('img');
foreach ($imageElements as $element) {
if (!$element instanceof \DOMElement || !$element->hasAttribute('src')) {
continue;
}
$file = $this->extractFile($element->getAttribute('src'));
if ($file !== null) {
$files[] = $file;
}
}
}
return $files;
}
public function copyPayload(string $rangeId = ''): array
{
$payload = $this->getPayload();
$document = new \DOMDocument();
if ($payload['text']) {
$document->loadHTML(mb_convert_encoding($payload['text'], 'HTML-ENTITIES', 'UTF-8'));
$imageElements = $document->getElementsByTagName('img');
foreach ($imageElements as $element) {
if (!$element instanceof \DOMElement || !$element->hasAttribute('src')) {
continue;
}
$file = $this->extractFile($element->getAttribute('src'));
if ($file !== null) {
$file_copy_id = $this->copyFileById($file->id, $rangeId);
if ($file_copy_id) {
$element->setAttribute('src', \FileRef::find($file_copy_id)->getDownloadURL());
}
}
}
$payload['text'] = $document->saveHTML();
}
return $payload;
}
public static function getCategories(): array
{
return ['text'];
}
public static function getContentTypes(): array
{
return ['text'];
}
public static function getFileTypes(): array
{
return [];
}
public static function getTags(): array
{
return [
_('Text'), _('Bild'), _('schreiben'), _('WYSIWIG'), _('eintippen'),
_('Editor'), _('Eingabe'), _('Eingabefeld'), _('Skript'), _('einfügen'),
_('Foto'), _('Inhalt'), _('Tabelle'), _('Inhalt erstellen'),
_('Veranschaulichung'), _('Visualisierung'), 'png', 'jpg', 'gif',
];
}
/**
* Calls a callback if a given URL is an internal URL.
*
* @param string $url The url to check
* @param callable $callback A callable to execute
*
* @return mixed The return value of the callback or null if the callback
* is not executed
*/
private function applyCallbackOnInternalUrl($url, $callback)
{
if (! \Studip\MarkupPrivate\MediaProxy\isInternalLink($url)) {
return null;
}
$components = parse_url($url);
if (
isset($components['path'])
&& substr($components['path'], -13) == '/sendfile.php'
&& isset($components['query'])
&& $components['query'] != ''
) {
parse_str($components['query'], $queryParams);
return $callback($components, $queryParams);
}
return null;
}
private function extractFile($url)
{
return $this->applyCallbackOnInternalUrl($url, function ($components, $queryParams) {
if (isset($queryParams['file_id'])) {
$file_ref = new \FileRef($queryParams['file_id']);
return $file_ref;
}
return null;
});
}
}
|