From a6534cc755f744dfaa3bdb0ac7e8b3e251a0de82 Mon Sep 17 00:00:00 2001 From: Faustin Date: Fri, 20 Jan 2023 13:31:03 +0100 Subject: [PATCH 1/3] Can edit service name --- htdocs/admin/oauth.php | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/htdocs/admin/oauth.php b/htdocs/admin/oauth.php index 217dfd63cc9..1ed4683496e 100644 --- a/htdocs/admin/oauth.php +++ b/htdocs/admin/oauth.php @@ -48,6 +48,8 @@ $action = GETPOST('action', 'aZ09'); $provider = GETPOST('provider', 'aZ09'); $label = GETPOST('label', 'aZ09'); +$servicetoeditname = GETPOST('servicetoeditname', 'aZ09'); + $error = 0; @@ -72,15 +74,24 @@ if ($action == 'update') { foreach ($conf->global as $key => $val) { if (!empty($val) && preg_match('/^OAUTH_.+_ID$/', $key)) { $constvalue = str_replace('_ID', '', $key); - if (!dolibarr_set_const($db, $constvalue.'_ID', GETPOST($constvalue.'_ID'), 'chaine', 0, '', $conf->entity)) { - $error++; + $newconstvalue = $constvalue; + if (GETPOSTISSET($constvalue.'_NAME')) { + $newconstvalue = preg_replace('/-.*$/', '', $constvalue).'-'.GETPOST($constvalue.'_NAME'); + } + + if (GETPOSTISSET($constvalue.'_ID')) { + if (!dolibarr_set_const($db, $newconstvalue.'_ID', GETPOST($constvalue.'_ID'), 'chaine', 0, '', $conf->entity)) { + $error++; + } } // If we reset this provider, we also remove the secret - if (!dolibarr_set_const($db, $constvalue.'_SECRET', GETPOST($constvalue.'_ID') ? GETPOST($constvalue.'_SECRET') : '', 'chaine', 0, '', $conf->entity)) { - $error++; + if (GETPOSTISSET($constvalue.'_SECRET')) { + if (!dolibarr_set_const($db, $newconstvalue.'_SECRET', GETPOST($constvalue.'_ID') ? GETPOST($constvalue.'_SECRET') : '', 'chaine', 0, '', $conf->entity)) { + $error++; + } } if (GETPOSTISSET($constvalue.'_URLAUTHORIZE')) { - if (!dolibarr_set_const($db, $constvalue.'_URLAUTHORIZE', GETPOST($constvalue.'_URLAUTHORIZE'), 'chaine', 0, '', $conf->entity)) { + if (!dolibarr_set_const($db, $newconstvalue.'_URLAUTHORIZE', GETPOST($constvalue.'_URLAUTHORIZE'), 'chaine', 0, '', $conf->entity)) { $error++; } } @@ -90,14 +101,22 @@ if ($action == 'update') { } else { $scopestring = GETPOST($constvalue.'_SCOPE'); } - if (!dolibarr_set_const($db, $constvalue.'_SCOPE', $scopestring, 'chaine', 0, '', $conf->entity)) { + if (!dolibarr_set_const($db, $newconstvalue.'_SCOPE', $scopestring, 'chaine', 0, '', $conf->entity)) { $error++; } - } else { - if (!dolibarr_set_const($db, $constvalue.'_SCOPE', '', 'chaine', 0, '', $conf->entity)) { + } elseif ($newconstvalue !== $constvalue) { + if (!dolibarr_set_const($db, $newconstvalue.'_SCOPE', '', 'chaine', 0, '', $conf->entity)) { $error++; } } + print $newconstvalue.'_ID'."######".GETPOST($constvalue.'_ID')."\n"; + + if ($constvalue !== $newconstvalue) { + dolibarr_del_const($db, $constvalue.'_ID', $conf->entity); + dolibarr_del_const($db, $constvalue.'_SECRET', $conf->entity); + dolibarr_del_const($db, $constvalue.'_URLAUTHORIZE', $conf->entity); + dolibarr_del_const($db, $constvalue.'_SCOPE', $conf->entity); + } } } @@ -270,11 +289,16 @@ if (count($listinsetup) > 0) { } else { print $label; } - if ($keyforprovider) { + if ($servicetoeditname == $key[0]) { + print ' ()'; + } elseif ($keyforprovider) { print ' ('.$keyforprovider.')'; } else { print ' ('.$langs->trans("NoName").')'; } + if (!($servicetoeditname == $key[0])) { + print ''.img_edit($langs->transnoentitiesnoconv('Edit'), 1).''; + } print ''; print ''; if (!empty($supportedoauth2array[$keyforsupportedoauth2array]['urlforcredentials'])) { From 9806941217c7f96c2612276858b5c1f4464252c5 Mon Sep 17 00:00:00 2001 From: Faustin Date: Sun, 22 Jan 2023 12:37:40 +0100 Subject: [PATCH 2/3] Update what is needed --- htdocs/admin/oauth.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/htdocs/admin/oauth.php b/htdocs/admin/oauth.php index 1ed4683496e..6fadc36a285 100644 --- a/htdocs/admin/oauth.php +++ b/htdocs/admin/oauth.php @@ -109,13 +109,36 @@ if ($action == 'update') { $error++; } } - print $newconstvalue.'_ID'."######".GETPOST($constvalue.'_ID')."\n"; + // If name changed, we have to delete old const and proceed few other changes if ($constvalue !== $newconstvalue) { dolibarr_del_const($db, $constvalue.'_ID', $conf->entity); dolibarr_del_const($db, $constvalue.'_SECRET', $conf->entity); dolibarr_del_const($db, $constvalue.'_URLAUTHORIZE', $conf->entity); dolibarr_del_const($db, $constvalue.'_SCOPE', $conf->entity); + + // Update name of token + $oldname = preg_replace('/^OAUTH_/', '', $constvalue); + $oldprovider = ucfirst(strtolower(preg_replace('/-.*$/', '', $oldname))); + $oldlabel = preg_replace('/^.*-/', '', $oldname); + $newlabel = preg_replace('/^.*-/', '', $newconstvalue); + + $sql = "UPDATE ".MAIN_DB_PREFIX."oauth_token"; + $sql.= " SET service = '".$oldprovider."-".$newlabel."'"; + $sql.= " WHERE service = '".$oldprovider."-".$oldlabel."'"; + + + $resql = $db->query($sql); + if (!$resql) { + $error++; + } + + // Update const where the token was used, might not be exhaustive + if (getDolGlobalString('MAIN_MAIL_SMTPS_OAUTH_SERVICE') == $oldname) { + if (!dolibarr_set_const($db, 'MAIN_MAIL_SMTPS_OAUTH_SERVICE', strtoupper($oldprovider).'-'.$newlabel, 'chaine', 0, '', $conf->entity)) { + $error++; + } + } } } } From 4208cb3bc67341ef7341f9a55aa70b327dd4c645 Mon Sep 17 00:00:00 2001 From: Faustin Date: Sun, 22 Jan 2023 13:54:39 +0100 Subject: [PATCH 3/3] escape missing in sql request --- htdocs/admin/oauth.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/htdocs/admin/oauth.php b/htdocs/admin/oauth.php index 6fadc36a285..8c082d674db 100644 --- a/htdocs/admin/oauth.php +++ b/htdocs/admin/oauth.php @@ -123,9 +123,10 @@ if ($action == 'update') { $oldlabel = preg_replace('/^.*-/', '', $oldname); $newlabel = preg_replace('/^.*-/', '', $newconstvalue); + $sql = "UPDATE ".MAIN_DB_PREFIX."oauth_token"; - $sql.= " SET service = '".$oldprovider."-".$newlabel."'"; - $sql.= " WHERE service = '".$oldprovider."-".$oldlabel."'"; + $sql.= " SET service = '".$db->escape($oldprovider."-".$newlabel)."'"; + $sql.= " WHERE service = '".$db->escape($oldprovider."-".$oldlabel)."'"; $resql = $db->query($sql);