mirror of https://github.com/MISP/MISP
new: [internal] Store browscap cache in apcu
parent
2ecbaa3bdc
commit
51640b0f3f
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
class ApcuCacheTool implements \Psr\SimpleCache\CacheInterface
|
||||
{
|
||||
/** @var string */
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* @param string $prefix
|
||||
*/
|
||||
public function __construct(string $prefix)
|
||||
{
|
||||
$this->prefix = $prefix;
|
||||
}
|
||||
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$success = false;
|
||||
$value = \apcu_fetch("$this->prefix:$key", $success);
|
||||
if ($success) {
|
||||
return $value;
|
||||
}
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function set($key, $value, $ttl = null)
|
||||
{
|
||||
return \apcu_store("$this->prefix:$key", $value, $ttl === null ? 0 : $ttl);
|
||||
}
|
||||
|
||||
public function delete($key)
|
||||
{
|
||||
return \apcu_delete("$this->prefix:$key");
|
||||
}
|
||||
|
||||
public function clear()
|
||||
{
|
||||
foreach (new APCUIterator("/^$this->prefix:/") as $item) {
|
||||
\apcu_delete($item['key']);
|
||||
}
|
||||
}
|
||||
|
||||
public function getMultiple($keys, $default = null)
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
yield $key => $this->get($key, $default);
|
||||
}
|
||||
}
|
||||
|
||||
public function setMultiple($values, $ttl = null)
|
||||
{
|
||||
foreach ($values as $key => $value) {
|
||||
$this->set($key, $value, $ttl);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function deleteMultiple($keys)
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
$this->delete($key);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function has($key)
|
||||
{
|
||||
return \apcu_exists("$this->prefix:$key");
|
||||
}
|
||||
}
|
|
@ -43,15 +43,29 @@ class UserLoginProfile extends AppModel
|
|||
|
||||
private $knownUserProfiles = [];
|
||||
|
||||
private function _buildBrowscapCache()
|
||||
private function browscapGetBrowser()
|
||||
{
|
||||
$this->log("Browscap - building new cache from browscap.ini file.", LOG_INFO);
|
||||
$fileCache = new \Doctrine\Common\Cache\FilesystemCache(UserLoginProfile::BROWSER_CACHE_DIR);
|
||||
$cache = new \Roave\DoctrineSimpleCache\SimpleCacheAdapter($fileCache);
|
||||
|
||||
$logger = new \Monolog\Logger('name');
|
||||
$bc = new \BrowscapPHP\BrowscapUpdater($cache, $logger);
|
||||
$bc->convertFile(UserLoginProfile::BROWSER_INI_FILE);
|
||||
|
||||
if (function_exists('apcu_fetch')) {
|
||||
App::uses('ApcuCacheTool', 'Tools');
|
||||
$cache = new ApcuCacheTool('misp:browscap');
|
||||
} else {
|
||||
$fileCache = new \Doctrine\Common\Cache\FilesystemCache(UserLoginProfile::BROWSER_CACHE_DIR);
|
||||
$cache = new \Roave\DoctrineSimpleCache\SimpleCacheAdapter($fileCache);
|
||||
}
|
||||
|
||||
try {
|
||||
$bc = new \BrowscapPHP\Browscap($cache, $logger);
|
||||
return $bc->getBrowser();
|
||||
} catch (\BrowscapPHP\Exception $e) {
|
||||
$this->log("Browscap - building new cache from browscap.ini file.", LOG_INFO);
|
||||
$bcUpdater = new \BrowscapPHP\BrowscapUpdater($cache, $logger);
|
||||
$bcUpdater->convertFile(UserLoginProfile::BROWSER_INI_FILE);
|
||||
}
|
||||
|
||||
$bc = new \BrowscapPHP\Browscap($cache, $logger);
|
||||
return $bc->getBrowser();
|
||||
}
|
||||
|
||||
public function beforeSave($options = [])
|
||||
|
@ -76,16 +90,7 @@ class UserLoginProfile extends AppModel
|
|||
if (!$this->userProfile) {
|
||||
// below uses https://github.com/browscap/browscap-php
|
||||
if (class_exists('\BrowscapPHP\Browscap')) {
|
||||
try {
|
||||
$fileCache = new \Doctrine\Common\Cache\FilesystemCache(UserLoginProfile::BROWSER_CACHE_DIR);
|
||||
$cache = new \Roave\DoctrineSimpleCache\SimpleCacheAdapter($fileCache);
|
||||
$logger = new \Monolog\Logger('name');
|
||||
$bc = new \BrowscapPHP\Browscap($cache, $logger);
|
||||
$browser = $bc->getBrowser();
|
||||
} catch (\BrowscapPHP\Exception $e) {
|
||||
$this->_buildBrowscapCache();
|
||||
return $this->_getUserProfile();
|
||||
}
|
||||
$browser = $this->browscapGetBrowser();
|
||||
} else {
|
||||
// a primitive OS & browser extraction capability
|
||||
$ua = $_SERVER['HTTP_USER_AGENT'] ?? null;
|
||||
|
|
Loading…
Reference in New Issue