Accesslog.php 3.55 KB
<?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";
// 		});
	}
}