/**
  * Read a course-object from a zip-file
  * @return course The course
  * @param boolean $delete Delete the file after reading the course?
  * @todo Check if the archive is a correct Chamilo-export
  */
 static function read_course($filename, $delete = false)
 {
     CourseArchiver::clean_backup_dir();
     // Create a temp directory
     $tmp_dir_name = 'CourseArchiver_' . uniqid('');
     $unzip_dir = api_get_path(SYS_ARCHIVE_PATH) . $tmp_dir_name;
     mkdir($unzip_dir, api_get_permissions_for_new_directories(), true);
     copy(api_get_path(SYS_ARCHIVE_PATH) . $filename, $unzip_dir . '/backup.zip');
     // unzip the archive
     $zip = new PclZip($unzip_dir . '/backup.zip');
     chdir($unzip_dir);
     $list = $zip->extract(PCLZIP_OPT_TEMP_FILE_ON);
     if ($list == 0) {
         /*global $app;
           $errorMessage = $zip->errorInfo(true);
           $app['session']->getFlashBag()->add('warning', $errorMessage);*/
     }
     // remove the archive-file
     if ($delete) {
         if (file_exists(api_get_path(SYS_ARCHIVE_PATH) . $filename)) {
             unlink(api_get_path(SYS_ARCHIVE_PATH) . $filename);
         }
     }
     // Read the course
     if (!is_file('course_info.dat')) {
         return new Course();
     }
     $contents = file_get_contents('course_info.dat');
     // CourseCopyLearnpath class appeared in Chamilo 1.8.7, it is the former Learnpath class in the "Copy course" tool.
     // For backward comaptibility with archives created on Chamilo 1.8.6.2 or older systems, we have to do the following:
     // Before unserialization, if class name "Learnpath" was found, it should be renamed as "CourseCopyLearnpath".
     $course = unserialize(str_replace('O:9:"Learnpath":', 'O:19:"CourseCopyLearnpath":', base64_decode($contents)));
     if (get_class($course) != 'Course') {
         return new Course();
     }
     $course->backup_path = $unzip_dir;
     return $course;
 }
