MDL-69257 core_h5p: Move $maxsize outside the foreach loop
[moodle.git] / h5p / ajax.php
CommitLineData
6da050d7
VDF
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Responsible for handling AJAX requests related to H5P.
19 *
20 * @package core_h5p
21 * @copyright 2020 Victor Deniz <victor@moodle.com>, based on code by Joubel AS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25use core_h5p\factory;
26use core_h5p\framework;
6157f593 27use core_h5p\local\library\autoloader;
6da050d7
VDF
28
29define('AJAX_SCRIPT', true);
30
31require(__DIR__ . '/../config.php');
32require_once($CFG->libdir . '/filelib.php');
33
6157f593
AA
34if (!confirm_sesskey()) {
35 autoloader::register();
36 H5PCore::ajaxError(get_string('invalidsesskey', 'error'));
6da050d7
VDF
37 header('HTTP/1.1 403 Forbidden');
38 return;
39}
6157f593
AA
40require_login();
41
42$action = required_param('action', PARAM_ALPHA);
6da050d7
VDF
43
44$factory = new factory();
45$editor = $factory->get_editor();
46
47switch ($action) {
48 // Load list of libraries or details for library.
49 case 'libraries':
50 // Get parameters.
51 $name = optional_param('machineName', '', PARAM_TEXT);
52 $major = optional_param('majorVersion', 0, PARAM_INT);
53 $minor = optional_param('minorVersion', 0, PARAM_INT);
54
55 $language = optional_param('default-language', null, PARAM_ALPHA);
56
57 if (!empty($name)) {
58 $editor->ajax->action(H5PEditorEndpoints::SINGLE_LIBRARY, $name,
59 $major, $minor, framework::get_language(), '', '', $language);
60 } else {
61 $editor->ajax->action(H5PEditorEndpoints::LIBRARIES);
62 }
63
64 break;
65
66 // Load content type cache list to display available libraries in hub.
67 case 'contenttypecache':
68 $editor->ajax->action(H5PEditorEndpoints::CONTENT_TYPE_CACHE);
69 break;
70
71 // Handle file upload through the editor.
6157f593
AA
72 // This endpoint needs a token that only users with H5P editor access could get.
73 // TODO: MDL-68907 to check capabilities.
6da050d7
VDF
74 case 'files':
75 $token = required_param('token', PARAM_RAW);
76 $contentid = required_param('contentId', PARAM_INT);
77
2212df5d 78 $maxsize = get_max_upload_file_size($CFG->maxbytes);
2355205a
MG
79 // Check size of each uploaded file and scan for viruses.
80 foreach ($_FILES as $uploadedfile) {
81 $filename = clean_param($uploadedfile['name'], PARAM_FILE);
2355205a
MG
82 if ($uploadedfile['size'] > $maxsize) {
83 H5PCore::ajaxError(get_string('maxbytesfile', 'error', ['file' => $filename, 'size' => display_size($maxsize)]));
84 return;
85 }
86 \core\antivirus\manager::scan_file($uploadedfile['tmp_name'], $filename, true);
87 }
88
6da050d7
VDF
89 $editor->ajax->action(H5PEditorEndpoints::FILES, $token, $contentid);
90 break;
91
6da050d7
VDF
92 // Get the $language libraries translations.
93 case 'translations':
94 $language = required_param('language', PARAM_RAW);
95 $editor->ajax->action(H5PEditorEndpoints::TRANSLATIONS, $language);
96 break;
97
98 // Handle filtering of parameters through AJAX.
99 case 'filter':
100 $token = required_param('token', PARAM_RAW);
101 $libraryparameters = required_param('libraryParameters', PARAM_RAW);
102
103 $editor->ajax->action(H5PEditorEndpoints::FILTER, $token, $libraryparameters);
104 break;
105
106 // Throw error if AJAX action is not handled.
107 default:
108 throw new coding_exception('Unhandled AJAX');
109 break;
110}