Skip to content

Commit

Permalink
[VUFIND-1657]: Prevent/clarify errors when cache is disabled (#3606)
Browse files Browse the repository at this point in the history
Co-authored-by: Demian Katz <demian.katz@villanova.edu>
  • Loading branch information
maccabeelevine and demiankatz committed Apr 22, 2024
1 parent 1a5597e commit 511f059
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
10 changes: 8 additions & 2 deletions module/VuFind/src/VuFind/Captcha/ImageFactory.php
Expand Up @@ -35,6 +35,8 @@
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;

use function is_callable;

/**
* Image CAPTCHA factory.
*
Expand Down Expand Up @@ -69,11 +71,15 @@ public function __invoke(
throw new \Exception('Unexpected options passed to factory.');
}

$cacheManager = $container->get(\VuFind\Cache\Manager::class);
$cacheOptions = $cacheManager->getCache('public')->getOptions();
if (!is_callable([$cacheOptions, 'getCacheDir'])) {
throw new \Exception('Image CAPTCHA requires access to public cache; is cache disabled?');
}
$imageOptions = [
'font' => APPLICATION_PATH
. '/vendor/webfontkit/open-sans/fonts/opensans-regular.ttf',
'imgDir' => $container->get(\VuFind\Cache\Manager::class)
->getCache('public')->getOptions()->getCacheDir(),
'imgDir' => $cacheOptions->getCacheDir(),
];

$config = $container->get(\VuFind\Config\PluginManager::class)
Expand Down
15 changes: 11 additions & 4 deletions module/VuFind/src/VuFind/Cover/CachingProxy.php
Expand Up @@ -69,9 +69,9 @@ class CachingProxy
/**
* Constructor
*
* @param Client $client HTTP client
* @param string $cache Base directory for cache
* @param array $allowedHosts Array of regular expressions for hosts to cache
* @param Client $client HTTP client
* @param ?string $cache Base directory for cache (null to disable caching)
* @param array $allowedHosts Array of regular expressions for hosts to cache
*/
public function __construct(Client $client, $cache, array $allowedHosts = [])
{
Expand All @@ -90,7 +90,7 @@ public function __construct(Client $client, $cache, array $allowedHosts = [])
public function fetch($url)
{
$file = $this->getCacheFile($url);
$cacheAllowed = $this->hasLegalHost($url);
$cacheAllowed = $this->cache && $this->hasLegalHost($url);
if (!$cacheAllowed || !($response = $this->fetchCache($file))) {
$response = $this->client->setUri($url)->send();
if ($cacheAllowed) {
Expand Down Expand Up @@ -124,6 +124,9 @@ protected function fetchCache($file)
*/
protected function setCache($file, Response $response)
{
if (!$this->cache) {
return; // don't write if cache is disabled
}
if (!file_exists($this->cache)) {
mkdir($this->cache);
}
Expand Down Expand Up @@ -157,9 +160,13 @@ protected function hasLegalHost($url)
* @param string $url URL
*
* @return string
* @throws \Exception
*/
protected function getCacheFile($url)
{
if (!$this->cache) {
throw new \Exception('Unexpected call to getCacheFile -- cache is disabled.');
}
$hash = md5($url);
return $this->cache . '/' . substr($hash, 0, 3) . '/' . substr($hash, 3);
}
Expand Down
15 changes: 11 additions & 4 deletions module/VuFind/src/VuFind/Cover/CachingProxyFactory.php
Expand Up @@ -35,6 +35,8 @@
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;

use function is_callable;

/**
* Cover caching proxy factory.
*
Expand Down Expand Up @@ -68,13 +70,18 @@ public function __invoke(
if (!empty($options)) {
throw new \Exception('Unexpected options sent to factory.');
}
$cacheDir = $container->get(\VuFind\Cache\Manager::class)
->getCache('cover')->getOptions()->getCacheDir();
$client = $container->get(\VuFindHttp\HttpService::class)->createClient();
$cacheOptions = $container->get(\VuFind\Cache\Manager::class)
->getCache('cover')->getOptions();
$cacheDir = is_callable([$cacheOptions, 'getCacheDir'])
? $cacheOptions->getCacheDir() : null;
$config = $container->get(\VuFind\Config\PluginManager::class)->get('config')
->toArray();
$allowedHosts = isset($config['Content']['coverproxyCache'])
? (array)$config['Content']['coverproxyCache'] : [];
return new $requestedName($client, $cacheDir . '/proxy', $allowedHosts);
return new $requestedName(
$container->get(\VuFindHttp\HttpService::class)->createClient(),
$cacheDir === null ? null : $cacheDir . '/proxy',
$allowedHosts
);
}
}
8 changes: 6 additions & 2 deletions module/VuFind/src/VuFind/Cover/LoaderFactory.php
Expand Up @@ -35,6 +35,8 @@
use Psr\Container\ContainerExceptionInterface as ContainerException;
use Psr\Container\ContainerInterface;

use function is_callable;

/**
* Cover loader factory.
*
Expand Down Expand Up @@ -68,8 +70,10 @@ public function __invoke(
if (!empty($options)) {
throw new \Exception('Unexpected options sent to factory.');
}
$cacheDir = $container->get(\VuFind\Cache\Manager::class)
->getCache('cover')->getOptions()->getCacheDir();
$cacheOptions = $container->get(\VuFind\Cache\Manager::class)
->getCache('cover')->getOptions();
$cacheDir = is_callable([$cacheOptions, 'getCacheDir'])
? $cacheOptions->getCacheDir() : null;
$config = $container->get(\VuFind\Config\PluginManager::class)
->get('config');
$loader = new $requestedName(
Expand Down

0 comments on commit 511f059

Please sign in to comment.