Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ShortlinkController | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
redirectViaHtml | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
redirectViaHttp | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
redirectAction | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | |
3 | /** |
4 | * Short link controller |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2019. |
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 Controller |
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 Main Site |
28 | */ |
29 | |
30 | namespace VuFind\Controller; |
31 | |
32 | use Laminas\Config\Config; |
33 | use Laminas\ServiceManager\ServiceLocatorInterface; |
34 | use VuFind\UrlShortener\UrlShortenerInterface; |
35 | |
36 | use function is_callable; |
37 | use function strlen; |
38 | |
39 | /** |
40 | * Short link controller |
41 | * |
42 | * @category VuFind |
43 | * @package Controller |
44 | * @author Demian Katz <demian.katz@villanova.edu> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org Main Site |
47 | */ |
48 | class ShortlinkController extends AbstractBase |
49 | { |
50 | /** |
51 | * Amount of seconds after which HTML redirect is performed. |
52 | * |
53 | * @var int |
54 | */ |
55 | protected $redirectDelayHtml = 3; |
56 | |
57 | /** |
58 | * Which redirect mechanism to use (html, http, threshold:<urlLength>) |
59 | * |
60 | * @var string |
61 | */ |
62 | protected $redirectMethod = 'threshold:1000'; |
63 | |
64 | /** |
65 | * Constructor |
66 | * |
67 | * @param ServiceLocatorInterface $sm Service manager |
68 | * @param Config $config VuFind configuration |
69 | */ |
70 | public function __construct(ServiceLocatorInterface $sm, Config $config) |
71 | { |
72 | // Call standard record controller initialization: |
73 | parent::__construct($sm); |
74 | |
75 | // Set redirect method, if specified: |
76 | if (isset($config->Mail->url_shortener_redirect_method)) { |
77 | $this->redirectMethod = strtolower( |
78 | trim($config->Mail->url_shortener_redirect_method) |
79 | ); |
80 | } |
81 | } |
82 | |
83 | /** |
84 | * Redirect to given URL by using a HTML meta redirect mechanism. |
85 | * |
86 | * @param string $url Redirect target |
87 | * |
88 | * @return mixed |
89 | */ |
90 | protected function redirectViaHtml($url) |
91 | { |
92 | $view = $this->createViewModel(); |
93 | $view->redirectTarget = $url; |
94 | $view->redirectDelay = $this->redirectDelayHtml; |
95 | return $view; |
96 | } |
97 | |
98 | /** |
99 | * Redirect to given URL by using a HTTP header. |
100 | * |
101 | * @param string $url Redirect target |
102 | * |
103 | * @return mixed |
104 | */ |
105 | protected function redirectViaHttp($url) |
106 | { |
107 | return $this->redirect()->toUrl($url); |
108 | } |
109 | |
110 | /** |
111 | * Resolve full version of shortlink & redirect to target. |
112 | * |
113 | * @return mixed |
114 | */ |
115 | public function redirectAction() |
116 | { |
117 | if ($id = $this->params('id')) { |
118 | $resolver = $this->serviceLocator->get(UrlShortenerInterface::class); |
119 | if ($url = $resolver->resolve($id)) { |
120 | $threshRegEx = '"^threshold:(\d+)$"i'; |
121 | if (preg_match($threshRegEx, $this->redirectMethod, $hits)) { |
122 | $threshold = $hits[1]; |
123 | $method = (strlen($url) > $threshold) ? 'Html' : 'Http'; |
124 | } else { |
125 | $method = ucwords($this->redirectMethod); |
126 | } |
127 | if (!is_callable([$this, 'redirectVia' . $method])) { |
128 | throw new \VuFind\Exception\BadConfig( |
129 | 'Invalid redirect method: ' . $method |
130 | ); |
131 | } |
132 | return $this->{'redirectVia' . $method}($url); |
133 | } |
134 | } |
135 | |
136 | $this->getResponse()->setStatusCode(404); |
137 | } |
138 | } |