1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package moodlecore
20 * @subpackage backup-helper
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once($CFG->dirroot.'/backup/util/xml/parser/processors/grouped_parser_processor.class.php');
26
27/**
28 * helper implementation of grouped_parser_processor that will
29 * support the process of all the moodle2 backup files, with
30 * complete specs about what to load (grouped or no), dispatching
31 * to corresponding methods and basic decoding of contents
32 * (NULLs and legacy file.php uses)
33 *
34 * TODO: Complete phpdocs
35 */
36class restore_structure_parser_processor extends grouped_parser_processor {
37
38    protected $courseid; // Course->id we are restoring to
39    protected $step;     // @restore_structure_step using this processor
40
41    public function __construct($courseid, $step) {
42        $this->courseid = $courseid;
43        $this->step     = $step;
44        parent::__construct();
45    }
46
47    /**
48     * Provide NULL and legacy file.php uses decoding
49     */
50    public function process_cdata($cdata) {
51        global $CFG;
52        if ($cdata === '$@NULL@$') {  // Some cases we know we can skip complete processing
53            return null;
54        } else if ($cdata === '') {
55            return '';
56        } else if (is_numeric($cdata)) {
57            return $cdata;
58        } else if (strlen($cdata) < 32) { // Impossible to have one link in 32cc
59            return $cdata;                // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
60        }
61
62        if (strpos($cdata, '$@FILEPHP@$') !== false) {
63            // We need to convert $@FILEPHP@$.
64            if ($CFG->slasharguments) {
65                $slash = '/';
66                $forcedownload = '?forcedownload=1';
67            } else {
68                $slash = '%2F';
69                $forcedownload = '&amp;forcedownload=1';
70            }
71
72            // We have to remove trailing slashes, otherwise file URLs will be restored with an extra slash.
73            $basefileurl = rtrim(moodle_url::make_legacyfile_url($this->courseid, null)->out(true), $slash);
74            // Decode file.php calls.
75            $search = array ("$@FILEPHP@$");
76            $replace = array($basefileurl);
77            $result = str_replace($search, $replace, $cdata);
78
79            // Now $@SLASH@$ and $@FORCEDOWNLOAD@$ MDL-18799.
80            $search = array('$@SLASH@$', '$@FORCEDOWNLOAD@$');
81            $replace = array($slash, $forcedownload);
82
83            $cdata = str_replace($search, $replace, $result);
84        }
85
86        if (strpos($cdata, '$@H5PEMBED@$') !== false) {
87            // We need to convert $@H5PEMBED@$.
88            // Decode embed.php calls.
89            $cdata = str_replace('$@H5PEMBED@$', $CFG->wwwroot.'/h5p/embed.php', $cdata);
90        }
91
92        return $cdata;
93    }
94
95    /**
96     * Override this method so we'll be able to skip
97     * dispatching some well-known chunks, like the
98     * ones being 100% part of subplugins stuff. Useful
99     * for allowing development without having all the
100     * possible restore subplugins defined
101     */
102    protected function postprocess_chunk($data) {
103
104        // Iterate over all the data tags, if any of them is
105        // not 'subplugin_XXXX' or has value, then it's a valid chunk,
106        // pass it to standard (parent) processing of chunks.
107        foreach ($data['tags'] as $key => $value) {
108            if (trim($value) !== '' || strpos($key, 'subplugin_') !== 0) {
109                parent::postprocess_chunk($data);
110                return;
111            }
112        }
113        // Arrived here, all the tags correspond to sublplugins and are empty,
114        // skip the chunk, and debug_developer notice
115        $this->chunks--; // not counted
116        debugging('Missing support on restore for ' . clean_param($data['path'], PARAM_PATH) .
117                  ' subplugin (' . implode(', ', array_keys($data['tags'])) .')', DEBUG_DEVELOPER);
118    }
119
120    protected function dispatch_chunk($data) {
121        $this->step->process($data);
122    }
123
124    protected function notify_path_start($path) {
125        // nothing to do
126    }
127
128    protected function notify_path_end($path) {
129        // nothing to do
130    }
131}
132