8
8
* License: GNU/GPLv2
9
9
* @see LICENSE.txt
10
10
*
11
- * This file: The loader (last modified: 2020.06.22 ).
11
+ * This file: The loader (last modified: 2020.06.27 ).
12
12
*/
13
13
14
14
namespace phpMussel \Core ;
@@ -174,7 +174,8 @@ class Loader
174
174
* @param string $ConfigurationPath Custom-defined path to phpMussel's
175
175
* configuration file (optional).
176
176
* @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).
178
179
* @param string $QuarantinePath An optional, custom-defined path to
179
180
* phpMussel's quarantine directory.
180
181
* @param string $SignaturesPath An optional, custom-defined path to
@@ -314,6 +315,9 @@ public function __construct(
314
315
/** Load phpMussel core L10N data. */
315
316
$ this ->loadL10N ($ this ->L10NPath );
316
317
318
+ /** Initialise the cache. */
319
+ $ this ->initialiseCache ();
320
+
317
321
/**
318
322
* Writes to the default error log.
319
323
*
@@ -1132,7 +1136,7 @@ public function hexSafe(string $Data): string
1132
1136
* @throws Exception if using flatfiles for caching and if an appropriate
1133
1137
* cache directory hasn't been specified or can't be written to.
1134
1138
*/
1135
- public function initialiseCache ()
1139
+ private function initialiseCache ()
1136
1140
{
1137
1141
/** Exit early if already initialised. */
1138
1142
if ($ this ->Cache instanceof \Maikuolan \Common \Cache) {
@@ -1153,188 +1157,16 @@ public function initialiseCache()
1153
1157
$ this ->Cache ->PDOdsn = $ this ->Configuration ['supplementary_cache_options ' ]['pdo_dsn ' ];
1154
1158
$ this ->Cache ->PDOusername = $ this ->Configuration ['supplementary_cache_options ' ]['pdo_username ' ];
1155
1159
$ 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 ();
1238
1160
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 ' ;
1249
1164
}
1250
1165
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. ' );
1280
1169
}
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 ;
1338
1170
}
1339
1171
1340
1172
/**
0 commit comments