28require
'../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
30require_once DOL_DOCUMENT_ROOT.
'/core/lib/oauth.lib.php';
31require_once DOL_DOCUMENT_ROOT.
'/core/class/doleditor.class.php';
33use OAuth\Common\Storage\DoliStorage;
34use OAuth\Common\Consumer\Credentials;
49$langs->loadLangs(array(
'admin',
'printing',
'oauth'));
51$action =
GETPOST(
'action',
'aZ09');
52$mode =
GETPOST(
'mode',
'alpha');
53$value =
GETPOST(
'value',
'alpha');
54$varname =
GETPOST(
'varname',
'alpha');
55$driver =
GETPOST(
'driver',
'alpha');
58 $langs->load($driver);
81if ($action ==
'setconst' && $user->admin) {
84 $setupconstarray =
GETPOST(
'setupdriver',
'array');
86 foreach ($setupconstarray as $setupconst) {
108if ($action ==
'setvalue' && $user->admin) {
112 if (!($result > 0)) {
127if ($action ==
'refreshtoken' && $user->admin) {
128 $keyforprovider =
GETPOST(
'keyforprovider');
129 $OAUTH_SERVICENAME =
GETPOST(
'service');
134 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
136 $keyforsupportedoauth2array = $OAUTH_SERVICENAME;
137 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
138 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
140 $keyforprovider =
'';
142 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', strtoupper($keyforsupportedoauth2array));
143 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
145 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
151 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
153 dol_syslog(
"oauthlogintokens.php: Read token for service ".$OAUTH_SERVICENAME);
154 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
156 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
158 $refreshtoken = $tokenobj->getRefreshToken();
159 print
'<!-- data stored into field token: '.$storage->token.
' - expire '.((string) $expire).
' -->';
168 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
169 $keyforoauthservice = strtoupper($oauthname[0]).(empty($oauthname[1]) ?
'' :
'-'.$oauthname[1]);
170 $credentials =
new Credentials(
176 $serviceFactory = new \OAuth\ServiceFactory();
177 $httpClient = new \OAuth\Common\Http\Client\CurlClient();
181 $serviceFactory->setHttpClient($httpClient);
184 if (preg_match(
'/^Microsoft/', $OAUTH_SERVICENAME)) {
186 $tmp = explode(
'-', $OAUTH_SERVICENAME);
187 $scopes = explode(
',',
getDolGlobalString(
'OAUTH_'.strtoupper($tmp[0]).(empty($tmp[1]) ?
'' :
'-'.$tmp[1]).
'_SCOPE'));
191 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, $scopes);
193 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
195 dol_syslog(
"oauthlogintokens.php: call refreshAccessToken to get the new access token");
196 $tokenobj = $apiService->refreshAccessToken($tokenobj);
198 dol_syslog(
"oauthlogintokens.php: call setRefreshToken");
199 $tokenobj->setRefreshToken($refreshtoken);
201 dol_syslog(
"oauthlogintokens.php: call storeAccessToken to save the new access token + the old refresh token");
202 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
205 setEventMessages($langs->trans(
"OldTokenWasExpiredItHasBeenRefresh"),
null,
'mesgs');
207 setEventMessages($langs->trans(
"OldTokenWasNotExpiredButItHasBeenRefresh"),
null,
'mesgs');
210 dol_print_error($db,
'apiService is not a correct OAUTH2 Abstract service');
213 dol_syslog(
"oauthlogintokens.php: Read token again for service ".$OAUTH_SERVICENAME);
214 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
217 print $e->getMessage();
227$urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
228$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
231$form =
new Form($db);
233$title = $langs->trans(
"TokenManager");
234$help_url =
'EN:Module_OAuth|FR:Module_OAuth_FR|ES:Módulo_OAuth_ES';
236llxHeader(
'', $title, $help_url,
'', 0, 0,
'',
'',
'',
'mod-admin page-oauthlogintokens');
238$linkback =
'<a href="'.DOL_URL_ROOT.
'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToModuleList").
'</a>';
239print
load_fiche_titre($langs->trans(
'ConfigOAuth'), $linkback,
'title_setup');
249if ($mode ==
'setup' && $user->admin) {
250 print
'<span class="opacitymedium">'.$langs->trans(
"OAuthSetupForLogin").
"</span><br><br>\n";
253 $listinsetup = array();
254 foreach (
$conf->global as $key => $val) {
255 if (!empty($val) && preg_match(
'/^OAUTH_.*_ID$/', $key)) {
256 $provider = preg_replace(
'/_ID$/',
'', $key);
257 $listinsetup[] = array(
267 $oauthstateanticsrf = bin2hex(random_bytes(128 / 8));
270 if (!empty($listinsetup)) {
271 foreach ($listinsetup as $key) {
273 $keyforsupportedoauth2array = $key[0];
274 $keyforsupportedoauth2array = preg_replace(
'/^OAUTH_/',
'', $keyforsupportedoauth2array);
275 $keyforsupportedoauth2array = preg_replace(
'/_NAME$/',
'', $keyforsupportedoauth2array);
276 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
277 $keybeforeprovider = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
278 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
280 $keybeforeprovider = $keyforsupportedoauth2array;
281 $keyforprovider =
'';
283 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', strtoupper($keyforsupportedoauth2array));
284 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
286 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
287 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
288 $OAUTH_SERVICENAME = $nameofservice;
290 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
296 $state = $shortscope;
298 $urltorefresh = $_SERVER[
"PHP_SELF"].
'?action=refreshtoken&token='.
newToken();
301 if ($keyforsupportedoauth2array ==
'OAUTH_GITHUB_NAME') {
306 $urltorenew = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($shortscope).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
307 $urltodelete = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
308 $urltocheckperms =
'https://github.com/settings/applications/';
309 } elseif ($keyforsupportedoauth2array ==
'OAUTH_GOOGLE_NAME') {
313 $urltorenew = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'-'.$oauthstateanticsrf.
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
314 $urltodelete = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
315 $urltocheckperms =
'https://security.google.com/settings/security/permissions';
316 } elseif (!empty($supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'])) {
317 $urltorenew = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
318 $urltodelete = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
319 $urltocheckperms =
'';
323 $urltocheckperms =
'';
327 $urltorenew .=
'&keyforprovider='.urlencode($keyforprovider);
330 $urltorefresh .=
'&keyforprovider='.urlencode($keyforprovider).
'&service='.urlencode($OAUTH_SERVICENAME);
333 $urltodelete .=
'&keyforprovider='.urlencode($keyforprovider);
339 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
344 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
345 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
346 print
'<!-- data stored into field token: '.$storage->token.
' -->';
355 $refreshtoken =
false;
360 if (is_object($tokenobj)) {
361 $expire = ($tokenobj->getEndOfLife() !== $tokenobj::EOL_NEVER_EXPIRES && $tokenobj->getEndOfLife() !== $tokenobj::EOL_UNKNOWN && time() > ($tokenobj->getEndOfLife() - 30));
363 if ($key[1] !=
'' && $key[2] !=
'') {
364 if (is_object($tokenobj)) {
365 $refreshtoken = $tokenobj->getRefreshToken();
367 $endoflife = $tokenobj->getEndOfLife();
368 if ($endoflife == $tokenobj::EOL_NEVER_EXPIRES) {
369 $expiredat = $langs->trans(
"Never");
370 } elseif ($endoflife == $tokenobj::EOL_UNKNOWN) {
371 $expiredat = $langs->trans(
"Unknown");
380 print
'<form method="post" action="'.$_SERVER[
"PHP_SELF"].
'?mode=setup&driver='.$driver.
'" autocomplete="off">';
381 print
'<input type="hidden" name="token" value="'.newToken().
'">';
382 print
'<input type="hidden" name="action" value="setconst">';
383 print
'<input type="hidden" name="page_y" value="">';
385 print
'<div class="div-table-responsive-no-min">';
386 print
'<table class="noborder centpercent">'.
"\n";
389 $label = $langs->trans($keyforsupportedoauth2array);
390 print
'<tr class="liste_titre">';
391 print
'<th class="titlefieldcreate">';
392 print
img_picto(
'', $supportedoauth2array[$keyforsupportedoauth2array][
'picto'],
'class="pictofixedwidth"');
393 if ($label == $keyforsupportedoauth2array) {
394 print $supportedoauth2array[$keyforsupportedoauth2array][
'name'];
398 if ($keyforprovider) {
399 print
' (<b>'.$keyforprovider.
'</b>)';
401 print
' (<b>'.$langs->trans(
"NoName").
'</b>)';
408 print
'<tr class="oddeven">';
411 print $langs->trans(
"OAuthIDSecret").
'</td>';
413 print
'<span class="opacitymedium">'.$langs->trans(
"SeePreviousTab").
'</span>';
420 print
'<tr class="oddeven">';
421 print
'<td>'.$langs->trans(
"Scopes").
'</td>';
422 print
'<td colspan="2">';
424 print $currentscopes;
427 print
'<tr class="oddeven">';
430 print $langs->trans(
"IsTokenGenerated");
433 if ($keyforprovider !=
'Login') {
434 if (is_object($tokenobj)) {
435 print $form->textwithpicto(
yn(1), $langs->trans(
"HasAccessToken").
' : '.
dol_print_date($storage->date_modification,
'dayhour').
' state='.
dol_escape_htmltag($storage->state));
437 print
'<span class="opacitymedium">'.$langs->trans(
"NoAccessToken").
'</span>';
440 print
'<span class="opacitymedium">'.$langs->trans(
"TokenNotRequiredForOAuthLogin").
'</span>';
443 print
'<td width="50%">';
444 if ($keyforprovider !=
'Login') {
446 if (is_object($tokenobj)) {
449 print
'<a class="button button-delete smallpaddingimp reposition marginright" href="'.$urltodelete.
'">'.$langs->trans(
'DeleteAccess').
'</a>';
451 print
'<span class="opacitymedium marginright">'.$langs->trans(
'GoOnTokenProviderToDeleteToken').
'</span>';
456 print
'<a class="button smallpaddingimp reposition classfortooltip marginright" href="'.$urltorenew.
'" title="'.
dolPrintHTMLForAttribute($langs->trans(
'RequestAccess')).
'">'.$langs->trans(
'GetAccess').
'</a>';
459 if ($urltorefresh && $refreshtoken) {
460 print
'<a class="button smallpaddingimp reposition classfortooltip marginright" href="'.$urltorefresh.
'" title="'.
dolPrintHTMLForAttribute($langs->trans(
'RefreshTokenHelp')).
'">'.$langs->trans(
'RefreshToken').
'</a>';
464 if ($urltocheckperms) {
465 print
'<br>'.$langs->trans(
"ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME).
': <a href="'.$urltocheckperms.
'" target="_'.strtolower($OAUTH_SERVICENAME).
'">'.$urltocheckperms.
'</a>';
471 if (is_object($tokenobj)) {
472 print
'<tr class="oddeven">';
475 print $langs->trans(
"TokenRawValue").
'</td>';
476 print
'<td colspan="2">';
477 if (is_object($tokenobj)) {
478 print
'<textarea class="quatrevingtpercent small" rows="'.ROWS_4.
'">'.var_export($tokenobj,
true).
'</textarea><br>'.
"\n";
483 print
'<tr class="oddeven">';
486 print $langs->trans(
"AccessToken").
'</td>';
487 print
'<td colspan="2">';
488 $tokentoshow = $tokenobj->getAccessToken();
497 print
'<span class="opacitymedium"> - ';
498 print $langs->trans(
"ExpirationDate").
': ';
502 print $expire ?
' ('.$langs->trans(
"TokenExpired").
')' :
' ('.$langs->trans(
"TokenNotExpired").
')';
508 print
'<tr class="oddeven">';
511 print $langs->trans(
"TOKEN_REFRESH");
513 print
'<td colspan="2">';
522 if (!empty($driver)) {
523 if ($submit_enabled) {
524 print $form->buttonsSaveCancel(
"Modify",
'');
534if ($mode ==
'test' && $user->admin) {
535 print $langs->trans(
'PrintTestDesc'.$driver).
"<br><br>\n";
537 print
'<div class="div-table-responsive-no-min">';
538 print
'<table class="noborder centpercent">';
539 if (!empty($driver)) {
540 require_once DOL_DOCUMENT_ROOT.
'/core/modules/printing/'.$driver.
'.modules.php';
541 $classname =
'printing_'.$driver;
542 $langs->load($driver);
543 $printer =
new $classname($db);
545 '@phan-var-force PrintingDriver $printer';
548 if (count($printer->getlistAvailablePrinters())) {
549 if ($printer->listAvailablePrinters() == 0) {
550 print $printer->resprint;
555 print $langs->trans(
'PleaseConfigureDriverfromList');
563if ($mode ==
'userconf' && $user->admin) {
564 print $langs->trans(
'PrintUserConfDesc'.$driver).
"<br><br>\n";
566 print
'<div class="div-table-responsive">';
567 print
'<table class="noborder centpercent">';
568 print
'<tr class="liste_titre">';
569 print
'<th>'.$langs->trans(
"User").
'</th>';
570 print
'<th>'.$langs->trans(
"PrintModule").
'</th>';
571 print
'<th>'.$langs->trans(
"PrintDriver").
'</th>';
572 print
'<th>'.$langs->trans(
"Printer").
'</th>';
573 print
'<th>'.$langs->trans(
"PrinterLocation").
'</th>';
574 print
'<th>'.$langs->trans(
"PrinterId").
'</th>';
575 print
'<th>'.$langs->trans(
"NumberOfCopy").
'</th>';
576 print
'<th class="center">'.$langs->trans(
"Delete").
'</th>';
578 $sql =
"SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login";
579 $sql .=
" FROM ".MAIN_DB_PREFIX.
"printing as p, ".MAIN_DB_PREFIX.
"user as u WHERE p.userid = u.rowid";
580 $resql = $db->query($sql);
581 while ($obj = $db->fetch_object($resql)) {
582 print
'<tr class="oddeven">';
583 print
'<td>'.$obj->login.
'</td>';
584 print
'<td>'.$obj->module.
'</td>';
585 print
'<td>'.$obj->driver.
'</td>';
586 print
'<td>'.$obj->printer_name.
'</td>';
587 print
'<td>'.$obj->printer_location.
'</td>';
588 print
'<td>'.$obj->printer_id.
'</td>';
589 print
'<td>'.$obj->copy.
'</td>';
590 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($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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)
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'.
yn($yesno, $format=1, $color=0)
Return yes or no in current language.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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 a 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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
getSupportedOauth2Array()
Return array of tabs to use 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.