]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/console.php
Merge branch '0.9.x' of git://gitorious.org/statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / scripts / console.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2009, StatusNet, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 # Abort if called from a web server
22
23 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
24
25 $helptext = <<<ENDOFHELP
26 console.php - provide an interactive PHP interpreter for testing
27
28 ENDOFHELP;
29
30 require_once INSTALLDIR.'/scripts/commandline.inc';
31
32 // Assume we're on a terminal if on Windows, otherwise posix_isatty tells us.
33 define('CONSOLE_INTERACTIVE', !function_exists('posix_isatty') || posix_isatty(0));
34 define('CONSOLE_READLINE', CONSOLE_INTERACTIVE && function_exists('readline'));
35
36 if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) {
37     define('CONSOLE_HISTORY', getenv("HOME") . "/.statusnet_console_history");
38     if (file_exists(CONSOLE_HISTORY)) {
39         readline_read_history(CONSOLE_HISTORY);
40     }
41 }
42
43 function read_input_line($prompt)
44 {
45     if (CONSOLE_INTERACTIVE) {
46         if (CONSOLE_READLINE) {
47             $line = readline($prompt);
48             readline_add_history($line);
49             if (defined('CONSOLE_HISTORY')) {
50                 // Save often; it's easy to hit fatal errors.
51                 readline_write_history(CONSOLE_HISTORY);
52             }
53             return $line;
54         } else {
55             return readline_emulation($prompt);
56         }
57     } else {
58         return fgets(STDIN);
59     }
60 }
61
62 /**
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. :(
67  *
68  * Shamelessly ripped from when I wrote the same code for MediaWiki. :)
69  * @author Brion Vibber <brion@status.net>
70  *
71  * @param string $prompt
72  * @return mixed string on success, false on fail or EOF
73  */
74 function readline_emulation($prompt)
75 {
76     if(CONSOLE_INTERACTIVE && 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";
81
82         // passthru passes our STDIN and TTY to the child...
83         // We can pull the returned string via output buffering.
84         ob_start();
85         $retval = false;
86         passthru($metaCommand, $retval);
87         $line = ob_get_contents();
88         ob_end_clean();
89
90         if ($retval == 0) {
91             return $line;
92         } elseif ($retval == 127) {
93             // Couldn't execute bash even though we thought we saw it.
94             // Shell probably spit out an error message, sorry :(
95             // Fall through to fgets()...
96         } else {
97             // EOF/ctrl+D
98             return false;
99         }
100     }
101
102     // Fallback... we'll have no editing controls, EWWW
103     if (feof(STDIN)) {
104         return false;
105     }
106     if (CONSOLE_INTERACTIVE) {
107         print $prompt;
108     }
109     return fgets(STDIN);
110 }
111
112 function console_help()
113 {
114     print "Welcome to StatusNet's interactive PHP console!\n";
115     print "Type some PHP code and it'll execute...\n";
116     print "\n";
117     print "Hint: return a value of any type to output it via var_export():\n";
118     print "  \$profile = new Profile();\n";
119     print "  \$profile->find();\n";
120     print "  \$profile->fetch();\n";
121     print "  return \$profile;\n";
122     print "\n";
123     print "Note that PHP is cranky and you can easily kill your session by mistyping.\n";
124     print "\n";
125     print "Type ctrl+D or enter 'exit' to exit.\n";
126 }
127
128 if (CONSOLE_INTERACTIVE) {
129     print "StatusNet interactive PHP console... type ctrl+D or enter 'exit' to exit.\n";
130     $prompt = common_config('site', 'name') . '> ';
131 } else {
132     $prompt = '';
133 }
134 while (!feof(STDIN)) {
135     $line = read_input_line($prompt);
136     if ($line === false) {
137         if (CONSOLE_INTERACTIVE) {
138             print "\n";
139         }
140         break;
141     } elseif ($line !== '') {
142         try {
143             if (trim($line) == 'exit') {
144                 break;
145             } elseif (trim($line) == 'help') {
146                 console_help();
147                 continue;
148             }
149             
150             // Let's do this!
151             $result = eval($line);
152             if ($result === false) {
153                 // parse error
154             } elseif ($result === null) {
155                 // no return
156             } else {
157                 // return value from eval'd code
158                 var_export($result);
159             }
160         } catch(Exception $e) {
161             print get_class($e) . ": " . $e->getMessage() . "\n";
162         }
163     }
164     if (CONSOLE_INTERACTIVE) {
165         print "\n";
166     }
167 }