ANSI.php 19 KB

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