PluginProbe
User Access Manager / 1.2.9
User Access Manager v1.2.9
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 / UamAccessHandler.php

UamAccessHandler.php in User Access Manager 1.2.9, at class/UamAccessHandler.php

882 lines 29.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UamAccessHandler.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-2016 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 access handler 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 class UamAccessHandler
28 {
29 const OBJECTS_FILTERED = 'filtered';
30 const OBJECTS_NONE_FILTERED = 'noneFiltered';
31
32 protected $_oUserAccessManager = null;
33 protected $_aObjectUserGroups = array();
34 protected $_aObjectAccess = array();
35 protected $_aUserGroups = array(
36 self::OBJECTS_FILTERED => array(),
37 self::OBJECTS_NONE_FILTERED => array(),
38 );
39 protected $_aPlObjects = array();
40 protected $_aObjectTypes = array(
41 UserAccessManager::TERM_OBJECT_TYPE => UserAccessManager::TERM_OBJECT_TYPE,
42 UserAccessManager::USER_OBJECT_TYPE => UserAccessManager::USER_OBJECT_TYPE,
43 UserAccessManager::ROLE_OBJECT_TYPE => UserAccessManager::ROLE_OBJECT_TYPE
44 );
45 protected $_aPostableTypes = array(
46 UserAccessManager::POST_OBJECT_TYPE => UserAccessManager::POST_OBJECT_TYPE,
47 UserAccessManager::PAGE_OBJECT_TYPE => UserAccessManager::PAGE_OBJECT_TYPE,
48 UserAccessManager::ATTACHMENT_OBJECT_TYPE => UserAccessManager::ATTACHMENT_OBJECT_TYPE
49 );
50 protected $_aAllObjectTypes = null;
51 protected $_aAllObjectTypesMap = null;
52 protected $_aSqlResults = array();
53 protected $_aValidObjectTypes = array();
54
55 /**
56 * The constructor
57 *
58 * @param UserAccessManager $oUserAccessManager The user access manager object.
59 */
60 public function __construct(UserAccessManager &$oUserAccessManager)
61 {
62 $this->_oUserAccessManager = $oUserAccessManager;
63 $this->_aPostableTypes = array_merge($this->_aPostableTypes, $oUserAccessManager->getPostTypes());
64 $this->_aObjectTypes = array_merge($this->_aPostableTypes, $this->_aObjectTypes, $oUserAccessManager->getTaxonomies());
65 add_action('registered_post_type', array( &$this, 'registeredPostType'), 10, 2);
66 }
67
68 /**
69 * used for adding custom post types using the registered_post_type hook
70 * @see http://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook
71 *
72 * @param string $sPostType The string for the new post_type
73 * @param stdClass $oArgs The array of arguments used to create the post_type
74 *
75 */
76 public function registeredPostType($sPostType, $oArgs)
77 {
78 if ($oArgs->publicly_queryable) {
79 $this->_aPostableTypes[$oArgs->name] = $oArgs->name;
80 $this->_aPostableTypes = array_unique($this->_aPostableTypes);
81 $this->_aObjectTypes = array_merge($this->_aPostableTypes, $this->_aObjectTypes);
82 $this->_aAllObjectTypes = null;
83 $this->_aAllObjectTypesMap = null;
84 $this->_aValidObjectTypes = null;
85 }
86 }
87
88 /**
89 * Checks if type is postable.
90 *
91 * @param string $sType
92 *
93 * @return bool
94 */
95 public function isPostableType($sType)
96 {
97 return isset($this->_aPostableTypes[$sType]);
98 }
99
100 /**
101 * Returns the user access manager object.
102 *
103 * @return UserAccessManager
104 */
105 public function &getUserAccessManager()
106 {
107 return $this->_oUserAccessManager;
108 }
109
110 /**
111 * Returns the predefined object types.
112 *
113 * @return array
114 */
115 public function getObjectTypes()
116 {
117 return $this->_aObjectTypes;
118 }
119
120 /**
121 * Returns the predefined object types.
122 *
123 * @return array;
124 */
125 public function getPostableTypes()
126 {
127 return $this->_aPostableTypes;
128 }
129
130 /**
131 * Returns all objects types.
132 *
133 * @return array
134 */
135 public function getAllObjectTypes()
136 {
137 if ($this->_aAllObjectTypes === null) {
138 $aPlObjects = $this->getPlObjects();
139
140 $this->_aAllObjectTypes = array_merge(
141 $this->_aObjectTypes,
142 array_keys($aPlObjects)
143 );
144 }
145
146 return $this->_aAllObjectTypes;
147 }
148
149 /**
150 * Returns all objects types as map.
151 *
152 * @return array
153 */
154 public function getAllObjectTypesMap()
155 {
156 if ($this->_aAllObjectTypesMap === null) {
157 $this->_aAllObjectTypesMap = array_flip($this->getAllObjectTypes());
158 }
159
160 return $this->_aAllObjectTypesMap;
161 }
162
163 /**
164 * Magic method getter.
165 *
166 * @param string $sName The name of the function
167 * @param array $aArguments The arguments for the function
168 *
169 * @return mixed
170 */
171 public function __call($sName, $aArguments)
172 {
173 $oUserAccessManager = $this->getUserAccessManager();
174
175 if ($oUserAccessManager->startsWith($sName, 'getUserGroupsFor')) {
176 $sPrefix = 'getUserGroupsFor';
177 } elseif ($oUserAccessManager->startsWith($sName, 'checkAccessFor')) {
178 $sPrefix = 'checkAccessFor';
179 }
180
181 if (isset($sPrefix)) {
182 $sObjectType = str_replace($sPrefix, '', $sName);
183 $sObjectType = strtolower($sObjectType);
184
185 $iObjectId = $aArguments[0];
186
187 if ($sPrefix == 'getUserGroupsFor') {
188 return $this->getUserGroupsForObject($sObjectType, $iObjectId);
189 } elseif ($sPrefix == 'checkAccessFor') {
190 return $this->checkObjectAccess($sObjectType, $iObjectId);
191 }
192 }
193
194 return null;
195 }
196
197 /**
198 * Filter the user groups of an object if authors_can_add_posts_to_groups
199 * option is enabled
200 *
201 * @param UamUserGroup[] $aUserGroups The user groups.
202 *
203 * @return array
204 */
205 protected function _filterUserGroups($aUserGroups)
206 {
207 $oConfig = $this->getUserAccessManager()->getConfig();
208
209 if ($oConfig->authorsCanAddPostsToGroups() === true
210 && !$this->checkUserAccess('manage_user_groups')
211 && $this->getUserAccessManager()->atAdminPanel()
212 ) {
213 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
214 $aUserGroupsForUser = $this->getUserGroupsForObject(UserAccessManager::USER_OBJECT_TYPE, $oCurrentUser->ID);
215
216 foreach ($aUserGroups as $sKey => $oUamUserGroup) {
217 if (!isset($aUserGroupsForUser[$oUamUserGroup->getId()])) {
218 unset($aUserGroups[$sKey]);
219 }
220 }
221 }
222
223 return $aUserGroups;
224 }
225
226 /**
227 * Checks if the object type is a valid one.
228 *
229 * @param string $sObjectType The object type to check.
230 *
231 * @return boolean
232 */
233 public function isValidObjectType($sObjectType)
234 {
235 if (!isset($this->_aValidObjectTypes[$sObjectType])) {
236 $aObjectTypesMap = $this->getAllObjectTypesMap();
237
238 if (isset($aObjectTypesMap[$sObjectType])) {
239 $this->_aValidObjectTypes[$sObjectType] = true;
240 } else {
241 $this->_aValidObjectTypes[$sObjectType] = false;
242 }
243 }
244
245 return $this->_aValidObjectTypes[$sObjectType];
246 }
247
248 /**
249 * Returns all user groups or one requested by the user group id.
250 *
251 * @param integer $iUserGroupId The id of the single user group which should be returned.
252 * @param boolean $blFilter Filter the groups.
253 *
254 * @return UamUserGroup[]|UamUserGroup
255 */
256 public function getUserGroups($iUserGroupId = null, $blFilter = true)
257 {
258 $sFilterAttr = ($blFilter === true) ? self::OBJECTS_FILTERED : self::OBJECTS_NONE_FILTERED;
259
260 if ($iUserGroupId === null
261 && $this->_aUserGroups[$sFilterAttr] != array()
262 ) {
263 return $this->_aUserGroups[$sFilterAttr];
264 } elseif ($iUserGroupId !== null
265 && $this->_aUserGroups[$sFilterAttr] != array()
266 ) {
267 if (isset($this->_aUserGroups[$sFilterAttr][$iUserGroupId])) {
268 return $this->_aUserGroups[$sFilterAttr][$iUserGroupId];
269 } else {
270 return null;
271 }
272 }
273
274 $this->_aUserGroups[$sFilterAttr] = array();
275
276 $oDatabase = $this->getUserAccessManager()->getDatabase();
277
278 $aUserGroupsDb = $oDatabase->get_results(
279 "SELECT ID
280 FROM " . DB_ACCESSGROUP . "
281 ORDER BY ID", ARRAY_A
282 );
283
284 if (isset($aUserGroupsDb)) {
285 foreach ($aUserGroupsDb as $aUserGroupDb) {
286 $this->_aUserGroups[$sFilterAttr][$aUserGroupDb['ID']] = new UamUserGroup($this, $aUserGroupDb['ID']);
287 }
288 }
289
290 //Filter the user groups
291 if ($blFilter) {
292 $this->_aUserGroups[$sFilterAttr] = $this->_filterUserGroups($this->_aUserGroups[$sFilterAttr]);
293 }
294
295 if ($iUserGroupId == null) {
296 if (isset($this->_aUserGroups[$sFilterAttr])) {
297 return $this->_aUserGroups[$sFilterAttr];
298 }
299
300 return array();
301 } else {
302 if (isset($this->_aUserGroups[$sFilterAttr][$iUserGroupId])) {
303 return $this->_aUserGroups[$sFilterAttr][$iUserGroupId];
304 }
305
306 return null;
307 }
308 }
309
310 /**
311 * Adds a user group.
312 *
313 * @param UamUserGroup $oUserGroup The user group which we want to add.
314 */
315 public function addUserGroup($oUserGroup)
316 {
317 $this->getUserGroups();
318 $this->_aUserGroups[self::OBJECTS_NONE_FILTERED][$oUserGroup->getId()] = $oUserGroup;
319 $this->_aUserGroups[self::OBJECTS_FILTERED] = array();
320 }
321
322 /**
323 * Deletes a user group.
324 *
325 * @param integer $iUserGroupId The user group _iId which we want to delete.
326 */
327 public function deleteUserGroup($iUserGroupId)
328 {
329 if ($this->getUserGroups($iUserGroupId) != null) {
330 $this->getUserGroups($iUserGroupId)->delete();
331 unset($this->_aUserGroups[self::OBJECTS_NONE_FILTERED][$iUserGroupId]);
332 $this->_aUserGroups[self::OBJECTS_FILTERED] = array();
333 }
334 }
335
336 /**
337 * Returns the user groups for the given object.
338 *
339 * @param string $sObjectType The object type.
340 * @param integer $iObjectId The _iId of the object.
341 * @param boolean $blFilter Filter the groups.
342 *
343 * @return UamUserGroup[]
344 */
345 public function getUserGroupsForObject($sObjectType, $iObjectId, $blFilter = true)
346 {
347 if (!$this->isValidObjectType($sObjectType)) {
348 return array();
349 }
350
351 $blFilter = ($sObjectType === UserAccessManager::USER_OBJECT_TYPE) ? false : $blFilter;
352 $sFilterAttr = ($blFilter === true) ? self::OBJECTS_FILTERED : self::OBJECTS_NONE_FILTERED;
353
354 if (!isset($this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId])) {
355 $sCacheKey = 'getUserGroupsForObject|' . $sObjectType . '|' . $sFilterAttr . '|' . $iObjectId;
356 $oUserAccessManager = $this->getUserAccessManager();
357 $aObjectUserGroups = $oUserAccessManager->getFromCache($sCacheKey);
358
359 if ($aObjectUserGroups !== null) {
360 $this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId] = $aObjectUserGroups;
361 } else {
362 $aObjectUserGroups = array();
363 $aUserGroups = $this->getUserGroups(null, $blFilter);
364
365 if (is_array($aUserGroups)) {
366 foreach ($aUserGroups as $oUserGroup) {
367 $mObjectMembership = $oUserGroup->objectIsMember($sObjectType, $iObjectId, true);
368
369 if ($mObjectMembership !== false) {
370 if (is_array($mObjectMembership)) {
371 $oUserGroup->setRecursiveMembership($sObjectType, $iObjectId, $mObjectMembership);
372 }
373
374 $aObjectUserGroups[$oUserGroup->getId()] = $oUserGroup;
375 }
376 }
377 }
378
379 //Filter the user groups
380 if ($blFilter) {
381 $aObjectUserGroups = $this->_filterUserGroups($aObjectUserGroups);
382 }
383
384 $oUserAccessManager->addToCache($sCacheKey, $aObjectUserGroups);
385 }
386
387 $this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId] = $aObjectUserGroups;
388 }
389
390 return $this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId];
391 }
392
393 /**
394 * Unset the user groups for _aObjects.
395 */
396 public function unsetUserGroupsForObject()
397 {
398 $this->_aObjectUserGroups = array();
399 }
400
401 /**
402 * Checks if the current_user has access to the given post.
403 *
404 * @param string $sObjectType The object type which should be checked.
405 * @param integer $iObjectId The _iId of the object.
406 *
407 * @return boolean
408 */
409 public function checkObjectAccess($sObjectType, $iObjectId)
410 {
411 if (!$this->isValidObjectType($sObjectType)) {
412 return true;
413 }
414
415 if (!isset($this->_aObjectAccess[$sObjectType][$iObjectId])) {
416 $this->_aObjectAccess[$sObjectType][$iObjectId] = false;
417 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
418
419 if ($this->isPostableType($sObjectType)) {
420 $oPost = $this->getUserAccessManager()->getPost($iObjectId);
421 $sAuthorId = $oPost->post_author;
422 } else {
423 $sAuthorId = -1;
424 }
425
426 $oConfig = $this->getUserAccessManager()->getConfig();
427 $aMembership = $this->getUserGroupsForObject($sObjectType, $iObjectId, false);
428
429 if ($aMembership == array()
430 || $this->checkUserAccess('manage_user_groups')
431 || $oCurrentUser->ID === $sAuthorId && $oConfig->authorsHasAccessToOwn() === true
432 ) {
433 $this->_aObjectAccess[$sObjectType][$iObjectId] = true;
434 } else {
435 $aCurrentIp = explode('.', $_SERVER['REMOTE_ADDR']);
436
437 foreach ($aMembership as $sKey => $oUserGroup) {
438 if ($oUserGroup->objectIsMember(UserAccessManager::USER_OBJECT_TYPE, $oCurrentUser->ID)
439 || $this->checkUserIp($aCurrentIp, $oUserGroup->getIpRange())
440 ) {
441 $this->_aObjectAccess[$sObjectType][$iObjectId] = true;
442 break;
443 } elseif ($this->getUserAccessManager()->atAdminPanel() && $oUserGroup->getWriteAccess() == 'all'
444 || !$this->getUserAccessManager()->atAdminPanel() && $oUserGroup->getReadAccess() == 'all'
445 ) {
446 unset($aMembership[$sKey]);
447 }
448 }
449
450 if ($aMembership == array()) {
451 $this->_aObjectAccess[$sObjectType][$iObjectId] = true;
452 }
453 }
454 }
455
456 return $this->_aObjectAccess[$sObjectType][$iObjectId];
457 }
458
459
460 /*
461 * SQL functions.
462 */
463
464 /**
465 * Returns the user groups for the current user as sql string.
466 *
467 * @return string
468 */
469 protected function _getUserGroupsForUserAsSqlString()
470 {
471 if (!isset($this->_aSqlResults['groupsForUser'])) {
472 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
473 $aUserUserGroups = $this->getUserGroupsForObject(UserAccessManager::USER_OBJECT_TYPE, $oCurrentUser->ID, false);
474 $aUserUserGroupIds = array();
475
476 foreach ($aUserUserGroups as $oUserUserGroup) {
477 $aUserUserGroupIds[] = $oUserUserGroup->getId();
478 }
479
480 if ($aUserUserGroupIds !== array()) {
481 $sUserUserGroups = implode(', ', $aUserUserGroupIds);
482 } else {
483 $sUserUserGroups = "''";
484 }
485
486 $this->_aSqlResults['groupsForUser'] = $sUserUserGroups;
487 }
488
489 return $this->_aSqlResults['groupsForUser'];
490 }
491
492 /**
493 * Returns the categories assigned to the user.
494 *
495 * @return array
496 */
497 public function getTermsForUser()
498 {
499 $oDatabase = $this->getUserAccessManager()->getDatabase();
500
501 if (!isset($this->_aSqlResults['termsAssignedToUser'])) {
502 $sUserUserGroups = $this->_getUserGroupsForUserAsSqlString();
503 $sTermType = UserAccessManager::TERM_OBJECT_TYPE;
504
505 $sTermsAssignedToUserSql = "
506 SELECT igc.object_id
507 FROM " . DB_ACCESSGROUP_TO_OBJECT . " AS igc
508 WHERE igc.object_type = '{$sTermType}'
509 AND igc.group_id IN ({$sUserUserGroups})";
510
511 $this->_aSqlResults['termsAssignedToUser'] = $oDatabase->get_col($sTermsAssignedToUserSql);
512 }
513
514 return $this->_aSqlResults['termsAssignedToUser'];
515 }
516
517 /**
518 * Returns the excluded terms for a user.
519 *
520 * @return array
521 */
522 public function getExcludedTerms()
523 {
524 if ($this->checkUserAccess('manage_user_groups')) {
525 $this->_aSqlResults['excludedTerms'] = array();
526 }
527
528 if (!isset($this->_aSqlResults['excludedTerms'])) {
529 $oDatabase = $this->getUserAccessManager()->getDatabase();
530 $sTermType = UserAccessManager::TERM_OBJECT_TYPE;
531 $sAccessType = ($this->getUserAccessManager()->atAdminPanel() === true) ? 'write' : 'read';
532 $aCategoriesAssignedToUser = $this->getTermsForUser();
533 $sCategoriesAssignedToUser = ($aCategoriesAssignedToUser !== array()) ? implode(', ', $aCategoriesAssignedToUser) : "''";
534
535 $sTermSql = "SELECT agto.object_id
536 FROM " . DB_ACCESSGROUP_TO_OBJECT . " agto
537 LEFT JOIN " . DB_ACCESSGROUP . " AS ag
538 ON agto.group_id = ag.id
539 WHERE agto.object_type = '{$sTermType}'
540 AND agto.object_id NOT IN ({$sCategoriesAssignedToUser})
541 AND ag.{$sAccessType}_access != 'all'";
542
543 $this->_aSqlResults['excludedTerms'] = $oDatabase->get_col($sTermSql);
544 }
545
546 return $this->_aSqlResults['excludedTerms'];
547 }
548
549 /**
550 * Returns the posts assigned to the user.
551 *
552 * @return array
553 */
554 public function getPostsForUser()
555 {
556 if (!isset($this->_aSqlResults['postsAssignedToUser'])) {
557 $oDatabase = $this->getUserAccessManager()->getDatabase();
558 $sUserUserGroup = $this->_getUserGroupsForUserAsSqlString();
559 $sPostableTypes = "'" . implode("','", $this->getPostableTypes()) . "'";
560
561 $sPostAssignedToUserSql = "
562 SELECT igp.object_id
563 FROM " . DB_ACCESSGROUP_TO_OBJECT . " AS igp
564 WHERE igp.object_type IN ({$sPostableTypes})
565 AND igp.group_id IN ({$sUserUserGroup})";
566
567 $this->_aSqlResults['postsAssignedToUser'] = $oDatabase->get_col($sPostAssignedToUserSql);
568 }
569
570 return $this->_aSqlResults['postsAssignedToUser'];
571 }
572
573 /**
574 * Returns the excluded posts.
575 *
576 * @return array
577 */
578 public function getExcludedPosts()
579 {
580 if ($this->checkUserAccess('manage_user_groups')) {
581 $this->_aSqlResults['excludedPosts'] = array(
582 'all' => array()
583 );
584 }
585
586 if (!isset($this->_aSqlResults['excludedPosts'])) {
587 $oDatabase = $this->getUserAccessManager()->getDatabase();
588 $oUserAccessManager = $this->getUserAccessManager();
589
590 $sAccessType = ($oUserAccessManager->atAdminPanel()) ? 'write' : 'read';
591
592 $aCategoriesAssignedToUser = $this->getTermsForUser();
593 $sCategoriesAssignedToUser = ($aCategoriesAssignedToUser !== array()) ?
594 implode(', ', $aCategoriesAssignedToUser) : null;
595
596 $aPostAssignedToUser = $this->getPostsForUser();
597 $sPostAssignedToUser = ($aPostAssignedToUser !== array()) ? implode(', ', $aPostAssignedToUser) : null;
598
599 $sTermType = UserAccessManager::TERM_OBJECT_TYPE;
600 $aPostableTypes = $this->getPostableTypes();
601
602 if (!$oUserAccessManager->atAdminPanel()) {
603 $oConfig = $oUserAccessManager->getConfig();
604
605 foreach ($aPostableTypes as $sKey =>$sType) {
606 if ($oConfig->hideObjectType($sType) === false) {
607 unset($aPostableTypes[$sKey]);
608 }
609 }
610 }
611
612 $sPostableTypes = "'" . implode("','", $aPostableTypes) . "'";
613
614 $sTermSql = "SELECT gc.object_id
615 FROM " . DB_ACCESSGROUP . " iag
616 INNER JOIN " . DB_ACCESSGROUP_TO_OBJECT . " AS gc
617 ON iag.id = gc.group_id
618 WHERE gc.object_type = '{$sTermType}'
619 AND iag.{$sAccessType}_access != 'all'";
620
621 if ($sCategoriesAssignedToUser !== null) {
622 $sTermSql .= " AND gc.object_id NOT IN ({$sCategoriesAssignedToUser})";
623 }
624
625 $sObjectQuery = "SELECT DISTINCT gp.object_id AS id, gp.object_type AS type
626 FROM " . DB_ACCESSGROUP . " AS ag
627 INNER JOIN " . DB_ACCESSGROUP_TO_OBJECT . " AS gp
628 ON ag.id = gp.group_id
629 LEFT JOIN {$oDatabase->term_relationships} AS tr
630 ON gp.object_id = tr.object_id
631 LEFT JOIN {$oDatabase->term_taxonomy} tt
632 ON tr.term_taxonomy_id = tt.term_taxonomy_id
633 WHERE gp.object_type IN ({$sPostableTypes})
634 AND ag.{$sAccessType}_access != 'all'";
635
636 if ($sPostAssignedToUser !== null) {
637 $sObjectQuery .= "AND gp.object_id NOT IN ({$sPostAssignedToUser})";
638 }
639
640 if ($sCategoriesAssignedToUser !== null) {
641 $sObjectQuery .= "AND tt.term_id NOT IN ({$sCategoriesAssignedToUser})";
642 }
643
644 if (isset($aPostableTypes['post'])) {
645 $sPostQuery = "SELECT DISTINCT p.ID AS id, post_type AS type
646 FROM {$oDatabase->posts} AS p
647 INNER JOIN {$oDatabase->term_relationships} AS tr
648 ON p.ID = tr.object_id
649 INNER JOIN {$oDatabase->term_taxonomy} AS tt
650 ON tr.term_taxonomy_id = tt.term_taxonomy_id
651 WHERE p.post_type != 'revision'
652 AND tt.taxonomy = 'category'
653 AND tt.term_id IN ({$sTermSql})";
654
655 if ($sPostAssignedToUser !== null) {
656 $sPostQuery .= " AND p.ID NOT IN ({$sPostAssignedToUser})";
657 }
658
659 $sFullQuery = "{$sPostQuery} UNION {$sObjectQuery}";
660 } else {
661 $sFullQuery = $sObjectQuery;
662 }
663
664 $aResult = $oDatabase->get_results($sFullQuery);
665 $aExcludedPosts = array(
666 'all' => array()
667 );
668
669 foreach ($aResult as $oExcludedPost) {
670 if (!isset($aExcludedPosts[$oExcludedPost->type])) {
671 $aExcludedPosts[$oExcludedPost->type] = array();
672 }
673
674 $aExcludedPosts[$oExcludedPost->type][$oExcludedPost->id] = $oExcludedPost->id;
675 }
676
677 $aPostTreeMap = $oUserAccessManager->getPostTreeMap();
678
679 foreach ($aExcludedPosts as $sType => $aIds) {
680 if ($sType !== 'all') {
681 if ($oUserAccessManager->isPostTypeHierarchical($sType)) {
682 foreach ($aIds as $iId) {
683 if (isset($aPostTreeMap[$iId])) {
684 $aExcludedPosts[$sType] = $aExcludedPosts[$sType] + $aPostTreeMap[$iId];
685 }
686 }
687 }
688
689 $aExcludedPosts['all'] = $aExcludedPosts['all'] + $aExcludedPosts[$sType];
690 }
691 }
692
693 $this->_aSqlResults['excludedPosts'] = $aExcludedPosts;
694 }
695
696 return $this->_aSqlResults['excludedPosts'];
697 }
698
699
700 /*
701 * Other functions
702 */
703
704 /**
705 * Checks if the given ip matches with the range.
706 *
707 * @param array $aCurrentIp The ip of the current user.
708 * @param array $aIpRanges The ip ranges.
709 *
710 * @return boolean
711 */
712 public function checkUserIp($aCurrentIp, $aIpRanges)
713 {
714 if (isset($aIpRanges)) {
715 foreach ($aIpRanges as $sIpRange) {
716 $aIpRange = explode('-', $sIpRange);
717 $aRangeBegin = explode('.', $aIpRange[0]);
718 $aRangeEnd = isset($aIpRange[1]) ? explode('.', $aIpRange[1]) : explode('.', $aIpRange[0]);
719
720 if (count($aRangeBegin) === 4 && count($aRangeEnd) === 4) {
721 $iCurIp = ($aCurrentIp[0] << 24) + ($aCurrentIp[1] << 16) + ($aCurrentIp[2] << 8) + $aCurrentIp[3];
722 $iRangeBegin = ($aRangeBegin[0] << 24) + ($aRangeBegin[1] << 16) + ($aRangeBegin[2] << 8) + $aRangeBegin[3];
723 $iRangeEnd = ($aRangeEnd[0] << 24) + ($aRangeEnd[1] << 16) + ($aRangeEnd[2] << 8) + $aRangeEnd[3];
724
725 if ($iRangeBegin <= $iCurIp && $iCurIp <= $iRangeEnd) {
726 return true;
727 }
728 }
729 }
730 }
731
732 return false;
733 }
734
735 /**
736 * Return the role of the user.
737 *
738 * @param integer $iUserId The user id.
739 *
740 * @return array
741 */
742 protected function _getUserRole($iUserId)
743 {
744 $oDatabase = $this->getUserAccessManager()->getDatabase();
745 $oUserData = $this->getUserAccessManager()->getUser($iUserId);
746
747 if (!empty($oUserData->user_level) && !isset($oUserData->user_level)) {
748 $oUserData->user_level = null;
749 }
750
751 if (isset($oUserData->{$oDatabase->prefix . "capabilities"})) {
752 $aCapabilities = $oUserData->{$oDatabase->prefix . "capabilities"};
753 } else {
754 $aCapabilities = array();
755 }
756
757 $aRoles = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole');
758 return $aRoles;
759 }
760
761 /**
762 * Checks if the user is an admin user
763 *
764 * @param integer $iUserId The user id.
765 *
766 * @return boolean
767 */
768 public function userIsAdmin($iUserId)
769 {
770 $aRoles = $this->_getUserRole($iUserId);
771 $aRolesMap = array_keys($aRoles);
772
773 if (isset($aRolesMap['administrator']) || is_super_admin($iUserId)) {
774 return true;
775 }
776
777 return false;
778 }
779
780 /**
781 * Checks the user access by user level.
782 *
783 * @param bool|string $sAllowedCapability If true check also for the capability.
784 *
785 * @return boolean
786 */
787 public function checkUserAccess($sAllowedCapability = false)
788 {
789 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
790 $oConfig = $this->getUserAccessManager()->getConfig();
791
792 $aRoles = $this->_getUserRole($oCurrentUser->ID);
793 $aRolesMap = array_keys($aRoles);
794 $aOrderedRoles = $this->getRolesOrdered();
795 $iRightsLevel = 0;
796
797 foreach ($aRoles as $sRole) {
798 if (isset($aOrderedRoles[$sRole])
799 && $aOrderedRoles[$sRole] > $iRightsLevel
800 ) {
801 $iRightsLevel = $aOrderedRoles[$sRole];
802 }
803 }
804
805 $sFullAccessRole = $oConfig->getFullAccessRole();
806
807 if ($iRightsLevel >= $aOrderedRoles[$sFullAccessRole]
808 || isset($aRolesMap['administrator'])
809 || is_super_admin($oCurrentUser->ID)
810 || ($sAllowedCapability && $oCurrentUser->has_cap($sAllowedCapability))
811 ) {
812 return true;
813 }
814
815 return false;
816 }
817
818 /**
819 * Returns the roles as associative array.
820 *
821 * @return array
822 */
823 public function getRolesOrdered()
824 {
825 $aOrderedRoles = array(
826 'norole' => 0,
827 'subscriber' => 1,
828 'contributor' => 2,
829 'author' => 3,
830 'editor' => 4,
831 'administrator' => 5
832 );
833
834 return $aOrderedRoles;
835 }
836
837 /**
838 * Registers object that should be handel by the user access manager.
839 *
840 * @param array $oObject The object which you want to register.
841 *
842 * @return boolean
843 */
844 public function registerPlObject($oObject)
845 {
846 if (!isset($oObject['name']) || !isset($oObject['reference'])
847 || !isset($oObject['getFull']) || !isset($oObject['getFullObjects'])
848 ) {
849 return false;
850 }
851
852 $this->_aPlObjects[$oObject['name']] = $oObject;
853
854 return true;
855 }
856
857 /**
858 * Returns a registered pluggable object.
859 *
860 * @param string $sObjectName The name of the object which should be returned.
861 *
862 * @return array
863 */
864 public function getPlObject($sObjectName)
865 {
866 if (isset($this->_aPlObjects[$sObjectName])) {
867 return $this->_aPlObjects[$sObjectName];
868 }
869
870 return array();
871 }
872
873 /**
874 * Returns all registered pluggable objects.
875 *
876 * @return array
877 */
878 public function getPlObjects()
879 {
880 return $this->_aPlObjects;
881 }
882 }