From 5ef7ca59c4694454d960b3eedb2952e807ec276e Mon Sep 17 00:00:00 2001 From: ToSchQLB Date: Fri, 27 Jan 2017 14:07:17 +0100 Subject: [PATCH 1/2] add composer support --- Module.php | 9 +++++++++ controllers/DefaultController.php | 23 ++++++++++++++++++----- views/default/index.php | 24 +++++++++++++++++++++++- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Module.php b/Module.php index ec526e9..4e56e5d 100644 --- a/Module.php +++ b/Module.php @@ -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 diff --git a/controllers/DefaultController.php b/controllers/DefaultController.php index 204e512..40947b6 100644 --- a/controllers/DefaultController.php +++ b/controllers/DefaultController.php @@ -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, ]); } @@ -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) . ' ' + . 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]; } } @@ -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); diff --git a/views/default/index.php b/views/default/index.php index 7a76de6..f1846fb 100644 --- a/views/default/index.php +++ b/views/default/index.php @@ -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); @@ -10,6 +11,25 @@ $this->title = $greetings; +$jsComposerHelp = ''; +$jsComposerExecution = ''; + +if($module->composerEnabled){ + $jsComposerHelp = <<registerJs( << Date: Sat, 28 Jan 2017 12:00:27 +0300 Subject: [PATCH 2/2] Fixed formatting --- controllers/DefaultController.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/controllers/DefaultController.php b/controllers/DefaultController.php index 40947b6..cba196c 100644 --- a/controllers/DefaultController.php +++ b/controllers/DefaultController.php @@ -35,7 +35,7 @@ public function actionIndex() return $this->render('index', [ 'quitUrl' => $this->module->quitUrl ? Url::toRoute($this->module->quitUrl) : null, 'greetings' => $this->module->greetings, - 'module' => $this->module, + 'module' => $this->module, ]); } @@ -52,21 +52,21 @@ public function actionRpc() switch ($options['method']) { case 'yii': $cmd = Yii::getAlias($this->module->yiiScript) . ' ' - . implode(' ', $options['params']) - . ' 2>&1'; + . 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']; - } + if (!$this->module->composerEnabled) { + return ['result' => 'no composer support']; + } - $cmd = $this->module->composerCommand . ' ' - . implode(' ', $options['params']) + $cmd = $this->module->composerCommand . ' ' + . implode(' ', $options['params']) . ' -d='.Yii::getAlias($this->module->composerWorkingDirectory) - . ' 2>&1'; - list ($status, $output) = $this->runConsole($cmd); - return ['result' => $output]; + . ' 2>&1'; + list ($status, $output) = $this->runConsole($cmd); + return ['result' => $output]; } } @@ -90,4 +90,4 @@ private function runConsole($command) return [$status, $output]; } -} \ No newline at end of file +}