View.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\home\controller;
  14. class View extends Base
  15. {
  16. // 模型标识
  17. public $nid = '';
  18. // 模型ID
  19. public $channel = '';
  20. // 模型名称
  21. public $modelName = '';
  22. public function _initialize() {
  23. parent::_initialize();
  24. }
  25. /**
  26. * 内容页
  27. */
  28. public function index($aid = '')
  29. {
  30. if (!is_numeric($aid) || strval(intval($aid)) !== strval($aid)) {
  31. abort(404,'页面不存在');
  32. }
  33. $seo_pseudo = config('ey_config.seo_pseudo');
  34. /*URL上参数的校验*/
  35. if (3 == $seo_pseudo)
  36. {
  37. if (stristr($this->request->url(), '&c=View&a=index&')) {
  38. abort(404,'页面不存在');
  39. }
  40. }
  41. else if (1 == $seo_pseudo || (2 == $seo_pseudo && isMobile()))
  42. {
  43. $seo_dynamic_format = config('ey_config.seo_dynamic_format');
  44. if (1 == $seo_pseudo && 2 == $seo_dynamic_format && stristr($this->request->url(), '&c=View&a=index&')) {
  45. abort(404,'页面不存在');
  46. }
  47. }
  48. /*--end*/
  49. $aid = intval($aid);
  50. $archivesInfo = M('archives')->field('a.typeid, a.channel, b.nid, b.ctl_name')
  51. ->alias('a')
  52. ->join('__CHANNELTYPE__ b', 'a.channel = b.id', 'LEFT')
  53. ->where([
  54. 'a.aid' => $aid,
  55. 'a.is_del' => 0,
  56. ])
  57. ->find();
  58. if (empty($archivesInfo) || !in_array($archivesInfo['channel'], config('global.allow_release_channel'))) {
  59. abort(404,'页面不存在');
  60. // $this->redirect('/public/static/errpage/404.html', 301);
  61. }
  62. $this->nid = $archivesInfo['nid'];
  63. $this->channel = $archivesInfo['channel'];
  64. $this->modelName = $archivesInfo['ctl_name'];
  65. $result = model($this->modelName)->getInfo($aid);
  66. // 若是管理员则不受限制
  67. if (session('?admin_id')) {
  68. if ($result['arcrank'] == -1 && $result['users_id'] != session('users_id')) {
  69. $this->success('待审核稿件,你没有权限阅读!');
  70. }
  71. }
  72. // 外部链接跳转
  73. if ($result['is_jump'] == 1) {
  74. header('Location: '.$result['jumplinks']);
  75. exit;
  76. }
  77. /*--end*/
  78. $tid = $result['typeid'];
  79. $arctypeInfo = model('Arctype')->getInfo($tid);
  80. /*自定义字段的数据格式处理*/
  81. $arctypeInfo = $this->fieldLogic->getTableFieldList($arctypeInfo, config('global.arctype_channel_id'));
  82. /*--end*/
  83. if (!empty($arctypeInfo)) {
  84. /*URL上参数的校验*/
  85. if (3 == $seo_pseudo) {
  86. $dirname = input('param.dirname/s');
  87. $dirname2 = '';
  88. $seo_rewrite_format = config('ey_config.seo_rewrite_format');
  89. if (1 == $seo_rewrite_format) {
  90. $toptypeRow = model('Arctype')->getAllPid($tid);
  91. $toptypeinfo = current($toptypeRow);
  92. $dirname2 = $toptypeinfo['dirname'];
  93. } else if (2 == $seo_rewrite_format) {
  94. $dirname2 = $arctypeInfo['dirname'];
  95. } else if (3 == $seo_rewrite_format) {
  96. $dirname2 = $arctypeInfo['dirname'];
  97. }
  98. if ($dirname != $dirname2) {
  99. abort(404,'页面不存在');
  100. }
  101. }
  102. /*--end*/
  103. // 是否有子栏目,用于标记【全部】选中状态
  104. $arctypeInfo['has_children'] = model('Arctype')->hasChildren($tid);
  105. // 文档模板文件,不指定文档模板,默认以栏目设置的为主
  106. empty($result['tempview']) && $result['tempview'] = $arctypeInfo['tempview'];
  107. /*给没有type前缀的字段新增一个带前缀的字段,并赋予相同的值*/
  108. foreach ($arctypeInfo as $key => $val) {
  109. if (!preg_match('/^type/i',$key)) {
  110. $key_new = 'type'.$key;
  111. !array_key_exists($key_new, $arctypeInfo) && $arctypeInfo[$key_new] = $val;
  112. }
  113. }
  114. /*--end*/
  115. } else {
  116. abort(404,'页面不存在');
  117. }
  118. $result = array_merge($arctypeInfo, $result);
  119. // 文档链接
  120. $result['arcurl'] = $result['pageurl'] = '';
  121. if ($result['is_jump'] != 1) {
  122. $result['arcurl'] = $result['pageurl'] = $this->request->url(true);
  123. }
  124. /*--end*/
  125. // seo
  126. $result['seo_title'] = set_arcseotitle($result['title'], $result['seo_title'], $result['typename']);
  127. $result['seo_description'] = @msubstr(checkStrHtml($result['seo_description']), 0, config('global.arc_seo_description_length'), false);
  128. /*支持子目录*/
  129. $result['litpic'] = handle_subdir_pic($result['litpic']);
  130. /*--end*/
  131. $result = view_logic($aid, $this->channel, $result, true); // 模型对应逻辑
  132. /*自定义字段的数据格式处理*/
  133. $result = $this->fieldLogic->getChannelFieldList($result, $this->channel);
  134. /*--end*/
  135. $eyou = array(
  136. 'type' => $arctypeInfo,
  137. 'field' => $result,
  138. );
  139. $this->eyou = array_merge($this->eyou, $eyou);
  140. $this->assign('eyou', $this->eyou);
  141. /*模板文件*/
  142. $viewfile = !empty($result['tempview'])
  143. ? str_replace('.'.$this->view_suffix, '',$result['tempview'])
  144. : 'view_'.$this->nid;
  145. /*--end*/
  146. /*多语言内置模板文件名*/
  147. if (!empty($this->home_lang)) {
  148. $viewfilepath = TEMPLATE_PATH.$this->theme_style.DS.$viewfile."_{$this->home_lang}.".$this->view_suffix;
  149. if (file_exists($viewfilepath)) {
  150. $viewfile .= "_{$this->home_lang}";
  151. }
  152. }
  153. /*--end*/
  154. // 若需要会员权限则执行
  155. if ($this->eyou['field']['arcrank'] > 0) {
  156. $msg = action('api/Ajax/get_arcrank', ['aid'=>$aid, 'vars'=>1]);
  157. if (true !== $msg) {
  158. $this->error($msg);
  159. }
  160. }
  161. return $this->fetch(":{$viewfile}");
  162. }
  163. /**
  164. * 下载文件
  165. */
  166. public function downfile()
  167. {
  168. $file_id = input('param.id/d', 0);
  169. $uhash = input('param.uhash/s', '');
  170. if (empty($file_id) || empty($uhash)) {
  171. $this->error('下载地址出错!');
  172. exit;
  173. }
  174. clearstatcache();
  175. // 查询信息
  176. $map = array(
  177. 'a.file_id' => $file_id,
  178. 'a.uhash' => $uhash,
  179. );
  180. $result = M('download_file')
  181. ->alias('a')
  182. ->field('a.*,b.arc_level_id')
  183. ->join('__ARCHIVES__ b', 'a.aid = b.aid', 'LEFT')
  184. ->where($map)
  185. ->find();
  186. $file_url_gbk = iconv("utf-8","gb2312//IGNORE",$result['file_url']);
  187. $file_url_gbk = preg_replace('#^(/[/\w]+)?(/public/upload/soft/|/uploads/soft/)#i', '$2', $file_url_gbk);
  188. if (empty($result) || (!is_http_url($result['file_url']) && !file_exists('.'.$file_url_gbk))) {
  189. $this->error('下载文件不存在!');
  190. exit;
  191. }
  192. // 判断会员信息
  193. if (0 < intval($result['arc_level_id'])) {
  194. $UsersData = session('users');
  195. if (empty($UsersData['users_id'])) {
  196. $this->error('请登录后下载!');
  197. exit;
  198. }else{
  199. /*判断会员是否可下载该文件--2019-06-21 陈风任添加*/
  200. // 查询会员信息
  201. $users = M('users')
  202. ->alias('a')
  203. ->field('a.users_id,b.level_value,b.level_name')
  204. ->join('__USERS_LEVEL__ b', 'a.level = b.level_id', 'LEFT')
  205. ->where(['a.users_id'=>$UsersData['users_id']])
  206. ->find();
  207. // 查询下载所需等级值
  208. $file_level = M('archives')
  209. ->alias('a')
  210. ->field('b.level_value,b.level_name')
  211. ->join('__USERS_LEVEL__ b', 'a.arc_level_id = b.level_id', 'LEFT')
  212. ->where(['a.aid'=>$result['aid']])
  213. ->find();
  214. if ($users['level_value'] < $file_level['level_value']) {
  215. $msg = '文件为【'.$file_level['level_name'].'】可下载,您当前为【'.$users['level_name'].'】,请先升级!';
  216. $this->error($msg);
  217. exit;
  218. }
  219. /*--end*/
  220. }
  221. }
  222. // 外部下载链接
  223. if (is_http_url($result['file_url'])) {
  224. if ($result['uhash'] != md5($result['file_url'])) {
  225. $this->error('下载地址出错!');
  226. }
  227. // 记录下载次数
  228. $this->download_log($result['file_id'], $result['aid']);
  229. if (IS_AJAX) {
  230. $this->success('正在跳转中……', $result['file_url']);
  231. } else {
  232. $this->redirect($result['file_url']);
  233. exit;
  234. }
  235. }
  236. // 本站链接
  237. else
  238. {
  239. if (md5_file('.'.$file_url_gbk) != $result['md5file']) {
  240. $this->error('下载文件包已损坏!');
  241. }
  242. // 记录下载次数
  243. $this->download_log($result['file_id'], $result['aid']);
  244. $uhash_mch = mchStrCode($uhash);
  245. $url = $this->root_dir."/index.php?m=home&c=View&a=download_file&file_id={$file_id}&uhash={$uhash_mch}";
  246. if (IS_AJAX) {
  247. $this->success('开始下载中……', $url);
  248. } else {
  249. $url = $this->request->domain().$url;
  250. $this->redirect($url);
  251. exit;
  252. }
  253. }
  254. }
  255. /**
  256. * 本地附件下载
  257. */
  258. public function download_file()
  259. {
  260. $file_id = input('param.file_id/d');
  261. $uhash = input('param.uhash/s', '');
  262. $uhash = mchStrCode($uhash, 'DECODE');
  263. $map = array(
  264. 'file_id' => $file_id,
  265. );
  266. $result = M('download_file')->field('file_url,file_mime,uhash')->where($map)->find();
  267. if (!empty($result['uhash']) && $uhash != $result['uhash']) {
  268. $this->error('下载地址出错!');
  269. }
  270. download_file($result['file_url'], $result['file_mime']);
  271. exit;
  272. }
  273. /**
  274. * 记录下载次数(重复下载不做记录,游客可重复记录)
  275. */
  276. private function download_log($file_id = 0, $aid = 0)
  277. {
  278. try {
  279. $users_id = session('users_id');
  280. $users_id = intval($users_id);
  281. $counts = M('download_log')->where([
  282. 'file_id' => $file_id,
  283. 'aid' => $aid,
  284. 'users_id' => $users_id,
  285. ])->count();
  286. if (empty($users_id) || empty($counts)) {
  287. $saveData = [
  288. 'users_id' => $users_id,
  289. 'aid' => $aid,
  290. 'file_id' => $file_id,
  291. 'ip' => clientIP(),
  292. 'add_time' => getTime(),
  293. ];
  294. $r = M('download_log')->insertGetId($saveData);
  295. if ($r !== false) {
  296. M('download_file')->where(['file_id'=>$file_id])->setInc('downcount');
  297. M('archives')->where(['aid'=>$aid])->setInc('downcount');
  298. }
  299. }
  300. } catch (\Exception $e) {}
  301. }
  302. }