Repository: codeigniter4/CodeIgniter4 (PHP)

Issue:
Bug: Unable to connect to the database when name contains word `export`
### PHP Version

8.1

### CodeIgniter4 Version

4.2.1

### CodeIgniter4 Installation Method

Composer (using `codeigniter4/appstarter`)

### Which operating systems have you tested for this bug?

Windows

### Which server did you use?

cli

### Database

PostgreSQL

### What happened?

Unable to connect to the database when database group name contains word _export_, even is it's only a partial name, ie. _exports_data_ will also fail.

### Steps to Reproduce

Define db group with word `export` in .env file:
```
database.exports_test.hostname = localhost
database.exports_test.database = dev_db
database.exports_test.username = postgres
database.exports_test.password = postgres
database.exports_test.DBDriver = Postgre
database.exports_test.DBDebug = FALSE
database.exports_test.schema = exports_test
database.exports_test.port = 5432
```

define this group in app/Config/Database.php:
```
public $exports_test= [
    'hostname' => '',
    'schema' => '',
    'username' => '',
    'password' => '',
    'database' => '',
    'DBDriver' => '',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
];
```

try to run a query against the db. You will receive `You have not selected a database type to connect to` error message

### Expected Output

Query results

### Anything else?

_No response_


Discussion:
Thank you for reporting.

Is this because of *database group name* or *database schema name* ?

I think this is because of `DotEnv` class replacing `export` to empty string during sanitization.
group name. If I change the group name to `expor`, but keep the schema name it then works as it should
This is a bug in `DotEnv`.
A workaround is to set in the Config file.



Patch:
diff --git a/system/Config/DotEnv.php b/system/Config/DotEnv.php
index ddc590cd5986..724524e2d590 100644
--- a/system/Config/DotEnv.php
+++ b/system/Config/DotEnv.php
@@ -116,7 +116,8 @@ public function normaliseVariable(string $name, string $value = ''): array
         $value = trim($value);
 
         // Sanitize the name
-        $name = str_replace(['export', '\'', '"'], '', $name);
+        $name = preg_replace('/^export[ \t]++(\S+)/', '$1', $name);
+        $name = str_replace(['\'', '"'], '', $name);
 
         // Sanitize the value
         $value = $this->sanitizeValue($value);


Test patch:
diff --git a/tests/system/Config/DotEnvTest.php b/tests/system/Config/DotEnvTest.php
index 0b9dcc984e5a..e56aece895cc 100644
--- a/tests/system/Config/DotEnvTest.php
+++ b/tests/system/Config/DotEnvTest.php
@@ -55,14 +55,30 @@ public function testReturnsFalseIfCannotFindFile()
         $this->assertFalse($dotenv->load());
     }
 
-    public function testLoadsVars()
+    /**
+     * @dataProvider provideLoadVars
+     */
+    public function testLoadsVars(string $expected, string $varname)
     {
         $dotenv = new DotEnv($this->fixturesFolder);
         $dotenv->load();
-        $this->assertSame('bar', getenv('FOO'));
-        $this->assertSame('baz', getenv('BAR'));
-        $this->assertSame('with spaces', getenv('SPACED'));
-        $this->assertSame('', getenv('NULL'));
+
+        $this->assertSame($expected, getenv($varname));
+    }
+
+    public function provideLoadVars(): iterable
+    {
+        yield from [
+            ['bar', 'FOO'],
+            ['baz', 'BAR'],
+            ['with spaces', 'SPACED'],
+            ['', 'NULL'],
+            ['exported foo', 'char.expo.foo'],
+            ['variable', 'character.export.var'],
+            ['character', 'char.var'],
+            ['imports', 'char.exports'],
+            ['banana', 'fruit.export'],
+        ];
     }
 
     /**
diff --git a/tests/system/Config/fixtures/.env b/tests/system/Config/fixtures/.env
index 17d9e8e9116e..b850247a79a5 100644
--- a/tests/system/Config/fixtures/.env
+++ b/tests/system/Config/fixtures/.env
@@ -4,6 +4,12 @@ SPACED="with spaces"
 
 NULL=
 
+char.expo.foo="exported foo"
+character.export.var=variable
+export char.var=character
+export char.exports=imports
+fruit.export = "banana"
+
 SimpleConfig.onedeep=baz
 SimpleConfig.default.name=ci4
 # Use underscore as separator