From ee0b888b8ee9c7234541d67c6bc6d564f1b66c11 Mon Sep 17 00:00:00 2001 From: Christophe TREMBLAY-GUILLOUX Date: Mon, 15 Jan 2024 15:38:30 +0100 Subject: [PATCH 1/4] NEW: cronjob: deny to launch a method from a deactivated module --- htdocs/cron/class/cronjob.class.php | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index 1431dc400fd..87c5c0f3745 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -1223,6 +1223,46 @@ class Cronjob extends CommonObject // Run a method if ($this->jobtype == 'method') { + + // Deny to launch a method from a deactivated module + require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; + $modulesdir = dolGetModulesDirs(); + foreach ($modulesdir as $dir) { + $handle = @opendir($dir); + if (is_resource($handle)) { + while (($file = readdir($handle)) !== false) { + if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') { + $modName = substr($file, 0, dol_strlen($file) - 10); + if ($modName) { + try { + $res = include_once $dir.$file; // A class already exists in a different file will send a non catchable fatal error. + if (class_exists($modName)) { + try { + $objMod = new $modName($this->db); + if ($this->objectname == $objMod->getName()) { + $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod))); + if (empty($conf->global->$const_name)) { + $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname); + dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); + $this->lastoutput = $this->error; + $this->lastresult = -1; + $retval = $this->lastresult; + $error++; + } + } + } catch (Exception $e) { + dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR); + } + } + } catch (Exception $e) { + dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR); + } + } + } + } + } + } + // load classes if (!$error) { $ret = dol_include_once($this->classesname); From 2772b97bd4907bf9fcdeb7c807a409b7a0c8773a Mon Sep 17 00:00:00 2001 From: Christophe TREMBLAY-GUILLOUX Date: Mon, 15 Jan 2024 15:52:00 +0100 Subject: [PATCH 2/4] FIX: typo: Swith => Switch --- build/composer/README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/composer/README b/build/composer/README index 2352ac749ab..0871e7cd8b4 100644 --- a/build/composer/README +++ b/build/composer/README @@ -1,5 +1,5 @@ -* Swith to the minimal version of PHP +* Switch to the minimal version of PHP update-alternatives --config php From 8083984f923b6419b2a95d798270b8217a785934 Mon Sep 17 00:00:00 2001 From: Christophe TREMBLAY-GUILLOUX Date: Mon, 15 Jan 2024 16:07:07 +0100 Subject: [PATCH 3/4] FIX: Remove blank line --- htdocs/cron/class/cronjob.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index 87c5c0f3745..d8303a7d30b 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -1223,7 +1223,6 @@ class Cronjob extends CommonObject // Run a method if ($this->jobtype == 'method') { - // Deny to launch a method from a deactivated module require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; $modulesdir = dolGetModulesDirs(); From 0b259bf564468de80c2e8947576ee2dd0b841b9e Mon Sep 17 00:00:00 2001 From: Christophe TREMBLAY-GUILLOUX Date: Mon, 15 Jan 2024 18:07:13 +0100 Subject: [PATCH 4/4] Simpler version to check if a module is enabled --- htdocs/cron/class/cronjob.class.php | 43 +++++------------------------ 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index d8303a7d30b..0ff920750f7 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -1224,42 +1224,13 @@ class Cronjob extends CommonObject // Run a method if ($this->jobtype == 'method') { // Deny to launch a method from a deactivated module - require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; - $modulesdir = dolGetModulesDirs(); - foreach ($modulesdir as $dir) { - $handle = @opendir($dir); - if (is_resource($handle)) { - while (($file = readdir($handle)) !== false) { - if (is_readable($dir.$file) && substr($file, 0, 3) == 'mod' && substr($file, dol_strlen($file) - 10) == '.class.php') { - $modName = substr($file, 0, dol_strlen($file) - 10); - if ($modName) { - try { - $res = include_once $dir.$file; // A class already exists in a different file will send a non catchable fatal error. - if (class_exists($modName)) { - try { - $objMod = new $modName($this->db); - if ($this->objectname == $objMod->getName()) { - $const_name = 'MAIN_MODULE_'.strtoupper(preg_replace('/^mod/i', '', get_class($objMod))); - if (empty($conf->global->$const_name)) { - $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname); - dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); - $this->lastoutput = $this->error; - $this->lastresult = -1; - $retval = $this->lastresult; - $error++; - } - } - } catch (Exception $e) { - dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR); - } - } - } catch (Exception $e) { - dol_syslog("Failed to load ".$dir.$file." ".$e->getMessage(), LOG_ERR); - } - } - } - } - } + if (!empty($this->module_name) && !isModEnabled(strtolower($this->module_name))) { + $this->error = $langs->transnoentitiesnoconv('CronMethodNotAllowed', $this->methodename, $this->objectname); + dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); + $this->lastoutput = $this->error; + $this->lastresult = -1; + $retval = $this->lastresult; + $error++; } // load classes