mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-25 18:19:22 +03:00
v1.2.12.435
This commit is contained in:
parent
513936e5f7
commit
93e620e3de
399 changed files with 765 additions and 687 deletions
149
rainloop/v/1.2.12.435/app/libraries/MailSo/Cache/CacheClient.php
Normal file
149
rainloop/v/1.2.12.435/app/libraries/MailSo/Cache/CacheClient.php
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
<?php
|
||||
|
||||
namespace MailSo\Cache;
|
||||
|
||||
/**
|
||||
* @category MailSo
|
||||
* @package Cache
|
||||
*/
|
||||
class CacheClient
|
||||
{
|
||||
/**
|
||||
* @var \MailSo\Cache\DriverInterface
|
||||
*/
|
||||
private $oDriver;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $sCacheIndex;
|
||||
|
||||
/**
|
||||
* @access private
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
$this->oDriver = null;
|
||||
$this->sCacheIndex = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \MailSo\Cache\CacheClient
|
||||
*/
|
||||
public static function NewInstance()
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
* @param string $sValue
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function Set($sKey, $sValue)
|
||||
{
|
||||
return $this->oDriver ? $this->oDriver->Set($sKey.$this->sCacheIndex, $sValue) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function SetTimer($sKey)
|
||||
{
|
||||
return $this->Set($sKey.'/TIMER', time());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Get($sKey)
|
||||
{
|
||||
$sValue = '';
|
||||
|
||||
if ($this->oDriver)
|
||||
{
|
||||
$sValue = $this->oDriver->Get($sKey.$this->sCacheIndex);
|
||||
}
|
||||
|
||||
return $sValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function GetTimer($sKey)
|
||||
{
|
||||
$iTimer = 0;
|
||||
$sValue = $this->Get($sKey.'/TIMER');
|
||||
if (0 < strlen($sValue) && is_numeric($sValue))
|
||||
{
|
||||
$iTimer = (int) $sValue;
|
||||
}
|
||||
|
||||
return $iTimer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return \MailSo\Cache\CacheClient
|
||||
*/
|
||||
public function Delete($sKey)
|
||||
{
|
||||
if ($this->oDriver)
|
||||
{
|
||||
$this->oDriver->Delete($sKey.$this->sCacheIndex);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \MailSo\Cache\DriverInterface $oDriver
|
||||
*
|
||||
* @return \MailSo\Cache\CacheClient
|
||||
*/
|
||||
public function SetDriver(\MailSo\Cache\DriverInterface $oDriver)
|
||||
{
|
||||
$this->oDriver = $oDriver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $iTimeToClearInHours = 24
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function GC($iTimeToClearInHours = 24)
|
||||
{
|
||||
return $this->oDriver ? $this->oDriver->GC($iTimeToClearInHours) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function IsInited()
|
||||
{
|
||||
return $this->oDriver instanceof \MailSo\Cache\DriverInterface;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sCacheIndex
|
||||
*
|
||||
* @return \MailSo\Cache\CacheClient
|
||||
*/
|
||||
public function SetCacheIndex($sCacheIndex)
|
||||
{
|
||||
$this->sCacheIndex = 0 < \strlen($sCacheIndex) ? "\x0".$sCacheIndex : '';
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace MailSo\Cache;
|
||||
|
||||
/**
|
||||
* @category MailSo
|
||||
* @package Cache
|
||||
*/
|
||||
interface DriverInterface
|
||||
{
|
||||
/**
|
||||
* @param string $sKey
|
||||
* @param string $sValue
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function Set($sKey, $sValue);
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Get($sKey);
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function Delete($sKey);
|
||||
|
||||
/**
|
||||
* @param int $iTimeToClearInHours = 24
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function GC($iTimeToClearInHours = 24);
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
namespace MailSo\Cache\Drivers;
|
||||
|
||||
/**
|
||||
* @category MailSo
|
||||
* @package Cache
|
||||
* @subpackage Drivers
|
||||
*/
|
||||
class APC implements \MailSo\Cache\DriverInterface
|
||||
{
|
||||
/**
|
||||
* @return \MailSo\Cache\Drivers\APC
|
||||
*/
|
||||
public static function NewInstance()
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
* @param string $sValue
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function Set($sKey, $sValue)
|
||||
{
|
||||
return \apc_store($this->generateCachedKey($sKey), (string) $sValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Get($sKey)
|
||||
{
|
||||
$sValue = \apc_fetch($this->generateCachedKey($sKey));
|
||||
return \is_string($sValue) ? $sValue : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function Delete($sKey)
|
||||
{
|
||||
\apc_delete($this->generateCachedKey($sKey));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $iTimeToClearInHours = 24
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function GC($iTimeToClearInHours = 24)
|
||||
{
|
||||
if (0 === $iTimeToClearInHours)
|
||||
{
|
||||
return \apc_clear_cache('user');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function generateCachedKey($sKey)
|
||||
{
|
||||
return \sha1($sKey);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
namespace MailSo\Cache\Drivers;
|
||||
|
||||
/**
|
||||
* @category MailSo
|
||||
* @package Cache
|
||||
* @subpackage Drivers
|
||||
*/
|
||||
class File implements \MailSo\Cache\DriverInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $sCacheFolder;
|
||||
|
||||
/**
|
||||
* @access private
|
||||
*
|
||||
* @param string $sCacheFolder
|
||||
*/
|
||||
private function __construct($sCacheFolder)
|
||||
{
|
||||
$this->sCacheFolder = $sCacheFolder;
|
||||
$this->sCacheFolder = rtrim(trim($this->sCacheFolder), '\\/').'/';
|
||||
if (!\is_dir($this->sCacheFolder))
|
||||
{
|
||||
@\mkdir($this->sCacheFolder, 0777);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sCacheFolder
|
||||
*
|
||||
* @return \MailSo\Cache\Drivers\File
|
||||
*/
|
||||
public static function NewInstance($sCacheFolder)
|
||||
{
|
||||
return new self($sCacheFolder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
* @param string $sValue
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function Set($sKey, $sValue)
|
||||
{
|
||||
return false !== \file_put_contents($sPath = $this->generateCachedFileName($sKey, true), $sValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Get($sKey)
|
||||
{
|
||||
$sValue = '';
|
||||
$sPath = $this->generateCachedFileName($sKey);
|
||||
if (\file_exists($sPath))
|
||||
{
|
||||
$sValue = \file_get_contents($sPath);
|
||||
}
|
||||
|
||||
return \is_string($sValue) ? $sValue : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function Delete($sKey)
|
||||
{
|
||||
$sPath = $this->generateCachedFileName($sKey);
|
||||
if (\file_exists($sPath))
|
||||
{
|
||||
\unlink($sPath);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $iTimeToClearInHours = 24
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function GC($iTimeToClearInHours = 24)
|
||||
{
|
||||
if (0 < $iTimeToClearInHours)
|
||||
{
|
||||
\MailSo\Base\Utils::RecTimeDirRemove($this->sCacheFolder, 60 * 60 * $iTimeToClearInHours, \time());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
* @param bool $bMkDir = false
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function generateCachedFileName($sKey, $bMkDir = false)
|
||||
{
|
||||
$sFilePath = '';
|
||||
if (3 < \strlen($sKey))
|
||||
{
|
||||
$sKeyPath = \sha1($sKey);
|
||||
$sKeyPath = \substr($sKeyPath, 0, 2).'/'.\substr($sKeyPath, 2, 2).'/'.$sKeyPath;
|
||||
|
||||
$sFilePath = $this->sCacheFolder.'/'.$sKeyPath;
|
||||
if ($bMkDir && !\is_dir(\dirname($sFilePath)))
|
||||
{
|
||||
if (!\mkdir(\dirname($sFilePath), 0777, true))
|
||||
{
|
||||
$sFilePath = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $sFilePath;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,120 @@
|
|||
<?php
|
||||
|
||||
namespace MailSo\Cache\Drivers;
|
||||
|
||||
/**
|
||||
* @category MailSo
|
||||
* @package Cache
|
||||
* @subpackage Drivers
|
||||
*/
|
||||
class Memcache implements \MailSo\Cache\DriverInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $sHost;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $iPost;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $iExpire;
|
||||
|
||||
/**
|
||||
* @var \Memcache|null
|
||||
*/
|
||||
private $oMem;
|
||||
|
||||
/**
|
||||
* @param string $sHost = '127.0.0.1'
|
||||
* @param int $iPost = 11211
|
||||
* @param int $iExpire = 43200
|
||||
*/
|
||||
private function __construct($sHost = '127.0.0.1', $iPost = 11211, $iExpire = 43200)
|
||||
{
|
||||
$this->sHost = $sHost;
|
||||
$this->iPost = $iPost;
|
||||
$this->iExpire = 0 < $iExpire ? $iExpire : 43200;
|
||||
|
||||
$this->oMem = new \Memcache();
|
||||
if (!$this->oMem->connect($this->sHost, $this->iPost))
|
||||
{
|
||||
$this->oMem = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sHost = '127.0.0.1'
|
||||
* @param int $iPost = 11211
|
||||
*
|
||||
* @return \MailSo\Cache\Drivers\APC
|
||||
*/
|
||||
public static function NewInstance($sHost = '127.0.0.1', $iPost = 11211)
|
||||
{
|
||||
return new self($sHost, $iPost);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
* @param string $sValue
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function Set($sKey, $sValue)
|
||||
{
|
||||
return $this->oMem ? $this->oMem->set($this->generateCachedKey($sKey), $sValue, 0, $this->iExpire) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function Get($sKey)
|
||||
{
|
||||
$sValue = $this->oMem ? $this->oMem->get($this->generateCachedKey($sKey)) : '';
|
||||
return \is_string($sValue) ? $sValue : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function Delete($sKey)
|
||||
{
|
||||
if ($this->oMem)
|
||||
{
|
||||
$this->oMem->delete($this->generateCachedKey($sKey));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $iTimeToClearInHours = 24
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function GC($iTimeToClearInHours = 24)
|
||||
{
|
||||
if (0 === $iTimeToClearInHours && $this->oMem)
|
||||
{
|
||||
return $this->oMem->flush();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sKey
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function generateCachedKey($sKey)
|
||||
{
|
||||
return \sha1($sKey);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue