-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanage_embedded_editor_upload.php
More file actions
110 lines (97 loc) · 3.55 KB
/
manage_embedded_editor_upload.php
File metadata and controls
110 lines (97 loc) · 3.55 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
<?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/>.
/**
* Same-origin upload endpoint for installing the embedded editor in Playground.
*
* The browser downloads the ZIP and posts it here so the PHP WASM runtime only
* performs local extraction/installation work.
*
* @package mod_exeweb
* @copyright 2025 eXeLearning
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/mod/exeweb/classes/local/embedded_editor_installer.php');
require_once($CFG->dirroot . '/mod/exeweb/classes/external/manage_embedded_editor.php');
use mod_exeweb\external\manage_embedded_editor;
use mod_exeweb\local\embedded_editor_installer;
/**
* Emit a JSON response and terminate the request.
*
* @param array $payload Response payload.
* @param int $status HTTP status code.
* @return never
*/
function mod_exeweb_emit_json(array $payload, int $status = 200): void {
http_response_code($status);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($payload);
exit;
}
require_login();
require_sesskey();
$context = \context_system::instance();
require_capability('moodle/site:config', $context);
require_capability('mod/exeweb:manageembeddededitor', $context);
$action = required_param('action', PARAM_ALPHA);
$version = required_param('version', PARAM_TEXT);
$validactions = ['install', 'update', 'repair'];
if (!in_array($action, $validactions, true)) {
mod_exeweb_emit_json([
'success' => false,
'message' => get_string('invalidaction', 'mod_exeweb', $action),
], 400);
}
$file = $_FILES['editorzip'] ?? null;
if (!is_array($file) || empty($file['tmp_name'])) {
mod_exeweb_emit_json([
'success' => false,
'message' => get_string('editoruploadmissingfile', 'mod_exeweb'),
], 400);
}
if (($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
mod_exeweb_emit_json([
'success' => false,
'message' => get_string('editoruploadfailed', 'mod_exeweb', (string)($file['error'] ?? UPLOAD_ERR_NO_FILE)),
], 400);
}
$tmpname = (string)($file['tmp_name'] ?? '');
if ($tmpname === '' || !is_file($tmpname)) {
mod_exeweb_emit_json([
'success' => false,
'message' => get_string('editoruploadmissingfile', 'mod_exeweb'),
], 400);
}
try {
$installer = new embedded_editor_installer();
if ($action === 'repair') {
$installer->uninstall();
}
$result = $installer->install_from_local_zip($tmpname, $version);
mod_exeweb_emit_json([
'success' => true,
'action' => $action,
'message' => manage_embedded_editor::get_action_success_string($action),
'version' => $result['version'] ?? '',
'installed_at' => $result['installed_at'] ?? '',
]);
} catch (\Throwable $e) {
mod_exeweb_emit_json([
'success' => false,
'message' => $e->getMessage(),
], 500);
}