codeigniter4 / CodeIgniter4

Open Source PHP Framework (originally from EllisLab)
https://codeigniter.com/
MIT License
5.39k stars 1.9k forks source link

Bug: `spark key:generate` doesn't generate `encryption.key` if no template #6838

Closed totoprayogo1916 closed 2 years ago

totoprayogo1916 commented 2 years ago

PHP Version

7.4

CodeIgniter4 Version

4.2.10

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

cli-server (PHP built-in webserver)

Database

No response

What happened?

Try deleting all entries in .env so you can sort out which ones to use. But when running spark key:generate no encryption.key is created even though the message on the shell is successful.

Steps to Reproduce

remove all .env then run php spark key:generate

Expected Output

In .env should create encryption.key = even without template

Anything else?

No response

paulbalandan commented 2 years ago

You mean you cleared the contents of .env?

totoprayogo1916 commented 2 years ago

You mean you cleared the contents of .env?

yes

datamweb commented 2 years ago

You mean you cleared the contents of .env?

yes

The question is, why should you delete the contents of this file?

totoprayogo1916 commented 2 years ago

The question is, why should you delete the contents of this file?

Here I only try it locally, not for production purposes.

paulbalandan commented 2 years ago

I'm not sure which behavior is more intuitive to handle such edge case:

  1. Output the generated key to the terminal instead, like using the --show flag; or
  2. Adding the line if not found in the .env
datamweb commented 2 years ago

I chose the second option.😀

michalsn commented 2 years ago

2️⃣

kenjis commented 2 years ago

2.

totoprayogo1916 commented 2 years ago

https://github.com/codeigniter4/CodeIgniter4/blob/46eb15cfbe2dde214222159f6a9cdf22c0ad2667/system/Commands/Encryption/GenerateKey.php#L166-L170

I tried this code it works. but I can't write the test

-        $ret = file_put_contents($envFile, preg_replace(
-            $this->keyPattern($oldKey),
-            "\nencryption.key = {$newKey}",
-            file_get_contents($envFile)
-        ));
+        // get the file contents
+        $contents = file_get_contents($envFile);
+
+        // is encryption.key found?
+        $searchfor = 'encryption.key';
+
+        // escape special characters in the query
+        $pattern = preg_quote($searchfor, '/');
+
+        // finalise the regular expression, matching the whole line
+        $pattern = "/^.*$pattern.*\$/m";
+
+        if (preg_match_all($pattern, $contents, $matches)) {
+            $ins = preg_replace(
+                $this->keyPattern($oldKey),
+                "\nencryption.key = {$newKey}",
+                file_get_contents($envFile)
+            );
+        }
+        else {
+            $ins = $contents . "\nencryption.key = {$newKey}";
+        }
+
+        $ret = file_put_contents($envFile, $ins);
paulbalandan commented 2 years ago

Fix is underway.