27 use Luracast\Restler\Format\UploadFormat;
29 if (!defined(
'NOCSRFCHECK')) define(
'NOCSRFCHECK',
'1');
30 if (!defined(
'NOTOKENRENEWAL')) define(
'NOTOKENRENEWAL',
'1');
31 if (!defined(
'NOREQUIREMENU')) define(
'NOREQUIREMENU',
'1');
32 if (!defined(
'NOREQUIREHTML')) define(
'NOREQUIREHTML',
'1');
33 if (!defined(
'NOREQUIREAJAX')) define(
'NOREQUIREAJAX',
'1');
34 if (!defined(
"NOLOGIN")) define(
"NOLOGIN",
'1');
35 if (!defined(
"NOSESSION")) define(
"NOSESSION",
'1');
39 if (!empty(
$_SERVER[
'HTTP_DOLAPIENTITY'])) define(
"DOLENTITY", (
int)
$_SERVER[
'HTTP_DOLAPIENTITY']);
43 if (!$res && file_exists(
"../main.inc.php")) $res = include
'../main.inc.php';
44 if (!$res) die(
"Include of main fails");
46 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/AutoLoader.php';
48 call_user_func(
function () {
49 $loader = Luracast\Restler\AutoLoader::instance();
50 spl_autoload_register($loader);
54 require_once DOL_DOCUMENT_ROOT.
'/api/class/api.class.php';
55 require_once DOL_DOCUMENT_ROOT.
'/api/class/api_access.class.php';
56 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
60 if (preg_match(
'/api\/index\.php$/', $url)) {
61 $url =
$_SERVER[
'PHP_SELF'].$_SERVER[
'PATH_INFO'];
64 if (!empty($conf->global->MAIN_NGINX_FIX))
70 if (empty($conf->global->MAIN_MODULE_API))
72 $langs->load(
"admin");
73 dol_syslog(
"Call Dolibarr API interfaces with module REST disabled");
74 print $langs->trans(
"WarningModuleNotActive",
'Api').
'.<br><br>';
75 print $langs->trans(
"ToActivateModule");
81 if (preg_match(
'/api\/index\.php\/explorer/', $url) && !empty($conf->global->API_EXPLORER_DISABLED))
83 $langs->load(
"admin");
84 dol_syslog(
"Call Dolibarr API interfaces with module REST disabled");
85 print $langs->trans(
"WarningAPIExplorerDisabled").
'.<br><br>';
106 preg_match(
'/index\.php\/([^\/]+)(.*)$/', $url, $reg);
114 $refreshcache = (empty($conf->global->API_PRODUCTION_DO_NOT_ALWAYS_REFRESH_CACHE) ?
true :
false);
115 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'))
117 $refreshcache =
true;
125 $api->r->addAPIClass(
'Luracast\\Restler\\Explorer');
127 $api->r->setSupportedFormats(
'JsonFormat',
'XmlFormat',
'UploadFormat');
128 $api->r->addAuthenticationClass(
'DolibarrApiAccess',
'');
131 UploadFormat::$allowedMimeTypes = array(
'image/jpeg',
'image/png',
'text/plain',
'application/octet-stream');
135 if (!empty($conf->global->API_RESTRICT_ON_IP))
137 $allowedip = explode(
' ', $conf->global->API_RESTRICT_ON_IP);
139 if (!in_array($ipremote, $allowedip))
141 dol_syslog(
'Remote ip is '.$ipremote.
', not into list '.$conf->global->API_RESTRICT_ON_IP);
142 print 'APIs are not allowed from the IP '.$ipremote;
143 header(
'HTTP/1.1 503 API not allowed from your IP '.$ipremote);
151 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'))
155 $listofapis = array();
158 foreach ($modulesdir as $dir)
161 dol_syslog(
"Scan directory ".$dir.
" for module descriptor files, then search for API files");
164 if (is_resource($handle))
166 while (($file = readdir($handle)) !==
false)
169 if (is_readable($dir.$file) && preg_match(
"/^mod(.*)\.class\.php$/i", $file, $regmod))
171 $module = strtolower($regmod[1]);
173 $modulenameforenabled = $module;
174 if ($module ==
'propale') { $modulenameforenabled =
'propal'; }
175 if ($module ==
'supplierproposal') { $modulenameforenabled =
'supplier_proposal'; }
176 if ($module ==
'ficheinter') { $modulenameforenabled =
'ficheinter'; }
178 dol_syslog(
"Found module file ".$file.
" - module=".$module.
" - modulenameforenabled=".$modulenameforenabled.
" - moduledirforclass=".$moduledirforclass);
182 if (empty($conf->$modulenameforenabled->enabled)) $enabled =
false;
192 if (is_resource($handle_part))
194 while (($file_searched = readdir($handle_part)) !==
false)
196 if ($file_searched ==
'api_access.class.php')
continue;
199 if (is_readable($dir_part.$file_searched) && preg_match(
"/^api_(.*)\.class\.php$/i", $file_searched, $regapi))
201 $classname = ucwords($regapi[1]);
202 $classname = str_replace(
'_',
'', $classname);
203 require_once $dir_part.$file_searched;
204 if (class_exists($classname.
'Api'))
207 $listofapis[strtolower($classname.
'Api')] = $classname.
'Api';
208 } elseif (class_exists($classname))
211 $listofapis[strtolower($classname)] = $classname;
213 dol_syslog(
"We found an api_xxx file (".$file_searched.
") but class ".$classname.
" does not exists after loading file", LOG_WARNING);
227 foreach ($listofapis as $apiname => $classname)
229 $api->r->addAPIClass($classname, $apiname);
236 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')))
238 $moduleobject = $reg[1];
239 if ($moduleobject ==
'explorer')
241 $moduleobject = $regbis[2];
244 $moduleobject = strtolower($moduleobject);
248 dol_syslog(
"Load a dedicated API file moduleobject=".$moduleobject.
" moduledirforclass=".$moduledirforclass);
250 $tmpmodule = $moduleobject;
251 if ($tmpmodule !=
'api')
252 $tmpmodule = preg_replace(
'/api$/i',
'', $tmpmodule);
253 $classfile = str_replace(
'_',
'', $tmpmodule);
256 if ($moduleobject ==
'supplierproposals')
257 $classfile =
'supplier_proposals';
258 if ($moduleobject ==
'supplierorders')
259 $classfile =
'supplier_orders';
260 if ($moduleobject ==
'supplierinvoices')
261 $classfile =
'supplier_invoices';
262 if ($moduleobject ==
'ficheinter')
263 $classfile =
'interventions';
264 if ($moduleobject ==
'interventions')
265 $classfile =
'interventions';
267 $dir_part_file =
dol_buildpath(
'/'.$moduledirforclass.
'/class/api_'.$classfile.
'.class.php', 0, 2);
269 $classname = ucwords($moduleobject);
271 dol_syslog(
'Search api file /'.$moduledirforclass.
'/class/api_'.$classfile.
'.class.php => dir_part_file='.$dir_part_file.
' classname='.$classname);
275 $res = include_once $dir_part_file;
277 dol_syslog(
'Failed to make include_once '.$dir_part_file, LOG_WARNING);
278 print 'API not found (failed to include API file)';
279 header(
'HTTP/1.1 501 API not found (failed to include API file)');
284 if (class_exists($classname))
285 $api->r->addAPIClass($classname);
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
getUserRemoteIP()
Return the IP of remote user.
getModuleDirForApiClass($moduleobject)
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.