dolibarr  9.0.0
index.php
Go to the documentation of this file.
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@inodbox.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 
27 if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); // Do not check anti CSRF attack test
28 if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Do not check anti POST attack test
29 if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no need to load and show top and left menu
30 if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php
31 if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); // Do not load ajax.lib.php library
32 if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)
33 
34 
35 // Force entity if a value is provided into HTTP header. Otherwise, will use the entity of user of token used.
36 if (! empty($_SERVER['HTTP_DOLAPIENTITY'])) define("DOLENTITY", (int) $_SERVER['HTTP_DOLAPIENTITY']);
37 
38 
39 $res=0;
40 if (! $res && file_exists("../main.inc.php")) $res=include '../main.inc.php';
41 if (! $res) die("Include of main fails");
42 
43 require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/AutoLoader.php';
44 
45 call_user_func(function () {
46  $loader = Luracast\Restler\AutoLoader::instance();
47  spl_autoload_register($loader);
48  return $loader;
49 });
50 
51 require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
52 require_once DOL_DOCUMENT_ROOT.'/api/class/api_access.class.php';
53 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
54 
55 
56 
57 // Enable and test if module Api is enabled
58 if (empty($conf->global->MAIN_MODULE_API))
59 {
60  $langs->load("admin");
61  dol_syslog("Call Dolibarr API interfaces with module REST disabled");
62  print $langs->trans("WarningModuleNotActive",'Api').'.<br><br>';
63  print $langs->trans("ToActivateModule");
64  exit;
65 }
66 
67 // Test if explorer is not disabled
68 if (preg_match('/api\/index\.php\/explorer/', $_SERVER["PHP_SELF"]) && ! empty($conf->global->API_EXPLORER_DISABLED))
69 {
70  $langs->load("admin");
71  dol_syslog("Call Dolibarr API interfaces with module REST disabled");
72  print $langs->trans("WarningAPIExplorerDisabled").'.<br><br>';
73  exit;
74 }
75 
76 
77 // This 2 lines are usefull only if we want to exclude some Urls from the explorer
78 //use Luracast\Restler\Explorer;
79 //Explorer::$excludedPaths = array('/categories');
80 
81 
82 // Analyze URLs
83 // index.php/explorer do a redirect to index.php/explorer/
84 // index.php/explorer/ called by swagger to build explorer page
85 // index.php/explorer/.../....png|.css|.js called by swagger for resources to build explorer page
86 // index.php/explorer/resources.json called by swagger to get list of all services
87 // index.php/explorer/resources.json/xxx called by swagger to get detail of services xxx
88 // index.php/xxx called by any REST client to run API
89 
90 
91 preg_match('/index\.php\/([^\/]+)(.*)$/', $_SERVER["PHP_SELF"], $reg);
92 // .../index.php/categories?sortfield=t.rowid&sortorder=ASC
93 
94 
95 // Set the flag to say to refresh (when we reload the explorer, production must be for API call only)
96 $refreshcache=false;
97 if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/swagger.json' || $reg[2] == '/swagger.json/root' || $reg[2] == '/resources.json' || $reg[2] == '/resources.json/root'))
98 {
99  $refreshcache=true;
100 }
101 
102 
103 $api = new DolibarrApi($db, '', $refreshcache);
104 //var_dump($api->r->apiVersionMap);
105 
106 // Enable the Restler API Explorer.
107 // See https://github.com/Luracast/Restler-API-Explorer for more info.
108 $api->r->addAPIClass('Luracast\\Restler\\Explorer');
109 
110 $api->r->setSupportedFormats('JsonFormat', 'XmlFormat', 'UploadFormat'); // 'YamlFormat'
111 $api->r->addAuthenticationClass('DolibarrApiAccess','');
112 
113 // Define accepted mime types
114 UploadFormat::$allowedMimeTypes = array('image/jpeg', 'image/png', 'text/plain', 'application/octet-stream');
115 
116 
117 
118 // Call Explorer file for all APIs definitions
119 if (! empty($reg[1]) && $reg[1] == 'explorer' && ($reg[2] == '/swagger.json' || $reg[2] == '/swagger.json/root' || $reg[2] == '/resources.json' || $reg[2] == '/resources.json/root'))
120 {
121  // Scan all API files to load them
122 
123  $listofapis = array();
124 
125  $modulesdir = dolGetModulesDirs();
126  foreach ($modulesdir as $dir)
127  {
128  // Search available module
129  dol_syslog("Scan directory ".$dir." for module descriptor files, then search for API files");
130 
131  $handle=@opendir(dol_osencode($dir));
132  if (is_resource($handle))
133  {
134  while (($file = readdir($handle))!==false)
135  {
136  if (is_readable($dir.$file) && preg_match("/^mod(.*)\.class\.php$/i",$file,$regmod))
137  {
138  $module = strtolower($regmod[1]);
139  $moduledirforclass = getModuleDirForApiClass($module);
140  $modulenameforenabled = $module;
141  if ($module == 'propale') { $modulenameforenabled='propal'; }
142  if ($module == 'supplierproposal') { $modulenameforenabled='supplier_proposal'; }
143  if ($module == 'ficheinter') { $modulenameforenabled='ficheinter'; }
144 
145  dol_syslog("Found module file ".$file." - module=".$module." - modulenameforenabled=".$modulenameforenabled." - moduledirforclass=".$moduledirforclass);
146 
147  // Defined if module is enabled
148  $enabled=true;
149  if (empty($conf->$modulenameforenabled->enabled)) $enabled=false;
150 
151  if ($enabled)
152  {
153  // If exists, load the API class for enable module
154  // Search files named api_<object>.class.php into /htdocs/<module>/class directory
155  // @todo : use getElementProperties() function ?
156  $dir_part = dol_buildpath('/'.$moduledirforclass.'/class/');
157 
158  $handle_part=@opendir(dol_osencode($dir_part));
159  if (is_resource($handle_part))
160  {
161  while (($file_searched = readdir($handle_part))!==false)
162  {
163  if ($file_searched == 'api_access.class.php') continue;
164 
165  if (is_readable($dir_part.$file_searched) && preg_match("/^api_(.*)\.class\.php$/i",$file_searched,$regapi))
166  {
167  $classname = ucwords($regapi[1]);
168  $classname = str_replace('_', '', $classname);
169  require_once $dir_part.$file_searched;
170  if (class_exists($classname.'Api'))
171  {
172  //dol_syslog("Found API by index.php: classname=".$classname."Api for module ".$dir." into ".$dir_part.$file_searched);
173  $listofapis[strtolower($classname.'Api')] = $classname.'Api';
174  }
175  elseif (class_exists($classname))
176  {
177  //dol_syslog("Found API by index.php: classname=".$classname." for module ".$dir." into ".$dir_part.$file_searched);
178  $listofapis[strtolower($classname)] = $classname;
179  }
180  else
181  {
182  dol_syslog("We found an api_xxx file (".$file_searched.") but class ".$classname." does not exists after loading file", LOG_WARNING);
183  }
184  }
185  }
186  }
187  }
188  }
189  }
190  }
191  }
192 
193  // Sort the classes before adding them to Restler.
194  // The Restler API Explorer shows the classes in the order they are added and it's a mess if they are not sorted.
195  asort($listofapis);
196  foreach ($listofapis as $apiname => $classname)
197  {
198  $api->r->addAPIClass($classname, $apiname);
199  }
200  //var_dump($api->r);
201 }
202 
203 // Call one APIs or one definition of an API
204 if (! empty($reg[1]) && ($reg[1] != 'explorer' || ($reg[2] != '/swagger.json' && $reg[2] != '/resources.json' && preg_match('/^\/(swagger|resources)\.json\/(.+)$/', $reg[2], $regbis) && $regbis[2] != 'root')))
205 {
206  $module = $reg[1];
207  if ($module == 'explorer') // If we call page to explore details of a service
208  {
209  $module = $regbis[2];
210  }
211 
212  $module=strtolower($module);
213  $moduledirforclass = getModuleDirForApiClass($module);
214 
215  // Load a dedicated API file
216  dol_syslog("Load a dedicated API file module=".$module." moduledirforclass=".$moduledirforclass);
217 
218  $tmpmodule = $module;
219  if ($tmpmodule != 'api')
220  $tmpmodule = preg_replace('/api$/i', '', $tmpmodule);
221  $classfile = str_replace('_', '', $tmpmodule);
222  if ($module == 'supplierproposals')
223  $classfile = 'supplier_proposals';
224  if ($module == 'supplierorders')
225  $classfile = 'supplier_orders';
226  if ($module == 'supplierinvoices')
227  $classfile = 'supplier_invoices';
228  if ($module == 'ficheinter')
229  $classfile = 'interventions';
230  if ($module == 'interventions')
231  $classfile = 'interventions';
232 
233  $dir_part_file = dol_buildpath('/' . $moduledirforclass . '/class/api_' . $classfile . '.class.php', 0, 2);
234 
235  $classname = ucwords($module);
236 
237  dol_syslog('Search /' . $moduledirforclass . '/class/api_' . $classfile . '.class.php => dir_part_file=' . $dir_part_file . ' classname=' . $classname);
238 
239  $res = false;
240  if ($dir_part_file)
241  $res = include_once $dir_part_file;
242  if (! $res) {
243  print 'API not found (failed to include API file)';
244  header('HTTP/1.1 501 API not found (failed to include API file)');
245  exit(0);
246  }
247 
248  if (class_exists($classname))
249  $api->r->addAPIClass($classname);
250 }
251 
252 // TODO If not found, redirect to explorer
253 //var_dump($api->r->apiVersionMap);
254 //exit;
255 
256 // Call API (we suppose we found it)
257 $api->r->handle();
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
print
Draft customers invoices.
Definition: index.php:91
getModuleDirForApiClass($module)
Get name of directory where the api_...class.php file is stored.
dolGetModulesDirs($subdir='')
Return list of modules directories.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Class for API REST v1.
Definition: api.class.php:29
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.