2015-05-01 16:12:30 +02:00
< ? php
2017-05-25 13:48:30 +02:00
/* Copyright ( C ) 2015 Jean - François Ferry < jfefe @ aternatik . fr >
2016-09-26 02:18:11 +02:00
* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2017 Regis Houssin < regis . houssin @ inodbox . com >
2015-11-22 17:17:06 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2015-11-22 17:17:06 +01:00
*/
2015-05-01 16:12:30 +02:00
2015-11-22 17:17:06 +01:00
/**
* \defgroup api Module DolibarrApi
* \brief API loader
* Search files htdocs /< module >/ class / api_ < module >. class . php
2017-06-25 17:09:08 +02:00
* \file htdocs / api / index . php
2015-05-01 16:12:30 +02:00
*/
2016-09-08 12:12:00 +02:00
2019-08-25 16:40:21 +02:00
use Luracast\Restler\Format\UploadFormat ;
2019-01-27 11:55:16 +01:00
if ( ! defined ( 'NOCSRFCHECK' )) define ( 'NOCSRFCHECK' , '1' ); // Do not check anti CSRF attack test
if ( ! defined ( 'NOTOKENRENEWAL' )) define ( 'NOTOKENRENEWAL' , '1' ); // Do not check anti POST attack test
if ( ! defined ( 'NOREQUIREMENU' )) define ( 'NOREQUIREMENU' , '1' ); // If there is no need to load and show top and left menu
if ( ! defined ( 'NOREQUIREHTML' )) define ( 'NOREQUIREHTML' , '1' ); // If we don't need to load the html.form.class.php
if ( ! defined ( 'NOREQUIREAJAX' )) define ( 'NOREQUIREAJAX' , '1' ); // Do not load ajax.lib.php library
if ( ! defined ( " NOLOGIN " )) define ( " NOLOGIN " , '1' ); // If this page is public (can be called outside logged session)
2017-06-25 18:18:17 +02:00
2015-11-22 17:17:06 +01:00
2017-10-19 17:30:08 +02:00
// Force entity if a value is provided into HTTP header. Otherwise, will use the entity of user of token used.
2017-09-16 03:54:23 +02:00
if ( ! empty ( $_SERVER [ 'HTTP_DOLAPIENTITY' ])) define ( " DOLENTITY " , ( int ) $_SERVER [ 'HTTP_DOLAPIENTITY' ]);
2017-06-25 01:27:41 +02:00
2015-11-22 17:17:06 +01:00
$res = 0 ;
if ( ! $res && file_exists ( " ../main.inc.php " )) $res = include '../main.inc.php' ;
if ( ! $res ) die ( " Include of main fails " );
require_once DOL_DOCUMENT_ROOT . '/includes/restler/framework/Luracast/Restler/AutoLoader.php' ;
call_user_func ( function () {
$loader = Luracast\Restler\AutoLoader :: instance ();
spl_autoload_register ( $loader );
return $loader ;
});
require_once DOL_DOCUMENT_ROOT . '/api/class/api.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/api/class/api_access.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2019-11-02 13:52:24 +01:00
$url = $_SERVER [ 'PHP_SELF' ];
// Fix for some NGINX setups (this should not be required even with NGINX, however setup of NGINX are often mysterious and this may help is such cases)
if ( ! empty ( $conf -> global -> MAIN_NGINX_FIX ))
{
$url = ( isset ( $_SERVER [ 'SCRIPT_URI' ]) && $_SERVER [ " SCRIPT_URI " ] !== null ) ? $_SERVER [ " SCRIPT_URI " ] : $_SERVER [ 'PHP_SELF' ];
}
2019-11-02 13:51:45 +01:00
2015-11-22 17:17:06 +01:00
// Enable and test if module Api is enabled
if ( empty ( $conf -> global -> MAIN_MODULE_API ))
{
$langs -> load ( " admin " );
dol_syslog ( " Call Dolibarr API interfaces with module REST disabled " );
2019-01-27 11:55:16 +01:00
print $langs -> trans ( " WarningModuleNotActive " , 'Api' ) . '.<br><br>' ;
2015-11-22 17:17:06 +01:00
print $langs -> trans ( " ToActivateModule " );
exit ;
}
2017-01-22 20:55:26 +01:00
// Test if explorer is not disabled
2019-10-10 15:18:24 +02:00
if ( preg_match ( '/api\/index\.php\/explorer/' , $url ) && ! empty ( $conf -> global -> API_EXPLORER_DISABLED ))
2017-01-22 20:55:26 +01:00
{
$langs -> load ( " admin " );
dol_syslog ( " Call Dolibarr API interfaces with module REST disabled " );
print $langs -> trans ( " WarningAPIExplorerDisabled " ) . '.<br><br>' ;
exit ;
}
2018-01-14 15:58:04 +01:00
// This 2 lines are usefull only if we want to exclude some Urls from the explorer
//use Luracast\Restler\Explorer;
//Explorer::$excludedPaths = array('/categories');
2016-12-12 15:19:47 +01:00
2015-11-22 17:17:06 +01:00
2017-06-25 17:09:08 +02:00
// Analyze URLs
// index.php/explorer do a redirect to index.php/explorer/
// index.php/explorer/ called by swagger to build explorer page
// index.php/explorer/.../....png|.css|.js called by swagger for resources to build explorer page
// index.php/explorer/resources.json called by swagger to get list of all services
// index.php/explorer/resources.json/xxx called by swagger to get detail of services xxx
// index.php/xxx called by any REST client to run API
2019-07-26 13:16:49 +02:00
$reg = array ();
2019-10-10 15:18:24 +02:00
preg_match ( '/index\.php\/([^\/]+)(.*)$/' , $url , $reg );
2017-06-25 17:09:08 +02:00
// .../index.php/categories?sortfield=t.rowid&sortorder=ASC
2019-07-26 13:23:42 +02:00
// When in production mode, a file api/temp/routes.php is created with the API available of current call.
// But, if we set $refreshcache to false, so it may have only one API in the routes.php file if we make a call for one API without
// using the explorer. And when we make another call for another API, the API is not into the api/temp/routes.php and a 404 is returned.
// So we force refresh to each call.
$refreshcache = ( empty ( $conf -> global -> API_PRODUCTION_DO_NOT_ALWAYS_REFRESH_CACHE ) ? true : false );
2017-11-17 12:46:30 +01:00
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' ))
2017-06-25 18:18:17 +02:00
{
$refreshcache = true ;
2019-07-26 13:23:42 +02:00
}
2017-06-25 18:18:17 +02:00
$api = new DolibarrApi ( $db , '' , $refreshcache );
//var_dump($api->r->apiVersionMap);
2015-11-22 17:17:06 +01:00
2016-06-13 14:24:09 +02:00
// Enable the Restler API Explorer.
// See https://github.com/Luracast/Restler-API-Explorer for more info.
$api -> r -> addAPIClass ( 'Luracast\\Restler\\Explorer' );
2017-11-17 12:46:30 +01:00
$api -> r -> setSupportedFormats ( 'JsonFormat' , 'XmlFormat' , 'UploadFormat' ); // 'YamlFormat'
2019-01-27 11:55:16 +01:00
$api -> r -> addAuthenticationClass ( 'DolibarrApiAccess' , '' );
2015-11-22 17:17:06 +01:00
2016-12-12 15:19:47 +01:00
// Define accepted mime types
UploadFormat :: $allowedMimeTypes = array ( 'image/jpeg' , 'image/png' , 'text/plain' , 'application/octet-stream' );
2015-11-22 17:17:06 +01:00
2019-08-25 16:40:21 +02:00
// Restrict API to some IPs
if ( ! empty ( $conf -> global -> API_RESTRICT_ON_IP ))
{
$allowedip = explode ( ' ' , $conf -> global -> API_RESTRICT_ON_IP );
$ipremote = getUserRemoteIP ();
if ( ! in_array ( $ipremote , $allowedip ))
{
dol_syslog ( 'Remote ip is ' . $ipremote . ', not into list ' . $conf -> global -> API_RESTRICT_ON_IP );
print 'APIs are not allowed from the IP ' . $ipremote ;
header ( 'HTTP/1.1 503 API not allowed from your IP ' . $ipremote );
//print $conf->global->API_RESTRICT_ON_IP;
exit ( 0 );
}
}
2017-06-25 18:18:17 +02:00
2019-07-26 13:16:49 +02:00
// Call Explorer file for all APIs definitions (this part is slow)
2017-11-17 12:46:30 +01:00
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' ))
2015-11-22 17:17:06 +01:00
{
2017-06-25 17:09:08 +02:00
// Scan all API files to load them
2015-11-22 17:17:06 +01:00
2017-06-25 17:09:08 +02:00
$listofapis = array ();
2015-11-22 17:17:06 +01:00
2017-06-25 17:09:08 +02:00
$modulesdir = dolGetModulesDirs ();
foreach ( $modulesdir as $dir )
2015-11-22 17:17:06 +01:00
{
2017-06-25 17:09:08 +02:00
// Search available module
2017-11-02 01:16:42 +01:00
dol_syslog ( " Scan directory " . $dir . " for module descriptor files, then search for API files " );
2017-06-25 17:09:08 +02:00
$handle =@ opendir ( dol_osencode ( $dir ));
if ( is_resource ( $handle ))
2015-11-22 17:17:06 +01:00
{
2017-06-25 17:09:08 +02:00
while (( $file = readdir ( $handle )) !== false )
2015-11-22 17:17:06 +01:00
{
2019-08-25 16:40:21 +02:00
$regmod = array ();
2019-01-27 11:55:16 +01:00
if ( is_readable ( $dir . $file ) && preg_match ( " /^mod(.*) \ .class \ .php $ /i " , $file , $regmod ))
2017-06-25 17:09:08 +02:00
{
2017-07-05 11:12:48 +02:00
$module = strtolower ( $regmod [ 1 ]);
2017-06-25 17:09:08 +02:00
$moduledirforclass = getModuleDirForApiClass ( $module );
2017-10-30 12:40:12 +01:00
$modulenameforenabled = $module ;
2017-11-02 01:16:42 +01:00
if ( $module == 'propale' ) { $modulenameforenabled = 'propal' ; }
2017-11-06 11:06:31 +01:00
if ( $module == 'supplierproposal' ) { $modulenameforenabled = 'supplier_proposal' ; }
2018-02-03 20:58:26 +01:00
if ( $module == 'ficheinter' ) { $modulenameforenabled = 'ficheinter' ; }
2016-12-12 15:19:47 +01:00
2017-11-06 11:06:31 +01:00
dol_syslog ( " Found module file " . $file . " - module= " . $module . " - modulenameforenabled= " . $modulenameforenabled . " - moduledirforclass= " . $moduledirforclass );
2016-12-12 15:19:47 +01:00
2017-06-25 17:09:08 +02:00
// Defined if module is enabled
$enabled = true ;
2017-11-02 01:16:42 +01:00
if ( empty ( $conf -> $modulenameforenabled -> enabled )) $enabled = false ;
2015-11-22 17:17:06 +01:00
2017-06-25 17:09:08 +02:00
if ( $enabled )
2015-11-22 17:17:06 +01:00
{
2017-06-25 17:09:08 +02:00
// If exists, load the API class for enable module
// Search files named api_<object>.class.php into /htdocs/<module>/class directory
// @todo : use getElementProperties() function ?
$dir_part = dol_buildpath ( '/' . $moduledirforclass . '/class/' );
2016-12-12 15:19:47 +01:00
2017-06-25 17:09:08 +02:00
$handle_part =@ opendir ( dol_osencode ( $dir_part ));
if ( is_resource ( $handle_part ))
{
while (( $file_searched = readdir ( $handle_part )) !== false )
2015-11-22 17:17:06 +01:00
{
2017-06-25 17:09:08 +02:00
if ( $file_searched == 'api_access.class.php' ) continue ;
2019-08-25 16:40:21 +02:00
$regapi = array ();
2019-01-27 11:55:16 +01:00
if ( is_readable ( $dir_part . $file_searched ) && preg_match ( " /^api_(.*) \ .class \ .php $ /i " , $file_searched , $regapi ))
2016-12-05 13:31:29 +01:00
{
2017-07-05 11:12:48 +02:00
$classname = ucwords ( $regapi [ 1 ]);
2017-06-25 17:09:08 +02:00
$classname = str_replace ( '_' , '' , $classname );
require_once $dir_part . $file_searched ;
2017-11-05 18:15:59 +01:00
if ( class_exists ( $classname . 'Api' ))
{
2017-11-06 11:14:16 +01:00
//dol_syslog("Found API by index.php: classname=".$classname."Api for module ".$dir." into ".$dir_part.$file_searched);
2017-11-05 18:15:59 +01:00
$listofapis [ strtolower ( $classname . 'Api' )] = $classname . 'Api' ;
}
elseif ( class_exists ( $classname ))
2017-06-25 17:09:08 +02:00
{
2017-11-06 11:14:16 +01:00
//dol_syslog("Found API by index.php: classname=".$classname." for module ".$dir." into ".$dir_part.$file_searched);
2017-11-05 18:15:59 +01:00
$listofapis [ strtolower ( $classname )] = $classname ;
2017-06-25 17:09:08 +02:00
}
else
{
dol_syslog ( " We found an api_xxx file ( " . $file_searched . " ) but class " . $classname . " does not exists after loading file " , LOG_WARNING );
}
2016-12-05 13:31:29 +01:00
}
2015-11-22 17:17:06 +01:00
}
}
}
}
}
}
}
2017-06-25 17:09:08 +02:00
2017-06-25 18:18:17 +02:00
// Sort the classes before adding them to Restler.
// The Restler API Explorer shows the classes in the order they are added and it's a mess if they are not sorted.
2017-11-05 18:15:59 +01:00
asort ( $listofapis );
foreach ( $listofapis as $apiname => $classname )
2017-06-25 17:09:08 +02:00
{
2017-11-05 18:15:59 +01:00
$api -> r -> addAPIClass ( $classname , $apiname );
2017-06-25 17:09:08 +02:00
}
2017-11-17 12:46:30 +01:00
//var_dump($api->r);
2015-11-22 17:17:06 +01:00
}
2017-06-25 17:09:08 +02:00
// Call one APIs or one definition of an API
2019-08-25 16:40:21 +02:00
$regbis = array ();
2017-11-17 12:46:30 +01:00
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' )))
2016-06-15 22:53:05 +02:00
{
2017-06-25 17:09:08 +02:00
$module = $reg [ 1 ];
if ( $module == 'explorer' ) // If we call page to explore details of a service
{
2017-11-17 12:46:30 +01:00
$module = $regbis [ 2 ];
2017-06-25 17:09:08 +02:00
}
$module = strtolower ( $module );
$moduledirforclass = getModuleDirForApiClass ( $module );
2017-11-05 18:15:59 +01:00
// Load a dedicated API file
2018-02-03 20:58:26 +01:00
dol_syslog ( " Load a dedicated API file module= " . $module . " moduledirforclass= " . $moduledirforclass );
2017-11-05 18:15:59 +01:00
2017-11-25 13:14:28 +01:00
$tmpmodule = $module ;
if ( $tmpmodule != 'api' )
$tmpmodule = preg_replace ( '/api$/i' , '' , $tmpmodule );
$classfile = str_replace ( '_' , '' , $tmpmodule );
if ( $module == 'supplierproposals' )
$classfile = 'supplier_proposals' ;
if ( $module == 'supplierorders' )
$classfile = 'supplier_orders' ;
if ( $module == 'supplierinvoices' )
$classfile = 'supplier_invoices' ;
2018-02-03 20:58:26 +01:00
if ( $module == 'ficheinter' )
$classfile = 'interventions' ;
if ( $module == 'interventions' )
$classfile = 'interventions' ;
2017-11-25 13:14:28 +01:00
$dir_part_file = dol_buildpath ( '/' . $moduledirforclass . '/class/api_' . $classfile . '.class.php' , 0 , 2 );
$classname = ucwords ( $module );
2019-05-02 12:19:43 +02:00
dol_syslog ( 'Search api file /' . $moduledirforclass . '/class/api_' . $classfile . '.class.php => dir_part_file=' . $dir_part_file . ' classname=' . $classname );
2017-11-25 13:14:28 +01:00
$res = false ;
if ( $dir_part_file )
$res = include_once $dir_part_file ;
if ( ! $res ) {
2019-05-02 12:19:43 +02:00
dol_syslog ( 'Failed to make include_once ' . $dir_part_file , LOG_WARNING );
2017-11-25 13:14:28 +01:00
print 'API not found (failed to include API file)' ;
header ( 'HTTP/1.1 501 API not found (failed to include API file)' );
exit ( 0 );
}
if ( class_exists ( $classname ))
$api -> r -> addAPIClass ( $classname );
2016-06-15 22:53:05 +02:00
}
2015-11-22 17:17:06 +01:00
2017-06-25 18:18:17 +02:00
//var_dump($api->r->apiVersionMap);
2017-06-25 17:09:08 +02:00
//exit;
2016-04-30 00:00:59 +02:00
2019-07-26 13:16:49 +02:00
// Call API (we suppose we found it).
// The handle will use the file api/temp/routes.php to get data to run the API. If the file exists and the entry for API is not found, it will return 404.
2015-11-22 17:17:06 +01:00
$api -> r -> handle ();