4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2009, StatusNet, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # Abort if called from a web server
23 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
25 $helptext = <<<ENDOFHELP
26 console.php - provide an interactive PHP interpreter for testing
30 require_once INSTALLDIR.'/scripts/commandline.inc';
32 if (function_exists('posix_isatty')) {
33 define('CONSOLE_INTERACTIVE', posix_isatty(0));
35 // Windows? Assume we're on a terminal. :P
36 define('CONSOLE_INTERACTIVE', false);
38 if (CONSOLE_INTERACTIVE) {
39 define('CONSOLE_READLINE', function_exists('readline'));
42 if (CONSOLE_READLINE && CONSOLE_INTERACTIVE && file_exists(CONSOLE_HISTORY)) {
43 define(CONSOLE_HISTORY, getenv("HOME") . "/.statusnet_console_history");
44 readline_read_history(CONSOLE_HISTORY);
47 function read_input_line($prompt)
49 if (CONSOLE_INTERACTIVE) {
50 if (CONSOLE_READLINE) {
51 $line = readline($prompt);
52 readline_add_history($line);
55 return readline_emulation($prompt);
63 * On Unix-like systems where PHP readline extension isn't present,
64 * -cough- Mac OS X -cough- we can shell out to bash to do it for us.
65 * This lets us at least handle things like arrow keys, but we don't
66 * get any entry history. :(
68 * Shamelessly ripped from when I wrote the same code for MediaWiki. :)
69 * @author Brion Vibber <brion@status.net>
71 * @param string $prompt
72 * @return mixed string on success, false on fail or EOF
74 function readline_emulation($prompt)
76 if(file_exists(trim(shell_exec('which bash')))) {
77 $encPrompt = escapeshellarg($prompt);
78 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
79 $encCommand = escapeshellarg($command);
80 $metaCommand = "bash -c $encCommand";
82 // passthru passes our STDIN and TTY to the child...
83 // We can pull the returned string via output buffering.
86 passthru($metaCommand, $retval);
87 $line = ob_get_contents();
92 } elseif ($retval == 127) {
93 // Could not execute bash even though we thought we saw it.
94 // Shell probably spit out an error message, sorry :(
95 // Fall through to fgets()...
102 // Fallback... we'll have no editing controls, EWWW
110 function console_help()
112 print "Welcome to StatusNet's interactive PHP console!\n";
113 print "Type some PHP code and it'll execute...\n";
115 print "Hint: return a value of any type to output it via var_export():\n";
116 print " \$profile = new Profile();\n";
117 print " \$profile->find();\n";
118 print " \$profile->fetch();\n";
119 print " return \$profile;\n";
121 print "Note that PHP is cranky and you can easily kill your session by mistyping.\n";
123 print "Type ctrl+D or enter 'exit' to exit.\n";
127 print "StatusNet interactive PHP console... type ctrl+D or enter 'exit' to exit.\n";
128 $prompt = common_config('site', 'name') . '> ';
129 while (!feof(STDIN)) {
130 $line = read_input_line($prompt);
131 if ($line === false) {
134 } elseif ($line !== '') {
136 if (trim($line) == 'exit') {
138 } elseif (trim($line) == 'help') {
144 $result = eval($line);
145 if ($result === false) {
147 } elseif ($result === null) {
150 // return value from eval'd code
153 } catch(Exception $e) {
154 print get_class($e) . ": " . $e->getMessage() . "\n";
160 if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) {
161 @readline_write_history(CONSOLE_HISTORY);