dolibarr  16.0.5
oauth.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2015-2018 Frederic France <frederic.france@netlogic.fr>
3  * Copyright (C) 2016 RaphaĆ«l Doursenaud <rdoursenaud@gpcsolutions.fr>
4  * Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  *
19  */
20 
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
30 
31 // $supportedoauth2array is defined into oauth.lib.php
32 
33 // Define $urlwithroot
34 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
35 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
36 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
37 
38 // Load translation files required by the page
39 $langs->loadLangs(array('admin', 'oauth', 'modulebuilder'));
40 
41 // Security check
42 if (!$user->admin) {
44 }
45 
46 $action = GETPOST('action', 'aZ09');
47 $provider = GETPOST('provider', 'aZ09');
48 $label = GETPOST('label', 'aZ09');
49 
50 $error = 0;
51 
52 
53 /*
54  * Actions
55  */
56 
57 if ($action == 'add') { // $provider is OAUTH_XXX
58  if ($provider && $provider != '-1') {
59  $constname = strtoupper($provider).($label ? '-'.$label : '').'_ID';
60 
61  if (getDolGlobalString($constname)) {
62  setEventMessages($langs->trans("AOAuthEntryForThisProviderAndLabelAlreadyHasAKey"), null, 'errors');
63  $error++;
64  } else {
65  dolibarr_set_const($db, $constname, 'ToComplete', 'chaine', 0, '', $conf->entity);
66  setEventMessages($langs->trans("OAuthProviderAdded"), null);
67  }
68  }
69 }
70 if ($action == 'update') {
71  foreach ($conf->global as $key => $val) {
72  if (!empty($val) && preg_match('/^OAUTH_.+_ID$/', $key)) {
73  $constvalue = str_replace('_ID', '', $key);
74  if (!dolibarr_set_const($db, $constvalue.'_ID', GETPOST($constvalue.'_ID'), 'chaine', 0, '', $conf->entity)) {
75  $error++;
76  }
77  // If we reset this provider, we also remove the secret
78  if (!dolibarr_set_const($db, $constvalue.'_SECRET', GETPOST($constvalue.'_ID') ? GETPOST($constvalue.'_SECRET') : '', 'chaine', 0, '', $conf->entity)) {
79  $error++;
80  }
81  }
82  }
83 
84  if (!$error) {
85  setEventMessages($langs->trans("SetupSaved"), null);
86  } else {
87  setEventMessages($langs->trans("Error"), null, 'errors');
88  }
89 }
90 
91 
92 /*
93  * View
94  */
95 
96 llxHeader();
97 
98 $form = new Form($db);
99 
100 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
101 print load_fiche_titre($langs->trans('ConfigOAuth'), $linkback, 'title_setup');
102 
103 print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
104 print '<input type="hidden" name="token" value="'.newToken().'">';
105 print '<input type="hidden" name="action" value="add">';
106 
107 $head = oauthadmin_prepare_head();
108 
109 print dol_get_fiche_head($head, 'services', '', -1, '');
110 
111 
112 print '<span class="opacitymedium">'.$langs->trans("ListOfSupportedOauthProviders").'</span><br><br>';
113 
114 
115 print '<select name="provider" id="provider" class="minwidth150">';
116 print '<option name="-1" value="-1">'.$langs->trans("OAuthProvider").'</option>';
117 foreach ($list as $key) {
118  $supported = 0;
119  $keyforsupportedoauth2array = $key[0];
120 
121  if (in_array($keyforsupportedoauth2array, array_keys($supportedoauth2array))) {
122  $supported = 1;
123  }
124  if (!$supported) {
125  continue; // show only supported
126  }
127 
128  $i++;
129  print '<option name="'.$keyforsupportedoauth2array.'" value="'.str_replace('_NAME', '', $keyforsupportedoauth2array).'">'.$supportedoauth2array[$keyforsupportedoauth2array]['name'].'</option>'."\n";
130 }
131 print '</select>';
132 print ajax_combobox('provider');
133 print ' <input type="text" name="label" value="" placeholder="'.$langs->trans("Label").'" pattern="^\S+$" title="'.$langs->trans("SpaceOrSpecialCharAreNotAllowed").'">';
134 print ' <input type="submit" class="button small" name="add" value="'.$langs->trans("Add").'">';
135 print '</form>';
136 
137 print '<br>';
138 print '<br>';
139 
140 print dol_get_fiche_end();
141 
142 
143 //var_dump($list);
144 foreach ($conf->global as $key => $val) {
145  if (!empty($val) && preg_match('/^OAUTH_.*_ID$/', $key)) {
146  $provider = preg_replace('/_ID$/', '', $key);
147  $listinsetup[] = array($provider.'_NAME', $provider.'_ID', $provider.'_SECRET', 'OAUTH Provider '.str_replace('OAUTH_', '', $provider));
148  }
149 }
150 
151 
152 if (count($listinsetup) > 0) {
153  print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
154  print '<input type="hidden" name="token" value="'.newToken().'">';
155  print '<input type="hidden" name="action" value="update">';
156 
157  print '<div class="div-table-responsive-no-min">';
158  print '<table class="noborder centpercent">';
159 
160  $i = 0;
161 
162  // $list is defined into oauth.lib.php to the list of supporter OAuth providers.
163  foreach ($listinsetup as $key) {
164  $supported = 0;
165  $keyforsupportedoauth2array = $key[0]; // May be OAUTH_GOOGLE_NAME or OAUTH_GOOGLE_xxx_NAME
166  $keyforsupportedoauth2array = preg_replace('/^OAUTH_/', '', $keyforsupportedoauth2array);
167  $keyforsupportedoauth2array = preg_replace('/_NAME$/', '', $keyforsupportedoauth2array);
168  if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
169  $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
170  } else {
171  $keyforprovider = '';
172  }
173  $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
174  $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
175 
176  if (in_array($keyforsupportedoauth2array, array_keys($supportedoauth2array))) {
177  $supported = 1;
178  }
179  if (!$supported) {
180  continue; // show only supported
181  }
182 
183  $i++;
184 
185  print '<tr class="liste_titre'.($i > 1 ? ' liste_titre_add' : '').'">';
186  // Api Name
187  $label = $langs->trans($keyforsupportedoauth2array);
188  print '<td>';
189  print img_picto('', $supportedoauth2array[$keyforsupportedoauth2array]['picto'], 'class="pictofixedwidth"');
190  print $label;
191  if ($keyforprovider) {
192  print ' (<b>'.$keyforprovider.'</b>)';
193  } else {
194  print ' (<b>'.$langs->trans("NoName").'</b>)';
195  }
196  print '</td>';
197  print '<td>';
198  if (!empty($supportedoauth2array[$keyforsupportedoauth2array]['urlforcredentials'])) {
199  print $langs->trans("OAUTH_URL_FOR_CREDENTIAL", $supportedoauth2array[$keyforsupportedoauth2array]['urlforcredentials']);
200  }
201  print '</td>';
202  print '</tr>';
203 
204  if ($supported) {
205  $redirect_uri = $urlwithroot.'/core/modules/oauth/'.$supportedoauth2array[$keyforsupportedoauth2array]['callbackfile'].'_oauthcallback.php';
206  print '<tr class="oddeven value">';
207  print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
208  print '<td><input style="width: 80%" type"text" name="uri'.$keyforsupportedoauth2array.'" value="'.$redirect_uri.'">';
209  print '</td></tr>';
210  } else {
211  print '<tr class="oddeven value">';
212  print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
213  print '<td>'.$langs->trans("FeatureNotYetSupported").'</td>';
214  print '</td></tr>';
215  }
216 
217  // Api Id
218  print '<tr class="oddeven value">';
219  print '<td><label for="'.$key[1].'">'.$langs->trans("OAUTH_ID").'</label></td>';
220  print '<td><input type="text" size="100" id="'.$key[1].'" name="'.$key[1].'" value="'.$conf->global->{$key[1]}.'">';
221  print '</td></tr>';
222 
223  // Api Secret
224  print '<tr class="oddeven value">';
225  print '<td><label for="'.$key[2].'">'.$langs->trans("OAUTH_SECRET").'</label></td>';
226  print '<td><input type="password" size="100" id="'.$key[2].'" name="'.$key[2].'" value="'.$conf->global->{$key[2]}.'">';
227  print '</td></tr>';
228  }
229 
230  print '</table>'."\n";
231  print '</div>';
232 
233  print $form->buttonsSaveCancel("Modify", '');
234 
235  print '</form>';
236 }
237 
238 // End of page
239 llxFooter();
240 $db->close();
ajax_combobox
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1')
Convert a html select field into an ajax combobox.
Definition: ajax.lib.php:438
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
oauthadmin_prepare_head
oauthadmin_prepare_head()
Return array of tabs to used on pages to setup cron module.
Definition: oauth.lib.php:271
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:80
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
dolibarr_set_const
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
Definition: admin.lib.php:627
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59