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 $storage =
new DoliStorage($db, $conf, $keyforprovider);
132 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
134 dol_syslog(
"oauthlogintokens.php: Read token for service ".$OAUTH_SERVICENAME);
135 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
137 $expire = ($tokenobj->getEndOfLife() !== -9002 && $tokenobj->getEndOfLife() !== -9001 && time() > ($tokenobj->getEndOfLife() - 30));
139 $refreshtoken = $tokenobj->getRefreshToken();
140 print
'<!-- data stored into field token: '.$storage->token.
' - expire '.((string) $expire).
' -->';
149 $oauthname = explode(
'-', $OAUTH_SERVICENAME);
150 $keyforoauthservice = strtoupper($oauthname[0]).(empty($oauthname[1]) ?
'' :
'-'.$oauthname[1]);
151 $credentials =
new Credentials(
157 $serviceFactory = new \OAuth\ServiceFactory();
158 $httpClient = new \OAuth\Common\Http\Client\CurlClient();
162 $serviceFactory->setHttpClient($httpClient);
165 $apiService = $serviceFactory->createService($oauthname[0], $credentials, $storage, array());
167 if ($apiService instanceof OAuth\OAuth2\Service\AbstractService || $apiService instanceof OAuth\OAuth1\Service\AbstractService) {
169 dol_syslog(
"oauthlogintokens.php: call refreshAccessToken to get the new access token");
170 $tokenobj = $apiService->refreshAccessToken($tokenobj);
172 dol_syslog(
"oauthlogintokens.php: call setRefreshToken");
173 $tokenobj->setRefreshToken($refreshtoken);
175 dol_syslog(
"oauthlogintokens.php: call storeAccessToken to save the new access token + the old refresh token");
176 $storage->storeAccessToken($OAUTH_SERVICENAME, $tokenobj);
179 setEventMessages($langs->trans(
"OldTokenWasExpiredItHasBeenRefresh"),
null,
'mesgs');
181 setEventMessages($langs->trans(
"OldTokenWasNotExpiredButItHasBeenRefresh"),
null,
'mesgs');
184 dol_print_error($db,
'apiService is not a correct OAUTH2 Abstract service');
187 dol_syslog(
"oauthlogintokens.php: Read token again for service ".$OAUTH_SERVICENAME);
188 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
191 print $e->getMessage();
201$urlwithouturlroot = preg_replace(
'/'.preg_quote(DOL_URL_ROOT,
'/').
'$/i',
'', trim($dolibarr_main_url_root));
202$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT;
205$form =
new Form($db);
207$title = $langs->trans(
"TokenManager");
208$help_url =
'EN:Module_OAuth|FR:Module_OAuth_FR|ES:Módulo_OAuth_ES';
210llxHeader(
'', $title, $help_url,
'', 0, 0,
'',
'',
'',
'mod-admin page-oauthlogintokens');
212$linkback =
'<a href="'.DOL_URL_ROOT.
'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToModuleList").
'</a>';
213print
load_fiche_titre($langs->trans(
'ConfigOAuth'), $linkback,
'title_setup');
223if ($mode ==
'setup' && $user->admin) {
224 print
'<span class="opacitymedium">'.$langs->trans(
"OAuthSetupForLogin").
"</span><br><br>\n";
227 $listinsetup = array();
228 foreach ($conf->global as $key => $val) {
229 if (!empty($val) && preg_match(
'/^OAUTH_.*_ID$/', $key)) {
230 $provider = preg_replace(
'/_ID$/',
'', $key);
231 $listinsetup[] = array(
241 $oauthstateanticsrf = bin2hex(random_bytes(128 / 8));
244 if (!empty($listinsetup)) {
245 foreach ($listinsetup as $key) {
247 $keyforsupportedoauth2array = $key[0];
248 $keyforsupportedoauth2array = preg_replace(
'/^OAUTH_/',
'', $keyforsupportedoauth2array);
249 $keyforsupportedoauth2array = preg_replace(
'/_NAME$/',
'', $keyforsupportedoauth2array);
250 if (preg_match(
'/^.*-/', $keyforsupportedoauth2array)) {
251 $keybeforeprovider = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
252 $keyforprovider = preg_replace(
'/^.*-/',
'', $keyforsupportedoauth2array);
254 $keybeforeprovider = $keyforsupportedoauth2array;
255 $keyforprovider =
'';
257 $keyforsupportedoauth2array = preg_replace(
'/-.*$/',
'', $keyforsupportedoauth2array);
258 $keyforsupportedoauth2array =
'OAUTH_'.$keyforsupportedoauth2array.
'_NAME';
261 $OAUTH_SERVICENAME = (empty($supportedoauth2array[$keyforsupportedoauth2array][
'name']) ?
'Unknown' : $supportedoauth2array[$keyforsupportedoauth2array][
'name'].($keyforprovider ?
'-'.$keyforprovider :
''));
267 $state = $shortscope;
269 $urltorefresh = $_SERVER[
"PHP_SELF"].
'?action=refreshtoken&token='.
newToken();
272 if ($keyforsupportedoauth2array ==
'OAUTH_GITHUB_NAME') {
277 $urltorenew = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($shortscope).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
278 $urltodelete = $urlwithroot.
'/core/modules/oauth/github_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
279 $urltocheckperms =
'https://github.com/settings/applications/';
280 } elseif ($keyforsupportedoauth2array ==
'OAUTH_GOOGLE_NAME') {
284 $urltorenew = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'-'.$oauthstateanticsrf.
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
285 $urltodelete = $urlwithroot.
'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
286 $urltocheckperms =
'https://security.google.com/settings/security/permissions';
287 } elseif (!empty($supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'])) {
288 $urltorenew = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?shortscope='.urlencode($shortscope).
'&state='.urlencode($state).
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
289 $urltodelete = $urlwithroot.$supportedoauth2array[$keyforsupportedoauth2array][
'returnurl'].
'?action=delete&token='.
newToken().
'&backtourl='.urlencode(DOL_URL_ROOT.
'/admin/oauthlogintokens.php');
290 $urltocheckperms =
'';
294 $urltocheckperms =
'';
298 $urltorenew .=
'&keyforprovider='.urlencode($keyforprovider);
301 $urltorefresh .=
'&keyforprovider='.urlencode($keyforprovider).
'&service='.urlencode($OAUTH_SERVICENAME);
304 $urltodelete .=
'&keyforprovider='.urlencode($keyforprovider);
310 require_once DOL_DOCUMENT_ROOT.
'/includes/OAuth/bootstrap.php';
312 $storage =
new DoliStorage($db, $conf, $keyforprovider);
315 print
'<!-- '.$OAUTH_SERVICENAME.
' -->'.
"\n";
316 $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
317 print
'<!-- data stored into field token: '.$storage->token.
' -->';
326 $refreshtoken =
false;
331 if (is_object($tokenobj)) {
332 $expire = ($tokenobj->getEndOfLife() !== $tokenobj::EOL_NEVER_EXPIRES && $tokenobj->getEndOfLife() !== $tokenobj::EOL_UNKNOWN && time() > ($tokenobj->getEndOfLife() - 30));
334 if ($key[1] !=
'' && $key[2] !=
'') {
335 if (is_object($tokenobj)) {
336 $refreshtoken = $tokenobj->getRefreshToken();
338 $endoflife = $tokenobj->getEndOfLife();
339 if ($endoflife == $tokenobj::EOL_NEVER_EXPIRES) {
340 $expiredat = $langs->trans(
"Never");
341 } elseif ($endoflife == $tokenobj::EOL_UNKNOWN) {
342 $expiredat = $langs->trans(
"Unknown");
351 print
'<form method="post" action="'.$_SERVER[
"PHP_SELF"].
'?mode=setup&driver='.$driver.
'" autocomplete="off">';
352 print
'<input type="hidden" name="token" value="'.newToken().
'">';
353 print
'<input type="hidden" name="action" value="setconst">';
354 print
'<input type="hidden" name="page_y" value="">';
356 print
'<div class="div-table-responsive-no-min">';
357 print
'<table class="noborder centpercent">'.
"\n";
360 $label = $langs->trans($keyforsupportedoauth2array);
361 print
'<tr class="liste_titre">';
362 print
'<th class="titlefieldcreate">';
363 print
img_picto(
'', $supportedoauth2array[$keyforsupportedoauth2array][
'picto'],
'class="pictofixedwidth"');
364 if ($label == $keyforsupportedoauth2array) {
365 print $supportedoauth2array[$keyforsupportedoauth2array][
'name'];
369 if ($keyforprovider) {
370 print
' (<b>'.$keyforprovider.
'</b>)';
372 print
' (<b>'.$langs->trans(
"NoName").
'</b>)';
379 print
'<tr class="oddeven">';
382 print $langs->trans(
"OAuthIDSecret").
'</td>';
384 print
'<span class="opacitymedium">'.$langs->trans(
"SeePreviousTab").
'</span>';
391 print
'<tr class="oddeven">';
392 print
'<td>'.$langs->trans(
"Scopes").
'</td>';
393 print
'<td colspan="2">';
395 print $currentscopes;
398 print
'<tr class="oddeven">';
401 print $langs->trans(
"IsTokenGenerated");
404 if ($keyforprovider !=
'Login') {
405 if (is_object($tokenobj)) {
406 print $form->textwithpicto(
yn(1), $langs->trans(
"HasAccessToken").
' : '.
dol_print_date($storage->date_modification,
'dayhour').
' state='.
dol_escape_htmltag($storage->state));
408 print
'<span class="opacitymedium">'.$langs->trans(
"NoAccessToken").
'</span>';
411 print
'<span class="opacitymedium">'.$langs->trans(
"TokenNotRequiredForOAuthLogin").
'</span>';
414 print
'<td width="50%">';
415 if ($keyforprovider !=
'Login') {
417 if (is_object($tokenobj)) {
420 print
'<a class="button button-delete smallpaddingimp reposition marginright" href="'.$urltodelete.
'">'.$langs->trans(
'DeleteAccess').
'</a>';
422 print
'<span class="opacitymedium marginright">'.$langs->trans(
'GoOnTokenProviderToDeleteToken').
'</span>';
427 print
'<a class="button smallpaddingimp reposition classfortooltip marginright" href="'.$urltorenew.
'" title="'.
dolPrintHTMLForAttribute($langs->trans(
'RequestAccess')).
'">'.$langs->trans(
'GetAccess').
'</a>';
430 if ($urltorefresh && $refreshtoken) {
431 print
'<a class="button smallpaddingimp reposition classfortooltip marginright" href="'.$urltorefresh.
'" title="'.
dolPrintHTMLForAttribute($langs->trans(
'RefreshTokenHelp')).
'">'.$langs->trans(
'RefreshToken').
'</a>';
435 if ($urltocheckperms) {
436 print
'<br>'.$langs->trans(
"ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME).
': <a href="'.$urltocheckperms.
'" target="_'.strtolower($OAUTH_SERVICENAME).
'">'.$urltocheckperms.
'</a>';
442 if (is_object($tokenobj)) {
443 print
'<tr class="oddeven">';
446 print $langs->trans(
"TokenRawValue").
'</td>';
447 print
'<td colspan="2">';
448 if (is_object($tokenobj)) {
449 print
'<textarea class="quatrevingtpercent small" rows="'.ROWS_4.
'">'.var_export($tokenobj,
true).
'</textarea><br>'.
"\n";
454 print
'<tr class="oddeven">';
457 print $langs->trans(
"AccessToken").
'</td>';
458 print
'<td colspan="2">';
459 $tokentoshow = $tokenobj->getAccessToken();
468 print
'<span class="opacitymedium"> - ';
469 print $langs->trans(
"ExpirationDate").
': ';
473 print $expire ?
' ('.$langs->trans(
"TokenExpired").
')' :
' ('.$langs->trans(
"TokenNotExpired").
')';
479 print
'<tr class="oddeven">';
482 print $langs->trans(
"TOKEN_REFRESH");
484 print
'<td colspan="2">';
493 if (!empty($driver)) {
494 if ($submit_enabled) {
495 print $form->buttonsSaveCancel(
"Modify",
'');
505if ($mode ==
'test' && $user->admin) {
506 print $langs->trans(
'PrintTestDesc'.$driver).
"<br><br>\n";
508 print
'<div class="div-table-responsive-no-min">';
509 print
'<table class="noborder centpercent">';
510 if (!empty($driver)) {
511 require_once DOL_DOCUMENT_ROOT.
'/core/modules/printing/'.$driver.
'.modules.php';
512 $classname =
'printing_'.$driver;
513 $langs->load($driver);
514 $printer =
new $classname($db);
516 '@phan-var-force PrintingDriver $printer';
519 if (count($printer->getlistAvailablePrinters())) {
520 if ($printer->listAvailablePrinters() == 0) {
521 print $printer->resprint;
526 print $langs->trans(
'PleaseConfigureDriverfromList');
534if ($mode ==
'userconf' && $user->admin) {
535 print $langs->trans(
'PrintUserConfDesc'.$driver).
"<br><br>\n";
537 print
'<div class="div-table-responsive">';
538 print
'<table class="noborder centpercent">';
539 print
'<tr class="liste_titre">';
540 print
'<th>'.$langs->trans(
"User").
'</th>';
541 print
'<th>'.$langs->trans(
"PrintModule").
'</th>';
542 print
'<th>'.$langs->trans(
"PrintDriver").
'</th>';
543 print
'<th>'.$langs->trans(
"Printer").
'</th>';
544 print
'<th>'.$langs->trans(
"PrinterLocation").
'</th>';
545 print
'<th>'.$langs->trans(
"PrinterId").
'</th>';
546 print
'<th>'.$langs->trans(
"NumberOfCopy").
'</th>';
547 print
'<th class="center">'.$langs->trans(
"Delete").
'</th>';
549 $sql =
"SELECT p.rowid, p.printer_name, p.printer_location, p.printer_id, p.copy, p.module, p.driver, p.userid, u.login";
550 $sql .=
" FROM ".MAIN_DB_PREFIX.
"printing as p, ".MAIN_DB_PREFIX.
"user as u WHERE p.userid = u.rowid";
551 $resql = $db->query($sql);
552 while ($obj = $db->fetch_object($resql)) {
553 print
'<tr class="oddeven">';
554 print
'<td>'.$obj->login.
'</td>';
555 print
'<td>'.$obj->module.
'</td>';
556 print
'<td>'.$obj->driver.
'</td>';
557 print
'<td>'.$obj->printer_name.
'</td>';
558 print
'<td>'.$obj->printer_location.
'</td>';
559 print
'<td>'.$obj->printer_id.
'</td>';
560 print
'<td>'.$obj->copy.
'</td>';
561 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)
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.
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.