PluginProbe
User Access Manager / 2.0.13
User Access Manager v2.0.13
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.13, at src/UserAccessManager/Controller/FrontendController.php

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