Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Adminlog.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 后台用户管理
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\model;
  15. use app\admin\service\User;
  16. use think\Loader;
  17. use think\Model;
  18. class Adminlog extends Model
  19. {
  20. protected $autoWriteTimestamp = true;
  21. protected $updateTime = false;
  22. //自定义日志标题
  23. protected static $title = '';
  24. //自定义日志内容
  25. protected static $content = '';
  26. //忽略的链接正则列表
  27. protected static $ignoreRegex = [
  28. '/^(.*)\/(selectpage|index|logout)$/i',
  29. ];
  30. public static function setTitle($title)
  31. {
  32. self::$title = $title;
  33. }
  34. public static function setContent($content)
  35. {
  36. self::$content = $content;
  37. }
  38. public static function setIgnoreRegex($regex = [])
  39. {
  40. $regex = is_array($regex) ? $regex : [$regex];
  41. self::$ignoreRegex = array_merge(self::$ignoreRegex, $regex);
  42. }
  43. /**
  44. * 记录日志
  45. * @param string $title
  46. * @param string $content
  47. */
  48. public static function record($title = '', $content = '')
  49. {
  50. $auth = User::instance();
  51. $admin_id = $auth->isLogin() ? $auth->id : 0;
  52. $username = $auth->isLogin() ? $auth->username : '未知';
  53. // 设置过滤函数
  54. request()->filter('trim,strip_tags,htmlspecialchars');
  55. $controllername = Loader::parseName(request()->controller());
  56. $actionname = strtolower(request()->action());
  57. $path = $controllername . '/' . $actionname;
  58. if (self::$ignoreRegex) {
  59. foreach (self::$ignoreRegex as $index => $item) {
  60. if (preg_match($item, $path)) {
  61. return;
  62. }
  63. }
  64. }
  65. $content = $content ?: self::$content;
  66. if (!$content) {
  67. $content = request()->param('', null);
  68. $content = self::getPureContent($content);
  69. }
  70. $title = $title ?: self::$title;
  71. if (!$title) {
  72. $controllerTitle = AuthRule::where('name', $controllername)->value('title');
  73. $title = AuthRule::where('name', $path)->value('title');
  74. $title = $title ?: '未知' . '(' . $actionname . ')';
  75. $title = $controllerTitle ? ($controllerTitle . '-' . $title) : $title;
  76. }
  77. self::create([
  78. 'title' => $title,
  79. 'content' => !is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
  80. 'url' => substr(xss_clean(strip_tags(request()->url())), 0, 1500),
  81. 'admin_id' => $admin_id,
  82. 'username' => $username,
  83. 'useragent' => substr(request()->server('HTTP_USER_AGENT'), 0, 255),
  84. 'ip' => xss_clean(strip_tags(request()->ip())),
  85. ]);
  86. }
  87. /**
  88. * 获取已屏蔽关键信息的数据
  89. * @param $content
  90. * @return false|string
  91. */
  92. protected static function getPureContent($content)
  93. {
  94. if (!is_array($content)) {
  95. return $content;
  96. }
  97. foreach ($content as $index => &$item) {
  98. if (preg_match("/(password|salt|token)/i", $index)) {
  99. $item = "***";
  100. } else {
  101. if (is_array($item)) {
  102. $item = self::getPureContent($item);
  103. }
  104. }
  105. }
  106. return $content;
  107. }
  108. }