/** * Creates a new archive with the specified name and files * @param string $name Path to the archive with name and extension * @param array $files A numeric array of files * @param string $replace * @return int How many files has been successfully handled */ public function getArchive($name, array $files, $replace = ABSPATH) { set_time_limit(300); if (!class_exists('Zip', false)) { /** @var backupBup $backup */ $backup = $this->getModule(); $backup->loadLibrary('zip'); } $zip = new Zip(); $zip->setZipFile($name); foreach ($files as $file) { $filename = str_replace($replace, '', $file); if (file_exists($file) && is_readable($file) && (substr(basename($file), 0, 3) != 'pcl' && substr($file, -2) != 'gz')) { $stream = @fopen($file, 'rb'); if ($stream) { $zip->addLargeFile($stream, $filename); } } } $zip->finalize(); /* backward */ return rand(100, 1000); }
$flush_me .= hesk_date() . " | {$hesklang['cZIP']}<br />\n"; // Preferrably use the zip extension if (extension_loaded('zip')) { $save_to_zip = $export_dir . $export_name . '.zip'; $zip = new ZipArchive(); $res = $zip->open($save_to_zip, ZipArchive::CREATE); if ($res === TRUE) { $zip->addFile($save_to, "{$export_name}.xml"); $zip->close(); } else { die("{$hesklang['eZIP']} <{$save_to_zip}>\n"); } } elseif (class_exists('ZipArchive')) { require HESK_PATH . 'inc/zip/Zip.php'; $zip = new Zip(); $zip->addLargeFile($save_to, "{$export_name}.xml"); $zip->finalize(); $zip->setZipFile($save_to_zip); } else { require HESK_PATH . 'inc/zip/pclzip.lib.php'; $zip = new PclZip($save_to_zip); $zip->add($save_to, PCLZIP_OPT_REMOVE_ALL_PATH); } // Delete XML, just leave the Zip archive hesk_unlink($save_to); // Echo memory peak usage $flush_me .= hesk_date() . " | " . sprintf($hesklang['pmem'], @memory_get_peak_usage(true) / 1048576) . "<br />\r\n"; // We're done! $flush_me .= hesk_date() . " | {$hesklang['fZIP']}<br /><br />"; $flush_me .= '<a href="' . $save_to_zip . '">' . $hesklang['ch2d'] . "</a>\n"; } else {
public function backup_items($items) { $dir = SNS_BACKUPS_PATH . $this->filename; if (!class_exists('Zip', false)) { require_once SNS_LIB_PATH . 'Zip.php'; } $warns = array('not_readable' => array()); $zip = new Zip(); $zip->setZipFile($dir . '.zip'); self::$zipFile = $zip; foreach ($items as $name => $path) { if ($name == Sns_Option::DB) { $sql_file = SNS_BACKUPS_PATH . 'wp_dump.sql'; Sns_Log::log_action('Exporting DB'); Sns_Backup::export_db($sql_file); Sns_Log::log_action('Exporting DB', SNS_LOG_END); $stream = @fopen($sql_file, 'rb'); if ($stream) { $zip->addLargeFile($stream, 'wp_dump.sql'); } @unlink($sql_file); continue; } Sns_Log::log_action('Backup item - ' . $name); $itemZip = new Zip(); $itemZip->setZipFile(SNS_BACKUPS_PATH . $name . '.zip'); $path = realpath($path); $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS)); $exclude = array(realpath(SNS_BACKUP_ROOT), realpath(SNS_BACKUPS_PATH), realpath(WP_CONTENT_DIR . SNS_DS . 'debug.log')); for ($iterator->rewind(); $iterator->valid(); $iterator->next()) { $file = $iterator->current(); $continue = false; foreach ($exclude as $excludeFile) { if (strpos($file, $excludeFile) !== false) { $continue = true; } } if ($continue) { continue; } if (file_exists($file) && is_readable($file)) { $stream = @fopen($file, 'rb'); if ($stream) { $file = substr($file, strlen($path)); $itemZip->addLargeFile($stream, $file); } } else { $warns['not_readable'][] = $file; } } $itemZip->finalize(); $stream = @fopen(SNS_BACKUPS_PATH . $name . '.zip', 'rb'); if ($stream) { $zip->addLargeFile($stream, $name . '.zip'); } @unlink(SNS_BACKUPS_PATH . $name . '.zip'); Sns_Log::log_action('Backup item - ' . $name, SNS_LOG_END); } Sns_Log::log_action('Summarize item backups'); $zip->finalize(); Sns_Log::log_action('Summarize item backups', SNS_LOG_END); $this->hash; $this->filename; $this->save(); return $warns; }
/** * Creates a new archive with the specified name and files * @param string $name Path to the archive with name and extension * @param array $files A numeric array of files * @param string $replace * @param string $fullPath * @return int How many files has been successfully handled */ public function getArchive($name, array $files, $replace = ABSPATH, $fullPath = false) { set_time_limit(300); if (!class_exists('Zip', false)) { /** @var backupBup $backup */ $backup = $this->getModule(); $backup->loadLibrary('zip'); } $zip = new Zip(); $zip->setZipFile($name); if ($fullPath) { $absPath = null; } else { $absPath = str_replace('/', DS, ABSPATH); } foreach ($files as $filename) { $file = $absPath . $filename; if ($fullPath) { $file = str_replace('\\\\', DS, $filename); } if (file_exists($file) && is_readable($file) && (substr(basename($file), 0, 3) != 'pcl' && substr($file, -2) != 'gz')) { $stream = @fopen($file, 'rb'); if ($stream) { $zip->addLargeFile($stream, $filename); } } } $zip->finalize(); if (false !== strpos($name, 'backup_')) { // if backup created - remove all temporary files from tmp directory $this->clearTmpDirectory(); } /* backward */ return rand(100, 1000); }
public function createDownloadPackage($type, $itemIdArray, $parentId, $version) { $app = JFactory::getApplication(); // If set no_counting_download_time, storeId will be used to check if download file in "no counting download" period sort($itemIdArray); $storeId = md5($type . serialize($itemIdArray) . $version); if ($type == 'document') { $params = JUDownloadHelper::getParams($parentId); } else { $params = JUDownloadHelper::getParams(null, $parentId); } $noCountingDownloadSecond = (int) $params->get('no_counting_download_time', 300); if ($noCountingDownloadSecond > 0) { $storeIdArray = (array) $app->getUserState('com_judownload.download.storeid'); } else { $storeIdArray = array(); } $user = JFactory::getUser(); $downloadZippedFileMode = $params->get('download_zipped_file_mode', 'temp'); $downloadOneFileNoZippedMode = $params->get('download_one_file_no_zipped_mode', 'temp'); // Min download speed. $minDownloadSpeed = (int) $params->get('min_download_speed', 10); $minDownloadSpeed = $minDownloadSpeed > 0 ? $minDownloadSpeed : 10; $minDownloadSpeed = $minDownloadSpeed * 1024; //KBps // Min live time of download package. $adjustFileLiveTime = (int) $params->get('adjust_file_live_time', 60); $adjustFileLiveTime = $adjustFileLiveTime >= 0 ? $adjustFileLiveTime : 60; // Time download package created. $createdTimeDate = JFactory::getDate()->toSql(); $createdTimeStamp = strtotime($createdTimeDate); // Trigger JU Download after download $dispatcher = JDispatcher::getInstance(); JPluginHelper::importPlugin('judownload'); // Physical file path to download, to be used in temp folder mode $downloadFilePath = ''; // True if file is zipped by zip class $zipFile = false; // Get comment for zip package. $zipCommentConfig = $params->get('zip_comment', ''); // Zip comment parsed from $zipCommentConfig case by case $zipComment = ''; if ($type == 'document') { // Get category id. $categoryId = $parentId; $documentIdArray = $itemIdArray; // Download multi documents in the same cat if (count($documentIdArray) > 1) { // In this case : user downloading category. // Sort array document id. sort($documentIdArray); // Create zip package. $zip = new Zip(); $zipFile = true; // Parse zip comment $zipComment = $this->parseCommentTxt($zipCommentConfig, $categoryId); // File id array in all download documents to reference in tmp file table $fileIdArrayInTmpZip = array(); foreach ($documentIdArray AS $documentId) { $documentObject = JUDownloadHelper::getDocumentById($documentId); $documentTitle = $this->filterFileFolderName($documentObject->title); $documentTitle = trim($documentTitle); $fileObjectList = $this->getAllFilesOfDocument($documentId); // If document has file, add document title as a folder contains files if (count($fileObjectList)) { $zip->addDirectory($documentTitle); } // File id array in document to log document.download $fileIdArray = array(); $documentSize = 0; foreach ($fileObjectList AS $fileObject) { $physicalFilePath = $this->getPhysicalFilePath($fileObject->id); if (JFile::exists($physicalFilePath)) { $filePathInZip = $documentTitle . '/' . $this->filterFileFolderName($fileObject->rename); // Add file extension to file path, if the extension is not the same original file $fileExtOri = JFile::getExt($physicalFilePath); $fileExtInZip = JFile::getExt($filePathInZip); if ($fileExtInZip != $fileExtOri) { $filePathInZip = $filePathInZip . '.' . $fileExtOri; } $filePathInZip = trim($filePathInZip); $zip->addLargeFile($physicalFilePath, $filePathInZip); $documentSize += $fileObject->size; if (!in_array($storeId, $storeIdArray)) { $this->updateFileDownloadCounter($fileObject->id); } } $fileIdArray[] = $fileObject->id; $fileIdArrayInTmpZip[] = $fileObject->id; } if (!in_array($storeId, $storeIdArray)) { $this->updateDocumentDownloadCounter($documentId); $dispatcher->trigger('onAfterDownloadDocument', array($documentId, $fileIdArray, $documentSize)); // Add log when download $logData = array( 'user_id' => $user->id, 'event' => 'document.download', 'item_id' => $documentId, 'doc_id' => $documentId, 'value' => $documentSize, 'reference' => implode(',', $fileIdArray) ); JUDownloadFrontHelperLog::addLog($logData); } } // End - Zip file $categoryObject = JUDownloadHelper::getCategoryById($categoryId); $zipFileName = $this->makeSafeFileName($categoryObject->title) . ".zip"; } //Download one document else { /* * Download document when not isset cat_id * Download document we only get first element of documentIds array */ $documentId = $documentIdArray[0]; $documentObject = JUDownloadHelper::getDocumentById($documentId); $documentTitle = $this->filterFileFolderName($documentObject->title); $fileObjectList = $this->getAllFilesOfDocument($documentId); // Zip file even document has only one file, we can change this later here... // Create zip download package. $zip = new Zip(); $zipFile = true; // Parse zip comment $zipComment = $this->parseCommentTxt($zipCommentConfig, $categoryId, $documentId); $documentTitle = trim($documentTitle); //Add document title as a folder contains files $zip->addDirectory($documentTitle); $fileIdArray = array(); $documentSize = 0; foreach ($fileObjectList AS $fileObject) { // One document allow to download by version $physicalFilePath = $this->getPhysicalFilePath($fileObject->id, $version); if (JFile::exists($physicalFilePath)) { $filePathInZip = $documentTitle . '/' . $this->filterFileFolderName($fileObject->rename); // Add file extension to file path, if the extension is not the same original file $fileExtOri = JFile::getExt($physicalFilePath); $fileExtInZip = JFile::getExt($filePathInZip); if ($fileExtInZip != $fileExtOri) { $filePathInZip = $filePathInZip . '.' . $fileExtOri; } $filePathInZip = trim($filePathInZip); $zip->addLargeFile($physicalFilePath, $filePathInZip); $documentSize += $fileObject->size; if (!in_array($storeId, $storeIdArray)) { $this->updateFileDownloadCounter($fileObject->id, $version); } } $fileIdArray[] = $fileObject->id; } // End - Zip file if (!in_array($storeId, $storeIdArray)) { $this->updateDocumentDownloadCounter($documentId); $dispatcher->trigger('onAfterDownloadDocument', array($documentId, $fileIdArray, $documentSize)); // Add log $logData = array( 'user_id' => $user->id, 'event' => 'document.download', 'item_id' => $documentId, 'doc_id' => $documentId, 'value' => $documentSize, 'reference' => implode(',', $fileIdArray) . ($version ? ':' . $version : '') ); JUDownloadFrontHelperLog::addLog($logData); } $zipFileName = $this->makeSafeFileName($documentObject->title . " " . $version) . ".zip"; } } elseif ($type == 'file') { $fileIdArray = $itemIdArray; $documentId = $parentId; //Download multi files in one document if (count($fileIdArray) > 1) { $documentObject = JUDownloadHelper::getDocumentById($documentId); $documentTitle = $this->filterFileFolderName($documentObject->title); $mainCategoryId = JUDownloadFrontHelperCategory::getMainCategoryId($documentId); $zip = new Zip(); $zipFile = true; // Parse zip comment $zipComment = $this->parseCommentTxt($zipCommentConfig, $mainCategoryId, $documentId); $documentTitle = trim($documentTitle); $zip->addDirectory($documentTitle); $documentSize = 0; foreach ($fileIdArray AS $fileId) { // One document allow to download by version $physicalFilePath = $this->getPhysicalFilePath($fileId, $version); if (JFile::exists($physicalFilePath)) { $fileObject = $this->getFileObject($fileId); $filePathInZip = $documentTitle . '/' . $this->filterFileFolderName($fileObject->rename); // Add file extension to file path, if the extension is not the same original file $fileExtOri = JFile::getExt($physicalFilePath); $fileExtInZip = JFile::getExt($filePathInZip); if ($fileExtInZip != $fileExtOri) { $filePathInZip = $filePathInZip . '.' . $fileExtOri; } $filePathInZip = trim($filePathInZip); $zip->addLargeFile($physicalFilePath, $filePathInZip); $documentSize += $fileObject->size; if (!in_array($storeId, $storeIdArray)) { $this->updateFileDownloadCounter($fileId, $version); } } } // End - Zip file // Sort $fileIdArrayInTmpZip before add log and store it to tmp files table sort($fileIdArray); if (!in_array($storeId, $storeIdArray)) { $this->updateDocumentDownloadCounter($documentId); $dispatcher->trigger('onAfterDownloadDocument', array($documentId, $fileIdArray, $documentSize)); // Add log $logData = array( 'user_id' => $user->id, 'event' => 'document.download', 'item_id' => $documentId, 'doc_id' => $documentId, 'value' => $documentSize, 'reference' => implode(',', $fileIdArray) . ($version ? ':' . $version : '') ); JUDownloadFrontHelperLog::addLog($logData); } $zipFileName = $this->makeSafeFileName($documentObject->title . " " . $version) . ".zip"; } //Download one file elseif (count($fileIdArray) == 1) { $zipOneFile = $params->get('zip_one_file', 0); $fileId = $itemIdArray[0]; $fileObject = $this->getFileObject($fileId); // One file allow to download by version $physicalFilePath = $this->getPhysicalFilePath($fileId, $version); $physicalFilePath = JPath::clean($physicalFilePath); $fileExtOri = JFile::getExt($physicalFilePath); $configAllowZipFile = $params->get('allow_zip_file', 1); // Download one file no zipped (File can be zip file or not, but we still using var $zipFileName for general download file name) if ($fileExtOri == "zip" || !$zipOneFile || !$configAllowZipFile) { $zipFile = false; // In this case, $zipFileName is file name(zipped or not) $zipFileName = $this->filterFileFolderName($fileObject->rename); $zipFileName = $this->makeSafeFileName(JFile::stripExt($zipFileName) . " " . $version) . "." . JFile::getExt($zipFileName); // Add file extension to file path, if the extension is not the same original file $fileExtInZip = JFile::getExt($zipFileName); if ($fileExtInZip != $fileExtOri) { $zipFileName = $zipFileName . '.' . $fileExtOri; } $zipFileName = trim($zipFileName); } // Download one file zipped else { // Initialize zip object. $zip = new Zip(); $zipFile = true; $mainCategoryId = JUDownloadFrontHelperCategory::getMainCategoryId($documentId); // Parse zip comment $zipComment = $this->parseCommentTxt($zipCommentConfig, $mainCategoryId, $documentId); if (JFile::exists($physicalFilePath)) { $filePathInZip = $this->filterFileFolderName($fileObject->rename); // Add file extension to file path, if the extension is not the same original file $fileExtInZip = JFile::getExt($filePathInZip); if ($fileExtInZip != $fileExtOri) { $filePathInZip = $filePathInZip . '.' . $fileExtOri; } $filePathInZip = trim($filePathInZip); $zip->addLargeFile($physicalFilePath, $filePathInZip); } // End - Zip file $zipFileName = $this->filterFileFolderName($fileObject->rename); $zipFileName = $this->makeSafeFileName(JFile::stripExt($zipFileName) . " " . $version) . ".zip"; $zipFileName = trim($zipFileName); } if (!in_array($storeId, $storeIdArray)) { $this->updateDocumentDownloadCounter($documentId, $version); $this->updateFileDownloadCounter($fileId, $version); $dispatcher->trigger('onAfterDownloadDocument', array($documentId, $fileIdArray, $fileObject->size)); // Add log $logData = array( 'user_id' => $user->id, 'event' => 'document.download', 'item_id' => $documentId, 'doc_id' => $documentId, 'value' => $fileObject->size, 'reference' => $fileId . ($version ? ':' . $version : '') ); JUDownloadFrontHelperLog::addLog($logData); } } } // Only support download file/document, invalid type -> return false else { return false; } $serverTime = JFactory::getDate()->toSql(); $serverTimeStamp = strtotime($serverTime); // Store ID of download file(s) into session if ($noCountingDownloadSecond > 0) { $storeIdArray = (array) $app->getUserState('com_judownload.download.storeid'); $storeIdArray[$serverTimeStamp] = $storeId; $storeIdArray = array_unique($storeIdArray); $app->setUserState('com_judownload.download.storeid', $storeIdArray); } // Last download time to calculate download interval $session = JFactory::getSession(); $session->set('judl-last-download-time', $serverTime); // If use zip class to zip files, set comment then close the archive if ($zipFile) { // Set comment for zip file $zip->setComment($zipComment); // Close the archive $zip->finalize(); } // Send email by event for each document when download $docIdArray = array(); if ($type == 'file') { $docIdArray[] = $parentId; } else { $docIdArray = $itemIdArray; } $docIdArray = array_unique($docIdArray); //Send mail by event foreach ($docIdArray AS $docId) { JUDownloadFrontHelperMail::sendEmailByEvent('document.download', $docId); } // Download ZIPPED file if ($zipFile) { // Directly download(from zip resource by PHP) $resourceFilePath = $zip->getZipFile(); $transport = 'php'; $speed = (int) $params->get('max_download_speed', 200); $resume = $params->get('resume_download', 1); $downloadMultiParts = $params->get('download_multi_parts', 1); $downloadResult = JUDownloadHelper::downloadFile($resourceFilePath, $zipFileName, $transport, $speed, $resume, $downloadMultiParts); if ($downloadResult !== true) { $this->setError($downloadResult); return false; } } // Download ONE NO ZIPPED file, in this case $zipFileName is the download file name, file can be zip file or not else { // Directly download $transport = $downloadOneFileNoZippedMode; $speed = (int) $params->get('max_download_speed', 200); $resume = $params->get('resume_download', 1); $downloadMultiParts = $params->get('download_multi_parts', 1); $downloadResult = JUDownloadHelper::downloadFile($physicalFilePath, $zipFileName, $transport, $speed, $resume, $downloadMultiParts); if ($downloadResult !== true) { $this->setError($downloadResult); return false; } } return true; }