PluginProbe
User Access Manager / 2.0.3
User Access Manager v2.0.3
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.3, at src/UserAccessManager/Controller/FrontendController.php

1,061 lines 31.0 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 * Tries to get the post from the given mixed data.
219 *
220 * @param mixed $post
221 *
222 * @return false|\WP_Post
223 */
224 private function getPost($post)
225 {
226 if ($post instanceof \WP_post) {
227 return $post;
228 } elseif (is_int($post) === true) {
229 return $this->objectHandler->getPost($post);
230 } elseif ($post instanceof \stdClass && isset($post->ID)) {
231 return $this->objectHandler->getPost($post->ID);
232 }
233
234 return false;
235 }
236
237 /**
238 * Modifies the content of the post by the given settings.
239 *
240 * @param \WP_Post $post The current post.
241 * @param bool $locked
242 *
243 * @return null|\WP_Post
244 */
245 private function processPost(\WP_Post $post, &$locked = null)
246 {
247 $post->post_title .= $this->adminOutput($post->post_type, $post->ID);
248 $locked = ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false);
249
250 if ($locked === true) {
251 if ($this->config->hidePostType($post->post_type) === true
252 || $this->config->atAdminPanel() === true
253 ) {
254 return null;
255 }
256
257 $uamPostContent = $this->config->getPostTypeContent($post->post_type);
258 $uamPostContent = str_replace('[LOGIN_FORM]', $this->getLoginFormHtml(), $uamPostContent);
259
260 if ($post->post_type === 'post'
261 && $this->config->showPostContentBeforeMore() === true
262 && preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches)
263 ) {
264 $uamPostContent = explode($matches[0], $post->post_content)[0]." ".$uamPostContent;
265 }
266
267 $post->post_content = stripslashes($uamPostContent);
268
269 if ($this->config->hidePostTypeTitle($post->post_type) === true) {
270 $post->post_title = $this->config->getPostTypeTitle($post->post_type);
271 }
272
273 if ($this->config->hidePostTypeComments($post->post_type) === true) {
274 $post->comment_status = 'close';
275 }
276 }
277
278 return $post;
279 }
280
281 /**
282 * The function for the the_posts filter.
283 *
284 * @param array $posts The posts.
285 *
286 * @return array
287 */
288 public function showPosts($posts = [])
289 {
290 $showPosts = [];
291
292 if ($this->wordpress->isFeed() === false || $this->config->protectFeed() === true) {
293 foreach ($posts as $post) {
294 $post = $this->getPost($post);
295
296 if ($post !== false) {
297 $post = $this->processPost($post);
298
299 if ($post !== null) {
300 $showPosts[] = $post;
301 }
302 }
303 }
304 }
305
306 return $showPosts;
307 }
308
309 /**
310 * The function for the get_pages filter.
311 *
312 * @param \WP_Post[] $pages The pages.
313 *
314 * @return array
315 */
316 public function showPages($pages = [])
317 {
318 $showPages = [];
319
320 foreach ($pages as $page) {
321 $page = $this->getPost($page);
322
323 if ($page !== false) {
324 $page = $this->processPost($page);
325
326 if ($page !== null) {
327 $showPages[] = $page;
328 }
329 }
330 }
331
332 $pages = $showPages;
333
334 return $pages;
335 }
336
337 /**
338 * The function for the posts_where_paged filter.
339 *
340 * @param string $query The where sql statement.
341 *
342 * @return string
343 */
344 public function showPostSql($query)
345 {
346 $excludedPosts = $this->accessHandler->getExcludedPosts();
347
348 if (count($excludedPosts) > 0) {
349 $excludedPostsStr = implode(', ', $excludedPosts);
350 $query .= " AND {$this->database->getPostsTable()}.ID NOT IN ($excludedPostsStr) ";
351 }
352
353 return $query;
354 }
355
356 /**
357 * Function for the wp_count_posts filter.
358 *
359 * @param \stdClass $counts
360 * @param string $type
361 * @param string $perm
362 *
363 * @return \stdClass
364 */
365 public function showPostCount($counts, $type, $perm)
366 {
367 $cachedCounts = $this->cache->getFromCache(self::POST_COUNTS_CACHE_KEY);
368
369 if ($cachedCounts === null) {
370 $excludedPosts = $this->accessHandler->getExcludedPosts();
371
372 if (count($excludedPosts) > 0) {
373 $excludedPosts = implode('\', \'', $excludedPosts);
374
375 $query = "SELECT post_status, COUNT(*) AS num_posts
376 FROM {$this->database->getPostsTable()}
377 WHERE post_type = %s
378 AND ID NOT IN ('{$excludedPosts}')";
379
380 if ('readable' === $perm && $this->wordpress->isUserLoggedIn() === true) {
381 $postTypeObject = $this->wordpress->getPostTypeObject($type);
382
383 if ($this->wordpress->currentUserCan($postTypeObject->cap->read_private_posts) === false) {
384 $query .= $this->database->prepare(
385 ' AND (post_status != \'private\' OR (post_author = %d AND post_status = \'private\'))',
386 $this->wordpress->getCurrentUser()->ID
387 );
388 }
389 }
390
391 $query .= ' GROUP BY post_status';
392
393 $results = (array)$this->database->getResults(
394 $this->database->prepare($query, $type),
395 ARRAY_A
396 );
397
398 foreach ($results as $result) {
399 if (isset($counts->{$result['post_status']})) {
400 $counts->{$result['post_status']} = $result['num_posts'];
401 }
402 }
403 }
404
405 $cachedCounts = $counts;
406 $this->cache->addToCache(self::POST_COUNTS_CACHE_KEY, $cachedCounts);
407 }
408
409 return $cachedCounts;
410 }
411
412 /**
413 * Sets the excluded terms as argument.
414 *
415 * @param array $arguments
416 *
417 * @return array
418 */
419 public function getTermArguments(array $arguments)
420 {
421 $exclude = (isset($arguments['exclude']) === true) ?
422 $this->wordpress->parseIdList($arguments['exclude']) : [];
423 $arguments['exclude'] = array_merge($exclude, $this->accessHandler->getExcludedTerms());
424 $arguments['exclude'] = array_unique($arguments['exclude']);
425
426 return $arguments;
427 }
428
429 /**
430 * The function for the comments_array filter.
431 *
432 * @param \WP_Comment[] $comments The comments.
433 *
434 * @return array
435 */
436 public function showComment($comments = [])
437 {
438 $showComments = [];
439
440 foreach ($comments as $comment) {
441 $post = $this->objectHandler->getPost($comment->comment_post_ID);
442
443 if ($post !== false
444 && $this->accessHandler->checkObjectAccess($post->post_type, $post->ID) === false
445 ) {
446 if ($this->config->hidePostTypeComments($post->post_type) === true
447 || $this->config->hidePostType($post->post_type) === true
448 || $this->config->atAdminPanel() === true
449 ) {
450 continue;
451 }
452
453 $comment->comment_content = $this->config->getPostTypeCommentContent($post->post_type);
454 }
455
456 $showComments[] = $comment;
457 }
458
459 return $showComments;
460 }
461
462 /**
463 * The function for the get_ancestors filter.
464 *
465 * @param array $ancestors
466 * @param int $objectId
467 * @param string $objectType
468 *
469 * @return array
470 */
471 public function showAncestors($ancestors, $objectId, $objectType)
472 {
473 if ($this->config->lockRecursive() === true
474 && $this->accessHandler->checkObjectAccess($objectType, $objectId) === false
475 ) {
476 return [];
477 }
478
479 foreach ($ancestors as $key => $ancestorId) {
480 if ($this->accessHandler->checkObjectAccess($objectType, $ancestorId) === false) {
481 unset($ancestors[$key]);
482 }
483 }
484
485 return $ancestors;
486 }
487
488 /**
489 * The function for the get_previous_post_where and
490 * the get_next_post_where filter.
491 *
492 * @param string $query The current sql string.
493 *
494 * @return string
495 */
496 public function showNextPreviousPost($query)
497 {
498 $excludedPosts = $this->accessHandler->getExcludedPosts();
499
500 if (count($excludedPosts) > 0) {
501 $excludedPosts = implode(', ', $excludedPosts);
502 $query .= " AND p.ID NOT IN ({$excludedPosts}) ";
503 }
504
505 return $query;
506 }
507
508 /**
509 * Returns the post count for the term.
510 *
511 * @param string $termType
512 * @param int $termId
513 *
514 * @return int
515 */
516 private function getVisibleElementsCount($termType, $termId)
517 {
518 $count = 0;
519
520 $terms = [$termId => $termId];
521 $termTreeMap = $this->objectHandler->getTermTreeMap();
522
523 if (isset($termTreeMap[ObjectHandler::TREE_MAP_CHILDREN][$termType]) === true
524 && isset($termTreeMap[ObjectHandler::TREE_MAP_CHILDREN][$termType][$termId]) === true
525 ) {
526 $terms += $termTreeMap[ObjectHandler::TREE_MAP_CHILDREN][$termType][$termId];
527 }
528
529 $posts = [];
530 $termPostMap = $this->objectHandler->getTermPostMap();
531
532 foreach ($terms as $termId) {
533 if (isset($termPostMap[$termId]) === true) {
534 $posts += $termPostMap[$termId];
535 }
536 }
537
538 foreach ($posts as $postId => $postType) {
539 if ($this->config->hidePostType($postType) === false
540 || $this->accessHandler->checkObjectAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId) === true
541 ) {
542 $count++;
543 }
544 }
545
546 return $count;
547 }
548
549 /**
550 * Modifies the content of the term by the given settings.
551 *
552 * @param \WP_Term $term The current term.
553 * @param bool $isEmpty
554 *
555 * @return mixed
556 */
557 private function processTerm($term, &$isEmpty = null)
558 {
559 $isEmpty = false;
560
561 if (($term instanceof \WP_Term) === false) {
562 return $term;
563 }
564
565 if ($this->accessHandler->checkObjectAccess($term->taxonomy, $term->term_id) === false) {
566 return null;
567 }
568
569 $term->name .= $this->adminOutput($term->taxonomy, $term->term_id, $term->name);
570 $term->count = $this->getVisibleElementsCount($term->taxonomy, $term->term_id);
571
572 //For categories
573 if ($term->count <= 0
574 && $this->config->atAdminPanel() === false
575 && $this->config->hideEmptyTaxonomy($term->taxonomy) === true
576 ) {
577 $isEmpty = true;
578 }
579
580 if ($this->config->lockRecursive() === false) {
581 $currentTerm = $term;
582
583 while ($currentTerm->parent != 0) {
584 $currentTerm = $this->objectHandler->getTerm($currentTerm->parent);
585
586 if ($currentTerm === false) {
587 break;
588 }
589
590 $access = $this->accessHandler->checkObjectAccess(
591 $currentTerm->taxonomy,
592 $currentTerm->term_id
593 );
594
595 if ($access === true) {
596 $term->parent = $currentTerm->term_id;
597 break;
598 }
599 }
600 }
601
602 return $term;
603 }
604
605 /**
606 * The function for the get_term filter.
607 *
608 * @param \WP_Term $term
609 *
610 * @return null|object
611 */
612 public function showTerm($term)
613 {
614 return $this->processTerm($term);
615 }
616
617 /**
618 * The function for the get_terms filter.
619 *
620 * @param array $terms The terms.
621 *
622 * @return array
623 */
624 public function showTerms($terms = [])
625 {
626 foreach ($terms as $key => $term) {
627 $isNumeric = (is_numeric($term) === true);
628
629 if ($isNumeric === true) {
630 if ((int)$term === 0) {
631 unset($terms[$key]);
632 continue;
633 }
634
635 $term = $this->objectHandler->getTerm($term);
636 }
637
638 if (($term instanceof \WP_Term) === false) {
639 continue;
640 }
641
642 $term = $this->processTerm($term, $isEmpty);
643
644 if ($term !== null && $isEmpty === false) {
645 $terms[$key] = ($isNumeric === true) ? $term->term_id : $term;
646 } else {
647 unset($terms[$key]);
648 }
649 }
650
651 return $terms;
652 }
653
654 /**
655 * The function for the wp_get_nav_menu_items filter.
656 *
657 * @param array $items The menu item.
658 *
659 * @return array
660 */
661 public function showCustomMenu($items)
662 {
663 $showItems = [];
664
665 foreach ($items as $key => $item) {
666 $item->title .= $this->adminOutput($item->object, $item->object_id, $item->title);
667
668 if ($this->objectHandler->isPostType($item->object) === true) {
669 if ($this->accessHandler->checkObjectAccess($item->object, $item->object_id) === false) {
670 if ($this->config->hidePostType($item->object) === true
671 || $this->config->atAdminPanel() === true
672 ) {
673 continue;
674 }
675
676 if ($this->config->hidePostTypeTitle($item->object) === true) {
677 $item->title = $this->config->getPostTypeTitle($item->object);
678 }
679 }
680
681 $showItems[$key] = $item;
682 } elseif ($this->objectHandler->isTaxonomy($item->object) === true) {
683 $object = $this->objectHandler->getTerm($item->object_id);
684 $category = $this->processTerm($object, $isEmpty);
685
686 if ($category !== null && $isEmpty === false) {
687 $showItems[$key] = $item;
688 }
689 } else {
690 $showItems[$key] = $item;
691 }
692 }
693
694 return $showItems;
695 }
696
697 /**
698 * The function for the edit_post_link filter.
699 *
700 * @param string $link The edit link.
701 * @param integer $postId The _iId of the post.
702 *
703 * @return string
704 */
705 public function showGroupMembership($link, $postId)
706 {
707 $userGroups = $this->accessHandler->getFilteredUserGroupsForObject(
708 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
709 $postId
710 );
711
712 if (count($userGroups) > 0) {
713 $escapedGroups = array_map(
714 function (UserGroup $group) {
715 return htmlentities($group->getName());
716 },
717 $userGroups
718 );
719
720 $link .= ' | '.TXT_UAM_ASSIGNED_GROUPS.': ';
721 $link .= implode(', ', $escapedGroups);
722 }
723
724 return $link;
725 }
726
727 /**
728 * Checks if we allowed show the login form.
729 *
730 * @return bool
731 */
732 public function showLoginForm()
733 {
734 return $this->wordpress->isSingle() === true || $this->wordpress->isPage() === true;
735 }
736
737 /**
738 * Returns the login url.
739 *
740 * @return mixed
741 */
742 public function getLoginUrl()
743 {
744 $loginUrl = $this->wordpress->getBlogInfo('wpurl').'/wp-login.php';
745 return $this->wordpress->applyFilters('uam_login_form_url', $loginUrl);
746 }
747
748 /**
749 * Returns the login redirect url.
750 *
751 * @return mixed
752 */
753 public function getRedirectLoginUrl()
754 {
755 $loginUrl = $this->wordpress->getBlogInfo('wpurl')
756 .'/wp-login.php?redirect_to='.urlencode($_SERVER['REQUEST_URI']);
757 return $this->wordpress->applyFilters('uam_login_url', $loginUrl);
758 }
759
760 /**
761 * Returns the user login name.
762 *
763 * @return string
764 */
765 public function getUserLogin()
766 {
767 $userLogin = $this->getRequestParameter('log');
768 return $this->wordpress->escHtml(stripslashes($userLogin));
769 }
770
771
772 /*
773 * Functions for the redirection and files.
774 */
775
776 /**
777 * Returns the post by the given url.
778 *
779 * @param string $url The url of the post(attachment).
780 *
781 * @return int
782 */
783 public function getPostIdByUrl($url)
784 {
785 $postUrls = (array)$this->cache->getFromCache(self::POST_URL_CACHE_KEY);
786
787 if (isset($postUrls[$url]) === true) {
788 return $postUrls[$url];
789 }
790
791 $postUrls[$url] = null;
792
793 //Filter edit string
794 $newUrlPieces = preg_split('/-e[0-9]{1,}/', $url);
795 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
796
797 //Filter size
798 $newUrlPieces = preg_split('/-[0-9]{1,}x[0-9]{1,}/', $newUrl);
799 $newUrl = (count($newUrlPieces) === 2) ? $newUrlPieces[0].$newUrlPieces[1] : $newUrlPieces[0];
800
801 $query = $this->database->prepare(
802 "SELECT ID
803 FROM {$this->database->getPostsTable()}
804 WHERE guid = '%s'
805 LIMIT 1",
806 $newUrl
807 );
808
809 $dbPost = $this->database->getRow($query);
810
811 if ($dbPost !== null) {
812 $postUrls[$url] = $dbPost->ID;
813 $this->cache->addToCache(self::POST_URL_CACHE_KEY, $postUrls);
814 }
815
816 return $postUrls[$url];
817 }
818
819 /**
820 * Returns the file object by the given type and url.
821 *
822 * @param string $objectType The type of the requested file.
823 * @param string $objectUrl The file url.
824 *
825 * @return null|FileObject
826 */
827 private function getFileSettingsByType($objectType, $objectUrl)
828 {
829 $fileObject = null;
830
831 if ($objectType === ObjectHandler::ATTACHMENT_OBJECT_TYPE) {
832 $uploadDirs = $this->wordpress->getUploadDir();
833 $uploadDir = str_replace(ABSPATH, '/', $uploadDirs['basedir']);
834 $regex = '/.*'.str_replace('/', '\/', $uploadDir).'\//i';
835 $cleanObjectUrl = preg_replace($regex, '', $objectUrl);
836 $uploadUrl = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
837 $objectUrl = rtrim($uploadUrl, '/').'/'.ltrim($cleanObjectUrl, '/');
838
839 $post = $this->objectHandler->getPost($this->getPostIdByUrl($objectUrl));
840
841 if ($post !== false
842 && $post->post_type === ObjectHandler::ATTACHMENT_OBJECT_TYPE
843 ) {
844 $multiPath = str_replace('/files', $uploadDir, $uploadDirs['baseurl']);
845
846 $fileObject = $this->fileObjectFactory->createFileObject(
847 $post->ID,
848 $objectType,
849 $uploadDirs['basedir'].str_replace($multiPath, '', $objectUrl),
850 $this->wordpress->attachmentIsImage($post->ID)
851 );
852 }
853 } else {
854 $extraParameter = $this->getRequestParameter('uamextra');
855
856 $fileObject = $this->wordpress->applyFilters(
857 'uam_get_file_settings_by_type',
858 $fileObject,
859 $objectType,
860 $objectUrl,
861 $extraParameter
862 );
863 }
864
865 return $fileObject;
866 }
867
868 /**
869 * Delivers the content of the requested file.
870 *
871 * @param string $objectType The type of the requested file.
872 * @param string $objectUrl The file url.
873 *
874 * @return null
875 */
876 public function getFile($objectType, $objectUrl)
877 {
878 $fileObject = $this->getFileSettingsByType($objectType, $objectUrl);
879
880 if ($fileObject === null) {
881 return null;
882 }
883
884 if ($this->accessHandler->checkObjectAccess($fileObject->getType(), $fileObject->getId()) === true) {
885 $file = $fileObject->getFile();
886 } elseif ($fileObject->isImage() === true) {
887 $realPath = $this->config->getRealPath();
888 $file = $realPath.'assets/gfx/noAccessPic.png';
889 } else {
890 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS);
891 return null;
892 }
893
894 return $this->fileHandler->getFile($file, $fileObject->isImage());
895 }
896
897 /**
898 * Redirects the user to his destination.
899 *
900 * @param bool $checkPosts
901 */
902 public function redirectUser($checkPosts = true)
903 {
904 if ($checkPosts === true) {
905 $posts = (array)$this->wordpress->getWpQuery()->get_posts();
906
907 foreach ($posts as $post) {
908 if ($this->accessHandler->checkObjectAccess($post->post_type, $post->ID)) {
909 return;
910 }
911 }
912 }
913
914 $permalink = null;
915 $redirect = $this->config->getRedirect();
916
917 if ($redirect === 'custom_page') {
918 $redirectCustomPage = $this->config->getRedirectCustomPage();
919 $post = $this->objectHandler->getPost($redirectCustomPage);
920 $url = null;
921
922 if ($post !== false) {
923 $url = $post->guid;
924 $permalink = $this->wordpress->getPageLink($post);
925 }
926 } elseif ($redirect === 'custom_url') {
927 $url = $this->config->getRedirectCustomUrl();
928 } else {
929 $url = $this->wordpress->getHomeUrl('/');
930 }
931
932 $currentUrl = $this->util->getCurrentUrl();
933
934 if ($url !== null && $url !== $currentUrl && $permalink !== $currentUrl) {
935 $this->wordpress->wpRedirect($url);
936 return;
937 }
938 }
939
940 /**
941 * Redirects to a page or to content.
942 *
943 * @param string $headers The headers which are given from wordpress.
944 * @param object $pageParams The params of the current page.
945 *
946 * @return string
947 */
948 public function redirect($headers, $pageParams)
949 {
950 $fileUrl = $this->getRequestParameter('uamgetfile');
951 $fileType = $this->getRequestParameter('uamfiletype');
952
953 if ($fileUrl !== null && $fileType !== null) {
954 $this->getFile($fileType, $fileUrl);
955 } elseif ($this->config->atAdminPanel() === false
956 && $this->config->getRedirect() !== 'false'
957 ) {
958 $objectType = null;
959 $objectId = null;
960
961 if (isset($pageParams->query_vars['p']) === true) {
962 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
963 $objectId = $pageParams->query_vars['p'];
964 } elseif (isset($pageParams->query_vars['page_id']) === true) {
965 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
966 $objectId = $pageParams->query_vars['page_id'];
967 } elseif (isset($pageParams->query_vars['cat_id']) === true) {
968 $objectType = ObjectHandler::GENERAL_TERM_OBJECT_TYPE;
969 $objectId = $pageParams->query_vars['cat_id'];
970 } elseif (isset($pageParams->query_vars['name']) === true) {
971 $postableTypes = implode('\',\'', $this->objectHandler->getPostTypes());
972
973 $query = $this->database->prepare(
974 "SELECT ID
975 FROM {$this->database->getPostsTable()}
976 WHERE post_name = %s
977 AND post_type IN ('{$postableTypes}')",
978 $pageParams->query_vars['name']
979 );
980
981 $objectType = ObjectHandler::GENERAL_POST_OBJECT_TYPE;
982 $objectId = (int)$this->database->getVariable($query);
983 } elseif (isset($pageParams->query_vars['pagename']) === true) {
984 $object = $this->wordpress->getPageByPath($pageParams->query_vars['pagename']);
985
986 if ($object !== null) {
987 $objectType = $object->post_type;
988 $objectId = $object->ID;
989 }
990 }
991
992 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
993 $this->redirectUser(false);
994 }
995 }
996
997 return $headers;
998 }
999
1000 /**
1001 * Returns the url for a locked file.
1002 *
1003 * @param string $url The base url.
1004 * @param integer $id The _iId of the file.
1005 *
1006 * @return string
1007 */
1008 public function getFileUrl($url, $id)
1009 {
1010 if ($this->config->isPermalinksActive() === false && $this->config->lockFile() === true) {
1011 $post = $this->objectHandler->getPost($id);
1012
1013 if ($post !== null) {
1014 $type = explode('/', $post->post_mime_type);
1015 $type = (isset($type[1]) === true) ? $type[1] : $type[0];
1016
1017 $lockedFileTypes = $this->config->getLockedFileTypes();
1018 $fileTypes = explode(',', $lockedFileTypes);
1019
1020 if ($lockedFileTypes === 'all' || in_array($type, $fileTypes) === true) {
1021 $url = $this->wordpress->getHomeUrl('/').'?uamfiletype=attachment&uamgetfile='.$url;
1022 }
1023 }
1024 }
1025
1026 return $url;
1027 }
1028
1029 /**
1030 * Caches the urls for the post for a later lookup.
1031 *
1032 * @param string $url The url of the post.
1033 * @param object $post The post object.
1034 *
1035 * @return string
1036 */
1037 public function cachePostLinks($url, $post)
1038 {
1039 $postUrls = (array)$this->cache->getFromCache(self::POST_URL_CACHE_KEY);
1040 $postUrls[$url] = $post->ID;
1041 $this->cache->addToCache(self::POST_URL_CACHE_KEY, $postUrls);
1042 return $url;
1043 }
1044
1045 /**
1046 * Filter for Yoast SEO Plugin
1047 *
1048 * Hides the url from the site map if the user has no access
1049 *
1050 * @param string $url The url to check
1051 * @param string $type The object type
1052 * @param object $object The object
1053 *
1054 * @return false|string
1055 */
1056 public function getWpSeoUrl($url, $type, $object)
1057 {
1058 return ($this->accessHandler->checkObjectAccess($type, $object->ID) === true) ? $url : false;
1059 }
1060 }
1061