PluginProbe
User Access Manager / 1.0.1
User Access Manager v1.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 / class / UamUserGroup.class.php

UamUserGroup.class.php in User Access Manager 1.0.1, at class/UamUserGroup.class.php

1,649 lines 41.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UamUserGroup.class.php
4 *
5 * The UamUserGroup class file.
6 *
7 * PHP versions 5
8 *
9 * @category UserAccessManager
10 * @package UserAccessManager
11 * @author Alexander Schneider <alexanderschneider85@googlemail.com>
12 * @copyright 2008-2010 Alexander Schneider
13 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
14 * @version SVN: $Id$
15 * @link http://wordpress.org/extend/plugins/user-access-manager/
16 */
17
18 /**
19 * The user group class.
20 *
21 * @category UserAccessManager
22 * @package UserAccessManager
23 * @author Alexander Schneider <alexanderschneider85@gmail.com>
24 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
25 * @link http://wordpress.org/extend/plugins/user-access-manager/
26 */
27
28 class UamUserGroup
29 {
30 protected $accessHandler = null;
31 protected $id = null;
32 protected $groupName = null;
33 protected $groupDesc = null;
34 protected $readAccess = null;
35 protected $writeAccess = null;
36 protected $ipRange = null;
37 protected $roles = array();
38 protected $users = array(
39 'real' => -1,
40 'full' => -1
41 );
42 protected $singleUsers = array();
43 private $_assignedUsers = null;
44 protected $categories = array(
45 'real' => -1,
46 'full' => -1
47 );
48 protected $singleCategories = array();
49 private $_assignedCategories = null;
50 protected $posts = array(
51 'real' => -1,
52 'full' => -1
53 );
54 protected $singlePosts = array();
55 private $_assignedPosts = null;
56 protected $pages = array(
57 'real' => -1,
58 'full' => -1
59 );
60 protected $files = array(
61 'real' => -1,
62 'full' => -1
63 );
64 protected $pluggableObjects = null;
65 protected $singlePluggableObjects = null;
66 private $_assignedPluggableObjects = null;
67
68 /**
69 * Consturtor
70 *
71 * @param object &$uamAccessHandler The access handler object.
72 * @param integer $id The id of the user group.
73 *
74 * @return null
75 */
76 function __construct(&$uamAccessHandler, $id = null)
77 {
78 $this->accessHandler = $uamAccessHandler;
79
80 //Create default values for the pluggable objects.
81 $pluggableObjects = $uamAccessHandler->getPluggableObjects();
82
83 foreach ($pluggableObjects as $objectName => $pluggableObject) {
84 $this->$pluggableObjects[$objectName] = array(
85 'real' => -1,
86 'full' => -1
87 );
88 }
89
90 if ($id !== null) {
91 global $wpdb;
92
93 $this->id = $id;
94
95 $dbUsergroup = $wpdb->get_row(
96 "SELECT *
97 FROM " . DB_ACCESSGROUP . "
98 WHERE ID = " . $this->id . "
99 LIMIT 1",
100 ARRAY_A
101 );
102
103 $this->groupName = $dbUsergroup['groupname'];
104 $this->groupDesc = $dbUsergroup['groupdesc'];
105 $this->readAccess = $dbUsergroup['read_access'];
106 $this->writeAccess = $dbUsergroup['write_access'];
107 $this->ipRange = $dbUsergroup['ip_range'];
108 }
109 }
110
111 /**
112 * Returns the user access handler object.
113 *
114 * @return object
115 */
116 function &getAccessHandler()
117 {
118 return $this->accessHandler;
119 }
120
121 /**
122 * Deletes the user group.
123 *
124 * @return null
125 */
126 function delete()
127 {
128 if ($this->id == null) {
129 return false;
130 }
131
132 global $wpdb;
133
134 $wpdb->query(
135 "DELETE FROM " . DB_ACCESSGROUP . "
136 WHERE ID = $this->id LIMIT 1"
137 );
138
139 $this->_deleteRolesFromDb();
140 $this->_deletePostByTypeFromDb('all');
141 $this->_deleteCategoriesFromDb();
142 $this->_deleteUsersFromDb();
143 }
144
145 /**
146 * Saves the user group.
147 *
148 * @return null;
149 */
150 function save()
151 {
152 global $wpdb;
153
154 if ($this->id == null) {
155 $wpdb->query(
156 "INSERT INTO " . DB_ACCESSGROUP . " (
157 ID,
158 groupname,
159 groupdesc,
160 read_access,
161 write_access,
162 ip_range
163 )
164 VALUES (
165 NULL,
166 '" . $this->groupName . "',
167 '" . $this->groupDesc . "',
168 '" . $this->readAccess . "',
169 '" . $this->writeAccess . "',
170 '" . $this->ipRange . "'
171 )"
172 );
173
174 $this->id = $wpdb->insert_id;
175 } else {
176 $wpdb->query(
177 "UPDATE " . DB_ACCESSGROUP . "
178 SET groupname = '" . $this->groupName . "',
179 groupdesc = '" . $this->groupDesc . "',
180 read_access = '" . $this->readAccess . "',
181 write_access = '" . $this->writeAccess . "',
182 ip_range = '" . $this->ipRange . "'
183 WHERE ID = " . $this->id
184 );
185
186 $this->getPosts();
187 $this->getPages();
188 $this->getFiles();
189 $this->getRoles();
190 $this->getCategories();
191 $this->getUsers();
192
193 $this->_deletePostByTypeFromDb('all');
194 $this->_deleteRolesFromDb();
195 $this->_deleteCategoriesFromDb();
196 $this->_deleteUsersFromDb();
197 }
198
199 foreach ($this->getUsers() as $userKey => $user) {
200 $wpdb->query(
201 "INSERT INTO " . DB_ACCESSGROUP_TO_USER . " (
202 group_id,
203 user_id
204 )
205 VALUES(
206 '" . $this->id . "',
207 '" . $userKey . "'
208 )"
209 );
210 }
211
212 foreach ($this->getCategories() as $categoryKey => $category) {
213 $wpdb->query(
214 "INSERT INTO " . DB_ACCESSGROUP_TO_CATEGORY . " (
215 group_id,
216 category_id
217 )
218 VALUES(
219 '" . $this->id . "',
220 '" . $categoryKey . "'
221 )"
222 );
223 }
224
225 foreach ($this->getRoles() as $roleKey => $role) {
226 $wpdb->query(
227 "INSERT INTO " . DB_ACCESSGROUP_TO_ROLE . " (
228 group_id,
229 role_name
230 )
231 VALUES(
232 '" . $this->id . "',
233 '" . $roleKey . "'
234 )"
235 );
236 }
237
238 $allPosts
239 = array_merge($this->getPosts(), $this->getPages(), $this->getFiles());
240
241 foreach ($allPosts as $post) {
242 $wpdb->query(
243 "INSERT INTO " . DB_ACCESSGROUP_TO_POST . " (
244 group_id,
245 post_id
246 )
247 VALUES(
248 '" . $this->id . "',
249 '" . $post->ID . "'
250 )"
251 );
252 }
253 }
254
255
256 /*
257 * Primary values.
258 */
259
260 /**
261 * Returns the group id.
262 *
263 * @return integer
264 */
265 function getId()
266 {
267 return $this->id;
268 }
269
270 /**
271 * Returns the group name.
272 *
273 * @return string
274 */
275 function getGroupName()
276 {
277 return $this->groupName;
278 }
279
280 /**
281 * Sets the group name.
282 *
283 * @param string $groupName The new group name.
284 *
285 * @return null
286 */
287 function setGroupName($groupName)
288 {
289 $this->groupName = $groupName;
290 }
291
292 /**
293 * Returns the group description.
294 *
295 * @return string
296 */
297 function getGroupDesc()
298 {
299 return $this->groupDesc;
300 }
301
302 /**
303 * Sets the group description.
304 *
305 * @param string $groupDesc The new group description.
306 *
307 * @return null
308 */
309 function setGroupDesc($groupDesc)
310 {
311 $this->groupDesc = $groupDesc;
312 }
313
314 /**
315 * Returns the read access.
316 *
317 * @return string
318 */
319 function getReadAccess()
320 {
321 return $this->readAccess;
322 }
323
324 /**
325 * Sets the read access.
326 *
327 * @param string $readAccess The read access.
328 *
329 * @return null
330 */
331 function setReadAccess($readAccess)
332 {
333 $this->readAccess = $readAccess;
334 }
335
336 /**
337 * Returns the write access.
338 *
339 * @return string
340 */
341 function getWriteAccess()
342 {
343 return $this->writeAccess;
344 }
345
346 /**
347 * Sets the write access.
348 *
349 * @param string $writeAccess The wirte access.
350 *
351 * @return null
352 */
353 function setWriteAccess($writeAccess)
354 {
355 $this->writeAccess = $writeAccess;
356 }
357
358 /**
359 * Returns the ip range.
360 *
361 * @param string $type The return type.
362 *
363 * @return array
364 */
365 function getIpRange($type = null)
366 {
367 if ($type == 'string') {
368 return $this->ipRange;
369 }
370
371 $ipRange = explode(';', $this->ipRange);
372
373 if ($ipRange[0] == null) {
374 return null;
375 }
376
377 return $ipRange;
378 }
379
380 /**
381 * Sets the ip range.
382 *
383 * @param string $ipRange The new ip range.
384 *
385 * @return null
386 */
387 function setIpRange($ipRange)
388 {
389 if (is_array($ipRange)) {
390 $ipRange = implode(';', $ipRange);
391 }
392
393 $this->ipRange = $ipRange;
394 }
395
396
397 /*
398 * Group roles functions.
399 */
400
401 /**
402 * Returns the roles in the group
403 *
404 * @return array
405 */
406 function getRoles()
407 {
408 if ($this->id == null) {
409 return array();
410 }
411
412 if ($this->roles != array()) {
413 return $this->roles;
414 }
415
416 global $wpdb;
417
418 $dbRoles = $wpdb->get_results(
419 "SELECT *
420 FROM " . DB_ACCESSGROUP_TO_ROLE . "
421 WHERE group_id = " . $this->id,
422 ARRAY_A
423 );
424
425 if (isset($dbRoles)) {
426 foreach ($dbRoles as $dbRole) {
427 $this->roles[trim($dbRole['role_name'])] = $dbRole;
428 }
429 }
430
431 return $this->roles;
432 }
433
434 /**
435 * Adds a role to the role group.
436 *
437 * @param integer $roleName The role name which should be added.
438 *
439 * @return null
440 */
441 function addRole($roleName)
442 {
443 $this->getRoles();
444
445 /**
446 * $this->roles[$roleName] = &get_role($roleName);
447 * Makes trouble, but why?
448 * Error: "Notice: Only variable references should be returned by reference"
449 */
450
451 $this->roles[$roleName] = array('role_name' => $roleName);
452 }
453
454 /**
455 * Removes a role from the role group.
456 *
457 * @param integer $roleName The role name which should be removed.
458 *
459 * @return null
460 */
461 function removeRole($roleName)
462 {
463 $this->getRoles();
464 unset($this->roles[$roleName]);
465 }
466
467 /**
468 * Unsets the roles.
469 *
470 * @param boolean $plusRemove If true also database entrys will remove.
471 *
472 * @return null;
473 */
474 function unsetRoles($plusRemove = false)
475 {
476 if ($plusRemove) {
477 $this->_deleteRolesFromDb();
478 }
479
480 $this->roles = array();
481 }
482
483 /**
484 * Removes all roles from the user group.
485 *
486 * @return null
487 */
488 private function _deleteRolesFromDb()
489 {
490 if ($this->id == null) {
491 return false;
492 }
493
494 global $wpdb;
495
496 $wpdb->query(
497 "DELETE FROM " . DB_ACCESSGROUP_TO_ROLE . "
498 WHERE group_id = ".$this->id
499 );
500 }
501
502
503 /*
504 * Group users functions.
505 */
506
507 /**
508 * Returns the assigned users.
509 *
510 * @return array
511 */
512 private function _getAssignedUsers()
513 {
514 if ($this->_assignedUsers !== null) {
515 return $this->_assignedUsers;
516 }
517
518 global $wpdb;
519
520 $dbUsers = $wpdb->get_results(
521 "SELECT user_id
522 FROM " . DB_ACCESSGROUP_TO_USER . "
523 WHERE group_id = " . $this->id
524 );
525
526 $this->_assignedUsers = array();
527
528 foreach ($dbUsers as $dbUser) {
529 $this->_assignedUsers[$dbUser->user_id] = $dbUser->user_id;
530 }
531
532 return $this->_assignedUsers;
533 }
534
535 /**
536 * Checks it the user is assigned to the group.
537 *
538 * @param integer $userId The user id.
539 *
540 * @return boolean
541 */
542 private function _isUserAssignedToGroup($userId)
543 {
544 if (array_key_exists($userId, $this->_getAssignedUsers())) {
545 return true;
546 } else {
547 return false;
548 }
549 }
550
551 /**
552 * Returns a single user.
553 *
554 * @param object $user The user.
555 * @param string $type The return type. Can be real or full.
556 *
557 * @return object
558 */
559 private function _getSingleUser($user, $type)
560 {
561 if (!isset($user->ID)) {
562 return null;
563 }
564
565 if (isset($this->singleUsers[$user->ID])) {
566 return $this->singleUsers[$user->ID];
567 }
568
569 $isRecursiveMember = array();
570
571 if ($type == 'full') {
572 global $wpdb;
573
574 $curUserdata = get_userdata($user->ID);
575
576 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
577 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
578 } else {
579 $capabilities = null;
580 }
581
582 $role = is_array($capabilities) ?
583 array_keys($capabilities) : 'norole';
584
585 if (array_key_exists($role[0], $this->getRoles())
586 ) {
587 $isRecursiveMember
588 = array('byRole' => array());
589 $isRecursiveMember['byRole'][]
590 = $role[0];
591 }
592 }
593
594 if ($this->_isUserAssignedToGroup($user->ID)
595 || $isRecursiveMember != array()
596 ) {
597 if ($isRecursiveMember != array()) {
598 $user->recursiveMember = $isRecursiveMember;
599 }
600
601 $this->singleUsers[$user->ID] = $user;
602 } else {
603 $this->singleUsers[$user->ID] = null;
604 }
605
606 return $this->singleUsers[$user->ID];
607 }
608
609 /**
610 * Returns the users in the group
611 *
612 * @param string $type The return type. Can be real or full.
613 *
614 * @return array
615 */
616 function getUsers($type = 'real')
617 {
618 if ($this->id == null) {
619 return array();
620 }
621
622 if ($type != 'real'
623 && $type != 'full'
624 ) {
625 return array();
626 }
627
628 if ($this->users[$type] != -1) {
629 return $this->users[$type];
630 } else {
631 $this->users[$type] = array();
632 }
633
634 global $wpdb;
635
636 if ($type == 'real') {
637 $dbUsers = $wpdb->get_results(
638 "SELECT user_id as ID
639 FROM " . DB_ACCESSGROUP_TO_USER . "
640 WHERE group_id = " . $this->id
641 );
642 } elseif ($type == 'full') {
643 $dbUsers = $wpdb->get_results(
644 "SELECT ID, user_nicename
645 FROM ".$wpdb->users
646 );
647 }
648
649 if (isset($dbUsers)) {
650 foreach ($dbUsers as $dbUser) {
651 $user = $this->_getSingleUser($dbUser, $type);
652
653 if ($user !== null) {
654 $this->users[$type][$user->ID] = $user;
655 }
656 }
657 }
658
659 return $this->users[$type];
660 }
661
662 /**
663 * Adds a user to the user group.
664 *
665 * @param integer $userId The user id which should be added.
666 *
667 * @return null
668 */
669 function addUser($userId)
670 {
671 $this->getUsers();
672 $this->users['real'][$userId] = get_userdata($userId);
673 $this->users['full'] = -1;
674 }
675
676 /**
677 * Removes a user from the user group.
678 *
679 * @param integer $userId The user id which should be removed.
680 *
681 * @return null
682 */
683 function removeUser($userId)
684 {
685 $this->getUsers();
686 unset($this->users['real'][$userId]);
687 $this->users['full'] = -1;
688 }
689
690 /**
691 * Unsets the users.
692 *
693 * @param boolean $plusRemove If true also database entrys will remove.
694 *
695 * @return null;
696 */
697 function unsetUsers($plusRemove = false)
698 {
699 if ($plusRemove) {
700 $this->_deleteUsersFromDb();
701 }
702
703 $this->users = array(
704 'real' => array(),
705 'full' => array(),
706 );
707 }
708
709 /**
710 * Removes all users from the user group.
711 *
712 * @return null
713 */
714 private function _deleteUsersFromDb()
715 {
716 global $wpdb;
717
718 $wpdb->query(
719 "DELETE FROM " . DB_ACCESSGROUP_TO_USER . "
720 WHERE group_id = ".$this->id
721 );
722 }
723
724 /**
725 * Checks if the given user is a member of the group.
726 *
727 * @param interger $userId The id of the user which should be checked.
728 * @param boolean $withInfo If true then we return additional infos.
729 *
730 * @return boolean
731 */
732 function userIsMember($userId, $withInfo = false)
733 {
734 $user->ID = $userId;
735 $user = $this->_getSingleUser($user, 'full');
736
737 if ($user !== null) {
738 if (isset($user->recursiveMember)
739 && $withInfo
740 ) {
741 return $user->recursiveMember;
742 }
743
744 return true;
745 }
746
747 return false;
748 }
749
750
751 /*
752 * Group categories functions.
753 */
754
755 /**
756 * Returns the assigned categories.
757 *
758 * @return array
759 */
760 private function _getAssignedCategories()
761 {
762 if ($this->_assignedCategories !== null) {
763 return $this->_assignedCategories;
764 }
765
766 global $wpdb;
767
768 $dbCategories = $wpdb->get_results(
769 "SELECT *
770 FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
771 WHERE group_id = " . $this->id
772 );
773
774 $this->_assignedCategories = array();
775
776 foreach ($dbCategories as $dbCategory) {
777 $this->_assignedCategories[$dbCategory->category_id]
778 = $dbCategory->category_id;
779 }
780
781 return $this->_assignedCategories;
782 }
783
784 /**
785 * Checks it the category is assigned to the group.
786 *
787 * @param integer $categoryId The category id.
788 *
789 * @return boolean
790 */
791 private function _isCategoryAssignedToGroup($categoryId)
792 {
793 if (array_key_exists($categoryId, $this->_getAssignedCategories())) {
794 return true;
795 } else {
796 return false;
797 }
798 }
799
800 /**
801 * Returns a single category.
802 *
803 * @param object $category The category.
804 * @param string $type The return type. Can be real or full.
805 *
806 * @return object
807 */
808 private function _getSingleCategory($category, $type)
809 {
810 if (!isset($category->term_id)) {
811 return null;
812 }
813
814 if (isset($this->singleCategories[$category->term_id])) {
815 return $this->singleCategories[$category->term_id];
816 }
817
818 $isRecursiveMember = array();
819
820 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
821 $uamOptions = $userAccessManager->getAdminOptions();
822
823 if ($uamOptions['lock_recursive'] == 'true'
824 && $type == 'full'
825 ) {
826 if ($category->parent != 0) {
827 $parentCategory = get_category($category->parent);
828
829 $parentCategory = $this->_getSingleCategory(
830 $parentCategory,
831 $type
832 );
833
834 if ($parentCategory !== null) {
835 if (isset($parentCategory->recursiveMember)) {
836 $isRecursiveMember['byCategory'][]
837 = $parentCategory;
838 } else {
839 $isRecursiveMember['byCategory'][]
840 = $parentCategory->term_id;
841 }
842 }
843 }
844 }
845
846 if ($this->_isCategoryAssignedToGroup($category->term_id)
847 || $isRecursiveMember != array()
848 ) {
849 if ($isRecursiveMember != array()) {
850 $category->recursiveMember = $isRecursiveMember;
851 }
852
853 $this->singleCategories[$category->term_id] = $category;
854 } else {
855 $this->singleCategories[$category->term_id] = null;
856 }
857
858 return $this->singleCategories[$category->term_id];
859 }
860
861 /**
862 * Returns the categories in the group
863 *
864 * @param string $type The return type. Can be real or full.
865 *
866 * @return array
867 */
868 function getCategories($type = 'real')
869 {
870 if ($this->id == null) {
871 return array();
872 }
873
874 if ($type != 'real'
875 && $type != 'full'
876 ) {
877 return array();
878 }
879
880 if ($this->categories[$type] != -1) {
881 return $this->categories[$type];
882 } else {
883 $this->categories[$type] = array();
884 }
885
886 global $wpdb;
887 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
888 $uamOptions = $userAccessManager->getAdminOptions();
889
890 $dbCategories = $wpdb->get_results(
891 "SELECT *
892 FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
893 WHERE group_id = " . $this->id,
894 ARRAY_A
895 );
896
897 if (isset($dbCategories)) {
898 foreach ($dbCategories as $dbCategorie) {
899 $category = get_category($dbCategorie['category_id']);
900
901 if ($category != null) {
902 if ($uamOptions['lock_recursive'] == 'true'
903 && $type == 'full'
904 ) {
905 //We have to remove the filter to get all categories
906 $removeSucc = remove_filter(
907 'get_terms',
908 array(
909 $this->getAccessHandler()->getUserAccessManager(),
910 'showCategory'
911 )
912 );
913
914 if ($removeSucc) {
915 $args = array(
916 'child_of' => $category->term_id,
917 'hide_empty' => false
918 );
919
920 $categoryChilds = get_categories($args);
921
922 add_filter(
923 'get_terms',
924 array(&$userAccessManager, 'showCategory')
925 );
926
927 foreach ($categoryChilds as $categoryChild) {
928 $categoryChild->recursiveMember
929 = array('byCategory' => array());
930 $categoryChild->recursiveMember['byCategory'][]
931 = $category->term_id;
932 $this->categories[$type][$categoryChild->term_id]
933 = $categoryChild;
934 }
935 }
936 }
937
938 $this->categories[$type][$category->term_id] = $category;
939 }
940 }
941 }
942
943 return $this->categories[$type];
944 }
945
946 /**
947 * Adds a category to the user group.
948 *
949 * @param integer $categoryId The category id which should be added.
950 *
951 * @return null
952 */
953 function addCategory($categoryId)
954 {
955 $this->getCategories();
956 $this->categories['real'][$categoryId] = get_category($categoryId);
957 $this->categories['full'] = -1;
958 }
959
960 /**
961 * Removes a category from the user group.
962 *
963 * @param integer $categoryId The category id which should be removed.
964 *
965 * @return null
966 */
967 function removeCategory($categoryId)
968 {
969 $this->getCategories();
970 unset($this->categories['real'][$categoryId]);
971 $this->categories['full'] = -1;
972 }
973
974 /**
975 * Unsets the categories.
976 *
977 * @param boolean $plusRemove If true also database entrys will remove.
978 *
979 * @return null;
980 */
981 function unsetCategories($plusRemove = false)
982 {
983 if ($plusRemove) {
984 $this->_deleteCategoriesFromDb();
985 }
986
987 $this->categories = array(
988 'real' => array(),
989 'full' => array(),
990 );
991 }
992
993 /**
994 * Removes all categories from the user group.
995 *
996 * @return null
997 */
998 private function _deleteCategoriesFromDb()
999 {
1000 global $wpdb;
1001
1002 $wpdb->query(
1003 "DELETE FROM " . DB_ACCESSGROUP_TO_CATEGORY . "
1004 WHERE group_id = ".$this->id
1005 );
1006 }
1007
1008 /**
1009 * Checks if the given post is a member of the group.
1010 *
1011 * @param interger $categoryId The id of the category which should be checked.
1012 * @param boolean $withInfo If true then we return additional infos.
1013 *
1014 * @return boolean
1015 */
1016 function categoryIsMember($categoryId, $withInfo = false)
1017 {
1018 $category = get_category($categoryId);
1019 $category = $this->_getSingleCategory($category, 'full');
1020
1021 if ($category !== null) {
1022 if (isset($category->recursiveMember)
1023 && $withInfo
1024 ) {
1025 return $category->recursiveMember;
1026 }
1027
1028 return true;
1029 }
1030
1031 return false;
1032 }
1033
1034
1035 /*
1036 * Group posts functions.
1037 */
1038
1039 /**
1040 * Returns the assigned posts.
1041 *
1042 * @return array
1043 */
1044 private function _getAssignedPosts()
1045 {
1046 if ($this->_assignedPosts !== null) {
1047 return $this->_assignedPosts;
1048 }
1049
1050 global $wpdb;
1051
1052 $dbPosts = $wpdb->get_results(
1053 "SELECT post_id
1054 FROM " . DB_ACCESSGROUP_TO_POST . "
1055 WHERE group_id = " . $this->id
1056 );
1057
1058 $this->_assignedPosts = array();
1059
1060 foreach ($dbPosts as $dbPost) {
1061 $this->_assignedPosts[$dbPost->post_id] = $dbPost->post_id;
1062 }
1063
1064 return $this->_assignedPosts;
1065 }
1066
1067 /**
1068 * Checks it the post is assigned to the group.
1069 *
1070 * @param integer $postId The post id.
1071 *
1072 * @return boolean
1073 */
1074 private function _isPostAssignedToGroup($postId)
1075 {
1076 if (array_key_exists($postId, $this->_getAssignedPosts())) {
1077 return true;
1078 } else {
1079 return false;
1080 }
1081 }
1082
1083 /**
1084 * Returns the membership of a single post.
1085 *
1086 * @param object $post The post object.
1087 * @param string $type The return type.
1088 * @param string $postType The post type needed for the intern representation.
1089 *
1090 * @return object
1091 */
1092 function _getSinglePost($post, $type, $postType)
1093 {
1094 if (!isset($post->ID)) {
1095 return null;
1096 }
1097
1098 if (isset($this->singlePosts[$post->ID])) {
1099 return $this->singlePosts[$post->ID];
1100 }
1101
1102 $isRecursiveMember = array();
1103
1104 $userAccessManager = &$this->getAccessHandler()->getUserAccessManager();
1105 $uamOptions = $userAccessManager->getAdminOptions();
1106
1107 if ($type == 'full') {
1108 foreach ($this->getCategories('full') as $category) {
1109 if (in_category($category->cat_ID, $post->ID)) {
1110 $isRecursiveMember['byCategory'][] = $category->cat_ID;
1111 //break;
1112 }
1113 }
1114
1115 if (($postType == 'page'
1116 || $postType == 'file')
1117 && $uamOptions['lock_recursive'] == 'true'
1118 ) {
1119 if ($post->post_parent != 0) {
1120 $parentPost = get_post($post->post_parent);
1121
1122 $parentPost = $this->_getSinglePost(
1123 $parentPost,
1124 $type,
1125 $parentPost->post_type
1126 );
1127
1128 if ($parentPost !== null) {
1129 if (isset($parentPost->recursiveMember)) {
1130 $isRecursiveMember['byPost'][]
1131 = $parentPost;
1132 } else {
1133 $isRecursiveMember['byPost'][]
1134 = $parentPost->ID;
1135 }
1136 }
1137 }
1138 }
1139 }
1140
1141 if ($this->_isPostAssignedToGroup($post->ID)
1142 || $isRecursiveMember != array()
1143 ) {
1144 if ($isRecursiveMember != array()) {
1145 $post->recursiveMember = $isRecursiveMember;
1146 }
1147 $this->singlePosts[$post->ID] = $post;
1148 } else {
1149 $this->singlePosts[$post->ID] = null;
1150 }
1151
1152 return $this->singlePosts[$post->ID];
1153 }
1154
1155 /**
1156 * Returns the posts by the given type in the group
1157 *
1158 * @param string $postType The type of the post.
1159 * @param string $type The return type. Can be real or full.
1160 *
1161 * @return array
1162 */
1163 private function _getPostByType($postType, $type)
1164 {
1165 if ($type != 'real'
1166 && $type != 'full'
1167 ) {
1168 return array();
1169 }
1170
1171 if ($postType == 'file') {
1172 $wpType = 'attachment';
1173 } else {
1174 $wpType = $postType;
1175 }
1176
1177 if ($this->{$postType.'s'}[$type] != -1) {
1178 return $this->{$postType.'s'}[$type];
1179 } else {
1180 $this->{$postType.'s'}[$type] = array();
1181 }
1182
1183 global $wpdb;
1184
1185 $posts = $wpdb->get_results(
1186 "SELECT ID, post_parent
1187 FROM $wpdb->posts
1188 WHERE post_type = '".$wpType."'"
1189 );
1190
1191 if (isset($posts)) {
1192 foreach ($posts as $post) {
1193 $post = $this->_getSinglePost($post, $type, $postType);
1194
1195 if ($post !== null) {
1196 $this->{$postType.'s'}[$type][$post->ID] = $post;
1197 }
1198 }
1199 }
1200
1201 return $this->{$postType.'s'}[$type];
1202 }
1203
1204 /**
1205 * Removes all post of all types from the user group.
1206 *
1207 * @param string $postType The type which should be deleted.
1208 *
1209 * @return null;
1210 */
1211 private function _deletePostByTypeFromDb($postType)
1212 {
1213 if ($this->id == null) {
1214 return false;
1215 }
1216
1217 global $wpdb;
1218
1219 if ($postType == 'all') {
1220 $wpdb->query(
1221 "DELETE FROM " . DB_ACCESSGROUP_TO_POST . "
1222 WHERE group_id = ".$this->id
1223 );
1224 } else {
1225 if ($type == 'post') {
1226 $curPostTypes = $this->getPosts();
1227 } elseif ($type == 'page') {
1228 $curPostTypes = $this->getPages();
1229 } elseif ($type == 'file') {
1230 $curPostTypes = $this->getFiles();
1231 }
1232
1233 foreach ($curPostTypes as $id => $post) {
1234 $wpdb->query(
1235 "DELETE FROM " . DB_ACCESSGROUP_TO_POST . "
1236 WHERE group_id = ".$this->id."
1237 AND post_id = ".$id."
1238 LIMIT 1"
1239 );
1240 }
1241 }
1242 }
1243
1244 /**
1245 * Returns the pages in the group
1246 *
1247 * @param string $type The return type. Can be real or full.
1248 *
1249 * @return array
1250 */
1251 function getPosts($type = 'real')
1252 {
1253 return $this->_getPostByType('post', $type);
1254 }
1255
1256 /**
1257 * Adds a post to the user group.
1258 *
1259 * @param integer $postId The post id which should be added.
1260 *
1261 * @return null
1262 */
1263 function addPost($postId)
1264 {
1265 $this->getPosts();
1266 $this->posts['real'][$postId] = get_post($postId);
1267 $this->posts['full'] = -1;
1268 }
1269
1270 /**
1271 * Removes a post from the user group.
1272 *
1273 * @param integer $postId The post id which should be removed.
1274 *
1275 * @return null
1276 */
1277 function removePost($postId)
1278 {
1279 $this->getPosts();
1280 unset($this->posts['real'][$postId]);
1281 $this->posts['full'] = -1;
1282 }
1283
1284 /**
1285 * Unsets the posts.
1286 *
1287 * @param boolean $plusRemove If true also database entrys will remove.
1288 *
1289 * @return null;
1290 */
1291 function unsetPosts($plusRemove = false)
1292 {
1293 if ($plusRemove) {
1294 $this->_deletePostByTypeFromDb('post');
1295 }
1296
1297 $this->posts = array(
1298 'real' => array(),
1299 'full' => array(),
1300 );;
1301 }
1302
1303 /**
1304 * Returns the pages in the group.
1305 *
1306 * @param string $type The return type. Can be real or full.
1307 *
1308 * @return array
1309 */
1310 function getPages($type = 'real')
1311 {
1312 return $this->_getPostByType('page', $type);
1313 }
1314
1315 /**
1316 * Adds a page to the page group.
1317 *
1318 * @param integer $pageID The page id which should be added.
1319 *
1320 * @return null
1321 */
1322 function addPage($pageID)
1323 {
1324 $this->getPages();
1325 $this->pages['real'][$pageID] = get_post($pageID);
1326 $this->pages['full'] = -1;
1327 }
1328
1329 /**
1330 * Removes a page from the page group.
1331 *
1332 * @param integer $pageID The page id which should be removed.
1333 *
1334 * @return null
1335 */
1336 function removePage($pageID)
1337 {
1338 $this->getPages();
1339 unset($this->pages['real'][$pageID]);
1340 $this->pages['full'] = -1;
1341 }
1342
1343 /**
1344 * Unsets the pages.
1345 *
1346 * @param boolean $plusRemove If true also database entrys will remove.
1347 *
1348 * @return null;
1349 */
1350 function unsetPages($plusRemove = false)
1351 {
1352 if ($plusRemove) {
1353 $this->_deletePostByTypeFromDb('page');
1354 }
1355
1356 $this->pages = array(
1357 'real' => array(),
1358 'full' => array(),
1359 );;
1360 }
1361
1362 /**
1363 * Returns the files in the group
1364 *
1365 * @param string $type The return type. Can be real or full.
1366 *
1367 * @return array
1368 */
1369 function getFiles($type = 'real')
1370 {
1371 return $this->_getPostByType('file', $type);
1372 }
1373
1374 /**
1375 * Adds a file to the file group.
1376 *
1377 * @param integer $fileID The file id which should be added.
1378 *
1379 * @return null
1380 */
1381 function addFile($fileID)
1382 {
1383 $this->getFiles();
1384 $this->files['real'][$fileID] = get_post($fileID);
1385 $this->files['full'] = -1;
1386 }
1387
1388 /**
1389 * Removes a file from the file group.
1390 *
1391 * @param integer $fileID The file id which should be removed.
1392 *
1393 * @return null
1394 */
1395 function removeFile($fileID)
1396 {
1397 $this->getFiles();
1398 unset($this->files['real'][$fileID]);
1399 $this->files['full'] = -1;
1400 }
1401
1402 /**
1403 * Unsets the files.
1404 *
1405 * @param boolean $plusRemove If true also database entrys will remove.
1406 *
1407 * @return null;
1408 */
1409 function unsetFiles($plusRemove = false)
1410 {
1411 if ($plusRemove) {
1412 $this->_deletePostByTypeFromDb('file');
1413 }
1414
1415 $this->files = array(
1416 'real' => array(),
1417 'full' => array(),
1418 );
1419 }
1420
1421 /**
1422 * Checks if the given post is a member of the group.
1423 *
1424 * @param interger $postId The id of the post which should be checked.
1425 * @param boolean $withInfo If true then we return additional infos.
1426 *
1427 * @return boolean
1428 */
1429 function postIsMember($postId, $withInfo = false)
1430 {
1431 $post = get_post($postId);
1432
1433 if ($post->post_type == 'attachment') {
1434 $postType = 'file';
1435 } else {
1436 $postType = $post->post_type;
1437 }
1438
1439 $post = $this->_getSinglePost($post, 'full', $postType);
1440
1441 if ($post !== null) {
1442 if (isset($post->recursiveMember)
1443 && $withInfo
1444 ) {
1445 return $post->recursiveMember;
1446 }
1447
1448 return true;
1449 }
1450
1451 return false;
1452 }
1453
1454 /*
1455 * Group pluggable objects functions.
1456 */
1457
1458 /**
1459 * Returns the assigned pluggable object.
1460 *
1461 * @param string $object The name of the object.
1462 *
1463 * @return array
1464 */
1465 private function _getAssignedPluggableObjects($object)
1466 {
1467 if ($this->_assignedPluggableObjects[$object] !== null) {
1468 return $this->_assignedPluggableObjects[$object];
1469 }
1470
1471 global $wpdb;
1472
1473 $dbObjects = $wpdb->get_results(
1474 "SELECT *
1475 FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1476 WHERE group_id = " . $this->id . "
1477 AND object_type = '" . $object ."'"
1478 );
1479
1480 $this->_assignedPluggableObjects[$object] = array();
1481
1482 foreach ($dbObjects as $dbObject) {
1483 $this->_assignedPluggableObjects[$object][$dbObject->object_id]
1484 = $dbObject->object_id;
1485 }
1486
1487 return $this->_assignedPluggableObjects[$object];
1488 }
1489
1490 /**
1491 * Checks it the category is assigned to the group.
1492 *
1493 * @param string $object The name of the object.
1494 * @param integer $objectId The object id.
1495 *
1496 * @return boolean
1497 */
1498 private function _isPluggableObjectAssignedToGroup($object, $objectId)
1499 {
1500 if (array_key_exists($objectId, $this->_getAssignedPluggableObjects($object))) {
1501 return true;
1502 } else {
1503 return false;
1504 }
1505 }
1506
1507 /**
1508 * Returns a single pluggable object.
1509 *
1510 * @param object $object The pluggable object.
1511 * @param integer $objectId The object id.
1512 * @param string $type The return type. Can be real or full.
1513 *
1514 * @return object
1515 */
1516 private function _getSinglePluggableObject($object, $objectId, $type = 'full')
1517 {
1518 if (!isset($objectId)) {
1519 return null;
1520 }
1521
1522 if (isset($this->singlePluggableObjects[$objectId])) {
1523 return $this->singlePluggableObjects[$objectId];
1524 }
1525
1526 $isRecursiveMember = array();
1527
1528 $userAccessManager = &$this->getAccessHandler()->getUserAccessManager();
1529 $uamOptions = $userAccessManager->getAdminOptions();
1530
1531 if ($type == 'full') {
1532 //TODO Function for full.
1533 }
1534
1535 if ($this->_isPostAssignedToGroup($objectId)
1536 || $isRecursiveMember != array()
1537 ) {
1538 if ($isRecursiveMember != array()) {
1539 $object->recursiveMember = $isRecursiveMember;
1540 }
1541 $this->singlePluggableObjects[$objectId] = $object;
1542 } else {
1543 $this->singlePluggableObjects[$objectId] = null;
1544 }
1545
1546 return $this->singlePluggableObjects[$objectId];
1547 }
1548
1549 /**
1550 * Adds a pluggable object to the user group.
1551 *
1552 * @param string $object The name of the object.
1553 * @param integer $objectId The object id which should be added.
1554 *
1555 * @return null
1556 */
1557 function addPluggableObject($object, $objectId)
1558 {
1559 $this->getPluggableObjects();
1560 $pluggableObject = $this->getAccessHandler()->getPluggableObject($object);
1561
1562 $this->pluggableObjects[$object]['real'][$objectId]
1563 = $pluggableObject['ref']->{$pluggableObject['getObject']}($objectId);
1564 $this->pluggableObjects[$object]['full'] = -1;
1565 }
1566
1567 /**
1568 * Removes a pluggable from the user group.
1569 *
1570 * @param string $object The name of the object.
1571 * @param integer $objectId The object id which should be removed.
1572 *
1573 * @return null
1574 */
1575 function removePluggableObject($object, $objectId)
1576 {
1577 $this->getPluggableObjects();
1578 unset($this->pluggableObjects[$object]['real'][$objectId]);
1579 $this->pluggableObjects[$object]['full'] = -1;
1580 }
1581
1582 /**
1583 * Unsets the categories.
1584 *
1585 * @param string $object The name of the object.
1586 * @param boolean $plusRemove If true also database entrys will remove.
1587 *
1588 * @return null;
1589 */
1590 function unsetPluggableObject($object, $plusRemove = false)
1591 {
1592 if ($plusRemove) {
1593 $this->_deletePluggableObjectFromDb($object);
1594 }
1595
1596 $this->pluggableObjects[$object] = array(
1597 'real' => array(),
1598 'full' => array(),
1599 );
1600 }
1601
1602 /**
1603 * Removes all categories from the user group.
1604 *
1605 * @param string $object The name of the object.
1606 *
1607 * @return null
1608 */
1609 private function _deletePluggableObjectFromDb($object)
1610 {
1611 global $wpdb;
1612
1613 $wpdb->query(
1614 "DELETE FROM " . DB_ACCESSGROUP_TO_OBJECT . "
1615 WHERE group_id = " . $this->id . "
1616 AND object_type = '" . $object . "'"
1617 );
1618 }
1619
1620 /**
1621 * Checks if the given post is a member of the group.
1622 *
1623 * @param string $object The name of the object.
1624 * @param interger $objectId The id of the object which should be checked.
1625 * @param boolean $withInfo If true then we return additional infos.
1626 *
1627 * @return boolean
1628 */
1629 function pluggableObjectIsMember($object, $objectId, $withInfo = false)
1630 {
1631 $pluggableObject = $this->getAccessHandler()->getPluggableObject($object);
1632
1633 $object
1634 = $pluggableObject['ref']->{$pluggableObject['getObject']}($objectId);
1635 $object = $this->_getSinglePluggableObject($object, $objectId, 'full');
1636
1637 if ($object !== null) {
1638 if (isset($object->recursiveMember)
1639 && $withInfo
1640 ) {
1641 return $object->recursiveMember;
1642 }
1643
1644 return true;
1645 }
1646
1647 return false;
1648 }
1649 }