/var/www/hkosl.com/imusiccircle/webadmin/libraies/phpseclib/phpseclib/phpseclib/File/ANSI.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?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>
 * @copyright 2012 Jim Wigginton
 * @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;

    
/**
     * An empty attribute row
     *
     * @var Array
     * @access private
     */
    
var $attr_row;

    
/**
     * The current screen text
     *
     * @var Array
     * @access private
     */
    
var $screen;

    
/**
     * The current screen attributes
     *
     * @var Array
     * @access private
     */
    
var $attrs;

    
/**
     * The current foreground color
     *
     * @var String
     * @access private
     */
    
var $foreground;

    
/**
     * The current background color
     *
     * @var String
     * @access private
     */
    
var $background;

    
/**
     * Bold flag
     *
     * @var Boolean
     * @access private
     */
    
var $bold;

    
/**
     * Underline flag
     *
     * @var Boolean
     * @access private
     */
    
var $underline;

    
/**
     * Blink flag
     *
     * @var Boolean
     * @access private
     */
    
var $blink;

    
/**
     * Reverse flag
     *
     * @var Boolean
     * @access private
     */
    
var $reverse;

    
/**
     * Color flag
     *
     * @var Boolean
     * @access private
     */
    
var $color;

    
/**
     * Current ANSI code
     *
     * @var String
     * @access private
     */
    
var $ansi;

    
/**
     * Default Constructor.
     *
     * @return File_ANSI
     * @access public
     */
    
function File_ANSI()
    {
        
$this->setHistory(200);
        
$this->setDimensions(8024);
    }

    
