| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
class Phergie_Plugin_Eval extends Phergie_Plugin_Abstract_Command |
|---|
| 7 |
{ |
|---|
| 8 |
|
|---|
| 9 |
* Flag indicating whether or not the plugin is an admin plugin or not |
|---|
| 10 |
* |
|---|
| 11 |
* @var bool |
|---|
| 12 |
*/ |
|---|
| 13 |
public $needsAdmin = true; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
* Evaluates the given string and outputs the evaluated statement while any |
|---|
| 17 |
* any dumped data gets dumped to the console. |
|---|
| 18 |
* |
|---|
| 19 |
* @return void |
|---|
| 20 |
*/ |
|---|
| 21 |
public function onDoEval($code) |
|---|
| 22 |
{ |
|---|
| 23 |
$user = $this->event->getNick(); |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
if ($this->fromAdmin(true)) { |
|---|
| 27 |
$code = trim($code); |
|---|
| 28 |
if (empty($code)) { |
|---|
| 29 |
return; |
|---|
| 30 |
} |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
if (substr(strtolower($code), 0, 2) == '-r') { |
|---|
| 34 |
$code = trim(substr($code, 2)); |
|---|
| 35 |
} else { |
|---|
| 36 |
$code = trim((strtolower(substr($code, 0, 6)) != 'return' ? 'return ' : '') . $code); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
if (!empty($code)) { |
|---|
| 40 |
if (substr($code, -1) != ';') { |
|---|
| 41 |
$code .= ';'; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
ob_start(); |
|---|
| 46 |
$eval = eval($code); |
|---|
| 47 |
$contents = ob_get_contents(); |
|---|
| 48 |
ob_end_clean(); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
if (!empty($contents)) { |
|---|
| 52 |
$this->debug('OUTPUT:' . PHP_EOL . trim($contents)); |
|---|
| 53 |
unset($contents); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
if (!empty($eval)) { |
|---|
| 57 |
$this->doPrivmsg($this->event->getSource(), trim($eval)); |
|---|
| 58 |
unset($eval); |
|---|
| 59 |
} |
|---|
| 60 |
} |
|---|
| 61 |
} else { |
|---|
| 62 |
$this->doNotice($user, 'You do not have permission to use eval.'); |
|---|
| 63 |
} |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Executes the given string and outputs the last line that exec returns |
|---|
| 68 |
* while the full exec request gets dumped to the console if the-c flag |
|---|
| 69 |
* is specified. |
|---|
| 70 |
* |
|---|
| 71 |
* @return void |
|---|
| 72 |
*/ |
|---|
| 73 |
public function onDoExec($code) |
|---|
| 74 |
{ |
|---|
| 75 |
$user = $this->event->getNick(); |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
if ($this->fromAdmin(true)) { |
|---|
| 79 |
$code = trim($code); |
|---|
| 80 |
if (empty($code)) { |
|---|
| 81 |
return; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
$console = false; |
|---|
| 85 |
|
|---|
| 86 |
if (substr(strtolower($code), 0, 2) == '-c') { |
|---|
| 87 |
$code = trim(substr($code, 2)); |
|---|
| 88 |
$console = true; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
if (!empty($code)) { |
|---|
| 92 |
$exec = exec($code, $output); |
|---|
| 93 |
if (!empty($exec)) { |
|---|
| 94 |
$this->doPrivmsg($this->event->getSource(), trim($exec)); |
|---|
| 95 |
unset($exec); |
|---|
| 96 |
} |
|---|
| 97 |
if (!empty($output)) { |
|---|
| 98 |
if ($console) { |
|---|
| 99 |
$this->debug('OUTPUT:' . PHP_EOL . implode(PHP_EOL, $output)); |
|---|
| 100 |
} |
|---|
| 101 |
unset($output); |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|
| 104 |
} else { |
|---|
| 105 |
$this->doNotice($user, 'You do not have permission to use exec.'); |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|