Viewing File: /usr/local/cpanel/3rdparty/wpinstaller/src/WpDownloader/Log.php

<?php
namespace WpDownloader;

class Log
{
    public static $logDir;
    public static $logToEcho=true;
    public static $logToEchoLevel=6; // Logs with level lower than this will be echoed
    const EMERG   = 0;
    const ALERT   = 1;
    const CRIT    = 2;
    const ERR     = 3;
    const WARN    = 4;
    const NOTICE  = 5;
    const INFO    = 6;
    const DEBUG   = 7;
    public static $logLevels = [
        0 => 'EMERG',
        1 => 'ALERT',
        2 => 'CRIT',
        3 => 'ERR',
        4 => 'WARN',
        5 => 'NOTICE',
        6 => 'INFO',
        7 => 'DEBUG'
    ];
    public static $errorHandlerMap = array(
        E_NOTICE            => self::NOTICE,
        E_USER_NOTICE       => self::NOTICE,
        E_WARNING           => self::WARN,
        E_CORE_WARNING      => self::WARN,
        E_USER_WARNING      => self::WARN,
        E_ERROR             => self::ERR,
        E_USER_ERROR        => self::ERR,
        E_CORE_ERROR        => self::ERR,
        E_RECOVERABLE_ERROR => self::ERR,
        E_STRICT            => self::DEBUG,
        E_DEPRECATED        => self::DEBUG,
        E_USER_DEPRECATED   => self::DEBUG,
    );

    public static function write($msg, $level=7, $fileName='main')
    {
        $logDir = self::getLogDir();
        if ($logDir === false) {
            // Log directory doesn't exist; we can't do anything; it probably needs to live somewhere only root has access...
            return;
        }
        $logEntry = self::formatLogEntry($msg, $level);
        file_put_contents("{$logDir}/{$fileName}.log", $logEntry . PHP_EOL, FILE_APPEND | LOCK_EX);
        if (self::$logToEcho && $level <= self::$logToEchoLevel) {
            echo $logEntry . PHP_EOL;
        }
    }
    public static function formatLogEntry($msg, $level)
    {
        $levelString = self::getLogLevelString($level);
        $timestamp = self::getTimestamp();
        return "[{$timestamp}] : $levelString : ".print_r($msg, 1);
    }
    public static function getTimestamp()
    {
        return date("Y-m-d H:i:s T");
    }
    public static function getLogLevelString($level)
    {
        return self::$logLevels[$level];
    }
    public static function getLogDir()
    {
        if (isset(self::$logDir)) {
            return self::$logDir;
        }
        $logDir = dirname( dirname( dirname(__FILE__) ) ) . '/download_log';
        if (!is_dir($logDir) && !self::tryCreateDir($logDir)) {
            self::$logDir = false;
            $currentUser = get_current_user();
            $command = "[[ mkdir $logDir && chown $currentUser:$currentUser $logDir && chmod 0751 $logDir ]]";

            trigger_error("Tried to write to log but was unable to find the log directory. Run this command as root to create it: $command", E_USER_WARNING);
            return self::$logDir;
        }
        self::$logDir = $logDir;

        return self::$logDir;
    }
    public static function tryCreateDir($logDir)
    {
        $createResult = mkdir($logDir, 0751);
        if (!$createResult) {
            trigger_error("Failed to create directory {$logDir}.", E_USER_WARNING);
            return false;
        }

        $isCreated = is_dir($logDir);

        if (!$createResult) {
            trigger_error("mkdir({$logDir}) returned a true result but is_dir({$logDir}) did not. Unable to create log dir.", E_USER_WARNING);
            return false;
        }
        return $isCreated;
    }
    public static function catchFatalErrors()
    {
        register_shutdown_function(array(__CLASS__, 'fatalErrorShutdownHandler'));
    }
    // Hook into php's shutdown function so that we can send the benchmark data to the log on fatal errors.
    public static function fatalErrorShutdownHandler()
    {
        $last_error = error_get_last();
        if ($last_error['type'] === E_ERROR) {
            // fatal error
            self::write("{$last_error['message']} in {$last_error['file']} @ line {$last_error['line']}");
        }
    }
    public static function setLogToEcho($value, $level=7)
    {
        self::$logToEcho = (bool)$value;
        self::$logToEchoLevel = (int)$level;
    }

    public static function throwException($msg)
    {
        self::write($msg, self::CRIT);
        throw new \Exception($msg);
    }

    public static function logErrors($includingFatal=true)
    {
        set_error_handler(array(__CLASS__, 'errorHandler'));

        if ($includingFatal) {
            self::catchFatalErrors();
        }
    }

    public static function errorHandler($errno, $errstr, $file, $lineNumber, $errcontext="")
    {
        $errorLevel = error_reporting();
        if ($errorLevel & $errno) {
            if (isset(self::$errorHandlerMap[$errno])) {
                $priority = self::$errorHandlerMap[$errno];
            } else {
                $priority = self::INFO;
            }
            self::write("$errstr [in $file @ line $lineNumber]", $priority);
        }

        return false; // Let php still do its own error reporting.
    }
}
Back to Directory File Manager