eesyphp/includes/url.php
2020-04-18 00:51:33 +02:00

246 lines
6.6 KiB
PHP

<?php
$url_patterns =array();
function add_url_handler($pattern, $handler=null, $override=true) {
global $url_patterns;
if (is_array($pattern)) {
if (is_null($handler))
foreach($pattern as $p => $h)
add_url_handler($p, $h, $override);
else
foreach($pattern as $p)
add_url_handler($p, $handler, $override);
}
else {
if (!isset($url_patterns[$pattern])) {
$url_patterns[$pattern] = $handler;
}
elseif ($override) {
logging('DEBUG', "URL : override pattern '$pattern' with handler '$handler' (old handler = '".$url_patterns[$pattern]."')");
$url_patterns[$pattern] = $handler;
}
else {
logging('DEBUG', "URL : pattern '$pattern' already defined : do not override.");
}
}
}
/*
* Error 404 page
*/
function error_404($url_infos=array()) {
display_template('error_404.tpl', "Oups ! Page introuvable");
exit();
}
$_404_url_handler = 'error_404';
function set_404_url_handler($handler=null) {
global $_404_url_handler;
$_404_url_handler = $handler;
}
/*
* URL patterns :
*
* array (
* '[URL pattern]' => '[handler]',
* [...]
* )
*
* Example :
*
* array (
* '#get/(?P<name>[a-zA-Z0-9]+)$#' => 'get_by_name',
* '#get/status/(?P<status>pending|archived)$#' => 'get_by_status',
* '#get/all$#' => 'get_all',
* )
*
* Return example (for the last one previous URL pattern) :
*
* return : array (
* 'handler' => 'get',
* 'current_page' => 'get/[name]',
* 'name' => [name],
* )
*
*/
function get_current_url_handler($url_patterns=null, $default_page=false) {
global $_404_url_handler;
if (is_null($url_patterns))
global $url_patterns;
$current_page = get_current_page();
if ($current_page === false) {
logging('FATAL', 'Impossible de déterminer la page demandée. Si le problème persiste, merci de prendre contact avec le support.');
exit();
}
if (!is_array($url_patterns)) {
logging('FATAL', 'URL : No URL patterns provided !');
exit();
}
logging('DEBUG', "URL : current page = '$current_page'");
logging('DEBUG', "URL : check current page with the following URL patterns :\n - ".implode("\n - ", array_keys($url_patterns)));
foreach ($url_patterns as $pattern => $handler) {
$m = url_match($pattern, $current_page);
if (is_array($m)) {
$m['handler'] = $handler;
$m['current_page'] = $current_page;
// Reset last redirect
if (isset($_SESSION['last_redirect']))
unset($_SESSION['last_redirect']);
logging('DEBUG', "URL : result :\n".print_r($m, 1));
return $m;
}
}
if ($default_page !== false) {
logging('DEBUG', "Current page match with no pattern. Redirect to default page ('$default_page')");
redirect($default_page);
exit();
}
elseif ($_404_url_handler) {
logging('DEBUG', "Current page match with no pattern. Use error 404 handler.");
return array('handler' => $_404_url_handler);
}
return False;
}
function url_match($pattern, $current_page=false) {
if ($current_page === false) {
$current_page = get_current_page();
if (!$current_page) return False;
}
if (preg_match($pattern, $current_page, $m)) {
logging('DEBUG', "URL : Match found with pattern '$pattern' :\n\t".str_replace("\n", "\n\t", print_r($m, 1)));
return $m;
}
return False;
}
function get_current_page() {
if (isset($_REQUEST['go']))
return $_REQUEST['go'];
else
return detect_current_page();
}
function detect_current_page() {
logging('DEBUG', "URL : request URI = '".$_SERVER['REQUEST_URI']."'");
$base = get_rewrite_base();
logging('DEBUG', "URL : rewrite base = '$base'");
if ($_SERVER['REQUEST_URI'] == $base)
return '';
if (substr($_SERVER['REQUEST_URI'], 0, strlen($base)) != $base) {
logging('ERROR', "URL : request URI (".$_SERVER['REQUEST_URI'].") does not start with rewrite base ($base)");
return False;
}
$current_page = substr($_SERVER['REQUEST_URI'], strlen($base));
// URL contain params ?
$params_start = strpos($current_page, '?');
if ($params_start !== false)
// Params detected, remove it
// No page / currrent page start by '?' ?
if ($params_start == 0)
return '';
else
return substr($current_page, 0, $params_start);
return $current_page;
}
function get_rewrite_base() {
global $public_root_url;
if (preg_match('|^https?://[^/]+/(.*)$|', $public_root_url, $m))
return '/'.remove_trailing_slash($m[1]).'/';
elseif (preg_match('|^/(.*)$|', $public_root_url, $m))
return '/'.remove_trailing_slash($m[1]).'/';
return '/';
}
function redirect($go=false) {
global $public_root_url;
if ($go===false)
$go = "";
if (preg_match('#^https?://#',$go))
$url = $go;
elseif (isset($public_root_url) && $public_root_url) {
// 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";
}
else
$url="/$go";
// Prevent loop
if (isset($_SESSION['last_redirect']) && $_SESSION['last_redirect'] == $url)
logging('FATAL', 'Impossible de déterminer la page demandée (boucle). Si le problème persiste, merci de prendre contact avec le support.');
else
$_SESSION['last_redirect'] = $url;
logging('DEBUG',"redirect($go) => Redirect to : <$url>");
header("Location: $url");
exit();
}
function handle_url($url_patterns=null, $default_url=null) {
global $smarty;
if (is_null($url_patterns)) global $url_patterns;
if (is_null($default_url)) global $default_url;
global $url_info;
$url_info = get_current_url_handler();
if (!is_array($url_info))
logging('FATAL', "Une problème est survenu en interprétant l'URL de la page demandée.");
if (!function_exists($url_info['handler'])) {
logging('ERROR', "URL handler function ".$url_info['handler']."() does not exists !");
logging('FATAL', "Cette requête ne peut être traitée.");
}
if (isset($smarty) && $smarty)
$smarty -> assign('url_info', $url_info);
try {
return call_user_func($url_info['handler'], $url_info);
}
catch (Exception $e) {
log_exception($e, "An exception occured running URL handler function ".$url_info['handler']."()");
logging('FATAL', "Cette requête n'a put être traitée correctement.");
}
}
function remove_trailing_slash($url) {
if ($url == '/')
return $url;
elseif (substr($url, -1) == '/')
return substr($url, 0, -1);
return $url;
}
function check_ajax_request($session_key=null) {
global $ajax, $debug_ajax;
$ajax=true;
if (is_null($session_key) && isset($_REQUEST['session_key']))
$session_key = $_REQUEST['session_key'];
if ($_SESSION['session_key'] != $session_key) {
fatal_error('Invalid request');
}
if ($debug_ajax)
logging('DEBUG',"Ajax Request : ".vardump($_REQUEST));
}