Skip to content

Commit 1cbb013

Browse files
committed
Refactoring.
1 parent 400a8e7 commit 1cbb013

File tree

3 files changed

+44
-213
lines changed

3 files changed

+44
-213
lines changed

assets/config.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# License: GNU/GPLv2
88
# @see LICENSE.txt
99
#
10-
# This file: Configuration defaults file (last modified: 2020.06.22).
10+
# This file: Configuration defaults file (last modified: 2020.06.23).
1111
##/
1212

1313
core:
@@ -161,7 +161,7 @@ core:
161161
vi: "Tiếng Việt"
162162
zh: "中文(简体)"
163163
zh-tw: "中文(傳統)"
164-
choice_filter: "FilterLang"
164+
choice_filter: "filterL10N"
165165
lang_override:
166166
type: "bool"
167167
default: true
@@ -190,7 +190,7 @@ core:
190190
signatures:
191191
active:
192192
type: "string"
193-
default: ""
193+
default: "clamav.cedb,clamav.db,clamav.fdb,clamav.hdb,clamav.htdb,clamav.mdb,clamav.ndb,clamav_elf.db,clamav_elf_regex.db,clamav_email.db,clamav_email_regex.db,clamav_exe.db,clamav_exe_regex.db,clamav_graphics.db,clamav_graphics_regex.db,clamav_java.db,clamav_java_regex.db,clamav_macho.db,clamav_macho_regex.db,clamav_ole.db,clamav_ole_regex.db,clamav_pdf.db,clamav_pdf_regex.db,clamav_regex.db,clamav_regex.htdb,clamav_regex.ndb,clamav_swf.db,clamav_swf_regex.db,hex_general_commands.csv,phpmussel.cedb,phpmussel.db,phpmussel.fdb,phpmussel.hdb,phpmussel.htdb,phpmussel.mdb,phpmussel.medb,phpmussel.ndb,phpmussel.udb,phpmussel_elf.db,phpmussel_email.db,phpmussel_email_regex.db,phpmussel_exe.db,phpmussel_exe_regex.db,phpmussel_graphics.db,phpmussel_graphics_regex.db,phpmussel_ole.db,phpmussel_ole_regex.db,phpmussel_pdf.db,phpmussel_regex.db,phpmussel_regex.htdb,phpmussel_regex.ndb,phpmussel_swf.db,phpmussel_swf_regex.db"
194194
value_preg_filter:
195195
"~\s~": ""
196196
fail_silently:
@@ -377,7 +377,7 @@ legal:
377377
supplementary_cache_options:
378378
enable_apcu:
379379
type: "bool"
380-
default: false
380+
default: true
381381
enable_memcached:
382382
type: "bool"
383383
default: false

src/Loader.php

Lines changed: 13 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: GNU/GPLv2
99
* @see LICENSE.txt
1010
*
11-
* This file: The loader (last modified: 2020.06.22).
11+
* This file: The loader (last modified: 2020.06.27).
1212
*/
1313

