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', dirname(__DIR__));
24 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
26 $helptext = <<<ENDOFHELP
27 console.php - provide an interactive PHP interpreter for testing
31 require_once INSTALLDIR.'/scripts/commandline.inc';
33 // Assume we're on a terminal if on Windows, otherwise posix_isatty tells us.
34 define('CONSOLE_INTERACTIVE', !function_exists('posix_isatty') || posix_isatty(0));
35 define('CONSOLE_READLINE', CONSOLE_INTERACTIVE && function_exists('readline'));
37 if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) {
38 define('CONSOLE_HISTORY', getenv("HOME") . "/.statusnet_console_history");
39 if (file_exists(CONSOLE_HISTORY)) {
40 readline_read_history(CONSOLE_HISTORY);
44 function read_input_line($prompt)
46 if (CONSOLE_INTERACTIVE) {
47 if (CONSOLE_READLINE) {
48 $line = readline($prompt);
49 if (trim($line) != '') {
50 readline_add_history($line);
51 if (defined('CONSOLE_HISTORY')) {
52 // Save often; it's easy to hit fatal errors.
53 readline_write_history(CONSOLE_HISTORY);
58 return readline_emulation($prompt);
66 * On Unix-like systems where PHP readline extension isn't present,
67 * -cough- Mac OS X -cough- we can shell out to bash to do it for us.
68 * This lets us at least handle things like arrow keys, but we don't
69 * get any entry history. :(
71 * Shamelessly ripped from when I wrote the same code for MediaWiki. :)
72 * @author Brion Vibber <brion@status.net>
74 * @param string $prompt
75 * @return mixed string on success, false on fail or EOF
77 function readline_emulation($prompt)
79 if (CONSOLE_INTERACTIVE && file_exists(trim(shell_exec('which bash')))) {
80 $encPrompt = escapeshellarg($prompt);
81 $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
82 $encCommand = escapeshellarg($command);
83 $metaCommand = "bash -c $encCommand";
85 // passthru passes our STDIN and TTY to the child...
86 // We can pull the returned string via output buffering.
89 passthru($metaCommand, $retval);
90 $line = ob_get_contents();
95 } elseif ($retval == 127) {
96 // Couldn't execute bash even though we thought we saw it.
97 // Shell probably spit out an error message, sorry :(
98 // Fall through to fgets()...
105 // Fallback... we'll have no editing controls, EWWW
109 if (CONSOLE_INTERACTIVE) {
115 function console_help()
117 print "Welcome to GNU social's interactive PHP console!\n";
118 print "Type some PHP code and it'll execute...\n";
120 print "Hint: return a value of any type to output it via var_export():\n";
121 print " \$profile = new Profile();\n";
122 print " \$profile->find();\n";
123 print " \$profile->fetch();\n";
124 print " return \$profile;\n";
126 print "Note that PHP is cranky and you can easily kill your session by mistyping.\n";
128 print "Type ctrl+D or enter 'exit' to exit.\n";
131 if (CONSOLE_INTERACTIVE) {
132 print "GNU social interactive PHP console... type ctrl+D or enter 'exit' to exit.\n";
133 $prompt = common_slugify(common_config('site', 'name')) . '> ';
137 while (!feof(STDIN)) {
138 $line = read_input_line($prompt);
139 if ($line === false) {
140 if (CONSOLE_INTERACTIVE) {
144 } elseif ($line !== '') {
146 if (trim($line) == 'exit') {
148 } elseif (trim($line) == 'help') {
154 $result = eval($line);
155 if ($result === false) {
157 } elseif ($result === null) {
160 // return value from eval'd code
163 } catch (Exception $e) {
164 print get_class($e) . ": " . $e->getMessage() . "\n";
167 if (CONSOLE_INTERACTIVE) {