Work on php 8.1 compatibility and adodb removal

This commit is contained in:
Laurent Destailleur 2022-10-10 14:23:02 +02:00
parent d9f1435cea
commit b6dbdd6757
4 changed files with 89 additions and 15 deletions

View File

@ -60,7 +60,7 @@ function printDropdownBookmarksList()
if ($sortorder) { if ($sortorder) {
$tmpurl .= ($tmpurl ? '&' : '').'sortorder='.urlencode($sortorder); $tmpurl .= ($tmpurl ? '&' : '').'sortorder='.urlencode($sortorder);
} }
if (is_array($_POST)) { if (!empty($_POST) && is_array($_POST)) {
foreach ($_POST as $key => $val) { foreach ($_POST as $key => $val) {
if ((preg_match('/^search_/', $key) || in_array($key, $authorized_var)) if ((preg_match('/^search_/', $key) || in_array($key, $authorized_var))
&& $val != '' && $val != ''

View File

@ -2535,8 +2535,8 @@ function dol_print_date($time, $format = '', $tzoutput = 'auto', $outputlangs =
if ($tzoutput == 'tzserver') { if ($tzoutput == 'tzserver') {
$to_gmt = false; $to_gmt = false;
$offsettzstring = @date_default_timezone_get(); // Example 'Europe/Berlin' or 'Indian/Reunion' $offsettzstring = @date_default_timezone_get(); // Example 'Europe/Berlin' or 'Indian/Reunion'
$offsettz = 0; // Timezone 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, so 0 $offsetdst = 0; // Dst offset with server timezone (because to_gmt is false), so 0
} elseif ($tzoutput == 'tzuser' || $tzoutput == 'tzuserrel') { } elseif ($tzoutput == 'tzuser' || $tzoutput == 'tzuserrel') {
$to_gmt = true; $to_gmt = true;
$offsettzstring = (empty($_SESSION['dol_tz_string']) ? 'UTC' : $_SESSION['dol_tz_string']); // Example 'Europe/Berlin' or 'Indian/Reunion' $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 = new DateTime();
$user_dt->setTimezone($user_date_tz); $user_dt->setTimezone($user_date_tz);
$user_dt->setTimestamp($tzoutput == 'tzuser' ? dol_now() : (int) $time); $user_dt->setTimestamp($tzoutput == 'tzuser' ? dol_now() : (int) $time);
$offsettz = $user_dt->getOffset(); $offsettz = $user_dt->getOffset(); // should include dst ?
} else { // old method (The 'tzuser' was processed like the 'tzuserrel') } 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 $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 $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); $format = str_replace('%A', '__A__', $format);
} }
$noadodb = getDolGlobalInt('MAIN_NO_ADODB_FOR_DATE');
//$noadodb = 1; // To force test
// Analyze date // Analyze date
$reg = array(); $reg = array();
@ -2647,23 +2649,76 @@ function dol_print_date($time, $format = '', $tzoutput = 'auto', $outputlangs =
$ssec = (!empty($reg[6]) ? $reg[6] : ''); $ssec = (!empty($reg[6]) ? $reg[6] : '');
$time = dol_mktime($shour, $smin, $ssec, $smonth, $sday, $syear, true); $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 { } else {
// Date is a timestamps // Date is a timestamps
if ($time < 100000000000) { // Protection against bad date values 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 { } else {
$ret = 'Bad value '.$time.' for date'; $ret = 'Bad value '.$time.' for date';
} }
} }
if (preg_match('/__b__/i', $format)) { 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. if ($noadodb) {
$month = adodb_strftime('%m', $timetouse, $to_gmt); // If to_gmt = false then adodb_strftime use TZ of server 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'. $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) { if ($encodetooutput) {
$monthtext = $outputlangs->transnoentities('Month'.$month); $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"; //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. $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); $dayweek = $outputlangs->transnoentitiesnoconv('Day'.$w);
$ret = str_replace('__A__', $dayweek, $ret); $ret = str_replace('__A__', $dayweek, $ret);
$ret = str_replace('__a__', dol_substr($dayweek, 0, 3), $ret); $ret = str_replace('__a__', dol_substr($dayweek, 0, 3), $ret);
} }

View File

@ -362,6 +362,12 @@ class DateLibTest extends PHPUnit\Framework\TestCase
$langs=$this->savlangs; $langs=$this->savlangs;
$db=$this->savdb; $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 // Check %Y-%m-%d %H:%M:%S format
$result=dol_print_date(0, '%Y-%m-%d %H:%M:%S', true); $result=dol_print_date(0, '%Y-%m-%d %H:%M:%S', true);
print __METHOD__." result=".$result."\n"; print __METHOD__." result=".$result."\n";