Merge branch 'MDL-72054' of git://github.com/danmarsden/moodle
[moodle.git] / error / index.php
blobc94e522f98454520aff6a0f4be130889b8a3068a
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 // @codingStandardsIgnoreStart
31 require('../config.php');
32 // @codingStandardsIgnoreEnd
34 $context = context_system::instance();
35 $title = get_string('pagenotexisttitle', 'error');
36 $PAGE->set_url('/error/index.php');
37 $PAGE->set_context($context);
38 $PAGE->set_title($title);
39 $PAGE->set_heading($title);
40 $PAGE->navbar->add($title);
42 // This allows the webserver to dictate wether the http status should remain
43 // what it would have been, or force it to be a 404. Under other conditions
44 // it could most often be a 403, 405 or a 50x error.
45 $code = optional_param('code', 0, PARAM_INT);
46 if ($code == 404) {
47 header("HTTP/1.0 404 Not Found");
50 $canmessage = has_capability('moodle/site:senderrormessage', $context);
52 $supportuser = core_user::get_support_user();
54 // We can only message support if both the user has the capability
55 // and the support user is a real user.
56 if ($canmessage) {
57 $canmessage = core_user::is_real_user($supportuser->id);
60 $mform = new \core\form\error_feedback($CFG->wwwroot . '/error/index.php');
62 if ($data = $mform->get_data()) {
64 if (!$canmessage) {
65 redirect($CFG->wwwroot);
68 // Send the message and redirect.
69 $message = new \core\message\message();
70 $message->courseid = SITEID;
71 $message->component = 'moodle';
72 $message->name = 'errors';
73 $message->userfrom = $USER;
74 $message->userto = core_user::get_support_user();
75 $message->subject = 'Error: '. $data->referer .' -> '. $data->requested;
76 $message->fullmessage = $data->text;
77 $message->fullmessageformat = FORMAT_PLAIN;
78 $message->fullmessagehtml = '';
79 $message->smallmessage = '';
80 $message->contexturl = $data->requested;
81 message_send($message);
83 redirect($CFG->wwwroot, get_string('sendmessagesent', 'error', $data->requested), 5);
84 exit;
87 echo $OUTPUT->header();
88 echo $OUTPUT->notification(get_string('pagenotexist', 'error', s($ME)), 'error');
90 if (!empty($CFG->supportpage)) {
91 echo \html_writer::tag('h4', get_string('supportpage', 'admin'));
92 $link = \html_writer::link($CFG->supportpage, $CFG->supportpage);
93 echo \html_writer::tag('p', $link);
95 if (!empty($CFG->supportemail)) {
96 echo \html_writer::tag('h4', get_string('supportemail', 'admin'));
97 $link = \html_writer::link('mailto:' . $CFG->supportemail, $CFG->supportemail);
98 echo \html_writer::tag('p', $link);
101 if ($canmessage) {
102 echo \html_writer::tag('h4', get_string('sendmessage', 'error'));
103 $mform->display();
104 } else {
105 echo $OUTPUT->continue_button($CFG->wwwroot);
108 echo $OUTPUT->footer();