1<?php
2/* Copyright (C) 2015-2018  Frederic France     <frederic.france@netlogic.fr>
3 * Copyright (C) 2016       Raphaël Doursenaud  <rdoursenaud@gpcsolutions.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 */
19
20/**
21 * \file        htdocs/admin/oauth.php
22 * \ingroup     oauth
23 * \brief       Setup page to configure oauth access api
24 */
25
26require '../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
29
30
31// Define $urlwithroot
32$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
33$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
34//$urlwithroot=DOL_MAIN_URL_ROOT;					// This is to use same domain name than current
35
36// Load translation files required by the page
37$langs->loadLangs(array('admin', 'oauth'));
38
39// Security check
40if (!$user->admin)
41	accessforbidden();
42
43$action = GETPOST('action', 'aZ09');
44
45
46/*
47 * Actions
48 */
49
50if ($action == 'update')
51{
52	$error = 0;
53
54	foreach ($list as $constname) {
55		$constvalue = GETPOST($constname[1], 'alpha');
56		if (!dolibarr_set_const($db, $constname[1], $constvalue, 'chaine', 0, '', $conf->entity))
57			$error++;
58		$constvalue = GETPOST($constname[2], 'alpha');
59		if (!dolibarr_set_const($db, $constname[2], $constvalue, 'chaine', 0, '', $conf->entity))
60			$error++;
61	}
62
63	if (!$error)
64	{
65		setEventMessages($langs->trans("SetupSaved"), null);
66	} else {
67		setEventMessages($langs->trans("Error"), null, 'errors');
68	}
69}
70
71/*
72 * View
73 */
74
75llxHeader();
76
77$form = new Form($db);
78
79$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
80print load_fiche_titre($langs->trans('ConfigOAuth'), $linkback, 'title_setup');
81
82print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
83print '<input type="hidden" name="token" value="'.newToken().'">';
84print '<input type="hidden" name="action" value="update">';
85
86$head = oauthadmin_prepare_head();
87
88print dol_get_fiche_head($head, 'services', '', -1, 'technic');
89
90
91print '<span class="opacitymedium">'.$langs->trans("ListOfSupportedOauthProviders").'</span><br><br>';
92
93print '<table class="noborder centpercent">';
94
95$i = 0;
96
97// $list is defined into oauth.lib.php
98foreach ($list as $key)
99{
100	$supported = 0;
101	if (in_array($key[0], array_keys($supportedoauth2array))) $supported = 1;
102	if (!$supported) continue; // show only supported
103
104	$i++;
105
106	print '<tr class="liste_titre'.($i > 1 ? ' liste_titre_add' : '').'">';
107	// Api Name
108	$label = $langs->trans($key[0]);
109	print '<td>'.$label.'</td>';
110	print '<td>';
111	if (!empty($key[3])) print $langs->trans($key[3]);
112	print '</td>';
113	print '</tr>';
114
115	if ($supported)
116	{
117		$redirect_uri = $urlwithroot.'/core/modules/oauth/'.$supportedoauth2array[$key[0]].'_oauthcallback.php';
118		print '<tr class="oddeven value">';
119		print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
120		print '<td><input style="width: 80%" type"text" name="uri'.$key[0].'" value="'.$redirect_uri.'">';
121		print '</td></tr>';
122	} else {
123		print '<tr class="oddeven value">';
124		print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
125		print '<td>'.$langs->trans("FeatureNotYetSupported").'</td>';
126		print '</td></tr>';
127	}
128
129	// Api Id
130	print '<tr class="oddeven value">';
131	print '<td><label for="'.$key[1].'">'.$langs->trans($key[1]).'</label></td>';
132	print '<td><input type="text" size="100" id="'.$key[1].'" name="'.$key[1].'" value="'.$conf->global->{$key[1]}.'">';
133	print '</td></tr>';
134
135	// Api Secret
136	print '<tr class="oddeven value">';
137	print '<td><label for="'.$key[2].'">'.$langs->trans($key[2]).'</label></td>';
138	print '<td><input type="password" size="100" id="'.$key[2].'" name="'.$key[2].'" value="'.$conf->global->{$key[2]}.'">';
139	print '</td></tr>';
140}
141
142print '</table>'."\n";
143
144print dol_get_fiche_end();
145
146print '<div class="center"><input type="submit" class="button" value="'.$langs->trans('Modify').'" name="button"></div>';
147
148print '</form>';
149
150// End of page
151llxFooter();
152$db->close();
153