PluginProbe
User Access Manager / 2.0.1
User Access Manager v2.0.1
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 / UserAccessManager / Controller / FrontendController.php

FrontendController.php in User Access Manager 2.0.1, at src/UserAccessManager/Controller/FrontendController.php

1,035 lines 30.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * FrontendController.php
4 *
5 * The FrontendController 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;
16
17 use UserAccessManager\AccessHandler\AccessHandler;
18 use UserAccessManager\Cache\Cache;
19 use UserAccessManager\Config\Config;
20 use UserAccessManager\Database\Database;
21 use UserAccessManager\FileHandler\FileHandler;
22 use UserAccessManager\FileHandler\FileObject;
23 use UserAccessManager\FileHandler\FileObjectFactory;
24 use UserAccessManager\ObjectHandler\ObjectHandler;
25 use UserAccessManager\UserAccessManager;
26 use UserAccessManager\UserGroup\UserGroup;
27 use UserAccessManager\Util\Util;
28 use UserAccessManager\Wrapper\Php;
29 use UserAccessManager\Wrapper\Wordpress;
30
31 /**
32 * Class FrontendController
33 *
34 * @package UserAccessManager\Controller
35 */
36 class FrontendController extends Controller
37 {
38 const HANDLE_STYLE_LOGIN_FORM = 'UserAccessManagerLoginForm';
39 const POST_URL_CACHE_KEY = 'PostUrls';
40 const POST_COUNTS_CACHE_KEY = 'WpPostCounts';
41
42 /**
43 * @var Database
44 */
45 private $database;
46
47 /**
48 * @var Cache
49 */
50 private $cache;
51
52 /**
53 * @var Util
54 */
55 private $util;
56
57 /**
58 * @var ObjectHandler
59 */
60 private $objectHandler;
61
62 /**
63 * @var AccessHandler
64 */
65 private $accessHandler;
66
67 /**
68 * @var FileHandler
69 */
70 private $fileHandler;
71
72 /**
73 * @var FileObjectFactory
74 */
75 private $fileObjectFactory;
76
77 /**
78 * FrontendController constructor.
79 *
80 * @param Php $php
81 * @param Wordpress $wordpress
82 * @param Config $config
83 * @param Database $database
84 * @param Util $util
85 * @param Cache $cache
86 * @param ObjectHandler $objectHandler
87 * @param AccessHandler $accessHandler
88 * @param FileHandler $fileHandler
89 * @param FileObjectFactory $fileObjectFactory
90 */
91 public function __construct(
92 Php $php,
93 Wordpress $wordpress,
94 Config $config,
95 Database $database,
96 Util $util,
97 Cache $cache,
98 ObjectHandler $objectHandler,
99 AccessHandler $accessHandler,
100 FileHandler $fileHandler,
101 FileObjectFactory $fileObjectFactory
102 ) {
103 parent::__construct($php, $wordpress, $config);
104 $this->database = $database;
105 $this->util = $util;
106 $this->cache = $cache;
107 $this->objectHandler = $objectHandler;
108 $this->accessHandler = $accessHandler;
109 $this->fileHandler = $fileHandler;
110 $this->fileObjectFactory = $fileObjectFactory;
111 }
112
113 /**
114 * Functions for other content.
115 */
116
117 /**
118 * Register all other styles.
119 */
120 private function registerStylesAndScripts()
121 {
122 $urlPath = $this->config->getUrlPath();
123
124 $this->wordpress->registerStyle(
125 self::HANDLE_STYLE_LOGIN_FORM,
126 $urlPath.'assets/css/uamLoginForm.css',
127 [],
128 UserAccessManager::VERSION,
129 'screen'
130 );
131 }
132
133 /**
134 * The function for the wp_enqueue_scripts action.
135 */
136 public function enqueueStylesAndScripts()
137 {
138 $this->registerStylesAndScripts();
139 $this->wordpress->enqueueStyle(self::HANDLE_STYLE_LOGIN_FORM);
140 }
141
142 /*
143 * Functions for the blog content.
144 */
145
146 /**
147 * Manipulates the wordpress query object to filter content.
148 *
149 * @param \WP_Query $wpQuery The wordpress query object.
150 */
151 public function parseQuery($wpQuery)
152 {
153 if (isset($wpQuery->query_vars['suppress_filters']) === true
154 && $wpQuery->query_vars['suppress_filters'] === true
155 ) {
156 $excludedPosts = $this->accessHandler->getExcludedPosts();
157
158 if (count($excludedPosts) > 0) {
159 $postsNotIn = (isset($wpQuery->query_vars['post__not_in']) === true) ?
160 $wpQuery->query_vars['post__not_in'] : [];
161
162 $wpQuery->query_vars['post__not_in'] = array_unique(
163 array_merge($postsNotIn, $excludedPosts)
164 );
165 }
166 }
167 }
168
169 /**
170 * Returns the admin hint.
171 *
172 * @param string $objectType The object type.
173 * @param integer $objectId The object id we want to check.
174 * @param string $text The text on which we want to append the hint.
175 *
176 * @return string
177 */
178 public function adminOutput($objectType, $objectId, $text = null)
179 {
180 $output = '';
181
182 if ($this->config->atAdminPanel() === false
183 && $this->config->blogAdminHint() === true
184 ) {
185 $hintText = $this->config->getBlogAdminHintText();
186
187 if ($text !== null && $this->util->endsWith($text, $hintText) === true) {
188 return $output;
189 }
190
191 if ($this->accessHandler->userIsAdmin($this->wordpress->getCurrentUser()->ID) === true
192 && count($this->accessHandler->getUserGroupsForObject($objectType, $objectId)) > 0
193 ) {
194 $output .= $hintText;
195 }
196 }
197
198 return $output;
199 }
200
201 /**
202 * Returns the login bar.
203 *
204 * @return string
205 */
206 public function getLoginFormHtml()
207 {
208 $loginForm = '';
209
210 if ($this->wordpress->isUserLoggedIn() === false) {
211 $loginForm = $this->getIncludeContents('LoginForm.php');
212 }
213
214 return $this->wordpress->applyFilters('uam_login_form', $loginForm);
215 }
216
217 /**
218 * Modifies the content of the post by the given settings.
219 *
220 * @param \WP_Post $post The current post.
221 * @param bool $locked
222 *
223 * @return null|\WP_Post
224 */
225 private function processPost(\WP_Post $post, &$locked = null)
226 {
227 $post->post_title .= $this->adminOutput($post->post_type, $post->ID);
228 $locked = ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false);
229
230 if ($locked === true) {
231 if ($this->config->hidePostType($post->post_type) === true
232 || $this->config->atAdminPanel() === true
233 ) {
234 return null;
235 }
236
237 $uamPostContent = $this->config->getPostTypeContent($post->post_type);
238 $uamPostContent = str_replace('[LOGIN_FORM]', $this->getLoginFormHtml(), $uamPostContent);
239
240 if ($post->post_type === 'post'
241 && $this->config->showPostContentBeforeMore() === true
242 && preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches)
243 ) {
244 $uamPostContent = explode($matches[0], $post->post_content)[0]." ".$uamPostContent;
245 }
246
247 $post->post_content = stripslashes($uamPostContent);
248
249 if ($this->config->hidePostTypeTitle($post->post_type) === true) {
250 $post->post_title = $this->config->getPostTypeTitle($post->post_type);
251 }
252
253 if ($this->config->hidePostTypeComments($post->post_type) === true) {
254 $post->comment_status = 'close';
255 }
256 }
257
258 return $post;
259 }
260
261 /**
262 * The function for the the_posts filter.
263 *
264 * @param array $posts The posts.
265 *
266 * @return array
267 */
268 public function showPosts($posts = [])
269 {
270 $showPosts = [];
271
272 if ($this->wordpress->isFeed() === false || $this->config->protectFeed() === true) {
273 foreach ($posts as $post) {
274 if ($post !== null) {
275 $post = $this->processPost($post);
276
277 if ($post !== null) {
278 $showPosts[] = $post;
279 }
280 }
281 }
282 }
283
284 return $showPosts;
285 }
286
287 /**
288 * The function for the get_pages filter.
289 *
290 * @param \WP_Post[] $pages The pages.
291 *
292 * @return array
293 */
294 public function showPages($pages = [])
295 {
296 $showPages = [];
297
298 foreach ($pages as $page) {
299 $page = $this->processPost($page);
300
301 if ($page !== null) {
302 $showPages[] = $page;
303 }
304 }
305
306 $pages = $showPages;
307
308 return $pages;
309 }
310
311 /**
312 * The function for the posts_where_paged filter.
313 *
314 * @param string $query The where sql statement.
315 *
316 * @return string
317 */
318 public function showPostSql($query)
319 {
320 $excludedPosts = $this->accessHandler->getExcludedPosts();
321
322 if (count($excludedPosts) > 0) {
323 $excludedPostsStr = implode(', ', $excludedPosts);
324 $query .= " AND {$this->database->getPostsTable()}.ID NOT IN ($excludedPostsStr) ";
325 }
326
327 return $query;
328 }
329
330 /**
331 * Function for the wp_count_posts filter.
332 *
333 * @param \stdClass $counts
334 * @param string $type
335 * @param string $perm
336 *
337 * @return \stdClass
338 */
339 public function showPostCount($counts, $type, $perm)
340 {
341 $cachedCounts = $this->cache->getFromCache(self::POST_COUNTS_CACHE_KEY);
342
343 if ($cachedCounts === null) {
344 $excludedPosts = $this->accessHandler->getExcludedPosts();
345
346 if (count($excludedPosts) > 0) {
347 $excludedPosts = implode('\', \'', $excludedPosts);
348
349 $query = "SELECT post_status, COUNT(*) AS num_posts
350 FROM {$this->database->getPostsTable()}
351 WHERE post_type = %s
352 AND ID NOT IN ('{$excludedPosts}')";
353
354 if ('readable' === $perm && $this->wordpress->isUserLoggedIn() === true) {
355 $postTypeObject = $this->wordpress->getPostTypeObject($type);
356
357 if ($this->wordpress->currentUserCan($postTypeObject->cap->read_private_posts) === false) {
358 $query .= $this->database->prepare(
359 ' AND (post_status != \'private\' OR (post_author = %d AND post_status = \'private\'))',
360 $this->wordpress->getCurrentUser()->ID
361 );
362 }
363 }
364
365 $query .= ' GROUP BY post_status';
366
367 $results = (array)$this->database->getResults(
368 $this->database->prepare($query, $type),
369 ARRAY_A
370 );
371
372 foreach ($results as $result) {
373 if (isset($counts->{$result['post_status']})) {
374 $counts->{$result['post_status']} = $result['num_posts'];
375 }
376 }
377 }
378
379 $cachedCounts = $counts;
380 $this->cache->addToCache(self::POST_COUNTS_CACHE_KEY, $cachedCounts);
381 }
382
383 return $cachedCounts;
384 }
385
386 /**
387 * Sets the excluded terms as argument.
388 *
389 * @param array $arguments
390 *
391 * @return array
392 */
393 public function getTermArguments(array $arguments)
394 {
395 $exclude = (isset($arguments['exclude']) === true) ?
396 $this->wordpress->parseIdList($arguments['exclude']) : [];
397 $arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms());
398 $arguments['exclude'] = array_unique($arguments['exclude']);
399
400 return $arguments;
401 }
402
403 /**
404 * The function for the comments_array filter.
405 *
406 * @param \WP_Comment[] $comments The comments.
407 *
408 * @return array
409 */
410 public function showComment($comments = [])
411 {
412 $showComments = [];
413
414 foreach ($comments as $comment) {
415 $post = $this->objectHandler->getPost($comment->comment_post_ID);
416
417 if ($post !== false
418 && $this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false
419 ) {
420 if ($this->config->hidePostTypeComments($post->post_type) === true
421 || $this->config->hidePostType($post->post_type) === true
422 || $this->config->atAdminPanel() === true
423 ) {
424 continue;
425 }
426
427 $comment->comment_content = $this->config->getPostTypeCommentContent($post->post_type);
428 }
429
430 $showComments[] = $comment;
431 }
432
433 return $showComments;
434 }
435
436 /**
437 * The function for the get_ancestors filter.
438 *
439 * @param array $ancestors
440 * @param int $objectId
441 * @param string $objectType
442 *
443 * @return array
444 */
445 public function showAncestors($ancestors, $objectId, $objectType)
446 {
447 if ($this->config->lockRecursive() === true
448 && $this->accessHandler->checkObjectAccess($objectType, $objectId) === false
449 ) {
450 return [];
451 }
452
453 foreach ($ancestors as $key => $ancestorId) {
454 if ($this->accessHandler->checkObjectAccess($objectType, $ancestorId) === false) {
455 unset($ancestors[$key]);
456 }
457 }
458
459 return $ancestors;
460 }
461
462 /**
463 * The function for the get_previous_post_where and
464 * the get_next_post_where filter.
465 *
466 * @param string $query The current sql string.
467 *
468 * @return string
469 */
470 public function showNextPreviousPost($query)
471 {
472 $excludedPosts = $this->accessHandler->getExcludedPosts();
473
474 if (count($excludedPosts) > 0) {
475 $excludedPosts = implode(', ', $excludedPosts);
476 $query .= " AND p.ID NOT IN ({$excludedPosts}) ";
477 }
478
479 return $query;
480 }
481
482 /**
483 * Returns the post count for the term.
484 *
485 * @param string $termType
486 * @param int $termId
487 *
488 * @return int
489 */
490 private function getVisibleElementsCount($termType, $termId)
491 {
492 $count = 0;
493
494 $terms = [$termId => $termId];
495 $termTreeMap = $this->objectHandler->getTermTreeMap();
496
497 if (isset($termTreeMap[ObjectHandler::TREE_MAP_CHILDREN][$termType]) === true
498 && isset($termTreeMap[ObjectHandler::TREE_MAP_CHILDREN][$termType][$termId]) === true
499 ) {
500 $terms += $termTreeMap[ObjectHandler::TREE_MAP_CHILDREN][$termType][$termId];
501 }
502
503 $posts = [];
504 $termPostMap = $this->objectHandler->getTermPostMap();
505
506 foreach ($terms as $termId) {
507 if (isset($termPostMap[$termId]) === true) {
508 $posts += $termPostMap[$termId];
509 }
510 }
511
512 foreach ($posts as $postId => $postType) {
513 if ($this->config->hidePostType($postType) === false
514 || $this->accessHandler->checkObjectAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId) === true
515 ) {
516 $count++;
517 }
518 }
519
520 return $count;
521 }
522
523 /**
524 * Modifies the content of the term by the given settings.
525 *
526 * @param \WP_Term $term The current term.
527 * @param bool $isEmpty
528 *
529 * @return mixed
530 */
531 private function processTerm($term, &$isEmpty = null)
532 {
533 $isEmpty = false;
534
535 if (($term instanceof \WP_Term) === false) {
536 return $term;
537 }
538
539 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
540 return null;
541 }
542
543 $term->name .= $this->adminOutput($term->taxonomy, $term->term_id, $term->name);
544 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
545
546 //For categories
547 if ($term->count <= 0
548 && $this->config->atAdminPanel() === false
549 && $this->config->hideEmptyTaxonomy($term->taxonomy) === true
550 ) {
551 $isEmpty = true;
552 }
553
554 if ($this->config->lockRecursive() === false) {
555 $currentTerm = $term;
556
557 while ($currentTerm->parent != 0) {
558 $currentTerm = $this->objectHandler->getTerm($currentTerm->parent);
559
560 if ($currentTerm === false) {
561 break;
562 }
563
564 $access = $this->accessHandler->checkObjectAccess(
565 $currentTerm->taxonomy,
566 $currentTerm->term_id
567 );
568
569 if ($access === true) {
570 $term->parent = $currentTerm->term_id;
571 break;
572 }
573 }
574 }
575
576 return $term;
577 }
578
579 /**
580 * The function for the get_term filter.
581 *
582 * @param \WP_Term $term
583 *
584 * @return null|object
585 */
586 public function showTerm($term)
587 {
588 return $this->processTerm($term);
589 }
590
591 /**
592 * The function for the get_terms filter.
593 *
594 * @param array $terms The terms.
595 *
596 * @return array
597 */
598 public function showTerms($terms = [])
599 {
600 foreach ($terms as $key => $term) {
601 $isNumeric = (is_numeric($term) === true);
602
603 if ($isNumeric === true) {
604 if ((int)$term === 0) {
605 unset($terms[$key]);
606 continue;
607 }
608
609 $term = $this->objectHandler->getTerm($term);
610 }
611
612 if (($term instanceof \WP_Term) === false) {
613 continue;
614 }
615
616 $term = $this->processTerm($term, $isEmpty);
617
618 if ($term !== null && $isEmpty === false) {
619 $terms[$key] = ($isNumeric === true) ? $term->term_id : $term;
620 } else {
621 unset($terms[$key]);
622 }
623 }
624
625 return $terms;
626 }
627
628 /**
629 * The function for the wp_get_nav_menu_items filter.
630 *
631 * @param array $items The menu item.
632 *
633 * @return array
634 */
635 public function showCustomMenu($items)
636 {
637 $showItems = [];
638
639 foreach ($items as $key => $item) {
640 $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
641
642 if ($this->objectHandler->isPostType($item->object) === true) {
643 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
644 if ($this->config->hidePostType($item->object) === true
645 || $this->config->atAdminPanel() === true
646 ) {
647 continue;
648 }
649
650 if ($this->config->hidePostTypeTitle($item->object) === true) {
651 $item->title = $this->config->getPostTypeTitle($item->object);
652 }
653 }
654
655 $showItems[$key] = $item;
656 } elseif ($this->objectHandler->isTaxonomy($item->object) === true) {
657 $object = $this->objectHandler->getTerm($item->object_id);
658 $category = $this->processTerm($object, $isEmpty);
659
660 if ($category !== null && $isEmpty === false) {
661 $showItems[$key] = $item;
662 }
663 } else {
664 $showItems[$key] = $item;
665 }
666 }
667
668 return $showItems;
669 }
670
671 /**
672 * The function for the edit_post_link filter.
673 *
674 * @param string $link The edit link.
675 * @param integer $postId The _iId of the post.
676 *
677 * @return string
678 */
679 public function showGroupMembership($link, $postId)
680 {
681 $userGroups = $this->accessHandler->getFilteredUserGroupsForObject(
682 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
683 $postId
684 );
685
686 if (count($userGroups) > 0) {
687 $escapedGroups = array_map(
688 function (UserGroup $group) {
689 return htmlentities($group->getName());
690 },
691 $userGroups
692 );
693
694 $link .= ' | '.TXT_UAM_ASSIGNED_GROUPS.': ';
695 $link .= implode(', ', $escapedGroups);
696 }
697
698 return $link;
699 }
700
701 /**
702 * Checks if we allowed show the login form.
703 *
704 * @return bool
705 */
706 public function showLoginForm()
707 {
708 return $this->wordpress->isSingle() === true || $this->wordpress->isPage() === true;
709 }
710
711 /**
712 * Returns the login url.
713 *
714 * @return mixed
715 */
716 public function getLoginUrl()
717 {
718 $loginUrl = $this->wordpress->getBlogInfo('wpurl').'/wp-login.php';
719 return $this->wordpress->applyFilters('uam_login_form_url', $loginUrl);
720 }
721
722 /**
723 * Returns the login redirect url.
724 *
725 * @return mixed
726 */
727 public function getRedirectLoginUrl()
728 {
729 $loginUrl = $this->wordpress->getBlogInfo('wpurl')
730 .'/wp-login.php?redirect_to='.urlencode($_SERVER['REQUEST_URI']);
731 return $this->wordpress->applyFilters('uam_login_url', $loginUrl);
732 }
733
734 /**
735 * Returns the user login name.
736 *
737 * @return string
738 */
739 public function getUserLogin()
740 {
741 $userLogin = $this->getRequestParameter('log');
742 return $this->wordpress->escHtml(stripslashes($userLogin));
743 }
744
745
746 /*
747 * Functions for the redirection and files.
748 */
749
750 /**
751 * Returns the post by the given url.
752 *
753 * @param string $url The url of the post(attachment).
754 *
755 * @return int
756 */
757 public function getPostIdByUrl($url)
758 {
759 $postUrls = (array)$this->cache->getFromCache(self::POST_URL_CACHE_KEY);
760
761 if (isset($postUrls[$url]) === true) {
762 return $postUrls[$url];
763 }
764
765 $postUrls[$url] = null;
766
767 //Filter edit string
768 $newUrlPieces = preg_split('/-e[0-9]{1,}/', $url);
769 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
770
771 //Filter size
772 $newUrlPieces = preg_split('/-[0-9]{1,}x[0-9]{1,}/', $newUrl);
773 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
774
775 $query = $this->database->prepare(
776 "SELECT ID
777 FROM {$this->database->getPostsTable()}
778 WHERE guid = '%s'
779 LIMIT 1",
780 $newUrl
781 );
782
783 $dbPost = $this->database->getRow($query);
784
785 if ($dbPost !== null) {
786 $postUrls[$url] = $dbPost->ID;
787 $this->cache->addToCache(self::POST_URL_CACHE_KEY, $postUrls);
788 }
789
790 return $postUrls[$url];
791 }
792
793 /**
794 * Returns the file object by the given type and url.
795 *
796 * @param string $objectType The type of the requested file.
797 * @param string $objectUrl The file url.
798 *
799 * @return null|FileObject
800 */
801 private function getFileSettingsByType($objectType, $objectUrl)
802 {
803 $fileObject = null;
804
805 if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
806 $uploadDirs = $this->wordpress->getUploadDir();
807 $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
808 $regex = '/.*'.str_replace('/', '\/', $uploadDir).'\//i';
809 $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
810 $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
811 $objectUrl = rtrim($uploadUrl, '/').'/'.ltrim($cleanObjectUrl, '/');
812
813 $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
814
815 if ($post !== false
816 && $post->post_type === ObjectHandler::ATTACHMENT_OBJECT_TYPE
817 ) {
818 $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
819
820 $fileObject = $this->fileObjectFactory->createFileObject(
821 $post->ID,
822 $objectType,
823 $uploadDirs['basedir'].str_replace($multiPath, '', $objectUrl),
824 $this->wordpress->attachmentIsImage($post->ID)
825 );
826 }
827 } else {
828 $extraParameter = $this->getRequestParameter('uamextra');
829
830 $fileObject = $this->wordpress->applyFilters(
831 'uam_get_file_settings_by_type',
832 $fileObject,
833 $objectType,
834 $objectUrl,
835 $extraParameter
836 );
837 }
838
839 return $fileObject;
840 }
841
842 /**
843 * Delivers the content of the requested file.
844 *
845 * @param string $objectType The type of the requested file.
846 * @param string $objectUrl The file url.
847 *
848 * @return null
849 */
850 public function getFile($objectType, $objectUrl)
851 {
852 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
853
854 if ($fileObject === null) {
855 return null;
856 }
857
858 if ($this->accessHandler->checkObjectAccess($fileObject->getType(), $fileObject->getId()) === true) {
859 $file = $fileObject->getFile();
860 } elseif ($fileObject->isImage() === true) {
861 $realPath = $this->config->getRealPath();
862 $file = $realPath.'assets/gfx/noAccessPic.png';
863 } else {
864 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS);
865 return null;
866 }
867
868 return $this->fileHandler->getFile($file, $fileObject->isImage());
869 }
870
871 /**
872 * Redirects the user to his destination.
873 *
874 * @param bool $checkPosts
875 */
876 public function redirectUser($checkPosts = true)
877 {
878 if ($checkPosts === true) {
879 $posts = (array)$this->wordpress->getWpQuery()->get_posts();
880
881 foreach ($posts as $post) {
882 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
883 return;
884 }
885 }
886 }
887
888 $permalink = null;
889 $redirect = $this->config->getRedirect();
890
891 if ($redirect === 'custom_page') {
892 $redirectCustomPage = $this->config->getRedirectCustomPage();
893 $post = $this->objectHandler->getPost($redirectCustomPage);
894 $url = null;
895
896 if ($post !== false) {
897 $url = $post->guid;
898 $permalink = $this->wordpress->getPageLink($post);
899 }
900 } elseif ($redirect === 'custom_url') {
901 $url = $this->config->getRedirectCustomUrl();
902 } else {
903 $url = $this->wordpress->getHomeUrl('/');
904 }
905
906 $currentUrl = $this->util->getCurrentUrl();
907
908 if ($url !== null && $url !== $currentUrl && $permalink !== $currentUrl) {
909 $this->wordpress->wpRedirect($url);
910 return;
911 }
912 }
913
914 /**
915 * Redirects to a page or to content.
916 *
917 * @param string $headers The headers which are given from wordpress.
918 * @param object $pageParams The params of the current page.
919 *
920 * @return string
921 */
922 public function redirect($headers, $pageParams)
923 {
924 $fileUrl = $this->getRequestParameter('uamgetfile');
925 $fileType = $this->getRequestParameter('uamfiletype');
926
927 if ($fileUrl !== null && $fileType !== null) {
928 $this->getFile($fileType, $fileUrl);
929 } elseif ($this->config->atAdminPanel() === false
930 && $this->config->getRedirect() !== 'false'
931 ) {
932 $objectType = null;
933 $objectId = null;
934
935 if (isset($pageParams->query_vars['p']) === true) {
936 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
937 $objectId = $pageParams->query_vars['p'];
938 } elseif (isset($pageParams->query_vars['page_id']) === true) {
939 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
940 $objectId = $pageParams->query_vars['page_id'];
941 } elseif (isset($pageParams->query_vars['cat_id']) === true) {
942 $objectType = ObjectHandler::GENERAL_TERM_OBJECT_TYPE;
943 $objectId = $pageParams->query_vars['cat_id'];
944 } elseif (isset($pageParams->query_vars['name']) === true) {
945 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
946
947 $query = $this->database->prepare(
948 "SELECT ID
949 FROM {$this->database->getPostsTable()}
950 WHERE post_name = %s
951 AND post_type IN ('{$postableTypes}')",
952 $pageParams->query_vars['name']
953 );
954
955 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
956 $objectId = (int)$this->database->getVariable($query);
957 } elseif (isset($pageParams->query_vars['pagename']) === true) {
958 $object = $this->wordpress->getPageByPath($pageParams->query_vars['pagename']);
959
960 if ($object !== null) {
961 $objectType = $object->post_type;
962 $objectId = $object->ID;
963 }
964 }
965
966 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
967 $this->redirectUser(false);
968 }
969 }
970
971 return $headers;
972 }
973
974 /**
975 * Returns the url for a locked file.
976 *
977 * @param string $url The base url.
978 * @param integer $id The _iId of the file.
979 *
980 * @return string
981 */
982 public function getFileUrl($url, $id)
983 {
984 if ($this->config->isPermalinksActive() === false && $this->config->lockFile() === true) {
985 $post = $this->objectHandler->getPost($id);
986
987 if ($post !== null) {
988 $type = explode('/', $post->post_mime_type);
989 $type = (isset($type[1]) === true) ? $type[1] : $type[0];
990
991 $lockedFileTypes = $this->config->getLockedFileTypes();
992 $fileTypes = explode(',', $lockedFileTypes);
993
994 if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) {
995 $url = $this->wordpress->getHomeUrl('/').'?uamfiletype=attachment&uamgetfile='.$url;
996 }
997 }
998 }
999
1000 return $url;
1001 }
1002
1003 /**
1004 * Caches the urls for the post for a later lookup.
1005 *
1006 * @param string $url The url of the post.
1007 * @param object $post The post object.
1008 *
1009 * @return string
1010 */
1011 public function cachePostLinks($url, $post)
1012 {
1013 $postUrls = (array)$this->cache->getFromCache(self::POST_URL_CACHE_KEY);
1014 $postUrls[$url] = $post->ID;
1015 $this->cache->addToCache(self::POST_URL_CACHE_KEY, $postUrls);
1016 return $url;
1017 }
1018
1019 /**
1020 * Filter for Yoast SEO Plugin
1021 *
1022 * Hides the url from the site map if the user has no access
1023 *
1024 * @param string $url The url to check
1025 * @param string $type The object type
1026 * @param object $object The object
1027 *
1028 * @return false|string
1029 */
1030 public function getWpSeoUrl($url, $type, $object)
1031 {
1032 return ($this->accessHandler->checkObjectAccess($type, $object->ID) === true) ? $url : false;
1033 }
1034 }
1035