Skip to content

Commit

Permalink
New date validation function for Date Of Birth field (zencart#6314)
Browse files Browse the repository at this point in the history
* Dob field using local format with validation

---------

Co-authored-by: Chris Brown <[email protected]>
  • Loading branch information
piloujp and drbyte authored Mar 11, 2024
1 parent d3e6258 commit 0289240
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 32 deletions.
42 changes: 33 additions & 9 deletions includes/functions/functions_dates.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,53 @@
function zen_date_raw($date, $reverse = false) {
// sometimes zen_date_short is called with a zero-date value which returns false, which is then passed to $date here, so this just reformats to avoid confusion.
if (empty($date) || strpos($date, '0001') || strpos($date, '0000')) {
$date = '01/01/0001';
$date = DateTime::createFromFormat('!m/d/Y', '01/01/0001')->format(DATE_FORMAT);
}

if (DATE_FORMAT === 'd/m/Y') {
$date = preg_replace('/\D+/', '', $date);
$date_format = str_replace(['/', '-'], '', DATE_FORMAT);

if ($date_format === 'dmY') {
if ($reverse) {
return substr($date, 0, 2) . substr($date, 3, 2) . substr($date, 6, 4);
return substr($date, 0, 2) . substr($date, 2, 2) . substr($date, 4, 4);
} else {
return substr($date, 6, 4) . substr($date, 3, 2) . substr($date, 0, 2);
return substr($date, 4, 4) . substr($date, 2, 2) . substr($date, 0, 2);
}
} elseif (DATE_FORMAT === 'Y/m/d') {
} elseif ($date_format === 'Ymd') {
if ($reverse) {
return substr($date, 8, 2) . substr($date, 5, 2) . substr($date, 0, 4);
return substr($date, 6, 2) . substr($date, 4, 2) . substr($date, 0, 4);
} else {
return substr($date, 0, 4) . substr($date, 5, 2) . substr($date, 8, 2);
return substr($date, 0, 4) . substr($date, 4, 2) . substr($date, 6, 2);
}
} elseif ($reverse) {
return substr($date, 3, 2) . substr($date, 0, 2) . substr($date, 6, 4);
return substr($date, 2, 2) . substr($date, 0, 2) . substr($date, 4, 4);
} else {
return substr($date, 6, 4) . substr($date, 0, 2) . substr($date, 3, 2);
return substr($date, 4, 4) . substr($date, 0, 2) . substr($date, 2, 2);
}
}
}


/**
* Validate a date in the selected locale date format
*
* @param string $date
* @param string $format (optional) needs to be a valid short date format for DateTimeImmutableObject using / or - or nothing as separators
* @return bool
*/
function zen_valid_date(string $date, string $format = DATE_FORMAT): bool
{
// Build 3 formats from 1 with 3 possible separators
$format0 = str_replace('-', '/', $format);
$format1 = str_replace('/', '-', $format);
$format2 = str_replace(['/','-'], '', $format);
$d0 = DateTime::createFromFormat('!' . $format0, $date);
$d1 = DateTime::createFromFormat('!' . $format1, $date);
$d2 = DateTime::createFromFormat('!' . $format2, $date);
return ($d0 && $d0->format($format0) == $date) || ($d1 && $d1->format($format1) == $date) || ($d2 && $d2->format($format2) == $date);
}


/**
* Output a raw date string in the selected locale date format
*
Expand Down
4 changes: 2 additions & 2 deletions includes/languages/lang.english.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
'ENTRY_CUSTOMERS_REFERRAL' => 'Referral Code:',
'ENTRY_DATE_FROM' => 'Date From:',
'ENTRY_DATE_OF_BIRTH' => 'Date of Birth:',
'ENTRY_DATE_OF_BIRTH_ERROR' => 'Is your birth date correct? Our system requires the date in this format: MM/DD/YYYY (eg 05/21/1970) or this format: YYYY-MM-DD (eg 1970-05-21)',
'ENTRY_DATE_OF_BIRTH_TEXT' => '* (eg. 05/21/1970 or 1970-05-21)',
'ENTRY_DATE_OF_BIRTH_ERROR' => 'Is your birth date correct? Our system requires the date in this format: MM/DD/YYYY (eg 05/21/1970)',
'ENTRY_DATE_OF_BIRTH_TEXT' => '* (eg. 05/21/1970)',
'ENTRY_DATE_TO' => 'Date To:',
'ENTRY_EMAIL' => 'Email Address:',
'ENTRY_EMAIL_ADDRESS' => 'Email Address:',
Expand Down
9 changes: 1 addition & 8 deletions includes/modules/create_account.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,7 @@

if (ACCOUNT_DOB === 'true') {
if (ENTRY_DOB_MIN_LENGTH > 0 or !empty($_POST['dob'])) {
// Support ISO-8601 style date
if (preg_match('/^([0-9]{4})(|-|\/)([0-9]{2})\2([0-9]{2})$/', $dob)) {
// Account for incorrect date format provided to strtotime such as swapping day and month instead of the expected yyyymmdd, yyyy-mm-dd, or yyyy/mm/dd format
if (strtotime($dob) !== false) {
$_POST['dob'] = $dob = date(DATE_FORMAT, strtotime($dob));
}
}
if (substr_count($dob, '/') > 2 || checkdate((int)substr(zen_date_raw($dob), 4, 2), (int)substr(zen_date_raw($dob), 6, 2), (int)substr(zen_date_raw($dob), 0, 4)) == false) {
if (strlen($dob) >10 || zen_valid_date($dob) === false) {
$error = true;
$messageStack->add('create_account', ENTRY_DATE_OF_BIRTH_ERROR);
}
Expand Down
14 changes: 2 additions & 12 deletions includes/modules/pages/account_edit/header_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@

if (ACCOUNT_DOB == 'true') {
if (ENTRY_DOB_MIN_LENGTH > 0 or !empty($_POST['dob'])) {
// Support ISO-8601 style date
if (preg_match('/^([0-9]{4})(|-|\/)([0-9]{2})\2([0-9]{2})$/', $dob)) {
// Account for incorrect date format provided to strtotime such as swapping day and month instead of the expected yyyymmdd, yyyy-mm-dd, or yyyy/mm/dd format
if (strtotime($dob) !== false) {
$_POST['dob'] = $dob = date(DATE_FORMAT, strtotime($dob));
}
}
if (substr_count($dob,'/') > 2 || checkdate((int)substr(zen_date_raw($dob), 4, 2), (int)substr(zen_date_raw($dob), 6, 2), (int)substr(zen_date_raw($dob), 0, 4)) == false) {
if (strlen($dob) >10 || zen_valid_date($dob) === false) {
$error = true;
$messageStack->add('account_edit', ENTRY_DATE_OF_BIRTH_ERROR);
}
Expand Down Expand Up @@ -181,10 +174,7 @@
}

if (!(isset($_POST['action']) && ($_POST['action'] == 'process'))) {
// Posted page content is not requested to be processed, populate dob with customer's database entry.
// Using ISO-8601 format of date display to support javascript/jQuery driven date picker data handling.
$dob = zen_date_raw(zen_date_short($account->fields['customers_dob']));
$dob = substr($dob, 0, 4) . '-' . substr($dob, 4, 2) . '-' . substr($dob, 6, 2);
$dob = zen_date_short($account->fields['customers_dob']);
if ($dob <= '0001-01-01') {
$dob = '0001-01-01 00:00:00';
}
Expand Down
2 changes: 1 addition & 1 deletion zc_install/sql/install/mysql_zencart.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2482,7 +2482,7 @@ INSERT INTO configuration (configuration_title, configuration_key, configuration

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, val_function, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('First Name', 'ENTRY_FIRST_NAME_MIN_LENGTH', '2', '{"error":"TEXT_MIN_ADMIN_FIRST_NAME_LENGTH","id":"FILTER_VALIDATE_INT","options":{"options":{"min_range":0}}}', 'Minimum length of first name', '2', '1', now());
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, val_function, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Last Name', 'ENTRY_LAST_NAME_MIN_LENGTH', '2', '{"error":"TEXT_MIN_ADMIN_LAST_NAME_LENGTH","id":"FILTER_VALIDATE_INT","options":{"options":{"min_range":0}}}', 'Minimum length of last name', '2', '2', now());
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, val_function, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Date of Birth', 'ENTRY_DOB_MIN_LENGTH', '10', '{"error":"TEXT_MIN_ADMIN_DOB_LENGTH","id":"FILTER_VALIDATE_INT","options":{"options":{"min_range":0}}}', 'Minimum length of date of birth', '2', '3', now());
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, val_function, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Date of Birth', 'ENTRY_DOB_MIN_LENGTH', '8', '{"error":"TEXT_MIN_ADMIN_DOB_LENGTH","id":"FILTER_VALIDATE_INT","options":{"options":{"min_range":0}}}', 'Minimum length of date of birth', '2', '3', now());
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, val_function, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('E-Mail Address', 'ENTRY_EMAIL_ADDRESS_MIN_LENGTH', '6', '{"error":"TEXT_MIN_ADMIN_EMAIL_ADDRESS_LENGTH","id":"FILTER_VALIDATE_INT","options":{"options":{"min_range":0}}}', 'Minimum length of e-mail address', '2', '4', now());
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, val_function, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Street Address', 'ENTRY_STREET_ADDRESS_MIN_LENGTH', '5', '{"error":"TEXT_MIN_ADMIN_STREET_ADDRESS_LENGTH","id":"FILTER_VALIDATE_INT","options":{"options":{"min_range":0}}}', 'Minimum length of street address', '2', '5', now());
INSERT INTO configuration (configuration_title, configuration_key, configuration_value, val_function, configuration_description, configuration_group_id, sort_order, date_added) VALUES ('Company', 'ENTRY_COMPANY_MIN_LENGTH', '0', '{"error":"TEXT_MIN_ADMIN_COMPANY_LENGTH","id":"FILTER_VALIDATE_INT","options":{"options":{"min_range":0}}}', 'Minimum length of company name', '2', '6', now());
Expand Down
3 changes: 3 additions & 0 deletions zc_install/sql/updates/mysql_upgrade_zencart_200.sql
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ UPDATE configuration SET configuration_description = 'What is the weight of typi
# Add template_settings field
ALTER TABLE template_select ADD template_settings LONGTEXT DEFAULT NULL;

# Change minimum dob field length for new date VALIDATION
UPDATE configuration SET configuration_value = 8 WHERE configuration_key = 'ENTRY_DOB_MIN_LENGTH' AND configuration_value=10;


#### VERSION UPDATE STATEMENTS
## THE FOLLOWING 2 SECTIONS SHOULD BE THE "LAST" ITEMS IN THE FILE, so that if the upgrade fails prematurely, the version info is not updated.
Expand Down

0 comments on commit 0289240

Please sign in to comment.