Compare commits

...

3 commits

Author SHA1 Message Date
Benjamin Renard
df4cc74fea
Db object / AttrTimestamp: add support of millisecond / microsecond 2024-08-03 13:34:06 +02:00
Benjamin Renard
630e2c4d02
Url::error_page(): fix handling API mode 2024-08-03 09:05:46 +02:00
Benjamin Renard
72abbb7371
Trigger 401 HTTP error on authentication failure instead of 500 2024-08-03 09:04:49 +02:00
3 changed files with 28 additions and 10 deletions

View file

@ -86,12 +86,20 @@ class Date {
* @return \DateTime
*/
public static function from_timestamp($value, $timezone=null) {
$date = new \DateTime();
$date -> setTimestamp($value);
$date = \DateTime::createFromFormat("U.u", number_format($value, 6, ".", ""));
$date -> setTimezone(self :: timezone($timezone));
return $date;
}
/**
* Convert \DateTime object to timestamp as float with microsecond
* @param \DateTime $value The \DateTime object to convert
* @return float
*/
public static function to_timestamp($value) {
return floatval($value->format("U.u"));
}
/**
* Create \DateTime object
* @param int|null $timezone The expected timezone (optional, default: system one)

View file

@ -18,11 +18,11 @@ class AttrTimestamp extends Attr {
* The export format
* @var string
*/
protected $export_format = 'Y/m/d H:i:s';
protected $export_format = 'Y/m/d H:i:s.u';
/**
* Compute attribute value from DB
* @param int|null $value The value as retrieved from debug
* @param int|float|null $value The value as retrieved from debug
* @return \DateTime|null The attribute value
*/
public function from_db($value) {
@ -33,14 +33,14 @@ class AttrTimestamp extends Attr {
/**
* Compute attribute value for DB
* @param \DateTime|int|null $value The value as handled in PHP
* @return int|null The attribute value as stored in DB
* @param \DateTime|int|float|null $value The value as handled in PHP
* @return float|null The attribute value as stored in DB
*/
public function to_db($value) {
$value = parent::from_db($value);
if (is_null($value)) return null;
$value = $value instanceof \DateTime?$value:Date :: from_timestamp($value);
return $value->getTimestamp();
return Date :: to_timestamp($value);
}
/**
@ -61,7 +61,7 @@ class AttrTimestamp extends Attr {
/**
* Compute attribute value to string
* @param \DateTime|int|null $value The input value as handled in PHP
* @param \DateTime|int|float|null $value The input value as handled in PHP
* @return string The attribute value as string
*/
public function to_string($value) {

View file

@ -201,6 +201,14 @@ class Url {
http_response_code($error_code);
if (Tpl :: initialized()) {
if (self :: api_mode())
Tpl :: display_ajax_return(
[
"success" => false,
"error" => $error['message'],
],
$error_code
);
Tpl :: assign('message', $error['message']);
Tpl :: display('error_page.tpl', $error['pagetitle']);
exit();
@ -446,8 +454,10 @@ class Url {
Tpl :: assign('request', self :: $request );
// Check authentication (if need)
if(self :: $request -> authenticated && !Auth::login(true))
Log :: fatal(I18n::_("Authentication required but fail to authenticate you."));
if(self :: $request -> authenticated && !Auth::login(true)) {
Log :: error(I18n::_("Authentication required but fail to authenticate you."));
self :: error_page(null, 401);
}
Hook::trigger('before_handling_request', array('request' => self :: $request));
$success = false;