PluginProbe
User Access Manager / 1.2.14
User Access Manager v1.2.14
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.14, at class/UamAccessHandler.php

896 lines 30.2 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 $oUserGroup) {
477 $aUserUserGroupIds[$oUserGroup->getId()] = $oUserGroup->getId();
478 }
479
480 $aCurrentIp = explode('.', $_SERVER['REMOTE_ADDR']);
481 $aUserGroups = $this->getUserGroups();
482
483 foreach ($aUserGroups as $oUserGroup) {
484 if (!isset($aUserUserGroupIds[$oUserGroup->getId()])
485 && $this->checkUserIp($aCurrentIp, $oUserGroup->getIpRange())
486 ) {
487 $aUserUserGroupIds[$oUserGroup->getId()] = $oUserGroup->getId();
488 }
489 }
490
491 if ($aUserUserGroupIds !== array()) {
492 $sUserUserGroups = implode(', ', $aUserUserGroupIds);
493 } else {
494 $sUserUserGroups = "''";
495 }
496
497 $this->_aSqlResults['groupsForUser'] = $sUserUserGroups;
498 }
499
500 return $this->_aSqlResults['groupsForUser'];
501 }
502
503 /**
504 * Returns the categories assigned to the user.
505 *
506 * @return array
507 */
508 public function getTermsForUser()
509 {
510 $oDatabase = $this->getUserAccessManager()->getDatabase();
511
512 if (!isset($this->_aSqlResults['termsAssignedToUser'])) {
513 $sUserUserGroups = $this->_getUserGroupsForUserAsSqlString();
514 $sTermType = UserAccessManager::TERM_OBJECT_TYPE;
515
516 $sTermsAssignedToUserSql = "
517 SELECT igc.object_id
518 FROM " . DB_ACCESSGROUP_TO_OBJECT . " AS igc
519 WHERE igc.object_type = '{$sTermType}'
520 AND igc.group_id IN ({$sUserUserGroups})";
521
522 $this->_aSqlResults['termsAssignedToUser'] = $oDatabase->get_col($sTermsAssignedToUserSql);
523 }
524
525 return $this->_aSqlResults['termsAssignedToUser'];
526 }
527
528 /**
529 * Returns the excluded terms for a user.
530 *
531 * @return array
532 */
533 public function getExcludedTerms()
534 {
535 if ($this->checkUserAccess('manage_user_groups')) {
536 $this->_aSqlResults['excludedTerms'] = array();
537 }
538
539 if (!isset($this->_aSqlResults['excludedTerms'])) {
540 $oDatabase = $this->getUserAccessManager()->getDatabase();
541 $sTermType = UserAccessManager::TERM_OBJECT_TYPE;
542 $sAccessType = ($this->getUserAccessManager()->atAdminPanel() === true) ? 'write' : 'read';
543 $aCategoriesAssignedToUser = $this->getTermsForUser();
544 $sCategoriesAssignedToUser = ($aCategoriesAssignedToUser !== array()) ? implode(', ', $aCategoriesAssignedToUser) : "''";
545
546 $sTermSql = "SELECT agto.object_id
547 FROM " . DB_ACCESSGROUP_TO_OBJECT . " agto
548 LEFT JOIN " . DB_ACCESSGROUP . " AS ag
549 ON agto.group_id = ag.id
550 WHERE agto.object_type = '{$sTermType}'
551 AND agto.object_id NOT IN ({$sCategoriesAssignedToUser})
552 AND ag.{$sAccessType}_access != 'all'";
553
554 $this->_aSqlResults['excludedTerms'] = $oDatabase->get_col($sTermSql);
555 }
556
557 return $this->_aSqlResults['excludedTerms'];
558 }
559
560 /**
561 * Returns the posts assigned to the user.
562 *
563 * @return array
564 */
565 public function getPostsForUser()
566 {
567 if (!isset($this->_aSqlResults['postsAssignedToUser'])) {
568 $oDatabase = $this->getUserAccessManager()->getDatabase();
569 $sUserUserGroup = $this->_getUserGroupsForUserAsSqlString();
570 $sPostableTypes = "'" . implode("','", $this->getPostableTypes()) . "'";
571
572 $sPostAssignedToUserSql = "
573 SELECT igp.object_id
574 FROM " . DB_ACCESSGROUP_TO_OBJECT . " AS igp
575 WHERE igp.object_type IN ({$sPostableTypes})
576 AND igp.group_id IN ({$sUserUserGroup})";
577
578 $this->_aSqlResults['postsAssignedToUser'] = $oDatabase->get_col($sPostAssignedToUserSql);
579 }
580
581 return $this->_aSqlResults['postsAssignedToUser'];
582 }
583
584 /**
585 * Returns the excluded posts.
586 *
587 * @return array
588 */
589 public function getExcludedPosts()
590 {
591 if ($this->checkUserAccess('manage_user_groups')) {
592 $this->_aSqlResults['excludedPosts'] = array(
593 'all' => array()
594 );
595 }
596
597 if (!isset($this->_aSqlResults['excludedPosts'])) {
598 $oDatabase = $this->getUserAccessManager()->getDatabase();
599 $oUserAccessManager = $this->getUserAccessManager();
600
601 $sAccessType = ($oUserAccessManager->atAdminPanel()) ? 'write' : 'read';
602
603 $aCategoriesAssignedToUser = $this->getTermsForUser();
604 $sCategoriesAssignedToUser = ($aCategoriesAssignedToUser !== array()) ?
605 implode(', ', $aCategoriesAssignedToUser) : null;
606
607 $aPostAssignedToUser = $this->getPostsForUser();
608 $sPostAssignedToUser = ($aPostAssignedToUser !== array()) ? implode(', ', $aPostAssignedToUser) : null;
609
610 $sTermType = UserAccessManager::TERM_OBJECT_TYPE;
611 $aPostableTypes = $this->getPostableTypes();
612
613 if (!$oUserAccessManager->atAdminPanel()) {
614 $oConfig = $oUserAccessManager->getConfig();
615
616 foreach ($aPostableTypes as $sKey =>$sType) {
617 if ($oConfig->hideObjectType($sType) === false) {
618 unset($aPostableTypes[$sKey]);
619 }
620 }
621 }
622
623 $sPostableTypes = "'" . implode("','", $aPostableTypes) . "'";
624
625 $sTermSql = "SELECT gc.object_id
626 FROM " . DB_ACCESSGROUP . " iag
627 INNER JOIN " . DB_ACCESSGROUP_TO_OBJECT . " AS gc
628 ON iag.id = gc.group_id
629 WHERE gc.object_type = '{$sTermType}'
630 AND iag.{$sAccessType}_access != 'all'";
631
632 if ($sCategoriesAssignedToUser !== null) {
633 $sTermSql .= " AND gc.object_id NOT IN ({$sCategoriesAssignedToUser})";
634 }
635
636 $sObjectQuery = "SELECT DISTINCT gp.object_id AS id, gp.object_type AS type
637 FROM " . DB_ACCESSGROUP . " AS ag
638 INNER JOIN " . DB_ACCESSGROUP_TO_OBJECT . " AS gp
639 ON ag.id = gp.group_id
640 LEFT JOIN {$oDatabase->term_relationships} AS tr
641 ON gp.object_id = tr.object_id
642 LEFT JOIN {$oDatabase->term_taxonomy} tt
643 ON tr.term_taxonomy_id = tt.term_taxonomy_id
644 WHERE gp.object_type IN ({$sPostableTypes})
645 AND ag.{$sAccessType}_access != 'all'";
646
647 if ($sPostAssignedToUser !== null) {
648 $sObjectQuery .= "AND gp.object_id NOT IN ({$sPostAssignedToUser})";
649 }
650
651 if ($sCategoriesAssignedToUser !== null) {
652 $sObjectQuery .= "AND (tt.term_id NOT IN ({$sCategoriesAssignedToUser}) OR tt.term_id IS NULL)";
653 }
654
655 $aObjectResult = $oDatabase->get_results($sObjectQuery);
656
657 $sPostQuery = "SELECT DISTINCT p.ID AS id, post_type AS type
658 FROM {$oDatabase->posts} AS p
659 INNER JOIN {$oDatabase->term_relationships} AS tr
660 ON p.ID = tr.object_id
661 INNER JOIN {$oDatabase->term_taxonomy} AS tt
662 ON tr.term_taxonomy_id = tt.term_taxonomy_id
663 WHERE p.post_type != 'revision'
664 AND p.post_type IN ({$sPostableTypes})
665 AND tt.taxonomy = 'category'
666 AND tt.term_id IN ({$sTermSql})";
667
668 if ($sPostAssignedToUser !== null) {
669 $sPostQuery .= " AND p.ID NOT IN ({$sPostAssignedToUser})";
670 }
671
672 $aPostResult = $oDatabase->get_results($sPostQuery);
673 $aResult = array_merge($aObjectResult, $aPostResult);
674
675 $aExcludedPosts = array(
676 'all' => array()
677 );
678
679 foreach ($aResult as $oExcludedPost) {
680 if (!isset($aExcludedPosts[$oExcludedPost->type])) {
681 $aExcludedPosts[$oExcludedPost->type] = array();
682 }
683
684 $aExcludedPosts[$oExcludedPost->type][$oExcludedPost->id] = $oExcludedPost->id;
685 }
686
687 $aPostTreeMap = $oUserAccessManager->getPostTreeMap();
688
689 foreach ($aExcludedPosts as $sType => $aIds) {
690 if ($sType !== 'all') {
691 if ($oUserAccessManager->isPostTypeHierarchical($sType)) {
692 foreach ($aIds as $iId) {
693 if (isset($aPostTreeMap[$iId])) {
694 foreach ($aPostTreeMap[$iId] as $iPostId => $sPostType) {
695 if ($sPostType == $sType) {
696 $aExcludedPosts[$sType][$iPostId] = $iPostId;
697 }
698 }
699 }
700 }
701 }
702
703 $aExcludedPosts['all'] = $aExcludedPosts['all'] + $aExcludedPosts[$sType];
704 }
705 }
706
707 $this->_aSqlResults['excludedPosts'] = $aExcludedPosts;
708 }
709
710 return $this->_aSqlResults['excludedPosts'];
711 }
712
713
714 /*
715 * Other functions
716 */
717
718 /**
719 * Checks if the given ip matches with the range.
720 *
721 * @param array $aCurrentIp The ip of the current user.
722 * @param array $aIpRanges The ip ranges.
723 *
724 * @return boolean
725 */
726 public function checkUserIp($aCurrentIp, $aIpRanges)
727 {
728 if (isset($aIpRanges)) {
729 foreach ($aIpRanges as $sIpRange) {
730 $aIpRange = explode('-', $sIpRange);
731 $aRangeBegin = explode('.', $aIpRange[0]);
732 $aRangeEnd = isset($aIpRange[1]) ? explode('.', $aIpRange[1]) : explode('.', $aIpRange[0]);
733
734 if (count($aRangeBegin) === 4 && count($aRangeEnd) === 4) {
735 $iCurIp = ($aCurrentIp[0] << 24) + ($aCurrentIp[1] << 16) + ($aCurrentIp[2] << 8) + $aCurrentIp[3];
736 $iRangeBegin = ($aRangeBegin[0] << 24) + ($aRangeBegin[1] << 16) + ($aRangeBegin[2] << 8) + $aRangeBegin[3];
737 $iRangeEnd = ($aRangeEnd[0] << 24) + ($aRangeEnd[1] << 16) + ($aRangeEnd[2] << 8) + $aRangeEnd[3];
738
739 if ($iRangeBegin <= $iCurIp && $iCurIp <= $iRangeEnd) {
740 return true;
741 }
742 }
743 }
744 }
745
746 return false;
747 }
748
749 /**
750 * Return the role of the user.
751 *
752 * @param integer $iUserId The user id.
753 *
754 * @return array
755 */
756 protected function _getUserRole($iUserId)
757 {
758 $oDatabase = $this->getUserAccessManager()->getDatabase();
759 $oUserData = $this->getUserAccessManager()->getUser($iUserId);
760
761 if (!empty($oUserData->user_level) && !isset($oUserData->user_level)) {
762 $oUserData->user_level = null;
763 }
764
765 if (isset($oUserData->{$oDatabase->prefix . "capabilities"})) {
766 $aCapabilities = $oUserData->{$oDatabase->prefix . "capabilities"};
767 } else {
768 $aCapabilities = array();
769 }
770
771 $aRoles = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole');
772 return $aRoles;
773 }
774
775 /**
776 * Checks if the user is an admin user
777 *
778 * @param integer $iUserId The user id.
779 *
780 * @return boolean
781 */
782 public function userIsAdmin($iUserId)
783 {
784 $aRoles = $this->_getUserRole($iUserId);
785 $aRolesMap = array_keys($aRoles);
786
787 if (isset($aRolesMap['administrator']) || is_super_admin($iUserId)) {
788 return true;
789 }
790
791 return false;
792 }
793
794 /**
795 * Checks the user access by user level.
796 *
797 * @param bool|string $sAllowedCapability If true check also for the capability.
798 *
799 * @return boolean
800 */
801 public function checkUserAccess($sAllowedCapability = false)
802 {
803 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
804 $oConfig = $this->getUserAccessManager()->getConfig();
805
806 $aRoles = $this->_getUserRole($oCurrentUser->ID);
807 $aRolesMap = array_keys($aRoles);
808 $aOrderedRoles = $this->getRolesOrdered();
809 $iRightsLevel = 0;
810
811 foreach ($aRoles as $sRole) {
812 if (isset($aOrderedRoles[$sRole])
813 && $aOrderedRoles[$sRole] > $iRightsLevel
814 ) {
815 $iRightsLevel = $aOrderedRoles[$sRole];
816 }
817 }
818
819 $sFullAccessRole = $oConfig->getFullAccessRole();
820
821 if ($iRightsLevel >= $aOrderedRoles[$sFullAccessRole]
822 || isset($aRolesMap['administrator'])
823 || is_super_admin($oCurrentUser->ID)
824 || ($sAllowedCapability && $oCurrentUser->has_cap($sAllowedCapability))
825 ) {
826 return true;
827 }
828
829 return false;
830 }
831
832 /**
833 * Returns the roles as associative array.
834 *
835 * @return array
836 */
837 public function getRolesOrdered()
838 {
839 $aOrderedRoles = array(
840 'norole' => 0,
841 'subscriber' => 1,
842 'contributor' => 2,
843 'author' => 3,
844 'editor' => 4,
845 'administrator' => 5
846 );
847
848 return $aOrderedRoles;
849 }
850
851 /**
852 * Registers object that should be handel by the user access manager.
853 *
854 * @param array $oObject The object which you want to register.
855 *
856 * @return boolean
857 */
858 public function registerPlObject($oObject)
859 {
860 if (!isset($oObject['name']) || !isset($oObject['reference'])
861 || !isset($oObject['getFull']) || !isset($oObject['getFullObjects'])
862 ) {
863 return false;
864 }
865
866 $this->_aPlObjects[$oObject['name']] = $oObject;
867
868 return true;
869 }
870
871 /**
872 * Returns a registered pluggable object.
873 *
874 * @param string $sObjectName The name of the object which should be returned.
875 *
876 * @return array
877 */
878 public function getPlObject($sObjectName)
879 {
880 if (isset($this->_aPlObjects[$sObjectName])) {
881 return $this->_aPlObjects[$sObjectName];
882 }
883
884 return array();
885 }
886
887 /**
888 * Returns all registered pluggable objects.
889 *
890 * @return array
891 */
892 public function getPlObjects()
893 {
894 return $this->_aPlObjects;
895 }
896 }