Compare commits
No commits in common. "9cc0f8581adda394aa2439a4180742bdafb44158" and "73dc860972d0edd58ca87547d6caab49c4e46fb8" have entirely different histories.
9cc0f8581a
...
73dc860972
3 changed files with 34 additions and 129 deletions
76
src/Tpl.php
76
src/Tpl.php
|
@ -172,8 +172,6 @@ class Tpl {
|
||||||
|
|
||||||
foreach(App :: get('templates.static_directories', array(), 'array') as $path)
|
foreach(App :: get('templates.static_directories', array(), 'array') as $path)
|
||||||
self :: register_static_directory($path);
|
self :: register_static_directory($path);
|
||||||
|
|
||||||
self :: register_function('var_dump', array('EesyPHP\\Tpl', 'smarty_var_dump'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,44 +260,6 @@ class Tpl {
|
||||||
$_SESSION['messages'][] = $message;
|
$_SESSION['messages'][] = $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get errors
|
|
||||||
* @return array<string>
|
|
||||||
*/
|
|
||||||
public static function get_errors() {
|
|
||||||
if(isset($_SESSION['errors']) && is_array($_SESSION['errors']))
|
|
||||||
return $_SESSION['errors'];
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get messages
|
|
||||||
* @return array<string>
|
|
||||||
*/
|
|
||||||
public static function get_messages() {
|
|
||||||
if(isset($_SESSION['messages']) && is_array($_SESSION['messages']))
|
|
||||||
return $_SESSION['messages'];
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Purge messages
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function purge_errors() {
|
|
||||||
if(isset($_SESSION['errors']))
|
|
||||||
unset($_SESSION['errors']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Purge messages
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function purge_messages() {
|
|
||||||
if(isset($_SESSION['messages']))
|
|
||||||
unset($_SESSION['messages']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register CSS file(s) to load on next displayed page
|
* Register CSS file(s) to load on next displayed page
|
||||||
* @param string|array<string> $args CSS files to load
|
* @param string|array<string> $args CSS files to load
|
||||||
|
@ -367,8 +327,8 @@ class Tpl {
|
||||||
self :: add_js_file(App::get('templates.included_js_files', array(), 'array'));
|
self :: add_js_file(App::get('templates.included_js_files', array(), 'array'));
|
||||||
|
|
||||||
// Messages
|
// Messages
|
||||||
self :: assign('errors', self :: get_errors());
|
self :: assign('errors', (isset($_SESSION['errors'])?$_SESSION['errors']:array()));
|
||||||
self :: assign('messages', self :: get_messages());
|
self :: assign('messages', (isset($_SESSION['messages'])?$_SESSION['messages']:array()));
|
||||||
|
|
||||||
// Files inclusions
|
// Files inclusions
|
||||||
self :: assign('css', self :: $css_files);
|
self :: assign('css', self :: $css_files);
|
||||||
|
@ -426,8 +386,8 @@ class Tpl {
|
||||||
Log :: fatal(I18n::_("An error occurred while displaying this page."));
|
Log :: fatal(I18n::_("An error occurred while displaying this page."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self :: purge_errors();
|
unset($_SESSION['errors']);
|
||||||
self :: purge_messages();
|
unset($_SESSION['messages']);
|
||||||
Hook :: trigger('after_displaying_template');
|
Hook :: trigger('after_displaying_template');
|
||||||
$sentry_span->finish();
|
$sentry_span->finish();
|
||||||
}
|
}
|
||||||
|
@ -446,14 +406,14 @@ class Tpl {
|
||||||
elseif (isset($data['success']) && !$data['success'] && http_response_code() == 200)
|
elseif (isset($data['success']) && !$data['success'] && http_response_code() == 200)
|
||||||
http_response_code(400);
|
http_response_code(400);
|
||||||
|
|
||||||
$data['messages'] = self :: get_messages();
|
if (isset($_SESSION['messages']) && !empty($_SESSION['messages'])) {
|
||||||
if (!$data['messages']) unset($data['messages']);
|
$data['messages'] = $_SESSION['messages'];
|
||||||
self :: purge_messages();
|
unset($_SESSION['messages']);
|
||||||
|
}
|
||||||
$data['errors'] = self :: get_errors();
|
if (isset($_SESSION['errors']) && !empty($_SESSION['errors'])) {
|
||||||
if (!$data['errors']) unset($data['errors']);
|
$data['errors'] = $_SESSION['errors'];
|
||||||
self :: purge_errors();
|
unset($_SESSION['errors']);
|
||||||
|
}
|
||||||
if (self :: $_debug_ajax)
|
if (self :: $_debug_ajax)
|
||||||
Log :: debug("AJAX Response : ".vardump($data));
|
Log :: debug("AJAX Response : ".vardump($data));
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
@ -653,7 +613,6 @@ class Tpl {
|
||||||
Url :: add_url_handler(
|
Url :: add_url_handler(
|
||||||
$pattern,
|
$pattern,
|
||||||
array('EesyPHP\\Tpl', 'handle_static_file'),
|
array('EesyPHP\\Tpl', 'handle_static_file'),
|
||||||
null, // additionnal info
|
|
||||||
false, // authenticated
|
false, // authenticated
|
||||||
false, // override
|
false, // override
|
||||||
false, // API mode
|
false, // API mode
|
||||||
|
@ -738,15 +697,4 @@ class Tpl {
|
||||||
$url = self :: static_url($params['path']);
|
$url = self :: static_url($params['path']);
|
||||||
if ($url) echo $url;
|
if ($url) echo $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Smarty function to dump variable using var_dump()
|
|
||||||
* @param array<string,mixed> $params Parameters from template file
|
|
||||||
* @param Smarty $smarty The smarty object
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function smarty_var_dump($params, $smarty) {
|
|
||||||
if (!isset($params['data'])) return;
|
|
||||||
var_dump($params['data']);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
22
src/Url.php
22
src/Url.php
|
@ -14,14 +14,12 @@ class Url {
|
||||||
* array (
|
* array (
|
||||||
* '|get/(?P<name>[a-zA-Z0-9]+)$|' => array (
|
* '|get/(?P<name>[a-zA-Z0-9]+)$|' => array (
|
||||||
* 'handler' => 'get',
|
* 'handler' => 'get',
|
||||||
* 'additional_info' => array(),
|
|
||||||
* 'authenticated' => true,
|
* 'authenticated' => true,
|
||||||
* 'api_mode' => false,
|
* 'api_mode' => false,
|
||||||
* 'methods' => array('GET'),
|
* 'methods' => array('GET'),
|
||||||
* ),
|
* ),
|
||||||
* '|get/all$|' => => array (
|
* '|get/all$|' => => array (
|
||||||
* 'handler' => 'get_all',
|
* 'handler' => 'get_all',
|
||||||
* 'additional_info' => array(),
|
|
||||||
* 'authenticated' => true,
|
* 'authenticated' => true,
|
||||||
* 'api_mode' => false,
|
* 'api_mode' => false,
|
||||||
* 'methods' => array('GET', 'POST'),
|
* 'methods' => array('GET', 'POST'),
|
||||||
|
@ -74,7 +72,6 @@ class Url {
|
||||||
*
|
*
|
||||||
* @param string|array $pattern The URL pattern or an array of patterns (required)
|
* @param string|array $pattern The URL pattern or an array of patterns (required)
|
||||||
* @param callable $handler The URL pattern handler (must be callable, required)
|
* @param callable $handler The URL pattern handler (must be callable, required)
|
||||||
* @param array|null $additional_info Array of information to pass to the URL handler
|
|
||||||
* @param boolean $authenticated Permit to define if this URL is accessible only for
|
* @param boolean $authenticated Permit to define if this URL is accessible only for
|
||||||
* authenticated users (optional, default: true if the
|
* authenticated users (optional, default: true if the
|
||||||
* special force_authentication function is defined,
|
* special force_authentication function is defined,
|
||||||
|
@ -84,9 +81,8 @@ class Url {
|
||||||
* @param boolean $api_mode Enable API mode (optional, default: false)
|
* @param boolean $api_mode Enable API mode (optional, default: false)
|
||||||
* @param array|string|null $methods HTTP method (optional, default: array('GET', 'POST'))
|
* @param array|string|null $methods HTTP method (optional, default: array('GET', 'POST'))
|
||||||
**/
|
**/
|
||||||
public static function add_url_handler($pattern, $handler=null, $additional_info=null,
|
public static function add_url_handler($pattern, $handler=null, $authenticated=null, $override=true,
|
||||||
$authenticated=null, $override=true, $api_mode=false,
|
$api_mode=false, $methods=null) {
|
||||||
$methods=null) {
|
|
||||||
$authenticated = (
|
$authenticated = (
|
||||||
is_null($authenticated)?
|
is_null($authenticated)?
|
||||||
function_exists('force_authentication'):
|
function_exists('force_authentication'):
|
||||||
|
@ -99,21 +95,15 @@ class Url {
|
||||||
if (is_array($pattern)) {
|
if (is_array($pattern)) {
|
||||||
if (is_null($handler))
|
if (is_null($handler))
|
||||||
foreach($pattern as $p => $h)
|
foreach($pattern as $p => $h)
|
||||||
self :: add_url_handler(
|
self :: add_url_handler($p, $h, $authenticated, $override, $api_mode, $methods);
|
||||||
$p, $h, $additional_info, $authenticated, $override, $api_mode, $methods);
|
|
||||||
else
|
else
|
||||||
foreach($pattern as $p)
|
foreach($pattern as $p)
|
||||||
self :: add_url_handler(
|
self :: add_url_handler($p, $handler, $authenticated, $override, $api_mode, $methods);
|
||||||
$p, $handler, $additional_info, $authenticated, $override, $api_mode, $methods);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (!isset(self :: $patterns[$pattern])) {
|
if (!isset(self :: $patterns[$pattern])) {
|
||||||
self :: $patterns[$pattern] = array(
|
self :: $patterns[$pattern] = array(
|
||||||
'handler' => $handler,
|
'handler' => $handler,
|
||||||
'additional_info' => (
|
|
||||||
is_array($additional_info)?
|
|
||||||
$additional_info:array()
|
|
||||||
),
|
|
||||||
'authenticated' => $authenticated,
|
'authenticated' => $authenticated,
|
||||||
'api_mode' => $api_mode,
|
'api_mode' => $api_mode,
|
||||||
'methods' => $methods,
|
'methods' => $methods,
|
||||||
|
@ -126,10 +116,6 @@ class Url {
|
||||||
);
|
);
|
||||||
self :: $patterns[$pattern] = array(
|
self :: $patterns[$pattern] = array(
|
||||||
'handler' => $handler,
|
'handler' => $handler,
|
||||||
'additional_info' => (
|
|
||||||
is_array($additional_info)?
|
|
||||||
$additional_info:array()
|
|
||||||
),
|
|
||||||
'authenticated' => $authenticated,
|
'authenticated' => $authenticated,
|
||||||
'api_mode' => $api_mode,
|
'api_mode' => $api_mode,
|
||||||
'methods' => $methods,
|
'methods' => $methods,
|
||||||
|
|
|
@ -13,56 +13,31 @@ namespace EesyPHP;
|
||||||
*/
|
*/
|
||||||
class UrlRequest {
|
class UrlRequest {
|
||||||
|
|
||||||
/**
|
// The URL requested handler
|
||||||
* The URL requested handler
|
private $current_url = null;
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $current_url;
|
|
||||||
|
|
||||||
/**
|
// The URL requested handler
|
||||||
* The URL requested handler
|
private $handler = null;
|
||||||
* @var callable
|
|
||||||
*/
|
|
||||||
private $handler;
|
|
||||||
|
|
||||||
/**
|
// Request need authentication ?
|
||||||
* Request need authentication ?
|
private $authenticated = true;
|
||||||
* @var bool|null
|
|
||||||
*/
|
|
||||||
private $authenticated = null;
|
|
||||||
|
|
||||||
/**
|
// API mode enabled ?
|
||||||
* API mode enabled ?
|
private $api_mode = false;
|
||||||
* @var bool
|
|
||||||
*/
|
|
||||||
private $api_mode;
|
|
||||||
|
|
||||||
/**
|
// Parameters detected on requested URL
|
||||||
* Parameters detected on requested URL
|
|
||||||
* @var array<string,mixed>
|
|
||||||
*/
|
|
||||||
private $url_params = array();
|
private $url_params = array();
|
||||||
|
|
||||||
/**
|
public function __construct($current_url, $handler_infos, $url_params=array()) {
|
||||||
* Additional info pass when the URL handler was registered
|
|
||||||
* @var array<string,mixed>
|
|
||||||
*/
|
|
||||||
private $additional_info = array();
|
|
||||||
|
|
||||||
public function __construct($current_url, $handler_info, $url_params=array()) {
|
|
||||||
$this -> current_url = $current_url;
|
$this -> current_url = $current_url;
|
||||||
$this -> handler = $handler_info['handler'];
|
$this -> handler = $handler_infos['handler'];
|
||||||
$this -> authenticated = (
|
$this -> authenticated = (
|
||||||
isset($handler_info['authenticated'])?
|
isset($handler_infos['authenticated'])?
|
||||||
boolval($handler_info['authenticated']):true);
|
boolval($handler_infos['authenticated']):true);
|
||||||
$this -> api_mode = (
|
$this -> api_mode = (
|
||||||
isset($handler_info['api_mode'])?
|
isset($handler_infos['api_mode'])?
|
||||||
boolval($handler_info['api_mode']):false);
|
boolval($handler_infos['api_mode']):false);
|
||||||
$this -> url_params = $url_params;
|
$this -> url_params = $url_params;
|
||||||
$this -> additional_info = (
|
|
||||||
isset($handler_info['additional_info']) && is_array($handler_info['additional_info'])?
|
|
||||||
$handler_info['additional_info']:array()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -85,10 +60,9 @@ class UrlRequest {
|
||||||
return $this -> get_referer();
|
return $this -> get_referer();
|
||||||
if ($key == 'http_method')
|
if ($key == 'http_method')
|
||||||
return $_SERVER['REQUEST_METHOD'];
|
return $_SERVER['REQUEST_METHOD'];
|
||||||
if (array_key_exists($key, $this->url_params))
|
if (array_key_exists($key, $this->url_params)) {
|
||||||
return urldecode($this->url_params[$key]);
|
return urldecode($this->url_params[$key]);
|
||||||
if (array_key_exists($key, $this->additional_info))
|
}
|
||||||
return urldecode($this->additional_info[$key]);
|
|
||||||
// Unknown key, log warning
|
// Unknown key, log warning
|
||||||
Log :: warning(
|
Log :: warning(
|
||||||
"__get($key): invalid property requested\n%s",
|
"__get($key): invalid property requested\n%s",
|
||||||
|
@ -129,10 +103,7 @@ class UrlRequest {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return True;
|
return True;
|
||||||
return (
|
return array_key_exists($key, $this->url_params);
|
||||||
array_key_exists($key, $this->url_params)
|
|
||||||
|| array_key_exists($key, $this->additional_info)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue