<?php
namespace think\addons;
use app\common\library\Auth;
use think\Config;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
class Controller extends \think\Controller
{
protected $addon = null;
protected $controller = null;
protected $action = null;
protected $template;
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
protected $auth = null;
protected $layout = null;
public function __construct(Request $request = null)
{
if (is_null($request))
{
$request = Request::instance();
}
$this->request = $request;
$this->request->filter('strip_tags');
$convert = Config::get('url_convert');
$filter = $convert ? 'strtolower' : 'trim';
$param = $this->request->param();
$dispatch = $this->request->dispatch();
$var = isset($dispatch['var']) ? $dispatch['var'] : [];
$var = array_merge($param, $var);
if (isset($dispatch['method']) && substr($dispatch['method'][0], 0, 7) == "\\addons")
{
$arr = explode("\\", $dispatch['method'][0]);
$addon = strtolower($arr[2]);
$controller = strtolower(end($arr));
$action = $dispatch['method'][1];
}
else
{
$addon = isset($var['addon']) ? $var['addon'] : '';
$controller = isset($var['controller']) ? $var['controller'] : '';
$action = isset($var['action']) ? $var['action'] : '';
}
$this->addon = $addon ? call_user_func($filter, $addon) : '';
$this->controller = $controller ? call_user_func($filter, $controller) : 'index';
$this->action = $action ? call_user_func($filter, $action) : 'index';
Config::set('template.view_path', ADDON_PATH . $this->addon . DS . 'view' . DS);
parent::__construct($this->request);
}
protected function _initialize()
{
$config = get_addon_config($this->addon);
$this->view->assign("config", $config);
Lang::load([
ADDON_PATH . $this->addon . DS . 'lang' . DS . $this->request->langset() . EXT,
]);
$cdnurl = Config::get('site.cdnurl');
$this->view->replace('__ADDON__', $cdnurl . "/assets/addons/" . $this->addon);
$this->auth = Auth::instance();
$token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
$path = 'addons/' . $this->addon . '/' . str_replace('.', '/', $this->controller) . '/' . $this->action;
$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);
}
}
if ($this->layout)
{
$this->view->engine->layout('layout/' . $this->layout);
}
$this->view->assign('user', $this->auth->getUser());
$site = Config::get("site");
$upload = \app\common\model\Config::upload();
Hook::listen("upload_config_init", $upload);
Config::set('upload', array_merge(Config::get('upload'), $upload));
$this->assign('site', $site);
}
protected function fetch($template = '', $vars = [], $replace = [], $config = [])
{
$controller = Loader::parseName($this->controller);
if ('think' == strtolower(Config::get('template.type')) && $controller && 0 !== strpos($template, '/'))
{
$depr = Config::get('template.view_depr');
$template = str_replace(['/', ':'], $depr, $template);
if ('' == $template)
{
$template = str_replace('.', DS, $controller) . $depr . $this->action;
}
elseif (false === strpos($template, $depr))
{
$template = str_replace('.', DS, $controller) . $depr . $template;
}
}
return parent::fetch($template, $vars, $replace, $config);
}
}