From b6dbdd675795eb7ff52aff2cfb4a5113554abeca Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 10 Oct 2022 14:23:02 +0200 Subject: [PATCH] Work on php 8.1 compatibility and adodb removal --- htdocs/bookmarks/bookmarks.lib.php | 2 +- htdocs/core/lib/functions.lib.php | 90 +++++++++++++++++--- htdocs/includes/adodbtime/adodb-time.inc.php | 6 +- test/phpunit/DateLibTest.php | 6 ++ 4 files changed, 89 insertions(+), 15 deletions(-) diff --git a/htdocs/bookmarks/bookmarks.lib.php b/htdocs/bookmarks/bookmarks.lib.php index 797bffa187a..d5258ba26f1 100644 --- a/htdocs/bookmarks/bookmarks.lib.php +++ b/htdocs/bookmarks/bookmarks.lib.php @@ -60,7 +60,7 @@ function printDropdownBookmarksList() if ($sortorder) { $tmpurl .= ($tmpurl ? '&' : '').'sortorder='.urlencode($sortorder); } - if (is_array($_POST)) { + if (!empty($_POST) && is_array($_POST)) { foreach ($_POST as $key => $val) { if ((preg_match('/^search_/', $key) || in_array($key, $authorized_var)) && $val != '' diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index d771c75ba48..61871f8b891 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -2535,8 +2535,8 @@ function dol_print_date($time, $format = '', $tzoutput = 'auto', $outputlangs = if ($tzoutput == 'tzserver') { $to_gmt = false; $offsettzstring = @date_default_timezone_get(); // Example 'Europe/Berlin' or 'Indian/Reunion' - $offsettz = 0; // Timezone offset with server timezone, so 0 - $offsetdst = 0; // Dst offset with server timezone, so 0 + $offsettz = 0; // Timezone offset with server timezone (because to_gmt is false), so 0 + $offsetdst = 0; // Dst offset with server timezone (because to_gmt is false), so 0 } elseif ($tzoutput == 'tzuser' || $tzoutput == 'tzuserrel') { $to_gmt = true; $offsettzstring = (empty($_SESSION['dol_tz_string']) ? 'UTC' : $_SESSION['dol_tz_string']); // Example 'Europe/Berlin' or 'Indian/Reunion' @@ -2546,8 +2546,8 @@ function dol_print_date($time, $format = '', $tzoutput = 'auto', $outputlangs = $user_dt = new DateTime(); $user_dt->setTimezone($user_date_tz); $user_dt->setTimestamp($tzoutput == 'tzuser' ? dol_now() : (int) $time); - $offsettz = $user_dt->getOffset(); - } else { // old method (The 'tzuser' was processed like the 'tzuserrel') + $offsettz = $user_dt->getOffset(); // should include dst ? + } else { // with old method (The 'tzuser' was processed like the 'tzuserrel') $offsettz = (empty($_SESSION['dol_tz']) ? 0 : $_SESSION['dol_tz']) * 60 * 60; // Will not be used anymore $offsetdst = (empty($_SESSION['dol_dst']) ? 0 : $_SESSION['dol_dst']) * 60 * 60; // Will not be used anymore } @@ -2628,6 +2628,8 @@ function dol_print_date($time, $format = '', $tzoutput = 'auto', $outputlangs = $format = str_replace('%A', '__A__', $format); } + $noadodb = getDolGlobalInt('MAIN_NO_ADODB_FOR_DATE'); + //$noadodb = 1; // To force test // Analyze date $reg = array(); @@ -2647,23 +2649,76 @@ function dol_print_date($time, $format = '', $tzoutput = 'auto', $outputlangs = $ssec = (!empty($reg[6]) ? $reg[6] : ''); $time = dol_mktime($shour, $smin, $ssec, $smonth, $sday, $syear, true); - $ret = adodb_strftime($format, $time + $offsettz + $offsetdst, $to_gmt); + if ($noadodb) { + if ($to_gmt) { + $tzo = new DateTimeZone('UTC'); // when to_gmt is true, base for offsettz and offsetdst (so timetouse) is UTC + } else { + $tzo = new DateTimeZone(date_default_timezone_get()); // when to_gmt is false, base for offsettz and offsetdst (so timetouse) is PHP server + } + $dtts = new DateTime(); + $dtts->setTimestamp($time); + $dtts->setTimezone($tzo); + $newformat = str_replace( + array('%Y', '%y', '%m', '%d', '%H', '%M', '%S', 'T', 'Z', '__a__', '__A__', '__b__', '__B__'), + array('Y', 'y', 'm', 'd', 'H', 'i', 's', '__£__', '__$__', '__{__', '__}__', '__[__', '__]__'), + $format); + $ret = $dtts->format($newformat); + $ret = str_replace( + array('__£__', '__$__', '__{__', '__}__', '__[__', '__]__'), + array('T', 'Z', '__a__', '__A__', '__b__', '__B__'), + $ret); + } else { + $ret = adodb_strftime($format, $time + $offsettz + $offsetdst, $to_gmt); + } } else { // Date is a timestamps if ($time < 100000000000) { // Protection against bad date values - $timetouse = $time + $offsettz + $offsetdst; // TODO Replace this with function Date PHP. We also should not use anymore offsettz and offsetdst but only offsettzstring. + $timetouse = $time + $offsettz + $offsetdst; // TODO We could be able to disable use of offsettz and offsetdst to use only offsettzstring. - $ret = adodb_strftime($format, $timetouse, $to_gmt); // If to_gmt = false then adodb_strftime use TZ of server + if ($noadodb) { + if ($to_gmt) { + $tzo = new DateTimeZone('UTC'); // when to_gmt is true, base for offsettz and offsetdst (so timetouse) is UTC + } else { + $tzo = new DateTimeZone(date_default_timezone_get()); // when to_gmt is false, base for offsettz and offsetdst (so timetouse) is PHP server + } + $dtts = new DateTime(); + $dtts->setTimestamp($timetouse); + $dtts->setTimezone($tzo); + $newformat = str_replace( + array('%Y', '%y', '%m', '%d', '%H', '%M', '%S', 'T', 'Z', '__a__', '__A__', '__b__', '__B__'), + array('Y', 'y', 'm', 'd', 'H', 'i', 's', '__£__', '__$__', '__{__', '__}__', '__[__', '__]__'), + $format); + $ret = $dtts->format($newformat); + $ret = str_replace( + array('__£__', '__$__', '__{__', '__}__', '__[__', '__]__'), + array('T', 'Z', '__a__', '__A__', '__b__', '__B__'), + $ret); + } else { + $ret = adodb_strftime($format, $timetouse, $to_gmt); // If to_gmt = false then adodb_strftime use TZ of server + } + //var_dump($ret);exit; } else { $ret = 'Bad value '.$time.' for date'; } } if (preg_match('/__b__/i', $format)) { - $timetouse = $time + $offsettz + $offsetdst; // TODO Replace this with function Date PHP. We also should not use anymore offsettz and offsetdst but only offsettzstring. + $timetouse = $time + $offsettz + $offsetdst; // TODO We could be able to disable use of offsettz and offsetdst to use only offsettzstring. - // Here ret is string in PHP setup language (strftime was used). Now we convert to $outputlangs. - $month = adodb_strftime('%m', $timetouse, $to_gmt); // If to_gmt = false then adodb_strftime use TZ of server + if ($noadodb) { + if ($to_gmt) { + $tzo = new DateTimeZone('UTC'); // when to_gmt is true, base for offsettz and offsetdst (so timetouse) is UTC + } else { + $tzo = new DateTimeZone(date_default_timezone_get()); // when to_gmt is false, base for offsettz and offsetdst (so timetouse) is PHP server + } + $dtts = new DateTime(); + $dtts->setTimestamp($timetouse); + $dtts->setTimezone($tzo); + $month = $dtts->format("m"); + } else { + // After this ret is string in PHP setup language (strftime was used). Now we convert to $outputlangs. + $month = adodb_strftime('%m', $timetouse, $to_gmt); // If to_gmt = false then adodb_strftime use TZ of server + } $month = sprintf("%02d", $month); // $month may be return with format '06' on some installation and '6' on other, so we force it to '06'. if ($encodetooutput) { $monthtext = $outputlangs->transnoentities('Month'.$month); @@ -2682,8 +2737,21 @@ function dol_print_date($time, $format = '', $tzoutput = 'auto', $outputlangs = //print "time=$time offsettz=$offsettz offsetdst=$offsetdst offsettzstring=$offsettzstring"; $timetouse = $time + $offsettz + $offsetdst; // TODO Replace this with function Date PHP. We also should not use anymore offsettz and offsetdst but only offsettzstring. - $w = adodb_strftime('%w', $timetouse, $to_gmt); // If to_gmt = false then adodb_strftime use TZ of server + if ($noadodb) { + if ($to_gmt) { + $tzo = new DateTimeZone('UTC'); + } else { + $tzo = new DateTimeZone(date_default_timezone_get()); + } + $dtts = new DateTime(); + $dtts->setTimestamp($timetouse); + $dtts->setTimezone($tzo); + $w = $dtts->format("w"); + } else { + $w = adodb_strftime('%w', $timetouse, $to_gmt); // If to_gmt = false then adodb_strftime use TZ of server + } $dayweek = $outputlangs->transnoentitiesnoconv('Day'.$w); + $ret = str_replace('__A__', $dayweek, $ret); $ret = str_replace('__a__', dol_substr($dayweek, 0, 3), $ret); } diff --git a/htdocs/includes/adodbtime/adodb-time.inc.php b/htdocs/includes/adodbtime/adodb-time.inc.php index 030196db275..1dfa97929a2 100644 --- a/htdocs/includes/adodbtime/adodb-time.inc.php +++ b/htdocs/includes/adodbtime/adodb-time.inc.php @@ -1,8 +1,8 @@ savlangs; $db=$this->savdb; + // Check %Y-%m-%d %H:%M:%S format + $result=dol_print_date('1970-01-01', '%Y-%m-%d %H:%M:%S', true); // A case for compatibility check + print __METHOD__." result=".$result."\n"; + $this->assertEquals('1970-01-01 00:00:00', $result); + + // Check %Y-%m-%d %H:%M:%S format $result=dol_print_date(0, '%Y-%m-%d %H:%M:%S', true); print __METHOD__." result=".$result."\n";