index.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2017 Regis Houssin <regis.houssin@capnetworks.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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \defgroup api Module DolibarrApi
  21. * \brief API loader
  22. * Search files htdocs/<module>/class/api_<module>.class.php
  23. * \file htdocs/api/indexphp
  24. *
  25. * @todo User authentication with api_key
  26. */
  27. if (! defined("NOLOGIN")) define("NOLOGIN",'1');
  28. if (! defined("NOCSRFCHECK")) define("NOCSRFCHECK",'1');
  29. $res=0;
  30. if (! $res && file_exists("../main.inc.php")) $res=include '../main.inc.php';
  31. if (! $res) die("Include of main fails");
  32. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/AutoLoader.php';
  33. call_user_func(function () {
  34. $loader = Luracast\Restler\AutoLoader::instance();
  35. spl_autoload_register($loader);
  36. return $loader;
  37. });
  38. require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/api/class/api_access.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  41. // Enable and test if module Api is enabled
  42. if (empty($conf->global->MAIN_MODULE_API))
  43. {
  44. $langs->load("admin");
  45. dol_syslog("Call Dolibarr API interfaces with module REST disabled");
  46. print $langs->trans("WarningModuleNotActive",'Api').'.<br><br>';
  47. print $langs->trans("ToActivateModule");
  48. exit;
  49. }
  50. // Test if explorer is not disabled
  51. if (preg_match('/api\/index\.php\/explorer/', $_SERVER["PHP_SELF"]) && ! empty($conf->global->API_EXPLORER_DISABLED))
  52. {
  53. $langs->load("admin");
  54. dol_syslog("Call Dolibarr API interfaces with module REST disabled");
  55. print $langs->trans("WarningAPIExplorerDisabled").'.<br><br>';
  56. exit;
  57. }
  58. $api = new DolibarrApi($db);
  59. // Enable the Restler API Explorer.
  60. // See https://github.com/Luracast/Restler-API-Explorer for more info.
  61. $api->r->addAPIClass('Luracast\\Restler\\Explorer');
  62. $api->r->setSupportedFormats('JsonFormat', 'XmlFormat', 'UploadFormat');
  63. $api->r->addAuthenticationClass('DolibarrApiAccess','');
  64. // Define accepted mime types
  65. UploadFormat::$allowedMimeTypes = array('image/jpeg', 'image/png', 'text/plain', 'application/octet-stream');
  66. $listofapis = array();
  67. $modulesdir = dolGetModulesDirs();
  68. foreach ($modulesdir as $dir)
  69. {
  70. /*
  71. * Search available module
  72. */
  73. //dol_syslog("Scan directory ".$dir." for API modules");
  74. $handle=@opendir(dol_osencode($dir));
  75. if (is_resource($handle))
  76. {
  77. while (($file = readdir($handle))!==false)
  78. {
  79. if (is_readable($dir.$file) && preg_match("/^mod(.*)\.class\.php$/i",$file,$reg))
  80. {
  81. $module = strtolower($reg[1]);
  82. $moduledirforclass = $module;
  83. $moduleforperm = $module;
  84. if ($module == 'propale') {
  85. $moduledirforclass = 'comm/propal';
  86. $moduleforperm='propal';
  87. }
  88. elseif ($module == 'agenda') {
  89. $moduledirforclass = 'comm/action';
  90. }
  91. elseif ($module == 'adherent') {
  92. $moduledirforclass = 'adherents';
  93. }
  94. elseif ($module == 'banque') {
  95. $moduledirforclass = 'compta/bank';
  96. }
  97. elseif ($module == 'categorie') {
  98. $moduledirforclass = 'categories';
  99. }
  100. elseif ($module == 'facture') {
  101. $moduledirforclass = 'compta/facture';
  102. }
  103. elseif ($module == 'project') {
  104. $moduledirforclass = 'projet';
  105. }
  106. elseif ($module == 'task') {
  107. $moduledirforclass = 'projet';
  108. }
  109. elseif ($module == 'stock') {
  110. $moduledirforclass = 'product/stock';
  111. }
  112. elseif ($module == 'fournisseur') {
  113. $moduledirforclass = 'fourn';
  114. }
  115. //dol_syslog("Found module file ".$file." - module=".$module." - moduledirforclass=".$moduledirforclass);
  116. // Defined if module is enabled
  117. $enabled=true;
  118. if (empty($conf->$moduleforperm->enabled)) $enabled=false;
  119. if ($enabled)
  120. {
  121. /*
  122. * If exists, load the API class for enable module
  123. *
  124. * Search files named api_<object>.class.php into /htdocs/<module>/class directory
  125. *
  126. * @todo : use getElementProperties() function ?
  127. */
  128. $dir_part = dol_buildpath('/'.$moduledirforclass.'/class/');
  129. $handle_part=@opendir(dol_osencode($dir_part));
  130. if (is_resource($handle_part))
  131. {
  132. while (($file_searched = readdir($handle_part))!==false)
  133. {
  134. if ($file_searched == 'api_access.class.php') continue;
  135. // Support of the deprecated API.
  136. if (is_readable($dir_part.$file_searched) && preg_match("/^api_deprecated_(.*)\.class\.php$/i",$file_searched,$reg))
  137. {
  138. $classname = ucwords($reg[1]).'Api';
  139. require_once $dir_part.$file_searched;
  140. if (class_exists($classname))
  141. {
  142. //dol_syslog("Found deprecated API by index.php: classname=".$classname." for module ".$dir." into ".$dir_part.$file_searched);
  143. $api->r->addAPIClass($classname, '/');
  144. }
  145. else
  146. {
  147. dol_syslog("We found an api_xxx file (".$file_searched.") but class ".$classname." does not exists after loading file", LOG_WARNING);
  148. }
  149. }
  150. elseif (is_readable($dir_part.$file_searched) && preg_match("/^api_(.*)\.class\.php$/i",$file_searched,$reg))
  151. {
  152. $classname = ucwords($reg[1]);
  153. $classname = str_replace('_', '', $classname);
  154. require_once $dir_part.$file_searched;
  155. if (class_exists($classname))
  156. {
  157. //dol_syslog("Found API by index.php: classname=".$classname." for module ".$dir." into ".$dir_part.$file_searched);
  158. $listofapis[] = $classname;
  159. }
  160. else
  161. {
  162. dol_syslog("We found an api_xxx file (".$file_searched.") but class ".$classname." does not exists after loading file", LOG_WARNING);
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. // Sort the classes before adding them to Restler. The Restler API Explorer
  173. // shows the classes in the order they are added and it's a mess if they are
  174. // not sorted.
  175. sort($listofapis);
  176. //var_dump($listofapis);
  177. foreach ($listofapis as $classname)
  178. {
  179. $api->r->addAPIClass($classname);
  180. }
  181. // TODO If not found, redirect to explorer
  182. //var_dump($api);
  183. // Call API (we suppose we found it)
  184. $api->r->handle();