forked from h5p/moodle-mod_hvp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy_pluginfile.php
76 lines (65 loc) · 2.73 KB
/
proxy_pluginfile.php
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
<?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/>.
/**
* Pluginfile proxy script.
*
* @package mod_hvp
* @author Kevin Pham <[email protected]>
* @copyright Catalyst IT, 2021
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(__FILE__) . '/../../config.php');
require_once($CFG->libdir . '/filelib.php');
\core\session\manager::write_close();
$relativepath = get_file_argument();
if (empty($relativepath)) {
exit();
}
// This should target the custom content types. We also filter out the
// Content-Length Header in this case to avoid truncation.
$jstobereplaced = stripos($relativepath, 'cachedassets') !== false && stripos($relativepath, '.js') !== false;
$url = new moodle_url('/pluginfile.php/'.ltrim($relativepath, '/'));
if (!$jstobereplaced) {
header("Location: $url");
die; // This will never be reached!
} else {
header('Content-Type: application/javascript');
}
$curl = new curl();
// Add user cookies to the request, to ensure they are processed properly.
$cookiestr = [];
foreach ($_COOKIE as $key => $value) {
$cookiestr[] = $key.'='.$value;
}
$cookiestr = implode('; ', $cookiestr) . ';';
$curl->setHeader('Cookie: '.$cookiestr);
$curl->setopt([
'CURLOPT_WRITEFUNCTION' => function ($curl, $body) use ($jstobereplaced, $relativepath) {
// Even if we modify the body, we need to return the original length.
$originalbodylength = strlen($body);
// Remove the transitionend event which occurs in the referenced
// link, to prevent card focusing on page load, as this might be
// included as embedded content, and a page scroll/focus does not
// make sense.
// https://github.com/h5p/h5p-dialogcards/blob/7a7580aad3424c60f45cb65eac52ff14bc83b540/src/scripts/h5p-dialogcards-card.js#L482-L484.
$body = str_replace('.one("transitionend",', '.off("_removed_",', $body);
echo $body;
return $originalbodylength;
},
]);
// The proxy will pass content back from pluginfile.php
$url = new moodle_url('/pluginfile.php/'.ltrim($relativepath, '/'));
$curl->get($url->out());