Merge branch 'MDL-79673' of https://github.com/paulholden/moodle
[moodle.git] / error / index.php
blobe76e6b5cdc4940863864bcbdc56a8011ed48fc4b
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.
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/>.
17 /**
18 * Moodle 404 Error page
20 * This is for 404 error pages served by the webserver and then passed
21 * to Moodle to be rendered using the site theme.
23 * ErrorDocument 404 /error/index.php
25 * @package core
26 * @copyright 2020 Brendan Heywood <brendan@catalyst-au.net>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require('../config.php'); // phpcs:ignore
32 // Until we have a more robust routing api in place this is a very simple
33 // and clean way to handle arbitrary urls without a php extension.
34 if ($ME === '/.well-known/change-password') {
35 redirect(new moodle_url('/login/change_password.php'));
38 $context = context_system::instance();
39 $title = get_string('pagenotexisttitle', 'error');
40 $PAGE->set_url('/error/index.php');
41 $PAGE->set_context($context);
42 $PAGE->set_title($title);
43 $PAGE->set_heading($title);
44 $PAGE->navbar->add($title);
46 // This allows the webserver to dictate wether the http status should remain
47 // what it would have been, or force it to be a 404. Under other conditions
48 // it could most often be a 403, 405 or a 50x error.
49 $code = optional_param('code', 0, PARAM_INT);
50 if ($code == 404) {
51 header("HTTP/1.0 404 Not Found");
54 $canmessage = has_capability('moodle/site:senderrormessage', $context);
56 $supportuser = core_user::get_support_user();
58 // We can only message support if both the user has the capability
59 // and the support user is a real user.
60 if ($canmessage) {
61 $canmessage = core_user::is_real_user($supportuser->id);
64 $mform = new \core\form\error_feedback($CFG->wwwroot . '/error/index.php');
66 if ($data = $mform->get_data()) {
68 if (!$canmessage) {
69 redirect($CFG->wwwroot);
72 // Send the message and redirect.
73 $message = new \core\message\message();
74 $message->courseid = SITEID;
75 $message->component = 'moodle';
76 $message->name = 'errors';
77 $message->userfrom = $USER;
78 $message->userto = core_user::get_support_user();
79 $message->subject = 'Error: '. $data->referer .' -> '. $data->requested;
80 $message->fullmessage = $data->text;
81 $message->fullmessageformat = FORMAT_PLAIN;
82 $message->fullmessagehtml = '';
83 $message->smallmessage = '';
84 $message->contexturl = $data->requested;
85 message_send($message);
87 redirect($CFG->wwwroot, get_string('sendmessagesent', 'error', $data->requested), 5);
88 exit;
91 echo $OUTPUT->header();
92 echo $OUTPUT->notification(get_string('pagenotexist', 'error', s($ME)), 'error');
93 echo $OUTPUT->supportemail(['class' => 'text-center d-block mb-3 font-weight-bold']);
95 if ($canmessage) {
96 echo \html_writer::tag('h4', get_string('sendmessage', 'error'));
97 $mform->display();
98 } else {
99 echo $OUTPUT->continue_button($CFG->wwwroot);
102 echo $OUTPUT->footer();