From 2ab510d7465ec71f16889eb8d3776d2b55b6eeff Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 26 Mar 2023 17:59:28 +0200 Subject: [PATCH] Clean code. $hookmanager must be global and never be erased --- htdocs/comm/action/class/actioncomm.class.php | 8 +++++--- htdocs/core/class/CMailFile.class.php | 8 +++++--- htdocs/core/lib/functions.lib.php | 3 ++- htdocs/core/lib/security.lib.php | 4 +++- htdocs/core/website.inc.php | 2 ++ htdocs/expedition/index.php | 1 + htdocs/hrm/index.php | 1 + htdocs/main.inc.php | 1 + htdocs/projet/activity/index.php | 10 +++++----- htdocs/public/payment/newpayment.php | 2 +- htdocs/public/payment/paymentok.php | 3 ++- htdocs/public/project/index.php | 3 ++- htdocs/public/project/viewandvote.php | 3 ++- 13 files changed, 32 insertions(+), 17 deletions(-) diff --git a/htdocs/comm/action/class/actioncomm.class.php b/htdocs/comm/action/class/actioncomm.class.php index 48c51840e83..94daee3164e 100644 --- a/htdocs/comm/action/class/actioncomm.class.php +++ b/htdocs/comm/action/class/actioncomm.class.php @@ -1319,15 +1319,17 @@ class ActionComm extends CommonObject */ public function getActions($socid = 0, $fk_element = 0, $elementtype = '', $filter = '', $sortfield = 'a.datep', $sortorder = 'DESC', $limit = 0) { - global $conf, $langs; + global $conf, $langs, $hookmanager; $resarray = array(); dol_syslog(get_class()."::getActions", LOG_DEBUG); - require_once DOL_DOCUMENT_ROOT . '/core/class/hookmanager.class.php'; - $hookmanager = new HookManager($this->db); // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context + if (!is_object($hookmanager)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; + $hookmanager = new HookManager($db); + } $hookmanager->initHooks(array('agendadao')); $sql = "SELECT a.id"; diff --git a/htdocs/core/class/CMailFile.class.php b/htdocs/core/class/CMailFile.class.php index 01964313ea3..e350cae47bc 100644 --- a/htdocs/core/class/CMailFile.class.php +++ b/htdocs/core/class/CMailFile.class.php @@ -641,7 +641,7 @@ class CMailFile */ public function sendfile() { - global $conf, $db, $langs; + global $conf, $db, $langs, $hookmanager; $errorlevel = error_reporting(); //error_reporting($errorlevel ^ E_WARNING); // Desactive warnings @@ -649,8 +649,10 @@ class CMailFile $res = false; if (empty($conf->global->MAIN_DISABLE_ALL_MAILS)) { - require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; - $hookmanager = new HookManager($db); + if (!is_object($hookmanager)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; + $hookmanager = new HookManager($db); + } $hookmanager->initHooks(array('mail')); $parameters = array(); diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 6a02df0c1c6..5e8ef2a579c 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -210,7 +210,8 @@ function getEntity($element, $shared = 1, $currentobject = null) { global $conf, $mc, $hookmanager, $object, $action, $db; - if (! is_object($hookmanager)) { + if (!is_object($hookmanager)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); } diff --git a/htdocs/core/lib/security.lib.php b/htdocs/core/lib/security.lib.php index 63202c693e4..8e948f2ed32 100644 --- a/htdocs/core/lib/security.lib.php +++ b/htdocs/core/lib/security.lib.php @@ -1114,6 +1114,7 @@ function httponly_accessforbidden($message = 1, $http_response_code = 403, $stri function accessforbidden($message = '', $printheader = 1, $printfooter = 1, $showonlymessage = 0, $params = null) { global $conf, $db, $user, $langs, $hookmanager; + global $action, $object; if (!is_object($langs)) { include_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php'; @@ -1139,12 +1140,13 @@ function accessforbidden($message = '', $printheader = 1, $printfooter = 1, $sho print ''; print '
'; if (empty($showonlymessage)) { - global $action, $object; if (empty($hookmanager)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context $hookmanager->initHooks(array('main')); } + $parameters = array('message'=>$message, 'params'=>$params); $reshook = $hookmanager->executeHooks('getAccessForbiddenMessage', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks print $hookmanager->resPrint; diff --git a/htdocs/core/website.inc.php b/htdocs/core/website.inc.php index 769f8edd8a4..4d067f992d6 100644 --- a/htdocs/core/website.inc.php +++ b/htdocs/core/website.inc.php @@ -122,6 +122,7 @@ if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) { $contentsecuritypolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSPRO'); if (!is_object($hookmanager)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); } $hookmanager->initHooks(array("main")); @@ -154,6 +155,7 @@ if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) { $contentsecuritypolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSP'); if (!is_object($hookmanager)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); } $hookmanager->initHooks(array("main")); diff --git a/htdocs/expedition/index.php b/htdocs/expedition/index.php index a644aeedbad..6cae17a835c 100644 --- a/htdocs/expedition/index.php +++ b/htdocs/expedition/index.php @@ -31,6 +31,7 @@ require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; require_once DOL_DOCUMENT_ROOT.'/expedition/class/expedition.class.php'; $hookmanager = new HookManager($db); + $socid = GETPOST('socid', 'int'); // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array diff --git a/htdocs/hrm/index.php b/htdocs/hrm/index.php index 4fabcaa3840..b8ea9116418 100644 --- a/htdocs/hrm/index.php +++ b/htdocs/hrm/index.php @@ -54,6 +54,7 @@ if (isModEnabled('holiday')) { // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context $hookmanager = new HookManager($db); + $hookmanager->initHooks('hrmindex'); // Load translation files required by the page diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index 30fde1ef185..b21608a55a9 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -1622,6 +1622,7 @@ function top_htmlhead($head, $title = '', $disablejs = 0, $disablehead = 0, $arr //print ''."\n"; if (empty($disablehead)) { if (!is_object($hookmanager)) { + include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); } $hookmanager->initHooks(array("main")); diff --git a/htdocs/projet/activity/index.php b/htdocs/projet/activity/index.php index 8f69a8afada..93da1e218e4 100644 --- a/htdocs/projet/activity/index.php +++ b/htdocs/projet/activity/index.php @@ -37,6 +37,11 @@ if ($search_project_user == $user->id) { $mine = 1; } +$hookmanager = new HookManager($db); + +// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array +$hookmanager->initHooks(array('activityindex')); + // Security check $socid = 0; if ($user->socid > 0) { @@ -47,11 +52,6 @@ if (!$user->rights->projet->lire) { accessforbidden(); } -$hookmanager = new HookManager($db); - -// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array -$hookmanager->initHooks(array('activityindex')); - // Load translation files required by the page $langs->load("projects"); diff --git a/htdocs/public/payment/newpayment.php b/htdocs/public/payment/newpayment.php index a59870fbdeb..17474adddf8 100644 --- a/htdocs/public/payment/newpayment.php +++ b/htdocs/public/payment/newpayment.php @@ -66,8 +66,8 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; + // Hook to be used by external payment modules (ie Payzen, ...) -include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); $hookmanager->initHooks(array('newpayment')); diff --git a/htdocs/public/payment/paymentok.php b/htdocs/public/payment/paymentok.php index 4ee7a4a77ca..cb9eaab2863 100644 --- a/htdocs/public/payment/paymentok.php +++ b/htdocs/public/payment/paymentok.php @@ -58,9 +58,10 @@ if (isModEnabled('paypal')) { require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypal.lib.php'; require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypalfunctions.lib.php'; } + // Hook to be used by external payment modules (ie Payzen, ...) -include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); + $hookmanager->initHooks(array('newpayment')); $langs->loadLangs(array("main", "other", "dict", "bills", "companies", "paybox", "paypal")); diff --git a/htdocs/public/project/index.php b/htdocs/public/project/index.php index dfcfde94b17..ef3f1d1dab0 100644 --- a/htdocs/public/project/index.php +++ b/htdocs/public/project/index.php @@ -50,9 +50,10 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; + // Hook to be used by external payment modules (ie Payzen, ...) -include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); + $hookmanager->initHooks(array('newpayment')); // Load translation files diff --git a/htdocs/public/project/viewandvote.php b/htdocs/public/project/viewandvote.php index 1d9fc58c810..5659fd00dfd 100644 --- a/htdocs/public/project/viewandvote.php +++ b/htdocs/public/project/viewandvote.php @@ -51,9 +51,10 @@ require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php'; + // Hook to be used by external payment modules (ie Payzen, ...) -include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; $hookmanager = new HookManager($db); + $hookmanager->initHooks(array('newpayment')); // For encryption