Fix time_offset setting (#1241)

This commit is contained in:
RainLoop Team 2016-10-27 22:51:38 +03:00
parent 7e135b10ac
commit 022ed24c2a
6 changed files with 1345 additions and 1310 deletions

View file

@ -1,123 +1,154 @@
<?php <?php
/* /*
* This file is part of MailSo. * This file is part of MailSo.
* *
* (c) 2014 Usenko Timur * (c) 2014 Usenko Timur
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace MailSo\Base; namespace MailSo\Base;
/** /**
* @category MailSo * @category MailSo
* @package Base * @package Base
*/ */
class DateTimeHelper class DateTimeHelper
{ {
/** /**
* @access private * @access private
*/ */
private function __construct() private function __construct()
{ {
} }
/** /**
* @staticvar \DateTimeZone $oDateTimeZone * @staticvar \DateTimeZone $oDateTimeZone
* *
* @return \DateTimeZone * @return \DateTimeZone
*/ */
public static function GetUtcTimeZoneObject() public static function GetUtcTimeZoneObject()
{ {
static $oDateTimeZone = null; static $oDateTimeZone = null;
if (null === $oDateTimeZone) if (null === $oDateTimeZone)
{ {
$oDateTimeZone = new \DateTimeZone('UTC'); $oDateTimeZone = new \DateTimeZone('UTC');
} }
return $oDateTimeZone; return $oDateTimeZone;
} }
/** /**
* Parse date string formated as "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)" * Parse date string formated as "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)"
* RFC2822 * RFC2822
* *
* @param string $sDateTime * @param string $sDateTime
* *
* @return int * @return int
*/ */
public static function ParseRFC2822DateString($sDateTime) public static function ParseRFC2822DateString($sDateTime)
{ {
$sDateTime = \trim($sDateTime); $sDateTime = \trim($sDateTime);
if (empty($sDateTime)) if (empty($sDateTime))
{ {
return 0; return 0;
} }
$sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', $sDateTime)); $sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', $sDateTime));
$oDateTime = \DateTime::createFromFormat('D, d M Y H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject()); $oDateTime = \DateTime::createFromFormat('D, d M Y H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject());
return $oDateTime ? $oDateTime->getTimestamp() : 0; return $oDateTime ? $oDateTime->getTimestamp() : 0;
} }
/** /**
* Parse date string formated as "10-Jan-2012 01:58:17 -0800" * Parse date string formated as "10-Jan-2012 01:58:17 -0800"
* IMAP INTERNALDATE Format * IMAP INTERNALDATE Format
* *
* @param string $sDateTime * @param string $sDateTime
* *
* @return int * @return int
*/ */
public static function ParseInternalDateString($sDateTime) public static function ParseInternalDateString($sDateTime)
{ {
$sDateTime = \trim($sDateTime); $sDateTime = \trim($sDateTime);
if (empty($sDateTime)) if (empty($sDateTime))
{ {
return 0; return 0;
} }
if (\preg_match('/^[a-z]{2,4}, /i', $sDateTime)) // RFC2822 ~ "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)" if (\preg_match('/^[a-z]{2,4}, /i', $sDateTime)) // RFC2822 ~ "Thu, 10 Jun 2010 08:58:33 -0700 (PDT)"
{ {
return \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDateTime); return \MailSo\Base\DateTimeHelper::ParseRFC2822DateString($sDateTime);
} }
$oDateTime = \DateTime::createFromFormat('d-M-Y H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject()); $oDateTime = \DateTime::createFromFormat('d-M-Y H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject());
return $oDateTime ? $oDateTime->getTimestamp() : 0; return $oDateTime ? $oDateTime->getTimestamp() : 0;
} }
/** /**
* Parse date string formated as "2011-06-14 23:59:59 +0400" * Parse date string formated as "2011-06-14 23:59:59 +0400"
* *
* @param string $sDateTime * @param string $sDateTime
* *
* @return int * @return int
*/ */
public static function ParseDateStringType1($sDateTime) public static function ParseDateStringType1($sDateTime)
{ {
$sDateTime = \trim($sDateTime); $sDateTime = \trim($sDateTime);
if (empty($sDateTime)) if (empty($sDateTime))
{ {
return 0; return 0;
} }
$oDateTime = \DateTime::createFromFormat('Y-m-d H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject()); $oDateTime = \DateTime::createFromFormat('Y-m-d H:i:s O', $sDateTime, \MailSo\Base\DateTimeHelper::GetUtcTimeZoneObject());
return $oDateTime ? $oDateTime->getTimestamp() : 0; return $oDateTime ? $oDateTime->getTimestamp() : 0;
} }
/** /**
* Parse date string formated as "2015-05-08T14:32:18.483-07:00" * Parse date string formated as "2015-05-08T14:32:18.483-07:00"
* *
* @param string $sDateTime * @param string $sDateTime
* *
* @return int * @return int
*/ */
public static function TryToParseSpecEtagFormat($sDateTime) public static function TryToParseSpecEtagFormat($sDateTime)
{ {
$sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', \trim($sDateTime))); $sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', \trim($sDateTime)));
$sDateTime = \trim(\preg_replace('/(:[\d]{2})\.[\d]{3}/', '$1', \trim($sDateTime))); $sDateTime = \trim(\preg_replace('/(:[\d]{2})\.[\d]{3}/', '$1', \trim($sDateTime)));
$sDateTime = \trim(\preg_replace('/(-[\d]{2})T([\d]{2}:)/', '$1 $2', \trim($sDateTime))); $sDateTime = \trim(\preg_replace('/(-[\d]{2})T([\d]{2}:)/', '$1 $2', \trim($sDateTime)));
$sDateTime = \trim(\preg_replace('/([\-+][\d]{2}):([\d]{2})$/', ' $1$2', \trim($sDateTime))); $sDateTime = \trim(\preg_replace('/([\-+][\d]{2}):([\d]{2})$/', ' $1$2', \trim($sDateTime)));
return \MailSo\Base\DateTimeHelper::ParseDateStringType1($sDateTime); return \MailSo\Base\DateTimeHelper::ParseDateStringType1($sDateTime);
} }
}
/**
* @param string $sTime
*
* @return int
*/
public static function TimeToSec($sTime)
{
$iMod = 1;
$sTime = \trim($sTime);
if ('-' === \substr($sTime, 0, 1))
{
$iMod = -1;
$sTime = \substr($sTime, 1);
}
$aParts = \preg_split('/[:.,]/', (string) $sTime);
$iResult = 0;
if (isset($aParts[0]) && \is_numeric($aParts[0]))
{
$iResult += 3600 * ((int) $aParts[0]);
}
if (isset($aParts[1]) && \is_numeric($aParts[1]))
{
$iResult += 60 * ((int) $aParts[1]);
}
return $iResult * $iMod;
}
}

View file

