PluginProbe
ManageWP Worker / 3.9.28
ManageWP Worker v3.9.28
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / lib / PHPSecLib / File / ANSI.php

ANSI.php in ManageWP Worker 3.9.28, at lib/PHPSecLib/File/ANSI.php

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