yiiext/webshell-module

Yii Web Shell 允许您从浏览器中运行控制台命令。对于无SSH的Web服务器和类似控制台风格的行政模块都很有用。

安装: 61

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 11

语言:JavaScript

类型:yii-extension

dev-master 2014-11-18 09:48 UTC

This package is not auto-updated.

Last update: 2024-09-10 03:30:22 UTC


README

Yii Web Shell 允许您从浏览器中运行控制台命令。对于无SSH的Web服务器和类似控制台风格的行政模块都很有用。

Yii web shell 使用与您的Web应用程序相同的配置,如果您的应用程序可以正常工作,它也应该可以工作。

由于我们在Web环境中工作,我们没有STDIN。这意味着像 yiic migrate 这样的命令将无法在交互模式下工作。对于 migrate,您可以通过将 interactive 设置为 false 来关闭它。

安装

要使用Web Shell,您必须在应用程序配置中将其作为模块包含,如下所示

[php]
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',
				),
				'migrate' => array(
					'class' => 'system.cli.commands.MigrateCommand',
					'interactive'=>false,
				),
			),
		),
	),
)

使用上述配置,您将能够使用以下URL通过浏览器访问Web Shell

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

如果您的应用程序正在使用路径格式URL和一些自定义URL规则,您可能需要在应用程序配置中添加以下URL,以便访问Web Shell模块

[php]
'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…
        ),
    )
)

然后您可以通过以下方式访问Web Shell

http://localhost/path_to_webroot/webshell

添加自定义命令

您可以添加shell命令和yiic命令。

Shell命令通过 WebShellModule 的 commands 属性进行配置

[php]
'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.',
    ),
),

shell命令的回调应该如下所示

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

有关创建自定义yiic命令的详细信息,您可以阅读 "控制台应用程序"。

安全

有两个模块设置可以帮助您保持Web控制台的安全

[php]
// 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;
}

特别感谢