2014-07-30 17:18:59 +10:00
|
|
|
<?php namespace System\Models;
|
2014-07-29 20:07:20 +10:00
|
|
|
|
2016-04-16 07:44:18 +10:00
|
|
|
use App;
|
2014-07-29 20:07:20 +10:00
|
|
|
use Model;
|
|
|
|
use Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Model for logging 404 errors
|
2014-10-03 18:00:21 +10:00
|
|
|
*
|
|
|
|
* @package october\system
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
2014-07-29 20:07:20 +10:00
|
|
|
*/
|
2014-07-30 17:18:59 +10:00
|
|
|
class RequestLog extends Model
|
2014-07-29 20:07:20 +10:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string The database table used by the model.
|
|
|
|
*/
|
2014-07-30 17:18:59 +10:00
|
|
|
protected $table = 'system_request_logs';
|
2014-07-29 20:07:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array The attributes that aren't mass assignable.
|
|
|
|
*/
|
|
|
|
protected $guarded = [];
|
|
|
|
|
2014-07-30 18:24:16 +10:00
|
|
|
/**
|
|
|
|
* @var array List of attribute names which are json encoded and decoded from the database.
|
|
|
|
*/
|
|
|
|
protected $jsonable = ['referer'];
|
|
|
|
|
2014-07-29 20:07:20 +10:00
|
|
|
/**
|
|
|
|
* Creates a log record
|
|
|
|
* @return self
|
|
|
|
*/
|
2014-07-30 17:33:26 +10:00
|
|
|
public static function add($statusCode = 404)
|
2014-07-29 20:07:20 +10:00
|
|
|
{
|
2016-04-16 07:44:18 +10:00
|
|
|
if (!App::hasDatabase()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-14 15:07:57 +11:00
|
|
|
|
2017-02-08 05:43:40 +11:00
|
|
|
if (!LogSetting::get('log_requests')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-29 20:07:20 +10:00
|
|
|
$record = static::firstOrNew([
|
2019-04-21 20:12:30 -04:00
|
|
|
'url' => substr(Request::fullUrl(), 0, 191),
|
2014-07-30 17:33:26 +10:00
|
|
|
'status_code' => $statusCode,
|
2014-07-29 20:07:20 +10:00
|
|
|
]);
|
|
|
|
|
2014-07-30 18:24:16 +10:00
|
|
|
if ($referer = Request::header('referer')) {
|
|
|
|
$referers = (array) $record->referer ?: [];
|
|
|
|
$referers[] = $referer;
|
|
|
|
$record->referer = $referers;
|
|
|
|
}
|
|
|
|
|
2014-07-29 20:07:20 +10:00
|
|
|
if (!$record->exists) {
|
|
|
|
$record->count = 1;
|
|
|
|
$record->save();
|
2014-11-01 12:00:45 +11:00
|
|
|
}
|
|
|
|
else {
|
2014-07-29 20:07:20 +10:00
|
|
|
$record->increment('count');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|