27require
'../main.inc.php';
28require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
29require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/class/doleditor.class.php';
32use OAuth\Common\Storage\DoliStorage;
33use OAuth\Common\Consumer\Credentials;
38$langs->loadLangs(array(
'admin',
'printing',
'oauth'));
40$action =
GETPOST(
'action',
'aZ09');
41$mode =
GETPOST(
'mode',
'alpha');
42$value =
GETPOST(
'value',
'alpha');
43$varname =
GETPOST(
'varname',
'alpha');
44$driver =
GETPOST(
'driver',
'alpha');
47 $langs->load($driver);
70if ($action ==
'setconst' && $user->admin) {
74 $setupconstarray =
GETPOST(
'setupdriver',
'array');
76 foreach ($setupconstarray as $setupconst) {
84 $result =
dolibarr_set_const($db, $constname, $constvalue, $consttype, 0, $constnote, $conf->entity);
100if ($action ==
'setvalue' && $user->admin) {
104 if (!($result > 0)) {
119if ($action ==
'refreshtoken' && $user->admin) {
120 $keyforprovider =
GETPOST(
'keyforprovider');
121 $OAUTH_SERVICENAME =
GETPOST(
'service');
126 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
128 $keyforsupportedoauth2array = $OAUTH_SERVICENAME;
129 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
130 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
132 $keyforprovider =
'';
134 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', strtoupper($keyforsupportedoauth2array));
135 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
137 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
140 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
143 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
145 dol_syslog(
"oauthlogintokens.php: Read token for service ".$OAUTH_SERVICENAME);
146 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
148 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
150 $refreshtoken = $tokenobj->getRefreshToken();
151 print
'<!-- data stored into field token: '.$storage->token.
' - expire '.((string) $expire).
' -->';
160 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
161 $keyforoauthservice = strtoupper($oauthname[0]).(empty($oauthname[1]) ?
'' :
'-'.$oauthname[1]);
162 $credentials =
new Credentials(
168 $serviceFactory = new \OAuth\ServiceFactory();
169 $httpClient = new \OAuth\Common\Http\Client\CurlClient();
173 $serviceFactory->setHttpClient($httpClient);
176 if (preg_match(
'/^Microsoft/', $OAUTH_SERVICENAME)) {
178 $tmp = explode(
'-', $OAUTH_SERVICENAME);
179 $scopes = explode(
',',
getDolGlobalString(
'OAUTH_'.strtoupper($tmp[0]).(empty($tmp[1]) ?
'' :
'-'.$tmp[1]).
'_SCOPE'));
183 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, $scopes);
185 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
187 dol_syslog(
"oauthlogintokens.php: call refreshAccessToken to get the new access token");
188 $tokenobj = $apiService->refreshAccessToken($tokenobj);
190 dol_syslog(
"oauthlogintokens.php: call setRefreshToken");
191 $tokenobj->setRefreshToken($refreshtoken);
193 dol_syslog(
"oauthlogintokens.php: call storeAccessToken to save the new access token + the old refresh token");
194 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
197 setEventMessages($langs->trans(
"OldTokenWasExpiredItHasBeenRefresh"),
null,
'mesgs');
199 setEventMessages($langs->trans(
"OldTokenWasNotExpiredButItHasBeenRefresh"),
null,
'mesgs');
202 dol_print_error($db,
'apiService is not a correct OAUTH2 Abstract service');
205 dol_syslog(
"oauthlogintokens.php: Read token again for service ".$OAUTH_SERVICENAME);
206 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
209 print $e->getMessage();
219$urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
220$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
223$form =
new Form($db);
225$title = $langs->trans(
"TokenManager");
226$help_url =
'EN:Module_OAuth|FR:Module_OAuth_FR|ES:Módulo_OAuth_ES';
228llxHeader(
'', $title, $help_url,
'', 0, 0,
'',
'',
'',
'mod-admin page-oauthlogintokens');
230$linkback =
'<a href="'.DOL_URL_ROOT.
'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToModuleList").
'</a>';
231print
load_fiche_titre($langs->trans(
'ConfigOAuth'), $linkback,
'title_setup');
241if ($mode ==
'setup' && $user->admin) {
242 print
'<span class="opacitymedium">'.$langs->trans(
"OAuthSetupForLogin").
"</span><br><br>\n";
245 $listinsetup = array();
246 foreach ($conf->global as $key => $val) {
247 if (!empty($val) && preg_match(
'/^OAUTH_.*_ID$/', $key)) {
248 $provider = preg_replace(
'/_ID$/',
'', $key);
249 $listinsetup[] = array(
253 $provider.
'_URLAUTHORIZE',
259 $oauthstateanticsrf = bin2hex(random_bytes(128 / 8));
262 if (!empty($listinsetup)) {
263 foreach ($listinsetup as $key) {
265 $keyforsupportedoauth2array = $key[0];
266 $keyforsupportedoauth2array = preg_replace(
'/^OAUTH_/',
'', $keyforsupportedoauth2array);
267 $keyforsupportedoauth2array = preg_replace(
'/_NAME$/',
'', $keyforsupportedoauth2array);
268 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
269 $keybeforeprovider = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
270 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
272 $keybeforeprovider = $keyforsupportedoauth2array;
273 $keyforprovider =
'';
275 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', strtoupper($keyforsupportedoauth2array));
276 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
278 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
279 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
280 $OAUTH_SERVICENAME = $nameofservice;
282 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
288 $state = $shortscope;
290 $urltorefresh = $_SERVER[
"PHP_SELF"].
'?action=refreshtoken&token='.
newToken();
293 if ($keyforsupportedoauth2array ==
'OAUTH_GITHUB_NAME') {
298 $urltorenew = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($shortscope).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
299 $urltodelete = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
300 $urltocheckperms =
'https://github.com/settings/applications/';
301 } elseif ($keyforsupportedoauth2array ==
'OAUTH_GOOGLE_NAME') {
305 $urltorenew = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'-'.$oauthstateanticsrf.
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
306 $urltodelete = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
307 $urltocheckperms =
'https://security.google.com/settings/security/permissions';
308 } elseif (!empty($supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'])) {
309 $urltorenew = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
310 $urltodelete = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
311 $urltocheckperms =
'';
315 $urltocheckperms =
'';
319 $urltorenew .=
'&keyforprovider='.urlencode($keyforprovider);
322 $urltorefresh .=
'&keyforprovider='.urlencode($keyforprovider).
'&service='.urlencode($OAUTH_SERVICENAME);
325 $urltodelete .=
'&keyforprovider='.urlencode($keyforprovider);
331 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
333 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
336 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
337 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
338 print
'<!-- data stored into field token: '.$storage->token.
' -->';
348 $refreshtoken =
false;
353 if (is_object($tokenobj)) {
354 $expire = ($tokenobj->getEndOfLife() !== $tokenobj::EOL_NEVER_EXPIRES && $tokenobj->getEndOfLife() !== $tokenobj::EOL_UNKNOWN && time() > ($tokenobj->getEndOfLife() - 30));
356 if ($key[1] !=
'' && $key[2] !=
'') {
357 if (is_object($tokenobj)) {
358 $refreshtoken = $tokenobj->getRefreshToken();
360 $endoflife = $tokenobj->getEndOfLife();
361 if ($endoflife == $tokenobj::EOL_NEVER_EXPIRES) {
362 $expiredat = $langs->trans(
"Never");
363 } elseif ($endoflife == $tokenobj::EOL_UNKNOWN) {
364 $expiredat = $langs->trans(
"Unknown");
373 print
'<form method="post" action="'.$_SERVER[
"PHP_SELF"].
'?mode=setup&driver='.$driver.
'" autocomplete="off">';
374 print
'<input type="hidden" name="token" value="'.newToken().
'">';
375 print
'<input type="hidden" name="action" value="setconst">';
376 print
'<input type="hidden" name="page_y" value="">';
378 print
'<div class="div-table-responsive-no-min">';
379 print
'<table class="noborder centpercent">'.
"\n";
382 $label = $langs->trans($keyforsupportedoauth2array);
383 print
'<tr class="liste_titre">';
384 print
'<th class="titlefieldcreate">';
385 print
img_picto(
'', $supportedoauth2array[$keyforsupportedoauth2array][
'picto'],
'class="pictofixedwidth"');
386 if ($label == $keyforsupportedoauth2array) {
387 print $supportedoauth2array[$keyforsupportedoauth2array][
'name'];
391 if ($keyforprovider) {
392 print
' (<b>'.$keyforprovider.
'</b>)';
394 print
' (<b>'.$langs->trans(
"NoName").
'</b>)';
401 print
'<tr class="oddeven">';
404 print $langs->trans(
"OAuthIDSecret").
'</td>';
406 print
'<span class="opacitymedium">'.$langs->trans(
"SeePreviousTab").
'</span>';
413 print
'<tr class="oddeven">';
414 print
'<td>'.$langs->trans(
"Scopes").
'</td>';
415 print
'<td colspan="2">';
417 print $currentscopes;
420 print
'<tr class="oddeven">';
423 print $langs->trans(
"IsTokenGenerated");
426 if ($keyforprovider !=
'Login') {
427 if (is_object($tokenobj)) {
428 print $form->textwithpicto(
yn(1), $langs->trans(
"HasAccessToken").
' : '.
dol_print_date($storage->date_modification,
'dayhour').
' state='.
dol_escape_htmltag($storage->state));
430 print
'<span class="opacitymedium">'.$langs->trans(
"NoAccessToken").
'</span>';
433 print
'<span class="opacitymedium">'.$langs->trans(
"TokenNotRequiredForOAuthLogin").
'</span>';
436 print
'<td width="50%">';
437 if ($keyforprovider !=
'Login') {
439 if (is_object($tokenobj)) {
442 print
'<a class="button smallpaddingimp reposition" href="'.$urltodelete.
'">'.$langs->trans(
'DeleteAccess').
'</a><br>';
444 print
'<span class="opacitymedium">'.$langs->trans(
'GoOnTokenProviderToDeleteToken').
'</span><br>';
449 print
'<a class="button smallpaddingimp reposition" href="'.$urltorenew.
'">'.$langs->trans(
'GetAccess').
'</a>';
450 print $form->textwithpicto(
'', $langs->trans(
'RequestAccess'));
454 if ($urltorefresh && $refreshtoken) {
455 print
'<a class="button smallpaddingimp reposition classfortooltip marginright" href="'.$urltorefresh.
'" title="'.
dolPrintHTMLForAttribute($langs->trans(
'RefreshTokenHelp')).
'">'.$langs->trans(
'RefreshToken').
'</a>';
459 if ($urltocheckperms) {
460 print
'<br>'.$langs->trans(
"ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME).
': <a href="'.$urltocheckperms.
'" target="_'.strtolower($OAUTH_SERVICENAME).
'">'.$urltocheckperms.
'</a>';
466 print
'<tr class="oddeven">';
469 print $langs->trans(
"Token").
'</td>';
470 print
'<td colspan="2">';
472 if (is_object($tokenobj)) {
473 $tokentoshow = $tokenobj->getAccessToken();
485 if (is_object($tokenobj)) {
487 print
'<tr class="oddeven">';
490 print $langs->trans(
"TOKEN_REFRESH");
492 print
'<td colspan="2">';
498 print
'<tr class="oddeven">';
501 print $langs->trans(
"TOKEN_EXPIRED");
503 print
'<td colspan="2">';
509 print
'<tr class="oddeven">';
512 print $langs->trans(
"TOKEN_EXPIRE_AT");
514 print
'<td colspan="2">';
523 if (!empty($driver)) {
524 if ($submit_enabled) {
525 print $form->buttonsSaveCancel(
"Modify",
'');
535if ($mode ==
'test' && $user->admin) {
536 print $langs->trans(
'PrintTestDesc'.$driver).
"<br><br>\n";
538 print
'<div class="div-table-responsive-no-min">';
539 print
'<table class="noborder centpercent">';
540 if (!empty($driver)) {
541 require_once DOL_DOCUMENT_ROOT.
'/core/modules/printing/'.$driver.
'.modules.php';
542 $classname =
'printing_'.$driver;
543 $langs->load($driver);
544 $printer =
new $classname($db);
546 if (count($printer->getlistAvailablePrinters())) {
547 if ($printer->listAvailablePrinters() == 0) {
548 print $printer->resprint;
553 print $langs->trans(
'PleaseConfigureDriverfromList');
561if ($mode ==
'userconf' && $user->admin) {
562 print $langs->trans(
'PrintUserConfDesc'.$driver).
"<br><br>\n";
564 print
'<div class="div-table-responsive">';
565 print
'<table class="noborder centpercent">';
566 print
'<tr class="liste_titre">';
567 print
'<th>'.$langs->trans(
"User").
'</th>';
568 print
'<th>'.$langs->trans(
"PrintModule").
'</th>';
569 print
'<th>'.$langs->trans(
"PrintDriver").
'</th>';
570 print
'<th>'.$langs->trans(
"Printer").
'</th>';
571 print
'<th>'.$langs->trans(
"PrinterLocation").
'</th>';
572 print
'<th>'.$langs->trans(
"PrinterId").
'</th>';
573 print
'<th>'.$langs->trans(
"NumberOfCopy").
'</th>';
574 print
'<th class="center">'.$langs->trans(
"Delete").
'</th>';
576 $sql =
"SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login";
577 $sql .=
" FROM ".MAIN_DB_PREFIX.
"printing as p, ".MAIN_DB_PREFIX.
"user as u WHERE p.userid = u.rowid";
578 $resql = $db->query($sql);
579 while ($obj = $db->fetch_object($resql)) {
580 print
'<tr class="oddeven">';
581 print
'<td>'.$obj->login.
'</td>';
582 print
'<td>'.$obj->module.
'</td>';
583 print
'<td>'.$obj->driver.
'</td>';
584 print
'<td>'.$obj->printer_name.
'</td>';
585 print
'<td>'.$obj->printer_location.
'</td>';
586 print
'<td>'.$obj->printer_id.
'</td>';
587 print
'<td>'.$obj->copy.
'</td>';
588 print
'<td class="center">'.img_picto($langs->trans(
"Delete"),
'delete').
'</td>';
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).
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
showValueWithClipboardCPButton($valuetocopy, $showonlyonhover=1, $texttoshow='')
Create a button to copy $valuetocopy in the clipboard (for copy and paste feature).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
yn($yesno, $case=1, $color=0)
Return yes or no in current language.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dolPrintHTMLForAttribute($s)
Return a string ready to be output on an HTML attribute (alt, title, data-html, .....
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getSupportedOauth2Array()
Return array of tabs to used on pages to setup cron module.
oauthadmin_prepare_head()
Return array of tabs to used on pages to setup cron module.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.