index.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2005-2016 Laurent Destailleur <eldy@users.sourceforge.org>
  4. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2012-2018 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/api/admin/index.php
  23. * \ingroup api
  24. * \brief Page to setup Webservices REST module
  25. */
  26. // Load Dolibarr environment
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  30. // Load translation files required by the page
  31. $langs->load("admin");
  32. if (!$user->admin) {
  33. accessforbidden();
  34. }
  35. $action = GETPOST('action', 'aZ09');
  36. // Activate Production mode
  37. if ($action == 'setproductionmode') {
  38. $status = GETPOST('status', 'alpha');
  39. if (dolibarr_set_const($db, 'API_PRODUCTION_MODE', $status, 'chaine', 0, '', 0) > 0) {
  40. $error = 0;
  41. if ($status == 1) {
  42. $result = dol_mkdir($conf->api->dir_temp);
  43. if ($result < 0) {
  44. setEventMessages($langs->trans("ErrorFailedToCreateDir", $conf->api->dir_temp), null, 'errors');
  45. $error++;
  46. }
  47. } else {
  48. // Delete the cache file otherwise it does not update
  49. $result = dol_delete_file($conf->api->dir_temp.'/routes.php');
  50. if ($result < 0) {
  51. setEventMessages($langs->trans("ErrorFailedToDeleteFile", $conf->api->dir_temp.'/routes.php'), null, 'errors');
  52. $error++;
  53. }
  54. }
  55. if (!$error) {
  56. header("Location: ".$_SERVER["PHP_SELF"]);
  57. exit;
  58. }
  59. } else {
  60. dol_print_error($db);
  61. }
  62. }
  63. // Disable compression mode
  64. if ($action == 'setdisablecompression') {
  65. $status = GETPOST('status', 'alpha');
  66. if (dolibarr_set_const($db, 'API_DISABLE_COMPRESSION', $status, 'chaine', 0, '', 0) > 0) {
  67. header("Location: ".$_SERVER["PHP_SELF"]);
  68. exit;
  69. } else {
  70. dol_print_error($db);
  71. }
  72. }
  73. if ($action == 'save') {
  74. dolibarr_set_const($db, 'API_RESTRICT_ON_IP', GETPOST('API_RESTRICT_ON_IP', 'alpha'));
  75. }
  76. dol_mkdir(DOL_DATA_ROOT.'/api/temp'); // May have been deleted by a purge
  77. /*
  78. * View
  79. */
  80. llxHeader();
  81. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  82. print load_fiche_titre($langs->trans("ApiSetup"), $linkback, 'title_setup');
  83. print '<span class="opacitymedium">'.$langs->trans("ApiDesc")."</span><br>\n";
  84. print "<br>\n";
  85. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  86. print '<input type="hidden" name="token" value="'.newToken().'">';
  87. print '<input type="hidden" name="action" value="save">';
  88. print '<table class="noborder centpercent">';
  89. print '<tr class="liste_titre">';
  90. print "<td>".$langs->trans("Parameter")."</td>";
  91. print '<td>'.$langs->trans("Value")."</td>";
  92. print "<td>&nbsp;</td>";
  93. print "</tr>";
  94. print '<tr class="oddeven">';
  95. print '<td>'.$langs->trans("ApiProductionMode").'</td>';
  96. $production_mode = (empty($conf->global->API_PRODUCTION_MODE) ?false:true);
  97. if ($production_mode) {
  98. print '<td><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setproductionmode&token='.newToken().'&status=0">';
  99. print img_picto($langs->trans("Activated"), 'switch_on');
  100. print '</a></td>';
  101. } else {
  102. print '<td><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setproductionmode&token='.newToken().'&status=1">';
  103. print img_picto($langs->trans("Disabled"), 'switch_off');
  104. print '</a></td>';
  105. }
  106. print '<td>&nbsp;</td>';
  107. print '</tr>';
  108. print '<tr class="oddeven">';
  109. print '<td>'.$langs->trans("API_DISABLE_COMPRESSION").'</td>';
  110. $disable_compression = (empty($conf->global->API_DISABLE_COMPRESSION) ?false:true);
  111. if ($disable_compression) {
  112. print '<td><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setdisablecompression&token='.newToken().'&status=0">';
  113. print img_picto($langs->trans("Activated"), 'switch_on');
  114. print '</a></td>';
  115. } else {
  116. print '<td><a class="reposition" href="'.$_SERVER['PHP_SELF'].'?action=setdisablecompression&token='.newToken().'&status=1">';
  117. print img_picto($langs->trans("Disabled"), 'switch_off');
  118. print '</a></td>';
  119. }
  120. print '<td>&nbsp;</td>';
  121. print '</tr>';
  122. print '<tr class="oddeven">';
  123. print '<td>'.$langs->trans("RESTRICT_ON_IP");
  124. print ' '.$langs->trans("Example").': '.$langs->trans("IPListExample");
  125. print '</td>';
  126. print '<td><input type="text" name="API_RESTRICT_ON_IP" value="'.dol_escape_htmltag(getDolGlobalString('API_RESTRICT_ON_IP')).'"></td>';
  127. print '<td>';
  128. print '<input type="submit" class="button button-save" name="save" value="'.dol_escape_htmltag($langs->trans("Save")).'"></td>';
  129. print '</td>';
  130. print '</tr>';
  131. print '</table>';
  132. print '<br><br>';
  133. print '</form>';
  134. // Define $urlwithroot
  135. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  136. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  137. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  138. // Show message
  139. $message = '';
  140. //$url = $urlwithroot.'/api/index.php/login?login=<strong>auserlogin</strong>&password=<strong>thepassword</strong>[&reset=1]';
  141. $url = $urlwithroot.'/api/index.php/login?login=auserlogin&password=thepassword[&reset=1]';
  142. $message .= '<span class="opacitymedium">'.$langs->trans("UrlToGetKeyToUseAPIs").':</span><br>';
  143. $message .= '<div class="urllink soixantepercent">'.img_picto('', 'globe').' <input type="text" class="quatrevingtpercent" id="urltogettoken" value="'.$url.'"></div>';
  144. print $message;
  145. print ajax_autoselect("urltogettoken");
  146. print '<br>';
  147. print '<br>';
  148. // Explorer
  149. print '<span class="opacitymedium">'.$langs->trans("ApiExporerIs").':</span><br>';
  150. if (dol_is_dir(DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/explorer')) {
  151. $url = DOL_MAIN_URL_ROOT.'/api/index.php/explorer';
  152. print '<div class="urllink soixantepercent">'.img_picto('', 'globe').' <a href="'.$url.'" target="_blank" rel="noopener noreferrer">'.$url."</a></div><br>\n";
  153. print '<div class="opacitymediumxxx"><br><span class="opacitymedium">'.$langs->trans("SwaggerDescriptionFile").':</span><br>';
  154. $urlswagger = DOL_MAIN_URL_ROOT.'/api/index.php/explorer/swagger.json?DOLAPIKEY=youruserapikey';
  155. //$urlswaggerreal = DOL_MAIN_URL_ROOT.'/api/index.php/explorer/swagger.json?DOLAPIKEY='.$user->api_key;
  156. print '<div class="urllink soixantepercent">'.img_picto('', 'globe').' <a href="'.$urlswagger.'" target="_blank" rel="noopener noreferrer">'.$urlswagger."</a></div><br>\n";
  157. print '</div>';
  158. } else {
  159. $langs->load("errors");
  160. print info_admin($langs->trans("ErrorNotAvailableWithThisDistribution"), 0, 0, 'error');
  161. }
  162. llxFooter();
  163. $db->close();