123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace app\common\controller;
- use app\common\library\Auth;
- use think\Config;
- use think\Controller;
- use think\Hook;
- use think\Lang;
- use think\Loader;
- use think\Validate;
- class Frontend extends Controller
- {
-
- protected $layout = '';
-
- protected $noNeedLogin = [];
-
- protected $noNeedRight = [];
-
- protected $auth = null;
- public function _initialize()
- {
-
- $this->request->filter('trim,strip_tags,htmlspecialchars');
- $modulename = $this->request->module();
- $controllername = Loader::parseName($this->request->controller());
- $actionname = strtolower($this->request->action());
-
- check_ip_allowed();
-
- if ($this->layout) {
- $this->view->engine->layout('layout/' . $this->layout);
- }
- $this->auth = Auth::instance();
-
- $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
- $path = str_replace('.', '/', $controllername) . '/' . $actionname;
-
- $this->auth->setRequestUri($path);
-
- if (!$this->auth->match($this->noNeedLogin)) {
-
- $this->auth->init($token);
-
- if (!$this->auth->isLogin()) {
- $this->error(__('Please login first'), 'index/user/login');
- }
-
- if (!$this->auth->match($this->noNeedRight)) {
-
- if (!$this->auth->check($path)) {
- $this->error(__('You have no permission'));
- }
- }
- } else {
-
- if ($token) {
- $this->auth->init($token);
- }
- }
- $this->view->assign('user', $this->auth->getUser());
-
- $lang = $this->request->langset();
- $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
- $site = Config::get("site");
- $upload = \app\common\model\Config::upload();
-
- Hook::listen("upload_config_init", $upload);
-
- $config = [
- 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
- 'upload' => $upload,
- 'modulename' => $modulename,
- 'controllername' => $controllername,
- 'actionname' => $actionname,
- 'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
- 'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
- 'language' => $lang
- ];
- $config = array_merge($config, Config::get("view_replace_str"));
- Config::set('upload', array_merge(Config::get('upload'), $upload));
-
- Hook::listen("config_init", $config);
-
- $this->loadlang($controllername);
- $this->assign('site', $site);
- $this->assign('config', $config);
- }
-
- protected function loadlang($name)
- {
- $name = Loader::parseName($name);
- $name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';
- $lang = $this->request->langset();
- $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
- Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
- }
-
- protected function assignconfig($name, $value = '')
- {
- $this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
- }
-
- protected function token()
- {
- $token = $this->request->param('__token__');
-
- if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
- $this->error(__('Token verification error'), '', ['__token__' => $this->request->token()]);
- }
-
- $this->request->token();
- }
- }
|