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;
39$langs->loadLangs(array(
'admin',
'printing',
'oauth'));
41$action =
GETPOST(
'action',
'aZ09');
42$mode =
GETPOST(
'mode',
'alpha');
43$value =
GETPOST(
'value',
'alpha');
44$varname =
GETPOST(
'varname',
'alpha');
45$driver =
GETPOST(
'driver',
'alpha');
48 $langs->load($driver);
71if ($action ==
'setconst' && $user->admin) {
75 $setupconstarray =
GETPOST(
'setupdriver',
'array');
77 foreach ($setupconstarray as $setupconst) {
85 $result =
dolibarr_set_const($db, $constname, $constvalue, $consttype, 0, $constnote, $conf->entity);
101if ($action ==
'setvalue' && $user->admin) {
105 if (!($result > 0)) {
120if ($action ==
'refreshtoken' && $user->admin) {
121 $keyforprovider =
GETPOST(
'keyforprovider');
122 $OAUTH_SERVICENAME =
GETPOST(
'service');
127 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
129 $keyforsupportedoauth2array = $OAUTH_SERVICENAME;
130 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
131 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
133 $keyforprovider =
'';
135 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', strtoupper($keyforsupportedoauth2array));
136 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
138 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
141 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
144 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
146 dol_syslog(
"oauthlogintokens.php: Read token for service ".$OAUTH_SERVICENAME);
147 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
149 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
151 $refreshtoken = $tokenobj->getRefreshToken();
152 print
'<!-- data stored into field token: '.$storage->token.
' - expire '.((string) $expire).
' -->';
161 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
162 $keyforoauthservice = strtoupper($oauthname[0]).(empty($oauthname[1]) ?
'' :
'-'.$oauthname[1]);
163 $credentials =
new Credentials(
169 $serviceFactory = new \OAuth\ServiceFactory();
170 $httpClient = new \OAuth\Common\Http\Client\CurlClient();
174 $serviceFactory->setHttpClient($httpClient);
177 if (preg_match(
'/^Microsoft/', $OAUTH_SERVICENAME)) {
179 $tmp = explode(
'-', $OAUTH_SERVICENAME);
180 $scopes = explode(
',',
getDolGlobalString(
'OAUTH_'.strtoupper($tmp[0]).(empty($tmp[1]) ?
'' :
'-'.$tmp[1]).
'_SCOPE'));
184 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, $scopes);
186 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
188 dol_syslog(
"oauthlogintokens.php: call refreshAccessToken to get the new access token");
189 $tokenobj = $apiService->refreshAccessToken($tokenobj);
191 dol_syslog(
"oauthlogintokens.php: call setRefreshToken");
192 $tokenobj->setRefreshToken($refreshtoken);
194 dol_syslog(
"oauthlogintokens.php: call storeAccessToken to save the new access token + the old refresh token");
195 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
198 setEventMessages($langs->trans(
"OldTokenWasExpiredItHasBeenRefresh"),
null,
'mesgs');
200 setEventMessages($langs->trans(
"OldTokenWasNotExpiredButItHasBeenRefresh"),
null,
'mesgs');
203 dol_print_error($db,
'apiService is not a correct OAUTH2 Abstract service');
206 dol_syslog(
"oauthlogintokens.php: Read token again for service ".$OAUTH_SERVICENAME);
207 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
210 print $e->getMessage();
220$urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
221$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
224$form =
new Form($db);
226$title = $langs->trans(
"TokenManager");
227$help_url =
'EN:Module_OAuth|FR:Module_OAuth_FR|ES:Módulo_OAuth_ES';
229llxHeader(
'', $title, $help_url,
'', 0, 0,
'',
'',
'',
'mod-admin page-oauthlogintokens');
231$linkback =
'<a href="'.DOL_URL_ROOT.
'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToModuleList").
'</a>';
232print
load_fiche_titre($langs->trans(
'ConfigOAuth'), $linkback,
'title_setup');
242if ($mode ==
'setup' && $user->admin) {
243 print
'<span class="opacitymedium">'.$langs->trans(
"OAuthSetupForLogin").
"</span><br><br>\n";
246 $listinsetup = array();
247 foreach ($conf->global as $key => $val) {
248 if (!empty($val) && preg_match(
'/^OAUTH_.*_ID$/', $key)) {
249 $provider = preg_replace(
'/_ID$/',
'', $key);
250 $listinsetup[] = array(
260 $oauthstateanticsrf = bin2hex(random_bytes(128 / 8));
263 if (!empty($listinsetup)) {
264 foreach ($listinsetup as $key) {
266 $keyforsupportedoauth2array = $key[0];
267 $keyforsupportedoauth2array = preg_replace(
'/^OAUTH_/',
'', $keyforsupportedoauth2array);
268 $keyforsupportedoauth2array = preg_replace(
'/_NAME$/',
'', $keyforsupportedoauth2array);
269 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
270 $keybeforeprovider = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
271 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
273 $keybeforeprovider = $keyforsupportedoauth2array;
274 $keyforprovider =
'';
276 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', strtoupper($keyforsupportedoauth2array));
277 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
279 $nameofservice = ucfirst(strtolower(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']));
280 $nameofservice .= ($keyforprovider ?
'-'.$keyforprovider :
'');
281 $OAUTH_SERVICENAME = $nameofservice;
283 $keyforparamtenant =
'OAUTH_'.strtoupper(empty($supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'callbackfile']).($keyforprovider ?
'-'.$keyforprovider :
'').
'_TENANT';
289 $state = $shortscope;
291 $urltorefresh = $_SERVER[
"PHP_SELF"].
'?action=refreshtoken&token='.
newToken();
294 if ($keyforsupportedoauth2array ==
'OAUTH_GITHUB_NAME') {
299 $urltorenew = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($shortscope).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
300 $urltodelete = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
301 $urltocheckperms =
'https://github.com/settings/applications/';
302 } elseif ($keyforsupportedoauth2array ==
'OAUTH_GOOGLE_NAME') {
306 $urltorenew = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'-'.$oauthstateanticsrf.
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
307 $urltodelete = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
308 $urltocheckperms =
'https://security.google.com/settings/security/permissions';
309 } elseif (!empty($supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'])) {
310 $urltorenew = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
311 $urltodelete = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
312 $urltocheckperms =
'';
316 $urltocheckperms =
'';
320 $urltorenew .=
'&keyforprovider='.urlencode($keyforprovider);
323 $urltorefresh .=
'&keyforprovider='.urlencode($keyforprovider).
'&service='.urlencode($OAUTH_SERVICENAME);
326 $urltodelete .=
'&keyforprovider='.urlencode($keyforprovider);
332 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
334 $storage =
new DoliStorage($db, $conf, $keyforprovider,
getDolGlobalString($keyforparamtenant));
337 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
338 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
339 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 button-delete smallpaddingimp reposition marginright" href="'.$urltodelete.
'">'.$langs->trans(
'DeleteAccess').
'</a>';
444 print
'<span class="opacitymedium marginright">'.$langs->trans(
'GoOnTokenProviderToDeleteToken').
'</span>';
449 print
'<a class="button smallpaddingimp reposition classfortooltip marginright" href="'.$urltorenew.
'" title="'.
dolPrintHTMLForAttribute($langs->trans(
'RequestAccess')).
'">'.$langs->trans(
'GetAccess').
'</a>';
452 if ($urltorefresh && $refreshtoken) {
453 print
'<a class="button smallpaddingimp reposition classfortooltip marginright" href="'.$urltorefresh.
'" title="'.
dolPrintHTMLForAttribute($langs->trans(
'RefreshTokenHelp')).
'">'.$langs->trans(
'RefreshToken').
'</a>';
457 if ($urltocheckperms) {
458 print
'<br>'.$langs->trans(
"ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME).
': <a href="'.$urltocheckperms.
'" target="_'.strtolower($OAUTH_SERVICENAME).
'">'.$urltocheckperms.
'</a>';
464 if (is_object($tokenobj)) {
465 print
'<tr class="oddeven">';
468 print $langs->trans(
"TokenRawValue").
'</td>';
469 print
'<td colspan="2">';
470 if (is_object($tokenobj)) {
471 print
'<textarea class="quatrevingtpercent small" rows="'.ROWS_4.
'">'.var_export($tokenobj,
true).
'</textarea><br>'.
"\n";
476 print
'<tr class="oddeven">';
479 print $langs->trans(
"AccessToken").
'</td>';
480 print
'<td colspan="2">';
481 $tokentoshow = $tokenobj->getAccessToken();
490 print
'<span class="opacitymedium"> - ';
491 print $langs->trans(
"ExpirationDate").
': ';
495 print $expire ?
' ('.$langs->trans(
"TokenExpired").
')' :
' ('.$langs->trans(
"TokenNotExpired").
')';
501 print
'<tr class="oddeven">';
504 print $langs->trans(
"TOKEN_REFRESH");
506 print
'<td colspan="2">';
515 if (!empty($driver)) {
516 if ($submit_enabled) {
517 print $form->buttonsSaveCancel(
"Modify",
'');
527if ($mode ==
'test' && $user->admin) {
528 print $langs->trans(
'PrintTestDesc'.$driver).
"<br><br>\n";
530 print
'<div class="div-table-responsive-no-min">';
531 print
'<table class="noborder centpercent">';
532 if (!empty($driver)) {
533 require_once DOL_DOCUMENT_ROOT.
'/core/modules/printing/'.$driver.
'.modules.php';
534 $classname =
'printing_'.$driver;
535 $langs->load($driver);
536 $printer =
new $classname($db);
538 '@phan-var-force PrintingDriver $printer';
541 if (count($printer->getlistAvailablePrinters())) {
542 if ($printer->listAvailablePrinters() == 0) {
543 print $printer->resprint;
548 print $langs->trans(
'PleaseConfigureDriverfromList');
556if ($mode ==
'userconf' && $user->admin) {
557 print $langs->trans(
'PrintUserConfDesc'.$driver).
"<br><br>\n";
559 print
'<div class="div-table-responsive">';
560 print
'<table class="noborder centpercent">';
561 print
'<tr class="liste_titre">';
562 print
'<th>'.$langs->trans(
"User").
'</th>';
563 print
'<th>'.$langs->trans(
"PrintModule").
'</th>';
564 print
'<th>'.$langs->trans(
"PrintDriver").
'</th>';
565 print
'<th>'.$langs->trans(
"Printer").
'</th>';
566 print
'<th>'.$langs->trans(
"PrinterLocation").
'</th>';
567 print
'<th>'.$langs->trans(
"PrinterId").
'</th>';
568 print
'<th>'.$langs->trans(
"NumberOfCopy").
'</th>';
569 print
'<th class="center">'.$langs->trans(
"Delete").
'</th>';
571 $sql =
"SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login";
572 $sql .=
" FROM ".MAIN_DB_PREFIX.
"printing as p, ".MAIN_DB_PREFIX.
"user as u WHERE p.userid = u.rowid";
573 $resql = $db->query($sql);
574 while ($obj = $db->fetch_object($resql)) {
575 print
'<tr class="oddeven">';
576 print
'<td>'.$obj->login.
'</td>';
577 print
'<td>'.$obj->module.
'</td>';
578 print
'<td>'.$obj->driver.
'</td>';
579 print
'<td>'.$obj->printer_name.
'</td>';
580 print
'<td>'.$obj->printer_location.
'</td>';
581 print
'<td>'.$obj->printer_id.
'</td>';
582 print
'<td>'.$obj->copy.
'</td>';
583 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...
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.