oauth.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /* Copyright (C) 2015-2018 Frederic France <frederic.france@netlogic.fr>
  3. * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. */
  19. /**
  20. * \file htdocs/admin/oauth.php
  21. * \ingroup oauth
  22. * \brief Setup page to configure oauth access api
  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';
  27. // Define $urlwithroot
  28. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  29. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  30. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('admin', 'oauth'));
  33. // Security check
  34. if (!$user->admin) {
  35. accessforbidden();
  36. }
  37. $action = GETPOST('action', 'aZ09');
  38. /*
  39. * Actions
  40. */
  41. if ($action == 'update') {
  42. $error = 0;
  43. foreach ($list as $constname) {
  44. $constvalue = GETPOST($constname[1], 'alpha');
  45. if (!dolibarr_set_const($db, $constname[1], $constvalue, 'chaine', 0, '', $conf->entity)) {
  46. $error++;
  47. }
  48. $constvalue = GETPOST($constname[2], 'alpha');
  49. if (!dolibarr_set_const($db, $constname[2], $constvalue, 'chaine', 0, '', $conf->entity)) {
  50. $error++;
  51. }
  52. }
  53. if (!$error) {
  54. setEventMessages($langs->trans("SetupSaved"), null);
  55. } else {
  56. setEventMessages($langs->trans("Error"), null, 'errors');
  57. }
  58. }
  59. /*
  60. * View
  61. */
  62. llxHeader();
  63. $form = new Form($db);
  64. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  65. print load_fiche_titre($langs->trans('ConfigOAuth'), $linkback, 'title_setup');
  66. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  67. print '<input type="hidden" name="token" value="'.newToken().'">';
  68. print '<input type="hidden" name="action" value="update">';
  69. $head = oauthadmin_prepare_head();
  70. print dol_get_fiche_head($head, 'services', '', -1, 'technic');
  71. print '<span class="opacitymedium">'.$langs->trans("ListOfSupportedOauthProviders").'</span><br><br>';
  72. print '<div class="div-table-responsive">';
  73. print '<table class="noborder centpercent">';
  74. $i = 0;
  75. // $list is defined into oauth.lib.php
  76. foreach ($list as $key) {
  77. $supported = 0;
  78. if (in_array($key[0], array_keys($supportedoauth2array))) {
  79. $supported = 1;
  80. }
  81. if (!$supported) {
  82. continue; // show only supported
  83. }
  84. $i++;
  85. print '<tr class="liste_titre'.($i > 1 ? ' liste_titre_add' : '').'">';
  86. // Api Name
  87. $label = $langs->trans($key[0]);
  88. print '<td>'.$label.'</td>';
  89. print '<td>';
  90. if (!empty($key[3])) {
  91. print $langs->trans($key[3]);
  92. }
  93. print '</td>';
  94. print '</tr>';
  95. if ($supported) {
  96. $redirect_uri = $urlwithroot.'/core/modules/oauth/'.$supportedoauth2array[$key[0]].'_oauthcallback.php';
  97. print '<tr class="oddeven value">';
  98. print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
  99. print '<td><input style="width: 80%" type"text" name="uri'.$key[0].'" value="'.$redirect_uri.'">';
  100. print '</td></tr>';
  101. } else {
  102. print '<tr class="oddeven value">';
  103. print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
  104. print '<td>'.$langs->trans("FeatureNotYetSupported").'</td>';
  105. print '</td></tr>';
  106. }
  107. // Api Id
  108. print '<tr class="oddeven value">';
  109. print '<td><label for="'.$key[1].'">'.$langs->trans($key[1]).'</label></td>';
  110. print '<td><input type="text" size="100" id="'.$key[1].'" name="'.$key[1].'" value="'.$conf->global->{$key[1]}.'">';
  111. print '</td></tr>';
  112. // Api Secret
  113. print '<tr class="oddeven value">';
  114. print '<td><label for="'.$key[2].'">'.$langs->trans($key[2]).'</label></td>';
  115. print '<td><input type="password" size="100" id="'.$key[2].'" name="'.$key[2].'" value="'.$conf->global->{$key[2]}.'">';
  116. print '</td></tr>';
  117. }
  118. print '</table>'."\n";
  119. print '</div>';
  120. print dol_get_fiche_end();
  121. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans('Modify').'" name="button"></div>';
  122. print '</form>';
  123. // End of page
  124. llxFooter();
  125. $db->close();