@ -1,403 +1,408 @@
<?php <?php
/* /*
* This file is part of MailSo. * This file is part of MailSo.
* *
* (c) 2014 Usenko Timur * (c) 2014 Usenko Timur
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace MailSo\Log; namespace MailSo\Log;
/** /**
* @category MailSo * @category MailSo
* @package Log * @package Log
*/ */
abstract class Driver abstract class Driver
{ {
/** /**
* @var string * @var string
*/ */
protected $sDatePattern; protected $sDatePattern;
/** /**
* @var string * @var string
*/ */
protected $sName; protected $sName;
/** /**
* @var array * @var array
*/ */
protected $aPrefixes; protected $aPrefixes;
/** /**
* @var bool * @var bool
*/ */
protected $bGuidPrefix; protected $bGuidPrefix;
/** /**
* @var bool * @var string
*/ */
protected $bTimePrefix; protected $sTimeOffset;
/** /**
* @var bool * @var bool
*/ */
protected $bTypedPrefix; protected $bTimePrefix;
/** /**
* @var string * @var bool
*/ */
protected $sNewLine; protected $bTypedPrefix;
/** /**
* @var int * @var string
*/ */
private $iWriteOnTimeoutOnly; protected $sNewLine;
/** /**
* @var bool * @var int
*/ */
private $bWriteOnErrorOnly; private $iWriteOnTimeoutOnly;
/** /**
* @var bool * @var bool
*/ */
private $bWriteOnPhpErrorOnly; private $bWriteOnErrorOnly;
/** /**
* @var bool * @var bool
*/ */
private $bFlushCache; private $bWriteOnPhpErrorOnly;
/** /**
* @var array * @var bool
*/ */
private $aCache; private $bFlushCache;
/** /**
* @access protected * @var array
*/ */
protected function __construct() private $aCache;
{
$this->sDatePattern = 'H:i:s'; /**
$this->sName = 'INFO'; * @access protected
$this->sNewLine = "\r\n"; */
$this->bTimePrefix = true; protected function __construct()
$this->bTypedPrefix = true; {
$this->bGuidPrefix = true; $this->sDatePattern = 'H:i:s';
$this->sName = 'INFO';
$this->iTimeOffset = 0; $this->sNewLine = "\r\n";
$this->bTimePrefix = true;
$this->iWriteOnTimeoutOnly = 0; $this->bTypedPrefix = true;
$this->bWriteOnErrorOnly = false; $this->bGuidPrefix = true;
$this->bWriteOnPhpErrorOnly = false;
$this->bFlushCache = false; $this->sTimeOffset = '0';
$this->aCache = array();
$this->iWriteOnTimeoutOnly = 0;
$this->aPrefixes = array( $this->bWriteOnErrorOnly = false;
\MailSo\Log\Enumerations\Type::INFO => '[DATA]', $this->bWriteOnPhpErrorOnly = false;
\MailSo\Log\Enumerations\Type::SECURE => '[SECURE]', $this->bFlushCache = false;
\MailSo\Log\Enumerations\Type::NOTE => '[NOTE]', $this->aCache = array();
\MailSo\Log\Enumerations\Type::TIME => '[TIME]',
\MailSo\Log\Enumerations\Type::TIME_DELTA => '[TIME]', $this->aPrefixes = array(
\MailSo\Log\Enumerations\Type::MEMORY => '[MEMORY]', \MailSo\Log\Enumerations\Type::INFO => '[DATA]',
\MailSo\Log\Enumerations\Type::NOTICE => '[NOTICE]', \MailSo\Log\Enumerations\Type::SECURE => '[SECURE]',
\MailSo\Log\Enumerations\Type::WARNING => '[WARNING]', \MailSo\Log\Enumerations\Type::NOTE => '[NOTE]',
\MailSo\Log\Enumerations\Type::ERROR => '[ERROR]', \MailSo\Log\Enumerations\Type::TIME => '[TIME]',
\MailSo\Log\Enumerations\Type::TIME_DELTA => '[TIME]',
\MailSo\Log\Enumerations\Type::NOTICE_PHP => '[NOTICE]', \MailSo\Log\Enumerations\Type::MEMORY => '[MEMORY]',
\MailSo\Log\Enumerations\Type::WARNING_PHP => '[WARNING]', \MailSo\Log\Enumerations\Type::NOTICE => '[NOTICE]',
\MailSo\Log\Enumerations\Type::ERROR_PHP => '[ERROR]', \MailSo\Log\Enumerations\Type::WARNING => '[WARNING]',
); \MailSo\Log\Enumerations\Type::ERROR => '[ERROR]',
}
\MailSo\Log\Enumerations\Type::NOTICE_PHP => '[NOTICE]',
/** \MailSo\Log\Enumerations\Type::WARNING_PHP => '[WARNING]',
* @param int $iTimeOffset \MailSo\Log\Enumerations\Type::ERROR_PHP => '[ERROR]',
* );
* @return \MailSo\Log\Driver }
*/
public function SetTimeOffset($iTimeOffset) /**
{ * @param string $sTimeOffset
$this->iTimeOffset = $iTimeOffset; *
return $this; * @return \MailSo\Log\Driver
} */
public function SetTimeOffset($sTimeOffset)
/** {
* @return \MailSo\Log\Driver $this->sTimeOffset = (string) $sTimeOffset;
*/ return $this;
public function DisableGuidPrefix() }
{
$this->bGuidPrefix = false; /**
return $this; * @return \MailSo\Log\Driver
} */
public function DisableGuidPrefix()
/** {
* @return \MailSo\Log\Driver $this->bGuidPrefix = false;
*/ return $this;
public function DisableTimePrefix() }
{
$this->bTimePrefix = false; /**
return $this; * @return \MailSo\Log\Driver
} */
public function DisableTimePrefix()
/** {
* @param bool $bValue $this->bTimePrefix = false;
* return $this;
* @return \MailSo\Log\Driver }
*/
public function WriteOnErrorOnly($bValue) /**
{ * @param bool $bValue
$this->bWriteOnErrorOnly = !!$bValue; *
return $this; * @return \MailSo\Log\Driver
} */
public function WriteOnErrorOnly($bValue)
/** {
* @param bool $bValue $this->bWriteOnErrorOnly = !!$bValue;
* return $this;
* @return \MailSo\Log\Driver }
*/
public function WriteOnPhpErrorOnly($bValue) /**
{ * @param bool $bValue
$this->bWriteOnPhpErrorOnly = !!$bValue; *
return $this; * @return \MailSo\Log\Driver
} */
public function WriteOnPhpErrorOnly($bValue)
/** {
* @param int $iTimeout $this->bWriteOnPhpErrorOnly = !!$bValue;
* return $this;
* @return \MailSo\Log\Driver }
*/
public function WriteOnTimeoutOnly($iTimeout) /**
{ * @param int $iTimeout
$this->iWriteOnTimeoutOnly = (int) $iTimeout; *
if (0 > $this->iWriteOnTimeoutOnly) * @return \MailSo\Log\Driver
{ */
$this->iWriteOnTimeoutOnly = 0; public function WriteOnTimeoutOnly($iTimeout)
} {
$this->iWriteOnTimeoutOnly = (int) $iTimeout;
return $this; if (0 > $this->iWriteOnTimeoutOnly)
} {
$this->iWriteOnTimeoutOnly = 0;
/** }
* @return \MailSo\Log\Driver
*/ return $this;
public function DisableTypedPrefix() }
{
$this->bTypedPrefix = false; /**
return $this; * @return \MailSo\Log\Driver
} */
public function DisableTypedPrefix()
/** {
* @param string|array $mDesc $this->bTypedPrefix = false;
* @return bool return $this;
*/ }
abstract protected function writeImplementation($mDesc);
/**
/** * @param string|array $mDesc
* @return bool * @return bool
*/ */
protected function writeEmptyLineImplementation() abstract protected function writeImplementation($mDesc);
{
return $this->writeImplementation(''); /**
} * @return bool
*/
/** protected function writeEmptyLineImplementation()
* @param string $sTimePrefix {
* @param string $sDesc return $this->writeImplementation('');
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO }
* @param array $sName = ''
* /**
* @return string * @param string $sTimePrefix
*/ * @param string $sDesc
protected function loggerLineImplementation($sTimePrefix, $sDesc, * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
$iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '') * @param array $sName = ''
{ *
return \ltrim( * @return string
($this->bTimePrefix ? '['.$sTimePrefix.']' : ''). */
($this->bGuidPrefix ? '['.\MailSo\Log\Logger::Guid().']' : ''). protected function loggerLineImplementation($sTimePrefix, $sDesc,
($this->bTypedPrefix ? ' '.$this->getTypedPrefix($iType, $sName) : '') $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '')
).$sDesc; {
} return \ltrim(
($this->bTimePrefix ? '['.$sTimePrefix.']' : '').
/** ($this->bGuidPrefix ? '['.\MailSo\Log\Logger::Guid().']' : '').
* @return bool ($this->bTypedPrefix ? ' '.$this->getTypedPrefix($iType, $sName) : '')
*/ ).$sDesc;
protected function clearImplementation() }
{
return true; /**
} * @return bool
*/
/** protected function clearImplementation()
* @return string {
*/ return true;
protected function getTimeWithMicroSec() }
{
$aMicroTimeItems = \explode(' ', \microtime()); /**
return \MailSo\Log\Logger::DateHelper($this->sDatePattern, $this->iTimeOffset, $aMicroTimeItems[1]).'.'. * @return string
\str_pad((int) ($aMicroTimeItems[0] * 1000), 3, '0', STR_PAD_LEFT); */
} protected function getTimeWithMicroSec()
{
/** $aMicroTimeItems = \explode(' ', \microtime());
* @param int $iType return \MailSo\Log\Logger::DateHelper($this->sDatePattern, $this->sTimeOffset, $aMicroTimeItems[1]).'.'.
* @param string $sName = '' \str_pad((int) ($aMicroTimeItems[0] * 1000), 3, '0', STR_PAD_LEFT);
* }
* @return string
*/ /**
protected function getTypedPrefix($iType, $sName = '') * @param int $iType
{ * @param string $sName = ''
$sName = 0 < \strlen($sName) ? $sName : $this->sName; *
return isset($this->aPrefixes[$iType]) ? $sName.$this->aPrefixes[$iType].': ' : ''; * @return string
} */
protected function getTypedPrefix($iType, $sName = '')
/** {
* @param string|array $mDesc $sName = 0 < \strlen($sName) ? $sName : $this->sName;
* @param bool $bDiplayCrLf = false return isset($this->aPrefixes[$iType]) ? $sName.$this->aPrefixes[$iType].': ' : '';
* }
* @return string
*/ /**
protected function localWriteImplementation($mDesc, $bDiplayCrLf = false) * @param string|array $mDesc
{ * @param bool $bDiplayCrLf = false
if ($bDiplayCrLf) *
{ * @return string
if (\is_array($mDesc)) */
{ protected function localWriteImplementation($mDesc, $bDiplayCrLf = false)
foreach ($mDesc as &$sLine) {
{ if ($bDiplayCrLf)
$sLine = \strtr($sLine, array("\r" => '\r', "\n" => '\n'.$this->sNewLine)); {
$sLine = \rtrim($sLine); if (\is_array($mDesc))
} {
} foreach ($mDesc as &$sLine)
else {
{ $sLine = \strtr($sLine, array("\r" => '\r', "\n" => '\n'.$this->sNewLine));
$mDesc = \strtr($mDesc, array("\r" => '\r', "\n" => '\n'.$this->sNewLine)); $sLine = \rtrim($sLine);
$mDesc = \rtrim($mDesc); }
} }
} else
{
return $this->writeImplementation($mDesc); $mDesc = \strtr($mDesc, array("\r" => '\r', "\n" => '\n'.$this->sNewLine));
} $mDesc = \rtrim($mDesc);
}
/** }
* @final
* @param string $sDesc return $this->writeImplementation($mDesc);
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO }
* @param string $sName = ''
* @param bool $bDiplayCrLf = false /**
* * @final
* @return bool * @param string $sDesc
*/ * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', $bDiplayCrLf = false) * @param string $sName = ''
{ * @param bool $bDiplayCrLf = false
$bResult = true; *
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly)) * @return bool
{ */
$bErrorPhp = false; final public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', $bDiplayCrLf = false)
{
$bError = $this->bWriteOnErrorOnly && \in_array($iType, array( $bResult = true;
\MailSo\Log\Enumerations\Type::NOTICE, if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
\MailSo\Log\Enumerations\Type::NOTICE_PHP, {
\MailSo\Log\Enumerations\Type::WARNING, $bErrorPhp = false;
\MailSo\Log\Enumerations\Type::WARNING_PHP,
\MailSo\Log\Enumerations\Type::ERROR, $bError = $this->bWriteOnErrorOnly && \in_array($iType, array(
\MailSo\Log\Enumerations\Type::ERROR_PHP \MailSo\Log\Enumerations\Type::NOTICE,
)); \MailSo\Log\Enumerations\Type::NOTICE_PHP,
\MailSo\Log\Enumerations\Type::WARNING,
if (!$bError) \MailSo\Log\Enumerations\Type::WARNING_PHP,
{ \MailSo\Log\Enumerations\Type::ERROR,
$bErrorPhp = $this->bWriteOnPhpErrorOnly && \in_array($iType, array( \MailSo\Log\Enumerations\Type::ERROR_PHP
\MailSo\Log\Enumerations\Type::NOTICE_PHP, ));
\MailSo\Log\Enumerations\Type::WARNING_PHP,
\MailSo\Log\Enumerations\Type::ERROR_PHP if (!$bError)
)); {
} $bErrorPhp = $this->bWriteOnPhpErrorOnly && \in_array($iType, array(
\MailSo\Log\Enumerations\Type::NOTICE_PHP,
if ($bError || $bErrorPhp) \MailSo\Log\Enumerations\Type::WARNING_PHP,
{ \MailSo\Log\Enumerations\Type::ERROR_PHP
$sFlush = '--- FlushLogCache: '.($bError ? 'WriteOnErrorOnly' : 'WriteOnPhpErrorOnly'); ));
if (isset($this->aCache[0]) && empty($this->aCache[0])) }
{
$this->aCache[0] = $sFlush; if ($bError || $bErrorPhp)
\array_unshift($this->aCache, ''); {
} $sFlush = '--- FlushLogCache: '.($bError ? 'WriteOnErrorOnly' : 'WriteOnPhpErrorOnly');
else if (isset($this->aCache[0]) && empty($this->aCache[0]))
{ {
\array_unshift($this->aCache, $sFlush); $this->aCache[0] = $sFlush;
} \array_unshift($this->aCache, '');
}
$this->aCache[] = '--- FlushLogCache: Trigger'; else
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName); {
\array_unshift($this->aCache, $sFlush);
$this->bFlushCache = true; }
$bResult = $this->localWriteImplementation($this->aCache, $bDiplayCrLf);
$this->aCache = array(); $this->aCache[] = '--- FlushLogCache: Trigger';
} $this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
else if (0 < $this->iWriteOnTimeoutOnly && \time() - APP_START_TIME > $this->iWriteOnTimeoutOnly)
{ $this->bFlushCache = true;
$sFlush = '--- FlushLogCache: WriteOnTimeoutOnly ['.(\time() - APP_START_TIME).'sec]'; $bResult = $this->localWriteImplementation($this->aCache, $bDiplayCrLf);
if (isset($this->aCache[0]) && empty($this->aCache[0])) $this->aCache = array();
{ }
$this->aCache[0] = $sFlush; else if (0 < $this->iWriteOnTimeoutOnly && \time() - APP_START_TIME > $this->iWriteOnTimeoutOnly)
\array_unshift($this->aCache, ''); {
} $sFlush = '--- FlushLogCache: WriteOnTimeoutOnly ['.(\time() - APP_START_TIME).'sec]';
else if (isset($this->aCache[0]) && empty($this->aCache[0]))
{ {
\array_unshift($this->aCache, $sFlush); $this->aCache[0] = $sFlush;
} \array_unshift($this->aCache, '');
}
$this->aCache[] = '--- FlushLogCache: Trigger'; else
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName); {
\array_unshift($this->aCache, $sFlush);
$this->bFlushCache = true; }
$bResult = $this->localWriteImplementation($this->aCache, $bDiplayCrLf);
$this->aCache = array(); $this->aCache[] = '--- FlushLogCache: Trigger';
} $this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
else
{ $this->bFlushCache = true;
$this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName); $bResult = $this->localWriteImplementation($this->aCache, $bDiplayCrLf);
} $this->aCache = array();
} }
else else
{ {
$bResult = $this->localWriteImplementation( $this->aCache[] = $this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName);
$this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName), $bDiplayCrLf); }
} }
else
return $bResult; {
} $bResult = $this->localWriteImplementation(
$this->loggerLineImplementation($this->getTimeWithMicroSec(), $sDesc, $iType, $sName), $bDiplayCrLf);
/** }
* @return string
*/ return $bResult;
public function GetNewLine() }
{
return $this->sNewLine; /**
} * @return string
*/
/** public function GetNewLine()
* @final {
* @return bool return $this->sNewLine;
*/ }
final public function Clear()
{ /**
return $this->clearImplementation(); * @final
} * @return bool
*/
/** final public function Clear()
* @final {
* @return void return $this->clearImplementation();
*/ }
final public function WriteEmptyLine()
{ /**
if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly)) * @final
{ * @return void
$this->aCache[] = ''; */
} final public function WriteEmptyLine()
else {
{ if (!$this->bFlushCache && ($this->bWriteOnErrorOnly || $this->bWriteOnPhpErrorOnly || 0 < $this->iWriteOnTimeoutOnly))
$this->writeEmptyLineImplementation(); {
} $this->aCache[] = '';
} }
} else
{
$this->writeEmptyLineImplementation();
}
}
}

