Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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   * This is the main script for the complete XMLDB interface. From here
  19   * all the actions supported will be launched.
  20   *
  21   * @package    tool_xmldb
  22   * @copyright  (C) 1999 onwards Martin Dougiamas http://dougiamas.com,
  23   *             (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  require('../../../config.php');
  28  require_once($CFG->libdir.'/adminlib.php');
  29  require_once($CFG->libdir.'/ddllib.php');
  30  // Add required XMLDB action classes
  31  require_once ('actions/XMLDBAction.class.php');
  32  require_once ('actions/XMLDBCheckAction.class.php');
  33  
  34  
  35  admin_externalpage_setup('toolxmld');
  36  
  37  // Add other used libraries
  38  require_once($CFG->libdir . '/xmlize.php');
  39  
  40  // Handle session data
  41  global $XMLDB;
  42  
  43  // State is stored in session - we have to serialise it because the classes are not loaded when creating session
  44  if (!isset($SESSION->xmldb)) {
  45      $XMLDB = new stdClass;
  46  } else {
  47      $XMLDB = unserialize($SESSION->xmldb);
  48  }
  49  
  50  // Some previous checks
  51  $site = get_site();
  52  
  53  
  54  // Body of the script, based on action, we delegate the work
  55  $action = optional_param ('action', 'main_view', PARAM_ALPHAEXT);
  56  
  57  // Get the action path and invoke it
  58  $actionsroot = "$CFG->dirroot/$CFG->admin/tool/xmldb/actions";
  59  $actionclass = $action . '.class.php';
  60  $actionpath = "$actionsroot/$action/$actionclass";
  61  
  62  // Load and invoke the proper action
  63  if (file_exists($actionpath) && is_readable($actionpath)) {
  64      require_once($actionpath);
  65      if ($xmldb_action = new $action) {
  66          // Invoke it
  67          $result = $xmldb_action->invoke();
  68          // store the result in session
  69          $SESSION->xmldb = serialize($XMLDB);
  70  
  71          if ($result) {
  72              // Based on getDoesGenerate()
  73              switch ($xmldb_action->getDoesGenerate()) {
  74                  case ACTION_GENERATE_HTML:
  75  
  76                      $action = optional_param('action', '', PARAM_ALPHAEXT);
  77                      $postaction = optional_param('postaction', '', PARAM_ALPHAEXT);
  78                      // If the js exists, load it
  79                      if ($action) {
  80                          $script = $CFG->admin . '/tool/xmldb/actions/' . $action . '/' . $action . '.js';
  81                          $file = $CFG->dirroot . '/' . $script;
  82                          if (file_exists($file) && is_readable($file)) {
  83                              $PAGE->requires->js('/'.$script);
  84                          } else if ($postaction) {
  85                              // Try to load the postaction javascript if exists
  86                              $script = $CFG->admin . '/tool/xmldb/actions/' . $postaction . '/' . $postaction . '.js';
  87                              $file = $CFG->dirroot . '/' . $script;
  88                              if (file_exists($file) && is_readable($file)) {
  89                                  $PAGE->requires->js('/'.$script);
  90                              }
  91                          }
  92                      }
  93  
  94                      // Go with standard admin header
  95                      echo $OUTPUT->header();
  96                      echo $OUTPUT->heading($xmldb_action->getTitle());
  97                      echo $xmldb_action->getOutput();
  98                      echo $OUTPUT->footer();
  99                      break;
 100                  case ACTION_GENERATE_XML:
 101                      header('Content-type: application/xhtml+xml; charset=utf-8');
 102                      echo $xmldb_action->getOutput();
 103                      break;
 104              }
 105          } else {
 106              // TODO: need more detailed error info
 107              throw new \moodle_exception('xmldberror');
 108          }
 109      } else {
 110          $a = new stdClass();
 111          $a->action = $action;
 112          $a->actionclass = $actionclass;
 113          throw new \moodle_exception('cannotinstantiateclass', 'tool_xmldb', '', $a);
 114      }
 115  } else {
 116      throw new \moodle_exception('invalidaction');
 117  }
 118  
 119  if ($xmldb_action->getDoesGenerate() != ACTION_GENERATE_XML) {
 120      if (debugging()) {
 121          // print_object($XMLDB);
 122      }
 123  }