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