/**
     * 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->$this->0;
        
$this->history $this->history_attrs = array();
        
$this->attr_row array_fill(0$this->max_x 1'');
        
$this->screen array_fill(0$this->max_y 1'');
        
$this->attrs array_fill(0$this->max_y 1$this->attr_row);
        
$this->foreground 'white';
        
$this->background 'black';
        
$this->bold false;
        
$this->underline false;
        
$this->blink false;
        
$this->reverse false;
        
$this->color false;

        
$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)
    {
        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) == && $chr >= 64 && $chr <= 95 && $chr != ord('['):
                    case 
strlen($this->ansi) > && $chr >= 64 && $chr <= 126:
                        break;
                    default:
                        continue 
2;
                }
                
// 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->$this->0;
                        break;
                    case 
"\x1B[J"// Clear screen from cursor down
                        
$this->history array_merge($this->historyarray_slice(array_splice($this->screen$this->1), 0$this->old_y));
                        
$this->screen array_merge($this->screenarray_fill($this->y$this->max_y''));

                        
$this->history_attrs array_merge($this->history_attrsarray_slice(array_splice($this->attrs$this->1), 0$this->old_y));
                        
$this->attrs array_merge($this->attrsarray_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);

                        
array_splice($this->attrs[$this->y], $this->1);
                        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
                        
break;
                    case 
"\x1BE"// Move to next line
                        
$this->_newLine();
                        
$this->0;
                        break;
                    default:
                        switch (
true) {
                            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->$match[2] - 1;
                                
$this->$match[1] - 1;
                                break;
                            case 
preg_match('#\x1B\[(\d+)C#'$this->ansi$match): // Move cursor right n lines
                                
$this->old_x $this->x;
                                
$x $match[1] - 1;
                                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
                                
$mods explode(';'$match[1]);
                                foreach (
$mods as $mod) {
                                    switch (
$mod) {
                                        case 
0// Turn off character attributes
                                            
$this->attrs[$this->y][$this->x] = '';

                                            if (
$this->bold$this->attrs[$this->y][$this->x].= '</b>';
                                            if (
$this->underline$this->attrs[$this->y][$this->x].= '</u>';
                                            if (
$this->blink$this->attrs[$this->y][$this->x].= '</blink>';
                                            if (
$this->color$this->attrs[$this->y][$this->x].= '</span>';

                                            if (
$this->reverse) {
                                                
$temp $this->background;
                                                
$this->background $this->foreground;
                                                
$this->foreground $temp;
                                            }

                                            
$this->bold $this->underline $this->blink $this->color $this->reverse false;
                                            break;
                                        case 
1// Turn bold mode on
                                            
if (!$this->bold) {
                                                
$this->attrs[$this->y][$this->x] = '<b>';
                                                
$this->bold true;
                                            }
                                            break;
                                        case 
4// Turn underline mode on
                                            
if (!$this->underline) {
                                                
$this->attrs[$this->y][$this->x] = '<u>';
                                                
$this->underline true;
                                            }
                                            break;
                                        case 
5// Turn blinking mode on
                                            
if (!$this->blink) {
                                                
$this->attrs[$this->y][$this->x] = '<blink>';
                                                
$this->blink true;
                                            }
                                            break;
                                        case 
7// Turn reverse video on
                                            
$this->reverse = !$this->reverse;
                                            
$temp $this->background;
                                            
$this->background $this->foreground;
                                            
$this->foreground $temp;
                                            
$this->attrs[$this->y][$this->x] = '<span style="color: ' $this->foreground '; background: ' $this->background '">';
                                            if (
$this->color) {
                                                
$this->attrs[$this->y][$this->x] = '</span>' $this->attrs[$this->y][$this->x];
                                            }
                                            
$this->color true;
                                            break;
                                        default: 
// set colors
                                            //$front = $this->reverse ? &$this->background : &$this->foreground;
                                            
$front = &$this->{ $this->reverse 'background' 'foreground' };
                                            
//$back = $this->reverse ? &$this->foreground : &$this->background;
                                            
$back = &$this->{ $this->reverse 'foreground' 'background' };
                                            switch (
$mod) {
                                                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;

                                                default:
                                                    
user_error('Unsupported attribute: ' $mod);
                                                    
$this->ansi '';
                                                    break 
2;
                                            }

                                            unset(
$temp);
                                            
$this->attrs[$this->y][$this->x] = '<span style="color: ' $this->foreground '; background: ' $this->background '">';
                                            if (
$this->color) {
                                                
$this->attrs[$this->y][$this->x] = '</span>' $this->attrs[$this->y][$this->x];
                                            }
                                            
$this->color true;
                                    }
                                }
                                break;
                            default:
                                
user_error("{$this->ansi} unsupported\r\n");
                        }
                }
                
$this->ansi '';
                continue;
            }

            switch (
$source[$i]) {
                case 
"\r":
                    
$this->0;
                    break;
                case 
"\n":
                    
$this->_newLine();
                    break;
                case 
"\x0F"// shift
                    
break;
                case 
"\x1B"// start ANSI escape code
                    
$this->ansi.= "\x1B";
                    break;
                default:
                    
$this->screen[$this->y] = substr_replace(
                        
$this->screen[$this->y],
                        
$source[$i],
                        
$this->x,
                        
1
                    
);

                    if (
$this->$this->max_x) {
                        
$this->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->>= $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++;
    }

    
/**
     * Returns the current screen without preformating
     *
     * @access private
     * @return String
     */
    
function _getScreen()
    {
        
$output '';
        for (
$i 0$i <= $this->max_y$i++) {
            for (
$j 0$j <= $this->max_x 1$j++) {
                if (isset(
$this->attrs[$i][$j])) {
                    
$output.= $this->attrs[$i][$j];
                }
                if (isset(
$this->screen[$i][$j])) {
                    
$output.= htmlspecialchars($this->screen[$i][$j]);
                }
            }
            
$output.= "\r\n";
        }
        return 
rtrim($output);
    }

    
/**
     * Returns the current screen
     *
     * @access public
     * @return String
     */
    
function getScreen()
    {
        return 
'<pre style="color: white; background: black" width="' . ($this->max_x 1) . '">' $this->_getScreen() . '</pre>';
    }

    
/**
     * Returns the current screen and the x previous lines
     *
     * @access public
     * @return String
     */
    
function getHistory()
    {
        
$scrollback '';
        for (
$i 0$i count($this->history); $i++) {
            for (
$j 0$j <= $this->max_x 1$j++) {
                if (isset(
$this->history_attrs[$i][$j])) {
                    
$scrollback.= $this->history_attrs[$i][$j];
                }
                if (isset(
$this->history[$i][$j])) {
                    
$scrollback.= htmlspecialchars($this->history[$i][$j]);
                }
            }
            
$scrollback.= "\r\n";
        }
        
$scrollback.= $this->_getScreen();

        return 
'<pre style="color: white; background: black" width="' . ($this->max_x 1) . '">' $scrollback '</pre>';
    }
}