forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_check.html
440 lines (386 loc) · 19.6 KB
/
restore_check.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
//This page receive all the restore_form data. Then, if existing course
//has been selected, shows a list of courses to select one.
//It cheks that the parammeter from restore_form are coherent.
//It puts all the restore info in the session.
//Finally, it calls restore_execute to do the hard work
//Get objects from session
global $DB, $OUTPUT;
if ($SESSION) {
$info = $SESSION->info;
$course_header = $SESSION->course_header;
if (isset($SESSION->restore)) {
$restore = $SESSION->restore;
}
}
//Detect if we are coming from the restore form
$fromform = optional_param ('fromform', 0, PARAM_INT);
if ($form1 = data_submitted()) {
$currentcourseshortname = $course_header->course_shortname; //"store_ShortName";
$course_header->course_shortname = !empty($form1->shortname) ? $form1->shortname : ''; //"update_ShortName";
$course_header->course_fullname = !empty($form1->fullname) ? $form1->fullname : ''; //"update_FullName";
/// Roll dates only if the backup course has a start date
/// (some formats like main page, social..., haven't it and rolling dates
/// from 0 produces crazy dates. MDL-10125 and we have passed some custom startyear/month/day. MDL-12922
if ($course_header->course_startdate && !empty($form1->startyear)) {
$form1->startdate = make_timestamp($form1->startyear, $form1->startmonth, $form1->startday);
$currentcoursestartdate = $course_header->course_startdate;
$coursestartdatedateoffset = $form1->startdate - $currentcoursestartdate;
$restore->course_startdateoffset = $coursestartdatedateoffset; //change to restore
// Only apply rolling of dates if differences are bigger than one day
// that should solve current problems with daylight changes between
// backup and restore
if (abs($restore->course_startdateoffset) < 24 * 60 * 60) {
$coursestartdatedateoffset = 0;
$restore->course_startdateoffset = 0;
}
} else { // don't roll if the course hasn't start date
$coursestartdatedateoffset = 0;
$restore->course_startdateoffset = 0;
}
}
///Enforce SESSION->course_header rewrite (PHP 4.x needed because assigns are by value) MDL-8298
$SESSION->course_header = $course_header;
//If restore session info exists, but we are coming from the form
//it has prioriry
if (isset($restore) and !empty($fromform)) {
unset($restore);
}
// check for session objects
if (empty($info) or empty($course_header)) {
print_error('sessionmissing', 'debug', '',
'info and course_header');
}
//If the restore object doesn't exist, we are going
//to check every variable individually and create it
if (!isset($restore)) {
//Check that we have all we need
//backup_unique_code
$backup_unique_code = required_param('backup_unique_code', PARAM_INT);
//file
$file = required_param('file', PARAM_PATH);
//Checks for the required restoremod parameters
if ($allmods = $DB->get_records("modules")) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$var = "restore_".$modname;
$$var = optional_param( $var,0, PARAM_CLEAN);
$var = "restore_user_info_".$modname;
$$var = optional_param( $var,0, PARAM_CLEAN);
$instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
if ($instances === NULL) {
continue;
}
foreach ($instances as $instance) {
$var = 'restore_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0,PARAM_INT);
$var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
$$var = optional_param($var,0,PARAM_INT);
}
}
}
//restoreto
$restore_restoreto = required_param('restore_restoreto', PARAM_INT);
//restore_users
$restore_users = required_param('restore_users', PARAM_INT);
$restore_groups = required_param('restore_groups', PARAM_INT);
//restore_logs
$restore_logs = required_param('restore_logs', PARAM_INT);
//restore_user_files
$restore_user_files = required_param('restore_user_files', PARAM_INT);
//restore_course_files
$restore_course_files = required_param('restore_course_files', PARAM_INT);
//restore_site_files
$restore_site_files = required_param('restore_site_files', PARAM_INT);
//restore_gradebook_history
$restore_gradebook_history = required_param('restore_gradebook_history', PARAM_INT);
//restore_messages
$restore_messages = required_param('restore_messages', PARAM_INT);
//restore_blogs
$restore_blogs = required_param('restore_blogs', PARAM_INT);
//Check we've selected a course to restore to
$course_id = optional_param('course_id', 0, PARAM_INT);
//We are here, having all we need !!
//Create the restore object and put it in the session
$restore->backup_unique_code = $backup_unique_code;
$restore->file = $file;
if ($allmods = $DB->get_records("modules")) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$var = "restore_".$modname;
$restore->mods[$modname]->restore=$$var;
$var = "restore_user_info_".$modname;
$restore->mods[$modname]->userinfo=$$var;
$instances = !empty($info->mods[$mod->name]->instances) ? $info->mods[$mod->name]->instances : NULL;
if ($instances === NULL) {
continue;
}
foreach ($instances as $instance) {
$var = 'restore_'.$modname.'_instance_'.$instance->id;
$restore->mods[$modname]->instances[$instance->id]->restore = $$var;
$var = 'restore_user_info_'.$modname.'_instance_'.$instance->id;
$restore->mods[$modname]->instances[$instance->id]->userinfo = $$var;
}
}
}
$restore->restoreto=$restore_restoreto;
$restore->users=$restore_users;
$restore->groups=$restore_groups;
$restore->logs=$restore_logs;
$restore->user_files=$restore_user_files;
$restore->course_files=$restore_course_files;
$restore->site_files=$restore_site_files;
$restore->messages=$restore_messages;
$restore->blogs=$restore_blogs;
$restore->restore_gradebook_history=$restore_gradebook_history;
$restore->course_id=$course_id;
//add new vars to restore object
$restore->course_startdateoffset = $coursestartdatedateoffset;
$restore->course_shortname = $currentcourseshortname;
// create role mappings, not sure all should be here
if ($data2 = data_submitted()) {
foreach ($data2 as $tempname=>$tempdata) {
if (strstr($tempname, 'roles_')) {
$temprole = explode('_', $tempname);
$oldroleid = $temprole[1];
$newroleid = $tempdata;
$restore->rolesmapping[$oldroleid] = $newroleid;
}
}
}
// default role mapping for moodle < 1.7
if ($defaultteacheredit = optional_param('defaultteacheredit', 0, PARAM_INT)) {
$restore->rolesmapping['defaultteacheredit'] = $defaultteacheredit;
}
if ($defaultteacher = optional_param('defaultteacher', 0, PARAM_INT)) {
$restore->rolesmapping['defaultteacher'] = $defaultteacher;
}
if ($defaultstudent = optional_param('defaultstudent', 0, PARAM_INT)) {
$restore->rolesmapping['defaultstudent'] = $defaultstudent;
}
} else {
//We have the object, so check if we have a new course_id
//passed as parammeter
$course_id = optional_param('course_id', 0, PARAM_INT);
if ($course_id) {
$restore->course_id=$course_id;
}
}
// pass in the course category param
$restore->restore_restorecatto = optional_param('restore_restorecatto', 0, PARAM_INT);
//We have the object with data, put it in the session
$SESSION->restore = $restore;
//From here to the end of the script, only use the $restore object
//Check login
require_login();
$loginurl = get_login_url();
//Init restoreuserinfo
$restoreuserinfo = false;
//Check admin
if (!empty($id)) {
if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $id))) {
print_error("cannotuseadminadminorteacher", '', $loginurl);
}
$restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_COURSE, $id));
} else {
if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_SYSTEM))) {
print_error("cannotuseadmin", '', $loginurl);
}
$restoreuserinfo = has_capability('moodle/restore:userinfo', get_context_instance(CONTEXT_SYSTEM));
}
//Check site
$site = get_site();
// Get all the courses the user is able to restore to
//TODO: use better function which includes all courses for admins
$mycourses = get_user_courses_bycap($USER->id, 'moodle/restore:restorecourse');
// Calculate if the user can create courses
$cancreatecourses = user_can_create_courses();
if (empty($restore->course_id) && ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_CURRENT_ADDING)) {
$restore->course_id = $id; /// Force restore to current course, disabling pick course from list
}
//Set restore->deleting as needed
if ($restore->restoreto == RESTORETO_CURRENT_DELETING || $restore->restoreto == RESTORETO_EXISTING_DELETING) {
$restore->deleting = true;
} else {
$restore->deleting = false;
}
//Now, select the course if needed
if (empty($restore->course_id) && ($restore->restoreto == RESTORETO_EXISTING_DELETING || $restore->restoreto == RESTORETO_EXISTING_ADDING)) {
if ($courses = $mycourses) {
echo $OUTPUT->heading(get_string("choosecourse"));
echo $OUTPUT->box_start();
foreach ($courses as $course) {
if (!has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $course->id))) {
continue;
}
if (empty($course->visible)) {
$optdimmed = ' class="dimmed" ';
} else {
$optdimmed = '';
}
echo "<a $optdimmed href=\"restore.php?course_id=$course->id&launch=check&id=$id&file=$file\">".format_string($course->fullname).' ('.format_string($course->shortname).')</a><br />'."\n";
}
echo $OUTPUT->box_end();
} else {
echo $OUTPUT->heading(get_string("nocoursesyet"));
echo $OUTPUT->continue_button("$CFG->wwwroot/$CFG->admin/index.php");
}
//Checks everything and execute restore
} else if (($restore->restoreto != RESTORETO_NEW_COURSE and !empty($restore->course_id)) or ($restore->restoreto == RESTORETO_NEW_COURSE)) {
//Final access control check
if (empty($restore->course_id) and !$cancreatecourses) {
print_error("cannotrestoreadminorcreator");
} else if (!empty($restore->course_id) and !has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_COURSE, $restore->course_id))) {
print_error("cannotrestoreadminoredit");
}
$show_continue_button = true;
$messages = array();
//Check and warn if we are restoring over frontpage (SITEID) course. MDL-19163
if ($restore->course_id == SITEID) {
if ($restore->restoreto == RESTORETO_CURRENT_DELETING) {
$messages[] = get_string ('restoretositedeleting');
} else if ($restore->restoreto == RESTORETO_CURRENT_ADDING) {
$messages[] = get_string ('restoretositeadding');
}
}
// If we have selected to roll dates on restore and the user is missing
// such capability (moodle/restore:rolldates) in the target course/category,
// disable roll of dates and warn
if ($restore->course_startdateoffset != 0) {
$canrolldates = false; // init to false
// if we know the target course, check the capability on it
if (!empty($restore->course_id)) {
$canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSE, $restore->course_id));
// if we know the target category, check capability on it
} else if (!empty($restore->restore_restorecatto)) {
$canrolldates = has_capability('moodle/restore:rolldates', get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto));
}
if (!$canrolldates) {
$messages[] = get_string ("noteusercannotrolldatesoncontext");
$restore->course_startdateoffset = 0;
}
}
//Check if we've selected any mod's user info and restore->users
//is set to none. Change it to course and inform.
if ($restore->users == 2) {
$changed = false;
$mods = $restore->mods;
foreach ($mods as $mod) {
if ($mod->userinfo) {
$changed = true;
}
}
//If we have selected user files or messages or blogs, then users must be restored too
if ($restore->user_files || $restore->messages || $restore->blogs) {
$changed = 1;
}
if ($changed) {
$messages[] = get_string ("noteuserschangednonetocourse");
$restore->users = 1;
}
}
// Re-enforce 'moodle/restore:restorecourse' at system context to be able to restore all users
if ($restore->users == 0 and !has_capability('moodle/restore:restorecourse', get_context_instance(CONTEXT_SYSTEM))) {
$restore->users = 1; // users => course
}
// Re-enforce moodle/restore:userinfo capability
if (!$restoreuserinfo) {
$userinfocheck = true;
// Confirm that all the settings are properly set to no users
// if anything is wrong, message and stop
// First global settings
if ($restore->users != 2 or $restore->user_files or $restore->messages or $restore->blogs) {
$userinfocheck = false;
// Now all modules userinfo flag
} else {
$mods = $restore->mods;
foreach ($mods as $mod) {
if ($mod->userinfo) {
$userinfocheck = false;
}
}
}
if (!$userinfocheck) { // Something was wrong
$messages[] = get_string('restoreuserinfofailed');
$show_continue_button = false;
}
}
// Re-check all the roles creation/mapping stuff
if (!empty($restore->course_id)) {
$contexttosearch = get_context_instance(CONTEXT_COURSE, $restore->course_id);
// if we know the target category, check capability on it
} else if (!empty($restore->restore_restorecatto)) {
$contexttosearch = get_context_instance(CONTEXT_COURSECAT, $restore->restore_restorecatto);
} else {
$contexttosearch = get_context_instance(CONTEXT_SYSTEM);
}
$cancreateroles = has_capability('moodle/role:manage', get_context_instance(CONTEXT_SYSTEM));
$canassignroles = has_capability('moodle/role:assign', $contexttosearch);
$canoverridecaps= has_capability('moodle/role:override', $contexttosearch) || has_capability('moodle/role:safeoverride', $contexttosearch);
if (!empty($restore->rolesmapping)) {
foreach ($restore->rolesmapping as $originalrole => $targetrole) {
// First, if new role is selected, check used really can do that
if (empty($targetrole) && !$cancreateroles) {
$messages[] = get_string('restorecannotcreateorassignroles');
$show_continue_button = false;
break;
}
// Now, check that the target role is available for role assignments
if (!empty($targetrole) && !$canassignroles) {
$messages[] = get_string('restorecannotassignroles');
$show_continue_button = false;
break;
}
// Now, check that the target role is available for capability overrides
if (!empty($targetrole) && !$canoverridecaps) {
$messages[] = get_string('restorecannotoverrideperms');
$show_continue_button = false;
break;
}
}
}
/// If restoring users and backup has mnet remote users and we are restoring to different site, forbid restore to non-admins. MDL-17009
if ($restore->users != 2 && /// If restoring users
!empty($info->mnet_remoteusers) && $info->mnet_remoteusers === 'true' && /// and backup contains remote users
!backup_is_same_site($info)) { /// and backup is being restored to different site
/// If user is admin (by 'moodle/user:create' cap), warn about conversion to local auth if missing mnet hosts and continue restore
if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM))) {
$messages[] = get_string('mnetrestore_extusers_admin', 'admin');
$show_continue_button = true;
/// else, notify about the thing (suggesting to ask admin) and stop restore
} else {
$messages[] = get_string('mnetrestore_extusers_noadmin', 'admin');
$show_continue_button = false;
}
}
//Save the restore session object
$SESSION->restore = $restore;
// Calculate all session objects checksum and store them in session too
// so restore_execute.html (used by manual restore and import) will be
// able to detect any problem in session info.
restore_save_session_object_checksums($restore, $info, $course_header);
echo "<div style='text-align:center'>";
/// Printout messages
foreach ($messages as $message) {
echo '<p>' . $message . '</p>';
}
/// Warning and button
if ($show_continue_button) {
//Print the continue button to execute the restore NOW !!!!
//All is prepared !!!
$hidden["launch"] = "execute";
$hidden["file"] = $file;
$hidden["id"] = $id;
$hidden["sesskey"] = sesskey();
print_string('longtimewarning','admin');
/// Restore button
echo $OUTPUT->single_button(new moodle_url("restore.php", $hidden), get_string("restorecoursenow"));
}
echo "</div>";
//If we are here. Something must be wrong. Debug !!!
} else {
print_object($restore);
print_object($info);
print_object($course_header);
print_error('error');
}