1<?php
2
3/**
4 * Pure-PHP ANSI Decoder
5 *
6 * PHP version 5
7 *
8 * If you call read() in \phpseclib\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. \phpseclib\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 phpseclib\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    var $max_x;
39
40    /**
41     * Max Height
42     *
43     * @var int
44     * @access private
45     */
46    var $max_y;
47
48    /**
49     * Max History
50     *
51     * @var int
52     * @access private
53     */
54    var $max_history;
55
56    /**
57     * History
58     *
59     * @var array
60     * @access private
61     */
62    var $history;
63
64    /**
65     * History Attributes
66     *
67     * @var array
68     * @access private
69     */
70    var $history_attrs;
71
72    /**
73     * Current Column
74     *
75     * @var int
76     * @access private
77     */
78    var $x;
79
80    /**
81     * Current Row
82     *
83     * @var int
84     * @access private
85     */
86    var $y;
87
88    /**
89     * Old Column
90     *
91     * @var int
92     * @access private
93     */
94    var $old_x;
95
96    /**
97     * Old Row
98     *
99     * @var int
100     * @access private
101     */
102    var $old_y;
103
104    /**
105     * An empty attribute cell
106     *
107     * @var object
108     * @access private
109     */
110    var $base_attr_cell;
111
112    /**
113     * The current attribute cell
114     *
115     * @var object
116     * @access private
117     */
118    var $attr_cell;
119
120    /**
121     * An empty attribute row
122     *
123     * @var array
124     * @access private
125     */
126    var $attr_row;
127
128    /**
129     * The current screen text
130     *
131     * @var array
132     * @access private
133     */
134    var $screen;
135
136    /**
137     * The current screen attributes
138     *
139     * @var array
140     * @access private
141     */
142    var $attrs;
143
144    /**
145     * Current ANSI code
146     *
147     * @var string
148     * @access private
149     */
150    var $ansi;
151
152    /**
153     * Tokenization
154     *
155     * @var array
156     * @access private
157     */
158    var $tokenization;
159
160    /**
161     * Default Constructor.
162     *
163     * @return \phpseclib\File\ANSI
164     * @access public
165     */
166    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    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 = array();
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    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    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    function appendString($source)
233    {
234        $this->tokenization = array('');
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                    case "\x1B[K": // Clear screen from cursor right
272                        $this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x);
273
274                        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));
275                        break;
276                    case "\x1B[2K": // Clear entire line
277                        $this->screen[$this->y] = str_repeat(' ', $this->x);
278                        $this->attrs[$this->y] = $this->attr_row;
279                        break;
280                    case "\x1B[?1h": // set cursor key to application
281                    case "\x1B[?25h": // show the cursor
282                    case "\x1B(B": // set united states g0 character set
283                        break;
284                    case "\x1BE": // Move to next line
285                        $this->_newLine();
286                        $this->x = 0;
287                        break;
288                    default:
289                        switch (true) {
290                            case preg_match('#\x1B\[(\d+)B#', $this->ansi, $match): // Move cursor down n lines
291                                $this->old_y = $this->y;
292                                $this->y+= $match[1];
293                                break;
294                            case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match): // Move cursor to screen location v,h
295                                $this->old_x = $this->x;
296                                $this->old_y = $this->y;
297                                $this->x = $match[2] - 1;
298                                $this->y = $match[1] - 1;
299                                break;
300                            case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match): // Move cursor right n lines
301                                $this->old_x = $this->x;
302                                $this->x+= $match[1];
303                                break;
304                            case preg_match('#\x1B\[(\d+)D#', $this->ansi, $match): // Move cursor left n lines
305                                $this->old_x = $this->x;
306                                $this->x-= $match[1];
307                                if ($this->x < 0) {
308                                    $this->x = 0;
309                                }
310                                break;
311                            case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match): // Set top and bottom lines of a window
312                                break;
313                            case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match): // character attributes
314                                $attr_cell = &$this->attr_cell;
315                                $mods = explode(';', $match[1]);
316                                foreach ($mods as $mod) {
317                                    switch ($mod) {
318                                        case '':
319                                        case '0': // Turn off character attributes
320                                            $attr_cell = clone $this->base_attr_cell;
321                                            break;
322                                        case '1': // Turn bold mode on
323                                            $attr_cell->bold = true;
324                                            break;
325                                        case '4': // Turn underline mode on
326                                            $attr_cell->underline = true;
327                                            break;
328                                        case '5': // Turn blinking mode on
329                                            $attr_cell->blink = true;
330                                            break;
331                                        case '7': // Turn reverse video on
332                                            $attr_cell->reverse = !$attr_cell->reverse;
333                                            $temp = $attr_cell->background;
334                                            $attr_cell->background = $attr_cell->foreground;
335                                            $attr_cell->foreground = $temp;
336                                            break;
337                                        default: // set colors
338                                            //$front = $attr_cell->reverse ? &$attr_cell->background : &$attr_cell->foreground;
339                                            $front = &$attr_cell->{ $attr_cell->reverse ? 'background' : 'foreground' };
340                                            //$back = $attr_cell->reverse ? &$attr_cell->foreground : &$attr_cell->background;
341                                            $back = &$attr_cell->{ $attr_cell->reverse ? 'foreground' : 'background' };
342                                            switch ($mod) {
343                                                // @codingStandardsIgnoreStart
344                                                case '30': $front = 'black'; break;
345                                                case '31': $front = 'red'; break;
346                                                case '32': $front = 'green'; break;
347                                                case '33': $front = 'yellow'; break;
348                                                case '34': $front = 'blue'; break;
349                                                case '35': $front = 'magenta'; break;
350                                                case '36': $front = 'cyan'; break;
351                                                case '37': $front = 'white'; break;
352
353                                                case '40': $back = 'black'; break;
354                                                case '41': $back = 'red'; break;
355                                                case '42': $back = 'green'; break;
356                                                case '43': $back = 'yellow'; break;
357                                                case '44': $back = 'blue'; break;
358                                                case '45': $back = 'magenta'; break;
359                                                case '46': $back = 'cyan'; break;
360                                                case '47': $back = 'white'; break;
361                                                // @codingStandardsIgnoreEnd
362
363                                                default:
364                                                    //user_error('Unsupported attribute: ' . $mod);
365                                                    $this->ansi = '';
366                                                    break 2;
367                                            }
368                                    }
369                                }
370                                break;
371                            default:
372                                //user_error("{$this->ansi} is unsupported\r\n");
373                        }
374                }
375                $this->ansi = '';
376                continue;
377            }
378
379            $this->tokenization[count($this->tokenization) - 1].= $source[$i];
380            switch ($source[$i]) {
381                case "\r":
382                    $this->x = 0;
383                    break;
384                case "\n":
385                    $this->_newLine();
386                    break;
387                case "\x08": // backspace
388                    if ($this->x) {
389                        $this->x--;
390                        $this->attrs[$this->y][$this->x] = clone $this->base_attr_cell;
391                        $this->screen[$this->y] = substr_replace(
392                            $this->screen[$this->y],
393                            $source[$i],
394                            $this->x,
395                            1
396                        );
397                    }
398                    break;
399                case "\x0F": // shift
400                    break;
401                case "\x1B": // start ANSI escape code
402                    $this->tokenization[count($this->tokenization) - 1] = substr($this->tokenization[count($this->tokenization) - 1], 0, -1);
403                    //if (!strlen($this->tokenization[count($this->tokenization) - 1])) {
404                    //    array_pop($this->tokenization);
405                    //}
406                    $this->ansi.= "\x1B";
407                    break;
408                default:
409                    $this->attrs[$this->y][$this->x] = clone $this->attr_cell;
410                    if ($this->x > strlen($this->screen[$this->y])) {
411                        $this->screen[$this->y] = str_repeat(' ', $this->x);
412                    }
413                    $this->screen[$this->y] = substr_replace(
414                        $this->screen[$this->y],
415                        $source[$i],
416                        $this->x,
417                        1
418                    );
419
420                    if ($this->x > $this->max_x) {
421                        $this->x = 0;
422                        $this->_newLine();
423                    } else {
424                        $this->x++;
425                    }
426            }
427        }
428    }
429
430    /**
431     * Add a new line
432     *
433     * Also update the $this->screen and $this->history buffers
434     *
435     * @access private
436     */
437    function _newLine()
438    {
439        //if ($this->y < $this->max_y) {
440        //    $this->y++;
441        //}
442
443        while ($this->y >= $this->max_y) {
444            $this->history = array_merge($this->history, array(array_shift($this->screen)));
445            $this->screen[] = '';
446
447            $this->history_attrs = array_merge($this->history_attrs, array(array_shift($this->attrs)));
448            $this->attrs[] = $this->attr_row;
449
450            if (count($this->history) >= $this->max_history) {
451                array_shift($this->history);
452                array_shift($this->history_attrs);
453            }
454
455            $this->y--;
456        }
457        $this->y++;
458    }
459
460    /**
461     * Returns the current coordinate without preformating
462     *
463     * @access private
464     * @return string
465     */
466    function _processCoordinate($last_attr, $cur_attr, $char)
467    {
468        $output = '';
469
470        if ($last_attr != $cur_attr) {
471            $close = $open = '';
472            if ($last_attr->foreground != $cur_attr->foreground) {
473                if ($cur_attr->foreground != 'white') {
474                    $open.= '<span style="color: ' . $cur_attr->foreground . '">';
475                }
476                if ($last_attr->foreground != 'white') {
477                    $close = '</span>' . $close;
478                }
479            }
480            if ($last_attr->background != $cur_attr->background) {
481                if ($cur_attr->background != 'black') {
482                    $open.= '<span style="background: ' . $cur_attr->background . '">';
483                }
484                if ($last_attr->background != 'black') {
485                    $close = '</span>' . $close;
486                }
487            }
488            if ($last_attr->bold != $cur_attr->bold) {
489                if ($cur_attr->bold) {
490                    $open.= '<b>';
491                } else {
492                    $close = '</b>' . $close;
493                }
494            }
495            if ($last_attr->underline != $cur_attr->underline) {
496                if ($cur_attr->underline) {
497                    $open.= '<u>';
498                } else {
499                    $close = '</u>' . $close;
500                }
501            }
502            if ($last_attr->blink != $cur_attr->blink) {
503                if ($cur_attr->blink) {
504                    $open.= '<blink>';
505                } else {
506                    $close = '</blink>' . $close;
507                }
508            }
509            $output.= $close . $open;
510        }
511
512        $output.= htmlspecialchars($char);
513
514        return $output;
515    }
516
517    /**
518     * Returns the current screen without preformating
519     *
520     * @access private
521     * @return string
522     */
523    function _getScreen()
524    {
525        $output = '';
526        $last_attr = $this->base_attr_cell;
527        for ($i = 0; $i <= $this->max_y; $i++) {
528            for ($j = 0; $j <= $this->max_x; $j++) {
529                $cur_attr = $this->attrs[$i][$j];
530                $output.= $this->_processCoordinate($last_attr, $cur_attr, isset($this->screen[$i][$j]) ? $this->screen[$i][$j] : '');
531                $last_attr = $this->attrs[$i][$j];
532            }
533            $output.= "\r\n";
534        }
535        $output = substr($output, 0, -2);
536        // close any remaining open tags
537        $output.= $this->_processCoordinate($last_attr, $this->base_attr_cell, '');
538        return rtrim($output);
539    }
540
541    /**
542     * Returns the current screen
543     *
544     * @access public
545     * @return string
546     */
547    function getScreen()
548    {
549        return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $this->_getScreen() . '</pre>';
550    }
551
552    /**
553     * Returns the current screen and the x previous lines
554     *
555     * @access public
556     * @return string
557     */
558    function getHistory()
559    {
560        $scrollback = '';
561        $last_attr = $this->base_attr_cell;
562        for ($i = 0; $i < count($this->history); $i++) {
563            for ($j = 0; $j <= $this->max_x + 1; $j++) {
564                $cur_attr = $this->history_attrs[$i][$j];
565                $scrollback.= $this->_processCoordinate($last_attr, $cur_attr, isset($this->history[$i][$j]) ? $this->history[$i][$j] : '');
566                $last_attr = $this->history_attrs[$i][$j];
567            }
568            $scrollback.= "\r\n";
569        }
570        $base_attr_cell = $this->base_attr_cell;
571        $this->base_attr_cell = $last_attr;
572        $scrollback.= $this->_getScreen();
573        $this->base_attr_cell = $base_attr_cell;
574
575        return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $scrollback . '</span></pre>';
576    }
577}
578