@@ -286,9 +286,17 @@ private function getImageInfo()
286
286
$ mediaDirectory = $ this ->fileSystem ->getDirectoryRead (DirectoryList::MEDIA );
287
287
$ data = implode ('_ ' , $ this ->convertToReadableFormat ($ this ->miscParams ));
288
288
289
+ $ pathTemplate = $ this ->getModule ()
290
+ . DIRECTORY_SEPARATOR . "%s " . DIRECTORY_SEPARATOR
291
+ . $ this ->getFilePath ();
292
+
289
293
// New paths are generated without dependency on
290
294
// an encryption key.
291
- $ hashBasedPath = $ this ->generatePath ($ data );
295
+ $ hashBasedPath = preg_replace (
296
+ '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
297
+ DIRECTORY_SEPARATOR ,
298
+ sprintf ($ pathTemplate , hash (self ::HASH_ALGORITHM , $ data ))
299
+ );
292
300
293
301
if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ hashBasedPath )) {
294
302
return $ hashBasedPath ;
@@ -298,15 +306,19 @@ private function getImageInfo()
298
306
// existing encryption key based media gallery cache valid
299
307
// even if an encryption key was changed.
300
308
$ keys = explode ("\n" , $ this ->encryptor ->exportKeys ());
301
-
302
- if (count ($ keys ) === 1 ) {
303
- return $ this ->generatePath ($ data , $ this ->decodeKey ($ keys [0 ]));
304
- }
305
-
306
309
foreach ($ keys as $ key ) {
307
- $ key = $ this ->decodeKey ($ key );
310
+ if (str_starts_with ($ key , ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX )) {
311
+ // phpcs:disable Magento2.Functions.DiscouragedFunction
312
+ $ key = base64_decode (
313
+ substr ($ key , strlen (ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX ))
314
+ );
315
+ }
308
316
309
- $ keyBasedPath = $ this ->generatePath ($ data , $ key );
317
+ $ keyBasedPath = preg_replace (
318
+ '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
319
+ DIRECTORY_SEPARATOR ,
320
+ sprintf ($ pathTemplate , hash_hmac (self ::HASH_ALGORITHM , $ data , $ key ))
321
+ );
310
322
311
323
if ($ mediaDirectory ->isExist ($ this ->context ->getPath () . DIRECTORY_SEPARATOR . $ keyBasedPath )) {
312
324
return $ keyBasedPath ;
@@ -316,54 +328,13 @@ private function getImageInfo()
316
328
return $ hashBasedPath ;
317
329
}
318
330
319
- /**
320
- * Generate path based on data and key, If key is not provided, the path is generated without it
321
- *
322
- * @param string $data
323
- * @param string|null $key
324
- * @return string
325
- */
326
- private function generatePath (string $ data , ?string $ key = null ): string
327
- {
328
- $ pathTemplate = $ this ->getModule ()
329
- . DIRECTORY_SEPARATOR . "%s " . DIRECTORY_SEPARATOR
330
- . $ this ->getFilePath ();
331
-
332
- $ hash = $ key ? hash_hmac (self ::HASH_ALGORITHM , $ data , $ key ) : hash (self ::HASH_ALGORITHM , $ data );
333
-
334
- return preg_replace (
335
- '|\Q ' . DIRECTORY_SEPARATOR . '\E+| ' ,
336
- DIRECTORY_SEPARATOR ,
337
- sprintf ($ pathTemplate , $ hash )
338
- );
339
- }
340
-
341
- /**
342
- * Decode key if it was base64 encoded
343
- *
344
- * @param string $key
345
- *
346
- * @return string
347
- */
348
- private function decodeKey (string $ key ): string
349
- {
350
- if (str_starts_with ($ key , ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX )) {
351
- // phpcs:disable Magento2.Functions.DiscouragedFunction
352
- return base64_decode (
353
- substr ($ key , strlen (ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX ))
354
- );
355
- }
356
-
357
- return $ key ;
358
- }
359
-
360
331
/**
361
332
* Converting bool into a string representation
362
333
*
363
334
* @param array $miscParams
364
335
* @return array
365
336
*/
366
- private function convertToReadableFormat (array $ miscParams ): array
337
+ private function convertToReadableFormat (array $ miscParams )
367
338
{
368
339
return $ this ->convertImageMiscParamsToReadableFormat ->convertImageMiscParamsToReadableFormat ($ miscParams );
369
340
}
0 commit comments