From 8cc3d2358bf3855c19e203b60a9128d613feb504 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 26 Aug 2023 12:02:19 +0200 Subject: [PATCH] NEW Remove hardcoded code for OVH sms. Generic method is ok now. --- htdocs/admin/sms.php | 18 +--- htdocs/core/class/CSMSFile.class.php | 94 +++++++++------------ htdocs/core/class/conf.class.php | 4 +- htdocs/core/class/extrafields.class.php | 3 +- htdocs/core/class/html.form.class.php | 2 +- htdocs/core/class/html.formmargin.class.php | 1 + htdocs/core/class/html.formother.class.php | 1 + htdocs/core/class/html.formsms.class.php | 25 ++---- 8 files changed, 56 insertions(+), 92 deletions(-) diff --git a/htdocs/admin/sms.php b/htdocs/admin/sms.php index 6cc53eeb4e5..6158ad38286 100644 --- a/htdocs/admin/sms.php +++ b/htdocs/admin/sms.php @@ -185,7 +185,7 @@ if ($action == 'edit') { // Method print ''.$langs->trans("MAIN_SMS_SENDMODE").''; if (count($listofmethods)) { - print $form->selectarray('MAIN_SMS_SENDMODE', $listofmethods, $conf->global->MAIN_SMS_SENDMODE, 1); + print $form->selectarray('MAIN_SMS_SENDMODE', $listofmethods, getDolGlobalString('MAIN_SMS_SENDMODE'), 1); } else { print ''.$langs->trans("None").''; } @@ -224,7 +224,7 @@ if ($action == 'edit') { // Method print ''.$langs->trans("MAIN_SMS_SENDMODE").''; - $text = empty(getDolGlobalString('MAIN_SMS_SENDMODE')) ? '' : $listofmethods[getDolGlobalString('MAIN_SMS_SENDMODE')]; + $text = getDolGlobalString('MAIN_SMS_SENDMODE') ? $listofmethods[getDolGlobalString('MAIN_SMS_SENDMODE')] : ''; if (empty($text)) { $text = $langs->trans("Undefined").' '.img_warning(); } @@ -256,19 +256,7 @@ if ($action == 'edit') { print ''.$langs->trans("Modify").''; - /*if ($conf->global->MAIN_SMS_SENDMODE != 'mail' || ! $linuxlike) - { - if (function_exists('fsockopen') && $port && $server) - { - print ''.$langs->trans("DoTestServerAvailability").''; - } - } - else - { - print ''.$langs->trans("DoTestServerAvailability").''; - }*/ - - if (count($listofmethods) && !empty($conf->global->MAIN_SMS_SENDMODE)) { + if (count($listofmethods) && getDolGlobalString('MAIN_SMS_SENDMODE')) { print ''.$langs->trans("DoTestSend").''; } else { print ''.$langs->trans("DoTestSend").''; diff --git a/htdocs/core/class/CSMSFile.class.php b/htdocs/core/class/CSMSFile.class.php index d09a4413291..0b359f4221d 100644 --- a/htdocs/core/class/CSMSFile.class.php +++ b/htdocs/core/class/CSMSFile.class.php @@ -95,12 +95,12 @@ class CSMSFile } // If ending method not defined - if (empty($conf->global->MAIN_SMS_SENDMODE)) { + if (!getDolGlobalString('MAIN_SMS_SENDMODE')) { $this->error = 'No SMS Engine defined'; throw new Exception('No SMS Engine defined'); } - dol_syslog("CSMSFile::CSMSFile: MAIN_SMS_SENDMODE=".$conf->global->MAIN_SMS_SENDMODE." charset=".$conf->file->character_set_client." from=".$from.", to=".$to.", msg length=".strlen($msg), LOG_DEBUG); + dol_syslog("CSMSFile::CSMSFile: MAIN_SMS_SENDMODE=".getDolGlobalString('MAIN_SMS_SENDMODE')." charset=".$conf->file->character_set_client." from=".$from.", to=".$to.", msg length=".strlen($msg), LOG_DEBUG); dol_syslog("CSMSFile::CSMSFile: deferred=".$deferred." priority=".$priority." class=".$class, LOG_DEBUG); // Action according to choosed sending method @@ -138,69 +138,51 @@ class CSMSFile } if (empty($conf->global->MAIN_DISABLE_ALL_SMS)) { - // Action according to choosed sending method - if (getDolGlobalString('MAIN_SMS_SENDMODE') == 'ovh') { // Backward compatibility @deprecated - dol_include_once('/ovh/class/ovhsms.class.php'); - /** @phpstan-ignore-next-line */ - $sms = new OvhSms($this->db); - $sms->expe = $this->addr_from; - $sms->dest = $this->addr_to; - $sms->message = $this->message; - $sms->deferred = $this->deferred; - $sms->priority = $this->priority; - $sms->class = $this->class; - $sms->nostop = $this->nostop; - - $sms->socid = $this->socid; - $sms->contact_id = $this->contact_id; - $sms->member_id = $this->member_id; - $sms->project = $this->fk_project; - - $res = $sms->SmsSend(); - - if ($res <= 0) { - $this->error = $sms->error; - dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR); - } else { - dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG); - //var_dump($res); // 1973128 - if (!empty($conf->global->MAIN_SMS_DEBUG)) { - $this->dump_sms_result($res); - } + // Action according to the choosed sending method + if (getDolGlobalString('MAIN_SMS_SENDMODE')) { + $sendmode = getDolGlobalString('MAIN_SMS_SENDMODE'); // $conf->global->MAIN_SMS_SENDMODE looks like a value 'module' + $classmoduleofsender = getDolGlobalString('MAIN_MODULE_'.strtoupper($sendmode).'_SMS', $sendmode); // $conf->global->MAIN_MODULE_XXX_SMS looks like a value 'class@module' + if ($classmoduleofsender == 'ovh') { + $classmoduleofsender = 'ovhsms@ovh'; // For backward compatibility } - } elseif (!empty($conf->global->MAIN_SMS_SENDMODE)) { // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module' - $tmp = explode('@', $conf->global->MAIN_SMS_SENDMODE); + + $tmp = explode('@', $classmoduleofsender); $classfile = $tmp[0]; $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]); - dol_include_once('/'.$module.'/class/'.$classfile.'.class.php'); + dol_include_once('/'.$module.'/class/'.strtolower($classfile).'.class.php'); try { $classname = ucfirst($classfile); - $sms = new $classname($this->db); - $sms->expe = $this->addr_from; - $sms->dest = $this->addr_to; - $sms->deferred = $this->deferred; - $sms->priority = $this->priority; - $sms->class = $this->class; - $sms->message = $this->message; - $sms->nostop = $this->nostop; + if (class_exists($classname)) { + $sms = new $classname($this->db); + $sms->expe = $this->addr_from; + $sms->dest = $this->addr_to; + $sms->deferred = $this->deferred; + $sms->priority = $this->priority; + $sms->class = $this->class; + $sms->message = $this->message; + $sms->nostop = $this->nostop; - $sms->socid = $this->socid; - $sms->contact_id = $this->contact_id; - $sms->member_id = $this->member_id; - $sms->fk_project = $this->fk_project; + $sms->socid = $this->socid; + $sms->contact_id = $this->contact_id; + $sms->member_id = $this->member_id; + $sms->fk_project = $this->fk_project; - $res = $sms->SmsSend(); + $res = $sms->SmsSend(); - $this->error = $sms->error; - $this->errors = $sms->errors; - if ($res <= 0) { - dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR); - } else { - dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG); - //var_dump($res); // 1973128 - if (!empty($conf->global->MAIN_SMS_DEBUG)) { - $this->dump_sms_result($res); + $this->error = $sms->error; + $this->errors = $sms->errors; + if ($res <= 0) { + dol_syslog("CSMSFile::sendfile: sms send error=".$this->error, LOG_ERR); + } else { + dol_syslog("CSMSFile::sendfile: sms send success with id=".$res, LOG_DEBUG); + //var_dump($res); // 1973128 + if (!empty($conf->global->MAIN_SMS_DEBUG)) { + $this->dump_sms_result($res); + } } + } else { + $sms = new stdClass(); + $sms->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_MODULE_'.strtoupper($sendmode).'_SMS is not found'; } } catch (Exception $e) { dol_print_error('', 'Error to get list of senders: '.$e->getMessage()); diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index ed8603b1359..b356675ad7e 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -374,11 +374,9 @@ class Conf $newvalue = '/'.$modulename.'/core/'.$partname.'/'; } elseif (in_array($partname, array('models', 'theme'))) { $newvalue = '/'.$modulename.'/'; - } elseif (in_array($partname, array('sms'))) { - $newvalue = '/'.$modulename.'/'; } elseif ($value == 1) { $newvalue = '/'.$modulename.'/core/modules/'.$partname.'/'; // ex: partname = societe - } else { + } else { // $partname can be any other value like 'sms', ... $newvalue = $value; } diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 3e1e61e1f99..67ebcab560f 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -553,7 +553,8 @@ class ExtraFields */ public function update($attrname, $label, $type, $length, $elementtype, $unique = 0, $required = 0, $pos = 0, $param = '', $alwayseditable = 0, $perms = '', $list = '', $help = '', $default = '', $computed = '', $entity = '', $langfile = '', $enabled = '1', $totalizable = 0, $printable = 0, $moreparams = array()) { - global $hookmanager, $action; + global $hookmanager; + global $action; if ($elementtype == 'thirdparty') { $elementtype = 'societe'; diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index e7869e1ed75..dc0d3bd14cc 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -1153,7 +1153,7 @@ class Form public function select_type_of_lines($selected = '', $htmlname = 'type', $showempty = 0, $hidetext = 0, $forceall = 0) { // phpcs:enable - global $langs, $conf; + global $langs; // If product & services are enabled or both disabled. if ($forceall == 1 || (empty($forceall) && isModEnabled("product") && isModEnabled("service")) diff --git a/htdocs/core/class/html.formmargin.class.php b/htdocs/core/class/html.formmargin.class.php index 021c82d5c00..64d279cf1e3 100644 --- a/htdocs/core/class/html.formmargin.class.php +++ b/htdocs/core/class/html.formmargin.class.php @@ -208,6 +208,7 @@ class FormMargin public function displayMarginInfos($object, $force_price = false) { global $langs, $conf, $user, $hookmanager; + global $action; if (!empty($user->socid)) { return; diff --git a/htdocs/core/class/html.formother.class.php b/htdocs/core/class/html.formother.class.php index 221483a1421..8800d5a59fc 100644 --- a/htdocs/core/class/html.formother.class.php +++ b/htdocs/core/class/html.formother.class.php @@ -471,6 +471,7 @@ class FormOther { // phpcs:enable global $conf, $langs, $hookmanager; + global $action; $langs->load('users'); diff --git a/htdocs/core/class/html.formsms.class.php b/htdocs/core/class/html.formsms.class.php index cb393bf8738..940f76d0ae8 100644 --- a/htdocs/core/class/html.formsms.class.php +++ b/htdocs/core/class/html.formsms.class.php @@ -110,7 +110,7 @@ class FormSms public function show_form($morecss = 'titlefield', $showform = 1) { // phpcs:enable - global $conf, $langs, $user, $form; + global $conf, $langs, $form; if (!is_object($form)) { $form = new Form($this->db); @@ -185,21 +185,14 @@ function limitChars(textarea, limit, infodiv) print "\n"; } else { print ''.$langs->trans("SmsFrom").""; - //print ''; - if ($conf->global->MAIN_SMS_SENDMODE == 'ovh') { // For backward compatibility @deprecated - dol_include_once('/ovh/class/ovhsms.class.php'); - try { - $sms = new OvhSms($this->db); - if (empty($conf->global->OVHSMS_ACCOUNT)) { - $resultsender = 'ErrorOVHSMS_ACCOUNT not defined'; - } else { - $resultsender = $sms->SmsSenderList(); - } - } catch (Exception $e) { - dol_print_error('', 'Error to get list of senders: '.$e->getMessage()); + if (getDolGlobalString('MAIN_SMS_SENDMODE')) { + $sendmode = getDolGlobalString('MAIN_SMS_SENDMODE'); // $conf->global->MAIN_SMS_SENDMODE looks like a value 'module' + $classmoduleofsender = getDolGlobalString('MAIN_MODULE_'.strtoupper($sendmode).'_SMS', $sendmode); // $conf->global->MAIN_MODULE_XXX_SMS looks like a value 'class@module' + if ($classmoduleofsender == 'ovh') { + $classmoduleofsender = 'ovhsms@ovh'; // For backward compatibility } - } elseif (!empty($conf->global->MAIN_SMS_SENDMODE)) { // $conf->global->MAIN_SMS_SENDMODE looks like a value 'class@module' - $tmp = explode('@', $conf->global->MAIN_SMS_SENDMODE); + + $tmp = explode('@', $classmoduleofsender); $classfile = $tmp[0]; $module = (empty($tmp[1]) ? $tmp[0] : $tmp[1]); dol_include_once('/'.$module.'/class/'.$classfile.'.class.php'); @@ -210,7 +203,7 @@ function limitChars(textarea, limit, infodiv) $resultsender = $sms->SmsSenderList(); } else { $sms = new stdClass(); - $sms->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_SMS_SENDMODE is not found'; + $sms->error = 'The SMS manager "'.$classfile.'" defined into SMS setup MAIN_MODULE_'.strtoupper($sendmode).'_SMS is not found'; } } catch (Exception $e) { dol_print_error('', 'Error to get list of senders: '.$e->getMessage());