View file

@ -1,433 +1,431 @@
<?php <?php
/* /*
* This file is part of MailSo. * This file is part of MailSo.
* *
* (c) 2014 Usenko Timur * (c) 2014 Usenko Timur
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace MailSo\Log; namespace MailSo\Log;
/** /**
* @category MailSo * @category MailSo
* @package Log * @package Log
*/ */
class Logger extends \MailSo\Base\Collection class Logger extends \MailSo\Base\Collection
{ {
/** /**
* @var bool * @var bool
*/ */
private $bUsed; private $bUsed;
/** /**
* @var array * @var array
*/ */
private $aForbiddenTypes; private $aForbiddenTypes;
/** /**
* @var array * @var array
*/ */
private $aSecretWords; private $aSecretWords;
/** /**
* @var bool * @var bool
*/ */
private $bShowSecter; private $bShowSecter;
/** /**
* @var bool * @var bool
*/ */
private $bHideErrorNotices; private $bHideErrorNotices;
/** /**
* @access protected * @access protected
* *
* @param bool $bRegPhpErrorHandler = false * @param bool $bRegPhpErrorHandler = false
*/ */
protected function __construct($bRegPhpErrorHandler = true) protected function __construct($bRegPhpErrorHandler = true)
{ {
parent::__construct(); parent::__construct();
$this->bUsed = false; $this->bUsed = false;
$this->aForbiddenTypes = array(); $this->aForbiddenTypes = array();
$this->aSecretWords = array(); $this->aSecretWords = array();
$this->bShowSecter = false; $this->bShowSecter = false;
$this->bHideErrorNotices = false; $this->bHideErrorNotices = false;
if ($bRegPhpErrorHandler) if ($bRegPhpErrorHandler)
{ {
\set_error_handler(array(&$this, '__phpErrorHandler')); \set_error_handler(array(&$this, '__phpErrorHandler'));
} }
\register_shutdown_function(array(&$this, '__loggerShutDown')); \register_shutdown_function(array(&$this, '__loggerShutDown'));
} }
/** /**
* @param bool $bRegPhpErrorHandler = false * @param bool $bRegPhpErrorHandler = false
* *
* @return \MailSo\Log\Logger * @return \MailSo\Log\Logger
*/ */
public static function NewInstance($bRegPhpErrorHandler = false) public static function NewInstance($bRegPhpErrorHandler = false)
{ {
return new self($bRegPhpErrorHandler); return new self($bRegPhpErrorHandler);
} }
/** /**
* @staticvar \MailSo\Log\Logger $oInstance; * @staticvar \MailSo\Log\Logger $oInstance;
* *
* @return \MailSo\Log\Logger * @return \MailSo\Log\Logger
*/ */
public static function SingletonInstance() public static function SingletonInstance()
{ {
static $oInstance = null; static $oInstance = null;
if (null === $oInstance) if (null === $oInstance)
{ {
$oInstance = self::NewInstance(); $oInstance = self::NewInstance();
} }
return $oInstance; return $oInstance;
} }
/** /**
* @param string $sFormat * @param string $sFormat
* @param int $iTimeOffset = 0 * @param string $sTimeOffset = '0'
* @param int $iTimestamp = 0 * @param int $iTimestamp = 0
* *
* @return string * @return string
*/ */
public static function DateHelper($sFormat, $iTimeOffset = 0, $iTimestamp = null) public static function DateHelper($sFormat, $sTimeOffset = '0', $iTimestamp = null)
{ {
$iTimestamp = null === $iTimestamp ? \time() : (int) $iTimestamp; $iTimestamp = null === $iTimestamp ? \time() : (int) $iTimestamp;
$iTimeOffset = (int) $iTimeOffset; return \gmdate($sFormat, $iTimestamp + \MailSo\Base\DateTimeHelper::TimeToSec((string) $sTimeOffset));
}
return \gmdate($sFormat, $iTimestamp + $iTimeOffset * 3600);
} /**
* @return bool
/** */
* @return bool public static function IsSystemEnabled()
*/ {
public static function IsSystemEnabled() return !!(\MailSo\Config::$SystemLogger instanceof \MailSo\Log\Logger);
{ }
return !!(\MailSo\Config::$SystemLogger instanceof \MailSo\Log\Logger);
} /**
* @param mixed $mData
/** * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param mixed $mData */
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO public static function SystemLog($mData, $iType = \MailSo\Log\Enumerations\Type::INFO)
*/ {
public static function SystemLog($mData, $iType = \MailSo\Log\Enumerations\Type::INFO) if (\MailSo\Config::$SystemLogger instanceof \MailSo\Log\Logger)
{ {
if (\MailSo\Config::$SystemLogger instanceof \MailSo\Log\Logger) \MailSo\Config::$SystemLogger->WriteMixed($mData, $iType);
{ }
\MailSo\Config::$SystemLogger->WriteMixed($mData, $iType); }
}
} /**
* @staticvar string $sCache;
/** *
* @staticvar string $sCache; * @return string
* */
* @return string public static function Guid()
*/ {
public static function Guid() static $sCache = null;
{ if (null === $sCache)
static $sCache = null; {
if (null === $sCache) $sCache = \substr(\MailSo\Base\Utils::Md5Rand(), -8);
{ }
$sCache = \substr(\MailSo\Base\Utils::Md5Rand(), -8);
} return $sCache;
}
return $sCache;
} /**
* @return bool
/** */
* @return bool public function Ping()
*/ {
public function Ping() return true;
{ }
return true;
} /**
* @return bool
/** */
* @return bool public function IsEnabled()
*/ {
public function IsEnabled() return 0 < $this->Count();
{ }
return 0 < $this->Count();
} /**
* @param string $sWord
/** *
* @param string $sWord * @return bool
* */
* @return bool public function AddSecret($sWord)
*/ {
public function AddSecret($sWord) if (\is_string($sWord) && 0 < \strlen(\trim($sWord)))
{ {
if (\is_string($sWord) && 0 < \strlen(\trim($sWord))) $this->aSecretWords[] = $sWord;
{ $this->aSecretWords = \array_unique($this->aSecretWords);
$this->aSecretWords[] = $sWord; }
$this->aSecretWords = \array_unique($this->aSecretWords); }
}
} /**
* @param bool $bShow
/** *
* @param bool $bShow * @return \MailSo\Log\Logger
* */
* @return \MailSo\Log\Logger public function SetShowSecter($bShow)
*/ {
public function SetShowSecter($bShow) $this->bShowSecter = !!$bShow;
{ return $this;
$this->bShowSecter = !!$bShow; }
return $this;
} /**
* @param bool $bValue
/** *
* @param bool $bValue * @return \MailSo\Log\Logger
* */
* @return \MailSo\Log\Logger public function HideErrorNotices($bValue)
*/ {
public function HideErrorNotices($bValue) $this->bHideErrorNotices = !!$bValue;
{ return $this;
$this->bHideErrorNotices = !!$bValue; }
return $this;
} /**
* @return bool
/** */
* @return bool public function IsShowSecter()
*/ {
public function IsShowSecter() return $this->bShowSecter;
{ }
return $this->bShowSecter;
} /**
* @param int $iType
/** *
* @param int $iType * @return \MailSo\Log\Logger
* */
* @return \MailSo\Log\Logger public function AddForbiddenType($iType)
*/ {
public function AddForbiddenType($iType) $this->aForbiddenTypes[$iType] = true;
{
$this->aForbiddenTypes[$iType] = true; return $this;
}
return $this;
} /**
* @param int $iType
/** *
* @param int $iType * @return \MailSo\Log\Logger
* */
* @return \MailSo\Log\Logger public function RemoveForbiddenType($iType)
*/ {
public function RemoveForbiddenType($iType) $this->aForbiddenTypes[$iType] = false;
{ return $this;
$this->aForbiddenTypes[$iType] = false; }
return $this;
} /**
* @param int $iErrNo
/** * @param string $sErrStr
* @param int $iErrNo * @param string $sErrFile
* @param string $sErrStr * @param int $iErrLine
* @param string $sErrFile *
* @param int $iErrLine * @return bool
* */
* @return bool public function __phpErrorHandler($iErrNo, $sErrStr, $sErrFile, $iErrLine)
*/ {
public function __phpErrorHandler($iErrNo, $sErrStr, $sErrFile, $iErrLine) $iType = \MailSo\Log\Enumerations\Type::NOTICE_PHP;
{ switch ($iErrNo)
$iType = \MailSo\Log\Enumerations\Type::NOTICE_PHP; {
switch ($iErrNo) case E_USER_ERROR:
{ $iType = \MailSo\Log\Enumerations\Type::ERROR_PHP;
case E_USER_ERROR: break;
$iType = \MailSo\Log\Enumerations\Type::ERROR_PHP; case E_USER_WARNING:
break; $iType = \MailSo\Log\Enumerations\Type::WARNING_PHP;
case E_USER_WARNING: break;
$iType = \MailSo\Log\Enumerations\Type::WARNING_PHP; }
break;
} $this->Write($sErrFile.' [line:'.$iErrLine.', code:'.$iErrNo.']', $iType, 'PHP');
$this->Write('Error: '.$sErrStr, $iType, 'PHP');
$this->Write($sErrFile.' [line:'.$iErrLine.', code:'.$iErrNo.']', $iType, 'PHP');
$this->Write('Error: '.$sErrStr, $iType, 'PHP'); return !!(\MailSo\Log\Enumerations\Type::NOTICE === $iType && $this->bHideErrorNotices);
}
return !!(\MailSo\Log\Enumerations\Type::NOTICE === $iType && $this->bHideErrorNotices);
} /**
* @return void
/** */
* @return void public function __loggerShutDown()
*/ {
public function __loggerShutDown() if ($this->bUsed)
{ {
if ($this->bUsed) $aStatistic = \MailSo\Base\Loader::Statistic();
{ if (\is_array($aStatistic))
$aStatistic = \MailSo\Base\Loader::Statistic(); {
if (\is_array($aStatistic)) if (isset($aStatistic['php']['memory_get_peak_usage']))
{ {
if (isset($aStatistic['php']['memory_get_peak_usage'])) $this->Write('Memory peak usage: '.$aStatistic['php']['memory_get_peak_usage'],
{ \MailSo\Log\Enumerations\Type::MEMORY);
$this->Write('Memory peak usage: '.$aStatistic['php']['memory_get_peak_usage'], }
\MailSo\Log\Enumerations\Type::MEMORY);
} if (isset($aStatistic['time']))
{
if (isset($aStatistic['time'])) $this->Write('Time delta: '.$aStatistic['time'], \MailSo\Log\Enumerations\Type::TIME_DELTA);
{ }
$this->Write('Time delta: '.$aStatistic['time'], \MailSo\Log\Enumerations\Type::TIME_DELTA); }
} }
} }
}
} /**
* @return bool
/** */
* @return bool public function WriteEmptyLine()
*/ {
public function WriteEmptyLine() $iResult = 1;
{
$iResult = 1; $aLoggers =& $this->GetAsArray();
foreach ($aLoggers as /* @var $oLogger \MailSo\Log\Driver */ &$oLogger)
$aLoggers =& $this->GetAsArray(); {
foreach ($aLoggers as /* @var $oLogger \MailSo\Log\Driver */ &$oLogger) $iResult &= $oLogger->WriteEmptyLine();
{ }
$iResult &= $oLogger->WriteEmptyLine();
} return (bool) $iResult;
}
return (bool) $iResult;
} /**
* @param string $sDesc
/** * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param string $sDesc * @param string $sName = ''
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO * @param bool $bSearchSecretWords = true
* @param string $sName = '' * @param bool $bDiplayCrLf = false
* @param bool $bSearchSecretWords = true *
* @param bool $bDiplayCrLf = false * @return bool
* */
* @return bool public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO,
*/ $sName = '', $bSearchSecretWords = true, $bDiplayCrLf = false)
public function Write($sDesc, $iType = \MailSo\Log\Enumerations\Type::INFO, {
$sName = '', $bSearchSecretWords = true, $bDiplayCrLf = false) if (isset($this->aForbiddenTypes[$iType]) && true === $this->aForbiddenTypes[$iType])
{ {
if (isset($this->aForbiddenTypes[$iType]) && true === $this->aForbiddenTypes[$iType]) return true;
{ }
return true;
} $this->bUsed = true;
$this->bUsed = true; $oLogger = null;
$aLoggers = array();
$oLogger = null; $iResult = 1;
$aLoggers = array();
$iResult = 1; if ($bSearchSecretWords && !$this->bShowSecter && 0 < \count($this->aSecretWords))
{
if ($bSearchSecretWords && !$this->bShowSecter && 0 < \count($this->aSecretWords)) $sDesc = \str_replace($this->aSecretWords, '*******', $sDesc);
{ }
$sDesc = \str_replace($this->aSecretWords, '*******', $sDesc);
} $aLoggers =& $this->GetAsArray();
foreach ($aLoggers as /* @var $oLogger \MailSo\Log\Driver */ $oLogger)
$aLoggers =& $this->GetAsArray(); {
foreach ($aLoggers as /* @var $oLogger \MailSo\Log\Driver */ $oLogger) $iResult &= $oLogger->Write($sDesc, $iType, $sName, $bDiplayCrLf);
{ }
$iResult &= $oLogger->Write($sDesc, $iType, $sName, $bDiplayCrLf);
} return (bool) $iResult;
}
return (bool) $iResult;
} /**
* @param mixed $oValue
/** * @param int $iType = \MailSo\Log\Enumerations\Type::INFO
* @param mixed $oValue * @param string $sName = ''
* @param int $iType = \MailSo\Log\Enumerations\Type::INFO * @param bool $bSearchSecretWords = false
* @param string $sName = '' * @param bool $bDiplayCrLf = false
* @param bool $bSearchSecretWords = false *
* @param bool $bDiplayCrLf = false * @return bool
* */
* @return bool public function WriteDump($oValue, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '',
*/ $bSearchSecretWords = false, $bDiplayCrLf = false)
public function WriteDump($oValue, $iType = \MailSo\Log\Enumerations\Type::INFO, $sName = '', {
$bSearchSecretWords = false, $bDiplayCrLf = false) return $this->Write(\print_r($oValue, true), $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
{ }
return $this->Write(\print_r($oValue, true), $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
} /**
* @param \Exception $oException
/** * @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE
* @param \Exception $oException * @param string $sName = ''
* @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE * @param bool $bSearchSecretWords = true
* @param string $sName = '' * @param bool $bDiplayCrLf = false
* @param bool $bSearchSecretWords = true *
* @param bool $bDiplayCrLf = false * @return bool
* */
* @return bool public function WriteException($oException, $iType = \MailSo\Log\Enumerations\Type::NOTICE, $sName = '',
*/ $bSearchSecretWords = true, $bDiplayCrLf = false)
public function WriteException($oException, $iType = \MailSo\Log\Enumerations\Type::NOTICE, $sName = '', {
$bSearchSecretWords = true, $bDiplayCrLf = false) if ($oException instanceof \Exception)
{ {
if ($oException instanceof \Exception) if (isset($oException->__LOGINNED__))
{ {
if (isset($oException->__LOGINNED__)) return true;
{ }
return true;
} $oException->__LOGINNED__ = true;
$oException->__LOGINNED__ = true; return $this->Write((string) $oException, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
}
return $this->Write((string) $oException, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
} return false;
}
return false;
} /**
* @param \Exception $oException
/** * @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE
* @param \Exception $oException * @param string $sName = ''
* @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE * @param bool $bSearchSecretWords = true
* @param string $sName = '' * @param bool $bDiplayCrLf = false
* @param bool $bSearchSecretWords = true *
* @param bool $bDiplayCrLf = false * @return bool
* */
* @return bool public function WriteExceptionShort($oException, $iType = \MailSo\Log\Enumerations\Type::NOTICE, $sName = '',
*/ $bSearchSecretWords = true, $bDiplayCrLf = false)
public function WriteExceptionShort($oException, $iType = \MailSo\Log\Enumerations\Type::NOTICE, $sName = '', {
$bSearchSecretWords = true, $bDiplayCrLf = false) if ($oException instanceof \Exception)
{ {
if ($oException instanceof \Exception) if (isset($oException->__LOGINNED__))
{ {
if (isset($oException->__LOGINNED__)) return true;
{ }
return true;
} $oException->__LOGINNED__ = true;
$oException->__LOGINNED__ = true; return $this->Write($oException->getMessage(), $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
}
return $this->Write($oException->getMessage(), $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
} return false;
}
return false;
} /**
* @param mixed $mData
/** * @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE
* @param mixed $mData * @param string $sName = ''
* @param int $iType = \MailSo\Log\Enumerations\Type::NOTICE * @param bool $bSearchSecretWords = true
* @param string $sName = '' * @param bool $bDiplayCrLf = false
* @param bool $bSearchSecretWords = true *
* @param bool $bDiplayCrLf = false * @return bool
* */
* @return bool public function WriteMixed($mData, $iType = null, $sName = '',
*/ $bSearchSecretWords = true, $bDiplayCrLf = false)
public function WriteMixed($mData, $iType = null, $sName = '', {
$bSearchSecretWords = true, $bDiplayCrLf = false) $iType = null === $iType ? \MailSo\Log\Enumerations\Type::INFO : $iType;
{ if (\is_array($mData) || \is_object($mData))
$iType = null === $iType ? \MailSo\Log\Enumerations\Type::INFO : $iType; {
if (\is_array($mData) || \is_object($mData)) if ($mData instanceof \Exception)
{ {
if ($mData instanceof \Exception) $iType = null === $iType ? \MailSo\Log\Enumerations\Type::NOTICE : $iType;
{ return $this->WriteException($mData, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
$iType = null === $iType ? \MailSo\Log\Enumerations\Type::NOTICE : $iType; }
return $this->WriteException($mData, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf); else
} {
else return $this->WriteDump($mData, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
{ }
return $this->WriteDump($mData, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf); }
} else
} {
else return $this->Write($mData, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
{ }
return $this->Write($mData, $iType, $sName, $bSearchSecretWords, $bDiplayCrLf);
} return false;
}
return false; }
}
}

View file

@ -430,9 +430,9 @@ class Actions
if (false !== \strpos($sLine, '{date:')) if (false !== \strpos($sLine, '{date:'))
{ {
$iTimeOffset = (int) $this->Config()->Get('logs', 'time_offset', 0); $sTimeOffset = (string) $this->Config()->Get('logs', 'time_offset', '0');
$sLine = \preg_replace_callback('/\{date:([^}]+)\}/', function ($aMatch) use ($iTimeOffset, $bUrlEncode) { $sLine = \preg_replace_callback('/\{date:([^}]+)\}/', function ($aMatch) use ($sTimeOffset, $bUrlEncode) {
return \RainLoop\Utils::UrlEncode(\MailSo\Log\Logger::DateHelper($aMatch[1], $iTimeOffset), $bUrlEncode); return \RainLoop\Utils::UrlEncode(\MailSo\Log\Logger::DateHelper($aMatch[1], $sTimeOffset), $bUrlEncode);
}, $sLine); }, $sLine);
$aClear['/\{date:([^}]*)\}/'] = 'date'; $aClear['/\{date:([^}]*)\}/'] = 'date';
@ -1073,7 +1073,7 @@ class Actions
} }
} }
$iTimeOffset = (int) $this->Config()->Get('logs', 'time_offset', 0); $sTimeOffset = (string) $this->Config()->Get('logs', 'time_offset', '0');
$this->oLogger->SetShowSecter(!$this->Config()->Get('logs', 'hide_passwords', true)); $this->oLogger->SetShowSecter(!$this->Config()->Get('logs', 'hide_passwords', true));
@ -1101,7 +1101,7 @@ class Actions
->WriteOnErrorOnly($this->Config()->Get('logs', 'write_on_error_only', false)) ->WriteOnErrorOnly($this->Config()->Get('logs', 'write_on_error_only', false))
->WriteOnPhpErrorOnly($this->Config()->Get('logs', 'write_on_php_error_only', false)) ->WriteOnPhpErrorOnly($this->Config()->Get('logs', 'write_on_php_error_only', false))
->WriteOnTimeoutOnly($this->Config()->Get('logs', 'write_on_timeout_only', 0)) ->WriteOnTimeoutOnly($this->Config()->Get('logs', 'write_on_timeout_only', 0))
->SetTimeOffset($iTimeOffset) ->SetTimeOffset($sTimeOffset)
); );
if (!$this->Config()->Get('debug', 'enable', false)) if (!$this->Config()->Get('debug', 'enable', false))
@ -1113,9 +1113,9 @@ class Actions
$oHttp = $this->Http(); $oHttp = $this->Http();
$this->oLogger->Write('[DATE:'.\MailSo\Log\Logger::DateHelper('d.m.y', $iTimeOffset). $this->oLogger->Write('[DATE:'.\MailSo\Log\Logger::DateHelper('d.m.y', $sTimeOffset).
(0 !== $iTimeOffset ? '][OFFSET:'.(0 < $iTimeOffset ? '+' : '-'). (0 !== $sTimeOffset ? '][OFFSET:'.(0 < $sTimeOffset ? '+' : '-').
\str_pad((string) \abs($iTimeOffset), 2, '0', STR_PAD_LEFT) : ''). \str_pad((string) \abs($sTimeOffset), 2, '0', STR_PAD_LEFT) : '').
'][RL:'.APP_VERSION.'][PHP:'.PHP_VERSION.'][IP:'. '][RL:'.APP_VERSION.'][PHP:'.PHP_VERSION.'][IP:'.
$oHttp->GetClientIp($this->Config()->Get('labs', 'http_client_ip_check_proxy', false)).'][PID:'. $oHttp->GetClientIp($this->Config()->Get('labs', 'http_client_ip_check_proxy', false)).'][PID:'.
(\MailSo\Base\Utils::FunctionExistsAndEnabled('getmypid') ? \getmypid() : 'unknown').']['. (\MailSo\Base\Utils::FunctionExistsAndEnabled('getmypid') ? \getmypid() : 'unknown').']['.

View file

@ -1,342 +1,343 @@
<?php <?php
namespace RainLoop\Config; namespace RainLoop\Config;
abstract class AbstractConfig abstract class AbstractConfig
{ {
/** /**
* @var string * @var string
*/ */
private $sFile; private $sFile;
/** /**
* @var string * @var string
*/ */
private $sAdditionalFile; private $sAdditionalFile;
/** /**
* @var array * @var array
*/ */
private $aData; private $aData;
/** /**
* @var bool * @var bool
*/ */
private $bUseApcCache; private $bUseApcCache;
/** /**
* @var string * @var string
*/ */
private $sFileHeader; private $sFileHeader;
/** /**
* @param string $sFileName * @param string $sFileName
* @param string $sFileHeader = '' * @param string $sFileHeader = ''
* @param string $sAdditionalFileName = '' * @param string $sAdditionalFileName = ''
* *
* @return void * @return void
*/ */
public function __construct($sFileName, $sFileHeader = '', $sAdditionalFileName = '') public function __construct($sFileName, $sFileHeader = '', $sAdditionalFileName = '')
{ {
$this->sFile = \APP_PRIVATE_DATA.'configs/'.\trim($sFileName); $this->sFile = \APP_PRIVATE_DATA.'configs/'.\trim($sFileName);
$sAdditionalFileName = \trim($sAdditionalFileName); $sAdditionalFileName = \trim($sAdditionalFileName);
$this->sAdditionalFile = \APP_PRIVATE_DATA.'configs/'.$sAdditionalFileName; $this->sAdditionalFile = \APP_PRIVATE_DATA.'configs/'.$sAdditionalFileName;
$this->sAdditionalFile = 0 < \strlen($sAdditionalFileName) && $this->sAdditionalFile = 0 < \strlen($sAdditionalFileName) &&
\file_exists($this->sAdditionalFile) ? $this->sAdditionalFile : ''; \file_exists($this->sAdditionalFile) ? $this->sAdditionalFile : '';
$this->sFileHeader = $sFileHeader; $this->sFileHeader = $sFileHeader;
$this->aData = $this->defaultValues(); $this->aData = $this->defaultValues();
$this->bUseApcCache = APP_USE_APC_CACHE && $this->bUseApcCache = APP_USE_APC_CACHE &&
\MailSo\Base\Utils::FunctionExistsAndEnabled(array('apc_fetch', 'apc_store')); \MailSo\Base\Utils::FunctionExistsAndEnabled(array('apc_fetch', 'apc_store'));
} }
/** /**
* @return array * @return array
*/ */
protected abstract function defaultValues(); protected abstract function defaultValues();
/** /**
* @return bool * @return bool
*/ */
public function IsInited() public function IsInited()
{ {
return \is_array($this->aData) && 0 < \count($this->aData); return \is_array($this->aData) && 0 < \count($this->aData);
} }
/** /**
* @param string $sSection * @param string $sSection
* @param string $sName * @param string $sName
* @param mixed $mDefault = null * @param mixed $mDefault = null
* *
* @return mixed * @return mixed
*/ */
public function Get($sSection, $sName, $mDefault = null) public function Get($sSection, $sName, $mDefault = null)
{ {
$mResult = $mDefault; $mResult = $mDefault;
if (isset($this->aData[$sSection][$sName][0])) if (isset($this->aData[$sSection][$sName][0]))
{ {
$mResult = $this->aData[$sSection][$sName][0]; $mResult = $this->aData[$sSection][$sName][0];
} }
return $mResult; return $mResult;
} }
/** /**
* @param string $sSectionKey * @param string $sSectionKey
* @param string $sParamKey * @param string $sParamKey
* @param mixed $mParamValue * @param mixed $mParamValue
* *
* @return void * @return void
*/ */
public function Set($sSectionKey, $sParamKey, $mParamValue) public function Set($sSectionKey, $sParamKey, $mParamValue)
{ {
if (isset($this->aData[$sSectionKey][$sParamKey][0])) if (isset($this->aData[$sSectionKey][$sParamKey][0]))
{ {
$sType = \gettype($this->aData[$sSectionKey][$sParamKey][0]); $sType = \gettype($this->aData[$sSectionKey][$sParamKey][0]);
switch ($sType) switch ($sType)
{ {
default: default:
case 'string': case 'float':
$this->aData[$sSectionKey][$sParamKey][0] = (string) $mParamValue; case 'string':
break; $this->aData[$sSectionKey][$sParamKey][0] = (string) $mParamValue;
case 'int': break;
case 'integer': case 'int':
$this->aData[$sSectionKey][$sParamKey][0] = (int) $mParamValue; case 'integer':
break; $this->aData[$sSectionKey][$sParamKey][0] = (int) $mParamValue;
case 'bool': break;
case 'boolean': case 'bool':
$this->aData[$sSectionKey][$sParamKey][0] = (bool) $mParamValue; case 'boolean':
break; $this->aData[$sSectionKey][$sParamKey][0] = (bool) $mParamValue;
} break;
} }
else if ('custom' === $sSectionKey) }
{ else if ('custom' === $sSectionKey)
$this->aData[$sSectionKey][$sParamKey] = array((string) $mParamValue); {
} $this->aData[$sSectionKey][$sParamKey] = array((string) $mParamValue);
} }
}
/**
* @return string /**
*/ * @return string
private function cacheKey() */
{ private function cacheKey()
return 'config:'.\sha1($this->sFile).':'.\sha1($this->sAdditionalFile).':'; {
} return 'config:'.\sha1($this->sFile).':'.\sha1($this->sAdditionalFile).':';
}
/**
* @return bool /**
*/ * @return bool
private function loadDataFromCache() */
{ private function loadDataFromCache()
if ($this->bUseApcCache) {
{ if ($this->bUseApcCache)
$iMTime = @\filemtime($this->sFile); {
$iMTime = \is_int($iMTime) && 0 < $iMTime ? $iMTime : 0; $iMTime = @\filemtime($this->sFile);
$iMTime = \is_int($iMTime) && 0 < $iMTime ? $iMTime : 0;
$iATime = $this->sAdditionalFile ? @\filemtime($this->sAdditionalFile) : 0;
$iATime = \is_int($iATime) && 0 < $iATime ? $iATime : 0; $iATime = $this->sAdditionalFile ? @\filemtime($this->sAdditionalFile) : 0;
$iATime = \is_int($iATime) && 0 < $iATime ? $iATime : 0;
if (0 < $iMTime)
{ if (0 < $iMTime)
$sKey = $this->cacheKey(); {
$sKey = $this->cacheKey();
$sTimeHash = \apc_fetch($sKey.'time');
if ($sTimeHash && $sTimeHash === \md5($iMTime.'/'.$iATime)) $sTimeHash = \apc_fetch($sKey.'time');
{ if ($sTimeHash && $sTimeHash === \md5($iMTime.'/'.$iATime))
$aFetchData = \apc_fetch($sKey.'data'); {
if (\is_array($aFetchData)) $aFetchData = \apc_fetch($sKey.'data');
{ if (\is_array($aFetchData))
$this->aData = $aFetchData; {
return true; $this->aData = $aFetchData;
} return true;
} }
} }
} }
}
return false;
} return false;
}
/**
* @return bool /**
*/ * @return bool
private function storeDataToCache() */
{ private function storeDataToCache()
if ($this->bUseApcCache) {
{ if ($this->bUseApcCache)
$iMTime = @\filemtime($this->sFile); {
$iMTime = \is_int($iMTime) && 0 < $iMTime ? $iMTime : 0; $iMTime = @\filemtime($this->sFile);
$iMTime = \is_int($iMTime) && 0 < $iMTime ? $iMTime : 0;
$iATime = $this->sAdditionalFile ? @\filemtime($this->sAdditionalFile) : 0;
$iATime = \is_int($iATime) && 0 < $iATime ? $iATime : 0; $iATime = $this->sAdditionalFile ? @\filemtime($this->sAdditionalFile) : 0;
$iATime = \is_int($iATime) && 0 < $iATime ? $iATime : 0;
if (0 < $iMTime)
{ if (0 < $iMTime)
$sKey = $this->cacheKey(); {
$sKey = $this->cacheKey();
\apc_store($sKey.'time', \md5($iMTime.'/'.$iATime));
\apc_store($sKey.'data', $this->aData); \apc_store($sKey.'time', \md5($iMTime.'/'.$iATime));
\apc_store($sKey.'data', $this->aData);
return true;
} return true;
} }
}
return false;
} return false;
}
/**
* @return bool /**
*/ * @return bool
private function clearCache() */
{ private function clearCache()
if ($this->bUseApcCache) {
{ if ($this->bUseApcCache)
$sKey = $this->cacheKey(); {
$sKey = $this->cacheKey();
\apc_delete($sKey.'time');
\apc_delete($sKey.'data'); \apc_delete($sKey.'time');
\apc_delete($sKey.'data');
return true;
} return true;
}
return false;
} return false;
}
/**
* @return bool /**
*/ * @return bool
public function IsFileExists() */
{ public function IsFileExists()
return \file_exists($this->sFile); {
} return \file_exists($this->sFile);
}
/**
* @return bool /**
*/ * @return bool
public function Load() */
{ public function Load()
if (\file_exists($this->sFile) && \is_readable($this->sFile)) {
{ if (\file_exists($this->sFile) && \is_readable($this->sFile))
if ($this->loadDataFromCache()) {
{ if ($this->loadDataFromCache())
return true; {
} return true;
}
$aData = \RainLoop\Utils::CustomParseIniFile($this->sFile, true);
if (\is_array($aData) && 0 < \count($aData)) $aData = \RainLoop\Utils::CustomParseIniFile($this->sFile, true);
{ if (\is_array($aData) && 0 < \count($aData))
foreach ($aData as $sSectionKey => $aSectionValue) {
{ foreach ($aData as $sSectionKey => $aSectionValue)
if (\is_array($aSectionValue)) {
{ if (\is_array($aSectionValue))
foreach ($aSectionValue as $sParamKey => $mParamValue) {
{ foreach ($aSectionValue as $sParamKey => $mParamValue)
$this->Set($sSectionKey, $sParamKey, $mParamValue); {
} $this->Set($sSectionKey, $sParamKey, $mParamValue);
} }
} }
}
unset($aData);
unset($aData);
if (\file_exists($this->sAdditionalFile) && \is_readable($this->sAdditionalFile))
{ if (\file_exists($this->sAdditionalFile) && \is_readable($this->sAdditionalFile))
$aSubData = \RainLoop\Utils::CustomParseIniFile($this->sAdditionalFile, true); {
if (\is_array($aSubData) && 0 < \count($aSubData)) $aSubData = \RainLoop\Utils::CustomParseIniFile($this->sAdditionalFile, true);
{ if (\is_array($aSubData) && 0 < \count($aSubData))
foreach ($aSubData as $sSectionKey => $aSectionValue) {
{ foreach ($aSubData as $sSectionKey => $aSectionValue)
if (\is_array($aSectionValue)) {
{ if (\is_array($aSectionValue))
foreach ($aSectionValue as $sParamKey => $mParamValue) {
{ foreach ($aSectionValue as $sParamKey => $mParamValue)
$this->Set($sSectionKey, $sParamKey, $mParamValue); {
} $this->Set($sSectionKey, $sParamKey, $mParamValue);
} }
} }
} }
}
unset($aSubData);
} unset($aSubData);
}
$this->storeDataToCache();
$this->storeDataToCache();
return true;
} return true;
} }
}
return false;
} return false;
}
/**
* @return bool /**
*/ * @return bool
public function Save() */
{ public function Save()
if (\file_exists($this->sFile) && !\is_writable($this->sFile)) {
{ if (\file_exists($this->sFile) && !\is_writable($this->sFile))
return false; {
} return false;
}
$sNewLine = "\n";
$sNewLine = "\n";
$aResultLines = array();
$aResultLines = array();
foreach ($this->aData as $sSectionKey => $aSectionValue)
{ foreach ($this->aData as $sSectionKey => $aSectionValue)
if (\is_array($aSectionValue)) {
{ if (\is_array($aSectionValue))
$aResultLines[] = ''; {
$aResultLines[] = '['.$sSectionKey.']'; $aResultLines[] = '';
$bFirst = true; $aResultLines[] = '['.$sSectionKey.']';
$bFirst = true;
foreach ($aSectionValue as $sParamKey => $mParamValue)
{ foreach ($aSectionValue as $sParamKey => $mParamValue)
if (\is_array($mParamValue)) {
{ if (\is_array($mParamValue))
if (!empty($mParamValue[1])) {
{ if (!empty($mParamValue[1]))
$sDesk = \str_replace("\r", '', $mParamValue[1]); {
$aDesk = \explode("\n", $sDesk); $sDesk = \str_replace("\r", '', $mParamValue[1]);
$aDesk = \array_map(function (&$sLine) { $aDesk = \explode("\n", $sDesk);
return '; '.$sLine; $aDesk = \array_map(function (&$sLine) {
}, $aDesk); return '; '.$sLine;
}, $aDesk);
if (!$bFirst)
{ if (!$bFirst)
$aResultLines[] = ''; {
} $aResultLines[] = '';
}
$aResultLines[] = \implode($sNewLine, $aDesk);
} $aResultLines[] = \implode($sNewLine, $aDesk);
}
$bFirst = false;
$bFirst = false;
$sValue = '""';
switch (\gettype($mParamValue[0])) $sValue = '""';
{ switch (\gettype($mParamValue[0]))
default: {
case 'string': default:
$sValue = '"'.\str_replace('"', '\"', $mParamValue[0]).'"'; case 'string':
break; $sValue = '"'.\str_replace('"', '\"', $mParamValue[0]).'"';
case 'int': break;
case 'integer': case 'int':
$sValue = $mParamValue[0]; case 'integer':
break; $sValue = $mParamValue[0];
case 'bool': break;
case 'boolean': case 'bool':
$sValue = $mParamValue[0] ? 'On' : 'Off'; case 'boolean':
break; $sValue = $mParamValue[0] ? 'On' : 'Off';
} break;
}
$aResultLines[] = $sParamKey.' = '.$sValue;
} $aResultLines[] = $sParamKey.' = '.$sValue;
} }
} }
} }
}
$this->clearCache();
return false !== \file_put_contents($this->sFile, $this->clearCache();
(0 < \strlen($this->sFileHeader) ? $this->sFileHeader : ''). return false !== \file_put_contents($this->sFile,
$sNewLine.\implode($sNewLine, $aResultLines)); (0 < \strlen($this->sFileHeader) ? $this->sFileHeader : '').
} $sNewLine.\implode($sNewLine, $aResultLines));
} }
}

View file

@ -293,7 +293,7 @@ Values:
'hide_passwords' => array(true, 'Required for development purposes only. 'hide_passwords' => array(true, 'Required for development purposes only.
Disabling this option is not recommended.'), Disabling this option is not recommended.'),
'time_offset' => array(0), 'time_offset' => array('0'),
'session_filter' => array(''), 'session_filter' => array(''),
'filename' => array('log-{date:Y-m-d}.txt', 'filename' => array('log-{date:Y-m-d}.txt',