PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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 +183 -92 2.3.172.2.2 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,36 +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';
28 - public const REDIRECT_TO_PARAMETER = 'redirect_to';
44 + const POST_URL_CACHE_KEY = 'PostUrls';
29 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 + */
30 100 public function __construct(
31 101 Php $php,
32 102 Wordpress $wordpress,
33 103 WordpressConfig $wordpressConfig,
34 - private MainConfig $mainConfig,
35 - private Database $database,
36 - private Util $util,
37 - private Cache $cache,
38 - private ObjectHandler $objectHandler,
39 - private AccessHandler $accessHandler,
40 - private FileHandler $fileHandler,
41 - 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
42 112 ) {
43 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;
44 122 }
45 123
124 + /**
125 + * @return Wordpress
126 + */
46 127 protected function getWordpress(): Wordpress
47 128 {
48 129 return $this->wordpress;
49 130 }
50 131
132 + /**
133 + * Returns the post by the given url.
134 + * @param string $url The url of the post(attachment).
135 + * @return int
136 + */
51 137 public function getPostIdByUrl(string $url): int
52 138 {
53 139 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
54 140
@@ -60,91 +146,67 @@
60 146 $newUrlPieces = preg_split('/-[0-9]+x[0-9]+(_[a-z])?/', $url);
61 147 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0] . $newUrlPieces[1] : $newUrlPieces[0];
62 148 $newUrl = preg_replace('/-pdf\.jpg$/', '.pdf', $newUrl);
63 149
64 - $postId = $this->wordpress->attachmentUrlToPostId($newUrl);
65 -
66 - if ($postId === 0) {
67 - $newUrl = preg_replace('/(\\.[^.\\s]{3,4})$/', '-scaled$1', $newUrl);
68 - $postId = $this->wordpress->attachmentUrlToPostId($newUrl);
69 - }
70 -
71 - $postUrls[$url] = $postId;
150 + $postUrls[$url] = $this->wordpress->attachmentUrlToPostId($newUrl);
72 151 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
73 152
74 153 return $postUrls[$url];
75 154 }
76 155
77 - private function normalizeAttachmentUrl(array $uploadDirs, string $objectUrl): string
156 + /**
157 + * Returns the file object by the given type and url.
158 + * @param string $objectType The type of the requested file.
159 + * @param string $objectUrl The file url.
160 + * @return null|FileObject
161 + */
162 + private function getFileSettingsByType(string $objectType, string $objectUrl): ?FileObject
78 163 {
79 - $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
80 - $regex = '/.*' . str_replace('/', '\/', $uploadDir) . '\//i';
81 - $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
82 - $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
164 + $fileObject = null;
83 165
84 - return rtrim($uploadUrl, '/') . '/' . ltrim($cleanObjectUrl, '/');
85 - }
166 + if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
167 + $uploadDirs = $this->wordpress->getUploadDir();
168 + $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
169 + $regex = '/.*' . str_replace('/', '\/', $uploadDir) . '\//i';
170 + $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
171 + $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
172 + $objectUrl = rtrim($uploadUrl, '/') . '/' . ltrim($cleanObjectUrl, '/');
86 173
87 - private function getAttachmentFileObject(string $objectUrl): ?FileObject
88 - {
89 - $uploadDirs = $this->wordpress->getUploadDir();
90 - $postId = $this->getPostIdByUrl($this->normalizeAttachmentUrl($uploadDirs, $objectUrl));
174 + $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
175 + $postType = $post->post_type ?? '';
91 176
92 - if ($postId < 1) {
93 - return null;
94 - }
177 + if ($postType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
178 + $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
95 179
96 - $post = $this->objectHandler->getPost($postId);
180 + $fileObject = $this->fileObjectFactory->createFileObject(
181 + $post->ID,
182 + $objectType,
183 + $uploadDirs['basedir'] . str_replace($multiPath, '', $objectUrl),
184 + $this->wordpress->attachmentIsImage($post->ID)
185 + );
186 + }
187 + } else {
188 + $extraParameter = $this->getRequestParameter('uamextra');
97 189
98 - if (($post->post_type ?? '') !== ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
99 - return null;
190 + $fileObject = $this->wordpress->applyFilters(
191 + 'uam_get_file_settings_by_type',
192 + $fileObject,
193 + $objectType,
194 + $objectUrl,
195 + $extraParameter
196 + );
100 197 }
101 198
102 - $file = $this->wordpress->getAttachedFile($post->ID);
103 -
104 - if ($file === false || $this->isInsideUploadDirectory($file, $uploadDirs['basedir']) === false) {
105 - return null;
106 - }
107 -
108 - return $this->fileObjectFactory->createFileObject(
109 - $post->ID,
110 - ObjectHandler::ATTACHMENT_OBJECT_TYPE,
111 - $file,
112 - $this->wordpress->attachmentIsImage($post->ID)
113 - );
199 + return $fileObject;
114 200 }
115 201
116 - private function isInsideUploadDirectory(string $file, string $uploadBaseDir): bool
117 - {
118 - $realFile = $this->php->realpath($file);
119 - $realUploadBaseDir = $this->php->realpath($uploadBaseDir);
120 -
121 - return $realFile !== false
122 - && $realUploadBaseDir !== false
123 - && str_starts_with($realFile, $realUploadBaseDir . DIRECTORY_SEPARATOR);
124 - }
125 -
126 - private function getFileSettingsByType(string $objectType, string $objectUrl): ?FileObject
127 - {
128 - if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
129 - return $this->getAttachmentFileObject($objectUrl);
130 - }
131 -
132 - $extraParameter = $this->getRequestParameter('uamextra');
133 -
134 - return $this->wordpress->applyFilters(
135 - 'uam_get_file_settings_by_type',
136 - null,
137 - $objectType,
138 - $objectUrl,
139 - $extraParameter
140 - );
141 - }
142 -
143 202 /**
203 + * Delivers the content of the requested file.
204 + * @param string $objectType The type of the requested file.
205 + * @param string $objectUrl The file url.
144 206 * @throws UserGroupTypeException
145 207 */
146 - public function getFile(string $objectType, string $objectUrl): void
208 + public function getFile(string $objectType, string $objectUrl)
147 209 {
148 210 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
149 211
150 212 if ($fileObject === null) {
@@ -167,8 +229,13 @@
167 229
168 230 $this->fileHandler->getFile($file, $fileObject->isImage());
169 231 }
170 232
233 + /**
234 + * Returns the redirect url and the permalink of the post if exists.
235 + * @param null|string $permalink
236 + * @return null|string
237 + */
171 238 private function getRedirectUrlAndPermalink(?string &$permalink): ?string
172 239 {
173 240 $permalink = null;
174 241 $redirect = $this->mainConfig->getRedirect();
@@ -185,11 +252,8 @@
185 252 } elseif ($redirect === 'custom_url') {
186 253 $url = $this->mainConfig->getRedirectCustomUrl();
187 254 } elseif ($redirect === 'login') {
188 255 $url = $this->getLoginUrl();
189 - } elseif ($redirect === 'origin') {
190 - $referer = $this->wordpress->getReferer();
191 - $url = $referer !== false ? $referer : $this->wordpress->getHomeUrl('/');
192 256 } else {
193 257 $url = $this->wordpress->getHomeUrl('/');
194 258 }
195 259
@@ -196,14 +260,16 @@
196 260 return $url;
197 261 }
198 262
199 263 /**
264 + * Redirects the user to his destination.
265 + * @param bool $checkPosts
200 266 * @throws UserGroupTypeException
201 267 */
202 - public function redirectUser(bool $checkPosts = true): void
268 + public function redirectUser($checkPosts = true)
203 269 {
204 270 if ($checkPosts === true) {
205 - $posts = $this->wordpress->getWpQuery()->get_posts();
271 + $posts = (array)$this->wordpress->getWpQuery()->get_posts();
206 272
207 273 foreach ($posts as $post) {
208 274 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
209 275 return;
@@ -214,17 +280,18 @@
214 280 $url = $this->getRedirectUrlAndPermalink($permalink);
215 281 $currentUrl = $this->util->getCurrentUrl();
216 282
217 283 if ($url !== null && $url !== $currentUrl && $permalink !== $currentUrl) {
218 - if ($this->mainConfig->appendRedirectToParameter() === true) {
219 - $url = $this->wordpress->addQueryArg([self::REDIRECT_TO_PARAMETER => $currentUrl], $url);
220 - }
221 -
222 284 $this->wordpress->wpRedirect($url);
223 285 $this->php->callExit();
224 286 }
225 287 }
226 288
289 + /**
290 + * Returns the post id by the post name.
291 + * @param string $name
292 + * @return int
293 + */
227 294 private function getPostIdByName(string $name): int
228 295 {
229 296 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
230 297
@@ -231,9 +298,9 @@
231 298 $query = $this->database->prepare(
232 299 "SELECT ID
233 300 FROM {$this->database->getPostsTable()}
234 301 WHERE post_name = %s
235 - AND post_type IN ('$postableTypes')",
302 + AND post_type IN ('{$postableTypes}')",
236 303 $name
237 304 );
238 305
239 306 return (int) $this->database->getVariable($query);
@@ -238,9 +305,15 @@
238 305
239 306 return (int) $this->database->getVariable($query);
240 307 }
241 308
242 - private function extractObjectTypeAndId(mixed $pageParams, ?string &$objectType, int|string|null &$objectId): void
309 + /**
310 + * Extracts the object type and id.
311 + * @param object $pageParams
312 + * @param null|string $objectType
313 + * @param null|int|string $objectId
314 + */
315 + private function extractObjectTypeAndId(object $pageParams, ?string &$objectType, ?string &$objectId)
243 316 {
244 317 $objectType = null;
245 318 $objectId = null;
246 319
@@ -270,11 +343,15 @@
270 343 }
271 344 }
272 345
273 346 /**
347 + * Redirects to a page or to content.
348 + * @param array $headers The headers which are given from wordpress.
349 + * @param object $pageParams The params of the current page.
350 + * @return array
274 351 * @throws UserGroupTypeException
275 352 */
276 - public function redirect(?array $headers, mixed $pageParams): ?array
353 + public function redirect(array $headers, object $pageParams): array
277 354 {
278 355 $fileUrl = $this->getRequestParameter('uamgetfile');
279 356 $fileType = $this->getRequestParameter('uamfiletype');
280 357
@@ -292,9 +369,15 @@
292 369
293 370 return $headers;
294 371 }
295 372
296 - public function getFileUrl(string $url, int|string|null $id): string
373 + /**
374 + * Returns the url for a locked file.
375 + * @param string $url The base url.
376 + * @param integer $id The _iId of the file.
377 + * @return string
378 + */
379 + public function getFileUrl(string $url, int $id): string
297 380 {
298 381 // Nginx always supports real urls so we need the new urls only
299 382 // if we don't use nginx and mod_rewrite is disabled
300 383 if ($this->mainConfig->lockFile() === true
@@ -302,9 +385,9 @@
302 385 && $this->wordpress->gotModRewrite() === false
303 386 ) {
304 387 $post = $this->objectHandler->getPost($id);
305 388
306 - if ($post !== false) {
389 + if ($post !== null) {
307 390 $type = explode('/', $post->post_mime_type);
308 391 $type = $type[1] ?? $type[0];
309 392
310 393 $lockedFileTypes = $this->mainConfig->getLockedFiles();
@@ -318,8 +401,14 @@
318 401
319 402 return $url;
320 403 }
321 404
405 + /**
406 + * Caches the urls for the post for a later lookup.
407 + * @param string $url The url of the post.
408 + * @param object $post The post object.
409 + * @return string
410 + */
322 411 public function cachePostLinks(string $url, object $post): string
323 412 {
324 413 $postUrls = (array) $this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
325 414 $postUrls[$url] = $post->ID;
@@ -326,10 +415,12 @@
326 415 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
327 416 return $url;
328 417 }
329 418
330 - #[NoReturn]
331 - public function testXSendFile(): void
419 + /**
420 + * Tries to load the file via x send file
421 + */
422 + public function testXSendFile()
332 423 {
333 424 $this->fileHandler->deliverXSendFileTestFile();
334 425 }
335 426 }