FilterView.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Think.Admin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/Think.Admin
  12. // +----------------------------------------------------------------------
  13. namespace hook;
  14. use think\Request;
  15. /**
  16. * 视图输出过滤
  17. * Class FilterView
  18. * @package hook
  19. * @author Anyon <zoujingli@qq.com>
  20. * @date 2017/04/25 11:59
  21. */
  22. class FilterView
  23. {
  24. /**
  25. * 当前请求对象
  26. * @var Request
  27. */
  28. protected $request;
  29. /**
  30. * 行为入口
  31. * @param $params
  32. */
  33. public function run(&$params)
  34. {
  35. $this->request = Request::instance();
  36. $appRoot = $this->request->root(true);
  37. $replace = [
  38. '__APP__' => $appRoot,
  39. '__SELF__' => $this->request->url(true),
  40. '__PUBLIC__' => strpos($appRoot, EXT) ? ltrim(dirname($appRoot), DS) : $appRoot,
  41. ];
  42. $params = str_replace(array_keys($replace), array_values($replace), $params);
  43. !IS_CLI && $this->baidu($params);
  44. }
  45. /**
  46. * 百度统计实现代码
  47. * @param $params
  48. */
  49. public function baidu(&$params)
  50. {
  51. if (($key = sysconf('tongji_baidu_key'))) {
  52. $https = Request::instance()->isSsl() ? 'https' : 'http';
  53. $script = <<<SCRIPT
  54. \n<!-- 百度统计 开始 -->
  55. <script>
  56. var _hmt = _hmt || [];
  57. (function() {
  58. var hm = document.createElement("script");
  59. hm.src = "{$https}://hm.baidu.com/hm.js?{$key}";
  60. var s = document.getElementsByTagName("script")[0];
  61. s.parentNode.insertBefore(hm, s);
  62. })();
  63. </script>
  64. <!-- 百度统计 结束 -->\n\n
  65. SCRIPT;
  66. $params = preg_replace('|</body>|i', "{$script}</body>", $params);
  67. }
  68. }
  69. }