| @@ -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 | |
| @@ -59,21 +146,20 @@ | ||
| 59 | 146 | $newUrlPieces = preg_split('/-[0-9]+x[0-9]+(_[a-z])?/', $url); |
| 60 | 147 | $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0] . $newUrlPieces[1] : $newUrlPieces[0]; |
| 61 | 148 | $newUrl = preg_replace('/-pdf\.jpg$/', '.pdf', $newUrl); |
| 62 | 149 | |
| 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; | |
| 150 | + $postUrls[$url] = $this->wordpress->attachmentUrlToPostId($newUrl); | |
| 71 | 151 | $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls); |
| 72 | 152 | |
| 73 | 153 | return $postUrls[$url]; |
| 74 | 154 | } |
| 75 | 155 | |
| 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 | + */ | |
| 76 | 162 | private function getFileSettingsByType(string $objectType, string $objectUrl): ?FileObject |
| 77 | 163 | { |
| 78 | 164 | $fileObject = null; |
| 79 | 165 | |
| @@ -113,11 +199,14 @@ | ||
| 113 | 199 | return $fileObject; |
| 114 | 200 | } |
| 115 | 201 | |
| 116 | 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. | |
| 117 | 206 | * @throws UserGroupTypeException |
| 118 | 207 | */ |
| 119 | - public function getFile(string $objectType, string $objectUrl): void | |
| 208 | + public function getFile(string $objectType, string $objectUrl) | |
| 120 | 209 | { |
| 121 | 210 | $fileObject = $this->getFileSettingsByType($objectType, $objectUrl); |
| 122 | 211 | |
| 123 | 212 | if ($fileObject === null) { |
| @@ -140,8 +229,13 @@ | ||
| 140 | 229 | |
| 141 | 230 | $this->fileHandler->getFile($file, $fileObject->isImage()); |
| 142 | 231 | } |
| 143 | 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 | + */ | |
| 144 | 238 | private function getRedirectUrlAndPermalink(?string &$permalink): ?string |
| 145 | 239 | { |
| 146 | 240 | $permalink = null; |
| 147 | 241 | $redirect = $this->mainConfig->getRedirect(); |
| @@ -166,14 +260,16 @@ | ||
| 166 | 260 | return $url; |
| 167 | 261 | } |
| 168 | 262 | |
| 169 | 263 | /** |
| 264 | + * Redirects the user to his destination. | |
| 265 | + * @param bool $checkPosts | |
| 170 | 266 | * @throws UserGroupTypeException |
| 171 | 267 | */ |
| 172 | - public function redirectUser(bool $checkPosts = true): void | |
| 268 | + public function redirectUser($checkPosts = true) | |
| 173 | 269 | { |
| 174 | 270 | if ($checkPosts === true) { |
| 175 | - $posts = $this->wordpress->getWpQuery()->get_posts(); | |
| 271 | + $posts = (array)$this->wordpress->getWpQuery()->get_posts(); | |
| 176 | 272 | |
| 177 | 273 | foreach ($posts as $post) { |
| 178 | 274 | if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) { |
| 179 | 275 | return; |
| @@ -189,8 +285,13 @@ | ||
| 189 | 285 | $this->php->callExit(); |
| 190 | 286 | } |
| 191 | 287 | } |
| 192 | 288 | |
| 289 | + /** | |
| 290 | + * Returns the post id by the post name. | |
| 291 | + * @param string $name | |
| 292 | + * @return int | |
| 293 | + */ | |
| 193 | 294 | private function getPostIdByName(string $name): int |
| 194 | 295 | { |
| 195 | 296 | $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes()); |
| 196 | 297 | |
| @@ -197,9 +298,9 @@ | ||
| 197 | 298 | $query = $this->database->prepare( |
| 198 | 299 | "SELECT ID |
| 199 | 300 | FROM {$this->database->getPostsTable()} |
| 200 | 301 | WHERE post_name = %s |
| 201 | - AND post_type IN ('$postableTypes')", | |
| 302 | + AND post_type IN ('{$postableTypes}')", | |
| 202 | 303 | $name |
| 203 | 304 | ); |
| 204 | 305 | |
| 205 | 306 | return (int) $this->database->getVariable($query); |
| @@ -204,9 +305,15 @@ | ||
| 204 | 305 | |
| 205 | 306 | return (int) $this->database->getVariable($query); |
| 206 | 307 | } |
| 207 | 308 | |
| 208 | - 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) | |
| 209 | 316 | { |
| 210 | 317 | $objectType = null; |
| 211 | 318 | $objectId = null; |
| 212 | 319 | |
| @@ -236,11 +343,15 @@ | ||
| 236 | 343 | } |
| 237 | 344 | } |
| 238 | 345 | |
| 239 | 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 | |
| 240 | 351 | * @throws UserGroupTypeException |
| 241 | 352 | */ |
| 242 | - public function redirect(?array $headers, mixed $pageParams): ?array | |
| 353 | + public function redirect(array $headers, object $pageParams): array | |
| 243 | 354 | { |
| 244 | 355 | $fileUrl = $this->getRequestParameter('uamgetfile'); |
| 245 | 356 | $fileType = $this->getRequestParameter('uamfiletype'); |
| 246 | 357 | |
| @@ -258,9 +369,15 @@ | ||
| 258 | 369 | |
| 259 | 370 | return $headers; |
| 260 | 371 | } |
| 261 | 372 | |
| 262 | - 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 | |
| 263 | 380 | { |
| 264 | 381 | // Nginx always supports real urls so we need the new urls only |
| 265 | 382 | // if we don't use nginx and mod_rewrite is disabled |
| 266 | 383 | if ($this->mainConfig->lockFile() === true |
| @@ -268,9 +385,9 @@ | ||
| 268 | 385 | && $this->wordpress->gotModRewrite() === false |
| 269 | 386 | ) { |
| 270 | 387 | $post = $this->objectHandler->getPost($id); |
| 271 | 388 | |
| 272 | - if ($post !== false) { | |
| 389 | + if ($post !== null) { | |
| 273 | 390 | $type = explode('/', $post->post_mime_type); |
| 274 | 391 | $type = $type[1] ?? $type[0]; |
| 275 | 392 | |
| 276 | 393 | $lockedFileTypes = $this->mainConfig->getLockedFiles(); |
| @@ -284,8 +401,14 @@ | ||
| 284 | 401 | |
| 285 | 402 | return $url; |
| 286 | 403 | } |
| 287 | 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 | + */ | |
| 288 | 411 | public function cachePostLinks(string $url, object $post): string |
| 289 | 412 | { |
| 290 | 413 | $postUrls = (array) $this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY); |
| 291 | 414 | $postUrls[$url] = $post->ID; |
| @@ -292,10 +415,12 @@ | ||
| 292 | 415 | $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls); |
| 293 | 416 | return $url; |
| 294 | 417 | } |
| 295 | 418 | |
| 296 | - #[NoReturn] | |
| 297 | - public function testXSendFile(): void | |
| 419 | + /** | |
| 420 | + * Tries to load the file via x send file | |
| 421 | + */ | |
| 422 | + public function testXSendFile() | |
| 298 | 423 | { |
| 299 | 424 | $this->fileHandler->deliverXSendFileTestFile(); |
| 300 | 425 | } |
| 301 | 426 | } |