Esempio n. 1
0
/**
 * Create a zip archive containing the exported data.
 *
 * @param array $listing The list of usernames that were exported
 * @param array $files A list of archive files for each user
 */
function create_zipfile($listing, $files)
{
    global $USER;
    if (empty($listing) or empty($files)) {
        return false;
    }
    if (count($listing) != count($files)) {
        throw new MaharaException("Files and listing don't match.");
    }
    // create temporary directories for the export
    $exportdir = get_config('dataroot') . 'export/' . $USER->get('id') . '/' . time() . '/';
    if (!check_dir_exists($exportdir)) {
        throw new SystemException("Couldn't create the temporary export directory {$exportdir}");
    }
    $usersdir = 'users/';
    if (!check_dir_exists($exportdir . $usersdir)) {
        throw new SystemException("Couldn't create the temporary export directory {$usersdir}");
    }
    // move user zipfiles into the export directory
    foreach ($files as $filename) {
        if (copy($filename, $exportdir . $usersdir . basename($filename))) {
            unlink($filename);
        } else {
            throw new SystemException("Couldn't move {$filename} to {$usersdir}");
        }
    }
    // write username listing to a file
    $listingfile = 'usernames.csv';
    if (!file_put_contents($exportdir . $listingfile, data_to_csv($listing))) {
        throw new SystemException("Couldn't write usernames to a file");
    }
    // zip everything up
    $zipfile = $exportdir . 'mahara-bulk-export-' . time() . '.zip';
    $cwd = getcwd();
    $command = sprintf('%s %s %s %s %s', get_config('pathtozip'), get_config('ziprecursearg'), escapeshellarg($zipfile), escapeshellarg($listingfile), escapeshellarg($usersdir));
    $output = array();
    chdir($exportdir);
    exec($command, $output, $returnvar);
    chdir($cwd);
    if ($returnvar != 0) {
        throw new SystemException('Failed to zip the export file: return code ' . $returnvar);
    }
    return $zipfile;
}
Esempio n. 2
0
/**
 * Create a zip archive containing the exported data.
 *
 * @param array $listing The list of usernames that were exported
 * @param array $files A list of archive files for each user
 */
function create_zipfile($listing, $files)
{
    global $USER;
    if (empty($listing) or empty($files)) {
        return false;
    }
    if (count($listing) != count($files)) {
        throw new MaharaException("Files and listing don't match.");
    }
    // create temporary directories for the export
    $exportdir = get_config('dataroot') . 'export/' . $USER->get('id') . '/' . time() . '/';
    if (!check_dir_exists($exportdir)) {
        throw new SystemException("Couldn't create the temporary export directory {$exportdir}");
    }
    $usersdir = 'users/';
    if (!check_dir_exists($exportdir . $usersdir)) {
        throw new SystemException("Couldn't create the temporary export directory {$usersdir}");
    }
    // move user zipfiles into the export directory
    foreach ($files as $filename) {
        if (copy($filename, $exportdir . $usersdir . basename($filename))) {
            unlink($filename);
        } else {
            throw new SystemException("Couldn't move {$filename} to {$usersdir}");
        }
    }
    // write username listing to a file
    $listingfile = 'usernames.csv';
    if (!file_put_contents($exportdir . $listingfile, data_to_csv($listing))) {
        throw new SystemException("Couldn't write usernames to a file");
    }
    // zip everything up
    $filename = 'mahara-bulk-export-' . time() . '.zip';
    try {
        create_zip_archive($exportdir, $filename, array($listingfile, $usersdir));
    } catch (SystemException $e) {
        throw new SystemException('Failed to zip the export file: ' . $e->getMessage());
    }
    return $exportdir . $filename;
}