Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ class Module extends \yii\base\Module
*/
public $yiiScript = '@app/yii';

/** @var bool Enable composer support */
public $composerEnabled = false;

/** @var string composer-command: composer or php composer.phar */
public $composerCommand = 'composer';

/** @var string path to main folder */
public $composerWorkingDirectory = '@app';

/**
* @var array the list of IPs that are allowed to access this module.
* Each array element represents a single IP filter which can be either an IP address
Expand Down
25 changes: 19 additions & 6 deletions controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function actionIndex()
$this->layout = 'shell';
return $this->render('index', [
'quitUrl' => $this->module->quitUrl ? Url::toRoute($this->module->quitUrl) : null,
'greetings' => $this->module->greetings
'greetings' => $this->module->greetings,
'module' => $this->module,
]);
}

Expand All @@ -50,8 +51,22 @@ public function actionRpc()

switch ($options['method']) {
case 'yii':
list ($status, $output) = $this->runConsole(implode(' ', $options['params']));
$cmd = Yii::getAlias($this->module->yiiScript) . ' '
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of moving building of the command here I'd introduce runYiiCommand and runComposerCommand.

. implode(' ', $options['params'])
. ' 2>&1';
list ($status, $output) = $this->runConsole($cmd);
return ['result' => $output];
case 'composer':
if (!$this->module->composerEnabled) {
return ['result' => 'no composer support'];
}

$cmd = $this->module->composerCommand . ' '
. implode(' ', $options['params'])
. ' -d='.Yii::getAlias($this->module->composerWorkingDirectory)
. ' 2>&1';
list ($status, $output) = $this->runConsole($cmd);
return ['result' => $output];
}
}

Expand All @@ -64,9 +79,7 @@ public function actionRpc()
*/
private function runConsole($command)
{
$cmd = Yii::getAlias($this->module->yiiScript) . ' ' . $command . ' 2>&1';

$handler = popen($cmd, 'r');
$handler = popen($command, 'r');
$output = '';
while (!feof($handler)) {
$output .= fgets($handler);
Expand All @@ -77,4 +90,4 @@ private function runConsole($command)

return [$status, $output];
}
}
}
24 changes: 23 additions & 1 deletion views/default/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/** @var $this yii\web\View */
/** @var $quitUrl string */
/** @var $greetings string */
/** @var $module \samdark\webshell\Module*/
use yii\helpers\Url;

\samdark\webshell\WebshellAsset::register($this);
Expand All @@ -10,6 +11,25 @@

$this->title = $greetings;

$jsComposerHelp = '';
$jsComposerExecution = '';

if($module->composerEnabled){
$jsComposerHelp = <<<js
term.echo('composer\tcomposer command');
js;
$jsComposerExecution = <<<js
else if (command.indexOf('composer') === 0 || command.indexOf('composer') === 8){
$.jrpc('{$endpoint}', 'composer', [command.replace(/^composer ?/, '')], function(json) {
term.echo(json.result);
scrollDown();
});
}
js;


}

$this->registerJs(
<<<JS
jQuery(function($) {
Expand All @@ -22,10 +42,12 @@ function(command, term) {
term.echo(json.result);
scrollDown();
});
} else if (command === 'help') {
} $jsComposerExecution
else if (command === 'help') {
term.echo('Available commands are:');
term.echo('');
term.echo("clear\tClear console");
$jsComposerHelp
term.echo('help\tThis help text');
term.echo('yii\tyii command');
term.echo('quit\tQuit web shell');
Expand Down