From 1e3c3ec9079c77f6d5d0defc133109347b2b2794 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 10 Mar 2023 14:06:50 +0100 Subject: [PATCH] MDL-76459 xmldb: Add environmental check to verify $CFG->prefix While, right now, sites using long (> 10 chars) $CFG->prefix can continue working (because we still don't have any table > 28 chars), as soon as some new table with long name is added, it won't work with PostgreSQL anymore (if the 63 limit is raised). Hence, this environmental check will verify on both install and upgrade that the $CFG->prefix is always <= 10 chars. Sites with longer prefixes will need to rename all their tables (and maybe other objects, depending on the dbtype) to use a shorter prefix. --- admin/environment.xml | 2 ++ lang/en/admin.php | 1 + lib/upgradelib.php | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/admin/environment.xml b/admin/environment.xml index 29725ac2701..502d2dab073 100644 --- a/admin/environment.xml +++ b/admin/environment.xml @@ -4301,6 +4301,8 @@ + + diff --git a/lang/en/admin.php b/lang/en/admin.php index 1b26f02ff8a..51d659acbc1 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -467,6 +467,7 @@ $string['customusermenuitems'] = 'User menu items'; $string['datarootsecurityerror'] = '

SECURITY WARNING!

Your dataroot directory is in the wrong location and is exposed to the web. This means that all your private files are available to anyone in the world, and some of them could be used by a cracker to obtain unauthorised administrative access to your site!

You must move dataroot directory ({$a}) to a new location that is not within your public web directory, and update the $CFG->dataroot setting in your config.php accordingly.

'; $string['datarootsecuritywarning'] = 'Your site configuration might not be secure. Please make sure that your dataroot directory ({$a}) is not directly accessible via web.'; +$string['dbprefixtoolong'] = 'Your site\'s database prefix ($CFG->prefix) is too long ({$a->current} characters). The maximum number of characters allowed is {$a->maximum}.'; $string['dbsessions'] = 'Use database for session information'; $string['debug'] = 'Debug messages'; $string['debugall'] = 'ALL: Show all reasonable PHP debug messages'; diff --git a/lib/upgradelib.php b/lib/upgradelib.php index 3199f5a10ee..c75fee43f19 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -2524,6 +2524,26 @@ function check_igbinary322_version(environment_results $result) { return $result; } +/** + * This function checks that the database prefix ($CFG->prefix) is <= 10 + * + * @param environment_results $result + * @return environment_results|null updated results object, or null if the prefix check is passing ok. + */ +function check_db_prefix_length(environment_results $result) { + global $CFG; + + $prefixlen = strlen($CFG->prefix) ?? 0; + if ($prefixlen > 10) { + $parameters = (object)['current' => $prefixlen, 'maximum' => 10]; + $result->setFeedbackStr(['dbprefixtoolong', 'admin', $parameters]); + $result->setInfo('db prefix too long'); + $result->setStatus(false); + return $result; + } + return null; // All, good. By returning null we hide the check. +} + /** * Assert the upgrade key is provided, if it is defined. *