PluginProbe
User Access Manager / 2.1.10
User Access Manager v2.1.10
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
user-access-manager / src / Controller / Frontend / RedirectController.php

RedirectController.php in User Access Manager 2.1.10, at src/Controller/Frontend/RedirectController.php

436 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
15 namespace UserAccessManager\Controller\Frontend;
16
17 use UserAccessManager\Access\AccessHandler;
18 use UserAccessManager\Cache\Cache;
19 use UserAccessManager\Config\MainConfig;
20 use UserAccessManager\Config\WordpressConfig;
21 use UserAccessManager\Controller\Controller;
22 use UserAccessManager\Database\Database;
23 use UserAccessManager\File\FileHandler;
24 use UserAccessManager\File\FileObject;
25 use UserAccessManager\File\FileObjectFactory;
26 use UserAccessManager\Object\ObjectHandler;
27 use UserAccessManager\Util\Util;
28 use UserAccessManager\Wrapper\Php;
29 use UserAccessManager\Wrapper\Wordpress;
30
31 /**
32 * Class FrontendRedirectController
33 *
34 * @package UserAccessManager\Controller
35 */
36 class RedirectController extends Controller
37 {
38 use LoginControllerTrait;
39
40 const POST_URL_CACHE_KEY = 'PostUrls';
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 */
97 public function __construct(
98 Php $php,
99 Wordpress $wordpress,
100 WordpressConfig $wordpressConfig,
101 MainConfig $mainConfig,
102 Database $database,
103 Util $util,
104 Cache $cache,
105 ObjectHandler $objectHandler,
106 AccessHandler $accessHandler,
107 FileHandler $fileHandler,
108 FileObjectFactory $fileObjectFactory
109 ) {
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;
119 }
120
121 /**
122 * @return Wordpress
123 */
124 protected function getWordpress()
125 {
126 return $this->wordpress;
127 }
128
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)
137 {
138 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
139
140 if (isset($postUrls[$url]) === true) {
141 return $postUrls[$url];
142 }
143
144 //Filter size
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);
148
149 $postUrls[$url] = $this->wordpress->attachmentUrlToPostId($newUrl);
150 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
151
152 return $postUrls[$url];
153 }
154
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)
164 {
165 $fileObject = null;
166
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, '/');
174
175 $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
176
177 if ($post !== false
178 && $post->post_type === ObjectHandler::ATTACHMENT_OBJECT_TYPE
179 ) {
180 $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
181
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');
191
192 $fileObject = $this->wordpress->applyFilters(
193 'uam_get_file_settings_by_type',
194 $fileObject,
195 $objectType,
196 $objectUrl,
197 $extraParameter
198 );
199 }
200
201 return $fileObject;
202 }
203
204 /**
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.
209 */
210 public function getFile($objectType, $objectUrl)
211 {
212 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
213
214 if ($fileObject === null) {
215 return;
216 }
217
218 if ($this->accessHandler->checkObjectAccess($fileObject->getType(), $fileObject->getId()) === true) {
219 $file = $fileObject->getFile();
220 } elseif ($fileObject->isImage() === true) {
221 if ($this->mainConfig->getNoAccessImageType() === 'custom') {
222 $file = $this->mainConfig->getCustomNoAccessImage();
223 } else {
224 $realPath = $this->wordpressConfig->getRealPath();
225 $file = $realPath.'assets'.DIRECTORY_SEPARATOR.'gfx'.DIRECTORY_SEPARATOR.'noAccessPic.png';
226 }
227 } else {
228 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
229 return;
230 }
231
232 $this->fileHandler->getFile($file, $fileObject->isImage());
233 }
234
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)
243 {
244 $permalink = null;
245 $redirect = $this->mainConfig->getRedirect();
246
247 if ($redirect === 'custom_page') {
248 $redirectCustomPage = $this->mainConfig->getRedirectCustomPage();
249 $post = $this->objectHandler->getPost($redirectCustomPage);
250 $url = null;
251
252 if ($post !== false) {
253 $url = $post->guid;
254 $permalink = $this->wordpress->getPageLink($post);
255 }
256 } elseif ($redirect === 'custom_url') {
257 $url = $this->mainConfig->getRedirectCustomUrl();
258 } elseif ($redirect === 'login') {
259 $url = $this->getLoginUrl();
260 } else {
261 $url = $this->wordpress->getHomeUrl('/');
262 }
263
264 return $url;
265 }
266
267 /**
268 * Redirects the user to his destination.
269 *
270 * @param bool $checkPosts
271 */
272 public function redirectUser($checkPosts = true)
273 {
274 if ($checkPosts === true) {
275 $posts = (array)$this->wordpress->getWpQuery()->get_posts();
276
277 foreach ($posts as $post) {
278 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
279 return;
280 }
281 }
282 }
283
284 $url = $this->getRedirectUrlAndPermalink($permalink);
285 $currentUrl = $this->util->getCurrentUrl();
286
287 if ($url !== null && $url !== $currentUrl && $permalink !== $currentUrl) {
288 $this->wordpress->wpRedirect($url);
289 $this->php->callExit();
290 }
291 }
292
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)
301 {
302 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
303
304 $query = $this->database->prepare(
305 "SELECT ID
306 FROM {$this->database->getPostsTable()}
307 WHERE post_name = %s
308 AND post_type IN ('{$postableTypes}')",
309 $name
310 );
311
312 return (int)$this->database->getVariable($query);
313 }
314
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)
323 {
324 $objectType = null;
325 $objectId = null;
326
327 $simpleTypes = [
328 'p' => ObjectHandler::GENERAL_POST_OBJECT_TYPE,
329 'page_id' => ObjectHandler::GENERAL_POST_OBJECT_TYPE,
330 'cat_id' => ObjectHandler::GENERAL_TERM_OBJECT_TYPE
331 ];
332
333 foreach ($simpleTypes as $queryVar => $newObjectType) {
334 if (isset($pageParams->query_vars[$queryVar]) === true) {
335 $objectType = $newObjectType;
336 $objectId = $pageParams->query_vars[$queryVar];
337 }
338 }
339
340 if (isset($pageParams->query_vars['name']) === true) {
341 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
342 $objectId = $this->getPostIdByName($pageParams->query_vars['name']);
343 } elseif (isset($pageParams->query_vars['pagename']) === true) {
344 $object = $this->wordpress->getPageByPath($pageParams->query_vars['pagename']);
345
346 if ($object !== null) {
347 $objectType = $object->post_type;
348 $objectId = $object->ID;
349 }
350 }
351 }
352
353 /**
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
360 */
361 public function redirect($headers, $pageParams)
362 {
363 $fileUrl = $this->getRequestParameter('uamgetfile');
364 $fileType = $this->getRequestParameter('uamfiletype');
365
366 if ($fileUrl !== null && $fileType !== null) {
367 $this->getFile($fileType, $fileUrl);
368 } elseif ($this->wordpressConfig->atAdminPanel() === false
369 && $this->mainConfig->getRedirect() !== 'false'
370 ) {
371 $this->extractObjectTypeAndId($pageParams, $objectType, $objectId);
372
373 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
374 $this->redirectUser(false);
375 }
376 }
377
378 return $headers;
379 }
380
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)
390 {
391 if ($this->mainConfig->lockFile() === true
392 && ($this->wordpress->isNginx() === false || $this->wordpress->gotModRewrite() === false)
393 ) {
394 $post = $this->objectHandler->getPost($id);
395
396 if ($post !== null) {
397 $type = explode('/', $post->post_mime_type);
398 $type = (isset($type[1]) === true) ? $type[1] : $type[0];
399
400 $lockedFileTypes = $this->mainConfig->getLockedFiles();
401 $fileTypes = explode(',', $lockedFileTypes);
402
403 if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) {
404 $url = $this->wordpress->getHomeUrl('/').'?uamfiletype=attachment&uamgetfile='.$url;
405 }
406 }
407 }
408
409 return $url;
410 }
411
412 /**
413 * Caches the urls for the post for a later lookup.
414 *
415 * @param string $url The url of the post.
416 * @param object $post The post object.
417 *
418 * @return string
419 */
420 public function cachePostLinks($url, $post)
421 {
422 $postUrls = (array)$this->cache->getFromRuntimeCache(self::POST_URL_CACHE_KEY);
423 $postUrls[$url] = $post->ID;
424 $this->cache->addToRuntimeCache(self::POST_URL_CACHE_KEY, $postUrls);
425 return $url;
426 }
427
428 /**
429 * Tries to load the file via x send file
430 */
431 public function testXSendFile()
432 {
433 $this->fileHandler->deliverXSendFileTestFile();
434 }
435 }
436