oauth.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /* Copyright (C) 2015-2018 Frederic France <frederic.france@netlogic.fr>
  3. * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  4. * Copyright (C) 2022 Laurent Destailleur <eldy@users.sourceforge.net>
  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/oauth.php
  21. * \ingroup oauth
  22. * \brief Setup page to configure oauth access api
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/oauth.lib.php';
  28. // $supportedoauth2array is defined into oauth.lib.php
  29. // Define $urlwithroot
  30. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  31. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  32. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  33. // Load translation files required by the page
  34. $langs->loadLangs(array('admin', 'oauth', 'modulebuilder'));
  35. // Security check
  36. if (!$user->admin) {
  37. accessforbidden();
  38. }
  39. $action = GETPOST('action', 'aZ09');
  40. $provider = GETPOST('provider', 'aZ09');
  41. $label = GETPOST('label', 'aZ09');
  42. $error = 0;
  43. /*
  44. * Actions
  45. */
  46. if ($action == 'add') { // $provider is OAUTH_XXX
  47. if ($provider && $provider != '-1') {
  48. $constname = strtoupper($provider).($label ? '-'.$label : '').'_ID';
  49. if (getDolGlobalString($constname)) {
  50. setEventMessages($langs->trans("AOAuthEntryForThisProviderAndLabelAlreadyHasAKey"), null, 'errors');
  51. $error++;
  52. } else {
  53. dolibarr_set_const($db, $constname, $langs->trans('ToComplete'), 'chaine', 0, '', $conf->entity);
  54. setEventMessages($langs->trans("OAuthProviderAdded"), null);
  55. }
  56. }
  57. }
  58. if ($action == 'update') {
  59. foreach ($conf->global as $key => $val) {
  60. if (!empty($val) && preg_match('/^OAUTH_.+_ID$/', $key)) {
  61. $constvalue = str_replace('_ID', '', $key);
  62. if (!dolibarr_set_const($db, $constvalue.'_ID', GETPOST($constvalue.'_ID'), 'chaine', 0, '', $conf->entity)) {
  63. $error++;
  64. }
  65. // If we reset this provider, we also remove the secret
  66. if (!dolibarr_set_const($db, $constvalue.'_SECRET', GETPOST($constvalue.'_ID') ? GETPOST($constvalue.'_SECRET') : '', 'chaine', 0, '', $conf->entity)) {
  67. $error++;
  68. }
  69. if (GETPOSTISSET($constvalue.'_URLAUTHORIZE')) {
  70. if (!dolibarr_set_const($db, $constvalue.'_URLAUTHORIZE', GETPOST($constvalue.'_URLAUTHORIZE'), 'chaine', 0, '', $conf->entity)) {
  71. $error++;
  72. }
  73. }
  74. if (GETPOSTISSET($constvalue.'_SCOPE')) {
  75. if (!dolibarr_set_const($db, $constvalue.'_SCOPE', GETPOST($constvalue.'_SCOPE'), 'chaine', 0, '', $conf->entity)) {
  76. $error++;
  77. }
  78. }
  79. }
  80. }
  81. if (!$error) {
  82. setEventMessages($langs->trans("SetupSaved"), null);
  83. } else {
  84. setEventMessages($langs->trans("Error"), null, 'errors');
  85. }
  86. }
  87. /*
  88. * View
  89. */
  90. llxHeader();
  91. $form = new Form($db);
  92. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  93. print load_fiche_titre($langs->trans('ConfigOAuth'), $linkback, 'title_setup');
  94. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  95. print '<input type="hidden" name="token" value="'.newToken().'">';
  96. print '<input type="hidden" name="action" value="add">';
  97. $head = oauthadmin_prepare_head();
  98. print dol_get_fiche_head($head, 'services', '', -1, '');
  99. print '<span class="opacitymedium">'.$langs->trans("ListOfSupportedOauthProviders").'</span><br><br>';
  100. print '<select name="provider" id="provider" class="minwidth150">';
  101. print '<option name="-1" value="-1">'.$langs->trans("OAuthProvider").'</option>';
  102. foreach ($list as $key) {
  103. $supported = 0;
  104. $keyforsupportedoauth2array = $key[0];
  105. if (in_array($keyforsupportedoauth2array, array_keys($supportedoauth2array))) {
  106. $supported = 1;
  107. }
  108. if (!$supported) {
  109. continue; // show only supported
  110. }
  111. $i++;
  112. print '<option name="'.$keyforsupportedoauth2array.'" value="'.str_replace('_NAME', '', $keyforsupportedoauth2array).'">'.$supportedoauth2array[$keyforsupportedoauth2array]['name'].'</option>'."\n";
  113. }
  114. print '</select>';
  115. print ajax_combobox('provider');
  116. print ' <input type="text" name="label" value="" placeholder="'.$langs->trans("Label").'" pattern="^\S+$" title="'.$langs->trans("SpaceOrSpecialCharAreNotAllowed").'">';
  117. print ' <input type="submit" class="button small" name="add" value="'.$langs->trans("Add").'">';
  118. print '<br>';
  119. print '<br>';
  120. print dol_get_fiche_end();
  121. print '</form>';
  122. // Define $listinsetup
  123. foreach ($conf->global as $key => $val) {
  124. if (!empty($val) && preg_match('/^OAUTH_.*_ID$/', $key)) {
  125. $provider = preg_replace('/_ID$/', '', $key);
  126. $listinsetup[] = array(
  127. $provider.'_NAME',
  128. $provider.'_ID',
  129. $provider.'_SECRET',
  130. $provider.'_URLAUTHORIZE', // For custom oauth links
  131. $provider.'_SCOPE' // For custom oauth links
  132. );
  133. }
  134. }
  135. if (count($listinsetup) > 0) {
  136. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  137. print '<input type="hidden" name="token" value="'.newToken().'">';
  138. print '<input type="hidden" name="action" value="update">';
  139. print '<div class="div-table-responsive-no-min">';
  140. print '<table class="noborder centpercent">';
  141. $i = 0;
  142. // $list is defined into oauth.lib.php to the list of supporter OAuth providers.
  143. foreach ($listinsetup as $key) {
  144. $supported = 0;
  145. $keyforsupportedoauth2array = $key[0]; // May be OAUTH_GOOGLE_NAME or OAUTH_GOOGLE_xxx_NAME
  146. $keyforsupportedoauth2array = preg_replace('/^OAUTH_/', '', $keyforsupportedoauth2array);
  147. $keyforsupportedoauth2array = preg_replace('/_NAME$/', '', $keyforsupportedoauth2array);
  148. if (preg_match('/^.*-/', $keyforsupportedoauth2array)) {
  149. $keyforprovider = preg_replace('/^.*-/', '', $keyforsupportedoauth2array);
  150. } else {
  151. $keyforprovider = '';
  152. }
  153. $keyforsupportedoauth2array = preg_replace('/-.*$/', '', $keyforsupportedoauth2array);
  154. $keyforsupportedoauth2array = 'OAUTH_'.$keyforsupportedoauth2array.'_NAME';
  155. if (in_array($keyforsupportedoauth2array, array_keys($supportedoauth2array))) {
  156. $supported = 1;
  157. }
  158. if (!$supported) {
  159. continue; // show only supported
  160. }
  161. $i++;
  162. // Api Name
  163. $label = $langs->trans($keyforsupportedoauth2array);
  164. print '<tr class="liste_titre'.($i > 1 ? ' liste_titre_add' : '').'">';
  165. print '<td>';
  166. print img_picto('', $supportedoauth2array[$keyforsupportedoauth2array]['picto'], 'class="pictofixedwidth"');
  167. if ($label == $keyforsupportedoauth2array) {
  168. print $supportedoauth2array[$keyforsupportedoauth2array]['name'];
  169. } else {
  170. print $label;
  171. }
  172. if ($keyforprovider) {
  173. print ' (<b>'.$keyforprovider.'</b>)';
  174. } else {
  175. print ' (<b>'.$langs->trans("NoName").'</b>)';
  176. }
  177. print '</td>';
  178. print '<td>';
  179. if (!empty($supportedoauth2array[$keyforsupportedoauth2array]['urlforcredentials'])) {
  180. print $langs->trans("OAUTH_URL_FOR_CREDENTIAL", $supportedoauth2array[$keyforsupportedoauth2array]['urlforcredentials']);
  181. }
  182. print '</td>';
  183. print '</tr>';
  184. if ($supported) {
  185. $redirect_uri = $urlwithroot.'/core/modules/oauth/'.$supportedoauth2array[$keyforsupportedoauth2array]['callbackfile'].'_oauthcallback.php';
  186. print '<tr class="oddeven value">';
  187. print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
  188. print '<td><input style="width: 80%" type"text" name="uri'.$keyforsupportedoauth2array.'" value="'.$redirect_uri.'" disabled>';
  189. print '</td></tr>';
  190. if ($keyforsupportedoauth2array == 'OAUTH_OTHER_NAME') {
  191. print '<tr class="oddeven value">';
  192. print '<td>'.$langs->trans("URLOfServiceForAuthorization").'</td>';
  193. print '<td><input style="width: 80%" type"text" name="'.$key[3].'" value="'.getDolGlobalString($key[3]).'" >';
  194. print '</td></tr>';
  195. }
  196. } else {
  197. print '<tr class="oddeven value">';
  198. print '<td>'.$langs->trans("UseTheFollowingUrlAsRedirectURI").'</td>';
  199. print '<td>'.$langs->trans("FeatureNotYetSupported").'</td>';
  200. print '</td></tr>';
  201. }
  202. // Api Id
  203. print '<tr class="oddeven value">';
  204. print '<td><label for="'.$key[1].'">'.$langs->trans("OAUTH_ID").'</label></td>';
  205. print '<td><input type="text" size="100" id="'.$key[1].'" name="'.$key[1].'" value="'.getDolGlobalString($key[1]).'">';
  206. print '</td></tr>';
  207. // Api Secret
  208. print '<tr class="oddeven value">';
  209. print '<td><label for="'.$key[2].'">'.$langs->trans("OAUTH_SECRET").'</label></td>';
  210. print '<td><input type="password" size="100" id="'.$key[2].'" name="'.$key[2].'" value="'.getDolGlobalString($key[2]).'">';
  211. print '</td></tr>';
  212. // TODO Move this into token generation
  213. if ($supported) {
  214. if ($keyforsupportedoauth2array == 'OAUTH_OTHER_NAME') {
  215. print '<tr class="oddeven value">';
  216. print '<td>'.$langs->trans("Scopes").'</td>';
  217. print '<td>';
  218. print '<input style="width: 80%" type"text" name="'.$key[4].'" value="'.getDolGlobalString($key[4]).'" >';
  219. print '</td></tr>';
  220. } else {
  221. print '<tr class="oddeven value">';
  222. print '<td>'.$langs->trans("Scopes").'</td>';
  223. print '<td>';
  224. //print '<input style="width: 80%" type"text" name="'.$key[4].'" value="'.getDolGlobalString($key[4]).'" >';
  225. print $supportedoauth2array[$keyforsupportedoauth2array]['defaultscope'];
  226. print '</td></tr>';
  227. }
  228. }
  229. }
  230. print '</table>'."\n";
  231. print '</div>';
  232. print $form->buttonsSaveCancel("Modify", '');
  233. print '</form>';
  234. }
  235. // End of page
  236. llxFooter();
  237. $db->close();