Advertisement
Guest User

Untitled

a guest
May 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3. if (php_sapi_name() !== 'cli') {
  4. exit(1);
  5. }
  6.  
  7. function input() {
  8. return fgets(STDIN);
  9. }
  10.  
  11. function output($val) {
  12. fputs(STDOUT, $val);
  13. }
  14.  
  15. function colour($text = '', $colour = null) {
  16. $styles = array(
  17. 'green' => "\033[0;32m%s\033[0m",
  18. 'red' => "\033[31;31m%s\033[0m",
  19. 'yellow' => "\033[33;33m%s\033[0m",
  20. 'blue' => "\033[33;34m%s\033[0m",
  21. );
  22.  
  23. return sprintf(isset($styles[$colour]) ? $styles[$colour] : "%s", $text);
  24. }
  25.  
  26. function cmd($cmd, $input) {
  27. $input = trim($input);
  28. return substr($input, 0, strlen($cmd)) === $cmd;
  29. }
  30.  
  31. function snip($cmd, $input) {
  32. return substr($input, strlen($cmd));
  33. }
  34.  
  35. function asi($input) {
  36. if (trim($input) === '') {
  37. return $input;
  38. }
  39.  
  40. return rtrim($input, "\n;") . ';';
  41. }
  42.  
  43. output('PHP Version ' . colour(PHP_VERSION, 'green') . PHP_EOL);
  44. output('quit: Exit the REPL' . PHP_EOL);
  45. output('dump: Perform a vardump()' . PHP_EOL);
  46. output('printr: Perform a print_r()' . PHP_EOL);
  47. output('exec: Execute an external program' . PHP_EOL);
  48. output('>>>: Start a heredoc of PHP code' . PHP_EOL);
  49. output('<<<: End a heredoc of PHP code' . PHP_EOL);
  50. output('!!!: Discard a heredoc of PHP code' . PHP_EOL);
  51. output(PHP_EOL);
  52.  
  53. $buffer = '';
  54. $buffering = false;
  55.  
  56. while (true) {
  57. output(colour('> ', $buffering ? 'blue' : 'yellow'));
  58.  
  59. $input = input();
  60. if (cmd('quit', $input)) {
  61. exit;
  62. }
  63.  
  64. if (cmd('exec', $input)) {
  65. exec(snip('exec', $input), $result);
  66. $result = trim(implode("\n", $result));
  67. if (! empty($result)) {
  68. output($result . PHP_EOL);
  69. }
  70. continue;
  71. }
  72.  
  73. if (cmd('dump', $input)) {
  74. $input = 'var_dump(' . snip('dump', $input) . ');';
  75. } elseif (cmd('printr', $input)) {
  76. $input = 'print_r(' . snip('printr', $input) . ');';
  77. }
  78.  
  79. if (cmd('>>>', $input)) {
  80. $buffering = true;
  81. $input = snip('>>>', $input);
  82. } elseif (cmd('<<<', $input)) {
  83. if (! $buffering) {
  84. continue;
  85. }
  86.  
  87. $buffering = false;
  88. $input = snip('<<<', $input);
  89. } elseif (cmd('!!!', $input)) {
  90. $buffering = false;
  91. $buffer = '';
  92. continue;
  93. }
  94.  
  95. if ($buffering) {
  96. $buffer .= $input;
  97. continue;
  98. }
  99.  
  100. if (! empty($buffer)) {
  101. $input = $buffer;
  102. $buffer = '';
  103. } else {
  104. $input = asi($input) . PHP_EOL;
  105. }
  106.  
  107. eval($input);
  108. output(PHP_EOL);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement