2014-03-30 06:57:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP ANSI Decoder
|
|
|
|
*
|
|
|
|
* PHP versions 4 and 5
|
|
|
|
*
|
|
|
|
* If you call read() in Net_SSH2 you may get {@link http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape codes} back.
|
|
|
|
* They'd look like chr(0x1B) . '[00m' or whatever (0x1B = ESC). They tell a
|
|
|
|
* {@link http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator} how to format the characters, what
|
|
|
|
* color to display them in, etc. File_ANSI is a {@link http://en.wikipedia.org/wiki/VT100 VT100} terminal emulator.
|
|
|
|
*
|
|
|
|
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @category File
|
|
|
|
* @package File_ANSI
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2015-07-06 19:44:12 +00:00
|
|
|
* @copyright 2012 Jim Wigginton
|
2014-03-30 06:57:20 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pure-PHP ANSI Decoder
|
|
|
|
*
|
|
|
|
* @package File_ANSI
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
class File_ANSI
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Max Width
|
|
|
|
*
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $max_x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Max Height
|
|
|
|
*
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $max_y;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Max History
|
|
|
|
*
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $max_history;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History
|
|
|
|
*
|
|
|
|
* @var Array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $history;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History Attributes
|
|
|
|
*
|
|
|
|
* @var Array
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $history_attrs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current Column
|
|
|
|
*
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current Row
|
|
|
|
*
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $y;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Old Column
|
|
|
|
*
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $old_x;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Old Row
|
|
|
|
*
|
|
|
|
* @var Integer
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
var $old_y;
|
|
|
|
|
|
|
|
/**
|
2015-10-04 13:24:43 +00:00
|
|
|
* An empty attribute cell
|
2014-03-30 06:57:20 +00:00
|
|
|
*
|
2015-10-04 13:24:43 +00:00
|
|
|
* @var Object
|
2014-03-30 06:57:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
var $base_attr_cell;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-04 13:24:43 +00:00
|
|
|
* The current attribute cell
|
2014-03-30 06:57:20 +00:00
|
|
|
*
|
2015-10-04 13:24:43 +00:00
|
|
|
* @var Object
|
2014-03-30 06:57:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
var $attr_cell;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-04 13:24:43 +00:00
|
|
|
* An empty attribute row
|
2014-03-30 06:57:20 +00:00
|
|
|
*
|
|
|
|
* @var Array
|
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
var $attr_row;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-04 13:24:43 +00:00
|
|
|
* The current screen text
|
2014-03-30 06:57:20 +00:00
|
|
|
*
|
2015-10-04 13:24:43 +00:00
|
|
|
* @var Array
|
2014-03-30 06:57:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
var $screen;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-04 13:24:43 +00:00
|
|
|
* The current screen attributes
|
2014-03-30 06:57:20 +00:00
|
|
|
*
|
2015-10-04 13:24:43 +00:00
|
|
|
* @var Array
|
2014-03-30 06:57:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
var $attrs;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-04 13:24:43 +00:00
|
|
|
* Current ANSI code
|
2014-03-30 06:57:20 +00:00
|
|
|
*
|
2015-10-04 13:24:43 +00:00
|
|
|
* @var String
|
2014-03-30 06:57:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
var $ansi;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
/**
|
2015-10-04 13:24:43 +00:00
|
|
|
* Tokenization
|
2014-03-30 06:57:20 +00:00
|
|
|
*
|
2015-10-04 13:24:43 +00:00
|
|
|
* @var Array
|
2014-03-30 06:57:20 +00:00
|
|
|
* @access private
|
|
|
|
*/
|
2015-10-04 13:24:43 +00:00
|
|
|
var $tokenization;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default Constructor.
|
|
|
|
*
|
|
|
|
* @return File_ANSI
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function File_ANSI()
|
|
|
|
{
|
2015-10-04 13:24:43 +00:00
|
|
|
$attr_cell = new stdClass();
|
|
|
|
$attr_cell->bold = false;
|
|
|
|
$attr_cell->underline = false;
|
|
|
|
$attr_cell->blink = false;
|
|
|
|
$attr_cell->background = 'black';
|
|
|
|
$attr_cell->foreground = 'white';
|
|
|
|
$attr_cell->reverse = false;
|
|
|
|
$this->base_attr_cell = clone($attr_cell);
|
|
|
|
$this->attr_cell = clone($attr_cell);
|
|
|
|
|
2014-03-30 06:57:20 +00:00
|
|
|
$this->setHistory(200);
|
|
|
|
$this->setDimensions(80, 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set terminal width and height
|
|
|
|
*
|
|
|
|
* Resets the screen as well
|
|
|
|
*
|
|
|
|
* @param Integer $x
|
|
|
|
* @param Integer $y
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setDimensions($x, $y)
|
|
|
|
{
|
|
|
|
$this->max_x = $x - 1;
|
|
|
|
$this->max_y = $y - 1;
|
|
|
|
$this->x = $this->y = 0;
|
|
|
|
$this->history = $this->history_attrs = array();
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->attr_row = array_fill(0, $this->max_x + 2, $this->base_attr_cell);
|
2014-03-30 06:57:20 +00:00
|
|
|
$this->screen = array_fill(0, $this->max_y + 1, '');
|
|
|
|
$this->attrs = array_fill(0, $this->max_y + 1, $this->attr_row);
|
|
|
|
$this->ansi = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the number of lines that should be logged past the terminal height
|
|
|
|
*
|
|
|
|
* @param Integer $x
|
|
|
|
* @param Integer $y
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function setHistory($history)
|
|
|
|
{
|
|
|
|
$this->max_history = $history;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a string
|
|
|
|
*
|
|
|
|
* @param String $source
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function loadString($source)
|
|
|
|
{
|
|
|
|
$this->setDimensions($this->max_x + 1, $this->max_y + 1);
|
|
|
|
$this->appendString($source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appdend a string
|
|
|
|
*
|
|
|
|
* @param String $source
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
function appendString($source)
|
|
|
|
{
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->tokenization = array('');
|
2014-03-30 06:57:20 +00:00
|
|
|
for ($i = 0; $i < strlen($source); $i++) {
|
|
|
|
if (strlen($this->ansi)) {
|
|
|
|
$this->ansi.= $source[$i];
|
|
|
|
$chr = ord($source[$i]);
|
|
|
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
|
|
|
|
// single character CSI's not currently supported
|
|
|
|
switch (true) {
|
|
|
|
case $this->ansi == "\x1B=":
|
|
|
|
$this->ansi = '';
|
|
|
|
continue 2;
|
|
|
|
case strlen($this->ansi) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['):
|
|
|
|
case strlen($this->ansi) > 2 && $chr >= 64 && $chr <= 126:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue 2;
|
|
|
|
}
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->tokenization[] = $this->ansi;
|
|
|
|
$this->tokenization[] = '';
|
2014-03-30 06:57:20 +00:00
|
|
|
// http://ascii-table.com/ansi-escape-sequences-vt-100.php
|
|
|
|
switch ($this->ansi) {
|
|
|
|
case "\x1B[H": // Move cursor to upper left corner
|
|
|
|
$this->old_x = $this->x;
|
|
|
|
$this->old_y = $this->y;
|
|
|
|
$this->x = $this->y = 0;
|
|
|
|
break;
|
|
|
|
case "\x1B[J": // Clear screen from cursor down
|
|
|
|
$this->history = array_merge($this->history, array_slice(array_splice($this->screen, $this->y + 1), 0, $this->old_y));
|
|
|
|
$this->screen = array_merge($this->screen, array_fill($this->y, $this->max_y, ''));
|
|
|
|
|
|
|
|
$this->history_attrs = array_merge($this->history_attrs, array_slice(array_splice($this->attrs, $this->y + 1), 0, $this->old_y));
|
|
|
|
$this->attrs = array_merge($this->attrs, array_fill($this->y, $this->max_y, $this->attr_row));
|
|
|
|
|
|
|
|
if (count($this->history) == $this->max_history) {
|
|
|
|
array_shift($this->history);
|
|
|
|
array_shift($this->history_attrs);
|
|
|
|
}
|
|
|
|
case "\x1B[K": // Clear screen from cursor right
|
|
|
|
$this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x);
|
|
|
|
|
2015-10-04 13:24:43 +00:00
|
|
|
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));
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
case "\x1B[2K": // Clear entire line
|
|
|
|
$this->screen[$this->y] = str_repeat(' ', $this->x);
|
|
|
|
$this->attrs[$this->y] = $this->attr_row;
|
|
|
|
break;
|
|
|
|
case "\x1B[?1h": // set cursor key to application
|
|
|
|
case "\x1B[?25h": // show the cursor
|
2015-10-04 13:24:43 +00:00
|
|
|
case "\x1B(B": // set united states g0 character set
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
case "\x1BE": // Move to next line
|
|
|
|
$this->_newLine();
|
|
|
|
$this->x = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch (true) {
|
2015-10-04 13:24:43 +00:00
|
|
|
case preg_match('#\x1B\[(\d+)B#', $this->ansi, $match): // Move cursor down n lines
|
|
|
|
$this->old_y = $this->y;
|
|
|
|
$this->y+= $match[1];
|
|
|
|
break;
|
2014-03-30 06:57:20 +00:00
|
|
|
case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match): // Move cursor to screen location v,h
|
|
|
|
$this->old_x = $this->x;
|
|
|
|
$this->old_y = $this->y;
|
|
|
|
$this->x = $match[2] - 1;
|
|
|
|
$this->y = $match[1] - 1;
|
|
|
|
break;
|
|
|
|
case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match): // Move cursor right n lines
|
|
|
|
$this->old_x = $this->x;
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->x+= $match[1];
|
|
|
|
break;
|
|
|
|
case preg_match('#\x1B\[(\d+)D#', $this->ansi, $match): // Move cursor left n lines
|
|
|
|
$this->old_x = $this->x;
|
|
|
|
$this->x-= $match[1];
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match): // Set top and bottom lines of a window
|
|
|
|
break;
|
|
|
|
case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match): // character attributes
|
2015-10-04 13:24:43 +00:00
|
|
|
$attr_cell = &$this->attr_cell;
|
2014-03-30 06:57:20 +00:00
|
|
|
$mods = explode(';', $match[1]);
|
|
|
|
foreach ($mods as $mod) {
|
|
|
|
switch ($mod) {
|
|
|
|
case 0: // Turn off character attributes
|
2015-10-04 13:24:43 +00:00
|
|
|
$attr_cell = clone($this->base_attr_cell);
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
case 1: // Turn bold mode on
|
2015-10-04 13:24:43 +00:00
|
|
|
$attr_cell->bold = true;
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
case 4: // Turn underline mode on
|
2015-10-04 13:24:43 +00:00
|
|
|
$attr_cell->underline = true;
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
case 5: // Turn blinking mode on
|
2015-10-04 13:24:43 +00:00
|
|
|
$attr_cell->blink = true;
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
case 7: // Turn reverse video on
|
2015-10-04 13:24:43 +00:00
|
|
|
$attr_cell->reverse = !$attr_cell->reverse;
|
|
|
|
$temp = $attr_cell->background;
|
|
|
|
$attr_cell->background = $attr_cell->foreground;
|
|
|
|
$attr_cell->foreground = $temp;
|
2014-03-30 06:57:20 +00:00
|
|
|
break;
|
|
|
|
default: // set colors
|
2015-10-04 13:24:43 +00:00
|
|
|
//$front = $attr_cell->reverse ? &$attr_cell->background : &$attr_cell->foreground;
|
|
|
|
$front = &$attr_cell->{ $attr_cell->reverse ? 'background' : 'foreground' };
|
|
|
|
//$back = $attr_cell->reverse ? &$attr_cell->foreground : &$attr_cell->background;
|
|
|
|
$back = &$attr_cell->{ $attr_cell->reverse ? 'foreground' : 'background' };
|
2014-03-30 06:57:20 +00:00
|
|
|
switch ($mod) {
|
2015-10-04 13:24:43 +00:00
|
|
|
// @codingStandardsIgnoreStart
|
2014-03-30 06:57:20 +00:00
|
|
|
case 30: $front = 'black'; break;
|
|
|
|
case 31: $front = 'red'; break;
|
|
|
|
case 32: $front = 'green'; break;
|
|
|
|
case 33: $front = 'yellow'; break;
|
|
|
|
case 34: $front = 'blue'; break;
|
|
|
|
case 35: $front = 'magenta'; break;
|
|
|
|
case 36: $front = 'cyan'; break;
|
|
|
|
case 37: $front = 'white'; break;
|
|
|
|
|
|
|
|
case 40: $back = 'black'; break;
|
|
|
|
case 41: $back = 'red'; break;
|
|
|
|
case 42: $back = 'green'; break;
|
|
|
|
case 43: $back = 'yellow'; break;
|
|
|
|
case 44: $back = 'blue'; break;
|
|
|
|
case 45: $back = 'magenta'; break;
|
|
|
|
case 46: $back = 'cyan'; break;
|
|
|
|
case 47: $back = 'white'; break;
|
2015-10-04 13:24:43 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2014-03-30 06:57:20 +00:00
|
|
|
|
|
|
|
default:
|
2015-10-04 13:24:43 +00:00
|
|
|
//user_error('Unsupported attribute: ' . $mod);
|
2014-03-30 06:57:20 +00:00
|
|
|
$this->ansi = '';
|
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2015-10-04 13:24:43 +00:00
|
|
|
//user_error("{$this->ansi} is unsupported\r\n");
|
2014-03-30 06:57:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->ansi = '';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->tokenization[count($this->tokenization) - 1].= $source[$i];
|
2014-03-30 06:57:20 +00:00
|
|
|
switch ($source[$i]) {
|
|
|
|
case "\r":
|
|
|
|
$this->x = 0;
|
|
|
|
break;
|
|
|
|
case "\n":
|
|
|
|
$this->_newLine();
|
|
|
|
break;
|
2015-10-04 13:24:43 +00:00
|
|
|
case "\x08": // backspace
|
|
|
|
if ($this->x) {
|
|
|
|
$this->x--;
|
|
|
|
$this->attrs[$this->y][$this->x] = clone($this->base_attr_cell);
|
|
|
|
$this->screen[$this->y] = substr_replace(
|
|
|
|
$this->screen[$this->y],
|
|
|
|
$source[$i],
|
|
|
|
$this->x,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
2014-03-30 06:57:20 +00:00
|
|
|
case "\x0F": // shift
|
|
|
|
break;
|
|
|
|
case "\x1B": // start ANSI escape code
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->tokenization[count($this->tokenization) - 1] = substr($this->tokenization[count($this->tokenization) - 1], 0, -1);
|
|
|
|
//if (!strlen($this->tokenization[count($this->tokenization) - 1])) {
|
|
|
|
// array_pop($this->tokenization);
|
|
|
|
//}
|
2014-03-30 06:57:20 +00:00
|
|
|
$this->ansi.= "\x1B";
|
|
|
|
break;
|
|
|
|
default:
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->attrs[$this->y][$this->x] = clone($this->attr_cell);
|
|
|
|
if ($this->x > strlen($this->screen[$this->y])) {
|
|
|
|
$this->screen[$this->y] = str_repeat(' ', $this->x);
|
|
|
|
}
|
2014-03-30 06:57:20 +00:00
|
|
|
$this->screen[$this->y] = substr_replace(
|
|
|
|
$this->screen[$this->y],
|
|
|
|
$source[$i],
|
|
|
|
$this->x,
|
|
|
|
1
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->x > $this->max_x) {
|
|
|
|
$this->x = 0;
|
|
|
|
$this->y++;
|
|
|
|
} else {
|
|
|
|
$this->x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new line
|
|
|
|
*
|
|
|
|
* Also update the $this->screen and $this->history buffers
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
function _newLine()
|
|
|
|
{
|
|
|
|
//if ($this->y < $this->max_y) {
|
|
|
|
// $this->y++;
|
|
|
|
//}
|
|
|
|
|
|
|
|
while ($this->y >= $this->max_y) {
|
|
|
|
$this->history = array_merge($this->history, array(array_shift($this->screen)));
|
|
|
|
$this->screen[] = '';
|
|
|
|
|
|
|
|
$this->history_attrs = array_merge($this->history_attrs, array(array_shift($this->attrs)));
|
|
|
|
$this->attrs[] = $this->attr_row;
|
|
|
|
|
|
|
|
if (count($this->history) >= $this->max_history) {
|
|
|
|
array_shift($this->history);
|
|
|
|
array_shift($this->history_attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->y--;
|
|
|
|
}
|
|
|
|
$this->y++;
|
|
|
|
}
|
|
|
|
|
2015-10-04 13:24:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the current coordinate without preformating
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function _processCoordinate($last_attr, $cur_attr, $char)
|
|
|
|
{
|
|
|
|
$output = '';
|
|
|
|
|
|
|
|
if ($last_attr != $cur_attr) {
|
|
|
|
$close = $open = '';
|
|
|
|
if ($last_attr->foreground != $cur_attr->foreground) {
|
|
|
|
if ($cur_attr->foreground != 'white') {
|
|
|
|
$open.= '<span style="color: ' . $cur_attr->foreground . '">';
|
|
|
|
}
|
|
|
|
if ($last_attr->foreground != 'white') {
|
|
|
|
$close = '</span>' . $close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($last_attr->background != $cur_attr->background) {
|
|
|
|
if ($cur_attr->background != 'black') {
|
|
|
|
$open.= '<span style="background: ' . $cur_attr->background . '">';
|
|
|
|
}
|
|
|
|
if ($last_attr->background != 'black') {
|
|
|
|
$close = '</span>' . $close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($last_attr->bold != $cur_attr->bold) {
|
|
|
|
if ($cur_attr->bold) {
|
|
|
|
$open.= '<b>';
|
|
|
|
} else {
|
|
|
|
$close = '</b>' . $close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($last_attr->underline != $cur_attr->underline) {
|
|
|
|
if ($cur_attr->underline) {
|
|
|
|
$open.= '<u>';
|
|
|
|
} else {
|
|
|
|
$close = '</u>' . $close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($last_attr->blink != $cur_attr->blink) {
|
|
|
|
if ($cur_attr->blink) {
|
|
|
|
$open.= '<blink>';
|
|
|
|
} else {
|
|
|
|
$close = '</blink>' . $close;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$output.= $close . $open;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output.= htmlspecialchars($char);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2014-03-30 06:57:20 +00:00
|
|
|
/**
|
|
|
|
* Returns the current screen without preformating
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function _getScreen()
|
|
|
|
{
|
|
|
|
$output = '';
|
2015-10-04 13:24:43 +00:00
|
|
|
$last_attr = $this->base_attr_cell;
|
2014-03-30 06:57:20 +00:00
|
|
|
for ($i = 0; $i <= $this->max_y; $i++) {
|
2015-10-04 13:24:43 +00:00
|
|
|
for ($j = 0; $j <= $this->max_x; $j++) {
|
|
|
|
$cur_attr = $this->attrs[$i][$j];
|
|
|
|
$output.= $this->_processCoordinate($last_attr, $cur_attr, isset($this->screen[$i][$j]) ? $this->screen[$i][$j] : '');
|
|
|
|
$last_attr = $this->attrs[$i][$j];
|
2014-03-30 06:57:20 +00:00
|
|
|
}
|
|
|
|
$output.= "\r\n";
|
|
|
|
}
|
2015-10-04 13:24:43 +00:00
|
|
|
$output = substr($output, 0, -2);
|
|
|
|
// close any remaining open tags
|
|
|
|
$output.= $this->_processCoordinate($last_attr, $this->base_attr_cell, '');
|
2014-03-30 06:57:20 +00:00
|
|
|
return rtrim($output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current screen
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function getScreen()
|
|
|
|
{
|
2015-10-04 13:24:43 +00:00
|
|
|
return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $this->_getScreen() . '</pre>';
|
2014-03-30 06:57:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current screen and the x previous lines
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function getHistory()
|
|
|
|
{
|
|
|
|
$scrollback = '';
|
2015-10-04 13:24:43 +00:00
|
|
|
$last_attr = $this->base_attr_cell;
|
2014-03-30 06:57:20 +00:00
|
|
|
for ($i = 0; $i < count($this->history); $i++) {
|
|
|
|
for ($j = 0; $j <= $this->max_x + 1; $j++) {
|
2015-10-04 13:24:43 +00:00
|
|
|
$cur_attr = $this->history_attrs[$i][$j];
|
|
|
|
$scrollback.= $this->_processCoordinate($last_attr, $cur_attr, isset($this->history[$i][$j]) ? $this->history[$i][$j] : '');
|
|
|
|
$last_attr = $this->history_attrs[$i][$j];
|
2014-03-30 06:57:20 +00:00
|
|
|
}
|
|
|
|
$scrollback.= "\r\n";
|
|
|
|
}
|
2015-10-04 13:24:43 +00:00
|
|
|
$base_attr_cell = $this->base_attr_cell;
|
|
|
|
$this->base_attr_cell = $last_attr;
|
2014-03-30 06:57:20 +00:00
|
|
|
$scrollback.= $this->_getScreen();
|
2015-10-04 13:24:43 +00:00
|
|
|
$this->base_attr_cell = $base_attr_cell;
|
2014-03-30 06:57:20 +00:00
|
|
|
|
2015-10-04 13:24:43 +00:00
|
|
|
return '<pre width="' . ($this->max_x + 1) . '" style="color: white; background: black">' . $scrollback . '</span></pre>';
|
2014-03-30 06:57:20 +00:00
|
|
|
}
|
|
|
|
}
|