From: Brion Vibber Date: Fri, 6 Nov 2009 13:31:39 +0000 (+0100) Subject: console.php terminal script provides interactive PHP console in StatusNet environment... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4b7a36ea1932cfd2245560fa9949dc32fe2d2fc7;p=quix0rs-gnu-social.git console.php terminal script provides interactive PHP console in StatusNet environment, handy for testing! Uses readline for line input editing if available; falls back to bash+readline if not native, and takes fgets() in worst case. Currently a bit awkward in that each input line is parsed separately, so loops and function defs have to be squished to one line. --- diff --git a/scripts/console.php b/scripts/console.php new file mode 100755 index 0000000000..e65529a8d2 --- /dev/null +++ b/scripts/console.php @@ -0,0 +1,153 @@ +#!/usr/bin/env php +. + */ + +# Abort if called from a web server + +define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); + +$helptext = << + * + * @param string $prompt + * @return mixed string on success, false on fail or EOF + */ +function readline_emulation($prompt) +{ + if(file_exists(trim(shell_exec('which bash')))) { + $encPrompt = escapeshellarg($prompt); + $command = "read -er -p $encPrompt && echo \"\$REPLY\""; + $encCommand = escapeshellarg($command); + $metaCommand = "bash -c $encCommand"; + + // passthru passes our STDIN and TTY to the child... + // We can pull the returned string via output buffering. + ob_start(); + $retval = false; + passthru($metaCommand, $retval); + $line = ob_get_contents(); + ob_end_clean(); + + if ($retval == 0) { + return $line; + } elseif ($retval == 127) { + // Couldn't execute bash even though we thought we saw it. + // Shell probably spit out an error message, sorry :( + // Fall through to fgets()... + } else { + // EOF/ctrl+D + return false; + } + } + + // Fallback... we'll have no editing controls, EWWW + if (feof(STDIN)) { + return false; + } + print $prompt; + return fgets(STDIN); +} + +function console_help() +{ + print "Welcome to StatusNet's interactive PHP console!\n"; + print "Type some PHP code and it'll run!\n"; + print "\n"; + print "Note that PHP is cranky and you can easily kill your session.\n"; +} + + +$prompt = common_config('site', 'name') . '> '; +while (!feof(STDIN)) { + $line = read_input_line($prompt); + if ($line === false) { + print "\n"; + break; + } elseif ($line !== '') { + try { + if ($line == 'exit') { + break; + } elseif ($line == 'help') { + console_help(); + continue; + } + + // Let's do this + $result = eval($line); + if ($result === false) { + // parse error + } elseif ($result === null) { + // no return + } else { + // return value from eval'd code + var_export($result); + } + } catch(Exception $e) { + print get_class($e) . ": " . $e->getMessage() . "\n"; + } + } + print "\n"; +} + +if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) { + @readline_write_history(CONSOLE_HISTORY); +}