示例#2
0
    $course = CourseArchiver::read_course($filename, $delete_file);
    if ($course->has_resources() && $filename !== false) {
        $hiddenFields['same_file_name_option'] = $_POST['same_file_name_option'];
        // Add token to Course select form
        $hiddenFields['sec_token'] = Security::get_token();
        CourseSelectForm::display_form($course, $hiddenFields);
    } elseif ($filename === false) {
        Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'));
        echo '<a class="btn" href="import_backup.php?' . api_get_cidreq() . '">' . get_lang('TryAgain') . '</a>';
    } else {
        Display::display_warning_message(get_lang('NoResourcesInBackupFile'));
        echo '<a class="btn" href="import_backup.php?' . api_get_cidreq() . '">' . get_lang('TryAgain') . '</a>';
    }
} else {
    $user = api_get_user_info();
    $backups = CourseArchiver::get_available_backups($is_platformAdmin ? null : $user['user_id']);
    $backups_available = count($backups) > 0;
    $form = new FormValidator('import_backup_form', 'post', api_get_path(WEB_CODE_PATH) . 'coursecopy/import_backup.php?' . api_get_cidreq(), '', 'multipart/form-data');
    $form->addElement('header', get_lang('SelectBackupFile'));
    $renderer = $form->defaultRenderer();
    $renderer->setElementTemplate('<div>{element}</div> ');
    $form->addElement('hidden', 'action', 'restore_backup');
    $form->addElement('radio', 'backup_type', '', get_lang('LocalFile'), 'local', 'id="bt_local" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=true;document.import_backup_form.backup.disabled=false;"');
    $form->addElement('file', 'backup', '', 'style="margin-left: 50px;"');
    $form->addElement('html', '<br />');
    if ($backups_available) {
        $form->addElement('radio', 'backup_type', '', get_lang('ServerFile'), 'server', 'id="bt_server" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=false;document.import_backup_form.backup.disabled=true;"');
        $options['null'] = '-';
        foreach ($backups as $index => $backup) {
            $options[$backup['file']] = $backup['course_code'] . ' (' . $backup['date'] . ')';
        }
示例#3
0
require_once 'classes/CourseArchiver.class.php';
require_once 'classes/CourseRestorer.class.php';
require_once 'classes/CourseSelectForm.class.php';
// Display the tool title
echo Display::page_header($nameTools);
/*	MAIN CODE */
if (Security::check_token('post') && (isset($_POST['action']) && $_POST['action'] == 'course_select_form' || isset($_POST['backup_option']) && $_POST['backup_option'] == 'full_backup')) {
    // Clear token
    Security::clear_token();
    if (isset($_POST['action']) && $_POST['action'] == 'course_select_form') {
        $course = CourseSelectForm::get_posted_course();
    } else {
        $cb = new CourseBuilder();
        $course = $cb->build();
    }
    $zip_file = CourseArchiver::write_course($course);
    Display::display_confirmation_message(get_lang('BackupCreated'));
    echo '<br /><a class="btn btn-primary btn-large" href="' . api_get_path(WEB_CODE_PATH) . 'course_info/download.php?archive=' . $zip_file . '&' . api_get_cidreq() . '">
    ' . get_lang('Download') . '</a>';
} elseif (Security::check_token('post') && (isset($_POST['backup_option']) && $_POST['backup_option'] == 'select_items')) {
    // Clear token
    Security::clear_token();
    $cb = new CourseBuilder('partial');
    $course = $cb->build();
    // Add token to Course select form
    $hiddenFields['sec_token'] = Security::get_token();
    CourseSelectForm::display_form($course, $hiddenFields);
} else {
    $cb = new CourseBuilder();
    $course = $cb->build();
    if (!$course->has_resources()) {
示例#4
0
        $delete_file = true;
    }
    $course = CourseArchiver::read_course($filename, $delete_file);
    if ($course->has_resources() && $filename !== false) {
        CourseSelectForm::display_form($course, array('same_file_name_option' => $_POST['same_file_name_option']));
    } elseif ($filename === false) {
        Display::display_error_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'));
        echo '<a class="btn" href="import_backup.php?' . api_get_cidreq() . '">' . get_lang('TryAgain') . '</a>';
    } else {
        Display::display_warning_message(get_lang('NoResourcesInBackupFile'));
        echo '<a class="btn" href="import_backup.php?' . api_get_cidreq() . '">' . get_lang('TryAgain') . '</a>';
    }
} else {
    $user = api_get_user_info();
    $user = $is_platformAdmin ? null : $user['user_id'];
    $backups = CourseArchiver::get_available_backups($user);
    $backups_available = count($backups) > 0;
    $form = new FormValidator('import_backup_form', 'post', api_get_path(WEB_PUBLIC_PATH) . 'main/coursecopy/import_backup.php', '', 'multipart/form-data');
    $form->addElement('header', get_lang('SelectBackupFile'));
    $renderer = $form->defaultRenderer();
    $renderer->setElementTemplate('<div>{element}</div> ');
    $form->addElement('hidden', 'action', 'restore_backup');
    $form->addElement('radio', 'backup_type', '', get_lang('LocalFile'), 'local', 'id="bt_local" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=true;document.import_backup_form.backup.disabled=false;"');
    $form->addElement('file', 'backup', '', 'style="margin-left: 50px;"');
    $form->addElement('html', '<br />');
    if ($backups_available) {
        $form->addElement('radio', 'backup_type', '', get_lang('ServerFile'), 'server', 'id="bt_server" class="checkbox" onclick="javascript: document.import_backup_form.backup_server.disabled=false;document.import_backup_form.backup.disabled=true;"');
        $options['null'] = '-';
        foreach ($backups as $index => $backup) {
            $options[$backup['file']] = $backup['course_code'] . ' (' . $backup['date'] . ')';
        }
 /**
  * Read a course-object from a zip-file
  * @param string $filename
  * @param boolean $delete Delete the file after reading the course?
  *
  * @return course The course
  * @todo Check if the archive is a correct Chamilo-export
  */
 public static function read_course($filename, $delete = false)
 {
     CourseArchiver::clean_backup_dir();
     // Create a temp directory
     $tmp_dir_name = 'CourseArchiver_' . uniqid('');
     $unzip_dir = api_get_path(SYS_ARCHIVE_PATH) . '' . $tmp_dir_name;
     @mkdir($unzip_dir, api_get_permissions_for_new_directories(), true);
     @copy(api_get_path(SYS_ARCHIVE_PATH) . '' . $filename, $unzip_dir . '/backup.zip');
     // unzip the archive
     $zip = new PclZip($unzip_dir . '/backup.zip');
     @chdir($unzip_dir);
     $zip->extract(PCLZIP_OPT_TEMP_FILE_ON);
     // remove the archive-file
     if ($delete) {
         @unlink(api_get_path(SYS_ARCHIVE_PATH) . '' . $filename);
     }
     // read the course
     if (!is_file('course_info.dat')) {
         return new Course();
     }
     $fp = @fopen('course_info.dat', "r");
     $contents = @fread($fp, filesize('course_info.dat'));
     @fclose($fp);
     // CourseCopyLearnpath class appeared in Chamilo 1.8.7, it is the former Learnpath class in the "Copy course" tool.
     // For backward comaptibility with archives created on Chamilo 1.8.6.2 or older systems, we have to do the following:
     // Before unserialization, if class name "Learnpath" was found, it should be renamed as "CourseCopyLearnpath".
     $course = unserialize(str_replace('O:9:"Learnpath":', 'O:19:"CourseCopyLearnpath":', base64_decode($contents)));
     if (get_class($course) != 'Course') {
         return new Course();
     }
     $course->backup_path = $unzip_dir;
     return $course;
 }