Example #1
0
function backup_user_info($bf, $preferences)
{
    global $CFG;
    $status = true;
    // Use a recordset to for the memory handling on to
    // the DB and run faster
    $users = get_recordset_sql("SELECT b.old_id, b.table_name, b.info,\n                                           u.*, m.wwwroot\n                                    FROM   {$CFG->prefix}backup_ids b\n                                      JOIN {$CFG->prefix}user       u ON b.old_id=u.id\n                                      JOIN {$CFG->prefix}mnet_host  m ON u.mnethostid=m.id\n                                    WHERE b.backup_code = '{$preferences->backup_unique_code}' AND\n                                          b.table_name = 'user'");
    //If we have users to backup
    if ($users && $users->RecordCount()) {
        //Begin Users tag
        fwrite($bf, start_tag("USERS", 2, true));
        $counter = 0;
        //With every user
        while ($user = $users->FetchNextObj()) {
            //Begin User tag
            fwrite($bf, start_tag("USER", 3, true));
            //Output all user data
            fwrite($bf, full_tag("ID", 4, false, $user->id));
            fwrite($bf, full_tag("AUTH", 4, false, $user->auth));
            fwrite($bf, full_tag("CONFIRMED", 4, false, $user->confirmed));
            fwrite($bf, full_tag("POLICYAGREED", 4, false, $user->policyagreed));
            fwrite($bf, full_tag("DELETED", 4, false, $user->deleted));
            fwrite($bf, full_tag("USERNAME", 4, false, $user->username));
            fwrite($bf, full_tag("PASSWORD", 4, false, $user->password));
            fwrite($bf, full_tag("IDNUMBER", 4, false, $user->idnumber));
            fwrite($bf, full_tag("FIRSTNAME", 4, false, $user->firstname));
            fwrite($bf, full_tag("LASTNAME", 4, false, $user->lastname));
            fwrite($bf, full_tag("EMAIL", 4, false, $user->email));
            fwrite($bf, full_tag("EMAILSTOP", 4, false, $user->emailstop));
            fwrite($bf, full_tag("ICQ", 4, false, $user->icq));
            fwrite($bf, full_tag("SKYPE", 4, false, $user->skype));
            fwrite($bf, full_tag("YAHOO", 4, false, $user->yahoo));
            fwrite($bf, full_tag("AIM", 4, false, $user->aim));
            fwrite($bf, full_tag("MSN", 4, false, $user->msn));
            fwrite($bf, full_tag("PHONE1", 4, false, $user->phone1));
            fwrite($bf, full_tag("PHONE2", 4, false, $user->phone2));
            fwrite($bf, full_tag("INSTITUTION", 4, false, $user->institution));
            fwrite($bf, full_tag("DEPARTMENT", 4, false, $user->department));
            fwrite($bf, full_tag("ADDRESS", 4, false, $user->address));
            fwrite($bf, full_tag("CITY", 4, false, $user->city));
            fwrite($bf, full_tag("COUNTRY", 4, false, $user->country));
            fwrite($bf, full_tag("LANG", 4, false, $user->lang));
            fwrite($bf, full_tag("THEME", 4, false, $user->theme));
            fwrite($bf, full_tag("TIMEZONE", 4, false, $user->timezone));
            fwrite($bf, full_tag("FIRSTACCESS", 4, false, $user->firstaccess));
            fwrite($bf, full_tag("LASTACCESS", 4, false, $user->lastaccess));
            fwrite($bf, full_tag("LASTLOGIN", 4, false, $user->lastlogin));
            fwrite($bf, full_tag("CURRENTLOGIN", 4, false, $user->currentlogin));
            fwrite($bf, full_tag("LASTIP", 4, false, $user->lastip));
            fwrite($bf, full_tag("SECRET", 4, false, $user->secret));
            fwrite($bf, full_tag("PICTURE", 4, false, $user->picture));
            fwrite($bf, full_tag("URL", 4, false, $user->url));
            fwrite($bf, full_tag("DESCRIPTION", 4, false, $user->description));
            fwrite($bf, full_tag("MAILFORMAT", 4, false, $user->mailformat));
            fwrite($bf, full_tag("MAILDIGEST", 4, false, $user->maildigest));
            fwrite($bf, full_tag("MAILDISPLAY", 4, false, $user->maildisplay));
            fwrite($bf, full_tag("HTMLEDITOR", 4, false, $user->htmleditor));
            fwrite($bf, full_tag("AJAX", 4, false, $user->ajax));
            fwrite($bf, full_tag("AUTOSUBSCRIBE", 4, false, $user->autosubscribe));
            fwrite($bf, full_tag("TRACKFORUMS", 4, false, $user->trackforums));
            if ($user->mnethostid != $CFG->mnet_localhost_id) {
                fwrite($bf, full_tag("MNETHOSTURL", 4, false, $user->wwwroot));
            }
            fwrite($bf, full_tag("TIMEMODIFIED", 4, false, $user->timemodified));
            /// write assign/override code for context_userid
            $user->isneeded = strpos($user->info, "needed");
            fwrite($bf, start_tag("ROLES", 4, true));
            if ($user->info != "needed" && $user->info != "") {
                //PRINT ROLE INFO
                $roles = explode(",", $user->info);
                foreach ($roles as $role) {
                    if ($role != "" && $role != "needed") {
                        fwrite($bf, start_tag("ROLE", 5, true));
                        //Print Role info
                        fwrite($bf, full_tag("TYPE", 6, false, $role));
                        //Print ROLE end
                        fwrite($bf, end_tag("ROLE", 5, true));
                    }
                }
            }
            //Needed
            if ($user->isneeded !== false) {
                //Print ROLE start
                fwrite($bf, start_tag("ROLE", 5, true));
                //Print Role info
                fwrite($bf, full_tag("TYPE", 6, false, "needed"));
                //Print ROLE end
                fwrite($bf, end_tag("ROLE", 5, true));
            }
            //End ROLES tag
            fwrite($bf, end_tag("ROLES", 4, true));
            //Check if we have user_preferences to backup
            if ($preferences_data = get_records("user_preferences", "userid", $user->old_id)) {
                //Start USER_PREFERENCES tag
                fwrite($bf, start_tag("USER_PREFERENCES", 4, true));
                //Write each user_preference
                foreach ($preferences_data as $user_preference) {
                    fwrite($bf, start_tag("USER_PREFERENCE", 5, true));
                    fwrite($bf, full_tag("NAME", 6, false, $user_preference->name));
                    fwrite($bf, full_tag("VALUE", 6, false, $user_preference->value));
                    fwrite($bf, end_tag("USER_PREFERENCE", 5, true));
                }
                //End USER_PREFERENCES tag
                fwrite($bf, end_tag("USER_PREFERENCES", 4, true));
            }
            $context = get_context_instance(CONTEXT_USER, $user->old_id);
            write_role_overrides_xml($bf, $context, 4);
            /// write role_assign code here
            write_role_assignments_xml($bf, $preferences, $context, 4);
            //End User tag
            fwrite($bf, end_tag("USER", 3, true));
            //Do some output
            $counter++;
            if ($counter % 10 == 0) {
                echo ".";
                if ($counter % 200 == 0) {
                    echo "<br />";
                }
                backup_flush(300);
            }
        }
        //End Users tag
        fwrite($bf, end_tag("USERS", 2, true));
    } else {
        // There aren't any users.
        $status = true;
    }
    return $status;
}
function backup_user_info($bf, $preferences)
{
    global $CFG;
    require_once $CFG->dirroot . '/tag/lib.php';
    $status = true;
    // Use a recordset to for the memory handling on to
    // the DB and run faster
    // Note the outer join with mnet_host: It shouldn't be neccesary
    // but there are some sites having mnet_host records missing
    // and that causes backup to fail (no users). Being a bit more
    // flexible here (outer joing) we bypass the problem and doesn't
    // cause more troubles. Eloy - MDL-16879
    $users = get_recordset_sql("SELECT b.old_id, b.table_name, b.info,\n                                           u.*, m.wwwroot\n                                    FROM   {$CFG->prefix}backup_ids b\n                                      JOIN {$CFG->prefix}user       u ON b.old_id=u.id\n                                      LEFT JOIN {$CFG->prefix}mnet_host  m ON u.mnethostid=m.id\n                                    WHERE b.backup_code = '{$preferences->backup_unique_code}' AND\n                                          b.table_name = 'user'");
    //If we have users to backup
    if ($users && !rs_EOF($users)) {
        //Begin Users tag
        fwrite($bf, start_tag("USERS", 2, true));
        $counter = 0;
        //With every user
        while ($user = rs_fetch_next_record($users)) {
            //Begin User tag
            fwrite($bf, start_tag("USER", 3, true));
            //Output all user data
            fwrite($bf, full_tag("ID", 4, false, $user->id));
            fwrite($bf, full_tag("AUTH", 4, false, $user->auth));
            fwrite($bf, full_tag("CONFIRMED", 4, false, $user->confirmed));
            fwrite($bf, full_tag("POLICYAGREED", 4, false, $user->policyagreed));
            fwrite($bf, full_tag("DELETED", 4, false, $user->deleted));
            fwrite($bf, full_tag("USERNAME", 4, false, $user->username));
            // Prevent user passwords in backup files unless
            // $CFG->includeuserpasswordsinbackup is defined. MDL-20838
            if (!empty($CFG->includeuserpasswordsinbackup)) {
                fwrite($bf, full_tag("PASSWORD", 4, false, $user->password));
            }
            fwrite($bf, full_tag("IDNUMBER", 4, false, $user->idnumber));
            fwrite($bf, full_tag("FIRSTNAME", 4, false, $user->firstname));
            fwrite($bf, full_tag("LASTNAME", 4, false, $user->lastname));
            fwrite($bf, full_tag("EMAIL", 4, false, $user->email));
            fwrite($bf, full_tag("EMAILSTOP", 4, false, $user->emailstop));
            fwrite($bf, full_tag("ICQ", 4, false, $user->icq));
            fwrite($bf, full_tag("SKYPE", 4, false, $user->skype));
            fwrite($bf, full_tag("YAHOO", 4, false, $user->yahoo));
            fwrite($bf, full_tag("AIM", 4, false, $user->aim));
            fwrite($bf, full_tag("MSN", 4, false, $user->msn));
            fwrite($bf, full_tag("PHONE1", 4, false, $user->phone1));
            fwrite($bf, full_tag("PHONE2", 4, false, $user->phone2));
            fwrite($bf, full_tag("INSTITUTION", 4, false, $user->institution));
            fwrite($bf, full_tag("DEPARTMENT", 4, false, $user->department));
            fwrite($bf, full_tag("ADDRESS", 4, false, $user->address));
            fwrite($bf, full_tag("CITY", 4, false, $user->city));
            fwrite($bf, full_tag("COUNTRY", 4, false, $user->country));
            fwrite($bf, full_tag("LANG", 4, false, $user->lang));
            fwrite($bf, full_tag("THEME", 4, false, $user->theme));
            fwrite($bf, full_tag("TIMEZONE", 4, false, $user->timezone));
            fwrite($bf, full_tag("FIRSTACCESS", 4, false, $user->firstaccess));
            fwrite($bf, full_tag("LASTACCESS", 4, false, $user->lastaccess));
            fwrite($bf, full_tag("LASTLOGIN", 4, false, $user->lastlogin));
            fwrite($bf, full_tag("CURRENTLOGIN", 4, false, $user->currentlogin));
            fwrite($bf, full_tag("LASTIP", 4, false, $user->lastip));
            fwrite($bf, full_tag("PICTURE", 4, false, $user->picture));
            fwrite($bf, full_tag("URL", 4, false, $user->url));
            fwrite($bf, full_tag("DESCRIPTION", 4, false, $user->description));
            fwrite($bf, full_tag("MAILFORMAT", 4, false, $user->mailformat));
            fwrite($bf, full_tag("MAILDIGEST", 4, false, $user->maildigest));
            fwrite($bf, full_tag("MAILDISPLAY", 4, false, $user->maildisplay));
            fwrite($bf, full_tag("HTMLEDITOR", 4, false, $user->htmleditor));
            fwrite($bf, full_tag("AJAX", 4, false, $user->ajax));
            fwrite($bf, full_tag("AUTOSUBSCRIBE", 4, false, $user->autosubscribe));
            fwrite($bf, full_tag("TRACKFORUMS", 4, false, $user->trackforums));
            if ($user->mnethostid != $CFG->mnet_localhost_id && !empty($user->wwwroot)) {
                fwrite($bf, full_tag("MNETHOSTURL", 4, false, $user->wwwroot));
            }
            fwrite($bf, full_tag("TIMEMODIFIED", 4, false, $user->timemodified));
            /// write assign/override code for context_userid
            $user->isneeded = strpos($user->info, "needed");
            fwrite($bf, start_tag("ROLES", 4, true));
            if ($user->info != "needed" && $user->info != "") {
                //PRINT ROLE INFO
                $roles = explode(",", $user->info);
                foreach ($roles as $role) {
                    if ($role != "" && $role != "needed") {
                        fwrite($bf, start_tag("ROLE", 5, true));
                        //Print Role info
                        fwrite($bf, full_tag("TYPE", 6, false, $role));
                        //Print ROLE end
                        fwrite($bf, end_tag("ROLE", 5, true));
                    }
                }
            }
            //Needed
            if ($user->isneeded !== false) {
                //Print ROLE start
                fwrite($bf, start_tag("ROLE", 5, true));
                //Print Role info
                fwrite($bf, full_tag("TYPE", 6, false, "needed"));
                //Print ROLE end
                fwrite($bf, end_tag("ROLE", 5, true));
            }
            //End ROLES tag
            fwrite($bf, end_tag("ROLES", 4, true));
            //Check if we have custom profile fields to backup
            if ($cpfields = get_records_sql("SELECT uif.shortname, uif.datatype, uidata.data\n                                                 FROM {$CFG->prefix}user_info_field uif,\n                                                      {$CFG->prefix}user_info_data uidata\n                                                 WHERE uif.id = uidata.fieldid\n                                                   AND uidata.userid = {$user->id}")) {
                //Start USER_CUSTOM_PROFILE_FIELDS tag
                fwrite($bf, start_tag("USER_CUSTOM_PROFILE_FIELDS", 4, true));
                //Write custom profile fields
                foreach ($cpfields as $cpfield) {
                    fwrite($bf, start_tag("USER_CUSTOM_PROFILE_FIELD", 5, true));
                    fwrite($bf, full_tag("FIELD_NAME", 6, false, $cpfield->shortname));
                    fwrite($bf, full_tag("FIELD_TYPE", 6, false, $cpfield->datatype));
                    fwrite($bf, full_tag("FIELD_DATA", 6, false, $cpfield->data));
                    fwrite($bf, end_tag("USER_CUSTOM_PROFILE_FIELD", 5, true));
                }
                //End USER_CUSTOM_PROFILE_FIELDS tag
                fwrite($bf, end_tag("USER_CUSTOM_PROFILE_FIELDS", 4, true));
            }
            //Check if we have user tags to backup
            if (!empty($CFG->usetags)) {
                if ($tags = tag_get_tags('user', $user->id)) {
                    //This return them ordered by default
                    //Start USER_TAGS tag
                    fwrite($bf, start_tag("USER_TAGS", 4, true));
                    //Write user tags fields
                    foreach ($tags as $tag) {
                        fwrite($bf, start_tag("USER_TAG", 5, true));
                        fwrite($bf, full_tag("NAME", 6, false, $tag->name));
                        fwrite($bf, full_tag("RAWNAME", 6, false, $tag->rawname));
                        fwrite($bf, end_tag("USER_TAG", 5, true));
                    }
                    //End USER_TAGS tag
                    fwrite($bf, end_tag("USER_TAGS", 4, true));
                }
            }
            //Check if we have user_preferences to backup
            if ($preferences_data = get_records("user_preferences", "userid", $user->old_id)) {
                //Start USER_PREFERENCES tag
                fwrite($bf, start_tag("USER_PREFERENCES", 4, true));
                //Write each user_preference
                foreach ($preferences_data as $user_preference) {
                    fwrite($bf, start_tag("USER_PREFERENCE", 5, true));
                    fwrite($bf, full_tag("NAME", 6, false, $user_preference->name));
                    fwrite($bf, full_tag("VALUE", 6, false, $user_preference->value));
                    fwrite($bf, end_tag("USER_PREFERENCE", 5, true));
                }
                //End USER_PREFERENCES tag
                fwrite($bf, end_tag("USER_PREFERENCES", 4, true));
            }
            $context = get_context_instance(CONTEXT_USER, $user->old_id);
            write_role_overrides_xml($bf, $context, 4);
            /// write role_assign code here
            write_role_assignments_xml($bf, $preferences, $context, 4);
            //End User tag
            fwrite($bf, end_tag("USER", 3, true));
            //Do some output
            $counter++;
            if ($counter % 10 == 0) {
                echo ".";
                if ($counter % 200 == 0) {
                    echo "<br />";
                }
                backup_flush(300);
            }
        }
        //End Users tag
        fwrite($bf, end_tag("USERS", 2, true));
    } else {
        // There aren't any users.
        $status = true;
    }
    if ($users) {
        rs_close($users);
    }
    return $status;
}
Example #3
0
/**
 * Writes out all of the data that should appear for every context.
 */
function write_per_context_data($bf, $preferences, $context, $startlevel)
{
    write_role_overrides_xml($bf, $context, $startlevel);
    write_role_assignments_xml($bf, $preferences, $context, $startlevel);
    write_local_filter_settings($bf, $preferences, $context, $startlevel);
}