forked from bycs-lp/moodle-repository_imagehub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
381 lines (357 loc) · 13 KB
/
Copy pathlib.php
File metadata and controls
381 lines (357 loc) · 13 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* repository_imagehub plugin implementation
*
* Documentation: {@link https://moodledev.io/docs/apis/plugintypes/repository}
*
* @package repository_imagehub
* @copyright 2024 ISB Bayern
* @author Stefan Hanauska <stefan.hanauska@csg-in.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
/**
* Repository repository_imagehub implementation
*
* @package repository_imagehub
* @copyright 2024 ISB Bayern
* @author Stefan Hanauska <stefan.hanauska@csg-in.de>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_imagehub extends repository {
/** @var string The manual source type. */
public const SOURCE_TYPE_MANUAL = 'manual';
/** @var string The value of the manual source type. */
public const SOURCE_TYPE_MANUAL_VALUE = '0';
/** @var string The zip source type. */
public const SOURCE_TYPE_ZIP = 'zip';
/** @var string The value of the zip source type. */
public const SOURCE_TYPE_ZIP_VALUE = '1';
/**
* Given a path, and perhaps a search, get a list of files.
*
* See details on {@link http://docs.moodle.org/dev/Repository_plugins}
*
* @param string $path this parameter can a folder name, or a identification of folder
* @param string $page the page number of file list
* @return array the list of files, including meta infomation, containing the following keys
* manage, url to manage url
* client_id
* login, login form
* repo_id, active repository id
* login_btn_action, the login button action
* login_btn_label, the login button label
* total, number of results
* perpage, items per page
* page
* pages, total pages
* issearchresult, is it a search result?
* list, file list
* path, current path and parent path
*/
public function get_listing($path = '', $page = '') {
$ret = [
'list' => self::get_file_list($path, $page),
'norefresh' => true,
'nologin' => true,
'dynload' => true,
'nosearch' => false,
];
return $ret;
}
/**
* Search for files.
*
* @param string $search
* @param int $page
* @return void
*/
public function search($search, $page = 0) {
return [
'list' => $this->get_file_list('', $page, $search),
'norefresh' => true,
'nologin' => true,
'dynload' => true,
'nosearch' => false,
'issearchresult' => true,
];
}
/**
* Get a list of files.
* @param string $path
* @param string $page
* @param string $search
* @return array
*/
public function get_file_list($path = '', $page = '', $search = ''): array {
global $DB, $OUTPUT;
$sourceids = $DB->get_fieldset('repository_imagehub_sources', 'id');
$filelist = [];
$fs = get_file_storage();
$files = [];
foreach ($sourceids as $sourceid) {
$files = array_merge(
$files,
$fs->get_directory_files(
\context_system::instance()->id,
'repository_imagehub',
'images',
$sourceid,
'/',
true,
false
)
);
}
$where = '';
$params = [];
if (!empty($search)) {
$where = $DB->sql_like('title', ':search', false, false);
$where .= ' OR ' . $DB->sql_like('description', ':search2', false, false);
$params = [
'search' => '%' . $DB->sql_like_escape($search) . '%',
'search2' => '%' . $DB->sql_like_escape($search) . '%',
];
}
$results = $DB->get_records_select('repository_imagehub', $where, $params, '', 'fileid, title');
foreach ($files as $file) {
if (!str_starts_with($file->get_mimetype(), 'image') || !array_key_exists($file->get_id(), $results)) {
continue;
}
$node['thumbnail'] = $OUTPUT->image_url(file_extension_icon($file->get_filename()))->out(false);
$filelistentry = [
'title' => $file->get_filename() ?? $results[$file->get_id()]->description,
'shorttitle' => $results[$file->get_id()]->title ?? $file->get_filename(),
'size' => $file->get_filesize(),
'filename' => $file->get_filename(),
'thumbnail' => $OUTPUT->image_url(file_extension_icon($file->get_filename()))->out(false),
'icon' => $OUTPUT->image_url(file_extension_icon($file->get_filename()))->out(false),
'source' => $file->get_id(),
'author' => $file->get_author(),
'license' => $file->get_license() ?? 'U',
];
if ($file->get_mimetype() == 'image/svg+xml') {
$filelistentry['realthumbnail'] = $this->get_thumbnail_url($file, 'thumb')->out(false);
$filelistentry['realicon'] = $this->get_thumbnail_url($file, 'icon')->out(false);
$filelistentry['image_width'] = 100;
$filelistentry['image_height'] = 100;
} else if ($imageinfo = @getimagesizefromstring($file->get_content())) {
$filelistentry['realthumbnail'] = $this->get_thumbnail_url($file, 'thumb')->out(false);
$filelistentry['realicon'] = $this->get_thumbnail_url($file, 'icon')->out(false);
$filelistentry['image_width'] = $imageinfo[0];
$filelistentry['image_height'] = $imageinfo[1];
}
$filelist[] = $filelistentry;
}
return $filelist;
}
/**
* This plugin supports only web images.
*/
public function supported_filetypes() {
return ['web_image'];
}
/**
* Repository supports only internal files.
*
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL;
}
/**
* Create an instance for this plug-in
*
* @param string $type the type of the repository
* @param int $userid the user id
* @param stdClass $context the context
* @param array $params the options for this instance
* @param int $readonly whether to create it readonly or not (defaults to not)
* @return mixed
* @throws dml_exception
* @throws required_capability_exception
*/
public static function create($type, $userid, $context, $params, $readonly = 0) {
require_capability('moodle/site:config', context_system::instance());
return parent::create($type, $userid, $context, $params, $readonly);
}
/**
* Get the configuration form for this repository type.
* @param moodleform $mform
* @param string $classname
*/
public static function type_config_form($mform, $classname = 'repository_imagehub') {
$type = repository::get_type_by_typename('imagehub');
// Link to managesources.
if ($type !== null) {
$url = new moodle_url('/repository/imagehub/managesources.php');
$mform->addElement(
'static',
null,
get_string('linktomanagesources', 'repository_imagehub', $url),
get_string('linktomanagesources_description', 'repository_imagehub')
);
} else {
$mform->addElement(
'static',
null,
get_string('repositorynotenabled', 'repository_imagehub'),
''
);
}
}
/**
* Return names of the general options.
* By default: no general option name
*
* @return array
*/
public static function get_type_option_names() {
return ['pluginname'];
}
/**
* Is this repository used to browse moodle files?
*
* @return boolean
*/
public function has_moodle_files() {
return true;
}
/**
* Returns url of thumbnail file.
*
* @param stored_file $file current path in repository (dir and filename)
* @param string $thumbsize 'thumb' or 'icon'
* @return moodle_url
*/
protected static function get_thumbnail_url($file, $thumbsize) {
return moodle_url::make_pluginfile_url(
context_system::instance()->id,
'repository_imagehub',
$thumbsize,
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename()
);
}
/**
* Returns thumbnail file.
*
* @param stored_file $file
* @param string $thumbsize 'thumb' or 'icon'
* @return stored_file|null
*/
public static function get_thumbnail($file, $thumbsize) {
global $CFG;
$filecontents = $file->get_content();
$fs = get_file_storage();
if (
!($thumbfile = $fs->get_file(
context_system::instance()->id,
'repository_imagehub',
$thumbsize,
$file->get_itemid(),
$file->get_filepath(),
$file->get_filename()
))
) {
require_once($CFG->libdir . '/gdlib.php');
if ($thumbsize === 'thumb') {
$size = 90;
} else {
$size = 24;
}
if (!$data = generate_image_thumbnail_from_string($filecontents, $size, $size)) {
return null;
}
$record = [
'contextid' => context_system::instance()->id,
'component' => 'repository_imagehub',
'filearea' => $thumbsize,
'itemid' => $file->get_itemid(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
];
$thumbfile = $fs->create_file_from_string($record, $data);
}
return $thumbfile;
}
/**
* Get the file reference.
*
* @param int $fileid
* @return string
*/
public function get_file_reference($fileid) {
$fs = get_file_storage();
$file = $fs->get_file_by_id($fileid);
$filerecord = [
'component' => $file->get_component(),
'filearea' => $file->get_filearea(),
'itemid' => $file->get_itemid(),
'author' => $file->get_author(),
'filepath' => $file->get_filepath(),
'filename' => $file->get_filename(),
'contextid' => $file->get_contextid(),
];
return file_storage::pack_reference($filerecord);
}
/**
* Return whether the file is accessible.
*
* @param string $fileid
* @return bool
*/
public function file_is_accessible($fileid) {
$fs = get_file_storage();
$file = $fs->get_file_by_id($fileid);
return $file->get_component() == 'repository_imagehub';
}
}
/**
* Deliver a file from the repository.
* @param stdClass $course
* @param stdClass $cm
* @param context $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @param array $options
* @return bool|null
*/
function repository_imagehub_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []): ?bool {
global $OUTPUT;
$fullpath = "/1/repository_imagehub/images/" . implode('/', $args);
$fs = get_file_storage();
if ((!$file = $fs->get_file_by_hash(sha1($fullpath))) || $file->is_directory()) {
return false;
}
if ($filearea === 'thumb' || $filearea === 'icon') {
if (str_ends_with($fullpath, '.svg')) {
// SVG files are not resized.
$file = $fs->get_file_by_hash(sha1($fullpath));
} else {
if (!($file = repository_imagehub::get_thumbnail($file, $filearea))) {
// Generation failed, redirect to default icon for file extension.
// Do not use redirect() here because is not compatible with webservice/pluginfile.php.
header('Location: ' . $OUTPUT->image_url(file_extension_icon($file)));
}
}
}
send_stored_file($file, 0, 0, false, $options);
}