MDL-26189 protect imsenterprise enrol file
[moodle.git] / enrol / imsenterprise / db / upgrade.php
bloba7d9841ee6417c5b818378d6c16493fdf193dcf3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This file keeps track of upgrades to the imsenterprise enrolment plugin
20 * @package enrol
21 * @subpackage imsenterprise
22 * @copyright 2011 Petr Skoda {@link http://skodak.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 function xmldb_enrol_imsenterprise_upgrade($oldversion) {
29 global $CFG, $DB, $OUTPUT;
31 $dbman = $DB->get_manager();
34 //NOTE: this file is not executed during upgrade from 1.9.x!
37 if ($oldversion < 2011013000) {
38 // this plugin does not use the new file api - lets undo the migration
40 $fs = get_file_storage();
42 if ($DB->record_exists('course', array('id'=>1))) { //course 1 is hardcoded here intentionally!
43 if ($context = get_context_instance(CONTEXT_COURSE, 1)) {
44 if ($file = $fs->get_file($context->id, 'course', 'legacy', 0, '/', 'imsenterprise-enrol.xml')) {
45 if (!file_exists("$CFG->dataroot/1/imsenterprise-enrol.xml")) {
46 check_dir_exists($CFG->dataroot.'/');
47 $file->copy_content_to("$CFG->dataroot/1/imsenterprise-enrol.xml");
49 $file->delete();
54 if (!empty($CFG->enrol_imsfilelocation)) {
55 if (strpos($CFG->enrol_imsfilelocation, "$CFG->dataroot/") === 0) {
56 $location = str_replace("$CFG->dataroot/", '', $CFG->enrol_imsfilelocation);
57 $location = str_replace('\\', '/', $location);
58 $parts = explode('/', $location);
59 $courseid = array_shift($parts);
60 if (is_number($courseid) and $DB->record_exists('course', array('id'=>$courseid))) {
61 if ($context = get_context_instance(CONTEXT_COURSE, $courseid)) {
62 $file = array_pop($parts);
63 if ($parts) {
64 $dir = '/'.implode('/', $parts).'/';
65 } else {
66 $dir = '/';
68 if ($file = $fs->get_file($context->id, 'course', 'legacy', 0, $dir, $file)) {
69 if (!file_exists($CFG->enrol_imsfilelocation)) {
70 check_dir_exists($CFG->dataroot.'/'.$courseid.$dir);
71 $file->copy_content_to($CFG->enrol_imsfilelocation);
73 $file->delete();
80 upgrade_plugin_savepoint(true, 2011013000, 'enrol', 'imsenterprise');
84 return true;