File "class-gplvault-log-handler-file.php"

Full path: /home/kosmetik/public_html/wp-content/plugins/gplvault-updater/includes/logger/handlers/class-gplvault-log-handler-file.php
File size: 7.5 B
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

if (!defined('ABSPATH')) {
    exit;
}
class GPLVault_Log_Handler_File extends GPLVault_Log_Handler
{
    protected $handles = array();
    protected $log_size_limit;
    protected $cached_logs = array();
    public function __construct($log_size_limit = null)
    {
        if (null === $log_size_limit) {
            $log_size_limit = 5 * 1024 * 1024;
        }
        $this->log_size_limit = apply_filters('gplvault_log_file_size_limit', $log_size_limit);
        add_action('plugins_loaded', array($this, 'write_cached_logs'));
    }
    public function __destruct()
    {
        foreach ($this->handles as $handle) {
            if (is_resource($handle)) {
                fclose($handle);
            }
        }
    }
    public function handle($timestamp, $level, $message, $context)
    {
        if (isset($context['source']) && $context['source']) {
            $handle = $context['source'];
        } else {
            $handle = 'log';
        }
        $entry = self::format_entry($timestamp, $level, $message, $context);
        return $this->add($entry, $handle);
    }
    protected static function format_entry($timestamp, $level, $message, $context)
    {
        if (isset($context['_legacy']) && true === $context['_legacy']) {
            if (isset($context['source']) && $context['source']) {
                $handle = $context['source'];
            } else {
                $handle = 'log';
            }
            $message = apply_filters('gplvault_logger_add_message', $message, $handle);
            $time = date_i18n('m-d-Y @ H:i:s');
            $entry = "{$time} - {$message}";
        } else {
            $entry = parent::format_entry($timestamp, $level, $message, $context);
        }
        return $entry;
    }
    protected function open($handle, $mode = 'a')
    {
        if ($this->is_open($handle)) {
            return true;
        }
        $file = self::get_log_file_path($handle);
        if ($file) {
            if (!file_exists($file)) {
                $temphandle = @fopen($file, 'w+');
                @fclose($temphandle);
                if (defined('FS_CHMOD_FILE')) {
                    @chmod($file, FS_CHMOD_FILE);
                }
            }
            $resource = @fopen($file, $mode);
            if ($resource) {
                $this->handles[$handle] = $resource;
                return true;
            }
        }
        return false;
    }
    protected function is_open($handle)
    {
        return array_key_exists($handle, $this->handles) && is_resource($this->handles[$handle]);
    }
    protected function close($handle)
    {
        $result = false;
        if ($this->is_open($handle)) {
            $result = fclose($this->handles[$handle]);
            unset($this->handles[$handle]);
        }
        return $result;
    }
    protected function add($entry, $handle)
    {
        $result = false;
        if ($this->should_rotate($handle)) {
            $this->log_rotate($handle);
        }
        if ($this->open($handle) && is_resource($this->handles[$handle])) {
            $result = fwrite($this->handles[$handle], $entry . PHP_EOL);
        } else {
            $this->cache_log($entry, $handle);
        }
        return false !== $result;
    }
    public function clear($handle)
    {
        $result = false;
        $this->close($handle);
        if ($this->open($handle, 'w') && is_resource($this->handles[$handle])) {
            $result = true;
        }
        do_action('woocommerce_log_clear', $handle);
        return $result;
    }
    public function remove($handle)
    {
        $removed = false;
        $logs = $this->get_log_files();
        $handle = sanitize_title($handle);
        if (isset($logs[$handle]) && $logs[$handle]) {
            $file = realpath(trailingslashit(GV_UPDATER_LOG_DIR) . $logs[$handle]);
            if (0 === stripos($file, realpath(trailingslashit(GV_UPDATER_LOG_DIR))) && is_file($file) && is_writable($file)) {
                $this->close($file);
                $removed = unlink($file);
            }
            do_action('woocommerce_log_remove', $handle, $removed);
        }
        return $removed;
    }
    protected function should_rotate($handle)
    {
        $file = self::get_log_file_path($handle);
        if ($file) {
            if ($this->is_open($handle)) {
                $file_stat = fstat($this->handles[$handle]);
                return $file_stat['size'] > $this->log_size_limit;
            } elseif (file_exists($file)) {
                return filesize($file) > $this->log_size_limit;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    protected function log_rotate($handle)
    {
        for ($i = 8; $i >= 0; $i--) {
            $this->increment_log_infix($handle, $i);
        }
        $this->increment_log_infix($handle);
    }
    protected function increment_log_infix($handle, $number = null)
    {
        if (null === $number) {
            $suffix = '';
            $next_suffix = '.0';
        } else {
            $suffix = '.' . $number;
            $next_suffix = '.' . ($number + 1);
        }
        $rename_from = self::get_log_file_path("{$handle}{$suffix}");
        $rename_to = self::get_log_file_path("{$handle}{$next_suffix}");
        if ($this->is_open($rename_from)) {
            $this->close($rename_from);
        }
        if (is_writable($rename_from)) {
            return rename($rename_from, $rename_to);
        }
        return false;
    }
    public static function get_log_file_path($handle)
    {
        if (function_exists('wp_hash')) {
            return trailingslashit(GV_UPDATER_LOG_DIR) . self::get_log_file_name($handle);
        } else {
            gv_doing_it_wrong(__METHOD__, __('This method should not be called before plugins_loaded.', 'gplvault'), '2.1.0');
            return false;
        }
    }
    public static function get_log_file_name($handle)
    {
        if (function_exists('wp_hash')) {
            $date_suffix = gmdate('Y-m-d', time());
            $hash_suffix = wp_hash($handle);
            return sanitize_file_name(implode('-', array($handle, $date_suffix, $hash_suffix)) . '.log');
        } else {
            gv_doing_it_wrong(__METHOD__, __('This method should not be called before plugins_loaded.', 'gplvault'), '2.1.0');
            return false;
        }
    }
    protected function cache_log($entry, $handle)
    {
        $this->cached_logs[] = array('entry' => $entry, 'handle' => $handle);
    }
    public function write_cached_logs()
    {
        foreach ($this->cached_logs as $log) {
            $this->add($log['entry'], $log['handle']);
        }
    }
    public static function delete_logs_before_timestamp($timestamp = 0)
    {
        if (!$timestamp) {
            return;
        }
        $log_files = self::get_log_files();
        foreach ($log_files as $log_file) {
            $last_modified = filemtime(trailingslashit(GV_UPDATER_LOG_DIR) . $log_file);
            if ($last_modified < $timestamp) {
                @unlink(trailingslashit(GV_UPDATER_LOG_DIR) . $log_file);
            }
        }
    }
    public static function get_log_files()
    {
        $files = @scandir(GV_UPDATER_LOG_DIR);
        $result = array();
        if (!empty($files)) {
            foreach ($files as $key => $value) {
                if (!in_array($value, array('.', '..'), true)) {
                    if (!is_dir($value) && strstr($value, '.log')) {
                        $result[sanitize_title($value)] = $value;
                    }
                }
            }
        }
        return $result;
    }
}