Managesieve: Fix parsing of vacation date-time with non-default date_format (#5372)

Added new method rcube_utils::format_datestr() to convert date_format date
into ISO date format.
pull/5754/head
Aleksander Machniak 8 years ago
parent 654d4e51fc
commit 4624b22967

@ -3,6 +3,7 @@ CHANGELOG Roundcube Webmail
- Enigma: Add possibility to configure gpg-agent binary location (enigma_pgp_agent)
- Fix regression in resizing JPEG images with Imagick (#5376)
- Managesieve: Fix parsing of vacation date-time with non-default date_format (#5372)
RELEASE 1.2.1
-------------

@ -239,9 +239,11 @@ class rcube_sieve_vacation extends rcube_sieve_engine
}
if ($date_extension) {
$date_format = $this->rc->config->get('date_format', 'Y-m-d');
foreach (array('date_from', 'date_to') as $var) {
$time = ${str_replace('date', 'time', $var)};
$date = trim($$var . ' ' . $time);
$date = rcube_utils::format_datestr($$var, $date_format);
$date = trim($date . ' ' . $time);
if ($date && ($dt = rcube_utils::anytodatetime($date, $timezone))) {
if ($time) {

@ -758,6 +758,7 @@ class rcube_utils
$dt = $timezone ? new DateTime($date, $timezone) : new DateTime($date);
}
catch (Exception $e) {
rcube::raise_error($e, true, false);
// ignore
}
}
@ -810,15 +811,47 @@ class rcube_utils
$date = trim($date);
// try to fix dd/mm vs. mm/dd discrepancy, we can't do more here
if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})$/', $date, $m)) {
if (preg_match('/^(\d{1,2})[.\/-](\d{1,2})[.\/-](\d{4})(.*)$/', $date, $m)) {
$mdy = $m[2] > 12 && $m[1] <= 12;
$day = $mdy ? $m[2] : $m[1];
$month = $mdy ? $m[1] : $m[2];
$date = sprintf('%04d-%02d-%02d 00:00:00', intval($m[3]), $month, $day);
$date = sprintf('%04d-%02d-%02d%s', intval($m[3]), $month, $day, $m[4] ?: '00:00:00');
}
// I've found that YYYY.MM.DD is recognized wrong, so here's a fix
else if (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})$/', $date)) {
$date = str_replace('.', '-', $date) . ' 00:00:00';
else if (preg_match('/^(\d{4})\.(\d{1,2})\.(\d{1,2})(.*)$/', $date)) {
$date = str_replace('.', '-', $date) . ($m[4] ?: '00:00:00');
}
return $date;
}
/**
* Turns the given date-only string in defined format into YYYY-MM-DD format.
*
* Supported formats: 'Y/m/d', 'Y.m.d', 'd-m-Y', 'd/m/Y', 'd.m.Y', 'j.n.Y'
*
* @param string $date Date string
* @param string $format Input date format
*
* @return strin Date string in YYYY-MM-DD format, or the original string
* if format is not supported
*/
public static function format_datestr($date, $format)
{
$format_items = preg_split('/[.-\/\\\\]/', $format);
$date_items = preg_split('/[.-\/\\\\]/', $date);
$iso_format = '%04d-%02d-%02d';
if (count($format_items) == 3 && count($date_items) == 3) {
if ($format_items[0] == 'Y') {
$date = sprintf($iso_format, $date_items[0], $date_items[1], $date_items[2]);
}
else if (strpos('dj', $format_items[0]) !== false) {
$date = sprintf($iso_format, $date_items[2], $date_items[1], $date_items[0]);
}
else if (strpos('mn', $format_items[0]) !== false) {
$date = sprintf($iso_format, $date_items[2], $date_items[0], $date_items[1]);
}
}
return $date;

@ -351,6 +351,24 @@ class Framework_Utils extends PHPUnit_Framework_TestCase
}
}
/**
* rcube:utils::format_datestr()
*/
function test_format_datestr()
{
$test = array(
array('abc-555', 'abc', 'abc-555'),
array('2013-04-22', 'Y-m-d', '2013-04-22'),
array('22/04/2013', 'd/m/Y', '2013-04-22'),
array('4.22.2013', 'm.d.Y', '2013-04-22'),
);
foreach ($test as $data) {
$result = rcube_utils::format_datestr($data[0], $data[1]);
$this->assertSame($data[2], $result, "Error formatting date: " . $data[0]);
}
}
/**
* rcube:utils::tokenize_string()
*/

Loading…
Cancel
Save