2013-10-17 04:12:14 +02:00
|
|
|
<?php
|
|
|
|
|
2014-08-04 16:55:43 +02:00
|
|
|
final class PhabricatorRemarkupCowsayBlockInterpreter
|
2013-10-17 04:12:14 +02:00
|
|
|
extends PhutilRemarkupBlockInterpreter {
|
|
|
|
|
|
|
|
public function getInterpreterName() {
|
|
|
|
return 'cowsay';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markupContent($content, array $argv) {
|
|
|
|
if (!Filesystem::binaryExists('cowsay')) {
|
|
|
|
return $this->markupError(
|
|
|
|
pht('Unable to locate the `cowsay` binary. Install cowsay.'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$bin = idx($argv, 'think') ? 'cowthink' : 'cowsay';
|
|
|
|
$eyes = idx($argv, 'eyes', 'oo');
|
|
|
|
$tongue = idx($argv, 'tongue', ' ');
|
|
|
|
$cow = idx($argv, 'cow', 'default');
|
|
|
|
|
|
|
|
// NOTE: Strip this aggressively to prevent nonsense like
|
|
|
|
// `cow=/etc/passwd`. We could build a whiltelist with `cowsay -l`.
|
|
|
|
$cow = preg_replace('/[^a-z.-]+/', '', $cow);
|
|
|
|
|
|
|
|
$future = new ExecFuture(
|
|
|
|
'%s -e %s -T %s -f %s ',
|
|
|
|
$bin,
|
|
|
|
$eyes,
|
|
|
|
$tongue,
|
|
|
|
$cow);
|
|
|
|
|
2013-11-25 23:55:25 +01:00
|
|
|
$future->setTimeout(15);
|
2013-10-17 04:12:14 +02:00
|
|
|
$future->write($content);
|
|
|
|
|
|
|
|
list($err, $stdout, $stderr) = $future->resolve();
|
|
|
|
|
|
|
|
if ($err) {
|
|
|
|
return $this->markupError(
|
|
|
|
pht(
|
|
|
|
'Execution of `cowsay` failed:', $stderr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->getEngine()->isTextMode()) {
|
|
|
|
return $stdout;
|
|
|
|
}
|
|
|
|
|
|
|
|
return phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'PhabricatorMonospaced remarkup-cowsay',
|
|
|
|
),
|
|
|
|
$stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|