Dolibarr / dolibarr

Dolibarr ERP CRM is a modern software package to manage your company or foundation's activity (contacts, suppliers, invoices, orders, stocks, agenda, accounting, ...). it's an open source Web application (written in PHP) designed for businesses of any sizes, foundations and freelancers.
https://www.dolibarr.org
GNU General Public License v3.0
5.32k stars 2.75k forks source link

Enhance URL Retrieval Code in API Module for Compatibility with Various Server Types (Nginx, Apache, Caddy) #27209

Open amibeldev opened 9 months ago

amibeldev commented 9 months ago

Bug

When I access the URL https://localhost/api/index.php/explorer/

I encounter several dysfunctions:

1 - Display of the warning: "_Warning: Undefined array key 'ORIG_PATHINFO' in /app/htdocs/api/index.php on line 107" 2 - Unable to retrieve the list of actions: "Can't read from server. It may not have the appropriate access-control-origin settings.""

Environment Version

18.0.4

Environment OS

ubuntu

Environment Web server

Caddy

Environment PHP

8.2

Environment Database

mysql

Environment URL(s)

https://localhost:9943/api/index.php/explorer/

Expected and actual behavior

Capture d’écran du 2023-12-22 10-54-46

Steps to reproduce the behavior

No response

Attached files

Capture d’écran du 2023-12-22 10-53-09

amibeldev commented 9 months ago

suggestion

in htdocs/api/index.php

replace current code from line 102:

...
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';

$url = $_SERVER['PHP_SELF'];
if (preg_match('/api\/index\.php$/', $url)) {   // sometimes $_SERVER['PHP_SELF'] is 'api\/index\.php' instead of 'api\/index\.php/explorer.php' or 'api\/index\.php/method'
    $url = $_SERVER['PHP_SELF'].(empty($_SERVER['PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : $_SERVER['PATH_INFO']);
}
// 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'];
}

// Enable and test if module Api is enabled
if (empty($conf->global->MAIN_MODULE_API)) {
...

By :

...
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';

if (preg_match('/api\/index\.php$/', $url)) {
    $pathInfo = $_SERVER['PATH_INFO'] ?? $_SERVER['ORIG_PATH_INFO'] ?? '';
    $url = $_SERVER['PHP_SELF'] . $pathInfo;
}

// Enable and test if module Api is enabled
if (empty($conf->global->MAIN_MODULE_API)) {
....