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