From 9a5ac5b21a0cfda921c52c63086973a4f4b16845 Mon Sep 17 00:00:00 2001 From: Nick Liu Date: Tue, 23 Jan 2018 07:48:24 -0600 Subject: [PATCH] Permissive field validation in db_verify Even if it's a new installation of e107, `db_verify` will be too strict in validating database field defaults that are equivalent to each other. This can happen on some MySQL or MariaDB servers that correct the field definitions, which were not strictly correct in the table structures built into e107 in the first place. This commit introduces db_verify::validateFieldPermissive(), a backwards-compatible way to validate database field defaults. It works regardless of whether the MySQL server stores a corrected representation of the schema and does not require fixing the schema in any existing table structure files. Fixes: #2998 --- e107_handlers/db_verify_class.php | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/e107_handlers/db_verify_class.php b/e107_handlers/db_verify_class.php index ac33cb980..70c8e0938 100644 --- a/e107_handlers/db_verify_class.php +++ b/e107_handlers/db_verify_class.php @@ -131,6 +131,29 @@ class db_verify + } + + /** + * Permissive field validation + */ + private function validateFieldPermissive($expected, $actual) + { + // Permit actual text types that default to null even when + // expected does not explicitly default to null + if(0 === strcasecmp($expected['type'], $actual['type']) && + 1 === preg_match('/[A-Z]*TEXT/i', $expected['type']) && + 0 === strcasecmp($actual['default'], "DEFAULT NULL")) + { + $expected['default'] = $actual['default']; + } + + // Loosely typed default value for int-like types + if(1 === preg_match('/[A-Z]*INT/i', $expected['type'])) + { + $expected['default'] = preg_replace("/DEFAULT '(\d+)'/i", 'DEFAULT $1', $expected['default']); + $actual['default'] = preg_replace("/DEFAULT '(\d+)'/i", 'DEFAULT $1', $actual['default'] ); + } + return !count($off = array_diff_assoc($expected, $actual)); } /** @@ -347,7 +370,7 @@ class db_verify $this->results[$tbl][$field]['_valid'] = $info; $this->results[$tbl][$field]['_file'] = $selection; } - elseif(count($off = array_diff_assoc($info,$sqlFieldData[$field]))) + elseif(!$this->validateFieldPermissive($info, $sqlFieldData[$field])) { $this->errors[$tbl]['_status'] = 'mismatch'; $this->results[$tbl][$field]['_status'] = 'mismatch'; @@ -1671,4 +1694,4 @@ function table_list() */ -?> \ No newline at end of file +?>