Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/moodle2/backup_xml_transformer.class.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00030 class backup_xml_transformer extends xml_contenttransformer {
00031 
00032     private $absolute_links_encoders; // array of static methods to be called in order to
00033                                       // perform the encoding of absolute links to all the
00034                                       // contents sent to xml
00035     private $courseid;                // courseid this content belongs to
00036     private $unicoderegexp;           // to know if the site supports unicode regexp
00037 
00038     public function __construct($courseid) {
00039         $this->absolute_links_encoders = array();
00040         $this->courseid = $courseid;
00041         // Check if we support unicode modifiers in regular expressions
00042         $this->unicoderegexp = @preg_match('/\pL/u', 'a'); // This will fail silently, returning false,
00043                                                            // if regexp libraries don't support unicode
00044         // Register all the available content link encoders
00045         $this->absolute_links_encoders = $this->register_link_encoders();
00046     }
00047 
00048     public function process($content) {
00049 
00050         // Array or object, debug and try our best recursively, shouldn't happen but...
00051         if (is_array($content)) {
00052             debugging('Backup XML transformer should process arrays but plain content always', DEBUG_DEVELOPER);
00053             foreach($content as $key => $plaincontent) {
00054                 $content[$key] = $this->process($plaincontent);
00055             }
00056             return $content;
00057         } else if (is_object($content)) {
00058             debugging('Backup XML transformer should not process objects but plain content always', DEBUG_DEVELOPER);
00059             foreach((array)$content as $key => $plaincontent) {
00060                 $content[$key] = $this->process($plaincontent);
00061             }
00062             return (object)$content;
00063         }
00064 
00065         if (is_null($content)) {  // Some cases we know we can skip complete processing
00066             return '$@NULL@$';
00067         } else if ($content === '') {
00068             return '';
00069         } else if (is_numeric($content)) {
00070             return $content;
00071         } else if (strlen($content) < 32) { // Impossible to have one link in 32cc
00072             return $content;                // (http://10.0.0.1/file.php/1/1.jpg, http://10.0.0.1/mod/url/view.php?id=)
00073         }
00074 
00075         $content = $this->process_filephp_links($content); // Replace all calls to file.php by $@FILEPHP@$ in a normalised way
00076         $content = $this->encode_absolute_links($content); // Pass the content against all the found encoders
00077 
00078         return $content;
00079     }
00080 
00081     private function process_filephp_links($content) {
00082         global $CFG;
00083 
00084         if (strpos($content, 'file.php') === false) { // No file.php, nothing to convert
00085             return $content;
00086         }
00087 
00088         //First, we check for every call to file.php inside the course
00089         $search = array($CFG->wwwroot.'/file.php/' . $this->courseid,
00090                         $CFG->wwwroot.'/file.php?file=/' . $this->courseid,
00091                         $CFG->wwwroot.'/file.php?file=%2f' . $this->courseid,
00092                         $CFG->wwwroot.'/file.php?file=%2F' . $this->courseid);
00093         $replace = array('$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$', '$@FILEPHP@$');
00094         $content = str_replace($search, $replace, $content);
00095 
00096         // Now we look for any '$@FILEPHP@$' URLs, replacing:
00097         //     - slashes and %2F by $@SLASH@$
00098         //     - &forcedownload=1 &amp;forcedownload=1 and ?forcedownload=1 by $@FORCEDOWNLOAD@$
00099         // This way, backup contents will be neutral and independent of slasharguments configuration. MDL-18799
00100         // Based in $this->unicoderegexp, decide the regular expression to use
00101         if ($this->unicoderegexp) { //We can use unicode modifiers
00102             $search = '/(\$@FILEPHP@\$)((?:(?:\/|%2f|%2F))(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*)?(\?(?:(?:(?:\([-;:@#&=\pL0-9\$~_.+!*\',]*?\))|[-;:@#&=?\pL0-9\$~_.+!*\',]|%[a-fA-F0-9]{2}|\/)*))?(?<![,.;])/u';
00103         } else { //We cannot ue unicode modifiers
00104             $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}|\/)*))?(?<![,.;])/';
00105         }
00106         $content = preg_replace_callback($search, array('backup_xml_transformer', 'process_filephp_uses'), $content);
00107 
00108         return $content;
00109     }
00110 
00111     private function encode_absolute_links($content) {
00112         foreach ($this->absolute_links_encoders as $classname => $methodname) {
00113             $content = call_user_func(array($classname, $methodname), $content);
00114         }
00115         return $content;
00116     }
00117 
00118     static private function process_filephp_uses($matches) {
00119 
00120         // Replace slashes (plain and encoded) and forcedownload=1 parameter
00121         $search = array('/', '%2f', '%2F', '?forcedownload=1', '&forcedownload=1', '&amp;forcedownload=1');
00122         $replace = array('$@SLASH@$', '$@SLASH@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$', '$@FORCEDOWNLOAD@$');
00123 
00124         $result = $matches[1] . (isset($matches[2]) ? str_replace($search, $replace, $matches[2]) : '') . (isset($matches[3]) ? str_replace($search, $replace, $matches[3]) : '');
00125 
00126         return $result;
00127     }
00128 
00129     private function register_link_encoders() {
00130         $encoders = array();
00131 
00132         // Add the course encoder
00133         $encoders['backup_course_task'] = 'encode_content_links';
00134 
00135         // Add the module ones. Each module supporting moodle2 backups MUST have it
00136         $mods = get_plugin_list('mod');
00137         foreach ($mods as $mod => $moddir) {
00138             if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
00139                 $encoders['backup_' . $mod . '_activity_task'] = 'encode_content_links';
00140             }
00141         }
00142 
00143         // Add the block encoders
00144         $blocks = get_plugin_list('block');
00145         foreach ($blocks as $block => $blockdir) {
00146             if (class_exists('backup_' . $block . '_block_task')) {
00147                 $encoders['backup_' . $block . '_block_task'] = 'encode_content_links';
00148             }
00149         }
00150 
00151         // Add the course format encodes
00152         // TODO: Same than blocks, need to know how courseformats are going to handle backup
00153         //       (1.9 was based in backuplib function, see code)
00154 
00155         // Add local encodes
00156         // TODO: Any interest? 1.9 never had that.
00157 
00158         return $encoders;
00159     }
00160 }
 All Data Structures Namespaces Files Functions Variables Enumerations