yf_logs classLogging some common actions.
| File | plugins/logs/classes/yf_logs.class.php |
|---|---|
| Use | _class('logs') |
A site sets them in its config with $PROJECT_CONF['logs']['NAME'] = ...;
| Property | Default | Description |
|---|---|---|
$CUR_LOG_LEVELint |
8 |
@conf_skip Current log level limit ('0' for disabling), could be: 0|E_ERROR|E_WARNING|E_NOTICE |
$LOG_EXEC_ADMINbool |
true |
Only if main()->LOG_EXEC enabled |
$LOG_EXEC_USERbool |
false |
Only if main()->LOG_EXEC enabled |
$LOG_USER_ACTIONSbool |
true |
Turn logging of user actions for the stats on/off |
$STORE_ADMIN_AUTHbool |
true |
Store user auth into log table |
$STORE_USER_AUTHbool |
false |
Store user auth into log table |
$UPDATE_ADMIN_LAST_LOGINbool |
true |
Update admin's record for last login |
$UPDATE_LAST_LOGINbool |
false |
Update user's record for last login |
$_LOGGINGbool |
false |
Turn logging on/off |
$_avail_action_namesarray |
['visit', 'review', 'add_comment', 'del_comment', 'add_friend', 'del_friend'] |
@conf_skip Available action names |
$_error_levels_namesarray |
[1 => 'error', 2 => 'warning', 8 => 'notice'] |
@conf_skip Error levels text representation |
public function log_exec()
Log script execution params.
public function log_exec()
{
if (MAIN_TYPE_ADMIN && $this->LOG_EXEC_ADMIN) {
return _class('logs_exec_admin', 'classes/logs/')->go();
} elseif (MAIN_TYPE_USER && $this->LOG_EXEC_USER) {
return _class('logs_exec_user', 'classes/logs/')->go();
}
}
public function save_user_error($errors)
public function save_user_error($errors)
{
return _class('logs_user_error', 'classes/logs/')->_track_error(implode(PHP_EOL, (array) $errors));
}
public function store_admin_auth($A = [])
Store admin authentication in log table.
public function store_admin_auth($A = [])
{
// Check if looging needed
if ( ! is_array($A) || ! $this->_LOGGING) {
return false;
}
if ($this->UPDATE_ADMIN_LAST_LOGIN) {
db()->query(
'UPDATE ' . db('admin') . ' SET
last_login = ' . time() . ',
num_logins = num_logins + 1
WHERE id=' . (int) ($A['id'])
);
}
// Prepare db record
$IP = is_object(common()) ? common()->get_ip() : false;
if ( ! $IP) {
$IP = ($_SERVER['REMOTE_ADDR'] ?? null);
}
if ($this->STORE_ADMIN_AUTH) {
db()->INSERT('log_admin_auth', [
'admin_id' => (int) ($A['id']),
'login' => _es($A['login']),
'group' => (int) ($A['group']),
'date' => time(),
'session_id' => session_id(),
'ip' => $IP,
'user_agent' => _es(getenv('HTTP_USER_AGENT')),
'referer' => _es(getenv('HTTP_REFERER')),
]);
conf('_log_admin_auth_insert_id', db()->INSERT_ID());
}
}
public function store_db_queries_log()
Save queries log.
public function store_db_queries_log()
{
return _class('logs_db_queries', 'classes/logs/')->go();
}
public function store_slow_pages()
Store slow pages (even when DEBUG_MODE is turned off).
public function store_slow_pages()
{
// TODO
}
public function store_user_auth($A = [])
Store user authentication in log table.
public function store_user_auth($A = [])
{
// Check if looging needed
if ( ! is_array($A) || ! $this->_LOGGING) {
return false;
}
// Update user last login
$this->_update_last_login($A);
// Prepare db record
$IP = is_object(common()) ? common()->get_ip() : false;
if ( ! $IP) {
$IP = ($_SERVER['REMOTE_ADDR'] ?? null);
}
if ($this->STORE_USER_AUTH) {
db()->INSERT('log_auth', [
'user_id' => (int) ($A['id']),
'login' => _es($A['login']),
'group' => (int) ($A['group']),
'date' => time(),
'session_id' => session_id(),
'ip' => $IP,
'user_agent' => _es(getenv('HTTP_USER_AGENT')),
'referer' => _es(getenv('HTTP_REFERER')),
]);
conf('_log_auth_insert_id', db()->INSERT_ID());
}
}
_ hooks and helpers; not routable as page actionspublic function __call($name, $args)
Catch missing method call.
public function __call($name, $args)
{
return main()->extend_call($this, $name, $args);
}
public function _init()
Constructor.
public function _init()
{
$site_logging = conf('site_logging');
if (isset($site_logging)) {
$this->_LOGGING = $site_logging;
}
}
public function _log_user_action($action_name, $owner_id, $object_name = '', $object_id = 0)
Log user actions to the DB for creating statistics reports.
public function _log_user_action($action_name, $owner_id, $object_name = '', $object_id = 0)
{
if ( ! $this->LOG_USER_ACTIONS || ! in_array($action_name, $this->_avail_action_names)) {
return false;
}
db()->insert_safe('log_user_action', [
'owner_id' => (int) $owner_id,
'action_name' => $action_name,
'member_id' => main()->USER_ID,
'object_name' => $object_name,
'object_id' => (int) $object_id,
'add_date' => time(),
]);
return true;
}
public function _save_debug_log($text = '', $log_level = E_NOTICE, $trace = [], $simple = false)
Save debug log.
public function _save_debug_log($text = '', $log_level = E_NOTICE, $trace = [], $simple = false)
{
if (empty($log_level) || ! isset($this->_error_levels_names[$log_level])) {
$log_level = E_NOTICE;
}
if (empty($this->CUR_LOG_LEVEL) || $log_level > $this->CUR_LOG_LEVEL) {
return false;
}
$LOGS_DIR = APP_PATH . 'logs/';
_mkdir_m($LOGS_DIR);
$log_data = '';
$log_data .= ! $simple ? date('Y-m-d H:i:s') . ' [' . $this->_error_levels_names[$log_level] . '] ' : '';
$log_data .= $text;
$log_data .= ! $simple ? ' | ' . str_replace("\n", ' ', main()->trace_string()) : '';
$log_data .= "\n";
file_put_contents($LOGS_DIR . 'debug_logs.log', $log_data, FILE_APPEND);
}
public function _update_last_login($A = [])
Store user authentication in log table.
public function _update_last_login($A = [])
{
// Check if looging needed
if ( ! is_array($A) || ! $this->_LOGGING || ! $this->UPDATE_LAST_LOGIN) {
return false;
}
// Prepare db record
$sql = 'UPDATE ' . db('user') . ' SET
last_login = ' . time() . ',
num_logins = num_logins + 1
WHERE id=' . (int) ($A['id']);
db()->_add_shutdown_query($sql);
}