]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/simple_console.php
Make attachment fit better in notice: drop text and link
[quix0rs-gnu-social.git] / scripts / simple_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', dirname(__DIR__));
24 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
25
26 $helptext = <<<ENDOFHELP
27 console.php - provide an interactive PHP interpreter for testing
28
29 ENDOFHELP;
30
31 require_once INSTALLDIR.'/scripts/commandline.inc';
32
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'));
36
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);
41     }
42 }
43
44 function read_input_line($prompt)
45 {
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);
54                 }
55             }
56             return $line;
57         } else {
58             return readline_emulation($prompt);
59         }
60     } else {
61         return fgets(STDIN);
62     }
63 }
64
65 /**
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. :(
70  *
71  * Shamelessly ripped from when I wrote the same code for MediaWiki. :)
72  * @author Brion Vibber <brion@status.net>
73  *
74  * @param string $prompt
75  * @return mixed string on success, false on fail or EOF
76  */
77 function readline_emulation($prompt)
78 {
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";
84
85         // passthru passes our STDIN and TTY to the child...
86         // We can pull the returned string via output buffering.
87         ob_start();
88         $retval = false;
89         passthru($metaCommand, $retval);
90         $line = ob_get_contents();
91         ob_end_clean();
92
93         if ($retval == 0) {
94             return $line;
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()...
99         } else {
100             // EOF/ctrl+D
101             return false;
102         }
103     }
104
105     // Fallback... we'll have no editing controls, EWWW
106     if (feof(STDIN)) {
107         return false;
108     }
109     if (CONSOLE_INTERACTIVE) {
110         print $prompt;
111     }
112     return fgets(STDIN);
113 }
114
115 function console_help()
116 {
117     print "Welcome to GNU social's interactive PHP console!\n";
118     print "Type some PHP code and it'll execute...\n";
119     print "\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";
125     print "\n";
126     print "Note that PHP is cranky and you can easily kill your session by mistyping.\n";
127     print "\n";
128     print "Type ctrl+D or enter 'exit' to exit.\n";
129 }
130
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')) . '> ';
134 } else {
135     $prompt = '';
136 }
137 while (!feof(STDIN)) {
138     $line = read_input_line($prompt);
139     if ($line === false) {
140         if (CONSOLE_INTERACTIVE) {
141             print "\n";
142         }
143         break;
144     } elseif ($line !== '') {
145         try {
146             if (trim($line) == 'exit') {
147                 break;
148             } elseif (trim($line) == 'help') {
149                 console_help();
150                 continue;
151             }
152
153             // Let's do this!
154             $result = eval($line);
155             if ($result === false) {
156                 // parse error
157             } elseif ($result === null) {
158                 // no return
159             } else {
160                 // return value from eval'd code
161                 var_export($result);
162             }
163         } catch (Exception $e) {
164             print get_class($e) . ": " . $e->getMessage() . "\n";
165         }
166     }
167     if (CONSOLE_INTERACTIVE) {
168         print "\n";
169     }
170 }