dolibarr/htdocs/admin/oauth.php

319 lines
11 KiB
PHP
Raw Normal View History

2015-10-21 18:23:35 +02:00
<?php
2018-05-26 18:41:16 +02:00
/* Copyright (C) 2015-2018 Frederic France <frederic.france@netlogic.fr>
* Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
2022-06-30 01:34:58 +02:00
* Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
2015-10-21 18:23:35 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2015-10-21 18:23:35 +02:00
*/
/**
* \file htdocs/admin/oauth.php
* \ingroup oauth
* \brief Setup page to configure oauth access api
*/
2022-09-07 20:08:59 +02:00
// Load Dolibarr environment
2015-10-21 18:23:35 +02:00
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
2016-10-07 17:53:41 +02:00
require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
2015-10-21 18:23:35 +02:00
2022-01-19 18:23:31 +01:00
// $supportedoauth2array is defined into oauth.lib.php
// Define $urlwithroot
$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
2018-05-26 18:41:16 +02:00
// Load translation files required by the page
2022-09-17 13:49:39 +02:00
$langs->loadLangs(array('admin', 'oauth', 'modulebuilder'));
2015-10-21 18:23:35 +02:00
// Security check
2021-02-26 22:04:03 +01:00
if (!$user->admin) {
accessforbidden();
2021-02-26 22:04:03 +01:00
}
2015-10-21 18:23:35 +02:00
2020-09-16 19:39:50 +02:00
$action = GETPOST('action', 'aZ09');
$provider = GETPOST('provider', 'aZ09');
$label = GETPOST('label', 'aZ09');
2015-10-21 18:23:35 +02:00
2022-06-30 01:34:58 +02:00
$error = 0;
2015-10-21 18:23:35 +02:00
/*
* Actions
*/
2022-06-30 01:34:58 +02:00
if ($action == 'add') { // $provider is OAUTH_XXX
if ($provider && $provider != '-1') {
$constname = strtoupper($provider).($label ? '-'.$label : '').'_ID';
2022-06-30 01:34:58 +02:00
if (getDolGlobalString($constname)) {
setEventMessages($langs->trans("AOAuthEntryForThisProviderAndLabelAlreadyHasAKey"), null, 'errors');
$error++;
} else {
dolibarr_set_const($db, $constname, $langs->trans('ToComplete'), 'chaine', 0, '', $conf->entity);
2022-06-30 01:34:58 +02:00
setEventMessages($langs->trans("OAuthProviderAdded"), null);
}
}
}
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++;
}
// 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.'_URLAUTHORIZE')) {
if (!dolibarr_set_const($db, $constvalue.'_URLAUTHORIZE', GETPOST($constvalue.'_URLAUTHORIZE'), 'chaine', 0, '', $conf->entity)) {
$error++;
}
}
if (GETPOSTISSET($constvalue.'_SCOPE')) {
if (is_array(GETPOST($constvalue.'_SCOPE'))) {
$scopestring = implode(',', GETPOST($constvalue.'_SCOPE'));
} else {
$scopestring = GETPOST($constvalue.'_SCOPE');
}
if (!dolibarr_set_const($db, $constvalue.'_SCOPE', $scopestring, 'chaine', 0, '', $conf->entity)) {
$error++;
}
} else {
if (!dolibarr_set_const($db, $constvalue.'_SCOPE', '', 'chaine', 0, '', $conf->entity)) {
$error++;
}
}
}
}
2022-06-30 01:34:58 +02:00
if (!$error) {
setEventMessages($langs->trans("SetupSaved"), null);
} else {
setEventMessages($langs->trans("Error"), null, 'errors');
}
2015-10-21 18:23:35 +02:00
}
2022-06-30 01:34:58 +02:00
2015-10-21 18:23:35 +02:00
/*
* View
*/
llxHeader();
$form = new Form($db);
$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
print load_fiche_titre($langs->trans('ConfigOAuth'), $linkback, 'title_setup');
2015-10-21 18:23:35 +02:00
2022-09-17 13:41:07 +02:00
print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
print '<input type="hidden" name="token" value="'.newToken().'">';
2022-06-30 01:34:58 +02:00
print '<input type="hidden" name="action" value="add">';
2015-10-21 18:23:35 +02:00
2016-10-07 17:53:41 +02:00
$head = oauthadmin_prepare_head();
2022-06-30 01:51:10 +02:00
print dol_get_fiche_head($head, 'services', '', -1, '');
2019-12-16 21:06:32 +01:00
print '<span class="opacitymedium">'.$langs->trans("ListOfSupportedOauthProviders").'</span><br><br>';
2015-10-21 18:23:35 +02:00
2022-06-30 01:34:58 +02:00
print '<select name="provider" id="provider" class="minwidth150">';
print '<option name="-1" value="-1">'.$langs->trans("OAuthProvider").'</option>';
foreach ($list as $key) {
$supported = 0;
$keyforsupportedoauth2array = $key[0];
if (in_array($keyforsupportedoauth2array, array_keys($supportedoauth2array))) {
$supported = 1;
}
if (!$supported) {
continue; // show only supported
}
$i++;
print '<option name="'.$keyforsupportedoauth2array.'" value="'.str_replace('_NAME', '', $keyforsupportedoauth2array).'">'.$supportedoauth2array[$keyforsupportedoauth2array]['name'].'</option>'."\n";
}
print '</select>';
print ajax_combobox('provider');
2022-09-17 13:49:39 +02:00
print ' <input type="text" name="label" value="" placeholder="'.$langs->trans("Label").'" pattern="^\S+$" title="'.$langs->trans("SpaceOrSpecialCharAreNotAllowed").'">';
print ' <input type="submit" class="button small" name="add" value="'.$langs->trans("Add").'">';
2022-06-30 01:34:58 +02:00
print '<br>';
print '<br>';
2022-09-17 13:41:07 +02:00
print dol_get_fiche_end();
print '</form>';
2015-10-21 18:23:35 +02:00
// Define $listinsetup
foreach ($conf->global as $key => $val) {
if (!empty($val) && preg_match('/^OAUTH_.*_ID$/', $key)) {
$provider = preg_replace('/_ID$/', '', $key);
$listinsetup[] = array(
$provider.'_NAME',
$provider.'_ID',
$provider.'_SECRET',
$provider.'_URLAUTHORIZE', // For custom oauth links
$provider.'_SCOPE' // For custom oauth links
);
}
}
2022-09-17 13:41:07 +02:00
if (count($listinsetup) > 0) {
print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="update">';
2022-01-19 18:23:31 +01:00
2022-09-17 13:41:07 +02:00
print '<div class="div-table-responsive-no-min">';
2022-09-17 13:41:07 +02:00
$i = 0;
2022-09-17 13:41:07 +02:00
// $list is defined into oauth.lib.php to the list of supporter OAuth providers.
foreach ($listinsetup as $key) {
$supported = 0;
$keyforsupportedoauth2array = $key[0]; // May be OAUTH_GOOGLE_NAME or OAUTH_GOOGLE_xxx_NAME
$keyforsupportedoauth2array = preg_replace('/^OAUTH_/', '', $keyforsupportedoauth2array);
$keyforsupportedoauth2array = preg_replace('/_NAME$/', '', $keyforsupportedoauth2array);
if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
$keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
} else {
$keyforprovider = '';
}
$keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
$keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
2022-09-17 13:41:07 +02:00
if (in_array($keyforsupportedoauth2array, array_keys($supportedoauth2array))) {
$supported = 1;
}
2022-09-17 13:41:07 +02:00
if (!$supported) {
continue; // show only supported
}
2022-09-17 13:41:07 +02:00
$i++;
2022-09-19 19:55:18 +02:00
print '<table class="noborder centpercent">';
// OAUTH service name
2022-09-17 13:41:07 +02:00
$label = $langs->trans($keyforsupportedoauth2array);
print '<tr class="liste_titre'.($i > 1 ? ' liste_titre_add' : '').'">';
2022-09-19 19:55:18 +02:00
print '<td class="titlefieldcreate">';
2022-09-17 13:41:07 +02:00
print img_picto('', $supportedoauth2array[$keyforsupportedoauth2array]['picto'], 'class="pictofixedwidth"');
if ($label == $keyforsupportedoauth2array) {
print $supportedoauth2array[$keyforsupportedoauth2array]['name'];
} else {
print $label;
}
2022-09-17 13:41:07 +02:00
if ($keyforprovider) {
print ' (<b>'.$keyforprovider.'</b>)';
} else {
print ' (<b>'.$langs->trans("NoName").'</b>)';
}
print '</td>';
print '<td>';
if (!empty($supportedoauth2array[$keyforsupportedoauth2array]['urlforcredentials'])) {
print $langs->trans("OAUTH_URL_FOR_CREDENTIAL", $supportedoauth2array[$keyforsupportedoauth2array]['urlforcredentials']);
}
print '</td>';
print '</tr>';
2022-09-19 17:41:58 +02:00
if ($supported) {
$redirect_uri = $urlwithroot.'/core/modules/oauth/'.$supportedoauth2array[$keyforsupportedoauth2array]['callbackfile'].'_oauthcallback.php';
print '<tr class="oddeven value">';
print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
print '<td><input style="width: 80%" type"text" name="uri'.$keyforsupportedoauth2array.'" value="'.$redirect_uri.'" disabled>';
print '</td></tr>';
if ($keyforsupportedoauth2array == 'OAUTH_OTHER_NAME') {
print '<tr class="oddeven value">';
print '<td>'.$langs->trans("URLOfServiceForAuthorization").'</td>';
print '<td><input style="width: 80%" type"text" name="'.$key[3].'" value="'.getDolGlobalString($key[3]).'" >';
print '</td></tr>';
}
} else {
print '<tr class="oddeven value">';
print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
print '<td>'.$langs->trans("FeatureNotYetSupported").'</td>';
print '</td></tr>';
}
2022-09-19 17:46:42 +02:00
2022-09-17 13:41:07 +02:00
// Api Id
print '<tr class="oddeven value">';
2022-09-17 13:41:07 +02:00
print '<td><label for="'.$key[1].'">'.$langs->trans("OAUTH_ID").'</label></td>';
print '<td><input type="text" size="100" id="'.$key[1].'" name="'.$key[1].'" value="'.getDolGlobalString($key[1]).'">';
print '</td></tr>';
2022-09-17 13:41:07 +02:00
// Api Secret
print '<tr class="oddeven value">';
2022-09-17 13:41:07 +02:00
print '<td><label for="'.$key[2].'">'.$langs->trans("OAUTH_SECRET").'</label></td>';
print '<td><input type="password" size="100" id="'.$key[2].'" name="'.$key[2].'" value="'.getDolGlobalString($key[2]).'">';
print '</td></tr>';
// TODO Move this into token generation
if ($supported) {
if ($keyforsupportedoauth2array == 'OAUTH_OTHER_NAME') {
print '<tr class="oddeven value">';
print '<td>'.$langs->trans("Scopes").'</td>';
print '<td>';
print '<input style="width: 80%" type"text" name="'.$key[4].'" value="'.getDolGlobalString($key[4]).'" >';
print '</td></tr>';
} else {
2022-09-19 17:46:42 +02:00
$availablescopes = array_flip(explode(',', $supportedoauth2array[$keyforsupportedoauth2array]['availablescopes']));
$currentscopes = explode(',', getDolGlobalString($key[4]));
$scopestodispay = array();
foreach ($availablescopes as $keyscope => $valscope) {
if (in_array($keyscope, $currentscopes)) {
$scopestodispay[$keyscope] = 1;
} else {
$scopestodispay[$keyscope] = 0;
}
}
// Api Scope
print '<tr class="oddeven value">';
print '<td>'.$langs->trans("Scopes").'</td>';
print '<td>';
foreach ($scopestodispay as $scope => $val) {
2022-09-19 19:46:14 +02:00
print '<input type="checkbox" id="'.$keyforprovider.$scope.'" name="'.$key[4].'[]" value="'.$scope.'"'.($val ? ' checked' : '').'>';
print '<label style="margin-right: 10px" for="'.$keyforprovider.$scope.'">'.$scope.'</label>';
2022-09-19 17:46:42 +02:00
}
print '</td></tr>';
}
} else {
print '<tr class="oddeven value">';
print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
print '<td>'.$langs->trans("FeatureNotYetSupported").'</td>';
print '</td></tr>';
}
2022-09-19 19:55:18 +02:00
print '</table>'."\n";
print '<br>';
}
2015-10-21 18:23:35 +02:00
2022-09-17 13:41:07 +02:00
print '</div>';
2015-10-21 18:23:35 +02:00
2022-09-17 13:41:07 +02:00
print $form->buttonsSaveCancel("Modify", '');
2022-09-17 13:41:07 +02:00
print '</form>';
}
2018-07-28 18:03:14 +02:00
// End of page
2015-10-21 18:23:35 +02:00
llxFooter();
$db->close();