PluginProbe
User Access Manager / 1.2.12
User Access Manager v1.2.12
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.12, at class/UamAccessHandler.php

885 lines 29.7 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}) OR tt.term_id IS NULL)";
642 }
643
644 $aObjectResult = $oDatabase->get_results($sObjectQuery);
645
646 $sPostQuery = "SELECT DISTINCT p.ID AS id, post_type AS type
647 FROM {$oDatabase->posts} AS p
648 INNER JOIN {$oDatabase->term_relationships} AS tr
649 ON p.ID = tr.object_id
650 INNER JOIN {$oDatabase->term_taxonomy} AS tt
651 ON tr.term_taxonomy_id = tt.term_taxonomy_id
652 WHERE p.post_type != 'revision'
653 AND p.post_type IN ({$sPostableTypes})
654 AND tt.taxonomy = 'category'
655 AND tt.term_id IN ({$sTermSql})";
656
657 if ($sPostAssignedToUser !== null) {
658 $sPostQuery .= " AND p.ID NOT IN ({$sPostAssignedToUser})";
659 }
660
661 $aPostResult = $oDatabase->get_results($sPostQuery);
662 $aResult = array_merge($aObjectResult, $aPostResult);
663
664 $aExcludedPosts = array(
665 'all' => array()
666 );
667
668 foreach ($aResult as $oExcludedPost) {
669 if (!isset($aExcludedPosts[$oExcludedPost->type])) {
670 $aExcludedPosts[$oExcludedPost->type] = array();
671 }
672
673 $aExcludedPosts[$oExcludedPost->type][$oExcludedPost->id] = $oExcludedPost->id;
674 }
675
676 $aPostTreeMap = $oUserAccessManager->getPostTreeMap();
677
678 foreach ($aExcludedPosts as $sType => $aIds) {
679 if ($sType !== 'all') {
680 if ($oUserAccessManager->isPostTypeHierarchical($sType)) {
681 foreach ($aIds as $iId) {
682 if (isset($aPostTreeMap[$iId])) {
683 foreach ($aPostTreeMap[$iId] as $iPostId => $sPostType) {
684 if ($sPostType == $sType) {
685 $aExcludedPosts[$sType][$iPostId] = $iPostId;
686 }
687 }
688 }
689 }
690 }
691
692 $aExcludedPosts['all'] = $aExcludedPosts['all'] + $aExcludedPosts[$sType];
693 }
694 }
695
696 $this->_aSqlResults['excludedPosts'] = $aExcludedPosts;
697 }
698
699 return $this->_aSqlResults['excludedPosts'];
700 }
701
702
703 /*
704 * Other functions
705 */
706
707 /**
708 * Checks if the given ip matches with the range.
709 *
710 * @param array $aCurrentIp The ip of the current user.
711 * @param array $aIpRanges The ip ranges.
712 *
713 * @return boolean
714 */
715 public function checkUserIp($aCurrentIp, $aIpRanges)
716 {
717 if (isset($aIpRanges)) {
718 foreach ($aIpRanges as $sIpRange) {
719 $aIpRange = explode('-', $sIpRange);
720 $aRangeBegin = explode('.', $aIpRange[0]);
721 $aRangeEnd = isset($aIpRange[1]) ? explode('.', $aIpRange[1]) : explode('.', $aIpRange[0]);
722
723 if (count($aRangeBegin) === 4 && count($aRangeEnd) === 4) {
724 $iCurIp = ($aCurrentIp[0] << 24) + ($aCurrentIp[1] << 16) + ($aCurrentIp[2] << 8) + $aCurrentIp[3];
725 $iRangeBegin = ($aRangeBegin[0] << 24) + ($aRangeBegin[1] << 16) + ($aRangeBegin[2] << 8) + $aRangeBegin[3];
726 $iRangeEnd = ($aRangeEnd[0] << 24) + ($aRangeEnd[1] << 16) + ($aRangeEnd[2] << 8) + $aRangeEnd[3];
727
728 if ($iRangeBegin <= $iCurIp && $iCurIp <= $iRangeEnd) {
729 return true;
730 }
731 }
732 }
733 }
734
735 return false;
736 }
737
738 /**
739 * Return the role of the user.
740 *
741 * @param integer $iUserId The user id.
742 *
743 * @return array
744 */
745 protected function _getUserRole($iUserId)
746 {
747 $oDatabase = $this->getUserAccessManager()->getDatabase();
748 $oUserData = $this->getUserAccessManager()->getUser($iUserId);
749
750 if (!empty($oUserData->user_level) && !isset($oUserData->user_level)) {
751 $oUserData->user_level = null;
752 }
753
754 if (isset($oUserData->{$oDatabase->prefix . "capabilities"})) {
755 $aCapabilities = $oUserData->{$oDatabase->prefix . "capabilities"};
756 } else {
757 $aCapabilities = array();
758 }
759
760 $aRoles = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole');
761 return $aRoles;
762 }
763
764 /**
765 * Checks if the user is an admin user
766 *
767 * @param integer $iUserId The user id.
768 *
769 * @return boolean
770 */
771 public function userIsAdmin($iUserId)
772 {
773 $aRoles = $this->_getUserRole($iUserId);
774 $aRolesMap = array_keys($aRoles);
775
776 if (isset($aRolesMap['administrator']) || is_super_admin($iUserId)) {
777 return true;
778 }
779
780 return false;
781 }
782
783 /**
784 * Checks the user access by user level.
785 *
786 * @param bool|string $sAllowedCapability If true check also for the capability.
787 *
788 * @return boolean
789 */
790 public function checkUserAccess($sAllowedCapability = false)
791 {
792 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
793 $oConfig = $this->getUserAccessManager()->getConfig();
794
795 $aRoles = $this->_getUserRole($oCurrentUser->ID);
796 $aRolesMap = array_keys($aRoles);
797 $aOrderedRoles = $this->getRolesOrdered();
798 $iRightsLevel = 0;
799
800 foreach ($aRoles as $sRole) {
801 if (isset($aOrderedRoles[$sRole])
802 && $aOrderedRoles[$sRole] > $iRightsLevel
803 ) {
804 $iRightsLevel = $aOrderedRoles[$sRole];
805 }
806 }
807
808 $sFullAccessRole = $oConfig->getFullAccessRole();
809
810 if ($iRightsLevel >= $aOrderedRoles[$sFullAccessRole]
811 || isset($aRolesMap['administrator'])
812 || is_super_admin($oCurrentUser->ID)
813 || ($sAllowedCapability && $oCurrentUser->has_cap($sAllowedCapability))
814 ) {
815 return true;
816 }
817
818 return false;
819 }
820
821 /**
822 * Returns the roles as associative array.
823 *
824 * @return array
825 */
826 public function getRolesOrdered()
827 {
828 $aOrderedRoles = array(
829 'norole' => 0,
830 'subscriber' => 1,
831 'contributor' => 2,
832 'author' => 3,
833 'editor' => 4,
834 'administrator' => 5
835 );
836
837 return $aOrderedRoles;
838 }
839
840 /**
841 * Registers object that should be handel by the user access manager.
842 *
843 * @param array $oObject The object which you want to register.
844 *
845 * @return boolean
846 */
847 public function registerPlObject($oObject)
848 {
849 if (!isset($oObject['name']) || !isset($oObject['reference'])
850 || !isset($oObject['getFull']) || !isset($oObject['getFullObjects'])
851 ) {
852 return false;
853 }
854
855 $this->_aPlObjects[$oObject['name']] = $oObject;
856
857 return true;
858 }
859
860 /**
861 * Returns a registered pluggable object.
862 *
863 * @param string $sObjectName The name of the object which should be returned.
864 *
865 * @return array
866 */
867 public function getPlObject($sObjectName)
868 {
869 if (isset($this->_aPlObjects[$sObjectName])) {
870 return $this->_aPlObjects[$sObjectName];
871 }
872
873 return array();
874 }
875
876 /**
877 * Returns all registered pluggable objects.
878 *
879 * @return array
880 */
881 public function getPlObjects()
882 {
883 return $this->_aPlObjects;
884 }
885 }