PluginProbe
User Access Manager / 2.2.23
User Access Manager v2.2.23
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Controller/Frontend/RedirectController.php +152 -20 2.3.132.2.23 View file →
@@ -1,11 +1,23 @@
1 1 <?php
2 +/**
3 + * FrontendRedirectController.php
4 + *
5 + * The FrontendRedirectController class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Controller\Frontend;
6 19
7 -use JetBrains\PhpStorm\NoReturn;
8 20 use UserAccessManager\Access\AccessHandler;
9 21 use UserAccessManager\Cache\Cache;
10 22 use UserAccessManager\Config\MainConfig;
11 23 use UserAccessManager\Config\WordpressConfig;
@@ -19,35 +31,110 @@
19 31 use UserAccessManager\Util\Util;
20 32 use UserAccessManager\Wrapper\Php;
21 33 use UserAccessManager\Wrapper\Wordpress;
22 34
35 +/**
36 + * Class FrontendRedirectController
37 + *
38 + * @package UserAccessManager\Controller
39 + */
23 40 class RedirectController extends Controller
24 41 {
25 42 use LoginControllerTrait;
26 43
27 - public const POST_URL_CACHE_KEY = 'PostUrls';
44 + const POST_URL_CACHE_KEY = 'PostUrls';
28 45
46 + /**
47 + * @var MainConfig
48 + */
49 + private $mainConfig;
50 +
51 + /**
52 + * @var Database
53 + */
54 + private $database;
55 +
56 + /**
57 + * @var Cache
58 + */
59 + private $cache;
60 +
61 + /**
62 + * @var Util
63 + */
64 + private $util;
65 +
66 + /**
67 + * @var ObjectHandler
68 + */
69 + private $objectHandler;
70 +
71 + /**
72 + * @var AccessHandler
73 + */
74 + private $accessHandler;
75 +
76 + /**
77 + * @var FileHandler
78 + */
79 + private $fileHandler;
80 +
81 + /**
82 + * @var FileObjectFactory
83 + */
84 + private $fileObjectFactory;
85 +
86 + /**
87 + * RedirectController constructor.
88 + * @param Php $php
89 + * @param Wordpress $wordpress
90 + * @param WordpressConfig $wordpressConfig
91 + * @param MainConfig $mainConfig
92 + * @param Database $database
93 + * @param Util $util
94 + * @param Cache $cache
95 + * @param ObjectHandler $objectHandler
96 + * @param AccessHandler $accessHandler
97 + * @param FileHandler $fileHandler
98 + * @param FileObjectFactory $fileObjectFactory
99 + */
29 100 public function __construct(
30 101 Php $php,
31 102 Wordpress $wordpress,
32 103 WordpressConfig $wordpressConfig,
33 - private MainConfig $mainConfig,
34 - private Database $database,
35 - private Util $util,
36 - private Cache $cache,
37 - private ObjectHandler $objectHandler,
38 - private AccessHandler $accessHandler,
39 - private FileHandler $fileHandler,
40 - private FileObjectFactory $fileObjectFactory
104 + MainConfig $mainConfig,
105 + Database $database,
106 + Util $util,
107 + Cache $cache,
108 + ObjectHandler $objectHandler,
109 + AccessHandler $accessHandler,
110 + FileHandler $fileHandler,
111 + FileObjectFactory $fileObjectFactory
41 112 ) {
42 113 parent::__construct($php, $wordpress, $wordpressConfig);
114 + $this->mainConfig = $mainConfig;
115 + $this->database = $database;
116 + $this->util = $util;
117 + $this->cache = $cache;
118 + $this->objectHandler = $objectHandler;
119 + $this->accessHandler = $accessHandler;
120 + $this->fileHandler = $fileHandler;
121 + $this->fileObjectFactory = $fileObjectFactory;
43 122 }
44 123
124 + /**
125 + * @return Wordpress
126 + */
45 127 protected function getWordpress(): Wordpress
46 128 {
47 129 return $this->wordpress;
48 130 }
49 131
132 + /**
133 + * Returns the post by the given url.
134 + * @param string $url The url of the post(attachment).
135 + * @return int
136 + */
50 137 public function getPostIdByUrl(string $url): int
51 138 {
52 139 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
53 140
@@ -72,8 +159,14 @@
72 159
73 160 return $postUrls[$url];
74 161 }
75 162
163 + /**
164 + * Returns the file object by the given type and url.
165 + * @param string $objectType The type of the requested file.
166 + * @param string $objectUrl The file url.
167 + * @return null|FileObject
168 + */
76 169 private function getFileSettingsByType(string $objectType, string $objectUrl): ?FileObject
77 170 {
78 171 $fileObject = null;
79 172
@@ -113,11 +206,14 @@
113 206 return $fileObject;
114 207 }
115 208
116 209 /**
210 + * Delivers the content of the requested file.
211 + * @param string $objectType The type of the requested file.
212 + * @param string $objectUrl The file url.
117 213 * @throws UserGroupTypeException
118 214 */
119 - public function getFile(string $objectType, string $objectUrl): void
215 + public function getFile(string $objectType, string $objectUrl)
120 216 {
121 217 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
122 218
123 219 if ($fileObject === null) {
@@ -140,8 +236,13 @@
140 236
141 237 $this->fileHandler->getFile($file, $fileObject->isImage());
142 238 }
143 239
240 + /**
241 + * Returns the redirect url and the permalink of the post if exists.
242 + * @param null|string $permalink
243 + * @return null|string
244 + */
144 245 private function getRedirectUrlAndPermalink(?string &$permalink): ?string
145 246 {
146 247 $permalink = null;
147 248 $redirect = $this->mainConfig->getRedirect();
@@ -166,14 +267,16 @@
166 267 return $url;
167 268 }
168 269
169 270 /**
271 + * Redirects the user to his destination.
272 + * @param bool $checkPosts
170 273 * @throws UserGroupTypeException
171 274 */
172 - public function redirectUser(bool $checkPosts = true): void
275 + public function redirectUser($checkPosts = true)
173 276 {
174 277 if ($checkPosts === true) {
175 - $posts = $this->wordpress->getWpQuery()->get_posts();
278 + $posts = (array)$this->wordpress->getWpQuery()->get_posts();
176 279
177 280 foreach ($posts as $post) {
178 281 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
179 282 return;
@@ -189,8 +292,13 @@
189 292 $this->php->callExit();
190 293 }
191 294 }
192 295
296 + /**
297 + * Returns the post id by the post name.
298 + * @param string $name
299 + * @return int
300 + */
193 301 private function getPostIdByName(string $name): int
194 302 {
195 303 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
196 304
@@ -197,9 +305,9 @@
197 305 $query = $this->database->prepare(
198 306 "SELECT ID
199 307 FROM {$this->database->getPostsTable()}
200 308 WHERE post_name = %s
201 - AND post_type IN ('$postableTypes')",
309 + AND post_type IN ('{$postableTypes}')",
202 310 $name
203 311 );
204 312
205 313 return (int) $this->database->getVariable($query);
@@ -204,9 +312,15 @@
204 312
205 313 return (int) $this->database->getVariable($query);
206 314 }
207 315
208 - private function extractObjectTypeAndId(mixed $pageParams, ?string &$objectType, int|string|null &$objectId): void
316 + /**
317 + * Extracts the object type and id.
318 + * @param mixed $pageParams
319 + * @param null|string $objectType
320 + * @param null|int|string $objectId
321 + */
322 + private function extractObjectTypeAndId($pageParams, ?string &$objectType, ?string &$objectId)
209 323 {
210 324 $objectType = null;
211 325 $objectId = null;
212 326
@@ -236,11 +350,15 @@
236 350 }
237 351 }
238 352
239 353 /**
354 + * Redirects to a page or to content.
355 + * @param array|null $headers The headers which are given from wordpress.
356 + * @param mixed $pageParams The params of the current page.
357 + * @return array|null
240 358 * @throws UserGroupTypeException
241 359 */
242 - public function redirect(?array $headers, mixed $pageParams): ?array
360 + public function redirect(?array $headers, $pageParams): ?array
243 361 {
244 362 $fileUrl = $this->getRequestParameter('uamgetfile');
245 363 $fileType = $this->getRequestParameter('uamfiletype');
246 364
@@ -258,9 +376,15 @@
258 376
259 377 return $headers;
260 378 }
261 379
262 - public function getFileUrl(string $url, int|string|null $id): string
380 + /**
381 + * Returns the url for a locked file.
382 + * @param string $url The base url.
383 + * @param int|string $id The id of the file.
384 + * @return string
385 + */
386 + public function getFileUrl(string $url, $id): string
263 387 {
264 388 // Nginx always supports real urls so we need the new urls only
265 389 // if we don't use nginx and mod_rewrite is disabled
266 390 if ($this->mainConfig->lockFile() === true
@@ -268,9 +392,9 @@
268 392 && $this->wordpress->gotModRewrite() === false
269 393 ) {
270 394 $post = $this->objectHandler->getPost($id);
271 395
272 - if ($post !== false) {
396 + if ($post !== null) {
273 397 $type = explode('/', $post->post_mime_type);
274 398 $type = $type[1] ?? $type[0];
275 399
276 400 $lockedFileTypes = $this->mainConfig->getLockedFiles();
@@ -284,8 +408,14 @@
284 408
285 409 return $url;
286 410 }
287 411
412 + /**
413 + * Caches the urls for the post for a later lookup.
414 + * @param string $url The url of the post.
415 + * @param object $post The post object.
416 + * @return string
417 + */
288 418 public function cachePostLinks(string $url, object $post): string
289 419 {
290 420 $postUrls = (array) $this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
291 421 $postUrls[$url] = $post->ID;
@@ -292,10 +422,12 @@
292 422 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
293 423 return $url;
294 424 }
295 425
296 - #[NoReturn]
297 - public function testXSendFile(): void
426 + /**
427 + * Tries to load the file via x send file
428 + */
429 + public function testXSendFile()
298 430 {
299 431 $this->fileHandler->deliverXSendFileTestFile();
300 432 }
301 433 }