Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
9 / 27
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProxyUrl
33.33% covered (danger)
33.33%
9 / 27
40.00% covered (danger)
40.00%
2 / 5
39.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
4.37
 checkConfig
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 checkUrl
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 queryWebService
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * Proxy URL view helper
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  View_Helpers
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\View\Helper\Root;
31
32use Exception;
33use Laminas\Cache\Storage\StorageInterface as CacheAdapter;
34
35use function intval;
36
37/**
38 * Proxy URL view helper
39 *
40 * @category VuFind
41 * @package  View_Helpers
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46class ProxyUrl extends \Laminas\View\Helper\AbstractHelper implements
47    \Laminas\Log\LoggerAwareInterface,
48    \VuFindHttp\HttpServiceAwareInterface
49{
50    use \VuFind\Cache\CacheTrait;
51    use \VuFind\Log\LoggerAwareTrait;
52    use \VuFindHttp\HttpServiceAwareTrait;
53
54    /**
55     * VuFind configuration
56     *
57     * @var \Laminas\Config\Config
58     */
59    protected $config;
60
61    /**
62     * Constructor
63     *
64     * @param \Laminas\Config\Config $config VuFind configuration
65     * @param CacheAdapter           $cache  Cache for web service responses
66     */
67    public function __construct($config = null, CacheAdapter $cache = null)
68    {
69        $this->config = $config;
70        $this->setCacheStorage($cache);
71        $this->cacheLifetime = intval($config->EZproxy->prefixLinksWebServiceCacheLifetime ?? 600);
72    }
73
74    /**
75     * Apply proxy prefix to URL (if configured).
76     *
77     * @param string $url The raw URL to adjust
78     *
79     * @return string
80     */
81    public function __invoke($url)
82    {
83        $useWebService = $this->config->EZproxy->prefixLinksWebServiceUrl ?? false;
84        if ($useWebService) {
85            $usePrefix = $this->checkUrl($url) ?? $this->checkConfig();
86        } else {
87            $usePrefix = $this->checkConfig();
88        }
89
90        return ($usePrefix && isset($this->config->EZproxy->host))
91            ? $this->config->EZproxy->host . '/login?qurl=' . urlencode($url)
92            : $url;
93    }
94
95    /**
96     * Return the configured prefixLinks setting.
97     *
98     * @return bool The configured setting, or the default
99     */
100    protected function checkConfig()
101    {
102        return $this->config->EZproxy->prefixLinks ?? true;
103    }
104
105    /**
106     * Check whether the given URL requires the proxy prefix.  Cache the response.
107     *
108     * @param string $url The raw URL to check
109     *
110     * @return mixed Whether the URL should be prefixed, or null if it can't be determined
111     */
112    protected function checkUrl($url)
113    {
114        $domain = parse_url($url, PHP_URL_SCHEME)
115         . '://'
116         . parse_url($url, PHP_URL_HOST);
117        $cacheKey = "proxyUrl-domainToUsePrefix-$domain";
118        $usePrefix = $this->getCachedData($cacheKey);
119        if (null === $usePrefix) {
120            $usePrefix = $this->queryWebService($domain);
121            $this->putCachedData($cacheKey, $usePrefix);
122        }
123        return $usePrefix;
124    }
125
126    /**
127     * Query the web service on whether to prefix URLs to a given domain.
128     *
129     * @param $domain The domain
130     *
131     * @return mixed Whether the URL should be prefixed, or null if it can't be determined
132     */
133    protected function queryWebService($domain)
134    {
135        $prefixLinksWebServiceUrl = $this->config->EZproxy->prefixLinksWebServiceUrl;
136        try {
137            $response = $this->httpService->get($prefixLinksWebServiceUrl, ['url' => $domain]);
138            $responseData = trim($response->getContent());
139        } catch (Exception $ex) {
140            $this->logError('Exception during EZproxy web service request: ' . $ex->getMessage());
141            return null;
142        }
143        return '1' === $responseData;
144    }
145}