ken\trade


<?php

namespace ken\trade;

use Yii;
use yii\base\BootstrapInterface;
use yii\web\ForbiddenHttpException;

/**
* 交易模块 - Ken Trade
*
* 使用配置
*
* ~~~
* return [
* 'bootstrap' => ['ktrade'],
* 'modules' => [
* 'ktrade' => [
* 'class' => 'ken\trade\Module',
* 'allowedIPs' => ['127.0.0.1', '192.168.*.*'],
* ],
* ],
* ]
* ~~~
*
* 默认时此模块仅允许在 localhost 上进行访问,也可以通过配置
* [[allowedIPs]] 属性来让其它机器访问.
*
* 访问地址: `http://localhost/path/to/index.php?r=gii`
*
* 如果启用url 美化功能 [[\yii\web\UrlManager::enablePrettyUrl|pretty URLs]],
* 则访问地址为: `http://localhost/path/to/index.php/gii`
*
* @author Xiaohan Xu <vb2005xu@qq.com>
* @since 2.0
*/
class Module extends \yii\base\Module implements BootstrapInterface
{
/**
* @inheritdoc
*/
public $controllerNamespace = 'ken\trade\controllers';

/**
* @var array
*
* 默认只允许 localhost 访问.
* 也可以设置 外网机器访问,例如 ['127.0.0.1', '192.168.*.*']
*/
public $allowedIPs = ['127.0.0.1', '::1'];
public $allowedHosts = [];

/**
* @var string 交易日志的目录
*/
public $logPath = '@runtime/ktrade';

/**
* @var array 对外访问的api列表
*/
public $apis = [];

/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->logPath = Yii::getAlias($this->logPath);
}

/**
* @inheritdoc
*/
public function bootstrap($app)
{
$app->getUrlManager()->addRules([
$this->id => $this->id,
$this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>' => $this->id . '/<controller>/<action>',
], false);
}

/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}

if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
throw new ForbiddenHttpException('You are not allowed to access this page.');
}

foreach (array_merge($this->coreApis(), $this->apis) as $id => $config) {
$this->apis[$id] = Yii::createObject($config);
}

$this->resetGlobalSettings();

return true;
}

/**
* Resets potentially incompatible global settings done in app config.
*/
protected function resetGlobalSettings()
{
if (Yii::$app instanceof \yii\web\Application) {
Yii::$app->assetManager->bundles = [];
}
}

/**
* @return boolean whether the module can be accessed by the current user
*/
protected function checkAccess()
{
$ip = Yii::$app->getRequest()->getUserIP();
foreach ($this->allowedIPs as $filter) {
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
return true;
}
}
foreach ($this->allowedHosts as $hostname) {
$filter = gethostbyname($hostname);
if ($filter === $ip) {
return true;
}
}
Yii::warning('拒绝访问 kentrade. 请求的 IP 是 ' . $ip, __METHOD__);
return false;
}

/**
* 返回 交易模块提供的对外api接口
* @return array
*
*
*
*/
protected function coreApis()
{
return [
'pay' => ['class' => 'ken\trade\apis'],
'pay' => ['class' => 'ken\trade\apis'],
];
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值