Skip to content

Commit c543b67

Browse files
committed
Fixed escaping of control chars in CLI formatter
Fixes #84 Signed-off-by: Michal Čihař <[email protected]>
1 parent a483d10 commit c543b67

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* Fix parsing of DEFINER without backquotes
66
* Fixed escaping HTML entities in HTML formatter
7+
* Fixed escaping of control chars in CLI formatter
78

89
## [3.4.6] - 2016-09-13
910

src/Utils/Formatter.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,27 @@ public function formatList($list)
430430
return $ret;
431431
}
432432

433+
public function escapeConsole($string)
434+
{
435+
return str_replace(
436+
array(
437+
"\x00", "\x01", "\x02", "\x03", "\x04",
438+
"\x05", "\x06", "\x07", "\x08", "\x09", "\x0A",
439+
"\x0B","\x0C","\x0D", "\x0E", "\x0F", "\x10", "\x11",
440+
"\x12","\x13","\x14","\x15", "\x16", "\x17", "\x18",
441+
"\x19","\x1A","\x1B","\x1C","\x1D", "\x1E", "\x1F"
442+
),
443+
array(
444+
'\x00', '\x01', '\x02', '\x03', '\x04',
445+
'\x05', '\x06', '\x07', '\x08', '\x09', '\x0A',
446+
'\x0B', '\x0C', '\x0D', '\x0E', '\x0F', '\x10', '\x11',
447+
'\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18',
448+
'\x19', '\x1A', '\x1B', '\x1C', '\x1D', '\x1E', '\x1F'
449+
),
450+
$string
451+
);
452+
}
453+
433454
/**
434455
* Tries to print the query and returns the result.
435456
*
@@ -455,15 +476,15 @@ public function toString($token)
455476
if ($this->options['type'] === 'html') {
456477
return '<span ' . $format['html'] . '>' . htmlspecialchars($text, ENT_NOQUOTES) . '</span>';
457478
} elseif ($this->options['type'] === 'cli') {
458-
return $format['cli'] . $text;
479+
return $format['cli'] . $this->escapeConsole($text);
459480
}
460481

461482
break;
462483
}
463484
}
464485

465486
if ($this->options['type'] === 'cli') {
466-
return "\x1b[39m" . $text;
487+
return "\x1b[39m" . $this->escapeConsole($text);
467488
} elseif ($this->options['type'] === 'html') {
468489
return htmlspecialchars($text, ENT_NOQUOTES);
469490
}

0 commit comments

Comments
 (0)