6

I need to execute kdiff3 command in my desktop machine (localhost) from PHP script (using browser, not command line). I've given permission for the user www-data that is executing the scripts to execute kdiff3 using visudo. In fact, if I log in as www-data I can execute it without problems (sudo kdiff3 ..., it's configured to not ask for a password at all).

The problem is when I try to execute this command from a PHP script. I've tried this:

$output = shell_exec("sudo kdiff3 -m $file.def.old $file $file.def -o $file");

and nothing happens (output is NULL). If I try a non interactive command, like ls it works:

$output = shell_exec("ls");

What's happening? Why cannot execute an interactive command?

5 Answers 5

16

Try passthru($cmd);

It will allow user's I/O on the Terminal screen.

Edit (27 january 2024)

readline() is a better option.

Here is an example:

<?php
$invalid = TRUE;
while($invalid) {
    $input = strtolower(readline('Confirm (y/n): '));
    $invalid = !in_array($input, ['y', 'n']);
}
$yes = $input === 'y';
var_dump($yes);
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't seem to work correctly with interactive TUIs like vim
1

kdiff3 is an interactive graphical user interface program, so it use KDE and Qt and requires an X11 server.

And within a web server (i.e. in PHP running for Apache or Lighttpd) you don't have any X11 server.

So there is no way to use kdiff3 from inside a PHP script (unless the web server is running on your desktop Linux machine; and then you need to set appropriately the environment, notably DISPLAY and probably XAUTHORITY environment variable). However you could run a command line program like diff3 ... (using the popen tricks).

2 Comments

May be I shouldn't say "I need to execute kdiff3 command in my localhost". Better: "I need to execute kdiff3 command in my desktop machine" :)
Just adding DISPLAY=:0.0 to /etc/environment has make it work. Thanks!
1

The problem is that sudo is waiting for user input, as you have said, it is "interactive" - it has asked you for a password.

You can supply one by using proc_open() to execute the command and get separate I/O streams, so that you can parse the output and supply the password when required.

I have come across scripts that use this approach of echo "suPassword\n" | sudo cmd to do this, but I personally have found this to be a bit hit and miss.

6 Comments

You could configure sudo so that it does not ask for a pasword.
Yes, but that is a very bad idea for the web server user, which is why I did not suggest it. Allowing the web server user to do, effectively, whatever it wants is a recipe for serious trouble.
Agreed, but using sudo and kdiff3 from a web sever is already very bad taste....
It might just be less harm than writing the password in the application if you really restrict it to a specific command.
@BasileStarynkevitch: In fact, I have configured sudo to not ask for a password. It's writen in my question, but poorly expressed, sorry. This is not the problem.
|
0

Try to use exec :

$Output= exec( $command ,$dom,$return )

Comments

0

Every interactive command expects a working stdin - this is not given with shell_exec();

Use popen() and friends or redirect your input from a file. You might still be out of luck, if the file in question checks ISATTY

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.