FilterView.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. list($appRoot, $uriSelf) = [$this->request->root(true), $this->request->url(true)];
  37. $uriRoot = strpos($appRoot, EXT) ? ltrim(dirname($appRoot), DS) : $appRoot;
  38. $uriStatic = (Request::instance()->isSsl() ? 'https' : 'http') . "://plugs.ctolog.com";
  39. $uriStatic = "{$uriRoot}/static";
  40. $replace = ['__APP__' => $appRoot, '__SELF__' => $uriSelf, '__PUBLIC__' => $uriRoot, '__STATIC__' => $uriStatic];
  41. $params = str_replace(array_keys($replace), array_values($replace), $params);
  42. !IS_CLI && $this->baidu($params);
  43. }
  44. /**
  45. * 百度统计实现代码
  46. * @param $params
  47. */
  48. public function baidu(&$params)
  49. {
  50. if (($key = sysconf('tongji_baidu_key'))) {
  51. $https = Request::instance()->isSsl() ? 'https' : 'http';
  52. $script = <<<SCRIPT
  53. \n<!-- 百度统计 开始 -->
  54. <script>
  55. var _hmt = _hmt || [];
  56. (function() {
  57. var hm = document.createElement("script");
  58. hm.src = "{$https}://hm.baidu.com/hm.js?{$key}";
  59. var s = document.getElementsByTagName("script")[0];
  60. s.parentNode.insertBefore(hm, s);
  61. })();
  62. </script>
  63. <!-- 百度统计 结束 -->\n\n
  64. SCRIPT;
  65. $params = preg_replace('|</body>|i', "{$script}</body>", $params);
  66. }
  67. }
  68. }