' . oc_('We are generating a ZIP archive of the files. Please do not close this page. Once the ZIP archive is created, you will be prompted to save the file.') . '

'; print '

'; } // Action to execute if (!isset($_GET['daction']) || empty($_GET['daction'])) { // redirect when starting out in case of double-click and so user knows what is going on printHeader($hdr, $hdrfn); displayZipNotice(10, 100); print ' '; printFooter(); flush(); print ' '; exit; } elseif ($_GET['daction'] == 'download') { // send out archive header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=files-" . $_GET['t'] . ".zip"); header("Content-Length: " . filesize($zipFileName)); header("Cache-control: private"); header("Pragma: public"); // IE issue work around ob_end_flush(); readfile($zipFileName) or err('Unable to read ZIP file', $hdr, $hdrfn); // delete archive unlink($zipFileName); // clean up old ZIP files still around if ($dh = opendir($zipDir)) { $fourHoursAgo = time() - (60 * 60 * 4); while (($file = readdir($dh)) !== false) { if (preg_match("/\.zip$/", $file) && ($mtime = filemtime($zipDir . $file)) && ($mtime < $fourHoursAgo)) { unlink($zipDir . $file); } } } exit; } elseif ($_GET['daction'] == 'complete') { printHeader($hdr, $hdrfn); if (is_file($zipFileName) && (filesize($zipFileName) > 0)) { displayZipNotice(1, 1); print '

' . oc_('The ZIP archive has been created, and the download should begin shortly.') . '

'; if (isset($_GET['skipped']) && ($_GET['skipped'] == 1)) { print '

' . oc_('One or more files were not found or too large to include in the ZIP archive.') . '

'; } print ' '; printFooter(); flush(); print ' '; } else { warn('Failed creating ZIP archive (4)'); } exit; } else { // daction == start // let user know what's going on printHeader($hdr, $hdrfn); // Retrieve assigned submissions $r = ocsql_query($q) or err('Unable to retrieve file data'); $fileAR = array(); while ($l = ocsql_fetch_assoc($r)) { if (!empty($l[$formatDBFldName])) { $fileAR[$l['paperid']] = $l['paperid'] . '.' . $l[$formatDBFldName]; } } ocsql_free_result($r); // free up memory as ZIP could use up a bit $fileARcount = count($fileAR); if ($fileARcount == 0) { warn(oc_('There are no files available')); } // Open ZIP archive $zip = new ZipArchive(); $res = $zip->open($zipFileName, (($_GET['daction']=='start') ? ZIPARCHIVE::CREATE | ZipArchive::OVERWRITE : ZipArchive::CREATE)) or err('Failed creating ZIP archive (1)'); if (($zip->numFiles > 0) && preg_match("/^\d+$/", $zip->getArchiveComment())) { $stoppedAt = $zip->getArchiveComment(); } else { $stoppedAt = 0; } // display progress displayZipNotice((($zip->numFiles > 0) ? $zip->numFiles : ceil($fileARcount * 0.1)), $fileARcount); // add files $files = 0; // count of files added per zip->open if (isset($_GET['skipped']) && ($_GET['skipped'] == 1)) { // track whether we had to skip files $skippedFiles = true; } else { $skippedFiles = false; } foreach ($fileAR as $pid => $fname) { // skip previously processed files if ($pid <= $stoppedAt) { continue; } // reload page in case limit(s) being reached if ( oc_checkTimeout() // processing time reaching timeout || ($files++ >= $zipFileCount) // too many files open || ( ($fileSize = oc_fileSize($dir . $fname)) === false ) // file does not exist || ((memory_get_usage() + ( 2 * $fileSize )) > $memoryLimit) // memory usage will be over limit if file added - 2x factor used (w/memoryLimit @ 95%) ) { if ($files == 1) { $zip->setArchiveComment($pid); // skip file because it's too large $skippedFiles = true; } else { $zip->setArchiveComment($pid - 1); // record 1 less than current file ID so it starts w/current file again } $zip->close(); print ' '; printFooter(); flush(); print ' '; exit; } // add file if (oc_hookSet('zip_add_file')) { call_user_func($GLOBALS['OC_hooksAR']['zip_add_file'][0], $zip, $dir . $fname, $savePath . $fname); } elseif (is_readable($dir . $fname)) { $zip->addFile($dir . $fname, $savePath . $fname) or err('Failed creating ZIP archive (3)'); } // reload script if close to timeout } $zip->close(); // done - redirect user print ' '; printFooter(); flush(); print ' '; } exit; ?>