PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/admin/oauthlogintokens.php

http://github.com/Dolibarr/dolibarr
PHP | 409 lines | 279 code | 59 blank | 71 comment | 62 complexity | 910cf9f52a7ab9cd2811273e5bdc1873 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT
  1. <?php
  2. /* Copyright (C) 2013-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2014-2018 Frederic France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2020 Nicolas ZABOURI <info@inovea-conseil.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/admin/oauthlogintokens.php
  21. * \ingroup oauth
  22. * \brief Setup page to configure oauth access to login information
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php'; // This define $list
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  28. use OAuth\Common\Storage\DoliStorage;
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('admin', 'printing', 'oauth'));
  31. if (!$user->admin) {
  32. accessforbidden();
  33. }
  34. $action = GETPOST('action', 'aZ09');
  35. $mode = GETPOST('mode', 'alpha');
  36. $value = GETPOST('value', 'alpha');
  37. $varname = GETPOST('varname', 'alpha');
  38. $driver = GETPOST('driver', 'alpha');
  39. if (!empty($driver)) {
  40. $langs->load($driver);
  41. }
  42. if (!$mode) {
  43. $mode = 'setup';
  44. }
  45. /*
  46. * Action
  47. */
  48. /*if (($mode == 'test' || $mode == 'setup') && empty($driver))
  49. {
  50. setEventMessages($langs->trans('PleaseSelectaDriverfromList'), null);
  51. header("Location: ".$_SERVER['PHP_SELF'].'?mode=config');
  52. exit;
  53. }*/
  54. if ($action == 'setconst' && $user->admin) {
  55. $error = 0;
  56. $db->begin();
  57. $setupconstarray = GETPOST('setupdriver', 'array');
  58. foreach ($setupconstarray as $setupconst) {
  59. //print '<pre>'.print_r($setupconst, true).'</pre>';
  60. $constname = dol_escape_htmltag($setupconst['varname']);
  61. $constvalue = dol_escape_htmltag($setupconst['value']);
  62. $consttype = dol_escape_htmltag($setupconst['type']);
  63. $constnote = dol_escape_htmltag($setupconst['note']);
  64. $result = dolibarr_set_const($db, $constname, $constvalue, $consttype, 0, $constnote, $conf->entity);
  65. if (!$result > 0) {
  66. $error++;
  67. }
  68. }
  69. if (!$error) {
  70. $db->commit();
  71. setEventMessages($langs->trans("SetupSaved"), null);
  72. } else {
  73. $db->rollback();
  74. dol_print_error($db);
  75. }
  76. $action = '';
  77. }
  78. if ($action == 'setvalue' && $user->admin) {
  79. $db->begin();
  80. $result = dolibarr_set_const($db, $varname, $value, 'chaine', 0, '', $conf->entity);
  81. if (!$result > 0) {
  82. $error++;
  83. }
  84. if (!$error) {
  85. $db->commit();
  86. setEventMessages($langs->trans("SetupSaved"), null);
  87. } else {
  88. $db->rollback();
  89. dol_print_error($db);
  90. }
  91. $action = '';
  92. }
  93. /*
  94. * View
  95. */
  96. // Define $urlwithroot
  97. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  98. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  99. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  100. $form = new Form($db);
  101. llxHeader('', $langs->trans("PrintingSetup"));
  102. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  103. print load_fiche_titre($langs->trans('ConfigOAuth'), $linkback, 'title_setup');
  104. $head = oauthadmin_prepare_head();
  105. print dol_get_fiche_head($head, 'tokengeneration', '', -1, 'technic');
  106. if (GETPOST('error')) {
  107. setEventMessages(GETPOST('error'), null, 'errors');
  108. }
  109. if ($mode == 'setup' && $user->admin) {
  110. print '<span class="opacitymedium">'.$langs->trans("OAuthSetupForLogin")."</span><br><br>\n";
  111. foreach ($list as $key) {
  112. $supported = 0;
  113. if (in_array($key[0], array_keys($supportedoauth2array))) {
  114. $supported = 1;
  115. }
  116. if (!$supported) {
  117. continue; // show only supported
  118. }
  119. $OAUTH_SERVICENAME = 'Unknown';
  120. if ($key[0] == 'OAUTH_GITHUB_NAME') {
  121. $OAUTH_SERVICENAME = 'GitHub';
  122. // List of keys that will be converted into scopes (from constants 'SCOPE_state_in_uppercase' in file of service).
  123. // We pass this param list in to 'state' because we need it before and after the redirect.
  124. $shortscope = 'user,public_repo';
  125. $urltorenew = $urlwithroot.'/core/modules/oauth/github_oauthcallback.php?shortscope='.$shortscope.'&state='.$shortscope.'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
  126. $urltodelete = $urlwithroot.'/core/modules/oauth/github_oauthcallback.php?action=delete&token='.newToken().'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
  127. $urltocheckperms = 'https://github.com/settings/applications/';
  128. } elseif ($key[0] == 'OAUTH_GOOGLE_NAME') {
  129. $OAUTH_SERVICENAME = 'Google';
  130. // List of keys that will be converted into scopes (from constants 'SCOPE_state_in_uppercase' in file of service).
  131. // We pass this param list in to 'state' because we need it before and after the redirect.
  132. $shortscope = 'userinfo_email,userinfo_profile,cloud_print';
  133. if (!empty($conf->global->OAUTH_GSUITE)) {
  134. $shortscope .= ',admin_directory_user';
  135. }
  136. //$scope.=',gmail_full';
  137. $urltorenew = $urlwithroot.'/core/modules/oauth/google_oauthcallback.php?shortscope='.$shortscope.'&state='.$shortscope.'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
  138. $urltodelete = $urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&token='.newToken().'&backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
  139. $urltocheckperms = 'https://security.google.com/settings/security/permissions';
  140. } elseif ($key[0] == 'OAUTH_STRIPE_TEST_NAME') {
  141. $OAUTH_SERVICENAME = 'StripeTest';
  142. $urltorenew = $urlwithroot.'/core/modules/oauth/stripetest_oauthcallback.php?backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
  143. $urltodelete = '';
  144. $urltocheckperms = '';
  145. } elseif ($key[0] == 'OAUTH_STRIPE_LIVE_NAME') {
  146. $OAUTH_SERVICENAME = 'StripeLive';
  147. $urltorenew = $urlwithroot.'/core/modules/oauth/stripelive_oauthcallback.php?backtourl='.urlencode(DOL_URL_ROOT.'/admin/oauthlogintokens.php');
  148. $urltodelete = '';
  149. $urltocheckperms = '';
  150. } else {
  151. $urltorenew = '';
  152. $urltodelete = '';
  153. $urltocheckperms = '';
  154. }
  155. // Show value of token
  156. $tokenobj = null;
  157. // Token
  158. require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
  159. require_once DOL_DOCUMENT_ROOT.'/includes/OAuth/bootstrap.php';
  160. // Dolibarr storage
  161. $storage = new DoliStorage($db, $conf);
  162. try {
  163. $tokenobj = $storage->retrieveAccessToken($OAUTH_SERVICENAME);
  164. } catch (Exception $e) {
  165. // Return an error if token not found
  166. }
  167. // Set other properties
  168. $refreshtoken = false;
  169. $expiredat = '';
  170. $expire = false;
  171. // Is token expired or will token expire in the next 30 seconds
  172. if (is_object($tokenobj)) {
  173. $expire = ($tokenobj->getEndOfLife() !== $tokenobj::EOL_NEVER_EXPIRES && $tokenobj->getEndOfLife() !== $tokenobj::EOL_UNKNOWN && time() > ($tokenobj->getEndOfLife() - 30));
  174. }
  175. if ($key[1] != '' && $key[2] != '') {
  176. if (is_object($tokenobj)) {
  177. $refreshtoken = $tokenobj->getRefreshToken();
  178. $endoflife = $tokenobj->getEndOfLife();
  179. if ($endoflife == $tokenobj::EOL_NEVER_EXPIRES) {
  180. $expiredat = $langs->trans("Never");
  181. } elseif ($endoflife == $tokenobj::EOL_UNKNOWN) {
  182. $expiredat = $langs->trans("Unknown");
  183. } else {
  184. $expiredat = dol_print_date($endoflife, "dayhour");
  185. }
  186. }
  187. }
  188. $submit_enabled = 0;
  189. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?mode=setup&amp;driver='.$driver.'" autocomplete="off">';
  190. print '<input type="hidden" name="token" value="'.newToken().'">';
  191. print '<input type="hidden" name="action" value="setconst">';
  192. print '<div class="div-table-responsive">';
  193. print '<table class="noborder centpercent">'."\n";
  194. print '<tr class="liste_titre">';
  195. print '<th class="titlefieldcreate">'.$langs->trans($key[0]).'</th>';
  196. print '<th></th>';
  197. print '<th></th>';
  198. print "</tr>\n";
  199. print '<tr class="oddeven">';
  200. print '<td'.($key['required'] ? ' class="required"' : '').'>';
  201. //var_dump($key);
  202. print $langs->trans("OAuthIDSecret").'</td>';
  203. print '<td>';
  204. print $langs->trans("SeePreviousTab");
  205. print '</td>';
  206. print '<td>';
  207. print '</td>';
  208. print '</tr>'."\n";
  209. print '<tr class="oddeven">';
  210. print '<td'.($key['required'] ? ' class="required"' : '').'>';
  211. //var_dump($key);
  212. print $langs->trans("IsTokenGenerated");
  213. print '</td>';
  214. print '<td>';
  215. if (is_object($tokenobj)) {
  216. print $langs->trans("HasAccessToken");
  217. } else {
  218. print $langs->trans("NoAccessToken");
  219. }
  220. print '</td>';
  221. print '<td width="50%">';
  222. // Links to delete/checks token
  223. if (is_object($tokenobj)) {
  224. //test on $storage->hasAccessToken($OAUTH_SERVICENAME) ?
  225. print '<a class="button smallpaddingimp" href="'.$urltodelete.'">'.$langs->trans('DeleteAccess').'</a><br>';
  226. }
  227. // Request remote token
  228. if ($urltorenew) {
  229. print '<a class="button smallpaddingimp" href="'.$urltorenew.'">'.$langs->trans('RequestAccess').'</a><br>';
  230. }
  231. // Check remote access
  232. if ($urltocheckperms) {
  233. print '<br>'.$langs->trans("ToCheckDeleteTokenOnProvider", $OAUTH_SERVICENAME).': <a href="'.$urltocheckperms.'" target="_'.strtolower($OAUTH_SERVICENAME).'">'.$urltocheckperms.'</a>';
  234. }
  235. print '</td>';
  236. print '</tr>';
  237. print '<tr class="oddeven">';
  238. print '<td'.($key['required'] ? ' class="required"' : '').'>';
  239. //var_dump($key);
  240. print $langs->trans("Token").'</td>';
  241. print '<td colspan="2">';
  242. if (is_object($tokenobj)) {
  243. //var_dump($tokenobj);
  244. print $tokenobj->getAccessToken().'<br>';
  245. //print 'Refresh: '.$tokenobj->getRefreshToken().'<br>';
  246. //print 'EndOfLife: '.$tokenobj->getEndOfLife().'<br>';
  247. //var_dump($tokenobj->getExtraParams());
  248. /*print '<br>Extra: <br><textarea class="quatrevingtpercent">';
  249. print ''.join(',',$tokenobj->getExtraParams());
  250. print '</textarea>';*/
  251. }
  252. print '</td>';
  253. print '</tr>'."\n";
  254. if (is_object($tokenobj)) {
  255. // Token refresh
  256. print '<tr class="oddeven">';
  257. print '<td'.($key['required'] ? ' class="required"' : '').'>';
  258. //var_dump($key);
  259. print $langs->trans("TOKEN_REFRESH").'</td>';
  260. print '<td colspan="2">';
  261. print yn($refreshtoken);
  262. print '</td>';
  263. print '</tr>';
  264. // Token expired
  265. print '<tr class="oddeven">';
  266. print '<td'.($key['required'] ? ' class="required"' : '').'>';
  267. //var_dump($key);
  268. print $langs->trans("TOKEN_EXPIRED").'</td>';
  269. print '<td colspan="2">';
  270. print yn($expire);
  271. print '</td>';
  272. print '</tr>';
  273. // Token expired at
  274. print '<tr class="oddeven">';
  275. print '<td'.($key['required'] ? ' class="required"' : '').'>';
  276. //var_dump($key);
  277. print $langs->trans("TOKEN_EXPIRE_AT").'</td>';
  278. print '<td colspan="2">';
  279. print $expiredat;
  280. print '</td>';
  281. print '</tr>';
  282. }
  283. print '</table>';
  284. print '</div>';
  285. if (!empty($driver)) {
  286. if ($submit_enabled) {
  287. print $form->buttonsSaveCancel("Modify", '');
  288. }
  289. }
  290. print '</form>';
  291. }
  292. }
  293. if ($mode == 'test' && $user->admin) {
  294. print $langs->trans('PrintTestDesc'.$driver)."<br><br>\n";
  295. print '<div class="div-table-responsive">';
  296. print '<table class="noborder centpercent">';
  297. if (!empty($driver)) {
  298. require_once DOL_DOCUMENT_ROOT.'/core/modules/printing/'.$driver.'.modules.php';
  299. $classname = 'printing_'.$driver;
  300. $langs->load($driver);
  301. $printer = new $classname($db);
  302. //print '<pre>'.print_r($printer, true).'</pre>';
  303. if (count($printer->getlistAvailablePrinters())) {
  304. if ($printer->listAvailablePrinters() == 0) {
  305. print $printer->resprint;
  306. } else {
  307. setEventMessages($printer->error, $printer->errors, 'errors');
  308. }
  309. } else {
  310. print $langs->trans('PleaseConfigureDriverfromList');
  311. }
  312. }
  313. print '</table>';
  314. print '</div>';
  315. }
  316. if ($mode == 'userconf' && $user->admin) {
  317. print $langs->trans('PrintUserConfDesc'.$driver)."<br><br>\n";
  318. print '<div class="div-table-responsive">';
  319. print '<table class="noborder centpercent">';
  320. print '<tr class="liste_titre">';
  321. print '<th>'.$langs->trans("User").'</th>';
  322. print '<th>'.$langs->trans("PrintModule").'</th>';
  323. print '<th>'.$langs->trans("PrintDriver").'</th>';
  324. print '<th>'.$langs->trans("Printer").'</th>';
  325. print '<th>'.$langs->trans("PrinterLocation").'</th>';
  326. print '<th>'.$langs->trans("PrinterId").'</th>';
  327. print '<th>'.$langs->trans("NumberOfCopy").'</th>';
  328. print '<th class="center">'.$langs->trans("Delete").'</th>';
  329. print "</tr>\n";
  330. $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';
  331. $resql = $db->query($sql);
  332. while ($row = $db->fetch_array($resql)) {
  333. print '<tr class="oddeven">';
  334. print '<td>'.$row['login'].'</td>';
  335. print '<td>'.$row['module'].'</td>';
  336. print '<td>'.$row['driver'].'</td>';
  337. print '<td>'.$row['printer_name'].'</td>';
  338. print '<td>'.$row['printer_location'].'</td>';
  339. print '<td>'.$row['printer_id'].'</td>';
  340. print '<td>'.$row['copy'].'</td>';
  341. print '<td class="center">'.img_picto($langs->trans("Delete"), 'delete').'</td>';
  342. print "</tr>\n";
  343. }
  344. print '</table>';
  345. print '</div>';
  346. }
  347. print dol_get_fiche_end();
  348. // End of page
  349. llxFooter();
  350. $db->close();