Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/error/ -> index.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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  /**
  18   * Moodle 404 Error page
  19   *
  20   * This is for 404 error pages served by the webserver and then passed
  21   * to Moodle to be rendered using the site theme.
  22   *
  23   * ErrorDocument 404 /error/index.php
  24   *
  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
  28   */
  29  
  30  require('../config.php'); // phpcs:ignore
  31  
  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'));
  36  }
  37  
  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);
  45  
  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");
  52  }
  53  
  54  $canmessage = has_capability('moodle/site:senderrormessage', $context);
  55  
  56  $supportuser = core_user::get_support_user();
  57  
  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);
  62  }
  63  
  64  $mform = new \core\form\error_feedback($CFG->wwwroot . '/error/index.php');
  65  
  66  if ($data = $mform->get_data()) {
  67  
  68      if (!$canmessage) {
  69          redirect($CFG->wwwroot);
  70      }
  71  
  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);
  86  
  87      redirect($CFG->wwwroot, get_string('sendmessagesent', 'error', $data->requested), 5);
  88      exit;
  89  }
  90  
  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']);
  94  
  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);
 100  }
 101  
 102  echo $OUTPUT->footer();