Example #1
0
/**
 * Internal function - do not use directly
 */
function upgrade_migrate_files_course($context, $path, $delete)
{
    global $CFG;
    $fullpathname = $CFG->dataroot . '/' . $context->instanceid . $path;
    if (!file_exists($fullpathname)) {
        return;
    }
    $items = new DirectoryIterator($fullpathname);
    $fs = get_file_storage();
    foreach ($items as $item) {
        if ($item->isDot()) {
            continue;
        }
        if ($item->isLink()) {
            // do not delete symbolic links or its children
            $delete_this = false;
        } else {
            $delete_this = $delete;
        }
        if (strpos($path, '/backupdata/') === 0) {
            $filearea = 'course_backup';
            $filepath = substr($path, strlen('/backupdata'));
        } else {
            $filearea = 'course_content';
            $filepath = $path;
        }
        if ($item->isFile()) {
            if (!$item->isReadable()) {
                notify(" File not readable, skipping: " . $fullpathname . $item->getFilename());
                continue;
            }
            $filepath = clean_param($filepath, PARAM_PATH);
            $filename = clean_param($item->getFilename(), PARAM_FILE);
            if ($filename === '') {
                continue;
            }
            if (!$fs->file_exists($context->id, $filearea, '0', $filepath, $filename)) {
                $file_record = array('contextid' => $context->id, 'filearea' => $filearea, 'itemid' => 0, 'filepath' => $filepath, 'filename' => $filename, 'timecreated' => $item->getCTime(), 'timemodified' => $item->getMTime());
                if ($fs->create_file_from_pathname($file_record, $fullpathname . $item->getFilename())) {
                    if ($delete_this) {
                        @unlink($fullpathname . $item->getFilename());
                    }
                }
            }
        } else {
            if ($path == '/' and $item->getFilename() == 'moddata') {
                continue;
                // modules are responsible
            }
            $filepath = clean_param($filepath . $item->getFilename() . '/', PARAM_PATH);
            if ($filepath !== '/backupdata/') {
                $fs->create_directory($context->id, $filearea, 0, $filepath);
            }
            //migrate recursively all subdirectories
            upgrade_migrate_files_course($context, $path . $item->getFilename() . '/', $delete_this);
            if ($delete_this) {
                // delete dir if empty
                @rmdir($fullpathname . $item->getFilename());
            }
        }
    }
    unset($items);
    //release file handles
}
Example #2
0
/**
 * Internal function - do not use directly
 */
function upgrade_migrate_files_course($context, $path, $delete)
{
    global $CFG, $OUTPUT;
    $fullpathname = $CFG->dataroot . '/' . $context->instanceid . $path;
    if (!file_exists($fullpathname)) {
        return;
    }
    $items = new DirectoryIterator($fullpathname);
    $fs = get_file_storage();
    $textlib = textlib_get_instance();
    foreach ($items as $item) {
        if ($item->isDot()) {
            continue;
        }
        if ($item->isLink()) {
            // do not delete symbolic links or its children
            $delete_this = false;
        } else {
            $delete_this = $delete;
        }
        if (strpos($path, '/backupdata/') === 0) {
            $component = 'backup';
            $filearea = 'course';
            $filepath = substr($path, strlen('/backupdata'));
        } else {
            $component = 'course';
            $filearea = 'legacy';
            $filepath = $path;
        }
        if ($item->isFile()) {
            if (!$item->isReadable()) {
                $notification = "File not readable, skipping: " . $fullpathname . $item->getFilename();
                echo $OUTPUT->notification($notification);
                upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
                continue;
            }
            $filepath = clean_param($filepath, PARAM_PATH);
            $filename = clean_param($item->getFilename(), PARAM_FILE);
            if ($filename === '') {
                //unsupported chars, sorry
                continue;
            }
            if ($textlib->strlen($filepath) > 255) {
                // we need something unique and reproducible, sorry no shortening possible
                $filepath = '/directory_over_255_chars/' . md5($filepath) . '/';
                $oldfile = $fullpathname . $item->getFilename();
                $newfile = $filepath . $item->getFilename();
                $notification = "File path longer than 255 chars '{$oldfile}', file path truncated to '{$newfile}'";
                echo $OUTPUT->notification($notification);
                upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
            }
            if ($textlib->strlen($filename) > 255) {
                //try to shorten, but look for collisions
                $oldfile = $fullpathname . $item->getFilename();
                $parts = explode('.', $filename);
                $ext = array_pop($parts);
                $name = implode('.', $parts);
                $name = $textlib->substr($name, 0, 254 - $textlib->strlen($ext));
                $newfilename = $name . '.' . $ext;
                if (file_exists($fullpathname . $newfilename) or $fs->file_exists($context->id, $component, $filearea, '0', $filepath, $newfilename)) {
                    $filename = 'file_name_over_255_chars' . md5($filename) . $ext;
                    // bad luck, file with shortened name exists
                } else {
                    $filename = $newfilename;
                    // shortened name should not cause collisions
                }
                $notification = "File name longer than 255 chars '{$oldfile}', file name truncated to '{$filename}'";
                echo $OUTPUT->notification($notification);
                upgrade_log(UPGRADE_LOG_NOTICE, null, $notification);
            }
            if (!$fs->file_exists($context->id, $component, $filearea, '0', $filepath, $filename)) {
                $file_record = array('contextid' => $context->id, 'component' => $component, 'filearea' => $filearea, 'itemid' => 0, 'filepath' => $filepath, 'filename' => $filename, 'timecreated' => $item->getCTime(), 'timemodified' => $item->getMTime());
                if ($fs->create_file_from_pathname($file_record, $fullpathname . $item->getFilename())) {
                    if ($delete_this) {
                        @unlink($fullpathname . $item->getFilename());
                    }
                }
            }
        } else {
            if ($path == '/' and $item->getFilename() == 'moddata') {
                continue;
                // modules are responsible
            }
            $dirname = clean_param($item->getFilename(), PARAM_PATH);
            if ($dirname === '') {
                //unsupported chars, sorry
                continue;
            }
            $filepath = $filepath . $dirname . '/';
            if ($filepath !== '/backupdata/' and $textlib->strlen($filepath) <= 255) {
                $fs->create_directory($context->id, $component, $filearea, 0, $filepath);
            }
            //migrate recursively all subdirectories
            upgrade_migrate_files_course($context, $path . $item->getFilename() . '/', $delete_this);
            if ($delete_this) {
                // delete dir if empty
                @rmdir($fullpathname . $item->getFilename());
            }
        }
    }
    unset($items);
    //release file handles
}