1441 |
ariadna |
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 |
namespace core\router\middleware;
|
|
|
18 |
|
|
|
19 |
use core\router\util;
|
|
|
20 |
use core\router\route_loader_interface;
|
|
|
21 |
use Psr\Http\Message\ResponseInterface;
|
|
|
22 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
23 |
use Psr\Http\Server\MiddlewareInterface;
|
|
|
24 |
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Middleware to set flags and define setup.
|
|
|
28 |
*
|
|
|
29 |
* @package core
|
|
|
30 |
* @copyright 2024 Andrew Lyons <andrew@nicols.co.uk>
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
*/
|
|
|
33 |
class moodle_bootstrap_middleware implements MiddlewareInterface {
|
|
|
34 |
#[\Override]
|
|
|
35 |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
|
|
|
36 |
global $CFG, $PAGE;
|
|
|
37 |
|
|
|
38 |
if (str_contains($request->getUri(), route_loader_interface::ROUTE_GROUP_API)) {
|
|
|
39 |
// @codeCoverageIgnoreStart
|
|
|
40 |
if (!defined('AJAX_SCRIPT')) {
|
|
|
41 |
define('AJAX_SCRIPT', true);
|
|
|
42 |
}
|
|
|
43 |
// @codeCoverageIgnoreEnd
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
$routeattribute = util::get_route_instance_for_request($request);
|
|
|
47 |
if ($routeattribute && !$routeattribute->cookies) {
|
|
|
48 |
// @codeCoverageIgnoreStart
|
|
|
49 |
// This request should not access Moodle cookies.
|
|
|
50 |
if (!defined('NO_MOODLE_COOKIES')) {
|
|
|
51 |
define('NO_MOODLE_COOKIES', true);
|
|
|
52 |
}
|
|
|
53 |
// @codeCoverageIgnoreEnd
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
if (!$routeattribute || !$routeattribute->abortafterconfig) {
|
|
|
57 |
// Do not load the full Moodle stack. This is a lightweight request.
|
|
|
58 |
$this->load_full_moodle();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
// Set the URL for the page.
|
|
|
62 |
// Normally in Moodle this is a largely hard-coded value with only the query string changing dynamically in the page.
|
|
|
63 |
// However, in this instance, we are generating the URL dynamically because we are the request terminator
|
|
|
64 |
// for a large number of requests at different endpoints.
|
|
|
65 |
|
|
|
66 |
// In basic cases this will just work, but there are some edge cases to consider - specficially were the site
|
|
|
67 |
// is behind a reverse proxy and/or an SSL terminator.
|
|
|
68 |
// In these cases the URL we generate from the ServerRequestInterface may be the _terminated_ URL,
|
|
|
69 |
// and not the URL that was requested by the client.
|
|
|
70 |
|
|
|
71 |
// We need to generate the URL that the client requested, not the URL that the server received.
|
|
|
72 |
$url = $request->getUri();
|
|
|
73 |
|
|
|
74 |
if (!empty($CFG->reverseproxy)) {
|
|
|
75 |
// This site is behind a reverse proxy. The requested URI may have a different:
|
|
|
76 |
// - scheme
|
|
|
77 |
// - host
|
|
|
78 |
// - port
|
|
|
79 |
// to the URL that the client requested.
|
|
|
80 |
|
|
|
81 |
$url = $url
|
|
|
82 |
// Start by setting the scheme and host to the wwwroot.
|
|
|
83 |
->withScheme(parse_url($CFG->wwwroot, PHP_URL_SCHEME))
|
|
|
84 |
->withHost(parse_url($CFG->wwwroot, PHP_URL_HOST))
|
|
|
85 |
|
|
|
86 |
// Update the URL to match the port of the wwwroot.
|
|
|
87 |
// While it is highly unlikely that a wwwroot includes an explicit port, we should still handle it.
|
|
|
88 |
->withPort(parse_url($CFG->wwwroot, PHP_URL_PORT));
|
|
|
89 |
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if (!empty($CFG->sslproxy)) {
|
|
|
93 |
// This site is behind an ssl terminating proxy. The requested URI may have a different:
|
|
|
94 |
// - scheme
|
|
|
95 |
// - port
|
|
|
96 |
// to the URL that the client requested.
|
|
|
97 |
$url = $url
|
|
|
98 |
// The wwwroot must use the https scheme, but the terminating request may have been received using http.
|
|
|
99 |
->withScheme('https')
|
|
|
100 |
|
|
|
101 |
// Update the URL to match the port of the wwwroot.
|
|
|
102 |
// While it is highly unlikely that a wwwroot includes an explicit port, we should still handle it.
|
|
|
103 |
->withPort(parse_url($CFG->wwwroot, PHP_URL_PORT));
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$PAGE->set_url((string) $url);
|
|
|
107 |
|
|
|
108 |
return $handler->handle($request);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Check whether Moodle is fully loaded.
|
|
|
113 |
*
|
|
|
114 |
* @return bool
|
|
|
115 |
* @codeCoverageIgnore
|
|
|
116 |
*/
|
|
|
117 |
public function is_full_moodle_loaded(): bool {
|
|
|
118 |
if (defined('ABORT_AFTER_CONFIG')) {
|
|
|
119 |
return defined('ABORT_AFTER_CONFIG_CANCEL');
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
return true;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Load the full Moodle Framework.
|
|
|
127 |
*
|
|
|
128 |
* @codeCoverageIgnore
|
|
|
129 |
*/
|
|
|
130 |
protected function load_full_moodle(): void {
|
|
|
131 |
// Note: These globals should be defined even if they are not used as they are used in the require.
|
|
|
132 |
global $CFG, $DB, $SESSION, $OUTPUT, $PAGE;
|
|
|
133 |
|
|
|
134 |
if ($this->is_full_moodle_loaded()) {
|
|
|
135 |
return;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
// Ok, now we need to start normal moodle script, we need to load all libs and $DB.
|
|
|
139 |
if (defined('ABORT_AFTER_CONFIG_CANCEL') && ABORT_AFTER_CONFIG_CANCEL) {
|
|
|
140 |
return;
|
|
|
141 |
}
|
|
|
142 |
define('ABORT_AFTER_CONFIG_CANCEL', true);
|
|
|
143 |
|
|
|
144 |
require("{$CFG->dirroot}/lib/setup.php");
|
|
|
145 |
}
|
|
|
146 |
}
|