Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * @package    tool_xmldb
  19   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  /**
  24   * This class will save one edited xml file
  25   *
  26   * This class will save the in-session xml structure to its
  27   * corresponding xml file, optionally reloading it if editing
  28   * is going to continue (unload=false). Else (default) the
  29   * file is unloaded once saved.
  30   *
  31   * @package    tool_xmldb
  32   * @copyright  2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class save_xml_file extends XMLDBAction {
  36  
  37      /**
  38       * Init method, every subclass will have its own
  39       */
  40      function init() {
  41          parent::init();
  42  
  43          // Set own custom attributes
  44  
  45          // Get needed strings
  46          $this->loadStrings(array(
  47              'filenotwriteable' => 'tool_xmldb'
  48          ));
  49      }
  50  
  51      /**
  52       * Invoke method, every class will have its own
  53       * returns true/false on completion, setting both
  54       * errormsg and output as necessary
  55       */
  56      function invoke() {
  57          parent::invoke();
  58  
  59          $result = true;
  60  
  61          // Set own core attributes
  62          $this->does_generate = ACTION_NONE;
  63  
  64          // These are always here
  65          global $CFG, $XMLDB;
  66  
  67          // Do the job, setting result as needed
  68  
  69          // Get the dir containing the file
  70          $dirpath = required_param('dir', PARAM_PATH);
  71          $dirpath = $CFG->dirroot . $dirpath;
  72          $unload = optional_param('unload', true, PARAM_BOOL);
  73  
  74          // Get the edited dir
  75          if (!empty($XMLDB->editeddirs)) {
  76              if (isset($XMLDB->editeddirs[$dirpath])) {
  77                  $editeddir = $XMLDB->editeddirs[$dirpath];
  78              }
  79          }
  80          // Copy the edited dir over the original one
  81          if (!empty($XMLDB->dbdirs)) {
  82              if (isset($XMLDB->dbdirs[$dirpath])) {
  83                  $XMLDB->dbdirs[$dirpath] = unserialize(serialize($editeddir));
  84                  $dbdir = $XMLDB->dbdirs[$dirpath];
  85              }
  86          }
  87  
  88          // Check for perms
  89          if (!is_writeable($dirpath . '/install.xml')) {
  90              $this->errormsg = $this->str['filenotwriteable'] . '(' . $dirpath . '/install.xml)';
  91              return false;
  92          }
  93  
  94          // Save the original dir
  95          $result = $dbdir->xml_file->saveXMLFile();
  96  
  97          if ($result) {
  98              // Delete the edited dir
  99              unset ($XMLDB->editeddirs[$dirpath]);
 100              // Unload de originaldir
 101              unset($XMLDB->dbdirs[$dirpath]->xml_file);
 102              unset($XMLDB->dbdirs[$dirpath]->xml_loaded);
 103              unset($XMLDB->dbdirs[$dirpath]->xml_changed);
 104              unset($XMLDB->dbdirs[$dirpath]->xml_exists);
 105              unset($XMLDB->dbdirs[$dirpath]->xml_writeable);
 106          } else {
 107              $this->errormsg = 'Error saving XML file (' . $dirpath . ')';
 108              return false;
 109          }
 110  
 111          // If unload has been disabled, simulate it by reloading the file now
 112          if (!$unload) {
 113              return $this->launch('load_xml_file');
 114          }
 115  
 116          // Launch postaction if exists (leave this here!)
 117          if ($this->getPostAction() && $result) {
 118              return $this->launch($this->getPostAction());
 119          }
 120  
 121          // Return ok if arrived here
 122          return $result;
 123      }
 124  }
 125