Yii 1.1: webshell

Web shell for Yii allows you to run console commands from your browser.
22 followers

Note: Latest release and documentation are available from GitHub module page.

Web shell for Yii allows you to run console commands from your browser. Can be useful for both no-ssh webservers and console-style administration modules.

Yii web shell uses the same config as your web application so if your application works it will work too.

Installing

To use web shell, you must include it as a module in the application configuration like the following:

return array('modules'=>array(
        'webshell'=>array(
            'class'=>'ext.yiiext.modules.webshell.WebShellModule',
            // when typing 'exit', user will be redirected to this URL
            'exitUrl' => '/',
            // custom wterm options
            'wtermOptions' => array(
                // linux-like command prompt
                'PS1' => '%',
            ),
            // additional commands (see below)
            'commands' => array(
                'test' => array('js:function(){return "Hello, world!";}', 'Just a test.'),
            ),
            // uncomment to disable yiic
            // 'useYiic' => false,
 
            // adding custom yiic commands not from protected/commands dir
            'yiicCommandMap' => array(
                'email'=>array(
                    'class'=>'ext.mailer.MailerCommand',
                    'from'=>'sam@rmcreative.ru',
                ),
            ),
        ),
    ),
)

With the above configuration, you will be able to access web shell in your browser using the following URL:

http://localhost/path/to/index.php?r=webshell

If your application is using path-format URLs with some customized URL rules, you may need to add the following URLs in your application configuration in order to access web shell module:

'components'=>array(
    'urlManager'=>array(
        'urlFormat'=>'path',
            'rules'=>array(
                'webshell'=>'webshell',
                'webshell/<controller:\w+>'=>'webshell/<controller>',
                'webshell/<controller:\w+>/<action:\w+>'=>'webshell/<controller>/<action>',
                …other rules),
    )
)

You can then access web shell via:

http://localhost/path_to_webroot/webshell

Adding custom commands

You can add both shell commands and yiic commands.

Shell commands are configured via commands property of WebShellModule:

'commands' => array(
    // js callback as a command
    'test' => array('js:function(tokens){return "Hello, world!";}', 'Just a test.'),
 
    // ajax callback to http://yourwebsite/post/index?action=cli (will be normalized according to URL rules)
    'postlist' => array(array('/post/index', array('action' => 'cli')), 'Description.'),
 
    // sticky command handler. One will need to type 'exit' to leave its context.
    'stickyhandler' => array(
        array(
            // optional: called when 'stickyhandler' is typed. Can be either URL array or callback.
            'START_HOOK' => array('/post/index', array('action' => 'start')),
            // optional: called when 'exit' is typed. Can be either URL array or callback.
            'EXIT_HOOK' => "js:function(){ return 'bye!'; }",
            // required: called when parameter is typed. Can be either URL array or callback.
            'DISPATCH' => "js:function(tokens){ return "Hi, Jack!"; }",
            // optional: custom prompt
            'PS1' => 'advanced >',
        ),
        'Advanced command.',
    ),
),

Callback for a shell command should look like this:

function actionMyCommandHandler(){
    $tokens = explode(" ", $_GET['tokens']);
    print_r($tokens);
}

To learn about creating custom yiic commands you can read "Console Applications".

Security

There are two module settings that will help you to keep web console secure:

// Allowed IPs, localhost by default. Set to false to allow all IPs.
'ipFilters' => array('127.0.0.1','::1'),
// Valid PHP callback that returns if user should be allowed to use web shell.
// In this example it's valid for PHP 5.3.
'checkAccessCallback' => function($controller, $action){
    return !Yii::app()->user->isGuest;
}

Special thanks

Changelog

1.2

  • Changed charset to application default (Sam Dark).
  • Moved all JS code into own namespace (Sam Dark).

1.1

  • Changed shorttags to echo (Sam Dark).

1.0

  • Initial release (Sam Dark).

Total 7 comments

#12241 report it
Jmper at 2013/03/09 05:53am
Excellent extension

Would be nice to also allow password verification in addition to IP filter, which is useless if you use dynamic IP. Similar to what Gii provides.

#5648 report it
samdark at 2011/10/28 10:34am
Re: yiqing95

It's executed in web context.

#5642 report it
yiqing95 at 2011/10/28 03:10am
using webshell , we can use more classess or resources than running commands from cli .

CConsoleCommand originally target to cli application , so there are some resources you can't access such as : $_REQUEST , $_SESSION , $_GET , $_POST ....

when invoke the command from webshell ,these variables are available ! so should we differ the scenarios ? the commands invokes from apache php module or php.exe ?

#5575 report it
Roman Solomatin at 2011/10/21 11:11am
Hmm

This looks like something that I have been looking for...

#5142 report it
ManInTheBox at 2011/09/18 08:47am
Using Yii's built-in commands

If you want to include Yii's built-in commands like migrate, message... etc, along with your custom commands from protected/commands, you'll need following changes:

// in ext/yiiext/modules/webshell/controllers/DefaultController.php
 
    /**
     * Yiic proxy action
     *
     * @return void
     */
    function actionYiic(){
        $tokens = explode(" ", $_GET['tokens']);
        $commandPath = Yii::app()->getBasePath().DIRECTORY_SEPARATOR.'commands';
        // add path to Yii's commands
        $commandPathYii = Yii::getPathOfAlias('system.cli.commands');
 
        $runner=new CConsoleCommandRunner();
        $runner->commands=$this->getModule()->yiicCommandMap;
        $runner->addCommands($commandPath);
                // register these commands too
        $runner->addCommands($commandPathYii);
 
 
        ob_start();
        $runner->run($tokens);
 
        echo htmlentities(ob_get_clean(), null, Yii::app()->charset);
    }

You cannot use now commands like migrate create because they use STDIN (from CLI) to get user input. But you can override method MigrateCommand::confirm() in your custom components/MyMigrate to always returns true so user won't have chance to answer [yes|no] question (always will be yes). You simply call it from webshell: mymigrate create test_migration

Also disadvantages from this approach are like you cannot change filemode for newly created migration file, so user eg www-data is owner and you cannot modify file, and like commandMap->migrate->templateFile because commandMap is available only for CConsoleApplication and in webshell you're running CWebApplication.

Maybe someone finds it useful and maybe author of extension rewrite it so Yii's built-in commands works 100% as expected.

Update: I just saw this article and realize that MigrateCommand have interactive property so you don't have to create MyMigrate class. In DefaultController you can check if there is migrate in tokens variable, and add one array element to tokens --interactive=0

#1999 report it
ricardograna at 2010/10/28 08:59am
Where is it?

I didn't understand it very well. Is it part of Yii core? Which version? No download?

It is a very interesting feature! I want to try it!

#1958 report it
zaccaria at 2010/10/20 07:58am
SVN

It can be very useful for update the application itself using svn.

Is also possible to authomatize the calling of some command? I mean create a static route that will execute a specific command (svn update, for instance)?

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet
  • Yii Version: 1.1
  • License: New BSD License
  • Developed by: samdark
  • Category: Console
  • Votes: +14
  • Downloaded: 643 times
  • Created on: Oct 19, 2010
  • Last updated: Jun 23, 2013
  • Tags: console