-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
663 lines (560 loc) · 22.1 KB
/
lib.php
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package block_simple_restore
* @copyright 2008 onwards Louisiana State University
* @copyright 2008 onwards Chad Mazilly, Robert Russo, Jason Peak, Dave Elliott, Adam Zapletal, Philip Cali
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// Be sure no one accesses the page directly.
defined('MOODLE_INTERNAL') || die();
abstract class simple_restore_utils {
// We don't need the includes on every request.
public static function includes() {
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
}
public static function backadel_shortname($shortname) {
if (preg_match('/\s/', $shortname)) {
$matchers = array('/\s/', '/\//');
return preg_replace($matchers, '-', $shortname);
}
return $shortname;
}
public static function selected_backadel($data) {
global $CFG;
$backadelpath = get_config('block_backadel', 'path');
$realpath = $CFG->dataroot . $backadelpath . $data->fileid;
if (!file_exists($realpath)) {
return true;
}
copy($realpath, $data->to_path);
$data->filename = $data->fileid;
return true;
}
public static function backadel_backups($search) {
global $CFG;
$backadelpath = get_config('block_backadel', 'path');
if (empty($backadelpath)) {
return array();
}
$backadelpath = $CFG->dataroot . $backadelpath;
$bysearch = function ($file) use ($search) {
return preg_match("/{$search}/i", $file);
};
$tobackup = function ($file) use ($backadelpath) {
$backadel = new stdClass;
$backadel->id = $file;
$backadel->filename = $file;
$backadel->filesize = filesize($backadelpath . $file);
$backadel->timemodified = filemtime($backadelpath . $file);
return $backadel;
};
$potentials = array_filter(scandir($backadelpath), $bysearch);
return array_map($tobackup, $potentials);
}
public static function backadel_criterion($course) {
global $USER;
$crit = get_config('block_backadel', 'suffix');
if (empty($crit)) {
return "";
}
$search = $crit == 'username' ? '_' . $USER->username : $course->{$crit};
return "{$search}[_\.]";
}
public static function backup_list($data) {
global $DB, $OUTPUT;
if (isset($data->shortname)) {
$search = self::backadel_shortname($data->shortname);
} else {
$course = $DB->get_record('course', array('id' => $data->courseid));
$search = self::backadel_criterion($course);
}
$list = new stdClass;
$list->header = get_string('semester_backups', 'block_simple_restore');
$list->backups = self::backadel_backups($search);
$list->order = 10;
$list->html = '';
if (!empty($list->backups)) {
$list->html = $OUTPUT->heading($list->header);
$list->html .= self::build_table(
$list->backups,
'backadel',
$data->courseid,
$data->restore_to
);
}
$data->lists[] = $list;
return true;
}
public static function permission($cap, $context) {
return has_capability("block/simple_restore:{$cap}", $context);
}
public static function _s($name, $a=null) {
return get_string($name, 'block_simple_restore', $a);
}
public static function build_table($backups, $name, $courseid, $restoreto) {
$table = new html_table();
$table->head = array(
get_string('name'),
get_string('size'),
get_string('modified')
);
$torow = function($backup) use ($name, $courseid, $restoreto) {
$link = html_writer::link(
new moodle_url('/blocks/simple_restore/list.php', array(
'id' => $courseid,
'name' => $name,
'action' => 'choosefile',
'restore_to' => $restoreto,
'fileid' => $backup->id
)), $backup->filename);
$name = new html_table_cell($link);
$size = new html_table_cell(display_size($backup->filesize));
$modified = new html_table_cell(date('d M Y, h:i:s A',
$backup->timemodified));
return new html_table_row(array($name, $size, $modified));
};
$table->data = array_map($torow, $backups);
return html_writer::table($table);
}
public static function filter_courses($shortname) {
global $DB;
$safeshortname = addslashes($shortname);
$select = "shortname LIKE '%{$safeshortname}%'";
return $DB->get_records_select('course', $select);
}
public static function heading($restoreto) {
switch($restoreto){
case 0:
return self::_s('delete_restore');
case 1:
return self::_s('restore_course');
case 2:
return self::_s('restore_course_archive');
}
}
public static function prep_restore($fileid, $name, $courseid) {
global $USER, $CFG;
// Get the includes.
self::includes();
if (empty($fileid) || empty($courseid)) {
throw new Exception(self::_s('no_arguments'));
}
$filename = restore_controller::get_tempdir_name($courseid, $USER->id);
$pathname = $CFG->tempdir . '/backup/' . $filename;
$data = new stdClass;
$data->userid = $USER->id;
$data->courseid = $courseid;
$data->fileid = $fileid;
$data->to_path = $pathname;
$data->filename = $filename;
self::selected_backadel($data);
simple_restore_selected_user::selected_user($data);
if (empty($data->filename)) {
throw new Exception(self::_s('no_file'));
}
return $filename;
}
/**
* Simple Restore post restore fixes
* NOT DONE.
* REQUIRES CPS AND UES TO FUNCTION.
* DO NOT CALL!!!
*
* @param $data
* @param int other['userid']
* @param int other['restore_to'] 0,1,2
* @param int other['courseid']
*/
public static function simple_restore_complete($data) {
try {
global $DB, $CFG, $USER;
require_once($CFG->dirroot . '/blocks/cps/classes/lib.php');
$sectionid = $data->other['ues_section_id'];
$restoreto = $data->other['restore_to'];
$oldcourse = get_course($data->other['courseid']);
$skip = array(
'id', 'category', 'sortorder',
'sectioncache', 'modinfo', 'newsitems'
);
$course = $DB->get_record('course', array('id' => $oldcourse->id));
$resetgrades = cps_setting::get(array(
'name' => 'user_grade_restore',
'userid' => $USER->id
));
// Defaults to reset grade items.
if (empty($resetgrades)) {
$resetgrades = new stdClass;
$resetgrades->value = 1;
}
// Maintain the correct config.
foreach (get_object_vars($oldcourse) as $key => $value) {
if (in_array($key, $skip)) {
continue;
}
$course->$key = $value;
}
$DB->update_record('course', $course);
if ($resetgrades->value == 1) {
require_once($CFG->libdir . '/gradelib.php');
$items = grade_item::fetch_all(array('courseid' => $course->id));
foreach ($items as $item) {
$item->plusfactor = 0.00000;
$item->multfactor = 1.00000;
$item->update();
}
grade_regrade_final_grades($course->id);
}
// This is an import, ignore.
if ($restoreto == 1) {
return true;
}
$keepenrollments = (bool) get_config('simple_restore', 'keep_roles_and_enrolments');
$keepgroups = (bool) get_config('simple_restore', 'keep_groups_and_groupings');
// No need to re-enroll.
if ($keepgroups and $keepenrollments) {
$enrolinstances = $DB->get_records('enrol', array(
'courseid' => $oldcourse->id,
'enrol' => 'ues'
));
// Cleanup old instances.
$ues = enrol_get_plugin('ues');
foreach (array_slice($enrolinstances, 1) as $instance) {
$ues->delete_instance($instance);
}
} else {
$sections = ues_section::from_course($course);
// Nothing to do.
if (empty($sections)) {
return true;
}
// Rebuild enrollment.
ues::enrollUsers(ues_section::from_course($course));
}
return true;
} catch (Exception $e) {
return false;
}
}
}
class archive_restore_utils extends simple_restore_utils {
/**
* Get course name and category from filename.
*
* NB: this function expects files from backadel whose names
* begin as 'backadel-', for example:
* backup-moodle2-course-2-2014_spring_tst2_2011_for_instructor_four-20140407-1539.mbz
*
* @param string $filename
*/
public static function coursedata_from_filename($filename) {
$prefix = 'backadel';
if (substr($filename, 0, strlen($prefix)) == $prefix) {
$filename = substr($filename, strlen($prefix) + 1);
} else {
// TODO - do something better than throw an error if it isn't a backadel file.
// Consider restricting the choice of files in the first place!
throw new exception("Archive Restore does not support filenames other than 'backadel-*'");
}
$chunks = explode('_', $filename);
$meta = $chunks[0];
$metachunks = explode('-', $meta);
$fullname = implode(' ', $metachunks);
$category = $metachunks[2];
return array($fullname, $category);
}
}
class simple_restore {
public $userid;
public $course;
public $filename;
public $restoreto;
public function __construct($course, $filename, $restoreto = 0) {
if (empty($course)) {
throw new Exception(simple_restore_utils::_s('no_context'));
}
if (empty($filename)) {
throw new Exception(simple_restore_utils::_s('no_file'));
}
global $USER;
$this->userid = $USER->id;
$this->course = $course;
$this->context = context_course::instance($course->id);
$this->filename = $filename;
$this->restore_to = $restoreto;
}
private function process_confirm() {
$restore = restore_ui::engage_independent_stage(
restore_ui::STAGE_CONFIRM, $this->context->id
);
$restore->process();
return $restore;
}
private function process_destination($restore) {
$_POST['sesskey'] = sesskey();
$_POST['filepath'] = $this->rip_value($restore, 'filepath');
$_POST['target'] = $this->restore_to;
$_POST['targetid'] = $this->course->id;
$rtn = restore_ui::engage_independent_stage(
restore_ui::STAGE_DESTINATION, $this->context->id
);
$rtn->process();
return $rtn;
}
private function process_schema($rc) {
// File dependencies.
$filedependencies = array(
'block' => 1, 'comments' => 1, 'filters' => 1
);
$_POST['stage'] = restore_ui::STAGE_SCHEMA;
$restore = new restore_ui($rc, array('contextid' => $this->context->id));
// Forge posts.
$_POST['restore'] = $restore->get_restoreid();
// Get all tasks from the UI object through reflection.
$tasks = $this->rip_ui($restore)->get_tasks();
foreach ($tasks as $task) {
$settings = $task->get_settings();
foreach ($settings as $setting) {
$settingname = $setting->get_name();
if (preg_match('/(.+)_(\d+)_(.+)/', $settingname, $matches)) {
$module = $matches[1];
$type = $matches[3];
$adminsettingkey = $module.'_'.$type;
} else {
$adminsettingkey = $settingname;
}
$adminsetting = get_config('simple_restore', $adminsettingkey);
if (!is_numeric($adminsetting)) {
continue;
}
if ($adminsetting and isset($filedependencies[$settingname])) {
$basepath = $task->get_taskbasepath();
if (!file_exists("$basepath/$settingname.xml")) {
continue;
}
}
// Set admin value.
// Some settings may be locked by permission.
if ($setting->get_status() == base_setting::NOT_LOCKED) {
$setting->set_value($adminsetting);
}
}
}
$restore->process();
$restore->save_controller();
return $restore;
}
private function rip_value($restore, $property) {
$reflector = new ReflectionObject($restore);
$prop = $reflector->getProperty($property);
$prop->setAccessible(true);
return $prop->getValue($restore);
}
private function rip_stage($restore) {
return $this->rip_value($restore, 'stage');
}
private function rip_ui($restore) {
return $this->rip_value($this->rip_stage($restore), 'ui');
}
private function process_final($restore) {
$_POST['stage'] = restore_ui::STAGE_PROCESS;
$rc = restore_ui::load_controller($restore->get_restoreid());
$final = new restore_ui($rc, array('contextid' => $this->context->id));
$final->process();
$final->execute();
$final->destroy();
unset($final);
}
public function execute() {
simple_restore_utils::includes();
// Archive mode.
if ($this->restore_to == 2 && get_config('simple_restore', 'is_archive_server')) {
return $this->archive_mode_execute();
}
// Confirmed ... process destination.
$confirmed = $this->process_destination($this->process_confirm());
// Setting up controller ... tmp tables.
$rc = new restore_controller(
$confirmed->get_filepath(),
$confirmed->get_course_id(),
backup::INTERACTIVE_YES,
backup::MODE_GENERAL,
$this->userid,
$confirmed->get_target()
);
if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
$rc->convert();
}
// Probably good to do this.
unset($confirmed);
$this->process_final($this->process_schema($rc));
// Restore blocks.
if ($this->restore_to == 0) {
blocks_delete_all_for_context($this->context->id);
blocks_add_default_course_blocks($this->course);
}
// It's important to pass the previous course's config.
$coursesettings = array(
'restore_to' => $this->restore_to,
'course' => $this->course
);
return true;
}
/**
* Create a new course from the selected backup file.
*
* This method is inspired by @see core_course_external::duplicate_course.
* found in /course/externallib.php.
*
* @global type $CFG
* @global type $DB
* @global type $USER
* @return boolean
* @throws moodle_exception
*/
public function archive_mode_execute() {
global $CFG, $DB, $USER;
require_once($CFG->dirroot.'/enrol/manual/lib.php');
simple_restore_utils::includes();
// Enrol the current user as teacher.
$plugin = new enrol_manual_plugin();
$plugin->add_instance($this->course);
$instances = enrol_get_instances($this->course->id, true);
$isntance = null;
foreach ($instances as $enrolinstance) {
if ($enrolinstance->enrol == 'manual') {
$instance = $enrolinstance;
break;
}
}
$roleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
$plugin->enrol_user($instance, $USER->id, $roleid);
// Setup tempdir for the restore process.
$extractname = restore_controller::get_tempdir_name($this->course->id, $USER->id);
$extractpath = $CFG->tempdir . '/backup/' . $extractname;
$filepath = $CFG->tempdir.'/backup/'.$this->filename;
if (!has_capability('moodle/restore:userinfo', $this->context, $USER->id)) {
// Delete, abort, etc.
echo "deleting temporary course files and materials.";
fulldelete($filepath);
delete_course($this->course);
// Update course count in catagories.
fix_course_sortorder();
// In order to restore Archived courses,
// this role must be granted the capability moodle/restore:userinfo - Ask your administrator.
throw new restore_controller_exception("no userinfo cap");
}
// Zip file needs to be unzipped.
if (!file_exists($filepath. "/moodle_backup.xml")) {
$fb = get_file_packer('application/vnd.moodle.backup');
$fb->extract_to_pathname("$CFG->tempdir/backup/".$this->filename, $extractpath);
}
$rc = new restore_controller($extractname, $this->course->id,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, backup::TARGET_NEW_COURSE);
// Iterate through our settings and make sure they are reflected in the restore plan.
$configsettings = array_values($this->get_settings());
foreach ($configsettings as $config) {
if ($rc->get_plan()->setting_exists($config->name)) {
$setting = $rc->get_plan()->get_setting($config->name);
if ($setting->get_status() == backup_setting::NOT_LOCKED) {
$setting->set_value($config->value);
}
}
}
// Setup restore process and ensure there are no errors.
if (!$rc->execute_precheck()) {
$precheckresults = $rc->get_precheck_results();
if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
if (empty($CFG->keeptempdirectoriesonbackup)) {
fulldelete($filepath);
}
$errorinfo = '';
foreach ($precheckresults['errors'] as $error) {
$errorinfo .= $error;
}
if (array_key_exists('warnings', $precheckresults)) {
foreach ($precheckresults['warnings'] as $warning) {
$errorinfo .= $warning;
}
}
throw new moodle_exception('backupprecheckerrors', 'webservice', '', $errorinfo);
}
}
// Get the correct course name - prevents dupe names.
list($this->course->fullname, $this->course->shortname) =
restore_dbops::calculate_course_names(
$this->course->id,
$this->course->fullname,
$this->course->shortname
);
$rc->execute_plan();
$rc->destroy();
// Set shortname and fullname back, ensure visibility.
$this->course->visible = 1;
$DB->update_record('course', $this->course);
// Clean up after ourselves.
if (empty($CFG->keeptempdirectoriesonbackup)) {
fulldelete($filepath);
}
return true;
}
private function get_settings() {
global $DB;
$settings = $DB->get_records('config_plugins', array('plugin' => 'simple_restore'), null, 'id,name,value');
return $settings;
}
}
class simple_restore_selected_user {
public static function selected_user($data) {
return self::selected($data);
}
private static function selected($data) {
global $DB, $CFG;
$backup = $DB->get_record('files', array('id' => $data->fileid));
if (empty($backup)) {
return true;
}
$fs = get_file_storage();
$browser = get_file_browser();
$filecontext = context::instance_by_id($backup->contextid);
$storedfile = $fs->get_file(
$filecontext->id,
$backup->component,
$backup->filearea,
$backup->itemid,
$backup->filepath,
$backup->filename
);
$fileinfo = new file_info_stored(
$browser,
$filecontext,
$storedfile,
$CFG->wwwroot.'/pluginfile.php',
'',
false,
simple_restore_utils::permission(
'canrestore',
context_course::instance($data->courseid)
),
false,
true
);
$fileinfo->copy_to_pathname($data->to_path);
$data->filename = $backup->filename;
return true;
}
}