ken\trade
<?phpnamespace 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.*.*'],* ],* ],* ]* ~~~** 默認(rèn)時(shí)此模塊僅允許在 localhost 上進(jìn)行訪問(wèn),也可以通過(guò)配置 * [[allowedIPs]] 屬性來(lái)讓其它機(jī)器訪問(wèn).* * 訪問(wèn)地址: `http://localhost/path/to/index.php?r=gii`** 如果啟用url 美化功能 [[\yii\web\UrlManager::enablePrettyUrl|pretty URLs]],* 則訪問(wèn)地址為: `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 * * 默認(rèn)只允許 localhost 訪問(wèn).* 也可以設(shè)置 外網(wǎng)機(jī)器訪問(wèn),例如 ['127.0.0.1', '192.168.*.*']*/public $allowedIPs = ['127.0.0.1', '::1'];public $allowedHosts = [];/*** @var string 交易日志的目錄*/public $logPath = '@runtime/ktrade';/*** @var array 對(duì)外訪問(wèn)的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('拒絕訪問(wèn) kentrade. 請(qǐng)求的 IP 是 ' . $ip, __METHOD__);return false;}/*** 返回 交易模塊提供的對(duì)外api接口* @return array*** */protected function coreApis(){return ['pay' => ['class' => 'ken\trade\apis'],'pay' => ['class' => 'ken\trade\apis'],];}
}
總結(jié)
- 上一篇: 嵌入式系统概述2-嵌入式系统组成和应用
- 下一篇: C语言描述:DNA