PHPSECLIB 0.3.1 added to the project to support SFTP transfers of lab orders and...
[openemr.git] / library / phpseclib / File / ANSI.php
blob29ad949e104061604c75a3c1ae91b134f12c4f56
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4 /**
5 * Pure-PHP ANSI Decoder
7 * PHP versions 4 and 5
9 * If you call read() in Net_SSH2 you may get {@link http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape codes} back.
10 * They'd look like chr(0x1B) . '[00m' or whatever (0x1B = ESC). They tell a
11 * {@link http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator} how to format the characters, what
12 * color to display them in, etc. File_ANSI is a {@link http://en.wikipedia.org/wiki/VT100 VT100} terminal emulator.
14 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 * THE SOFTWARE.
32 * @category File
33 * @package File_ANSI
34 * @author Jim Wigginton <terrafrost@php.net>
35 * @copyright MMXII Jim Wigginton
36 * @license http://www.opensource.org/licenses/mit-license.html MIT License
37 * @version $Id$
38 * @link htp://phpseclib.sourceforge.net
41 /**
42 * Pure-PHP ANSI Decoder
44 * @author Jim Wigginton <terrafrost@php.net>
45 * @version 0.3.0
46 * @access public
47 * @package File_ANSI
49 class File_ANSI {
50 /**
51 * Max Width
53 * @var Integer
54 * @access private
56 var $max_x;
58 /**
59 * Max Height
61 * @var Integer
62 * @access private
64 var $max_y;
66 /**
67 * Max History
69 * @var Integer
70 * @access private
72 var $max_history;
74 /**
75 * History
77 * @var Array
78 * @access private
80 var $history;
82 /**
83 * History Attributes
85 * @var Array
86 * @access private
88 var $history_attrs;
90 /**
91 * Current Column
93 * @var Integer
94 * @access private
96 var $x;
98 /**
99 * Current Row
101 * @var Integer
102 * @access private
104 var $y;
107 * Old Column
109 * @var Integer
110 * @access private
112 var $old_x;
115 * Old Row
117 * @var Integer
118 * @access private
120 var $old_y;
123 * An empty attribute row
125 * @var Array
126 * @access private
128 var $attr_row;
131 * The current screen text
133 * @var Array
134 * @access private
136 var $screen;
139 * The current screen attributes
141 * @var Array
142 * @access private
144 var $attrs;
147 * The current foreground color
149 * @var String
150 * @access private
152 var $foreground;
155 * The current background color
157 * @var String
158 * @access private
160 var $background;
163 * Bold flag
165 * @var Boolean
166 * @access private
168 var $bold;
171 * Underline flag
173 * @var Boolean
174 * @access private
176 var $underline;
179 * Blink flag
181 * @var Boolean
182 * @access private
184 var $blink;
187 * Reverse flag
189 * @var Boolean
190 * @access private
192 var $reverse;
195 * Color flag
197 * @var Boolean
198 * @access private
200 var $color;
203 * Current ANSI code
205 * @var String
206 * @access private
208 var $ansi;
211 * Default Constructor.
213 * @return File_ANSI
214 * @access public
216 function File_ANSI()
218 $this->setHistory(200);
219 $this->setDimensions(80, 24);
223 * Set terminal width and height
225 * Resets the screen as well
227 * @param Integer $x
228 * @param Integer $y
229 * @access public
231 function setDimensions($x, $y)
233 $this->max_x = $x - 1;
234 $this->max_y = $y - 1;
235 $this->x = $this->y = 0;
236 $this->history = $this->history_attrs = array();
237 $this->attr_row = array_fill(0, $this->max_x + 1, '');
238 $this->screen = array_fill(0, $this->max_y + 1, '');
239 $this->attrs = array_fill(0, $this->max_y + 1, $this->attr_row);
240 $this->foreground = 'white';
241 $this->background = 'black';
242 $this->bold = false;
243 $this->underline = false;
244 $this->blink = false;
245 $this->reverse = false;
246 $this->color = false;
248 $this->ansi = '';
252 * Set the number of lines that should be logged past the terminal height
254 * @param Integer $x
255 * @param Integer $y
256 * @access public
258 function setHistory($history)
260 $this->max_history = $history;
264 * Load a string
266 * @param String $source
267 * @access public
269 function loadString($source)
271 $this->setDimensions($this->max_x + 1, $this->max_y + 1);
272 $this->appendString($source);
276 * Appdend a string
278 * @param String $source
279 * @access public
281 function appendString($source)
283 for ($i = 0; $i < strlen($source); $i++) {
284 if (strlen($this->ansi)) {
285 $this->ansi.= $source[$i];
286 $chr = ord($source[$i]);
287 // http://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
288 // single character CSI's not currently supported
289 switch (true) {
290 case $this->ansi == "\x1B=":
291 $this->ansi = '';
292 continue 2;
293 case strlen($this->ansi) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['):
294 case strlen($this->ansi) > 2 && $chr >= 64 && $chr <= 126:
295 break;
296 default:
297 continue 2;
299 // http://ascii-table.com/ansi-escape-sequences-vt-100.php
300 switch ($this->ansi) {
301 case "\x1B[H":
302 $this->old_x = $this->x;
303 $this->old_y = $this->y;
304 $this->x = $this->y = 0;
305 break;
306 case "\x1B[J":
307 $this->history = array_merge($this->history, array_slice(array_splice($this->screen, $this->y + 1), 0, $this->old_y));
308 $this->screen = array_merge($this->screen, array_fill($this->y, $this->max_y, ''));
310 $this->history_attrs = array_merge($this->history_attrs, array_slice(array_splice($this->attrs, $this->y + 1), 0, $this->old_y));
311 $this->attrs = array_merge($this->attrs, array_fill($this->y, $this->max_y, $this->attr_row));
313 if (count($this->history) == $this->max_history) {
314 array_shift($this->history);
315 array_shift($this->history_attrs);
317 case "\x1B[K":
318 $this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x);
320 array_splice($this->attrs[$this->y], $this->x + 1);
321 break;
322 case "\x1B[?1h": // set cursor key to application
323 break;
324 default:
325 switch (true) {
326 case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match):
327 $this->old_x = $this->x;
328 $this->old_y = $this->y;
329 $this->x = $match[2] - 1;
330 $this->y = $match[1] - 1;
331 break;
332 case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match):
333 $this->old_x = $this->x;
334 $x = $match[1] - 1;
335 break;
336 case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match): // Set top and bottom lines of a window
337 break;
338 case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match):
339 $mods = explode(';', $match[1]);
340 foreach ($mods as $mod) {
341 switch ($mod) {
342 case 0:
343 $this->attrs[$this->y][$this->x] = '';
345 if ($this->bold) $this->attrs[$this->y][$this->x].= '</b>';
346 if ($this->underline) $this->attrs[$this->y][$this->x].= '</underline>';
347 if ($this->blink) $this->attrs[$this->y][$this->x].= '</blink>';
348 if ($this->color) $this->attrs[$this->y][$this->x].= '</span>';
350 if ($this->reverse) {
351 $temp = $this->background;
352 $this->background = $this->foreground;
353 $this->foreground = $temp;
356 $this->bold = $this->underline = $this->blink = $this->color = $this->reverse = false;
357 break;
358 case 1:
359 if (!$this->bold) {
360 $this->attrs[$this->y][$this->x] = '<b>';
361 $this->bold = true;
363 break;
364 case 4:
365 if (!$this->underline) {
366 $this->attrs[$this->y][$this->x] = '<u>';
367 $this->underline = true;
369 break;
370 case 5:
371 if (!$this->blink) {
372 $this->attrs[$this->y][$this->x] = '<blink>';
373 $this->blink = true;
375 break;
376 case 7:
377 $this->reverse = !$this->reverse;
378 $temp = $this->background;
379 $this->background = $this->foreground;
380 $this->foreground = $temp;
381 $this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
382 if ($this->color) {
383 $this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
385 $this->color = true;
386 break;
387 default:
388 //$front = $this->reverse ? &$this->background : &$this->foreground;
389 $front = &$this->{ $this->reverse ? 'background' : 'foreground' };
390 //$back = $this->reverse ? &$this->foreground : &$this->background;
391 $back = &$this->{ $this->reverse ? 'foreground' : 'background' };
392 switch ($mod) {
393 case 30: $front = 'black'; break;
394 case 31: $front = 'red'; break;
395 case 32: $front = 'green'; break;
396 case 33: $front = 'yellow'; break;
397 case 34: $front = 'blue'; break;
398 case 35: $front = 'magenta'; break;
399 case 36: $front = 'cyan'; break;
400 case 37: $front = 'white'; break;
402 case 40: $back = 'black'; break;
403 case 41: $back = 'red'; break;
404 case 42: $back = 'green'; break;
405 case 43: $back = 'yellow'; break;
406 case 44: $back = 'blue'; break;
407 case 45: $back = 'magenta'; break;
408 case 46: $back = 'cyan'; break;
409 case 47: $back = 'white'; break;
411 default:
412 user_error('Unsupported attribute: ' . $mod);
413 $this->ansi = '';
414 break 2;
417 unset($temp);
418 $this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
419 if ($this->color) {
420 $this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
422 $this->color = true;
425 break;
426 default:
427 echo "{$this->ansi} unsupported\r\n";
430 $this->ansi = '';
431 continue;
434 switch ($source[$i]) {
435 case "\r":
436 $this->x = 0;
437 break;
438 case "\n":
439 //if ($this->y < $this->max_y) {
440 // $this->y++;
443 while ($this->y >= $this->max_y) {
444 $this->history = array_merge($this->history, array(array_shift($this->screen)));
445 $this->screen[] = '';
447 $this->history_attrs = array_merge($this->history_attrs, array(array_shift($this->attrs)));
448 $this->attrs[] = $this->attr_row;
450 if (count($this->history) >= $this->max_history) {
451 array_shift($this->history);
452 array_shift($this->history_attrs);
455 $this->y--;
457 $this->y++;
458 break;
459 case "\x0F": // shift
460 break;
461 case "\x1B": // start ANSI escape code
462 $this->ansi.= "\x1B";
463 break;
464 default:
465 $this->screen[$this->y] = substr_replace(
466 $this->screen[$this->y],
467 $source[$i],
468 $this->x,
472 if ($this->x > $this->max_x) {
473 $this->x = 0;
474 $this->y++;
475 } else {
476 $this->x++;
483 * Returns the current screen without preformating
485 * @access private
486 * @return String
488 function _getScreen()
490 $output = '';
491 for ($i = 0; $i <= $this->max_y; $i++) {
492 for ($j = 0; $j <= $this->max_x + 1; $j++) {
493 if (isset($this->attrs[$i][$j])) {
494 $output.= $this->attrs[$i][$j];
496 if (isset($this->screen[$i][$j])) {
497 $output.= htmlspecialchars($this->screen[$i][$j]);
500 $output.= "\r\n";
502 return rtrim($output);
506 * Returns the current screen
508 * @access public
509 * @return String
511 function getScreen()
513 return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $this->_getScreen() . '</pre>';
517 * Returns the current screen and the x previous lines
519 * @access public
520 * @return String
522 function getHistory()
524 $scrollback = '';
525 for ($i = 0; $i < count($this->history); $i++) {
526 for ($j = 0; $j <= $this->max_x + 1; $j++) {
527 if (isset($this->history_attrs[$i][$j])) {
528 $scrollback.= $this->history_attrs[$i][$j];
530 if (isset($this->history[$i][$j])) {
531 $scrollback.= htmlspecialchars($this->history[$i][$j]);
534 $scrollback.= "\r\n";
536 $scrollback.= $this->_getScreen();
538 return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $scrollback . '</pre>';