| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace UserAccessManager\Controller\Frontend; |
| 6 |
|
| 7 |
use JetBrains\PhpStorm\NoReturn; |
| 8 |
use UserAccessManager\Access\AccessHandler; |
| 9 |
use UserAccessManager\Cache\Cache; |
| 10 |
use UserAccessManager\Config\MainConfig; |
| 11 |
use UserAccessManager\Config\WordpressConfig; |
| 12 |
use UserAccessManager\Controller\Controller; |
| 13 |
use UserAccessManager\Database\Database; |
| 14 |
use UserAccessManager\File\FileHandler; |
| 15 |
use UserAccessManager\File\FileObject; |
| 16 |
use UserAccessManager\File\FileObjectFactory; |
| 17 |
use UserAccessManager\Object\ObjectHandler; |
| 18 |
use UserAccessManager\UserGroup\UserGroupTypeException; |
| 19 |
use UserAccessManager\Util\Util; |
| 20 |
use UserAccessManager\Wrapper\Php; |
| 21 |
use UserAccessManager\Wrapper\Wordpress; |
| 22 |
|
| 23 |
class RedirectController extends Controller |
| 24 |
{ |
| 25 |
use LoginControllerTrait; |
| 26 |
|
| 27 |
public const POST_URL_CACHE_KEY = 'PostUrls'; |
| 28 |
|
| 29 |
public function __construct( |
| 30 |
Php $php, |
| 31 |
Wordpress $wordpress, |
| 32 |
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 |
| 41 |
) { |
| 42 |
parent::__construct($php, $wordpress, $wordpressConfig); |
| 43 |
} |
| 44 |
|
| 45 |
protected function getWordpress(): Wordpress |
| 46 |
{ |
| 47 |
return $this->wordpress; |
| 48 |
} |
| 49 |
|
| 50 |
public function getPostIdByUrl(string $url): int |
| 51 |
{ |
| 52 |
$postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY); |
| 53 |
|
| 54 |
if (isset($postUrls[$url]) === true) { |
| 55 |
return $postUrls[$url]; |
| 56 |
} |
| 57 |
|
| 58 |
//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); |
| 62 |
|
| 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; |
| 71 |
$this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls); |
| 72 |
|
| 73 |
return $postUrls[$url]; |
| 74 |
} |
| 75 |
|
| 76 |
private function getFileSettingsByType(string $objectType, string $objectUrl): ?FileObject |
| 77 |
{ |
| 78 |
$fileObject = null; |
| 79 |
|
| 80 |
if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) { |
| 81 |
$uploadDirs = $this->wordpress->getUploadDir(); |
| 82 |
$uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']); |
| 83 |
$regex = '/.*' . str_replace('/', '\/', $uploadDir) . '\//i'; |
| 84 |
$cleanObjectUrl = preg_replace($regex, '', $objectUrl); |
| 85 |
$uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']); |
| 86 |
$objectUrl = rtrim($uploadUrl, '/') . '/' . ltrim($cleanObjectUrl, '/'); |
| 87 |
|
| 88 |
$post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl)); |
| 89 |
$postType = $post->post_type ?? ''; |
| 90 |
|
| 91 |
if ($postType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) { |
| 92 |
$multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']); |
| 93 |
|
| 94 |
$fileObject = $this->fileObjectFactory->createFileObject( |
| 95 |
$post->ID, |
| 96 |
$objectType, |
| 97 |
$uploadDirs['basedir'] . str_replace($multiPath, '', $objectUrl), |
| 98 |
$this->wordpress->attachmentIsImage($post->ID) |
| 99 |
); |
| 100 |
} |
| 101 |
} else { |
| 102 |
$extraParameter = $this->getRequestParameter('uamextra'); |
| 103 |
|
| 104 |
$fileObject = $this->wordpress->applyFilters( |
| 105 |
'uam_get_file_settings_by_type', |
| 106 |
$fileObject, |
| 107 |
$objectType, |
| 108 |
$objectUrl, |
| 109 |
$extraParameter |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
return $fileObject; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* @throws UserGroupTypeException |
| 118 |
*/ |
| 119 |
public function getFile(string $objectType, string $objectUrl): void |
| 120 |
{ |
| 121 |
$fileObject = $this->getFileSettingsByType($objectType, $objectUrl); |
| 122 |
|
| 123 |
if ($fileObject === null) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
if ($this->accessHandler->checkObjectAccess($fileObject->getType(), $fileObject->getId()) === true) { |
| 128 |
$file = $fileObject->getFile(); |
| 129 |
} elseif ($fileObject->isImage() === true) { |
| 130 |
if ($this->mainConfig->getNoAccessImageType() === 'custom') { |
| 131 |
$file = $this->mainConfig->getCustomNoAccessImage(); |
| 132 |
} else { |
| 133 |
$realPath = $this->wordpressConfig->getRealPath(); |
| 134 |
$file = $realPath . 'assets' . DIRECTORY_SEPARATOR . 'gfx' . DIRECTORY_SEPARATOR . 'noAccessPic.png'; |
| 135 |
} |
| 136 |
} else { |
| 137 |
$this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
$this->fileHandler->getFile($file, $fileObject->isImage()); |
| 142 |
} |
| 143 |
|
| 144 |
private function getRedirectUrlAndPermalink(?string &$permalink): ?string |
| 145 |
{ |
| 146 |
$permalink = null; |
| 147 |
$redirect = $this->mainConfig->getRedirect(); |
| 148 |
|
| 149 |
if ($redirect === 'custom_page') { |
| 150 |
$redirectCustomPage = $this->mainConfig->getRedirectCustomPage(); |
| 151 |
$post = $this->objectHandler->getPost($redirectCustomPage); |
| 152 |
$url = null; |
| 153 |
|
| 154 |
if ($post !== false) { |
| 155 |
$url = $post->guid; |
| 156 |
$permalink = $this->wordpress->getPageLink($post); |
| 157 |
} |
| 158 |
} elseif ($redirect === 'custom_url') { |
| 159 |
$url = $this->mainConfig->getRedirectCustomUrl(); |
| 160 |
} elseif ($redirect === 'login') { |
| 161 |
$url = $this->getLoginUrl(); |
| 162 |
} else { |
| 163 |
$url = $this->wordpress->getHomeUrl('/'); |
| 164 |
} |
| 165 |
|
| 166 |
return $url; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* @throws UserGroupTypeException |
| 171 |
*/ |
| 172 |
public function redirectUser(bool $checkPosts = true): void |
| 173 |
{ |
| 174 |
if ($checkPosts === true) { |
| 175 |
$posts = $this->wordpress->getWpQuery()->get_posts(); |
| 176 |
|
| 177 |
foreach ($posts as $post) { |
| 178 |
if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) { |
| 179 |
return; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$url = $this->getRedirectUrlAndPermalink($permalink); |
| 185 |
$currentUrl = $this->util->getCurrentUrl(); |
| 186 |
|
| 187 |
if ($url !== null && $url !== $currentUrl && $permalink !== $currentUrl) { |
| 188 |
$this->wordpress->wpRedirect($url); |
| 189 |
$this->php->callExit(); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
private function getPostIdByName(string $name): int |
| 194 |
{ |
| 195 |
$postableTypes = implode('\',\'', $this->objectHandler->getPostTypes()); |
| 196 |
|
| 197 |
$query = $this->database->prepare( |
| 198 |
"SELECT ID |
| 199 |
FROM {$this->database->getPostsTable()} |
| 200 |
WHERE post_name = %s |
| 201 |
AND post_type IN ('$postableTypes')", |
| 202 |
$name |
| 203 |
); |
| 204 |
|
| 205 |
return (int) $this->database->getVariable($query); |
| 206 |
} |
| 207 |
|
| 208 |
private function extractObjectTypeAndId(mixed $pageParams, ?string &$objectType, int|string|null &$objectId): void |
| 209 |
{ |
| 210 |
$objectType = null; |
| 211 |
$objectId = null; |
| 212 |
|
| 213 |
$simpleTypes = [ |
| 214 |
'p' => ObjectHandler::GENERAL_POST_OBJECT_TYPE, |
| 215 |
'page_id' => ObjectHandler::GENERAL_POST_OBJECT_TYPE, |
| 216 |
'cat_id' => ObjectHandler::GENERAL_TERM_OBJECT_TYPE |
| 217 |
]; |
| 218 |
|
| 219 |
foreach ($simpleTypes as $queryVar => $newObjectType) { |
| 220 |
if (isset($pageParams->query_vars[$queryVar]) === true) { |
| 221 |
$objectType = $newObjectType; |
| 222 |
$objectId = $pageParams->query_vars[$queryVar]; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
if (isset($pageParams->query_vars['name']) === true) { |
| 227 |
$objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE; |
| 228 |
$objectId = $this->getPostIdByName($pageParams->query_vars['name']); |
| 229 |
} elseif (isset($pageParams->query_vars['pagename']) === true) { |
| 230 |
$object = $this->wordpress->getPageByPath($pageParams->query_vars['pagename']); |
| 231 |
|
| 232 |
if ($object !== null) { |
| 233 |
$objectType = $object->post_type ?? null; |
| 234 |
$objectId = $object->ID ?? null; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* @throws UserGroupTypeException |
| 241 |
*/ |
| 242 |
public function redirect(?array $headers, mixed $pageParams): ?array |
| 243 |
{ |
| 244 |
$fileUrl = $this->getRequestParameter('uamgetfile'); |
| 245 |
$fileType = $this->getRequestParameter('uamfiletype'); |
| 246 |
|
| 247 |
if ($fileUrl !== null && $fileType !== null) { |
| 248 |
$this->getFile($fileType, $fileUrl); |
| 249 |
} elseif ($this->wordpressConfig->atAdminPanel() === false |
| 250 |
&& $this->mainConfig->getRedirect() !== 'false' |
| 251 |
) { |
| 252 |
$this->extractObjectTypeAndId($pageParams, $objectType, $objectId); |
| 253 |
|
| 254 |
if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) { |
| 255 |
$this->redirectUser(false); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
return $headers; |
| 260 |
} |
| 261 |
|
| 262 |
public function getFileUrl(string $url, int|string|null $id): string |
| 263 |
{ |
| 264 |
// Nginx always supports real urls so we need the new urls only |
| 265 |
// if we don't use nginx and mod_rewrite is disabled |
| 266 |
if ($this->mainConfig->lockFile() === true |
| 267 |
&& $this->wordpress->isNginx() === false |
| 268 |
&& $this->wordpress->gotModRewrite() === false |
| 269 |
) { |
| 270 |
$post = $this->objectHandler->getPost($id); |
| 271 |
|
| 272 |
if ($post !== false) { |
| 273 |
$type = explode('/', $post->post_mime_type); |
| 274 |
$type = $type[1] ?? $type[0]; |
| 275 |
|
| 276 |
$lockedFileTypes = $this->mainConfig->getLockedFiles(); |
| 277 |
$fileTypes = explode(',', $lockedFileTypes); |
| 278 |
|
| 279 |
if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) { |
| 280 |
$url = $this->wordpress->getHomeUrl('/') . '?uamfiletype=attachment&uamgetfile=' . $url; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
return $url; |
| 286 |
} |
| 287 |
|
| 288 |
public function cachePostLinks(string $url, object $post): string |
| 289 |
{ |
| 290 |
$postUrls = (array) $this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY); |
| 291 |
$postUrls[$url] = $post->ID; |
| 292 |
$this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls); |
| 293 |
return $url; |
| 294 |
} |
| 295 |
|
| 296 |
#[NoReturn] |
| 297 |
public function testXSendFile(): void |
| 298 |
{ |
| 299 |
$this->fileHandler->deliverXSendFileTestFile(); |
| 300 |
} |
| 301 |
} |
| 302 |
|