1414
namespace phpMussel\Core;
@@ -174,7 +174,8 @@ class Loader
174174
* @param string $ConfigurationPath Custom-defined path to phpMussel's
175175
* configuration file (optional).
176176
* @param string $CachePath An optional, custom-defined path to phpMussel's
177-
* cache data.
177+
* cache data (this is also where files may be stored temporarily when
178+
* it's needed).
178179
* @param string $QuarantinePath An optional, custom-defined path to
179180
* phpMussel's quarantine directory.
180181
* @param string $SignaturesPath An optional, custom-defined path to
@@ -314,6 +315,9 @@ public function __construct(
314315
/** Load phpMussel core L10N data. */
315316
$this->loadL10N($this->L10NPath);
316317

318+
/** Initialise the cache. */
319+
$this->initialiseCache();
320+
317321
/**
318322
* Writes to the default error log.
319323
*
@@ -1132,7 +1136,7 @@ public function hexSafe(string $Data): string
11321136
* @throws Exception if using flatfiles for caching and if an appropriate
11331137
* cache directory hasn't been specified or can't be written to.
11341138
*/
1135-
public function initialiseCache()
1139+
private function initialiseCache()
11361140
{
11371141
/** Exit early if already initialised. */
11381142
if ($this->Cache instanceof \Maikuolan\Common\Cache) {
@@ -1153,188 +1157,16 @@ public function initialiseCache()
11531157
$this->Cache->PDOdsn = $this->Configuration['supplementary_cache_options']['pdo_dsn'];
11541158
$this->Cache->PDOusername = $this->Configuration['supplementary_cache_options']['pdo_username'];
11551159
$this->Cache->PDOpassword = $this->Configuration['supplementary_cache_options']['pdo_password'];
1156-
$this->Cache->connect();
1157-
1158-
/** Guard against missing cache directory. */
1159-
if (!$this->Cache->Using && !$this->CachePath) {
1160-
throw new \Exception('No valid cache path available.');
1161-
}
1162-
}
1163-
1164-
/**
1165-
* Deletes expired cache entries and regenerates cache files.
1166-
*
1167-
* @param string $Delete Forcibly delete a specific cache entry (optional).
1168-
* @return bool Operation succeeded (true) or failed (false).
1169-
*/
1170-
public function cleanCache(string $Delete = ''): bool
1171-
{
1172-
if (!empty($this->InstanceCache['CacheCleaned'])) {
1173-
return true;
1174-
}
1175-
$this->InstanceCache['CacheCleaned'] = true;
1176-
$CacheFiles = [];
1177-
$FileIndex = $this->CachePath . 'index.dat';
1178-
if (!is_readable($FileIndex)) {
1179-
return false;
1180-
}
1181-
$FileDataOld = $FileData = $this->readFileBlocks($FileIndex);
1182-
if (strpos($FileData, ';') !== false) {
1183-
$FileData = explode(';', $FileData);
1184-
foreach ($FileData as &$ThisData) {
1185-
if (strpos($ThisData, ':') === false) {
1186-
$ThisData = '';
1187-
continue;
1188-
}
1189-
$ThisData = explode(':', $ThisData, 3);
1190-
if (($Delete && $Delete === $ThisData[0]) || ($ThisData[1] > 0 && $this->Time > $ThisData[1])) {
1191-
$FileKey = bin2hex(substr($ThisData[0], 0, 1));
1192-
if (!isset($CacheFiles[$FileKey])) {
1193-
$CacheFiles[$FileKey] = $this->readFileBlocks($this->CachePath . $FileKey . '.tmp');
1194-
}
1195-
while (strpos($CacheFiles[$FileKey], $ThisData[0] . ':') !== false) {
1196-
$CacheFiles[$FileKey] = str_ireplace($ThisData[0] . ':' . $this->substrBeforeFirst(
1197-
$this->substrAfterFirst($CacheFiles[$FileKey], $ThisData[0] . ':'), ';'
1198-
) . ';', '', $CacheFiles[$FileKey]);
1199-
}
1200-
$ThisData = '';
1201-
continue;
1202-
}
1203-
$ThisData = $ThisData[0] . ':' . $ThisData[1];
1204-
}
1205-
$FileData = str_replace(';;', ';', implode(';', array_filter($FileData)) . ';');
1206-
if ($FileDataOld !== $FileData) {
1207-
$Handle = fopen($FileIndex, 'wb');
1208-
fwrite($Handle, $FileData);
1209-
fclose($Handle);
1210-
}
1211-
}
1212-
foreach ($CacheFiles as $CacheEntryKey => $CacheEntryValue) {
1213-
if (strlen($CacheEntryValue) < 2) {
1214-
if (file_exists($this->CachePath . $CacheEntryKey . '.tmp')) {
1215-
unlink($this->CachePath . $CacheEntryKey . '.tmp');
1216-
}
1217-
continue;
1218-
}
1219-
$Handle = fopen($this->CachePath . $CacheEntryKey . '.tmp', 'wb');
1220-
fwrite($Handle, $CacheEntryValue);
1221-
fclose($Handle);
1222-
}
1223-
return true;
1224-
}
1225-
1226-
/**
1227-
* Retrieves cache entries.
1228-
*
1229-
* @param string|array $Entry The name of the cache entr(y/ies) to retrieve;
1230-
* Can be a string to specify a single entry, or an array of strings to
1231-
* specify multiple entries.
1232-
* @return string|array Contents of the cache entr(y/ies).
1233-
*/
1234-
public function fetchCache($Entry = '')
1235-
{
1236-
$this->cleanCache();
1237-
$this->initialiseCache();
12381160

1239-
/** Override if using a different preferred caching mechanism. */
1240-
if ($this->Cache->Using) {
1241-
if (is_array($Entry)) {
1242-
$Out = [];
1243-
foreach ($Entry as $ThisKey => $ThisEntry) {
1244-
$Out[$ThisKey] = $this->Cache->getEntry($ThisEntry);
1245-
}
1246-
return $Out;
1247-
}
1248-
return $this->Cache->getEntry($Entry);
1161+
/** Assign cache path. */
1162+
if ($this->CachePath) {
1163+
$this->Cache->FFDefault = $this->CachePath . DIRECTORY_SEPARATOR . 'cache.dat';
12491164
}
12501165

1251-
/** Default process. */
1252-
if (!$Entry) {
1253-
return '';
1254-
}
1255-
if (is_array($Entry)) {
1256-
$Out = [];
1257-
foreach ($Entry as $Key => $Value) {
1258-
$Out[$Key] = $this->fetchCache($Value);
1259-
}
1260-
return $Out;
1261-
}
1262-
$File = $this->CachePath . bin2hex(substr($Entry, 0, 1)) . '.tmp';
1263-
if (!$FileData = $this->readFileBlocks($File)) {
1264-
return '';
1265-
}
1266-
if (!$Item = strpos($FileData, $Entry . ':') !== false ? $Entry . ':' . $this->substrBeforeFirst(
1267-
$this->substrAfterFirst($FileData, $Entry . ':'), ';'
1268-
) . ';' : '') {
1269-
return '';
1270-
}
1271-
$Expiry = $this->substrBeforeFirst($this->substrAfterFirst($Item, $Entry . ':'), ':');
1272-
if ($Expiry > 0 && $this->Time > $Expiry) {
1273-
while (strpos($FileData, $Entry . ':') !== false) {
1274-
$FileData = str_ireplace($Item, '', $FileData);
1275-
}
1276-
$Handle = fopen($File, 'wb');
1277-
fwrite($Handle, $FileData);
1278-
fclose($Handle);
1279-
return '';
1166+
/** Attempt to connect. */
1167+
if (!$this->Cache->connect()) {
1168+
throw new \Exception('Cache connect failed.');
12801169
}
1281-
if (!$ItemData = $this->substrBeforeFirst($this->substrAfterFirst($Item, $Entry . ':' . $Expiry . ':'), ';')) {
1282-
return '';
1283-
}
1284-
return gzinflate($this->hexSafe($ItemData)) ?: '';
1285-
}
1286-
1287-
/**
1288-
* Creates a cache entry and saves it to the cache.
1289-
*
1290-
* @param string $Entry Name of the cache entry to create.
1291-
* @param int $Expiry Unix time until the cache entry expires.
1292-
* @param string $ItemData Contents of the cache entry.
1293-
* @return bool True on success; False on failure.
1294-
*/
1295-
public function saveCache(string $Entry = '', int $Expiry = 0, string $ItemData = ''): bool
1296-
{
1297-
$this->cleanCache();
1298-
$this->initialiseCache();
1299-
1300-
/** Override if using a different preferred caching mechanism. */
1301-
if ($this->Cache->Using) {
1302-
if ($Expiry <= 0) {
1303-
$Expiry = 0;
1304-
} elseif ($Expiry > $this->Time) {
1305-
$Expiry = $Expiry - $this->Time;
1306-
}
1307-
return $this->Cache->setEntry($Entry, $ItemData, $Expiry);
1308-
}
1309-
1310-
/** Default process. */
1311-
if (!$Entry || !$ItemData) {
1312-
return false;
1313-
}
1314-
if (!$Expiry) {
1315-
$Expiry = $this->Time;
1316-
}
1317-
$File = $this->CachePath . bin2hex($Entry[0]) . '.tmp';
1318-
$Data = $this->readFileBlocks($File) ?: '';
1319-
while (strpos($Data, $Entry . ':') !== false) {
1320-
$Data = str_ireplace($Entry . ':' . $this->substrBeforeFirst($this->substrAfterFirst($Data, $Entry . ':'), ';') . ';', '', $Data);
1321-
}
1322-
$Data .= $Entry . ':' . $Expiry . ':' . bin2hex(gzdeflate($ItemData,9)) . ';';
1323-
$Handle = fopen($File, 'wb');
1324-
fwrite($Handle, $Data);
1325-
fclose($Handle);
1326-
$IndexFile = $this->CachePath . 'index.dat';
1327-
$IndexNewData = $IndexData = $this->readFileBlocks($IndexFile) ?: '';
1328-
while (strpos($IndexNewData, $Entry . ':') !== false) {
1329-
$IndexNewData = str_ireplace($Entry . ':' . $this->substrBeforeFirst($this->substrAfterFirst($IndexNewData, $Entry . ':'), ';') . ';', '', $IndexNewData);
1330-
}
1331-
$IndexNewData .= $Entry . ':' . $Expiry . ';';
1332-
if ($IndexNewData !== $IndexData) {
1333-
$IndexHandle = fopen($IndexFile, 'wb');
1334-
fwrite($IndexHandle, $IndexNewData);
1335-
fclose($IndexHandle);
1336-
}
1337-
return true;
13381170
}
13391171

13401172
/**

0 commit comments

Comments
 (0)