From c39ca4cff8824a7181b9354a40f5ff3c02135c6c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 31 May 2017 00:20:35 +0200 Subject: [PATCH] NEW Introduce function dol_compress_dir --- htdocs/core/lib/files.lib.php | 85 ++++++++++++++++++++++++++++++++++ htdocs/langs/en_US/errors.lang | 3 +- htdocs/langs/en_US/other.lang | 1 + htdocs/modulebuilder/index.php | 59 ++++++++++++++++++++++- 4 files changed, 146 insertions(+), 2 deletions(-) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index e52f8bf98ec..95fca68aa5d 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -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) * diff --git a/htdocs/langs/en_US/errors.lang b/htdocs/langs/en_US/errors.lang index aea5ad68113..740397a5044 100644 --- a/htdocs/langs/en_US/errors.lang +++ b/htdocs/langs/en_US/errors.lang @@ -19,6 +19,7 @@ ErrorFailToRenameDir=Failed to rename directory '%s' into '%s'. ErrorFailToCreateDir=Failed to create directory '%s'. ErrorFailToDeleteDir=Failed to delete directory '%s'. ErrorFailToMakeReplacementInto=Failed to make replacement into file '%s'. +ErrorFailToGenerateFile=Failed to generate file '%s'. 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 %s 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 diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index 280a1666b02..a14bfffffb6 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -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 %s. YearOfInvoice=Year of invoice date PreviousYearOfInvoice=Previous year of invoice date diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php index e11ff31a303..b3b2c8dac77 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -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 '
'; + } + print '
'; print ''; print '';