123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- define('INSTALLDIR', dirname(__DIR__));
- define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
- $helptext = <<<ENDOFHELP
- console.php - provide an interactive PHP interpreter for testing
- ENDOFHELP;
- require_once INSTALLDIR.'/scripts/commandline.inc';
- define('CONSOLE_INTERACTIVE', !function_exists('posix_isatty') || posix_isatty(0));
- define('CONSOLE_READLINE', CONSOLE_INTERACTIVE && function_exists('readline'));
- if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) {
- define('CONSOLE_HISTORY', getenv("HOME") . "/.statusnet_console_history");
- if (file_exists(CONSOLE_HISTORY)) {
- readline_read_history(CONSOLE_HISTORY);
- }
- }
- function read_input_line($prompt)
- {
- if (CONSOLE_INTERACTIVE) {
- if (CONSOLE_READLINE) {
- $line = readline($prompt);
- if (trim($line) != '') {
- readline_add_history($line);
- if (defined('CONSOLE_HISTORY')) {
-
- readline_write_history(CONSOLE_HISTORY);
- }
- }
- return $line;
- } else {
- return readline_emulation($prompt);
- }
- } else {
- return fgets(STDIN);
- }
- }
- function readline_emulation($prompt)
- {
- if (CONSOLE_INTERACTIVE && file_exists(trim(shell_exec('which bash')))) {
- $encPrompt = escapeshellarg($prompt);
- $command = "read -er -p $encPrompt && echo \"\$REPLY\"";
- $encCommand = escapeshellarg($command);
- $metaCommand = "bash -c $encCommand";
-
-
- ob_start();
- $retval = false;
- passthru($metaCommand, $retval);
- $line = ob_get_contents();
- ob_end_clean();
- if ($retval == 0) {
- return $line;
- } elseif ($retval == 127) {
-
-
-
- } else {
-
- return false;
- }
- }
-
- if (feof(STDIN)) {
- return false;
- }
- if (CONSOLE_INTERACTIVE) {
- print $prompt;
- }
- return fgets(STDIN);
- }
- function console_help()
- {
- print "Welcome to GNU social's interactive PHP console!\n";
- print "Type some PHP code and it'll execute...\n";
- print "\n";
- print "Hint: return a value of any type to output it via var_export():\n";
- print " \$profile = new Profile();\n";
- print " \$profile->find();\n";
- print " \$profile->fetch();\n";
- print " return \$profile;\n";
- print "\n";
- print "Note that PHP is cranky and you can easily kill your session by mistyping.\n";
- print "\n";
- print "Type ctrl+D or enter 'exit' to exit.\n";
- }
- if (CONSOLE_INTERACTIVE) {
- print "GNU social interactive PHP console... type ctrl+D or enter 'exit' to exit.\n";
- $prompt = common_slugify(common_config('site', 'name')) . '> ';
- } else {
- $prompt = '';
- }
- while (!feof(STDIN)) {
- $line = read_input_line($prompt);
- if ($line === false) {
- if (CONSOLE_INTERACTIVE) {
- print "\n";
- }
- break;
- } elseif ($line !== '') {
- try {
- if (trim($line) == 'exit') {
- break;
- } elseif (trim($line) == 'help') {
- console_help();
- continue;
- }
-
- $result = eval($line);
- if ($result === false) {
-
- } elseif ($result === null) {
-
- } else {
-
- var_export($result);
- }
- } catch (Exception $e) {
- print get_class($e) . ": " . $e->getMessage() . "\n";
- }
- }
- if (CONSOLE_INTERACTIVE) {
- print "\n";
- }
- }
|