Accesslog.php
3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Auth;
use Config;
use Route;
use Request;
use Input;
class Accesslog extends Model {
public $timestamps = false;
protected $table = 'accesslogs';
protected $fillable = array('starttime','endtime','user','action','duration','url','ip','postdata','group');
private $sqlloglen=0;
private function validLogCheck()
{
if(strstr(Config::get('app.skiplog'),",".Request::path().","))return false;
if(preg_match('/(.jpg|.jpeg|.css|.less|.js|.png|.flv|.swf|.pbj|.wav|.woff|.svg|.eot)/',strtolower(Request::path())))return false;
return true;
}
public function startLog()
{
if(Auth::check()&&Auth::user()->moduleACL("Admin",true,true,true))
{
Config::set('app.debug',Config::get('app.admindebug'));
}
if($this->validLogCheck())
{
$this->starttime=microtime(true);
if (Auth::check())
{
$this->user=Auth::user()->username;
$this->group=Auth::user()->group;
}
else $this->group='Default';
$this->url=Request::path().(Request::getQueryString() ? ('?' . Request::getQueryString()) : '');
$this->ip=Request::getClientIp();if($this->ip=='')$this->ip='-';
$this->postdata=json_encode(Input::except('password','csrftoken','_token'), true);
$this->save();
}
return $this;
}
public function stopLog()
{
if($this->validLogCheck())
{
if (Auth::check())
{
$this->user=Auth::user()->username;
$this->group=Auth::user()->group;
}
else $this->group='Default';
$this->action="".Route::currentRouteName();
$nowtime=microtime(true);
$this->endtime=date("Y-m-d H:i:s",$nowtime);
$this->duration=round($nowtime-$this->starttime,4);
$this->queries=json_encode(Config::get('app.sqllog'));
$this->memory=memory_get_peak_usage(true);
$this->save();
}
}
public function logQuery($query,$param,$militime)
{
if($this->sqlloglen<500)
{
$this->sqlloglen++;
$logstr=Config::get('app.sqllog');
$logstr[]=array($query,$param,$militime);
Config::set('app.sqllog',$logstr);
}
}
protected static function boot()
{
parent::boot();
static::addGlobalScope('groupacl', function(\Illuminate\Database\Eloquent\Builder $builder)
{
if(Auth::check())
{
$builder->whereIn('group',Auth::user()->getAccessList("group",true,false,false));
}
});
// static::creating(function($model)
// {
// if(Auth::check())
// {
// if($model->group=='')$model->group=Auth::user()->group;
// if($model->group=='')$model->group="Default";
//
// $groupacl=Auth::user()->getAccessList("group",false,true,false);
// if(!in_array($model->group,$groupacl))
// {
// throw new \Exception("No Access to Create [".Auth::user()->id."] : (".implode(",",$groupacl).") in $model->group");
// return false;
// }
// }
// else if($model->group=='')$model->group="Default";
// });
// static::updating(function($model)
// {
// if(Auth::check())
// {
// $original = $model->getOriginal();
// if($original['group']=='')$original['group']='Default';
//
// if($model->group=='')$model->group=Auth::user()->group;
// if($model->group=='')$model->group="Default";
//
// $groupacl=Auth::user()->getAccessList("group",false,true,false);
// if(!in_array($original['group'],$groupacl)||!in_array($model->group,$groupacl))
// {
// throw new \Exception("No Access to Update [".Auth::user()->id."] : (".implode(",",$groupacl).") in $model->group");
// return false;
// }
// }
// else if($model->group=='')$model->group="Default";
// });
}
}