From 2a2c0aaa5f620f21e316ecb3d34b487954d69afd Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Thu, 2 Nov 2017 20:17:27 +0100 Subject: [PATCH] Clean and debug code to execute CLI commands --- htdocs/core/actions_massactions.inc.php | 2 +- htdocs/core/lib/website.lib.php | 2 + htdocs/cron/class/cronjob.class.php | 137 +++++++++++++++--------- 3 files changed, 88 insertions(+), 53 deletions(-) diff --git a/htdocs/core/actions_massactions.inc.php b/htdocs/core/actions_massactions.inc.php index 0e9d405d44c..e6e9bbea604 100644 --- a/htdocs/core/actions_massactions.inc.php +++ b/htdocs/core/actions_massactions.inc.php @@ -567,7 +567,7 @@ if (! $error && $massaction == "builddoc" && $permtoread && ! GETPOST('button_se $input_files.=' '.escapeshellarg($f); } - $cmd = 'pdftk '.$input_files.' cat output '.escapeshellarg($file); + $cmd = 'pdftk '.escapeshellarg($input_files).' cat output '.escapeshellarg($file); exec($cmd); if (! empty($conf->global->MAIN_UMASK)) diff --git a/htdocs/core/lib/website.lib.php b/htdocs/core/lib/website.lib.php index 4098c89e4d4..1810e32868f 100644 --- a/htdocs/core/lib/website.lib.php +++ b/htdocs/core/lib/website.lib.php @@ -294,6 +294,8 @@ function exportWebSite($website) */ function importWebSite($pathtofile) { + global $db; + $result = 0; $filename = basename($pathtofile); diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index 5ba542c6945..24cac301379 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -1051,61 +1051,17 @@ class Cronjob extends CommonObject // Run a command line if ($this->jobtype=='command') { - $command=escapeshellcmd($this->command); - $command.=" 2>&1"; - dol_mkdir($conf->cronjob->dir_temp); - $outputfile=$conf->cronjob->dir_temp.'/cronjob.'.$userlogin.'.out'; + $outputdir = $conf->cron->dir_temp; + if (empty($outputdir)) $outputdir = $conf->cronjob->dir_temp; - dol_syslog(get_class($this)."::run_jobs system:".$command, LOG_DEBUG); - $output_arr=array(); - - $execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2 - if ($execmethod == 1) + if (! empty($outputdir)) { - exec($command, $output_arr, $retval); - if ($retval != 0) - { - $langs->load("errors"); - dol_syslog(get_class($this)."::run_jobs retval=".$retval, LOG_ERR); - $this->error = 'Error '.$retval; - $this->lastoutput = ''; // Will be filled later - $this->lastresult = $retval; - $retval = $this->lastresult; - $error++; - } + dol_mkdir($outputdir); + $outputfile=$outputdir.'/cronjob.'.$userlogin.'.out'; // File used with popen method + + // Execute a CLI + $retval = $this->executeCLI($this->command, $outputfile); } - if ($execmethod == 2) - { - $ok=0; - $handle = fopen($outputfile, 'w'); - if ($handle) - { - dol_syslog("Run command ".$command); - $handlein = popen($command, 'r'); - while (!feof($handlein)) - { - $read = fgets($handlein); - fwrite($handle,$read); - $output_arr[]=$read; - } - pclose($handlein); - fclose($handle); - } - if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK)); - } - - // Update with result - if (is_array($output_arr) && count($output_arr)>0) - { - foreach($output_arr as $val) - { - $this->lastoutput.=$val."\n"; - } - } - - $this->lastresult=$retval; - - dol_syslog(get_class($this)."::run_jobs output_arr:".var_export($output_arr,true)." lastoutput=".$this->lastoutput." lastresult=".$this->lastresult, LOG_DEBUG); } dol_syslog(get_class($this)."::run_jobs now we update job to track it is finished (with success or error)"); @@ -1125,6 +1081,83 @@ class Cronjob extends CommonObject } + + /** + * Execute a CLI command. + * this->error, this->lastoutput, this->lastresult are also set. + * + * @param string $command Command line + * @param string $outputfile Output file + * @param int $execmethod 0=Use default method, 1=Use the PHP 'exec', 2=Use the 'popen' method + * @return int Retval + */ + function executeCLI($command, $outputfile, $execmethod=0) + { + global $conf, $langs; + + $retval = 0; + + $command=escapeshellcmd($command); + $command.=" 2>&1"; + + if (! empty($conf->global->MAIN_EXEC_USE_POPEN)) $execmethod=$conf->global->MAIN_EXEC_USE_POPEN; + if (empty($execmethod)) $execmethod=1; + //$execmethod=1; + + dol_syslog(get_class($this)."::executeCLI execmethod=".$execmethod." system:".$command, LOG_DEBUG); + $output_arr=array(); + + if ($execmethod == 1) + { + exec($command, $output_arr, $retval); + if ($retval != 0) + { + $langs->load("errors"); + dol_syslog(get_class($this)."::executeCLI retval after exec=".$retval, LOG_ERR); + $this->error = 'Error '.$retval; + $this->lastoutput = ''; // Will be filled later from $output_arr + $this->lastresult = $retval; + $retval = $this->lastresult; + $error++; + } + } + if ($execmethod == 2) // This method may create + { + $ok=0; + $handle = fopen($outputfile, 'w+b'); + if ($handle) + { + dol_syslog(get_class($this)."::executeCLI run command ".$command); + $handlein = popen($command, 'r'); + while (!feof($handlein)) + { + $read = fgets($handlein); + fwrite($handle,$read); + $output_arr[]=$read; + } + pclose($handlein); + fclose($handle); + } + if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK)); + } + + // Update with result + if (is_array($output_arr) && count($output_arr)>0) + { + foreach($output_arr as $val) + { + $this->lastoutput.=$val.($execmethod == 2 ? '' : "\n"); + } + } + + $this->lastresult=$retval; + + dol_syslog(get_class($this)."::executeCLI output_arr:".var_export($output_arr,true)." lastoutput=".$this->lastoutput." lastresult=".$this->lastresult, LOG_DEBUG); + + return $reval; + } + + /** * Reprogram a job *