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   * @package    backup-convert
  18   * @copyright  2011 Darko Miletic <dmiletic@moodlerooms.com>
  19   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20   */
  21  
  22  require_once ('cc_general.php');
  23  
  24  class page11_resurce_file extends general_cc_file {
  25      protected $rootns = 'xmlns';
  26      protected $rootname = 'html';
  27      protected $ccnamespaces = array('xmlns' => 'http://www.w3.org/1999/xhtml');
  28  
  29      protected $content = null;
  30      protected $title = null;
  31      protected $intro = null;
  32  
  33      public function set_content($value) {
  34          // We are not cleaning up this one on purpose.
  35          $this->content = $value;
  36      }
  37  
  38      public function set_title($value) {
  39          $this->title = self::safexml($value);
  40      }
  41  
  42      public function set_intro($value) {
  43          $this->intro = self::safexml(strip_tags($value));
  44      }
  45  
  46      protected function on_create() {
  47          $impl = new DOMImplementation();
  48          $dtd  = $impl->createDocumentType( 'html',
  49                                             '-//W3C//DTD XHTML 1.0 Strict//EN',
  50                                             'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
  51          $doc = $impl->createDocument($this->ccnamespaces[$this->rootns], null, $dtd);
  52          $doc->formatOutput = true;
  53          $doc->preserveWhiteSpace = true;
  54          $this->doc = $doc;
  55          parent::on_create();
  56      }
  57  
  58      public function on_save() {
  59  
  60          $rns = $this->ccnamespaces[$this->rootns];
  61          // Add the basic tags.
  62          $head = $this->append_new_element_ns($this->root, $rns, 'head');
  63          $this->append_new_attribute_ns($head, $rns, 'profile', 'http://dublincore.org/documents/dc-html/');
  64  
  65          // Linking Dublin Core Metadata 1.1.
  66          $link_dc = $this->append_new_element_ns($head, $rns, 'link');
  67          $this->append_new_attribute_ns($link_dc, $rns, 'rel', 'schema.DC');
  68          $this->append_new_attribute_ns($link_dc, $rns, 'href', 'http://purl.org/dc/elements/1.1/');
  69          $link_dcterms = $this->append_new_element_ns($head, $rns, 'link');
  70          $this->append_new_attribute_ns($link_dcterms, $rns, 'rel', 'schema.DCTERMS');
  71          $this->append_new_attribute_ns($link_dcterms, $rns, 'href', 'http://purl.org/dc/terms/');
  72          // Content type.
  73          $meta_type = $this->append_new_element_ns($head, $rns, 'meta');
  74          $this->append_new_attribute_ns($meta_type, $rns, 'name', 'DC.type');
  75          $this->append_new_attribute_ns($meta_type, $rns, 'scheme', 'DCTERMS.DCMIType');
  76          $this->append_new_attribute_ns($meta_type, $rns, 'content', 'Text');
  77  
  78          // Content description.
  79          if (!empty($this->intro)) {
  80              $meta_description = $this->append_new_element_ns($head, $rns, 'meta');
  81              $this->append_new_attribute_ns($meta_description, $rns, 'name', 'DC.description');
  82              $this->append_new_attribute_ns($meta_description, $rns, 'content', $this->intro);
  83          }
  84  
  85          $meta = $this->append_new_element_ns($head, $rns, 'meta');
  86          $this->append_new_attribute_ns($meta, $rns, 'http-equiv', 'Content-type');
  87          $this->append_new_attribute_ns($meta, $rns, 'content', 'text/html; charset=UTF-8');
  88          // Set the title.
  89          $title = $this->append_new_element_ns($head, $rns, 'title', $this->title);
  90          $body = $this->append_new_element_ns($this->root, $rns, 'body');
  91          // We are unable to use DOM for embedding HTML due to numerous content errors.
  92          // Therefore we place a dummy tag that will be later replaced with the real content.
  93          $this->append_new_element_ns($body, $rns, 'div', '##REPLACE##');
  94  
  95          return true;
  96      }
  97  
  98      public function saveTo($fname) {
  99          $result = $this->on_save();
 100          if ($result) {
 101              $dret = str_replace('<?xml version="1.0"?>'."\n", '', $this->viewXML());
 102              $dret = str_replace('<div>##REPLACE##</div>', $this->content, $dret);
 103              $result = (file_put_contents($fname, $dret) !== false);
 104              if ($result) {
 105                  $this->filename = $fname;
 106                  $this->processPath();
 107              }
 108          }
 109          return $result;
 110      }
 111  }