Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/helper/backup_general_helper.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 
00032 abstract class backup_general_helper extends backup_helper {
00033 
00037     public static function array_checksum_recursive($arr) {
00038 
00039         $checksum = ''; // Init checksum
00040 
00041         // Check we are going to process one array always, objects must be cast before
00042         if (!is_array($arr)) {
00043             throw new backup_helper_exception('array_expected');
00044         }
00045         foreach ($arr as $key => $value) {
00046             if ($value instanceof checksumable) {
00047                 $checksum = md5($checksum . '-' . $key . '-' . $value->calculate_checksum());
00048             } else if (is_object($value)) {
00049                 $checksum = md5($checksum . '-' . $key . '-' . self::array_checksum_recursive((array)$value));
00050             } else if (is_array($value)) {
00051                 $checksum = md5($checksum . '-' . $key . '-' . self::array_checksum_recursive($value));
00052             } else {
00053                 $checksum = md5($checksum . '-' . $key . '-' . $value);
00054             }
00055         }
00056         return $checksum;
00057     }
00058 
00066     public static function get_blocks_from_path($path) {
00067         global $DB;
00068 
00069         $blocks = array(); // To return results
00070 
00071         static $availableblocks = array(); // Get and cache available blocks
00072         if (empty($availableblocks)) {
00073             $availableblocks = array_keys(get_plugin_list('block'));
00074         }
00075 
00076         $path = $path . '/blocks'; // Always look under blocks subdir
00077 
00078         if (!is_dir($path)) {
00079             return array();
00080         }
00081 
00082         $dir = opendir($path);
00083         while (false !== ($file = readdir($dir))) {
00084             if ($file == '.' || $file == '..') { // Skip dots
00085                 continue;
00086             }
00087             if (is_dir($path .'/' . $file)) { // Dir found, check it's a valid block
00088                 if (!file_exists($path .'/' . $file . '/block.xml')) { // Skip if xml file not found
00089                     continue;
00090                 }
00091                 // Extract block name
00092                 $blockname = preg_replace('/(.*)_\d+/', '\\1', $file);
00093                 // Check block exists and is installed
00094                 if (in_array($blockname, $availableblocks) && $DB->record_exists('block', array('name' => $blockname))) {
00095                     $blocks[$path .'/' . $file] = $blockname;
00096                 }
00097             }
00098         }
00099         closedir($dir);
00100 
00101         return $blocks;
00102     }
00103 
00112     public static function get_backup_information($tempdir) {
00113         global $CFG;
00114 
00115         $info = new stdclass(); // Final information goes here
00116 
00117         $moodlefile = $CFG->tempdir . '/backup/' . $tempdir . '/moodle_backup.xml';
00118         if (!file_exists($moodlefile)) { // Shouldn't happen ever, but...
00119             throw new backup_helper_exception('missing_moodle_backup_xml_file', $moodlefile);
00120         }
00121         // Load the entire file to in-memory array
00122         $xmlparser = new progressive_parser();
00123         $xmlparser->set_file($moodlefile);
00124         $xmlprocessor = new restore_moodlexml_parser_processor();
00125         $xmlparser->set_processor($xmlprocessor);
00126         $xmlparser->process();
00127         $infoarr = $xmlprocessor->get_all_chunks();
00128         if (count($infoarr) !== 1) { // Shouldn't happen ever, but...
00129             throw new backup_helper_exception('problem_parsing_moodle_backup_xml_file');
00130         }
00131         $infoarr = $infoarr[0]['tags']; // for commodity
00132 
00133         // Let's build info
00134         $info->moodle_version = $infoarr['moodle_version'];
00135         $info->moodle_release = $infoarr['moodle_release'];
00136         $info->backup_version = $infoarr['backup_version'];
00137         $info->backup_release = $infoarr['backup_release'];
00138         $info->backup_date    = $infoarr['backup_date'];
00139         $info->mnet_remoteusers         = $infoarr['mnet_remoteusers'];
00140         $info->original_wwwroot         = $infoarr['original_wwwroot'];
00141         $info->original_site_identifier_hash = $infoarr['original_site_identifier_hash'];
00142         $info->original_course_id       = $infoarr['original_course_id'];
00143         $info->original_course_fullname = $infoarr['original_course_fullname'];
00144         $info->original_course_shortname= $infoarr['original_course_shortname'];
00145         $info->original_course_startdate= $infoarr['original_course_startdate'];
00146         $info->original_course_contextid= $infoarr['original_course_contextid'];
00147         $info->original_system_contextid= $infoarr['original_system_contextid'];
00148         $info->type   =  $infoarr['details']['detail'][0]['type'];
00149         $info->format =  $infoarr['details']['detail'][0]['format'];
00150         $info->mode   =  $infoarr['details']['detail'][0]['mode'];
00151         // Build the role mappings custom object
00152         $rolemappings = new stdclass();
00153         $rolemappings->modified = false;
00154         $rolemappings->mappings = array();
00155         $info->role_mappings = $rolemappings;
00156         // Some initially empty containers
00157         $info->sections = array();
00158         $info->activities = array();
00159 
00160         // Now the contents
00161         $contentsarr = $infoarr['contents'];
00162         if (isset($contentsarr['course']) && isset($contentsarr['course'][0])) {
00163             $info->course = new stdclass();
00164             $info->course = (object)$contentsarr['course'][0];
00165             $info->course->settings = array();
00166         }
00167         if (isset($contentsarr['sections']) && isset($contentsarr['sections']['section'])) {
00168             $sectionarr = $contentsarr['sections']['section'];
00169             foreach ($sectionarr as $section) {
00170                 $section = (object)$section;
00171                 $section->settings = array();
00172                 $sections[basename($section->directory)] = $section;
00173             }
00174             $info->sections = $sections;
00175         }
00176         if (isset($contentsarr['activities']) && isset($contentsarr['activities']['activity'])) {
00177             $activityarr = $contentsarr['activities']['activity'];
00178             foreach ($activityarr as $activity) {
00179                 $activity = (object)$activity;
00180                 $activity->settings = array();
00181                 $activities[basename($activity->directory)] = $activity;
00182             }
00183             $info->activities = $activities;
00184         }
00185         $info->root_settings = array(); // For root settings
00186 
00187         // Now the settings, putting each one under its owner
00188         $settingsarr = $infoarr['settings']['setting'];
00189         foreach($settingsarr as $setting) {
00190             switch ($setting['level']) {
00191                 case 'root':
00192                     $info->root_settings[$setting['name']] = $setting['value'];
00193                     break;
00194                 case 'course':
00195                     $info->course->settings[$setting['name']] = $setting['value'];
00196                     break;
00197                 case 'section':
00198                     $info->sections[$setting['section']]->settings[$setting['name']] = $setting['value'];
00199                     break;
00200                 case 'activity':
00201                     $info->activities[$setting['activity']]->settings[$setting['name']] = $setting['value'];
00202                     break;
00203                 default: // Shouldn't happen
00204                     throw new backup_helper_exception('wrong_setting_level_moodle_backup_xml_file', $setting['level']);
00205             }
00206         }
00207 
00208         return $info;
00209     }
00210 
00222     public static function backup_is_samesite($info) {
00223         global $CFG;
00224         $hashedsiteid = md5(get_site_identifier());
00225         if (isset($info->original_site_identifier_hash) && !empty($info->original_site_identifier_hash)) {
00226             return $info->original_site_identifier_hash == $hashedsiteid;
00227         } else {
00228             return $info->original_wwwroot == $CFG->wwwroot;
00229         }
00230     }
00231 
00238     public static function detect_backup_format($tempdir) {
00239         global $CFG;
00240         require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php');
00241 
00242         if (convert_helper::detect_moodle2_format($tempdir)) {
00243             return backup::FORMAT_MOODLE;
00244         }
00245 
00246         // see if a converter can identify the format
00247         $converters = convert_helper::available_converters();
00248         foreach ($converters as $name) {
00249             $classname = "{$name}_converter";
00250             if (!class_exists($classname)) {
00251                 throw new coding_exception("available_converters() is supposed to load
00252                     converter classes but class $classname not found");
00253             }
00254 
00255             $detected = call_user_func($classname .'::detect_format', $tempdir);
00256             if (!empty($detected)) {
00257                 return $detected;
00258             }
00259         }
00260 
00261         return backup::FORMAT_UNKNOWN;
00262     }
00263 }
 All Data Structures Namespaces Files Functions Variables Enumerations