Welcome Guest, Not a member yet? Register   Sign In
CLI::error throws exception when run from controller
#1

So I have a spark command where I call CLI::error("something went wrong"). This works fine when run from the command line.
When I run this same spark command inside a controller, it will throw the following exception:

Quote:CRITICAL - 2024-01-18 15:16:50 --> Error: Undefined constant "CodeIgniter\CLI\STDERR"

in SYSTEMPATH\CLI\CLI.php on line 488.

If I instead, change it to CLI::write() then there are no problems.
Is there something I need to include?
Reply
#2

Try:
Code:
--- a/system/CLI/CLI.php
+++ b/system/CLI/CLI.php
@@ -180,6 +180,10 @@ class CLI
            // we need to define STDOUT ourselves
            // For "! defined('STDOUT')" see: https://github.com/codeigniter4/CodeIgniter4/issues/7047
            define('STDOUT', 'php://output'); // @codeCoverageIgnore
+
+            if (! defined('STDERR')) {
+                define('STDERR', 'php://output'); // @codeCoverageIgnore
+            }
        }
    }

But I don't recommend to call CLI command via web.
Reply
#3

Thanks, I don't want to modify system files and keep track of them for upgrade reasons. I think I will just use CLI::print if is_cli() returns false.

Are there some known dangers to calling CLI commands via a controller?
Reply
#4

I just want you to check the patch works or not.
If it works, I will fix CI4.

The known dangers are:
1. If you fails to restrict access to the controller, an attacker may execute the command.
2. If the command takes long time, the PHP process may be killed by the server.
Reply
#5

(This post was last modified: 01-20-2024, 02:01 AM by jetspeed.)

Thanks, I get the following error with your suggested patch (with xammp):


Code:
CRITICAL - 2024-01-20 16:56:18 --> TypeError: sapi_windows_vt100_support(): Argument #1 ($stream) must be of type resource, string given
in SYSTEMPATH\CLI\CLI.php on line 705.
1 SYSTEMPATH\CLI\CLI.php(705): sapi_windows_vt100_support('php://output')
2 SYSTEMPATH\CLI\CLI.php(731): CodeIgniter\CLI\CLI::streamSupports('sapi_windows_vt100_support', 'php://output')
3 SYSTEMPATH\CLI\CLI.php(492): CodeIgniter\CLI\CLI::hasColorSupport('php://output')
Reply
#6

Oh, it seems CLI commands do not work at all via web on Windows.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB