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 +193 -56 2.3.13 → 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,41 +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';
40 + const POST_URL_CACHE_KEY = 'PostUrls';
28 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 + */
29 97 public function __construct(
30 98 Php $php,
31 99 Wordpress $wordpress,
32 100 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
101 + MainConfig $mainConfig,
102 + Database $database,
103 + Util $util,
104 + Cache $cache,
105 + ObjectHandler $objectHandler,
106 + AccessHandler $accessHandler,
107 + FileHandler $fileHandler,
108 + FileObjectFactory $fileObjectFactory
41 109 ) {
42 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;
43 119 }
44 120
45 - protected function getWordpress(): Wordpress
121 + /**
122 + * @return Wordpress
123 + */
124 + protected function getWordpress()
46 125 {
47 126 return $this->wordpress;
48 127 }
49 128
50 - 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)
51 137 {
52 138 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
53 139
54 140 if (isset($postUrls[$url]) === true) {
@@ -55,26 +141,27 @@
55 141 return $postUrls[$url];
56 142 }
57 143
58 144 //Filter size
59 - $newUrlPieces = preg_split('/-[0-9]+x[0-9]+(_[a-z])?/', $url);
60 - $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0] . $newUrlPieces[1] : $newUrlPieces[0];
61 - $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);
62 148
63 - $postId = $this->wordpress->attachmentUrlToPostId($newUrl);
64 -
65 - if ($postId === 0) {
66 - $newUrl = preg_replace('/(\\.[^.\\s]{3,4})$/', '-scaled$1', $newUrl);
67 - $postId = $this->wordpress->attachmentUrlToPostId($newUrl);
68 - }
69 -
70 - $postUrls[$url] = $postId;
149 + $postUrls[$url] = $this->wordpress->attachmentUrlToPostId($newUrl);
71 150 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
72 151
73 152 return $postUrls[$url];
74 153 }
75 154
76 - private function getFileSettingsByType(string $objectType, string $objectUrl): ?FileObject
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)
77 164 {
78 165 $fileObject = null;
79 166
80 167 if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
@@ -79,23 +166,24 @@
79 166
80 167 if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
81 168 $uploadDirs = $this->wordpress->getUploadDir();
82 169 $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
83 - $regex = '/.*' . str_replace('/', '\/', $uploadDir) . '\//i';
170 + $regex = '/.*'.str_replace('/', '\/', $uploadDir).'\//i';
84 171 $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
85 172 $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
86 - $objectUrl = rtrim($uploadUrl, '/') . '/' . ltrim($cleanObjectUrl, '/');
173 + $objectUrl = rtrim($uploadUrl, '/').'/'.ltrim($cleanObjectUrl, '/');
87 174
88 175 $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
89 - $postType = $post->post_type ?? '';
90 176
91 - if ($postType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
177 + if ($post !== false
178 + && $post->post_type === ObjectHandler::ATTACHMENT_OBJECT_TYPE
179 + ) {
92 180 $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
93 181
94 182 $fileObject = $this->fileObjectFactory->createFileObject(
95 183 $post->ID,
96 184 $objectType,
97 - $uploadDirs['basedir'] . str_replace($multiPath, '', $objectUrl),
185 + $uploadDirs['basedir'].str_replace($multiPath, '', $objectUrl),
98 186 $this->wordpress->attachmentIsImage($post->ID)
99 187 );
100 188 }
101 189 } else {
@@ -113,11 +201,14 @@
113 201 return $fileObject;
114 202 }
115 203
116 204 /**
117 - * @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.
118 209 */
119 - public function getFile(string $objectType, string $objectUrl): void
210 + public function getFile($objectType, $objectUrl)
120 211 {
121 212 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
122 213
123 214 if ($fileObject === null) {
@@ -130,9 +221,9 @@
130 221 if ($this->mainConfig->getNoAccessImageType() === 'custom') {
131 222 $file = $this->mainConfig->getCustomNoAccessImage();
132 223 } else {
133 224 $realPath = $this->wordpressConfig->getRealPath();
134 - $file = $realPath . 'assets' . DIRECTORY_SEPARATOR . 'gfx' . DIRECTORY_SEPARATOR . 'noAccessPic.png';
225 + $file = $realPath.'assets'.DIRECTORY_SEPARATOR.'gfx'.DIRECTORY_SEPARATOR.'noAccessPic.png';
135 226 }
136 227 } else {
137 228 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
138 229 return;
@@ -140,9 +231,16 @@
140 231
141 232 $this->fileHandler->getFile($file, $fileObject->isImage());
142 233 }
143 234
144 - 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)
145 243 {
146 244 $permalink = null;
147 245 $redirect = $this->mainConfig->getRedirect();
148 246
@@ -166,14 +264,16 @@
166 264 return $url;
167 265 }
168 266
169 267 /**
170 - * @throws UserGroupTypeException
268 + * Redirects the user to his destination.
269 + *
270 + * @param bool $checkPosts
171 271 */
172 - public function redirectUser(bool $checkPosts = true): void
272 + public function redirectUser($checkPosts = true)
173 273 {
174 274 if ($checkPosts === true) {
175 - $posts = $this->wordpress->getWpQuery()->get_posts();
275 + $posts = (array)$this->wordpress->getWpQuery()->get_posts();
176 276
177 277 foreach ($posts as $post) {
178 278 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
179 279 return;
@@ -189,9 +289,16 @@
189 289 $this->php->callExit();
190 290 }
191 291 }
192 292
193 - 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)
194 301 {
195 302 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
196 303
197 304 $query = $this->database->prepare(
@@ -197,16 +304,23 @@
197 304 $query = $this->database->prepare(
198 305 "SELECT ID
199 306 FROM {$this->database->getPostsTable()}
200 307 WHERE post_name = %s
201 - AND post_type IN ('$postableTypes')",
308 + AND post_type IN ('{$postableTypes}')",
202 309 $name
203 310 );
204 311
205 - return (int) $this->database->getVariable($query);
312 + return (int)$this->database->getVariable($query);
206 313 }
207 314
208 - 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)
209 323 {
210 324 $objectType = null;
211 325 $objectId = null;
212 326
@@ -229,18 +343,23 @@
229 343 } elseif (isset($pageParams->query_vars['pagename']) === true) {
230 344 $object = $this->wordpress->getPageByPath($pageParams->query_vars['pagename']);
231 345
232 346 if ($object !== null) {
233 - $objectType = $object->post_type ?? null;
234 - $objectId = $object->ID ?? null;
347 + $objectType = $object->post_type;
348 + $objectId = $object->ID;
235 349 }
236 350 }
237 351 }
238 352
239 353 /**
240 - * @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
241 360 */
242 - public function redirect(?array $headers, mixed $pageParams): ?array
361 + public function redirect($headers, $pageParams)
243 362 {
244 363 $fileUrl = $this->getRequestParameter('uamgetfile');
245 364 $fileType = $this->getRequestParameter('uamfiletype');
246 365
@@ -258,9 +377,17 @@
258 377
259 378 return $headers;
260 379 }
261 380
262 - 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)
263 390 {
264 391 // Nginx always supports real urls so we need the new urls only
265 392 // if we don't use nginx and mod_rewrite is disabled
266 393 if ($this->mainConfig->lockFile() === true
@@ -268,17 +395,17 @@
268 395 && $this->wordpress->gotModRewrite() === false
269 396 ) {
270 397 $post = $this->objectHandler->getPost($id);
271 398
272 - if ($post !== false) {
399 + if ($post !== null) {
273 400 $type = explode('/', $post->post_mime_type);
274 - $type = $type[1] ?? $type[0];
401 + $type = (isset($type[1]) === true) ? $type[1] : $type[0];
275 402
276 403 $lockedFileTypes = $this->mainConfig->getLockedFiles();
277 404 $fileTypes = explode(',', $lockedFileTypes);
278 405
279 406 if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) {
280 - $url = $this->wordpress->getHomeUrl('/') . '?uamfiletype=attachment&uamgetfile=' . $url;
407 + $url = $this->wordpress->getHomeUrl('/').'?uamfiletype=attachment&uamgetfile='.$url;
281 408 }
282 409 }
283 410 }
284 411
@@ -284,18 +411,28 @@
284 411
285 412 return $url;
286 413 }
287 414
288 - 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)
289 424 {
290 - $postUrls = (array) $this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
425 + $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
291 426 $postUrls[$url] = $post->ID;
292 427 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
293 428 return $url;
294 429 }
295 430
296 - #[NoReturn]
297 - public function testXSendFile(): void
431 + /**
432 + * Tries to load the file via x send file
433 + */
434 + public function testXSendFile()
298 435 {
299 436 $this->fileHandler->deliverXSendFileTestFile();
300 437 }
301 438 }