dolibarr  9.0.0
oauthlogintokens.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014-2018 Frederic France <frederic.france@netlogic.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 <http://www.gnu.org/licenses/>.
17  */
18 
25 require '../main.inc.php';
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
27 require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
30 
31 // Load translation files required by the page
32 $langs->loadLangs(array('admin', 'printing', 'oauth'));
33 
34 if (! $user->admin) accessforbidden();
35 
36 $action = GETPOST('action','alpha');
37 $mode = GETPOST('mode','alpha');
38 $value = GETPOST('value','alpha');
39 $varname = GETPOST('varname', 'alpha');
40 $driver = GETPOST('driver', 'alpha');
41 
42 if (! empty($driver)) $langs->load($driver);
43 
44 if (!$mode) $mode='setup';
45 
46 
47 /*
48  * Action
49  */
50 
51 /*if (($mode == 'test' || $mode == 'setup') && empty($driver))
52 {
53  setEventMessages($langs->trans('PleaseSelectaDriverfromList'), null);
54  header("Location: ".$_SERVER['PHP_SELF'].'?mode=config');
55  exit;
56 }*/
57 
58 if ($action == 'setconst' && $user->admin)
59 {
60  $error=0;
61  $db->begin();
62  foreach ($_POST['setupdriver'] as $setupconst) {
63  //print '<pre>'.print_r($setupconst, true).'</pre>';
64  $result=dolibarr_set_const($db, $setupconst['varname'],$setupconst['value'],'chaine',0,'',$conf->entity);
65  if (! $result > 0) $error++;
66  }
67 
68  if (! $error)
69  {
70  $db->commit();
71  setEventMessages($langs->trans("SetupSaved"), null);
72  }
73  else
74  {
75  $db->rollback();
76  dol_print_error($db);
77  }
78  $action='';
79 }
80 
81 if ($action == 'setvalue' && $user->admin)
82 {
83  $db->begin();
84 
85  $result=dolibarr_set_const($db, $varname, $value,'chaine',0,'',$conf->entity);
86  if (! $result > 0) $error++;
87 
88  if (! $error)
89  {
90  $db->commit();
91  setEventMessages($langs->trans("SetupSaved"), null);
92  }
93  else
94  {
95  $db->rollback();
96  dol_print_error($db);
97  }
98  $action = '';
99 }
100 
101 
102 /*
103  * View
104  */
105 
106 // Define $urlwithroot
107 $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
108 $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
109 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
110 
111 $form = new Form($db);
112 
113 llxHeader('',$langs->trans("PrintingSetup"));
114 
115 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
116 print load_fiche_titre($langs->trans('ConfigOAuth'),$linkback,'title_setup');
117 
118 $head=oauthadmin_prepare_head($mode);
119 
120 dol_fiche_head($head, 'tokengeneration', '', -1, 'technic');
121 
122 
123 if ($mode == 'setup' && $user->admin)
124 {
125 
126  print $langs->trans("OAuthSetupForLogin")."<br><br>\n";
127 
128  foreach($list as $key)
129  {
130  $supported=0;
131  if (in_array($key[0], array_keys($supportedoauth2array))) $supported=1;
132  if (! $supported) continue; // show only supported
133 
134 
135  $OAUTH_SERVICENAME='Unknown';
136  if ($key[0] == 'OAUTH_GITHUB_NAME')
137  {
138  $OAUTH_SERVICENAME='GitHub';
139  $urltorenew=$urlwithroot.'/core/modules/oauth/github_oauthcallback.php?state=user,public_repo&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
140  $urltodelete=$urlwithroot.'/core/modules/oauth/github_oauthcallback.php?action=delete&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
141  $urltocheckperms='https://github.com/settings/applications/';
142  }
143  elseif ($key[0] == 'OAUTH_GOOGLE_NAME')
144  {
145  $OAUTH_SERVICENAME='Google';
146  $urltorenew=$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?state=userinfo_email,userinfo_profile,cloud_print&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
147  $urltodelete=$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
148  $urltocheckperms='https://security.google.com/settings/security/permissions';
149  }
150  elseif ($key[0] == 'OAUTH_STRIPE_TEST_NAME')
151  {
152  $OAUTH_SERVICENAME='StripeTest';
153  $urltorenew=$urlwithroot.'/core/modules/oauth/stripetest_oauthcallback.php?backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
154  $urltodelete='';
155  $urltocheckperms='';
156  }
157  else
158  {
159  $urltorenew='';
160  $urltodelete='';
161  $urltocheckperms='';
162  }
163 
164 
165  // Show value of token
166  $tokenobj=null;
167  // Token
168  require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
169  require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
170  // Dolibarr storage
171  $storage = new DoliStorage($db, $conf);
172  try
173  {
174  $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
175  }
176  catch(Exception $e)
177  {
178  // Return an error if token not found
179  }
180 
181  // Set other properties
182  $refreshtoken=false;
183  $expiredat='';
184 
185  $expire = false;
186  // Is token expired or will token expire in the next 30 seconds
187  if (is_object($tokenobj)) {
188  $expire = ($tokenobj->getEndOfLife() !== $tokenobj::EOL_NEVER_EXPIRES && $tokenobj->getEndOfLife() !== $tokenobj::EOL_UNKNOWN && time() > ($tokenobj->getEndOfLife() - 30));
189  }
190 
191  if ($key[1] != '' && $key[2] != '') {
192  if (is_object($tokenobj)) {
193  $refreshtoken = $tokenobj->getRefreshToken();
194 
195  $endoflife = $tokenobj->getEndOfLife();
196  if ($endoflife == $tokenobj::EOL_NEVER_EXPIRES)
197  {
198  $expiredat = $langs->trans("Never");
199  }
200  elseif ($endoflife == $tokenobj::EOL_UNKNOWN)
201  {
202  $expiredat = $langs->trans("Unknown");
203  }
204  else
205  {
206  $expiredat=dol_print_date($endoflife, "dayhour");
207  }
208  }
209  }
210 
211  $submit_enabled=0;
212 
213  print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&amp;driver='.$driver.'" autocomplete="off">';
214  print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
215  print '<input type="hidden" name="action" value="setconst">';
216 
217 
218  print '<table class="noborder" width="100%">'."\n";
219 
220  print '<tr class="liste_titre">';
221  print '<th class="titlefieldcreate">'.$langs->trans($key[0]).'</th>';
222  print '<th></th>';
223  print '<th></th>';
224  print "</tr>\n";
225 
226  print '<tr class="oddeven">';
227  print '<td'.($key['required']?' class="required"':'').'>';
228  //var_dump($key);
229  print $langs->trans("OAuthIDSecret").'</td>';
230  print '<td>';
231  print $langs->trans("SeePreviousTab");
232  print '</td>';
233  print '<td>';
234  print '</td>';
235  print '</tr>'."\n";
236 
237  print '<tr class="oddeven">';
238  print '<td'.($key['required']?' class="required"':'').'>';
239  //var_dump($key);
240  print $langs->trans("IsTokenGenerated");
241  print '</td>';
242  print '<td>';
243  if (is_object($tokenobj)) print $langs->trans("HasAccessToken");
244  else print $langs->trans("NoAccessToken");
245  print '</td>';
246  print '<td>';
247  // Links to delete/checks token
248  if (is_object($tokenobj))
249  {
250  //test on $storage->hasAccessToken($OAUTH_SERVICENAME) ?
251  print '<a class="button" href="'.$urltodelete.'">'.$langs->trans('DeleteAccess').'</a><br>';
252  }
253  // Request remote token
254  if ($urltorenew)
255  {
256  print '<a class="button" href="'.$urltorenew.'">'.$langs->trans('RequestAccess').'</a><br>';
257  }
258  // Check remote access
259  if ($urltocheckperms)
260  {
261  print '<br>'.$langs->trans("ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME).': <a href="'.$urltocheckperms.'" target="_'.strtolower($OAUTH_SERVICENAME).'">'.$urltocheckperms.'</a>';
262  }
263  print '</td>';
264  print '</tr>';
265 
266  print '<tr class="oddeven">';
267  print '<td'.($key['required']?' class="required"':'').'>';
268  //var_dump($key);
269  print $langs->trans("Token").'</td>';
270  print '<td colspan="2">';
271  if (is_object($tokenobj))
272  {
273  //var_dump($tokenobj);
274  print $tokenobj->getAccessToken().'<br>';
275  //print 'Refresh: '.$tokenobj->getRefreshToken().'<br>';
276  //print 'EndOfLife: '.$tokenobj->getEndOfLife().'<br>';
277  //var_dump($tokenobj->getExtraParams());
278  /*print '<br>Extra: <br><textarea class="quatrevingtpercent">';
279  print ''.join(',',$tokenobj->getExtraParams());
280  print '</textarea>';*/
281  }
282  print '</td>';
283  print '</tr>'."\n";
284 
285  if (is_object($tokenobj))
286  {
287  // Token refresh
288  print '<tr class="oddeven">';
289  print '<td'.($key['required']?' class="required"':'').'>';
290  //var_dump($key);
291  print $langs->trans("TOKEN_REFRESH").'</td>';
292  print '<td colspan="2">';
293  print yn($refreshtoken);
294  print '</td>';
295  print '</tr>';
296 
297  // Token expired
298  print '<tr class="oddeven">';
299  print '<td'.($key['required']?' class="required"':'').'>';
300  //var_dump($key);
301  print $langs->trans("TOKEN_EXPIRED").'</td>';
302  print '<td colspan="2">';
303  print yn($expire);
304  print '</td>';
305  print '</tr>';
306 
307  // Token expired at
308  print '<tr class="oddeven">';
309  print '<td'.($key['required']?' class="required"':'').'>';
310  //var_dump($key);
311  print $langs->trans("TOKEN_EXPIRE_AT").'</td>';
312  print '<td colspan="2">';
313  print $expiredat;
314  print '</td>';
315  print '</tr>';
316  }
317 
318  print '</table>';
319 
320  if (! empty($driver))
321  {
322  if ($submit_enabled) {
323  print '<div class="center"><input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Modify")).'"></div>';
324  }
325  }
326 
327 
328  print '</form>';
329  }
330 }
331 
332 if ($mode == 'test' && $user->admin)
333 {
334  print $langs->trans('PrintTestDesc'.$driver)."<br><br>\n";
335 
336  print '<table class="noborder" width="100%">';
337  if (! empty($driver))
338  {
339  require_once DOL_DOCUMENT_ROOT.'/core/modules/printing/'.$driver.'.modules.php';
340  $classname = 'printing_'.$driver;
341  $langs->load($driver);
342  $printer = new $classname($db);
343  //print '<pre>'.print_r($printer, true).'</pre>';
344  if (count($printer->getlistAvailablePrinters())) {
345  if ($printer->listAvailablePrinters()==0) {
346  print $printer->resprint;
347  } else {
348  setEventMessages($printer->error, $printer->errors, 'errors');
349  }
350  }
351  else {
352  print $langs->trans('PleaseConfigureDriverfromList');
353  }
354  }
355 
356  print '</table>';
357 }
358 
359 if ($mode == 'userconf' && $user->admin)
360 {
361  print $langs->trans('PrintUserConfDesc'.$driver)."<br><br>\n";
362 
363  print '<table class="noborder" width="100%">';
364  print '<tr class="liste_titre">';
365  print '<th>'.$langs->trans("User").'</th>';
366  print '<th>'.$langs->trans("PrintModule").'</th>';
367  print '<th>'.$langs->trans("PrintDriver").'</th>';
368  print '<th>'.$langs->trans("Printer").'</th>';
369  print '<th>'.$langs->trans("PrinterLocation").'</th>';
370  print '<th>'.$langs->trans("PrinterId").'</th>';
371  print '<th>'.$langs->trans("NumberOfCopy").'</th>';
372  print '<th class="center">'.$langs->trans("Delete").'</th>';
373  print "</tr>\n";
374  $sql = 'SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login FROM '.MAIN_DB_PREFIX.'printing as p, '.MAIN_DB_PREFIX.'user as u WHERE p.userid=u.rowid';
375  $resql = $db->query($sql);
376  while ($row=$db->fetch_array($resql)) {
377 
378  print '<tr class="oddeven">';
379  print '<td>'.$row['login'].'</td>';
380  print '<td>'.$row['module'].'</td>';
381  print '<td>'.$row['driver'].'</td>';
382  print '<td>'.$row['printer_name'].'</td>';
383  print '<td>'.$row['printer_location'].'</td>';
384  print '<td>'.$row['printer_id'].'</td>';
385  print '<td>'.$row['copy'].'</td>';
386  print '<td class="center">'.img_picto($langs->trans("Delete"), 'delete').'</td>';
387  print "</tr>\n";
388  }
389  print '</table>';
390 }
391 
392 dol_fiche_end();
393 
394 // End of page
395 llxFooter();
396 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) if(! empty($conf->don->enabled) && $user->rights->societe->lire) if(! empty($conf->tax->enabled) && $user->rights->tax->charges->lire) if(! empty($conf->facture->enabled) &&! empty($conf->commande->enabled) && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(! empty($conf->facture->enabled) && $user->rights->facture->lire) if(! empty($conf->fournisseur->enabled) && $user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1053
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:510
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
oauthadmin_prepare_head()
Return array of tabs to used on pages to setup cron module.
Definition: oauth.lib.php:272
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.