1<?php
2
3/**
4 * Pure-PHP ANSI Decoder
5 *
6 * PHP version 5
7 *
8 * If you call read() in \phpseclib3\Net\SSH2 you may get {@link http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape codes} back.
9 * They'd look like chr(0x1B) . '[00m' or whatever (0x1B = ESC).  They tell a
10 * {@link http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator} how to format the characters, what
11 * color to display them in, etc. \phpseclib3\File\ANSI is a {@link http://en.wikipedia.org/wiki/VT100 VT100} terminal emulator.
12 *
13 * @category  File
14 * @package   ANSI
15 * @author    Jim Wigginton <terrafrost@php.net>
16 * @copyright 2012 Jim Wigginton
17 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
18 * @link      http://phpseclib.sourceforge.net
19 */
20
21namespace phpseclib3\File;
22
23/**
24 * Pure-PHP ANSI Decoder
25 *
26 * @package ANSI
27 * @author  Jim Wigginton <terrafrost@php.net>
28 * @access  public
29 */
30class ANSI
31{
32    /**
33     * Max Width
34     *
35     * @var int
36     * @access private
37     */
38    private $max_x;
39
40    /**
41     * Max Height
42     *
43     * @var int
44     * @access private
45     */
46    private $max_y;
47
48    /**
49     * Max History
50     *
51     * @var int
52     * @access private
53     */
54    private $max_history;
55
56    /**
57     * History
58     *
59     * @var array
60     * @access private
61     */
62    private $history;
63
64    /**
65     * History Attributes
66     *
67     * @var array
68     * @access private
69     */
70    private $history_attrs;
71
72    /**
73     * Current Column
74     *
75     * @var int
76     * @access private
77     */
78    private $x;
79
80    /**
81     * Current Row
82     *
83     * @var int
84     * @access private
85     */
86    private $y;
87
88    /**
89     * Old Column
90     *
91     * @var int
92     * @access private
93     */
94    private $old_x;
95
96    /**
97     * Old Row
98     *
99     * @var int
100     * @access private
101     */
102    private $old_y;
103
104    /**
105     * An empty attribute cell
106     *
107     * @var object
108     * @access private
109     */
110    private $base_attr_cell;
111
112    /**
113     * The current attribute cell
114     *
115     * @var object
116     * @access private
117     */
118    private $attr_cell;
119
120    /**
121     * An empty attribute row
122     *
123     * @var array
124     * @access private
125     */
126    private $attr_row;
127
128    /**
129     * The current screen text
130     *
131     * @var list<string>
132     * @access private
133     */
134    private $screen;
135
136    /**
137     * The current screen attributes
138     *
139     * @var array
140     * @access private
141     */
142    private $attrs;
143
144    /**
145     * Current ANSI code
146     *
147     * @var string
148     * @access private
149     */
150    private $ansi;
151
152    /**
153     * Tokenization
154     *
155     * @var array
156     * @access private
157     */
158    private $tokenization;
159
160    /**
161     * Default Constructor.
162     *
163     * @return \phpseclib3\File\ANSI
164     * @access public
165     */
166    public function __construct()
167    {
168        $attr_cell = new \stdClass();
169        $attr_cell->bold = false;
170        $attr_cell->underline = false;
171        $attr_cell->blink = false;
172        $attr_cell->background = 'black';
173        $attr_cell->foreground = 'white';
174        $attr_cell->reverse = false;
175        $this->base_attr_cell = clone $attr_cell;
176        $this->attr_cell = clone $attr_cell;
177
178        $this->setHistory(200);
179        $this->setDimensions(80, 24);
180    }
181
182    /**
183     * Set terminal width and height
184     *
185     * Resets the screen as well
186     *
187     * @param int $x
188     * @param int $y
189     * @access public
190     */
191    public function setDimensions($x, $y)
192    {
193        $this->max_x = $x - 1;
194        $this->max_y = $y - 1;
195        $this->x = $this->y = 0;
196        $this->history = $this->history_attrs = [];
197        $this->attr_row = array_fill(0, $this->max_x + 2, $this->base_attr_cell);
198        $this->screen = array_fill(0, $this->max_y + 1, '');
199        $this->attrs = array_fill(0, $this->max_y + 1, $this->attr_row);
200        $this->ansi = '';
201    }
202
203    /**
204     * Set the number of lines that should be logged past the terminal height
205     *
206     * @param int $history
207     * @access public
208     */
209    public function setHistory($history)
210    {
211        $this->max_history = $history;
212    }
213
214    /**
215     * Load a string
216     *
217     * @param string $source
218     * @access public
219     */
220    public function loadString($source)
221    {
222        $this->setDimensions($this->max_x + 1, $this->max_y + 1);
223        $this->appendString($source);
224    }
225
226    /**
227     * Appdend a string
228     *
229     * @param string $source
230     * @access public
231     */
232    public function appendString($source)
233    {
234        $this->tokenization = [''];
235        for ($i = 0; $i < strlen($source); $i++) {
236            if (strlen($this->ansi)) {
237                $this->ansi .= $source[$i];
238                $chr = ord($source[$i]);
239                // http://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
240                // single character CSI's not currently supported
241                switch (true) {
242                    case $this->ansi == "\x1B=":
243                        $this->ansi = '';
244                        continue 2;
245                    case strlen($this->ansi) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['):
246                    case strlen($this->ansi) > 2 && $chr >= 64 && $chr <= 126:
247                        break;
248                    default:
249                        continue 2;
250                }
251                $this->tokenization[] = $this->ansi;
252                $this->tokenization[] = '';
253                // http://ascii-table.com/ansi-escape-sequences-vt-100.php
254                switch ($this->ansi) {
255                    case "\x1B[H": // Move cursor to upper left corner
256                        $this->old_x = $this->x;
257                        $this->old_y = $this->y;
258                        $this->x = $this->y = 0;
259                        break;
260                    case "\x1B[J": // Clear screen from cursor down
261                        $this->history = array_merge($this->history, array_slice(array_splice($this->screen, $this->y + 1), 0, $this->old_y));
262                        $this->screen = array_merge($this->screen, array_fill($this->y, $this->max_y, ''));
263
264                        $this->history_attrs = array_merge($this->history_attrs, array_slice(array_splice($this->attrs, $this->y + 1), 0, $this->old_y));
265                        $this->attrs = array_merge($this->attrs, array_fill($this->y, $this->max_y, $this->attr_row));
266
267                        if (count($this->history) == $this->max_history) {
268                            array_shift($this->history);
269                            array_shift($this->history_attrs);
270                        }
271                        // fall-through
272                    case "\x1B[K": // Clear screen from cursor right
273                        $this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x);
274
275                        array_splice($this->attrs[$this->y], $this->x + 1, $this->max_x - $this->x, array_fill($this->x, $this->max_x - ($this->x - 1), $this->base_attr_cell));
276                        break;
277                    case "\x1B[2K": // Clear entire line
278                        $this->screen[$this->y] = str_repeat(' ', $this->x);
279                        $this->attrs[$this->y] = $this->attr_row;
280                        break;
281                    case "\x1B[?1h": // set cursor key to application
282                    case "\x1B[?25h": // show the cursor
283                    case "\x1B(B": // set united states g0 character set
284                        break;
285                    case "\x1BE": // Move to next line
286                        $this->newLine();
287                        $this->x = 0;
288                        break;
289                    default:
290                        switch (true) {
291                            case preg_match('#\x1B\[(\d+)B#', $this->ansi, $match): // Move cursor down n lines
292                                $this->old_y = $this->y;
293                                $this->y += (int) $match[1];
294                                break;
295                            case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match): // Move cursor to screen location v,h
296                                $this->old_x = $this->x;
297                                $this->old_y = $this->y;
298                                $this->x = $match[2] - 1;
299                                $this->y = (int) $match[1] - 1;
300                                break;
301                            case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match): // Move cursor right n lines
302                                $this->old_x = $this->x;
303                                $this->x += $match[1];
304                                break;
305                            case preg_match('#\x1B\[(\d+)D#', $this->ansi, $match): // Move cursor left n lines
306                                $this->old_x = $this->x;
307                                $this->x -= $match[1];
308                                if ($this->x < 0) {
309                                    $this->x = 0;
310                                }
311                                break;
312                            case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match): // Set top and bottom lines of a window
313                                break;
314                            case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match): // character attributes
315                                $attr_cell = &$this->attr_cell;
316                                $mods = explode(';', $match[1]);
317                                foreach ($mods as $mod) {
318                                    switch ($mod) {
319                                        case '':
320                                        case '0': // Turn off character attributes
321                                            $attr_cell = clone $this->base_attr_cell;
322                                            break;
323                                        case '1': // Turn bold mode on
324                                            $attr_cell->bold = true;
325                                            break;
326                                        case '4': // Turn underline mode on
327                                            $attr_cell->underline = true;
328                                            break;
329                                        case '5': // Turn blinking mode on
330                                            $attr_cell->blink = true;
331                                            break;
332                                        case '7': // Turn reverse video on
333                                            $attr_cell->reverse = !$attr_cell->reverse;
334                                            $temp = $attr_cell->background;
335                                            $attr_cell->background = $attr_cell->foreground;
336                                            $attr_cell->foreground = $temp;
337                                            break;
338                                        default: // set colors
339                                            //$front = $attr_cell->reverse ? &$attr_cell->background : &$attr_cell->foreground;
340                                            $front = &$attr_cell->{ $attr_cell->reverse ? 'background' : 'foreground' };
341                                            //$back = $attr_cell->reverse ? &$attr_cell->foreground : &$attr_cell->background;
342                                            $back = &$attr_cell->{ $attr_cell->reverse ? 'foreground' : 'background' };
343                                            switch ($mod) {
344                                                // @codingStandardsIgnoreStart
345                                                case '30': $front = 'black'; break;
346                                                case '31': $front = 'red'; break;
347                                                case '32': $front = 'green'; break;
348                                                case '33': $front = 'yellow'; break;
349                                                case '34': $front = 'blue'; break;
350                                                case '35': $front = 'magenta'; break;
351                                                case '36': $front = 'cyan'; break;
352                                                case '37': $front = 'white'; break;
353
354                                                case '40': $back = 'black'; break;
355                                                case '41': $back = 'red'; break;
356                                                case '42': $back = 'green'; break;
357                                                case '43': $back = 'yellow'; break;
358                                                case '44': $back = 'blue'; break;
359                                                case '45': $back = 'magenta'; break;
360                                                case '46': $back = 'cyan'; break;
361                                                case '47': $back = 'white'; break;
362                                                // @codingStandardsIgnoreEnd
363
364                                                default:
365                                                    //user_error('Unsupported attribute: ' . $mod);
366                                                    $this->ansi = '';
367                                                    break 2;
368                                            }
369                                    }
370                                }
371                                break;
372                            default:
373                                //user_error("{$this->ansi} is unsupported\r\n");
374                        }
375                }
376                $this->ansi = '';
377                continue;
378            }
379
380            $this->tokenization[count($this->tokenization) - 1] .= $source[$i];
381            switch ($source[$i]) {
382                case "\r":
383                    $this->x = 0;
384                    break;
385                case "\n":
386                    $this->newLine();
387                    break;
388                case "\x08": // backspace
389                    if ($this->x) {
390                        $this->x--;
391                        $this->attrs[$this->y][$this->x] = clone $this->base_attr_cell;
392                        $this->screen[$this->y] = substr_replace(
393                            $this->screen[$this->y],
394                            $source[$i],
395                            $this->x,
396                            1
397                        );
398                    }
399                    break;
400                case "\x0F": // shift
401                    break;
402                case "\x1B": // start ANSI escape code
403                    $this->tokenization[count($this->tokenization) - 1] = substr($this->tokenization[count($this->tokenization) - 1], 0, -1);
404                    //if (!strlen($this->tokenization[count($this->tokenization) - 1])) {
405                    //    array_pop($this->tokenization);
406                    //}
407                    $this->ansi .= "\x1B";
408                    break;
409                default:
410                    $this->attrs[$this->y][$this->x] = clone $this->attr_cell;
411                    if ($this->x > strlen($this->screen[$this->y])) {
412                        $this->screen[$this->y] = str_repeat(' ', $this->x);
413                    }
414                    $this->screen[$this->y] = substr_replace(
415                        $this->screen[$this->y],
416                        $source[$i],
417                        $this->x,
418                        1
419                    );
420
421                    if ($this->x > $this->max_x) {
422                        $this->x = 0;
423                        $this->newLine();
424                    } else {
425                        $this->x++;
426                    }
427            }
428        }
429    }
430
431    /**
432     * Add a new line
433     *
434     * Also update the $this->screen and $this->history buffers
435     *
436     * @access private
437     */
438    private function newLine()
439    {
440        //if ($this->y < $this->max_y) {
441        //    $this->y++;
442        //}
443
444        while ($this->y >= $this->max_y) {
445            $this->history = array_merge($this->history, [array_shift($this->screen)]);
446            $this->screen[] = '';
447
448            $this->history_attrs = array_merge($this->history_attrs, [array_shift($this->attrs)]);
449            $this->attrs[] = $this->attr_row;
450
451            if (count($this->history) >= $this->max_history) {
452                array_shift($this->history);
453                array_shift($this->history_attrs);
454            }
455
456            $this->y--;
457        }
458        $this->y++;
459    }
460
461    /**
462     * Returns the current coordinate without preformating
463     *
464     * @access private
465     * @param \stdClass $last_attr
466     * @param \stdClass $cur_attr
467     * @param string $char
468     * @return string
469     */
470    private function processCoordinate($last_attr, $cur_attr, $char)
471    {
472        $output = '';
473
474        if ($last_attr != $cur_attr) {
475            $close = $open = '';
476            if ($last_attr->foreground != $cur_attr->foreground) {
477                if ($cur_attr->foreground != 'white') {
478                    $open .= '<span style="color: ' . $cur_attr->foreground . '">';
479                }
480                if ($last_attr->foreground != 'white') {
481                    $close = '</span>' . $close;
482                }
483            }
484            if ($last_attr->background != $cur_attr->background) {
485                if ($cur_attr->background != 'black') {
486                    $open .= '<span style="background: ' . $cur_attr->background . '">';
487                }
488                if ($last_attr->background != 'black') {
489                    $close = '</span>' . $close;
490                }
491            }
492            if ($last_attr->bold != $cur_attr->bold) {
493                if ($cur_attr->bold) {
494                    $open .= '<b>';
495                } else {
496                    $close = '</b>' . $close;
497                }
498            }
499            if ($last_attr->underline != $cur_attr->underline) {
500                if ($cur_attr->underline) {
501                    $open .= '<u>';
502                } else {
503                    $close = '</u>' . $close;
504                }
505            }
506            if ($last_attr->blink != $cur_attr->blink) {
507                if ($cur_attr->blink) {
508                    $open .= '<blink>';
509                } else {
510                    $close = '</blink>' . $close;
511                }
512            }
513            $output .= $close . $open;
514        }
515
516        $output .= htmlspecialchars($char);
517
518        return $output;
519    }
520
521    /**
522     * Returns the current screen without preformating
523     *
524     * @access private
525     * @return string
526     */
527    private function getScreenHelper()
528    {
529        $output = '';
530        $last_attr = $this->base_attr_cell;
531        for ($i = 0; $i <= $this->max_y; $i++) {
532            for ($j = 0; $j <= $this->max_x; $j++) {
533                $cur_attr = $this->attrs[$i][$j];
534                $output .= $this->processCoordinate($last_attr, $cur_attr, isset($this->screen[$i][$j]) ? $this->screen[$i][$j] : '');
535                $last_attr = $this->attrs[$i][$j];
536            }
537            $output .= "\r\n";
538        }
539        $output = substr($output, 0, -2);
540        // close any remaining open tags
541        $output .= $this->processCoordinate($last_attr, $this->base_attr_cell, '');
542        return rtrim($output);
543    }
544
545    /**
546     * Returns the current screen
547     *
548     * @access public
549     * @return string
550     */
551    public function getScreen()
552    {
553        return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $this->getScreenHelper() . '</pre>';
554    }
555
556    /**
557     * Returns the current screen and the x previous lines
558     *
559     * @access public
560     * @return string
561     */
562    public function getHistory()
563    {
564        $scrollback = '';
565        $last_attr = $this->base_attr_cell;
566        for ($i = 0; $i < count($this->history); $i++) {
567            for ($j = 0; $j <= $this->max_x + 1; $j++) {
568                $cur_attr = $this->history_attrs[$i][$j];
569                $scrollback .= $this->processCoordinate($last_attr, $cur_attr, isset($this->history[$i][$j]) ? $this->history[$i][$j] : '');
570                $last_attr = $this->history_attrs[$i][$j];
571            }
572            $scrollback .= "\r\n";
573        }
574        $base_attr_cell = $this->base_attr_cell;
575        $this->base_attr_cell = $last_attr;
576        $scrollback .= $this->getScreen();
577        $this->base_attr_cell = $base_attr_cell;
578
579        return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $scrollback . '</span></pre>';
580    }
581}
582