PluginProbe
User Access Manager / 2.0.5
User Access Manager v2.0.5
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.5, at src/UserAccessManager/Controller/FrontendController.php

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