From d832bfc056182e41c303a601ab02133bf00332e3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 17 Jul 2017 11:05:38 +0200 Subject: [PATCH] Some fix into dol_compress_dir function. --- htdocs/core/lib/files.lib.php | 19 +++++-- htdocs/langs/en_US/other.lang | 1 + htdocs/modulebuilder/index.php | 96 +++++++++++++++++++++++++++++++--- 3 files changed, 105 insertions(+), 11 deletions(-) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index a785b55a5ec..77502f1737c 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -47,7 +47,7 @@ function dol_basename($pathfile) * @param string $filter Regex filter to restrict list. This regex value must be escaped for '/' by doing preg_quote($var,'/'), since this char is used for preg_match function, * but must not contains the start and end '/'. Filter is checked into basename only. * @param array $excludefilter Array of Regex for exclude filter (example: array('(\.meta|_preview.*\.png)$','^\.')). Exclude is checked into fullpath. - * @param string $sortcriteria Sort criteria ("","fullname","name","date","size") + * @param string $sortcriteria Sort criteria ("","fullname","relativename","name","date","size") * @param string $sortorder Sort order (SORT_ASC, SORT_DESC) * @param int $mode 0=Return array minimum keys loaded (faster), 1=Force all keys like date and size to be loaded (slower), 2=Force load of date only, 3=Force load of size only * @param int $nohook Disable all hooks @@ -1692,7 +1692,7 @@ 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 $outputfile Target file name (output directory must exists and be writable) * @param string $mode 'zip' * @return int <0 if KO, >0 if OK */ @@ -1701,6 +1701,15 @@ function dol_compress_dir($inputdir, $outputfile, $mode="zip") $foundhandler=0; dol_syslog("Try to zip dir ".$inputdir." into ".$outputdir." mode=".$mode); + + if (! dol_is_dir(dirname($outputfile)) || ! is_writable(dirname($outputfile))) + { + global $langs, $errormsg; + $langs->load("errors"); + $errormsg=$langs->trans("ErrorFailedToWriteInDir",$outputfile); + return -3; + } + try { if ($mode == 'gz') { $foundhandler=0; } @@ -1724,7 +1733,7 @@ function dol_compress_dir($inputdir, $outputfile, $mode="zip") // Initialize archive object $zip = new ZipArchive(); - $zip->open($outputfile, ZipArchive::CREATE | ZipArchive::OVERWRITE); + $result = $zip->open($outputfile, ZipArchive::CREATE | ZipArchive::OVERWRITE); // Create recursive directory iterator /** @var SplFileInfo[] $files */ @@ -1759,6 +1768,10 @@ function dol_compress_dir($inputdir, $outputfile, $mode="zip") dol_syslog("Try to zip with format ".$mode." with no handler for this format",LOG_ERR); return -2; } + else + { + return 0; + } } catch (Exception $e) { diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang index e15d490c0f2..e519f7af405 100644 --- a/htdocs/langs/en_US/other.lang +++ b/htdocs/langs/en_US/other.lang @@ -17,6 +17,7 @@ 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. +DocFileGeneratedInto=Doc 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 359c46f17ae..4afaf136e19 100644 --- a/htdocs/modulebuilder/index.php +++ b/htdocs/modulebuilder/index.php @@ -426,9 +426,19 @@ if ($dirins && $action == 'generatepackage') 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'); + $dirofmodule = dol_buildpath($modulelowercase, 0).'/bin'; + $outputfile = $dirofmodule.'/'.$FILENAMEZIP; + if ($dirofmodule) + { + if (! dol_is_dir($dirofmodule)) dol_mkdir($dirofmodule); + $result = dol_compress_dir($dir, $outputfile, 'zip'); + } + else + { + $result = -1; + } + if ($result > 0) { setEventMessages($langs->trans("ZipFileGeneratedInto", $outputfile), null); @@ -448,6 +458,76 @@ if ($dirins && $action == 'generatepackage') } } +if ($dirins && $action == 'generatedoc') +{ + $modulelowercase=strtolower($module); + + // Dir for module + $dir = $dirins.'/'.$modulelowercase; + // Zip file to build + $FILENAMEDOC=''; + + // 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)) + { + $FILENAMEDOC=$modulelowercase.'.html'; + + $dirofmodule = dol_buildpath($modulelowercase, 0).'/doc'; + $outputfile = $dirofmodule.'/'.$FILENAMEDOC; + if ($dirofmodule) + { + if (! dol_is_dir($dirofmodule)) dol_mkdir($dirofmodule); + //... + + $result = 0; + } + else + { + $result = 0; + } + + if ($result > 0) + { + setEventMessages($langs->trans("DocFileGeneratedInto", $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'); + } +} + // Save file if ($action == 'savefile' && empty($cancel)) { @@ -1408,10 +1488,10 @@ elseif (! empty($module)) if (count($arrayversion)) { $FILENAMEZIP="module_".$modulelowercase.'-'.$arrayversion[0].'.'.$arrayversion[1].($arrayversion[2]?".".$arrayversion[2]:"").".zip"; - $outputfile = $conf->admin->dir_temp.'/'.$FILENAMEZIP; + $outputfile = dol_buildpath($modulelowercase, 0).'/bin/'.$FILENAMEZIP; - $FILENAMEDOC="module_".$modulelowercase.'-'.$arrayversion[0].'.'.$arrayversion[1].($arrayversion[2]?".".$arrayversion[2]:"").".md"; - $outputfiledoc = $conf->admin->dir_temp.'/'.$FILENAMEDOC; + $FILENAMEDOC=$modulelowercase.'.html'; + $outputfiledoc = dol_buildpath($modulelowercase, 0).'/doc/'.$FILENAMEDOC; } print '
'; @@ -1430,7 +1510,7 @@ elseif (! empty($module)) print ''; print ''; print ''; - print ''; + print ''; print ''; print '


'; @@ -1445,11 +1525,11 @@ elseif (! empty($module)) print '
'; - print '
'; + print ''; print ''; print ''; print ''; - print ''; + print ''; print '
'; }