NEW Introduce function dol_compress_dir

This commit is contained in:
Laurent Destailleur 2017-05-31 00:20:35 +02:00
parent 48a6e9e038
commit c39ca4cff8
4 changed files with 146 additions and 2 deletions

View File

@ -1685,6 +1685,91 @@ function dol_uncompress($inputfile,$outputdir)
}
/**
* Compress a directory and subdirectories into a package file.
*
* @param string $inputdir Source dir name
* @param string $outputfile Target file name
* @param string $mode 'zip'
* @return int <0 if KO, >0 if OK
*/
function dol_compress_dir($inputdir, $outputfile, $mode="zip")
{
$foundhandler=0;
dol_syslog("Try to zip dir ".$inputdir." into ".$outputdir." mode=".$mode);
try
{
if ($mode == 'gz') { $foundhandler=0; }
elseif ($mode == 'bz') { $foundhandler=0; }
elseif ($mode == 'zip')
{
/*if (defined('ODTPHP_PATHTOPCLZIP'))
{
$foundhandler=0; // TODO implement this
include_once ODTPHP_PATHTOPCLZIP.'/pclzip.lib.php';
$archive = new PclZip($outputfile);
$archive->add($inputfile, PCLZIP_OPT_REMOVE_PATH, dirname($inputfile));
//$archive->add($inputfile);
return 1;
}
else*/
if (class_exists('ZipArchive'))
{
$foundhandler=1;
// Initialize archive object
$zip = new ZipArchive();
$zip->open($outputfile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($inputdir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($inputdir) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
// Zip archive will be created only after closing object
$zip->close();
return 1;
}
}
if (! $foundhandler)
{
dol_syslog("Try to zip with format ".$mode." with no handler for this format",LOG_ERR);
return -2;
}
}
catch (Exception $e)
{
global $langs, $errormsg;
$langs->load("errors");
dol_syslog("Failed to open file ".$outputfile, LOG_ERR);
dol_syslog($e->getMessage(), LOG_ERR);
$errormsg=$langs->trans("ErrorFailedToWriteInDir",$outputfile);
return -1;
}
}
/**
* Return file(s) into a directory (by default most recent)
*

View File

@ -19,6 +19,7 @@ ErrorFailToRenameDir=Failed to rename directory '<b>%s</b>' into '<b>%s</b>'.
ErrorFailToCreateDir=Failed to create directory '<b>%s</b>'.
ErrorFailToDeleteDir=Failed to delete directory '<b>%s</b>'.
ErrorFailToMakeReplacementInto=Failed to make replacement into file '<b>%s</b>'.
ErrorFailToGenerateFile=Failed to generate file '<b>%s</b>'.
ErrorThisContactIsAlreadyDefinedAsThisType=This contact is already defined as contact for this type.
ErrorCashAccountAcceptsOnlyCashMoney=This bank account is a cash account, so it accepts payments of type cash only.
ErrorFromToAccountsMustDiffers=Source and targets bank accounts must be different.
@ -115,7 +116,7 @@ ErrorQtyForCustomerInvoiceCantBeNegative=Quantity for line into customer invoice
ErrorWebServerUserHasNotPermission=User account <b>%s</b> used to execute web server has no permission for that
ErrorNoActivatedBarcode=No barcode type activated
ErrUnzipFails=Failed to unzip %s with ZipArchive
ErrNoZipEngine=No engine to unzip %s file in this PHP
ErrNoZipEngine=No engine to zip/unzip %s file in this PHP
ErrorFileMustBeADolibarrPackage=The file %s must be a Dolibarr zip package
ErrorModuleFileRequired=You must select a Dolibarr module package file
ErrorPhpCurlNotInstalled=The PHP CURL is not installed, this is essential to talk with Paypal

View File

@ -16,6 +16,7 @@ PreviousMonthOfInvoice=Previous month (number 1-12) of invoice date
TextPreviousMonthOfInvoice=Previous month (text) of invoice date
NextMonthOfInvoice=Following month (number 1-12) of invoice date
TextNextMonthOfInvoice=Following month (text) of invoice date
ZipFileGeneratedInto=Zip file generated into <b>%s</b>.
YearOfInvoice=Year of invoice date
PreviousYearOfInvoice=Previous year of invoice date

View File

@ -115,9 +115,60 @@ if ($dircustom && $action == 'initmodule' && $modulename)
if ($dircustom && $action == 'generatepackage')
{
$dir = $dircustom.'/'.$modulename;
$modulelowercase=strtolower($module);
// Dir for module
$dir = $dircustom.'/'.$modulelowercase;
// Zip file to build
$FILENAMEZIP='';
// Load module
dol_include_once($modulelowercase.'/core/modules/mod'.$module.'.class.php');
$class='mod'.$module;
if (class_exists($class))
{
try {
$moduleobj = new $class($db);
}
catch(Exception $e)
{
$error++;
dol_print_error($e->getMessage());
}
}
else
{
$error++;
$langs->load("errors");
dol_print_error($langs->trans("ErrorFailedToLoadModuleDescriptorForXXX", $module));
exit;
}
$arrayversion=explode('.',$moduleobj->version,3);
if (count($arrayversion))
{
$FILENAMEZIP="module_".$modulelowercase.'-'.$arrayversion[0].'.'.$arrayversion[1].($arrayversion[2]?".".$arrayversion[2]:"").".zip";
$outputfile = $conf->admin->dir_temp.'/'.$FILENAMEZIP;
$result = dol_compress_dir($dir, $outputfile, 'zip');
if ($result > 0)
{
setEventMessages($langs->trans("ZipFileGeneratedInto", $outputfile), null);
}
else
{
$error++;
$langs->load("errors");
setEventMessages($langs->trans("ErrorFailToGenerateFile", $outputfile), null, 'errors');
}
}
else
{
$error++;
$langs->load("errors");
setEventMessages($langs->trans("ErrorCheckVersionIsDefined"), null, 'errors');
}
}
@ -434,6 +485,12 @@ elseif (! empty($module))
if ($tab == 'buildpackage')
{
if (! class_exists('ZipArchive') && ! defined('ODTPHP_PATHTOPCLZIP'))
{
print img_warning().' '.$langs->trans("ErrNoZipEngine");
print '<br>';
}
print '<form name="generatepackage">';
print '<input type="hidden" name="action" value="generatepackage">';
print '<input type="hidden" name="tab" value="'.dol_escape_htmltag($tab).'">';