/moodle1917/backup/restorelib.php
PHP | 9516 lines | 6546 code | 988 blank | 1982 comment | 1802 complexity | ce73768a77d9254fda3096220aa17ac7 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <?php //$Id: restorelib.php,v 1.283.2.98 2011/10/19 19:39:26 moodlerobot Exp $
- //Functions used in restore
- require_once($CFG->libdir.'/gradelib.php');
- /**
- * Group backup/restore constants, 0.
- */
- define('RESTORE_GROUPS_NONE', 0);
- /**
- * Group backup/restore constants, 1.
- */
- define('RESTORE_GROUPS_ONLY', 1);
- /**
- * Group backup/restore constants, 2.
- */
- define('RESTORE_GROUPINGS_ONLY', 2);
- /**
- * Group backup/restore constants, course/all.
- */
- define('RESTORE_GROUPS_GROUPINGS', 3);
- //This function unzips a zip file in the same directory that it is
- //It automatically uses pclzip or command line unzip
- function restore_unzip ($file) {
- return unzip_file($file, '', false);
- }
- //This function checks if moodle.xml seems to be a valid xml file
- //(exists, has an xml header and a course main tag
- function restore_check_moodle_file ($file) {
- $status = true;
- //Check if it exists
- if ($status = is_file($file)) {
- //Open it and read the first 200 bytes (chars)
- $handle = fopen ($file, "r");
- $first_chars = fread($handle,200);
- $status = fclose ($handle);
- //Chek if it has the requires strings
- if ($status) {
- $status = strpos($first_chars,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
- if ($status !== false) {
- $status = strpos($first_chars,"<MOODLE_BACKUP>");
- }
- }
- }
- return $status;
- }
- //This function iterates over all modules in backup file, searching for a
- //MODNAME_refresh_events() to execute. Perhaps it should ve moved to central Moodle...
- function restore_refresh_events($restore) {
- global $CFG;
- $status = true;
- //Take all modules in backup
- $modules = $restore->mods;
- //Iterate
- foreach($modules as $name => $module) {
- //Only if the module is being restored
- if (isset($module->restore) && $module->restore == 1) {
- //Include module library
- include_once("$CFG->dirroot/mod/$name/lib.php");
- //If module_refresh_events exists
- $function_name = $name."_refresh_events";
- if (function_exists($function_name)) {
- $status = $function_name($restore->course_id);
- }
- }
- }
- return $status;
- }
- //This function makes all the necessary calls to xxxx_decode_content_links_caller()
- //function in each module/block/course format..., passing them the desired contents to be decoded
- //from backup format to destination site/course in order to mantain inter-activities
- //working in the backup/restore process
- function restore_decode_content_links($restore) {
- global $CFG;
- $status = true;
- if (!defined('RESTORE_SILENTLY')) {
- echo "<ul>";
- }
- // Recode links in the course summary.
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>' . get_string('from') . ' ' . get_string('course');
- }
- $course = get_record('course', 'id', $restore->course_id, '', '', '', '', 'id,summary');
- $coursesummary = backup_todb($course->summary,false); // Exception: Process FILEPHP (not available when restored) MDL-18222
- $coursesummary = restore_decode_content_links_worker($coursesummary, $restore);
- if ($coursesummary != $course->summary) {
- $course->summary = addslashes($coursesummary);
- if (!update_record('course', $course)) {
- $status = false;
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- // Recode links in section summaries.
- $sections = get_records('course_sections', 'course', $restore->course_id, 'id', 'id,summary');
- if ($sections) {
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>' . get_string('from') . ' ' . get_string('sections');
- }
- foreach ($sections as $section) {
- $sectionsummary = restore_decode_content_links_worker($section->summary, $restore);
- if ($sectionsummary != $section->summary) {
- $section->summary = addslashes($sectionsummary);
- if (!update_record('course_sections', $section)) {
- $status = false;
- }
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- }
- // Restore links in modules.
- foreach ($restore->mods as $name => $info) {
- //If the module is being restored
- if (isset($info->restore) && $info->restore == 1) {
- //Check if the xxxx_decode_content_links_caller exists
- include_once("$CFG->dirroot/mod/$name/restorelib.php");
- $function_name = $name."_decode_content_links_caller";
- if (function_exists($function_name)) {
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string ("from")." ".get_string("modulenameplural",$name);
- }
- $status = $function_name($restore) && $status;
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- }
- }
- }
- // For the course format call its decode_content_links method (if it exists)
- $format = get_field('course', 'format', 'id', $restore->course_id);
- if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
- include_once("$CFG->dirroot/course/format/$format/restorelib.php");
- $function_name = $format.'_decode_format_content_links_caller';
- if (function_exists($function_name)) {
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string ("from")." ".get_string("format").' '.$format;
- }
- $status = $function_name($restore);
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- }
- }
- // Process all html text also in blocks too
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>'.get_string ('from').' '.get_string('blocks');
- }
- if ($blocks = get_records('block', 'visible', 1)) {
- foreach ($blocks as $block) {
- if ($blockobject = block_instance($block->name)) {
- $blockobject->decode_content_links_caller($restore);
- }
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- // Restore links in questions.
- require_once("$CFG->dirroot/question/restorelib.php");
- if (!defined('RESTORE_SILENTLY')) {
- echo '<li>' . get_string('from') . ' ' . get_string('questions', 'quiz');
- }
- $status = question_decode_content_links_caller($restore) && $status;
- if (!defined('RESTORE_SILENTLY')) {
- echo '</li>';
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo "</ul>";
- }
- return $status;
- }
- /**
- * This function decodes some well-know links to course
- */
- function course_decode_content_links($content, $restore) {
- global $CFG;
- // Links to course
- $searchstring = '/\$@(COURSEVIEWBYID)\*([0-9]+)@\$/';
- // We look for it
- preg_match_all($searchstring, $content, $foundset);
- // If found, then we are going to look for its new id (in backup tables)
- if ($foundset[0]) {
- // Iterate over foundset[2]. They are the old_ids
- foreach ($foundset[2] as $old_id) {
- // We get the needed variables here (course id)
- $rec = backup_getid($restore->backup_unique_code, 'course', $old_id);
- // Personalize the searchstring
- $searchstring = '/\$@(COURSEVIEWBYID)\*('.$old_id.')@\$/';
- // If it is a link to itself, replace it
- if (!empty($rec->new_id)) {
- $replacestring = $CFG->wwwroot . '/course/view.php?id=' . $rec->new_id;
- // It s a link to another course, keep it unmodified
- } else {
- $replacestring = $restore->original_wwwroot . '/course/view.php?id=' . $old_id;
- }
- // Perform the replacement
- $content = preg_replace($searchstring, $replacestring, $content);
- }
- }
- return $content;
- }
- //This function is called from all xxxx_decode_content_links_caller(),
- //its task is to ask all modules (maybe other linkable objects) to restore
- //links to them.
- function restore_decode_content_links_worker($content,$restore) {
- global $CFG;
- // Course links decoder
- $content = course_decode_content_links($content, $restore);
- // Module links decoders
- foreach($restore->mods as $name => $info) {
- $function_name = $name."_decode_content_links";
- if (function_exists($function_name)) {
- $content = $function_name($content,$restore);
- }
- }
- // For the current format, call decode_format_content_links if it exists
- static $format_function_name;
- if (!isset($format_function_name)) {
- $format_function_name = false;
- if ($format = get_field('course', 'format', 'id', $restore->course_id)) {
- if (file_exists("$CFG->dirroot/course/format/$format/restorelib.php")) {
- include_once("$CFG->dirroot/course/format/$format/restorelib.php");
- $function_name = $format.'_decode_format_content_links';
- if (function_exists($function_name)) {
- $format_function_name = $function_name;
- }
- }
- }
- }
- // If the above worked - then we have a function to call
- if ($format_function_name) {
- $content = $format_function_name($content, $restore);
- }
- // For each block, call its encode_content_links method
- static $blockobjects = null;
- if (!isset($blockobjects)) {
- $blockobjects = array();
- if ($blocks = get_records('block', 'visible', 1)) {
- foreach ($blocks as $block) {
- if ($blockobject = block_instance($block->name)) {
- $blockobjects[] = $blockobject;
- }
- }
- }
- }
-
- foreach ($blockobjects as $blockobject) {
- $content = $blockobject->decode_content_links($content,$restore);
- }
- return $content;
- }
- //This function converts all the wiki texts in the restored course
- //to the Markdown format. Used only for backup files prior 2005041100.
- //It calls every module xxxx_convert_wiki2markdown function
- function restore_convert_wiki2markdown($restore) {
- $status = true;
- if (!defined('RESTORE_SILENTLY')) {
- echo "<ul>";
- }
- foreach ($restore->mods as $name => $info) {
- //If the module is being restored
- if ($info->restore == 1) {
- //Check if the xxxx_restore_wiki2markdown exists
- $function_name = $name."_restore_wiki2markdown";
- if (function_exists($function_name)) {
- $status = $function_name($restore);
- if (!defined('RESTORE_SILENTLY')) {
- echo "<li>".get_string("modulenameplural",$name);
- echo '</li>';
- }
- }
- }
- }
- if (!defined('RESTORE_SILENTLY')) {
- echo "</ul>";
- }
- return $status;
- }
- //This function receives a wiki text in the restore process and
- //return it with every link to modules " modulename:moduleid"
- //converted if possible. See the space before modulename!!
- function restore_decode_wiki_content($content,$restore) {
- global $CFG;
- $result = $content;
- $searchstring='/ ([a-zA-Z]+):([0-9]+)\(([^)]+)\)/';
- //We look for it
- preg_match_all($searchstring,$content,$foundset);
- //If found, then we are going to look for its new id (in backup tables)
- if ($foundset[0]) {
- //print_object($foundset); //Debug
- //Iterate over foundset[2]. They are the old_ids
- foreach($foundset[2] as $old_id) {
- //We get the needed variables here (course id)
- $rec = backup_getid($restore->backup_unique_code,"course_modules",$old_id);
- //Personalize the searchstring
- $searchstring='/ ([a-zA-Z]+):'.$old_id.'\(([^)]+)\)/';
- //If it is a link to this course, update the link to its new location
- if($rec->new_id) {
- //Now replace it
- $result= preg_replace($searchstring,' $1:'.$rec->new_id.'($2)',$result);
- } else {
- //It's a foreign link so redirect it to its original URL
- $result= preg_replace($searchstring,$restore->original_wwwroot.'/mod/$1/view.php?id='.$old_id.'($2)',$result);
- }
- }
- }
- return $result;
- }
- //This function read the xml file and store it data from the info zone in an object
- function restore_read_xml_info ($xml_file) {
- //We call the main read_xml function, with todo = INFO
- $info = restore_read_xml ($xml_file,"INFO",false);
- return $info;
- }
- //This function read the xml file and store it data from the course header zone in an object
- function restore_read_xml_course_header ($xml_file) {
- //We call the main read_xml function, with todo = COURSE_HEADER
- $info = restore_read_xml ($xml_file,"COURSE_HEADER",false);
- return $info;
- }
- //This function read the xml file and store its data from the blocks in a object
- function restore_read_xml_blocks ($restore, $xml_file) {
- //We call the main read_xml function, with todo = BLOCKS
- $info = restore_read_xml ($xml_file,'BLOCKS',$restore);
- return $info;
- }
- //This function read the xml file and store its data from the sections in a object
- function restore_read_xml_sections ($xml_file) {
- //We call the main read_xml function, with todo = SECTIONS
- $info = restore_read_xml ($xml_file,"SECTIONS",false);
- return $info;
- }
- //This function read the xml file and store its data from the course format in an object
- function restore_read_xml_formatdata ($xml_file) {
- //We call the main read_xml function, with todo = FORMATDATA
- $info = restore_read_xml ($xml_file,'FORMATDATA',false);
- return $info;
- }
- //This function read the xml file and store its data from the metacourse in a object
- function restore_read_xml_metacourse ($xml_file) {
- //We call the main read_xml function, with todo = METACOURSE
- $info = restore_read_xml ($xml_file,"METACOURSE",false);
- return $info;
- }
- //This function read the xml file and store its data from the gradebook in a object
- function restore_read_xml_gradebook ($restore, $xml_file) {
- //We call the main read_xml function, with todo = GRADEBOOK
- $info = restore_read_xml ($xml_file,"GRADEBOOK",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the users in
- //backup_ids->info db (and user's id in $info)
- function restore_read_xml_users ($restore,$xml_file) {
- //We call the main read_xml function, with todo = USERS
- $info = restore_read_xml ($xml_file,"USERS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the messages in
- //backup_ids->message backup_ids->message_read and backup_ids->contact and db (and their counters in info)
- function restore_read_xml_messages ($restore,$xml_file) {
- //We call the main read_xml function, with todo = MESSAGES
- $info = restore_read_xml ($xml_file,"MESSAGES",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the blogs in
- //backup_ids->blog and backup_ids->blog_tag and db (and their counters in info)
- function restore_read_xml_blogs ($restore,$xml_file) {
- //We call the main read_xml function, with todo = BLOGS
- $info = restore_read_xml ($xml_file,"BLOGS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the questions in
- //backup_ids->info db (and category's id in $info)
- function restore_read_xml_questions ($restore,$xml_file) {
- //We call the main read_xml function, with todo = QUESTIONS
- $info = restore_read_xml ($xml_file,"QUESTIONS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the scales in
- //backup_ids->info db (and scale's id in $info)
- function restore_read_xml_scales ($restore,$xml_file) {
- //We call the main read_xml function, with todo = SCALES
- $info = restore_read_xml ($xml_file,"SCALES",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the groups in
- //backup_ids->info db (and group's id in $info)
- function restore_read_xml_groups ($restore,$xml_file) {
- //We call the main read_xml function, with todo = GROUPS
- $info = restore_read_xml ($xml_file,"GROUPS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the groupings in
- //backup_ids->info db (and grouping's id in $info)
- function restore_read_xml_groupings ($restore,$xml_file) {
- //We call the main read_xml function, with todo = GROUPINGS
- $info = restore_read_xml ($xml_file,"GROUPINGS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the groupings in
- //backup_ids->info db (and grouping's id in $info)
- function restore_read_xml_groupings_groups ($restore,$xml_file) {
- //We call the main read_xml function, with todo = GROUPINGS
- $info = restore_read_xml ($xml_file,"GROUPINGSGROUPS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the events (course) in
- //backup_ids->info db (and event's id in $info)
- function restore_read_xml_events ($restore,$xml_file) {
- //We call the main read_xml function, with todo = EVENTS
- $info = restore_read_xml ($xml_file,"EVENTS",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the modules in
- //backup_ids->info
- function restore_read_xml_modules ($restore,$xml_file) {
- //We call the main read_xml function, with todo = MODULES
- $info = restore_read_xml ($xml_file,"MODULES",$restore);
- return $info;
- }
- //This function read the xml file and store its data from the logs in
- //backup_ids->info
- function restore_read_xml_logs ($restore,$xml_file) {
- //We call the main read_xml function, with todo = LOGS
- $info = restore_read_xml ($xml_file,"LOGS",$restore);
- return $info;
- }
- function restore_read_xml_roles ($xml_file) {
- //We call the main read_xml function, with todo = ROLES
- $info = restore_read_xml ($xml_file,"ROLES",false);
- return $info;
- }
- //This function prints the contents from the info parammeter passed
- function restore_print_info ($info) {
- global $CFG;
- $status = true;
- if ($info) {
- $table = new object();
- //This is tha align to every ingo table
- $table->align = array ("right","left");
- //This is the nowrap clause
- $table->wrap = array ("","nowrap");
- //The width
- $table->width = "70%";
- //Put interesting info in table
- //The backup original name
- $tab[0][0] = "<b>".get_string("backuporiginalname").":</b>";
- $tab[0][1] = $info->backup_name;
- //The moodle version
- $tab[1][0] = "<b>".get_string("moodleversion").":</b>";
- $tab[1][1] = $info->backup_moodle_release." (".$info->backup_moodle_version.")";
- //The backup version
- $tab[2][0] = "<b>".get_string("backupversion").":</b>";
- $tab[2][1] = $info->backup_backup_release." (".$info->backup_backup_version.")";
- //The backup date
- $tab[3][0] = "<b>".get_string("backupdate").":</b>";
- $tab[3][1] = userdate($info->backup_date);
- //Is this the same Moodle install?
- if (!empty($info->original_siteidentifier)) {
- $tab[4][0] = "<b>".get_string("backupfromthissite").":</b>";
- if (backup_is_same_site($info)) {
- $tab[4][1] = get_string('yes');
- } else {
- $tab[4][1] = get_string('no');
- }
- }
- //Print title
- print_heading(get_string("backup").":");
- $table->data = $tab;
- //Print backup general info
- print_table($table);
- if ($info->backup_backup_version <= 2005070500) {
- notify(get_string('backupnonisowarning')); // Message informing that this backup may not work!
- }
- //Now backup contents in another table
- $tab = array();
- //First mods info
- $mods = $info->mods;
- $elem = 0;
- foreach ($mods as $key => $mod) {
- $tab[$elem][0] = "<b>".get_string("modulenameplural",$key).":</b>";
- if ($mod->backup == "false") {
- $tab[$elem][1] = get_string("notincluded");
- } else {
- if ($mod->userinfo == "true") {
- $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
- } else {
- $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
- }
- if (isset($mod->instances) && is_array($mod->instances) && count($mod->instances)) {
- foreach ($mod->instances as $instance) {
- if ($instance->backup) {
- $elem++;
- $tab[$elem][0] = $instance->name;
- if ($instance->userinfo == 'true') {
- $tab[$elem][1] = get_string("included")." ".get_string("withuserdata");
- } else {
- $tab[$elem][1] = get_string("included")." ".get_string("withoutuserdata");
- }
- }
- }
- }
- }
- $elem++;
- }
- //Metacourse info
- $tab[$elem][0] = "<b>".get_string("metacourse").":</b>";
- if ($info->backup_metacourse == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //Users info
- $tab[$elem][0] = "<b>".get_string("users").":</b>";
- $tab[$elem][1] = get_string($info->backup_users);
- $elem++;
- //Logs info
- $tab[$elem][0] = "<b>".get_string("logs").":</b>";
- if ($info->backup_logs == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //User Files info
- $tab[$elem][0] = "<b>".get_string("userfiles").":</b>";
- if ($info->backup_user_files == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //Course Files info
- $tab[$elem][0] = "<b>".get_string("coursefiles").":</b>";
- if ($info->backup_course_files == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //site Files info
- $tab[$elem][0] = "<b>".get_string("sitefiles").":</b>";
- if (isset($info->backup_site_files) && $info->backup_site_files == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //gradebook history info
- $tab[$elem][0] = "<b>".get_string('gradebookhistories', 'grades').":</b>";
- if (isset($info->gradebook_histories) && $info->gradebook_histories == "true") {
- $tab[$elem][1] = get_string("yes");
- } else {
- $tab[$elem][1] = get_string("no");
- }
- $elem++;
- //Messages info (only showed if present)
- if ($info->backup_messages == 'true') {
- $tab[$elem][0] = "<b>".get_string('messages','message').":</b>";
- $tab[$elem][1] = get_string('yes');
- $elem++;
- } else {
- //Do nothing
- }
- $elem++;
- //Blogs info (only showed if present)
- if (isset($info->backup_blogs) && $info->backup_blogs == 'true') {
- $tab[$elem][0] = "<b>".get_string('blogs','blog').":</b>";
- $tab[$elem][1] = get_string('yes');
- $elem++;
- } else {
- //Do nothing
- }
- $table->data = $tab;
- //Print title
- print_heading(get_string("backupdetails").":");
- //Print backup general info
- print_table($table);
- } else {
- $status = false;
- }
- return $status;
- }
- //This function prints the contents from the course_header parammeter passed
- function restore_print_course_header ($course_header) {
- $status = true;
- if ($course_header) {
- $table = new object();
- //This is tha align to every ingo table
- $table->align = array ("right","left");
- //The width
- $table->width = "70%";
- //Put interesting course header in table
- //The course name
- $tab[0][0] = "<b>".get_string("name").":</b>";
- $tab[0][1] = $course_header->course_fullname." (".$course_header->course_shortname.")";
- //The course summary
- $tab[1][0] = "<b>".get_string("summary").":</b>";
- $tab[1][1] = $course_header->course_summary;
- $table->data = $tab;
- //Print title
- print_heading(get_string("course").":");
- //Print backup course header info
- print_table($table);
- } else {
- $status = false;
- }
- return $status;
- }
- /**
- * Given one user object (from backup file), perform all the neccesary
- * checks is order to decide how that user will be handled on restore.
- *
- * Note the function requires $user->mnethostid to be already calculated
- * so it's caller responsibility to set it
- *
- * This function is used both by @restore_precheck_users() and
- * @restore_create_users() to get consistent results in both places
- *
- * It returns:
- * - one user object (from DB), if match has been found and user will be remapped
- * - boolean true if the user needs to be created
- * - boolean false if some conflict happened and the user cannot be handled
- *
- * Each test is responsible for returning its results and interrupt
- * execution. At the end, boolean true (user needs to be created) will be
- * returned if no test has interrupted that.
- *
- * Here it's the logic applied, keep it updated:
- *
- * If restoring users from same site backup:
- * 1A - Normal check: If match by id and username and mnethost => ok, return target user
- * 1B - Handle users deleted in DB and "alive" in backup file:
- * If match by id and mnethost and user is deleted in DB and
- * (match by username LIKE 'backup_email.%' or by non empty email = md5(username)) => ok, return target user
- * 1C - Handle users deleted in backup file and "alive" in DB:
- * If match by id and mnethost and user is deleted in backup file
- * and match by email = email_without_time(backup_email) => ok, return target user
- * 1D - Conflict: If match by username and mnethost and doesn't match by id => conflict, return false
- * 1E - None of the above, return true => User needs to be created
- *
- * if restoring from another site backup (cannot match by id here, replace it by email/firstaccess combination):
- * 2A - Normal check: If match by username and mnethost and (email or non-zero firstaccess) => ok, return target user
- * 2B - Handle users deleted in DB and "alive" in backup file:
- * 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
- * (username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
- * 2B2 - If match by mnethost and user is deleted in DB and
- * username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
- * (to cover situations were md5(username) wasn't implemented on delete we requiere both)
- * 2C - Handle users deleted in backup file and "alive" in DB:
- * If match mnethost and user is deleted in backup file
- * and by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
- * 2D - Conflict: If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
- * 2E - None of the above, return true => User needs to be created
- *
- * Note: for DB deleted users email is stored in username field, hence we
- * are looking there for emails. See delete_user()
- * Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
- * hence we are looking there for usernames if not empty. See delete_user()
- */
- function restore_check_user($restore, $user) {
- global $CFG;
- // Verify mnethostid is set, return error if not
- // it's parent responsibility to define that before
- // arriving here
- if (empty($user->mnethostid)) {
- debugging("restore_check_user() wrong use, mnethostid not set for user $user->username", DEBUG_DEVELOPER);
- return false;
- }
- // Handle checks from same site backups
- if (backup_is_same_site($restore) && empty($CFG->forcedifferentsitecheckingusersonrestore)) {
- // 1A - If match by id and username and mnethost => ok, return target user
- if ($rec = get_record('user', 'id', $user->id, 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
- return $rec; // Matching user found, return it
- }
- // 1B - Handle users deleted in DB and "alive" in backup file
- // Note: for DB deleted users email is stored in username field, hence we
- // are looking there for emails. See delete_user()
- // Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
- // hence we are looking there for usernames if not empty. See delete_user()
- // If match by id and mnethost and user is deleted in DB and
- // match by username LIKE 'backup_email.%' or by non empty email = md5(username) => ok, return target user
- if ($rec = get_record_sql("SELECT *
- FROM {$CFG->prefix}user u
- WHERE id = $user->id
- AND mnethostid = $user->mnethostid
- AND deleted = 1
- AND (
- username LIKE '".addslashes($user->email).".%'
- OR (
- ".sql_isnotempty('user', 'email', false, false)."
- AND email = '".md5($user->username)."'
- )
- )")) {
- return $rec; // Matching user, deleted in DB found, return it
- }
- // 1C - Handle users deleted in backup file and "alive" in DB
- // If match by id and mnethost and user is deleted in backup file
- // and match by email = email_without_time(backup_email) => ok, return target user
- if ($user->deleted) {
- // Note: for DB deleted users email is stored in username field, hence we
- // are looking there for emails. See delete_user()
- // Trim time() from email
- $trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
- if ($rec = get_record_sql("SELECT *
- FROM {$CFG->prefix}user u
- WHERE id = $user->id
- AND mnethostid = $user->mnethostid
- AND email = '".addslashes($trimemail)."'")) {
- return $rec; // Matching user, deleted in backup file found, return it
- }
- }
- // 1D - If match by username and mnethost and doesn't match by id => conflict, return false
- if ($rec = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
- if ($user->id != $rec->id) {
- return false; // Conflict, username already exists and belongs to another id
- }
- }
- // Handle checks from different site backups
- } else {
- // 2A - If match by username and mnethost and
- // (email or non-zero firstaccess) => ok, return target user
- if ($rec = get_record_sql("SELECT *
- FROM {$CFG->prefix}user u
- WHERE username = '".addslashes($user->username)."'
- AND mnethostid = $user->mnethostid
- AND (
- email = '".addslashes($user->email)."'
- OR (
- firstaccess != 0
- AND firstaccess = $user->firstaccess
- )
- )")) {
- return $rec; // Matching user found, return it
- }
- // 2B - Handle users deleted in DB and "alive" in backup file
- // Note: for DB deleted users email is stored in username field, hence we
- // are looking there for emails. See delete_user()
- // Note: for DB deleted users md5(username) is stored *sometimes* in the email field,
- // hence we are looking there for usernames if not empty. See delete_user()
- // 2B1 - If match by mnethost and user is deleted in DB and not empty email = md5(username) and
- // (by username LIKE 'backup_email.%' or non-zero firstaccess) => ok, return target user
- if ($rec = get_record_sql("SELECT *
- FROM {$CFG->prefix}user u
- WHERE mnethostid = $user->mnethostid
- AND deleted = 1
- AND ".sql_isnotempty('user', 'email', false, false)."
- AND email = '".md5($user->username)."'
- AND (
- username LIKE '".addslashes($user->email).".%'
- OR (
- firstaccess != 0
- AND firstaccess = $user->firstaccess
- )
- )")) {
- return $rec; // Matching user found, return it
- }
- // 2B2 - If match by mnethost and user is deleted in DB and
- // username LIKE 'backup_email.%' and non-zero firstaccess) => ok, return target user
- // (this covers situations where md5(username) wasn't being stored so we require both
- // the email & non-zero firstaccess to match)
- if ($rec = get_record_sql("SELECT *
- FROM {$CFG->prefix}user u
- WHERE mnethostid = $user->mnethostid
- AND deleted = 1
- AND username LIKE '".addslashes($user->email).".%'
- AND firstaccess != 0
- AND firstaccess = $user->firstaccess")) {
- return $rec; // Matching user found, return it
- }
- // 2C - Handle users deleted in backup file and "alive" in DB
- // If match mnethost and user is deleted in backup file
- // and match by email = email_without_time(backup_email) and non-zero firstaccess=> ok, return target user
- if ($user->deleted) {
- // Note: for DB deleted users email is stored in username field, hence we
- // are looking there for emails. See delete_user()
- // Trim time() from email
- $trimemail = preg_replace('/(.*?)\.[0-9]+.?$/', '\\1', $user->username);
- if ($rec = get_record_sql("SELECT *
- FROM {$CFG->prefix}user u
- WHERE mnethostid = $user->mnethostid
- AND email = '".addslashes($trimemail)."'
- AND firstaccess != 0
- AND firstaccess = $user->firstaccess")) {
- return $rec; // Matching user, deleted in backup file found, return it
- }
- }
- // 2D - If match by username and mnethost and not by (email or non-zero firstaccess) => conflict, return false
- if ($rec = get_record_sql("SELECT *
- FROM {$CFG->prefix}user u
- WHERE username = '".addslashes($user->username)."'
- AND mnethostid = $user->mnethostid
- AND NOT (
- email = '".addslashes($user->email)."'
- OR (
- firstaccess != 0
- AND firstaccess = $user->firstaccess
- )
- )")) {
- return false; // Conflict, username/mnethostid already exist and belong to another user (by email/firstaccess)
- }
- }
- // Arrived here, return true as the user will need to be created and no
- // conflicts have been found in the logic above. This covers:
- // 1E - else => user needs to be created, return true
- // 2E - else => user needs to be created, return true
- return true;
- }
- /**
- * For all the users being restored, check if they are going to cause problems
- * before executing the restore process itself, detecting situations like:
- * - conflicts preventing restore to continue - provided by @restore_check_user()
- * - prevent creation of users if not allowed - check some global settings/caps
- */
- function restore_precheck_users($xml_file, $restore, &$problems) {
- global $CFG;
- $status = true; // Init $status
- // We aren't restoring users, nothing to check, allow continue
- if ($restore->users == 2) {
- return true;
- }
- // Get array of users from xml file and load them in backup_ids table
- if (!$info = restore_read_xml_users($restore,$xml_file)) {
- return true; // No users, nothing to check, allow continue
- }
- // We are going to map mnethostid, so load all the available ones
- $mnethosts = get_records('mnet_host', '', '', 'wwwroot', 'wwwroot, id');
- // Calculate the context we are going to use for capability checking
- if (!empty($restore->course_id)) { // Know the target (existing) course, check capabilities there
- $context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
- } else if (!empty($restore->restore_restorecatto)) { // Know the category, check capabilities there
- $context = get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto);
- } else { // Last resort, check capabilities at system level
- $context = get_context_instance(CONTEXT_SYSTEM);
- }
- // Calculate if we have perms to create users, by checking:
- // to 'moodle/restore:createuser' and 'moodle/restore:userinfo'
- // and also observe $CFG->disableusercreationonrestore
- $cancreateuser = false;
- if (has_capability('moodle/restore:createuser', $context) and
- has_capability('moodle/restore:userinfo', $context) and
- empty($CFG->disableusercreationonrestore)) { // Can create users
- $cancreateuser = true;
- }
- // Iterate over all users, checking if they are likely to cause problems on restore
- $counter = 0;
- foreach ($info->users as $userid) {
- $rec = backup_getid($restore->backup_unique_code, 'user', $userid);
- $user = $rec->info;
- // Find the correct mnethostid for user before performing any further check
- if (empty($user->mnethosturl) || $user->mnethosturl === $CFG->wwwroot) {
- $user->mnethostid = $CFG->mnet_localhost_id;
- } else {
- // fast url-to-id lookups
- if (isset($mnethosts[$user->mnethosturl])) {
- $user->mnethostid = $mnethosts[$user->mnethosturl]->id;
- } else {
- $user->mnethostid = $CFG->mnet_localhost_id;
- }
- }
- // Calculate the best way to handle this user from backup file
- $usercheck = restore_check_user($restore, $user);
- if (is_object($usercheck)) { // No problem, we have found one user in DB to be mapped to
- // Annotate it, for later process by restore_create_users(). Set new_id to mapping user->id
- backup_putid($restore->backup_unique_code, 'user', $userid, $usercheck->id, $user);
- } else if ($usercheck === false) { // Found conflict, report it as problem
- $problems[] = get_string('restoreuserconflict', '', $user->username);
- $status = false;
- } else if ($usercheck === true) { // User needs to be created, check if we are able
- if ($cancreateuser) { // Can create user, annotate it, for later process by restore_create_users(). Set new_id to 0
- backup_putid($restore->backup_unique_code, 'user', $userid, 0, $user);
- } else { // Cannot create user, report it as problem
- $problems[] = get_string('restorecannotcreateuser', '', $user->username);
- $status = false;
- }
- } else { // Shouldn't arrive here ever, something is for sure wrong in restore_check_user()
- if (!defined('RESTORE_SILENTLY')) {
- notify('Unexpected error pre-checking user ' . s($user->username) . ' from backup file');
- return false;
- }
- }
- // Do some output
- $counter++;
- if ($counter % 10 == 0) {
- if (!defined('RESTORE_SILENTLY')) {
- echo ".";
- if ($counter % 200 == 0) {
- echo "<br />";
- }
- }
- backup_flush(300);
- }
- }
- return $status;
- }
- //This function create a new course record.
- //When finished, course_header contains the id of the new course
- function restore_create_new_course($restore,&$course_header) {
- global $CFG, $SESSION;
- $status = true;
- $fullname = $course_header->course_fullname;
- $shortname = $course_header->course_shortname;
- $currentfullname = "";
- $currentshortname = "";
- $counter = 0;
- //Iteratere while the name exists
- do {
- if ($counter) {
- $suffixfull = " ".get_string("copyasnoun")." ".$counter;
- $suffixshort = "_".$counter;
- } else {
- $suffixfull = "";
- $suffixshort = "";
- }
- $currentfullname = $fullname.$suffixfull;
- // Limit the size of shortname - database column accepts <= 100 chars
- $currentshortname = substr($shortname, 0, 100 - strlen($suffixshort)).$suffixshort;
- $coursefull = get_record("course","fullname",addslashes($currentfullname));
- $courseshort = get_record("course","shortname",addslashes($currentshortname));
- $counter++;
- } while ($coursefull || $courseshort);
- //New name = currentname
- $course_header->course_fullname = $currentfullname;
- $course_header->course_shortname = $currentshortname;
- // first try to get it from restore
- if ($restore->restore_restorecatto) {
- $category = get_record('course_categories', 'id', $restore->restore_restorecatto);
- }
- // else we try to get it from the xml file
- //Now calculate the category
- if (empty($category)) {
- $category = get_record("course_categories","id",$course_header->category->id,
- "name",addslashes($course_header->category->name));
- }
- //If no exists, try by name only
- if (!$category) {
- $category = get_record("course_categories","name",addslashes($course_header->category->name));
- }
- //If no exists, get category id 1
- if (!$category) {
- $category = get_record("course_categories","id","1");
- }
- //If category 1 doesn'exists, lets create the course category (get it from backup file)
- if (!$category) {
- $ins_category = new object();
- $ins_category->name = addslashes($course_header->category->name);
- $ins_category->parent = 0;
- $ins_category->sortorder = 0;
- $ins_category->coursecount = 0;
- $ins_category->visible = 0; //To avoid interferences with the rest of the site
- $ins_category->timemodified = time();
- $newid = insert_record("course_categories",$ins_category);
- $category->id = $newid;
- $category->name = $course_header->category->name;
- }
- //If exists, put new category id
- if ($category) {
- $course_header->category->id = $category->id;
- $course_header->category->name = $category->name;
- //Error, cannot locate category
- } else {
- $course_header->category->id = 0;
- $course_header->category->name = get_string("unknowncategory");
- $status = false;
- }
- //Create the course_object
- if ($status) {
- $course = new object();
- $course->category = addslashes($course_header->category->id);
- $course->password = addslashes($course_header->course_password);
- $course->fullname = addslashes($course_header->course_fullname);
- $course->shortname = addslashes($course_header->course_shortname);
- $course->idnumber = addslashes($course_header->course_idnumber);
- $course->idnumber = ''; //addslashes($course_header->course_idnumber); // we don't want this at all.
- $course->summary = addslashes($course_header->course_summary);
- $course->format = addslashes($course_header->course_format);
- $course->showgrades = addslashes($course_header->course_showgrades);
- $course->newsitems = addslashes($course_header->course_newsitems);
- $course->teacher = addslashes($course_hea…
Large files files are truncated, but you can click here to view the full file