PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/phpseclib/File/ANSI.php

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