ldapsaisie/src/includes/class/class.LSurl.php

381 lines
13 KiB
PHP
Raw Normal View History

<?php
/*******************************************************************************
* Copyright (C) 2007 Easter-eggs
* https://ldapsaisie.org
*
* Author: See AUTHORS file in top-level directory.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******************************************************************************/
LSsession :: loadLSclass('LSurlRequest');
LSsession :: loadLSclass('LSlog_staticLoggerClass');
/**
* URL Routing Manager for LdapSaisie
*
* @author Benjamin Renard <brenard@easter-eggs.com>
*/
class LSurl extends LSlog_staticLoggerClass {
// Current request (defined at least current URL have been analyse by LSurl :: handle_request())
public static $request = null;
/*
* Configured URL patterns :
*
* Example :
*
* array (
* '|get/(?P<name>[a-zA-Z0-9]+)$|' => array (
* 'handler' => 'get',
* 'authenticated' => true,
* 'api_mode' => false,
* 'methods' => array('GET'),
* ),
* '|get/all$|' => => array (
* 'handler' => 'get_all',
* 'authenticated' => true,
* 'api_mode' => false,
* 'methods' => array('GET', 'POST'),
* ),
* )
*
*/
private static $patterns = array();
/**
* Add an URL pattern
*
* @param[in] $pattern string The URL pattern (required)
* @param[in] $handler callable The URL pattern handler (must be callable, required)
* @param[in] $authenticated boolean Permit to define if this URL is accessible only for authenticated users (optional, default: true)
* @param[in] $override boolean Allow override if a command already exists with the same name (optional, default: false)
Add API feature Some major changes have been made to handle this new feature : - LSsession now have a flag about API mode. The displayTemplate() and displayAjaxReturn() methods have been adjust to correctly handle this mode. - LSauth system have been adjust to handle a custom API mode : - LSauthMethod can support or not this mode : the $api_mode_supported permit to defined if supported (default, false). Currently, only HTTP (default in API mode) and annonymous mode support it. - An api_access parameter permit to configure witch type of user LSobject types could use the API. This flag must be set to True to allow a type of LSobject (default: False). In a same way, a web_access parameter now permit to disable Web access for some types of users (but this parameter is optional and its default value is True). - The HTTP method is the privileged first method for API mode. In this mode, if auth data aren't present in environment, it will request it by triggered a 403 HTTP error. Realm can be configured with new LSAUTHMETHOD_HTTP_API_REALM constant. - The LStemplate system handle API mode to correctly react on errors: it return a JSON answer instead of HTML page. Error pages also now return adjusted HTTP code (404 or 500). - The LSurl system have been adjust to handle API mode : - On declaring handlers, we could now specify if it's an API view with new $api_mode paremeter of add_handler() method - The LSurlRequest object have a new attribute to check if it's an API request - The error_404() method handle the API mode to return JSON answer. Furthermore, if no handlers matched with the requested URL, API mode is automatically enabled if the requested URL starts with 'api/'. - LSform implement it own API mode flag and a new submited flag that be toggle via the new setSubmited() method. Some major changes also occured on LSformElement classes to specifically handle API input/output for each types of attributes: - a new getApiValue() method permit to retrieve the API value of the attribute (on show API view) - the getPostData() method now have to correctly handle API input for the attribute (on create/modify API views). A programmatic way have been adopted for each types of attributes. - The LSimport and LScli create/modify commands also evolved to enable API mode of the LSform. This permit to take advantage of the new capability of LSform/LSformElement to handle input values with a programmatic way. - New routes have been add to handle API views. All this new routes start with 'api/1.0/' and use the same URL schema as the web UI. The API currently permit to search/show/add/modify/remove LSobjects and manages their relations.
2021-02-03 14:40:28 +01:00
* @param[in] $api_mode boolean Enable API mode (optional, default: false)
* @param[in] $methods array|null HTTP method (optional, default: array('GET', 'POST'))
**/
public static function add_handler($pattern, $handler=null, $authenticated=true, $override=true, $api_mode=false, $methods=null) {
if (is_null($methods))
$methods = array('GET', 'POST');
else
$methods = ensureIsArray($methods);
if (is_array($pattern)) {
if (is_null($handler))
foreach($pattern as $p => $h)
self :: add_handler($p, $h, $authenticated, $override, $api_mode, $methods);
else
foreach($pattern as $p)
self :: add_handler($p, $handler, $authenticated, $override, $api_mode, $methods);
}
else {
if (!isset(self :: $patterns[$pattern])) {
self :: $patterns[$pattern] = array(
'handler' => $handler,
'authenticated' => $authenticated,
Add API feature Some major changes have been made to handle this new feature : - LSsession now have a flag about API mode. The displayTemplate() and displayAjaxReturn() methods have been adjust to correctly handle this mode. - LSauth system have been adjust to handle a custom API mode : - LSauthMethod can support or not this mode : the $api_mode_supported permit to defined if supported (default, false). Currently, only HTTP (default in API mode) and annonymous mode support it. - An api_access parameter permit to configure witch type of user LSobject types could use the API. This flag must be set to True to allow a type of LSobject (default: False). In a same way, a web_access parameter now permit to disable Web access for some types of users (but this parameter is optional and its default value is True). - The HTTP method is the privileged first method for API mode. In this mode, if auth data aren't present in environment, it will request it by triggered a 403 HTTP error. Realm can be configured with new LSAUTHMETHOD_HTTP_API_REALM constant. - The LStemplate system handle API mode to correctly react on errors: it return a JSON answer instead of HTML page. Error pages also now return adjusted HTTP code (404 or 500). - The LSurl system have been adjust to handle API mode : - On declaring handlers, we could now specify if it's an API view with new $api_mode paremeter of add_handler() method - The LSurlRequest object have a new attribute to check if it's an API request - The error_404() method handle the API mode to return JSON answer. Furthermore, if no handlers matched with the requested URL, API mode is automatically enabled if the requested URL starts with 'api/'. - LSform implement it own API mode flag and a new submited flag that be toggle via the new setSubmited() method. Some major changes also occured on LSformElement classes to specifically handle API input/output for each types of attributes: - a new getApiValue() method permit to retrieve the API value of the attribute (on show API view) - the getPostData() method now have to correctly handle API input for the attribute (on create/modify API views). A programmatic way have been adopted for each types of attributes. - The LSimport and LScli create/modify commands also evolved to enable API mode of the LSform. This permit to take advantage of the new capability of LSform/LSformElement to handle input values with a programmatic way. - New routes have been add to handle API views. All this new routes start with 'api/1.0/' and use the same URL schema as the web UI. The API currently permit to search/show/add/modify/remove LSobjects and manages their relations.
2021-02-03 14:40:28 +01:00
'api_mode' => $api_mode,
'methods' => $methods,
);
}
elseif ($override) {
2020-05-08 15:16:24 +02:00
self :: log_debug("URL : override pattern '$pattern' with handler '$handler' (old handler = '".self :: $patterns[$pattern]."')");
self :: $patterns[$pattern] = array(
'handler' => $handler,
'authenticated' => $authenticated,
Add API feature Some major changes have been made to handle this new feature : - LSsession now have a flag about API mode. The displayTemplate() and displayAjaxReturn() methods have been adjust to correctly handle this mode. - LSauth system have been adjust to handle a custom API mode : - LSauthMethod can support or not this mode : the $api_mode_supported permit to defined if supported (default, false). Currently, only HTTP (default in API mode) and annonymous mode support it. - An api_access parameter permit to configure witch type of user LSobject types could use the API. This flag must be set to True to allow a type of LSobject (default: False). In a same way, a web_access parameter now permit to disable Web access for some types of users (but this parameter is optional and its default value is True). - The HTTP method is the privileged first method for API mode. In this mode, if auth data aren't present in environment, it will request it by triggered a 403 HTTP error. Realm can be configured with new LSAUTHMETHOD_HTTP_API_REALM constant. - The LStemplate system handle API mode to correctly react on errors: it return a JSON answer instead of HTML page. Error pages also now return adjusted HTTP code (404 or 500). - The LSurl system have been adjust to handle API mode : - On declaring handlers, we could now specify if it's an API view with new $api_mode paremeter of add_handler() method - The LSurlRequest object have a new attribute to check if it's an API request - The error_404() method handle the API mode to return JSON answer. Furthermore, if no handlers matched with the requested URL, API mode is automatically enabled if the requested URL starts with 'api/'. - LSform implement it own API mode flag and a new submited flag that be toggle via the new setSubmited() method. Some major changes also occured on LSformElement classes to specifically handle API input/output for each types of attributes: - a new getApiValue() method permit to retrieve the API value of the attribute (on show API view) - the getPostData() method now have to correctly handle API input for the attribute (on create/modify API views). A programmatic way have been adopted for each types of attributes. - The LSimport and LScli create/modify commands also evolved to enable API mode of the LSform. This permit to take advantage of the new capability of LSform/LSformElement to handle input values with a programmatic way. - New routes have been add to handle API views. All this new routes start with 'api/1.0/' and use the same URL schema as the web UI. The API currently permit to search/show/add/modify/remove LSobjects and manages their relations.
2021-02-03 14:40:28 +01:00
'api_mode' => $api_mode,
'methods' => $methods,
);
}
else {
2020-05-08 15:16:24 +02:00
self :: log_debug("URL : pattern '$pattern' already defined : do not override.");
}
}
}
/**
* Interprets the requested URL and return the corresponding LSurlRequest object
*
* @param[in] $default_url string|null The default URL if current one does not
* match with any configured pattern.
*
* @retval LSurlRequest The LSurlRequest object corresponding to the the requested URL.
**/
private static function get_request($default_url=null) {
$current_url = self :: get_current_url();
if (is_null($current_url)) {
2020-05-08 15:16:24 +02:00
self :: log_fatal(_("Fail to determine the requested URL."));
exit();
}
if (!is_array(self :: $patterns)) {
self :: log_fatal(_("No URL patterns configured !"));
exit();
}
2020-05-08 15:16:24 +02:00
self :: log_debug("URL : current url = '$current_url'");
self :: log_debug("URL : check current url with the following URL patterns :\n - ".implode("\n - ", array_keys(self :: $patterns)));
foreach (self :: $patterns as $pattern => $handler_infos) {
$m = self :: url_match($pattern, $current_url, $handler_infos['methods']);
if (is_array($m)) {
$request = new LSurlRequest($current_url, $handler_infos, $m);
// Reset last redirect
if (isset($_SESSION['last_redirect']))
unset($_SESSION['last_redirect']);
2020-05-08 15:16:24 +02:00
self :: log_debug("URL : result :\n".varDump($request, 1));
return $request;
}
}
if (!is_null($default_url)) {
2020-05-08 15:16:24 +02:00
self :: log_debug("Current URL match with no pattern. Redirect to default URL ('$default_url')");
self :: redirect($default_url);
exit();
}
Add API feature Some major changes have been made to handle this new feature : - LSsession now have a flag about API mode. The displayTemplate() and displayAjaxReturn() methods have been adjust to correctly handle this mode. - LSauth system have been adjust to handle a custom API mode : - LSauthMethod can support or not this mode : the $api_mode_supported permit to defined if supported (default, false). Currently, only HTTP (default in API mode) and annonymous mode support it. - An api_access parameter permit to configure witch type of user LSobject types could use the API. This flag must be set to True to allow a type of LSobject (default: False). In a same way, a web_access parameter now permit to disable Web access for some types of users (but this parameter is optional and its default value is True). - The HTTP method is the privileged first method for API mode. In this mode, if auth data aren't present in environment, it will request it by triggered a 403 HTTP error. Realm can be configured with new LSAUTHMETHOD_HTTP_API_REALM constant. - The LStemplate system handle API mode to correctly react on errors: it return a JSON answer instead of HTML page. Error pages also now return adjusted HTTP code (404 or 500). - The LSurl system have been adjust to handle API mode : - On declaring handlers, we could now specify if it's an API view with new $api_mode paremeter of add_handler() method - The LSurlRequest object have a new attribute to check if it's an API request - The error_404() method handle the API mode to return JSON answer. Furthermore, if no handlers matched with the requested URL, API mode is automatically enabled if the requested URL starts with 'api/'. - LSform implement it own API mode flag and a new submited flag that be toggle via the new setSubmited() method. Some major changes also occured on LSformElement classes to specifically handle API input/output for each types of attributes: - a new getApiValue() method permit to retrieve the API value of the attribute (on show API view) - the getPostData() method now have to correctly handle API input for the attribute (on create/modify API views). A programmatic way have been adopted for each types of attributes. - The LSimport and LScli create/modify commands also evolved to enable API mode of the LSform. This permit to take advantage of the new capability of LSform/LSformElement to handle input values with a programmatic way. - New routes have been add to handle API views. All this new routes start with 'api/1.0/' and use the same URL schema as the web UI. The API currently permit to search/show/add/modify/remove LSobjects and manages their relations.
2021-02-03 14:40:28 +01:00
// Error 404
$api_mode = (strpos($current_url, 'api/') === 0);
self :: log_debug("Current URL match with no pattern. Use error 404 handler (API mode=$api_mode).");
return new LSurlRequest(
$current_url,
array(
'handler' => array('LSurl', 'error_404'),
'authenticated' => false,
Add API feature Some major changes have been made to handle this new feature : - LSsession now have a flag about API mode. The displayTemplate() and displayAjaxReturn() methods have been adjust to correctly handle this mode. - LSauth system have been adjust to handle a custom API mode : - LSauthMethod can support or not this mode : the $api_mode_supported permit to defined if supported (default, false). Currently, only HTTP (default in API mode) and annonymous mode support it. - An api_access parameter permit to configure witch type of user LSobject types could use the API. This flag must be set to True to allow a type of LSobject (default: False). In a same way, a web_access parameter now permit to disable Web access for some types of users (but this parameter is optional and its default value is True). - The HTTP method is the privileged first method for API mode. In this mode, if auth data aren't present in environment, it will request it by triggered a 403 HTTP error. Realm can be configured with new LSAUTHMETHOD_HTTP_API_REALM constant. - The LStemplate system handle API mode to correctly react on errors: it return a JSON answer instead of HTML page. Error pages also now return adjusted HTTP code (404 or 500). - The LSurl system have been adjust to handle API mode : - On declaring handlers, we could now specify if it's an API view with new $api_mode paremeter of add_handler() method - The LSurlRequest object have a new attribute to check if it's an API request - The error_404() method handle the API mode to return JSON answer. Furthermore, if no handlers matched with the requested URL, API mode is automatically enabled if the requested URL starts with 'api/'. - LSform implement it own API mode flag and a new submited flag that be toggle via the new setSubmited() method. Some major changes also occured on LSformElement classes to specifically handle API input/output for each types of attributes: - a new getApiValue() method permit to retrieve the API value of the attribute (on show API view) - the getPostData() method now have to correctly handle API input for the attribute (on create/modify API views). A programmatic way have been adopted for each types of attributes. - The LSimport and LScli create/modify commands also evolved to enable API mode of the LSform. This permit to take advantage of the new capability of LSform/LSformElement to handle input values with a programmatic way. - New routes have been add to handle API views. All this new routes start with 'api/1.0/' and use the same URL schema as the web UI. The API currently permit to search/show/add/modify/remove LSobjects and manages their relations.
2021-02-03 14:40:28 +01:00
'api_mode' => $api_mode,
)
);
}
/**
* Check if the current requested URL match with a specific pattern
*
* @param[in] $pattern string The URL pattern
* @param[in] $current_url string|false The current URL (optional)
* @param[in] $methods array|null HTTP method (optional, default: no check)
*
* @retval array|false The URL info if pattern matched, false otherwise.
**/
private static function url_match($pattern, $current_url=false, $methods=null) {
if ($methods && !in_array($_SERVER['REQUEST_METHOD'], $methods))
return false;
if ($current_url === false) {
$current_url = self :: get_current_url();
if (!$current_url) return False;
}
if (preg_match($pattern, $current_url, $m)) {
2020-05-08 15:16:24 +02:00
self :: log_debug("URL : Match found with pattern '$pattern' :\n\t".str_replace("\n", "\n\t", print_r($m, 1)));
return $m;
}
return False;
}
/**
* Get the public absolute URL
*
* @param[in] $relative_url string|null Relative URL to convert (Default: current URL)
*
* @retval string The public absolute URL
**/
public static function get_public_absolute_url($relative_url=null) {
if (!is_string($relative_url))
$relative_url = self :: get_current_url();
$public_root_url = LSconfig :: get('public_root_url', '/', 'string');
if ($public_root_url[0] == '/') {
2020-05-08 15:16:24 +02:00
self :: log_debug("LSurl :: get_public_absolute_url($relative_url): configured public root URL is relative ($public_root_url) => try to detect it from current request infos.");
$public_root_url = 'http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'?'s':'').'://'.$_SERVER['HTTP_HOST'].$public_root_url;
2020-05-08 15:16:24 +02:00
self :: log_debug("LSurl :: get_public_absolute_url($relative_url): detected public_root_url: $public_root_url");
}
$url = self :: remove_trailing_slash($public_root_url)."/$relative_url";
2020-05-08 15:16:24 +02:00
self :: log_debug("LSurl :: get_public_absolute_url($relative_url): result = $url");
return $url;
}
/**
* Trigger redirect to specified URL (or homepage if omited)
*
* @param[in] $go string|false The destination URL
*
* @retval void
**/
public static function redirect($go=false) {
$public_root_url = LSconfig :: get('public_root_url', '/', 'string');
if ($go===false)
$go = "";
if (preg_match('#^(https?:)?//#',$go)) {
$url = $go;
}
else {
// Check $public_root_url end
if (substr($public_root_url, -1)=='/') {
$public_root_url=substr($public_root_url, 0, -1);
}
$url="$public_root_url/$go";
}
// Prevent loop
if (isset($_SESSION['last_redirect']) && $_SESSION['last_redirect'] == $url)
2020-05-08 15:16:24 +02:00
self :: log_fatal(_("Fail to determine the requested URL (loop detected)."));
else
$_SESSION['last_redirect'] = $url;
2020-05-08 15:16:24 +02:00
self :: log_debug("redirect($go) => Redirect to : <$url>");
header("Location: $url");
// Set & display template
LStemplate :: assign('url', $url);
LStemplate :: display('redirect.tpl');
exit();
}
/**
* Error 404 handler
*
* @param[in] $request LSurlRequest|null The request (optional, default: null)
*
* @retval void
**/
public static function error_404($request=null) {
Add API feature Some major changes have been made to handle this new feature : - LSsession now have a flag about API mode. The displayTemplate() and displayAjaxReturn() methods have been adjust to correctly handle this mode. - LSauth system have been adjust to handle a custom API mode : - LSauthMethod can support or not this mode : the $api_mode_supported permit to defined if supported (default, false). Currently, only HTTP (default in API mode) and annonymous mode support it. - An api_access parameter permit to configure witch type of user LSobject types could use the API. This flag must be set to True to allow a type of LSobject (default: False). In a same way, a web_access parameter now permit to disable Web access for some types of users (but this parameter is optional and its default value is True). - The HTTP method is the privileged first method for API mode. In this mode, if auth data aren't present in environment, it will request it by triggered a 403 HTTP error. Realm can be configured with new LSAUTHMETHOD_HTTP_API_REALM constant. - The LStemplate system handle API mode to correctly react on errors: it return a JSON answer instead of HTML page. Error pages also now return adjusted HTTP code (404 or 500). - The LSurl system have been adjust to handle API mode : - On declaring handlers, we could now specify if it's an API view with new $api_mode paremeter of add_handler() method - The LSurlRequest object have a new attribute to check if it's an API request - The error_404() method handle the API mode to return JSON answer. Furthermore, if no handlers matched with the requested URL, API mode is automatically enabled if the requested URL starts with 'api/'. - LSform implement it own API mode flag and a new submited flag that be toggle via the new setSubmited() method. Some major changes also occured on LSformElement classes to specifically handle API input/output for each types of attributes: - a new getApiValue() method permit to retrieve the API value of the attribute (on show API view) - the getPostData() method now have to correctly handle API input for the attribute (on create/modify API views). A programmatic way have been adopted for each types of attributes. - The LSimport and LScli create/modify commands also evolved to enable API mode of the LSform. This permit to take advantage of the new capability of LSform/LSformElement to handle input values with a programmatic way. - New routes have been add to handle API views. All this new routes start with 'api/1.0/' and use the same URL schema as the web UI. The API currently permit to search/show/add/modify/remove LSobjects and manages their relations.
2021-02-03 14:40:28 +01:00
http_response_code(404);
$error = _("The requested page was not found.");
if (LSsession :: getAjaxDisplay() || ($request && $request->api_mode)) {
LSerror :: addErrorCode(null, $error);
LSsession :: displayAjaxReturn();
}
else {
LStemplate :: assign('error', $error);
LSsession :: setTemplate('error.tpl');
LSsession :: displayTemplate();
}
}
/**
* Handle the current requested URL
*
* @param[in] $default_url string|null The default URL if current one does not
* match with any configured pattern.
*
* @retval void
**/
public static function handle_request($default_url=null) {
self :: $request = self :: get_request($default_url);
if (!is_callable(self :: $request -> handler)) {
self :: log_error(
"URL handler function ".self :: $request -> handler."() does not exists !"
);
self :: log_fatal(_("This request could not be handled."));
}
if (self :: $request -> api_mode) {
Add API feature Some major changes have been made to handle this new feature : - LSsession now have a flag about API mode. The displayTemplate() and displayAjaxReturn() methods have been adjust to correctly handle this mode. - LSauth system have been adjust to handle a custom API mode : - LSauthMethod can support or not this mode : the $api_mode_supported permit to defined if supported (default, false). Currently, only HTTP (default in API mode) and annonymous mode support it. - An api_access parameter permit to configure witch type of user LSobject types could use the API. This flag must be set to True to allow a type of LSobject (default: False). In a same way, a web_access parameter now permit to disable Web access for some types of users (but this parameter is optional and its default value is True). - The HTTP method is the privileged first method for API mode. In this mode, if auth data aren't present in environment, it will request it by triggered a 403 HTTP error. Realm can be configured with new LSAUTHMETHOD_HTTP_API_REALM constant. - The LStemplate system handle API mode to correctly react on errors: it return a JSON answer instead of HTML page. Error pages also now return adjusted HTTP code (404 or 500). - The LSurl system have been adjust to handle API mode : - On declaring handlers, we could now specify if it's an API view with new $api_mode paremeter of add_handler() method - The LSurlRequest object have a new attribute to check if it's an API request - The error_404() method handle the API mode to return JSON answer. Furthermore, if no handlers matched with the requested URL, API mode is automatically enabled if the requested URL starts with 'api/'. - LSform implement it own API mode flag and a new submited flag that be toggle via the new setSubmited() method. Some major changes also occured on LSformElement classes to specifically handle API input/output for each types of attributes: - a new getApiValue() method permit to retrieve the API value of the attribute (on show API view) - the getPostData() method now have to correctly handle API input for the attribute (on create/modify API views). A programmatic way have been adopted for each types of attributes. - The LSimport and LScli create/modify commands also evolved to enable API mode of the LSform. This permit to take advantage of the new capability of LSform/LSformElement to handle input values with a programmatic way. - New routes have been add to handle API views. All this new routes start with 'api/1.0/' and use the same URL schema as the web UI. The API currently permit to search/show/add/modify/remove LSobjects and manages their relations.
2021-02-03 14:40:28 +01:00
LSsession :: setApiMode();
}
else {
if (self :: $request -> ajax)
LSsession :: setAjaxDisplay();
if (class_exists('LStemplate'))
LStemplate :: assign('request', self :: $request);
}
// Check authentication (if need)
if(self :: $request -> authenticated && ! LSsession :: startLSsession()) {
LSsession :: displayTemplate();
return true;
}
try {
return call_user_func(self :: $request -> handler, self :: $request);
}
catch (Exception $e) {
self :: log_exception(
$e, "An exception occured running URL handler function ".
getCallableName(self :: $request -> handler)
);
self :: log_fatal(_("This request could not be processed correctly."));
}
}
/**
2021-08-25 18:02:37 +02:00
* Helpers to retrieve current requested URL
*/
/*
* Try to detect current requested URL and return it
*
* @retval string|false The current request URL or false if detection fail
**/
private static function get_current_url() {
2020-05-08 15:16:24 +02:00
self :: log_debug("URL : request URI = '".$_SERVER['REQUEST_URI']."'");
$base = self :: get_rewrite_base();
2020-05-08 15:16:24 +02:00
self :: log_debug("URL : rewrite base = '$base'");
if ($_SERVER['REQUEST_URI'] == $base)
return '';
if (substr($_SERVER['REQUEST_URI'], 0, strlen($base)) != $base) {
2020-05-08 15:16:24 +02:00
self :: log_error("URL : request URI (".$_SERVER['REQUEST_URI'].") does not start with rewrite base ($base)");
return False;
}
$current_url = substr($_SERVER['REQUEST_URI'], strlen($base));
// URL contain params ?
$params_start = strpos($current_url, '?');
if ($params_start !== false) {
// Params detected, remove it
// No url / currrent url start by '?' ?
if ($params_start == 0)
return '';
else
return substr($current_url, 0, $params_start);
}
return $current_url;
}
/**
* Try to detect rewrite base from public root URL
*
* @retval string The detected rewrite base
**/
private static function get_rewrite_base() {
$public_root_url = LSconfig :: get('public_root_url', '/', 'string');
$rewrite_base = '/';
2020-06-25 16:08:49 +02:00
if (preg_match('|^https?://[^/]+(/.*)$|', $public_root_url, $m))
$rewrite_base = $m[1];
2020-06-25 16:08:49 +02:00
elseif (preg_match('|^(/.+)$|', $public_root_url, $m))
$rewrite_base = $m[1];
if ($rewrite_base != '/')
return self :: remove_trailing_slash($rewrite_base).'/';
return $rewrite_base;
}
/**
* Remove trailing slash in specified URL
*
* @param[in] $url string The URL
*
* @retval string The specified URL without trailing slash
**/
private static function remove_trailing_slash($url) {
if ($url == '/')
return $url;
elseif (substr($url, -1) == '/')
return substr($url, 0, -1);
return $url;
}
}