2

I'm trying to write a helper script for doing various admin tasks on a server which can only be done from the command line and trying to use the 'dialog' command to display message boxes, inputs, password prompts etc, however, the needs of this task call for me to process the data in PHP.

I'm having problems getting the dialog command to work in this way and can't figure out what i'm doing wrong.

There's an example here

Unfortunately it doesn't work.

When you run PHP and exec/backtick/system to an external application, the IO doesn't appear to work how you'd expect.

The nearest I can get is using the passthru() command:

<?php
  $CMD = "dialog --menu \"Please select\" 10 40 3 backup \"Backup Files\" restore \"Restore Files\"";
  passthru($CMD);
?>

This is the only way that PHP will let dialog use the STDOUT properly, anything else results in no display but you can press return to select an option.

I've tried backticks, exec() and system() but nothing seems to work.

What I wondered was how to read STDERR properly from within PHP to get the return value into a variable called $result.

I'm sure some other sysadmins have had to do this before.

My reasons for not using bash for this are that one command I have to execute as a result of a selection produces XML output only and I can't parse that effectively in bash.

5 Answers 5

4

Just in case someone else is searching for this:

function dialog ($args) {
    $pipes = array (NULL, NULL, NULL);
    // Allow user to interact with dialog
    $in = fopen ('php://stdin', 'r');
    $out = fopen ('php://stdout', 'w');
    // But tell PHP to redirect stderr so we can read it
    $p = proc_open ('dialog '.$args, array (
        0 => $in,
        1 => $out,
        2 => array ('pipe', 'w')
    ), $pipes);
    // Wait for and read result
    $result = stream_get_contents ($pipes[2]);
    // Close all handles
    fclose ($pipes[2]);
    fclose ($out);
    fclose ($in);
    proc_close ($p);
    // Return result
    return $result;
}

It requires dialog (apt-get install dialog) and proc_xxx (PHP 4.3.0, PHP 5)

It works, at least for me. :)

4

You can use proc_open() but not as show above... All dialog boxes do not work in same way. I provides a concrete sample below:

#!/usr/bin/env php
<?php

$pipes = array();
$process = null;
$output = '';
$ret = -1;

/**
 * Start process
 *
 * @param string $cmd Command to execute
 * @param bool $wantinputfd Whether or not input fd (pipe) is required
 * @retun void
 */
function processStart($cmd, $wantinputfd = false)
{
    global $process, $pipes;

    $process = proc_open(
        $cmd,
        array(
            0 => ($wantinputfd) ? array('pipe', 'r') : STDIN, // pipe/fd from which child will read
            1 => STDOUT,
            2 => array('pipe', 'w'), // pipe to which child will write any errors
            3 => array('pipe', 'w') // pipe to which child will write any output
        ),
        $pipes
    );
}

/**
 * Stop process
 *
 * @return void
 */
function processStop()
{
    global $output, $pipes, $process, $ret;

    if (isset($pipes[0]) {
        fclose($pipes[0]);
        usleep(2000);
    }

    $output = '';
    while ($_ = fgets($pipes[3])) {
        $output .= $_;
    }

    $errors = '';
    while ($_ = fgets($pipes[2])) {
        fwrite(STDERR, $_);
        $errors++;
    }

    if ($errors) {
        fwrite(STDERR, "dialog output the above errors, giving up!\n");
        exit(1);
    }

    fclose($pipes[2]);
    fclose($pipes[3]);

    do {
        usleep(2000);
        $status = proc_get_status($process);
    } while ($status['running']);

    proc_close($process);
    $ret = $status['exitcode'];
}

// Test for yesno dialog box
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --yesno 'yesno dialog box' 0 70");
processStop();
echo "Exit code is $ret\n";

// Test for gauge dialog box
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --gauge  'Gauge dialog box' 0 70 0", true);
sleep(1);
fwrite($pipes[0], "XXX\n0\nFirst step\nXXX\n20\n");
sleep(1);
fwrite($pipes[0], "XXX\n20\nSecond step\nXXX\n50\n");
sleep(1);
fwrite($pipes[0], "XXX\n50\nThird step\nXXX\n80\n");
sleep(1);
fwrite($pipes[0], "XXX\n80\nFourth step\nXXX\n100\n");
sleep(1);
processStop();
echo "Exit code is $ret\n";

// Test for input dialog box
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --inputbox 'input dialog box' 0 70");
processStop();
echo "Output is $output\n";
echo "Exit code is $ret\n";

// Test for errors output
processStart("dialog --backtitle 'dialog test' --title 'Little test' --output-fd 3 --dummy 'my input box' 0 70");
processStop();

exit(0);
3
  • You can run Debian whiptail in same way. Commented Jul 7, 2012 at 2:19
  • 1
    Of course, an object approach sound better. Using globals variables is not recommended (A least, from my side...). Commented Jul 7, 2012 at 2:21
  • Thanks guys, I need something very very portable as what i've ended up doing is writing a cross-distribution deployment of a project so as long as dialog is present and php's command line version (which I can pack in the distro) then I can get it to work, i'll try Nuxwin's solution shortly.
    – JamesB
    Commented Aug 16, 2012 at 10:06
2

You can use proc_open() to run a command and interact on all pipes, STDIN, STDOUT and STDERR:

$pipes = array(NULL, NULL, NULL);
$proc = proc_open(
    "dialog --gauge ..",
    array(
        0 => array('pipe', 'r'),
        1 => array('pipe', 'w'),
        2 => array('pipe', 'w'),
    ),
    $pipes
);
print fgets($pipes[2]);

See the manual for more examples.

0
1

PHP-GTK looks like a solution for this problem http://gtk.php.net/

1
  • PHP-GTK is probably too heavyweight for my needs as it'll run in SSH and I don't need the overhead of the pre-install to be able to do a deployment helper although I am very excited to use PHP-GTK for other things.
    – JamesB
    Commented Aug 16, 2012 at 10:07
0

I think you can't run ncurses application through PHP like this - maybe you should take a look at this: http://php.net/manual/de/book.ncurses.php

1
  • For my particular application i'll take this answer as the accepted answer, after research and trying the proc_open() method above which didn't work, i've concluded that you can't do this as nfo said. I've had to implement a dumb menu using readline() to get the solution I needed, it's a shame as I would have loved to put directory browsers in there.
    – JamesB
    Commented Jan 19, 2011 at 14:33

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.