MDL-27120 Add global cache to backup_xml_transformer::register_link_encoders()
[moodle.git] / backup / moodle2 / backup_xml_transformer.class.php
CommitLineData
77547b46
EL
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/**
58b322cf
DM
19 * Defines backup_xml_transformer class
20 *
21 * @package core_backup
22 * @subpackage moodle2
23 * @category backup
24 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
77547b46
EL
26 */
27
58b322cf
DM
28defined('MOODLE_INTERNAL') || die();
29
77547b46
EL
30/**
31 * Class implementing the @xml_contenttrasnformed logic to be applied in moodle2 backups
32 *
33 * TODO: Finish phpdocs
34 */
d729ef29
AH
35
36// cache for storing link encoders, so that we don't need to call
37// register_link_encoders each time backup_xml_transformer is constructed
38global $LINKS_ENCODERS_CACHE;
39$LINKS_ENCODERS_CACHE = array();
40
77547b46
EL
41class backup_xml_transformer extends xml_contenttransformer {
42
43 private $absolute_links_encoders; // array of static methods to be called in order to
44 // perform the encoding of absolute links to all the
45 // contents sent to xml
46 private $courseid; // courseid this content belongs to
47 private $unicoderegexp; // to know if the site supports unicode regexp
48
49 public function __construct($courseid) {
d729ef29
AH
50 global $LINKS_ENCODERS_CACHE;
51
77547b46
EL
52 $this->absolute_links_encoders = array();
53 $this->courseid = $courseid;
54 // Check if we support unicode modifiers in regular expressions
39b5371c 55 $this->unicoderegexp = @preg_match('/\pL/u', 'a'); // This will fail silently, returning false,
77547b46
EL
56 // if regexp libraries don't support unicode
57 // Register all the available content link encoders
d729ef29
AH
58 if (!empty($LINKS_ENCODERS_CACHE)) {
59 $LINKS_ENCODERS_CACHE = $this->register_link_encoders();
60 }
61 $this->absolute_links_encoders = $LINKS_ENCODERS_CACHE;
77547b46
EL
62 }
63
64 public function process($content) {
65
5f8354eb
EL
66 // Array or object, debug and try our best recursively, shouldn't happen but...
67 if (is_array($content)) {
68 debugging('Backup XML transformer should process arrays but plain content always', DEBUG_DEVELOPER);
69 foreach($content as $key => $plaincontent) {
70 $content[$key] = $this->process($plaincontent);
71 }
72 return $content;
73 } else if (is_object($content)) {
74 debugging('Backup XML transformer should not process objects but plain content always', DEBUG_DEVELOPER);
75 foreach((array)$content as $key => $plaincontent) {
76 $content[$key] = $this->process($plaincontent);
77 }
78 return (object)$content;
79 }
80
77547b46
EL
81 if (is_null($content)) { // Some cases we know we can skip complete processing
82 return '$@NULL@$';
83 } else if ($content === '') {
84 return '';
85 } else if (is_numeric($content)) {
86 return $content;
767ae73d
EL
87 } else if (strlen($content) < 32) { // Impossible to have one link in 32cc
88 return $content; // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
77547b46
EL
89 }
90
91 $content = $this->process_filephp_links($content); // Replace all calls to file.php by $@FILEPHP@$ in a normalised way
92 $content = $this->encode_absolute_links($content); // Pass the content against all the found encoders
93
94 return $content;
95 }
96
97 private function process_filephp_links($content) {
98 global $CFG;
99
767ae73d
EL
100 if (strpos($content, 'file.php') === false) { // No file.php, nothing to convert
101 return $content;
102 }
103
77547b46
EL
104 //First, we check for every call to file.php inside the course
105 $search = array($CFG->wwwroot.'/file.php/' . $this->courseid,
106 $CFG->wwwroot.'/file.php?file=/' . $this->courseid,
107 $CFG->wwwroot.'/file.php?file=%2f' . $this->courseid,
108 $CFG->wwwroot.'/file.php?file=%2F' . $this->courseid);
109 $replace = array('$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$');
110 $content = str_replace($search, $replace, $content);
111
112 // Now we look for any '$@FILEPHP@$' URLs, replacing:
113 // - slashes and %2F by $@SLASH@$
114 // - &forcedownload=1 &amp;forcedownload=1 and ?forcedownload=1 by $@FORCEDOWNLOAD@$
115 // This way, backup contents will be neutral and independent of slasharguments configuration. MDL-18799
116 // Based in $this->unicoderegexp, decide the regular expression to use
117 if ($this->unicoderegexp) { //We can use unicode modifiers
118 $search = '/(\$@FILEPHP@\$)((?:(?:\/|%2f|%2F))(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*)?(\?(?:(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=?\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*))?(?<![,.;])/u';
119 } else { //We cannot ue unicode modifiers
120 $search = '/(\$@FILEPHP@\$)((?:(?:\/|%2f|%2F))(?:(?:\([-;:@#&=a-zA-Z0-9\$~_.+!*\',]*?\))|[-;:@#&=a-zA-Z0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*)?(\?(?:(?:(?:\([-;:@#&=a-zA-Z0-9\$~_.+!*\',]*?\))|[-;:@#&=?a-zA-Z0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*))?(?<![,.;])/';
121 }
122 $content = preg_replace_callback($search, array('backup_xml_transformer', 'process_filephp_uses'), $content);
123
124 return $content;
125 }
126
127 private function encode_absolute_links($content) {
128 foreach ($this->absolute_links_encoders as $classname => $methodname) {
129 $content = call_user_func(array($classname, $methodname), $content);
130 }
131 return $content;
132 }
133
134 static private function process_filephp_uses($matches) {
135
136 // Replace slashes (plain and encoded) and forcedownload=1 parameter
137 $search = array('/', '%2f', '%2F', '?forcedownload=1', '&forcedownload=1', '&amp;forcedownload=1');
138 $replace = array('$@SLASH@$', '$@SLASH@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$');
139
140 $result = $matches[1] . (isset($matches[2]) ? str_replace($search, $replace, $matches[2]) : '') . (isset($matches[3]) ? str_replace($search, $replace, $matches[3]) : '');
141
142 return $result;
143 }
144
145 private function register_link_encoders() {
146 $encoders = array();
147
9dbe1980
EL
148 // Add the course encoder
149 $encoders['backup_course_task'] = 'encode_content_links';
150
151 // Add the module ones. Each module supporting moodle2 backups MUST have it
77547b46
EL
152 $mods = get_plugin_list('mod');
153 foreach ($mods as $mod => $moddir) {
154 if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
155 $encoders['backup_' . $mod . '_activity_task'] = 'encode_content_links';
156 }
157 }
158
9dbe1980 159 // Add the block encoders
77547b46
EL
160 $blocks = get_plugin_list('block');
161 foreach ($blocks as $block => $blockdir) {
162 if (class_exists('backup_' . $block . '_block_task')) {
163 $encoders['backup_' . $block . '_block_task'] = 'encode_content_links';
164 }
165 }
166
167 // Add the course format encodes
168 // TODO: Same than blocks, need to know how courseformats are going to handle backup
169 // (1.9 was based in backuplib function, see code)
170
171 // Add local encodes
172 // TODO: Any interest? 1.9 never had that.
173
174 return $encoders;
175 }
176}