yf_logs_exec_admin class

Save execution info log for admin section.

Fileplugins/logs/classes/logs/yf_logs_exec_admin.class.php
Use_class('logs_exec_admin', 'classes/logs/')

Public properties

A site sets them in its config with $PROJECT_CONF['logs_exec_admin']['NAME'] = ...;

PropertyDefaultDescription
$EXCLUDE_IPS
array
['127.0.0.1']
$EXCLUDE_USER_AGENTS
array
['zabbix', 'acceptance_tests']
$FILTER_BOTS
bool
false
$LOGGING
bool
true
$LOG_DIR_NAME 'logs/log_admin_exec/'
$LOG_DRIVER
enum('db','file')
'db'
$LOG_NOT_FOUND_PAGES
bool
true
$LOG_NO_GRAPHICS_PAGES
bool
true
$STOP_LIST
array
[] Stop-list for logging (REGEXPs allowed here)
$USE_STOP_LIST
bool
true
$checks null
$log_denied_because null

Public methods 2

go

public function go()

Do save.

    public function go()
    {
        if ( ! $this->LOGGING || MAIN_TYPE_USER) {
            return false;
        }
        if (main()->NO_GRAPHICS && ! $this->LOG_NO_GRAPHICS_PAGES) {
            return false;
        }
        if (($GLOBALS['task_not_found'] ?? false) && ! $this->LOG_NOT_FOUND_PAGES) {
            return false;
        }
        if ($this->USE_STOP_LIST) {
            foreach ((array) $this->STOP_LIST as $_cur_pattern) {
                if (preg_match('/' . $_cur_pattern . '/i', $_SERVER['QUERY_STRING'])) {
                    return false;
                }
            }
        }
        if ($this->FILTER_BOTS && ! main()->USER_ID) {
            $SPIDER_NAME = common()->_is_spider(($_SERVER['REMOTE_ADDR'] ?? null), ($_SERVER['HTTP_USER_AGENT'] ?? null));
            if ($SPIDER_NAME) {
                return false;
            }
        }
        $checks = null;
        if ($this->EXCLUDE_IPS) {
            $ip = common()->get_ip();
            if ($ip && (isset($this->EXCLUDE_IPS[$ip]) || in_array($ip, $this->EXCLUDE_IPS))) {
                $checks['exclude_ip'] = true;
            }
        }
        if ($this->EXCLUDE_USER_AGENTS) {
            $ua = ($_SERVER['HTTP_USER_AGENT'] ?? null);
            foreach ((array) $this->EXCLUDE_USER_AGENTS as $pattern) {
                if ($ua && preg_match('~' . $pattern . '~i', $ua)) {
                    $checks['exclude_ua'] = true;
                }
            }
        }
        $this->checks = $checks;
        foreach ((array) $checks as $name => $is_denied) {
            if ($is_denied) {
                $this->log_denied_because = $name;
                return false;
            }
        }
        $time_end = $GLOBALS['time_end'] ?? microtime(true);
        $exec_time = str_replace(',', '.', common()->_format_time_value($time_end - main()->_time_start));
        $query_string = ($_SERVER['QUERY_STRING'] ?? null);
        // TODO: use parse_url here
        if (main()->is_console()) {
            $query_string = http_build_query($_GET);
        }
        $data = [
            'admin_id' => (int) main()->ADMIN_ID,
            'admin_group' => (int) main()->ADMIN_GROUP,
            'date' => time(),
            'ip' => common()->get_ip(),
            'user_agent' => (string) ($_SERVER['HTTP_USER_AGENT'] ?? null),
            'referer' => (string) ($_SERVER['HTTP_REFERER'] ?? null),
            'query_string' => (string) $query_string,
            'request_uri' => ($_SERVER['HTTP_HOST'] ?? null) . ($_SERVER['REQUEST_URI'] ?? null),
            'exec_time' => $exec_time,
            'num_dbq' => (int) db()->NUM_QUERIES,
            'page_size' => (int) tpl()->_output_body_length,
            'site_id' => (int) conf('SITE_ID'),
            'server_id' => (int) conf('SERVER_ID'),
        ];
        if ($this->LOG_DRIVER == 'db') {
            $sql = db()->insert_safe('log_admin_exec', $data, $as_sql = true);
            db()->_add_shutdown_query($sql);
        } elseif ($this->LOG_DRIVER == 'file') {
            $data['output_cache'] = '0';  // mean: exec full mode (not from output cache)
            $log_file_path = INCLUDE_PATH . $this->LOG_DIR_NAME . gmdate('Y-m-d') . '.log';
            $log_dir_path = dirname($log_file_path);
            if ( ! file_exists($log_dir_path)) {
                _mkdir_m($log_dir_path);
            }
            file_put_contents($log_file_path, implode('#@#', $data) . PHP_EOL, FILE_APPEND);
        }
    }

Methods starting with _ hooks and helpers; not routable as page actions

_init

public function _init()
    public function _init()
    {
        if ( ! $this->LOG_DRIVER || ! in_array($this->LOG_DRIVER, ['db', 'file'])) {
            $this->LOG_DRIVER = 'file';
        }
    }