| 1 |
<?php |
| 2 |
/** |
| 3 |
* UamAccessHandler.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 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 |
|
| 28 |
class UamAccessHandler |
| 29 |
{ |
| 30 |
protected $userAccessManager = null; |
| 31 |
protected $objectUserGroups = array(); |
| 32 |
protected $objectAccess = array(); |
| 33 |
protected $userGroups = array( |
| 34 |
'filtered' => array(), |
| 35 |
'noneFiltered' => array(), |
| 36 |
); |
| 37 |
protected $plObjects = array(); |
| 38 |
protected $objectTypes = array( |
| 39 |
'category', |
| 40 |
'user', |
| 41 |
'role', |
| 42 |
); |
| 43 |
protected $postableTypes = array( |
| 44 |
'post', |
| 45 |
'page', |
| 46 |
'attachment', |
| 47 |
); |
| 48 |
protected $allObjectTypes = null; |
| 49 |
protected $sqlResults = array(); |
| 50 |
|
| 51 |
/** |
| 52 |
* The consturctor |
| 53 |
* |
| 54 |
* @param object $userAccessManager The user access manager object. |
| 55 |
* |
| 56 |
* @return null |
| 57 |
*/ |
| 58 |
public function __construct($userAccessManager) |
| 59 |
{ |
| 60 |
$this->userAccessManager = $userAccessManager; |
| 61 |
|
| 62 |
$postTypes = get_post_types(array(), 'objects'); |
| 63 |
|
| 64 |
foreach ($postTypes as $postType) { |
| 65 |
if ($postType->publicly_queryable) { |
| 66 |
$this->postableTypes[] = $postType->name; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
$this->objectTypes = array_merge($this->postableTypes, $this->objectTypes); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the user access manager object. |
| 75 |
* |
| 76 |
* @return object |
| 77 |
*/ |
| 78 |
public function &getUserAccessManager() |
| 79 |
{ |
| 80 |
return $this->userAccessManager; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the predfined object types. |
| 85 |
* |
| 86 |
* @return array |
| 87 |
*/ |
| 88 |
public function getObjectTypes() |
| 89 |
{ |
| 90 |
return $this->objectTypes; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Returns the predfined object types. |
| 95 |
* |
| 96 |
* @return array(); |
| 97 |
*/ |
| 98 |
public function getPostableTypes() |
| 99 |
{ |
| 100 |
return $this->postableTypes; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns all objects types. |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function getAllObjectTypes() |
| 109 |
{ |
| 110 |
if (isset($this->allObjectTypes)) { |
| 111 |
return $this->allObjectTypes; |
| 112 |
} |
| 113 |
|
| 114 |
$plObjects = $this->getPlObjects(); |
| 115 |
|
| 116 |
$this->allObjectTypes = array_merge( |
| 117 |
$this->objectTypes, |
| 118 |
array_keys($plObjects) |
| 119 |
); |
| 120 |
|
| 121 |
return $this->allObjectTypes; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Magic method getter. |
| 126 |
* |
| 127 |
* @param string $name The name of the function |
| 128 |
* @param array $arguments The arguments for the function |
| 129 |
* |
| 130 |
* @return mixed |
| 131 |
*/ |
| 132 |
public function __call($name, $arguments) |
| 133 |
{ |
| 134 |
echo $name; |
| 135 |
exit; |
| 136 |
|
| 137 |
$uam = $this->getUserAccessManager(); |
| 138 |
|
| 139 |
$action = ''; |
| 140 |
|
| 141 |
if ($uam->startsWith($name, 'getUserGroupsFor')) { |
| 142 |
$prefix = 'getUserGroupsFor'; |
| 143 |
} elseif ($uam->startsWith($name, 'checkAccessFor')) { |
| 144 |
$prefix = 'checkAccessFor'; |
| 145 |
} |
| 146 |
|
| 147 |
$objectType = str_replace($prefix, '', $name); |
| 148 |
$objectType = strtolower($objectType); |
| 149 |
|
| 150 |
$objectId = $arguments[0]; |
| 151 |
|
| 152 |
if ($prefix == 'getUserGroupsFor') { |
| 153 |
return $this->getUserGroupsForObject( |
| 154 |
$objectType, |
| 155 |
$objectId |
| 156 |
); |
| 157 |
} elseif ($prefix == 'checkAccessFor') { |
| 158 |
return $this->checkObjectAccess( |
| 159 |
$objectType, |
| 160 |
$objectId |
| 161 |
); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Filter the user groups of an object if authors_can_add_posts_to_groups |
| 167 |
* option is enabled |
| 168 |
* |
| 169 |
* @param array $userGroups The user groups. |
| 170 |
* |
| 171 |
* @return array |
| 172 |
*/ |
| 173 |
private function _filterUserGroups($userGroups) |
| 174 |
{ |
| 175 |
$uamOptions = $this->getUserAccessManager()->getAdminOptions(); |
| 176 |
|
| 177 |
if ($uamOptions['authors_can_add_posts_to_groups'] == 'true' |
| 178 |
&& !$this->checkUserAccess('manage_user_groups') |
| 179 |
&& $this->getUserAccessManager()->atAdminPanel() |
| 180 |
) { |
| 181 |
global $current_user; |
| 182 |
//Force user infos |
| 183 |
wp_get_current_user(); |
| 184 |
|
| 185 |
$userGroupsForUser |
| 186 |
= $this->getUserGroupsForObject('user', $current_user->ID); |
| 187 |
|
| 188 |
foreach ($userGroups as $key => $uamUserGroup) { |
| 189 |
if (!array_key_exists($uamUserGroup->getId(), $userGroupsForUser)) { |
| 190 |
unset($userGroups[$key]); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $userGroups; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Returns all user groups or one requested by the user group id. |
| 200 |
* |
| 201 |
* @param integer $userGroupId The id of the single user group |
| 202 |
* which should be returned. |
| 203 |
* @param boolean $filter Filter the groups. |
| 204 |
* |
| 205 |
* @return array|object |
| 206 |
*/ |
| 207 |
public function getUserGroups($userGroupId = null, $filter = true) |
| 208 |
{ |
| 209 |
if ($filter) { |
| 210 |
$filterAttr = 'filtered'; |
| 211 |
} else { |
| 212 |
$filterAttr = 'noneFiltered'; |
| 213 |
} |
| 214 |
|
| 215 |
if ($userGroupId === null |
| 216 |
&& $this->userGroups[$filterAttr] != array() |
| 217 |
) { |
| 218 |
return $this->userGroups[$filterAttr]; |
| 219 |
} elseif ($userGroupId !== null |
| 220 |
&& $this->userGroups[$filterAttr] != array() |
| 221 |
) { |
| 222 |
if (isset($this->userGroups[$filterAttr][$userGroupId])) { |
| 223 |
return $this->userGroups[$filterAttr][$userGroupId]; |
| 224 |
} else { |
| 225 |
return null; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
$this->userGroups[$filterAttr] = array(); |
| 230 |
|
| 231 |
global $wpdb; |
| 232 |
|
| 233 |
$userGroupsDb = $wpdb->get_results( |
| 234 |
"SELECT ID |
| 235 |
FROM " . DB_ACCESSGROUP . " |
| 236 |
ORDER BY ID", ARRAY_A |
| 237 |
); |
| 238 |
|
| 239 |
if (isset($userGroupsDb)) { |
| 240 |
foreach ($userGroupsDb as $userGroupDb) { |
| 241 |
$this->userGroups[$filterAttr][$userGroupDb['ID']] |
| 242 |
= new UamUserGroup($this, $userGroupDb['ID']); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
//Filter the user groups |
| 247 |
if ($filter) { |
| 248 |
$this->userGroups[$filterAttr] |
| 249 |
= $this->_filterUserGroups($this->userGroups[$filterAttr]); |
| 250 |
} |
| 251 |
|
| 252 |
if ($userGroupId == null) { |
| 253 |
return $this->userGroups[$filterAttr]; |
| 254 |
} elseif ($userGroupId != null) { |
| 255 |
if (isset($this->userGroups[$filterAttr][$userGroupId])) { |
| 256 |
return $this->userGroups[$filterAttr][$userGroupId]; |
| 257 |
} else { |
| 258 |
return null; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Adds a user group. |
| 265 |
* |
| 266 |
* @param object $userGroup The user group which we want to add. |
| 267 |
* |
| 268 |
* @return null |
| 269 |
*/ |
| 270 |
public function addUserGroup($userGroup) |
| 271 |
{ |
| 272 |
$this->getUserGroups(); |
| 273 |
$this->userGroups['noneFiltered'][$userGroup->getId()] = $userGroup; |
| 274 |
$this->userGroups['filtered'] = array(); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Deletes a user group. |
| 279 |
* |
| 280 |
* @param integer $userGroupId The user group id which we want to delete. |
| 281 |
* |
| 282 |
* @return null |
| 283 |
*/ |
| 284 |
public function deleteUserGroup($userGroupId) |
| 285 |
{ |
| 286 |
if ($this->getUserGroups($userGroupId) != null) { |
| 287 |
$this->getUserGroups($userGroupId)->delete(); |
| 288 |
unset($this->userGroups['noneFiltered'][$userGroupId]); |
| 289 |
$this->userGroups['filtered'] = array(); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Returns the user groups for the given object. |
| 295 |
* |
| 296 |
* @param string $objectType The object type. |
| 297 |
* @param integer $objectId The id of the object. |
| 298 |
* @param boolean $filter Filter the groups. |
| 299 |
* |
| 300 |
* @return array |
| 301 |
*/ |
| 302 |
public function getUserGroupsForObject($objectType, $objectId, $filter = true) |
| 303 |
{ |
| 304 |
if (!in_array($objectType, $this->getAllObjectTypes())) { |
| 305 |
return array(); |
| 306 |
} |
| 307 |
|
| 308 |
if ($objectType == 'user') { |
| 309 |
$filter = false; |
| 310 |
} |
| 311 |
|
| 312 |
if ($filter) { |
| 313 |
$filterAttr = 'filtered'; |
| 314 |
} else { |
| 315 |
$filterAttr = 'noneFiltered'; |
| 316 |
} |
| 317 |
|
| 318 |
if (isset($this->objectUserGroups[$objectType][$filterAttr][$objectId])) { |
| 319 |
return $this->objectUserGroups[$objectType][$filterAttr][$objectId]; |
| 320 |
} |
| 321 |
|
| 322 |
$objectUserGroups = array(); |
| 323 |
|
| 324 |
$userGroups = $this->getUserGroups(null, $filter); |
| 325 |
|
| 326 |
$plObject = false; |
| 327 |
|
| 328 |
$postableTypes = $this->getPostableTypes(); |
| 329 |
|
| 330 |
if (!in_array($objectType, $postableTypes)) { |
| 331 |
$plObject = true; |
| 332 |
} |
| 333 |
|
| 334 |
$curIp = explode(".", $_SERVER['REMOTE_ADDR']); |
| 335 |
|
| 336 |
if (isset($userGroups)) { |
| 337 |
foreach ($userGroups as $userGroup) { |
| 338 |
$objectMembership = $userGroup->objectIsMember( |
| 339 |
$objectType, |
| 340 |
$objectId, |
| 341 |
true |
| 342 |
); |
| 343 |
|
| 344 |
if ($objectMembership !== false |
| 345 |
|| $objectType == 'user' |
| 346 |
&& $this->checkUserIp($curIp, $userGroup->getIpRange()) |
| 347 |
) { |
| 348 |
if (is_array($objectMembership)) { |
| 349 |
$userGroup->setRecursive[$objectType][$objectId] |
| 350 |
= $objectMembership; |
| 351 |
} |
| 352 |
|
| 353 |
$objectUserGroups[$userGroup->getId()] |
| 354 |
= $userGroup; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
//Filter the user groups |
| 360 |
if ($filter) { |
| 361 |
$objectUserGroups = $this->_filterUserGroups($objectUserGroups); |
| 362 |
} |
| 363 |
|
| 364 |
$this->objectUserGroups[$objectType][$filterAttr][$objectId] |
| 365 |
= $objectUserGroups; |
| 366 |
|
| 367 |
return $this->objectUserGroups[$objectType][$filterAttr][$objectId]; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Unsets the usergroups for objects. |
| 372 |
* |
| 373 |
* @return null |
| 374 |
*/ |
| 375 |
public function unsetUserGroupsForObject() |
| 376 |
{ |
| 377 |
$this->objectUserGroups = array(); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Checks if the current_user has access to the given post. |
| 382 |
* |
| 383 |
* @param string $objectType The object type which should be checked. |
| 384 |
* @param integer $objectId The id of the object. |
| 385 |
* |
| 386 |
* @return boolean |
| 387 |
*/ |
| 388 |
public function checkObjectAccess($objectType, $objectId) |
| 389 |
{ |
| 390 |
if (!in_array($objectType, $this->getAllObjectTypes())) { |
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
if (isset($this->objectAccess[$objectType][$objectId])) { |
| 395 |
return $this->objectAccess[$objectType][$objectId]; |
| 396 |
} |
| 397 |
|
| 398 |
global $current_user; |
| 399 |
//Force user infos |
| 400 |
wp_get_current_user(); |
| 401 |
|
| 402 |
$postableTypes = $this->getPostableTypes(); |
| 403 |
|
| 404 |
if (in_array($objectType, $postableTypes)) { |
| 405 |
$post = get_post($objectId); |
| 406 |
$authorId = $post->post_author; |
| 407 |
} else { |
| 408 |
$authorId = -1; |
| 409 |
} |
| 410 |
|
| 411 |
$uamOptions = $this->getUserAccessManager()->getAdminOptions(); |
| 412 |
$membership = $this->getUserGroupsForObject($objectType, $objectId, false); |
| 413 |
|
| 414 |
if ($membership == array() |
| 415 |
|| $this->checkUserAccess('manage_user_groups') |
| 416 |
|| $current_user->ID == $authorId |
| 417 |
&& $uamOptions['authors_has_access_to_own'] == 'true' |
| 418 |
) { |
| 419 |
return $this->objectAccess[$objectType][$objectId] = true; |
| 420 |
} |
| 421 |
|
| 422 |
$curIp = explode(".", $_SERVER['REMOTE_ADDR']); |
| 423 |
|
| 424 |
foreach ($membership as $key => $userGroup) { |
| 425 |
if ($this->checkUserIp($curIp, $userGroup->getIpRange()) |
| 426 |
|| $userGroup->objectIsMember('user', $current_user->ID) |
| 427 |
) { |
| 428 |
return $this->objectAccess[$objectType][$objectId] = true; |
| 429 |
break; |
| 430 |
} |
| 431 |
|
| 432 |
if ($this->getUserAccessManager()->atAdminPanel() |
| 433 |
&& $userGroup->getWriteAccess() == 'all' |
| 434 |
|| !$this->getUserAccessManager()->atAdminPanel() |
| 435 |
&& $userGroup->getReadAccess() == 'all' |
| 436 |
) { |
| 437 |
unset($membership[$key]); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
if ($membership == array()) { |
| 442 |
return $this->objectAccess[$objectType][$objectId] = true; |
| 443 |
} |
| 444 |
|
| 445 |
return $this->objectAccess[$objectType][$objectId] = false; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
/* |
| 450 |
* SQL functions. |
| 451 |
*/ |
| 452 |
|
| 453 |
/** |
| 454 |
* Returns the usergroups for the current user as sql string. |
| 455 |
* |
| 456 |
* @return string |
| 457 |
*/ |
| 458 |
private function _getUserGroupsForUserAsSqlString() |
| 459 |
{ |
| 460 |
if (isset($this->sqlResults['groupsForUser'])) { |
| 461 |
return $this->sqlResults['groupsForUser']; |
| 462 |
} |
| 463 |
|
| 464 |
global $current_user; |
| 465 |
//Force user infos |
| 466 |
wp_get_current_user(); |
| 467 |
|
| 468 |
$userUserGroups = $this->getUserGroupsForObject( |
| 469 |
'user', |
| 470 |
$current_user->ID, |
| 471 |
false |
| 472 |
); |
| 473 |
|
| 474 |
$userUserGroupArray = array(); |
| 475 |
|
| 476 |
foreach ($userUserGroups as $userUserGroup) { |
| 477 |
$userUserGroupArray[] = $userUserGroup->getId(); |
| 478 |
} |
| 479 |
|
| 480 |
if ($userUserGroupArray !== array()) { |
| 481 |
$userUserGroupString = implode(', ', $userUserGroupArray); |
| 482 |
} else { |
| 483 |
$userUserGroupString = "''"; |
| 484 |
} |
| 485 |
|
| 486 |
$this->sqlResults['groupsForUser'] = $userUserGroupString; |
| 487 |
|
| 488 |
return $this->sqlResults['groupsForUser']; |
| 489 |
} |
| 490 |
|
| 491 |
/** |
| 492 |
* Returns the categories assigned to the user. |
| 493 |
* |
| 494 |
* @return array |
| 495 |
*/ |
| 496 |
public function getCategoriesForUser() |
| 497 |
{ |
| 498 |
global $wpdb; |
| 499 |
|
| 500 |
if (isset($this->sqlResults['categoriesAssignedToUser'])) { |
| 501 |
return $this->sqlResults['categoriesAssignedToUser']; |
| 502 |
} |
| 503 |
|
| 504 |
$userUserGroupString = $this->_getUserGroupsForUserAsSqlString(); |
| 505 |
|
| 506 |
$categoriesAssignedToUserSql = " |
| 507 |
SELECT igc.object_id |
| 508 |
FROM ".DB_ACCESSGROUP_TO_OBJECT." AS igc |
| 509 |
WHERE igc.object_type = 'category' |
| 510 |
AND igc.group_id IN (".$userUserGroupString.")"; |
| 511 |
|
| 512 |
$this->sqlResults['categoriesAssignedToUser'] |
| 513 |
= $wpdb->get_col($categoriesAssignedToUserSql); |
| 514 |
|
| 515 |
return $this->sqlResults['categoriesAssignedToUser']; |
| 516 |
} |
| 517 |
|
| 518 |
/** |
| 519 |
* Returns the posts assigned to the user. |
| 520 |
* |
| 521 |
* @return array |
| 522 |
*/ |
| 523 |
public function getPostsForUser() |
| 524 |
{ |
| 525 |
global $wpdb; |
| 526 |
|
| 527 |
if (isset($this->sqlResults['postsAssignedToUser'])) { |
| 528 |
return $this->sqlResults['postsAssignedToUser']; |
| 529 |
} |
| 530 |
|
| 531 |
$userUserGroupString = $this->_getUserGroupsForUserAsSqlString(); |
| 532 |
|
| 533 |
$postAssignedToUserSql = " |
| 534 |
SELECT igp.object_id |
| 535 |
FROM ".DB_ACCESSGROUP_TO_OBJECT." AS igp |
| 536 |
WHERE igp.object_type = 'post' |
| 537 |
AND igp.group_id IN (".$userUserGroupString.")"; |
| 538 |
|
| 539 |
$this->sqlResults['postsAssignedToUser'] |
| 540 |
= $wpdb->get_col($postAssignedToUserSql); |
| 541 |
|
| 542 |
return $this->sqlResults['postsAssignedToUser']; |
| 543 |
} |
| 544 |
|
| 545 |
/** |
| 546 |
* Returns the excluded posts. |
| 547 |
* |
| 548 |
* @return array |
| 549 |
*/ |
| 550 |
public function getExcludedPosts() |
| 551 |
{ |
| 552 |
global $wpdb; |
| 553 |
|
| 554 |
if ($this->checkUserAccess('manage_user_groups')) { |
| 555 |
$this->sqlResults['excludedPosts'] = array(); |
| 556 |
} |
| 557 |
|
| 558 |
if (isset($this->sqlResults['excludedPosts'])) { |
| 559 |
return $this->sqlResults['excludedPosts']; |
| 560 |
} |
| 561 |
|
| 562 |
if ($this->getUserAccessManager()->atAdminPanel()) { |
| 563 |
$accessType = "write"; |
| 564 |
} else { |
| 565 |
$accessType = "read"; |
| 566 |
} |
| 567 |
|
| 568 |
$categoriesAssignedToUser = $this->getCategoriesForUser(); |
| 569 |
|
| 570 |
if ($categoriesAssignedToUser !== array()) { |
| 571 |
$categoriesAssignedToUserString |
| 572 |
= implode(', ', $categoriesAssignedToUser); |
| 573 |
} else { |
| 574 |
$categoriesAssignedToUserString = "''"; |
| 575 |
} |
| 576 |
|
| 577 |
$postAssignedToUser = $this->getPostsForUser(); |
| 578 |
|
| 579 |
if ($postAssignedToUser !== array()) { |
| 580 |
$postAssignedToUserString |
| 581 |
= implode(', ', $postAssignedToUser); |
| 582 |
} else { |
| 583 |
$postAssignedToUserString = "''"; |
| 584 |
} |
| 585 |
|
| 586 |
$postSql = "SELECT DISTINCT p.ID |
| 587 |
FROM $wpdb->posts AS p |
| 588 |
INNER JOIN $wpdb->term_relationships AS tr |
| 589 |
ON p.ID = tr.object_id |
| 590 |
INNER JOIN $wpdb->term_taxonomy tt |
| 591 |
ON tr.term_taxonomy_id = tt.term_taxonomy_id |
| 592 |
WHERE tt.taxonomy = 'category' |
| 593 |
AND tt.term_id IN ( |
| 594 |
SELECT gc.object_id |
| 595 |
FROM ".DB_ACCESSGROUP." iag |
| 596 |
INNER JOIN ".DB_ACCESSGROUP_TO_OBJECT." AS gc |
| 597 |
ON iag.id = gc.group_id |
| 598 |
WHERE gc.object_type = 'category' |
| 599 |
AND iag.".$accessType."_access != 'all' |
| 600 |
AND gc.object_id NOT IN (".$categoriesAssignedToUserString.") |
| 601 |
) AND p.ID NOT IN (".$postAssignedToUserString.") |
| 602 |
UNION |
| 603 |
SELECT DISTINCT gp.object_id |
| 604 |
FROM ".DB_ACCESSGROUP." AS ag |
| 605 |
INNER JOIN ".DB_ACCESSGROUP_TO_OBJECT." AS gp |
| 606 |
ON ag.id = gp.group_id |
| 607 |
INNER JOIN $wpdb->term_relationships AS tr |
| 608 |
ON gp.object_id = tr.object_id |
| 609 |
INNER JOIN $wpdb->term_taxonomy tt |
| 610 |
ON tr.term_taxonomy_id = tt.term_taxonomy_id |
| 611 |
WHERE gp.object_type = 'post' |
| 612 |
AND ag.".$accessType."_access != 'all' |
| 613 |
AND gp.object_id NOT IN (".$postAssignedToUserString.") |
| 614 |
AND tt.term_id NOT IN (".$categoriesAssignedToUserString.")"; |
| 615 |
|
| 616 |
$this->sqlResults['excludedPosts'] = $wpdb->get_col($postSql); |
| 617 |
|
| 618 |
return $this->sqlResults['excludedPosts']; |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
/* |
| 623 |
* Other functions |
| 624 |
*/ |
| 625 |
|
| 626 |
/** |
| 627 |
* Checks if the given ip matches with the range. |
| 628 |
* |
| 629 |
* @param string $curIp The ip of the current user. |
| 630 |
* @param array $ipRanges The ip ranges. |
| 631 |
* |
| 632 |
* @return boolean |
| 633 |
*/ |
| 634 |
public function checkUserIp($curIp, $ipRanges) |
| 635 |
{ |
| 636 |
if (isset($ipRanges)) { |
| 637 |
foreach ($ipRanges as $ipRange) { |
| 638 |
$ipRange = explode("-", $ipRange); |
| 639 |
$rangeBegin = explode(".", $ipRange[0]); |
| 640 |
|
| 641 |
if (isset($ipRange[1])) { |
| 642 |
$rangeEnd = explode(".", $ipRange[1]); |
| 643 |
} else { |
| 644 |
$rangeEnd = explode(".", $ipRange[0]); |
| 645 |
} |
| 646 |
|
| 647 |
if ($rangeBegin[0] <= $curIp[0] |
| 648 |
&& $curIp[0] <= $rangeEnd[0] |
| 649 |
&& $rangeBegin[1] <= $curIp[1] |
| 650 |
&& $curIp[1] <= $rangeEnd[1] |
| 651 |
&& $rangeBegin[2] <= $curIp[2] |
| 652 |
&& $curIp[2] <= $rangeEnd[2] |
| 653 |
&& $rangeBegin[3] <= $curIp[3] |
| 654 |
&& $curIp[3] <= $rangeEnd[3] |
| 655 |
) { |
| 656 |
return true; |
| 657 |
} |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
return false; |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Return the role of the user. |
| 666 |
* |
| 667 |
* @param integer $userId The user id. |
| 668 |
* |
| 669 |
* @return string|null |
| 670 |
*/ |
| 671 |
private function _getUserRole($userId) |
| 672 |
{ |
| 673 |
global $wpdb; |
| 674 |
|
| 675 |
$curUserdata = get_userdata($userId); |
| 676 |
|
| 677 |
if (!isset($curUserdata->user_level)) { |
| 678 |
$curUserdata->user_level = null; |
| 679 |
} |
| 680 |
|
| 681 |
if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) { |
| 682 |
$capabilities = $curUserdata->{$wpdb->prefix . "capabilities"}; |
| 683 |
} else { |
| 684 |
$capabilities = null; |
| 685 |
} |
| 686 |
|
| 687 |
$role = (is_array($capabilities) && count($capabilities) > 0) ? array_keys($capabilities) : array('norole'); |
| 688 |
|
| 689 |
return trim($role[0]); |
| 690 |
} |
| 691 |
|
| 692 |
/** |
| 693 |
* Checks if the user is an admin user |
| 694 |
* |
| 695 |
* @param integer $userId The user id. |
| 696 |
* |
| 697 |
* @return boolean |
| 698 |
*/ |
| 699 |
public function userIsAdmin($userId) |
| 700 |
{ |
| 701 |
$role = $this->_getUserRole($userId); |
| 702 |
|
| 703 |
if ($role == 'administrator' |
| 704 |
|| is_super_admin($userId) |
| 705 |
) { |
| 706 |
return true; |
| 707 |
} |
| 708 |
|
| 709 |
return false; |
| 710 |
} |
| 711 |
|
| 712 |
/** |
| 713 |
* Checks the user access by user level. |
| 714 |
* |
| 715 |
* @return boolean |
| 716 |
*/ |
| 717 |
public function checkUserAccess($allowedCapability = false) |
| 718 |
{ |
| 719 |
global $current_user; |
| 720 |
//Force user infos |
| 721 |
wp_get_current_user(); |
| 722 |
|
| 723 |
$uamOptions = $this->getUserAccessManager()->getAdminOptions(); |
| 724 |
|
| 725 |
$role = $this->_getUserRole($current_user->ID); |
| 726 |
$orderedRoles = $this->getRolesOrdered(); |
| 727 |
|
| 728 |
if (isset($orderedRoles[$role]) |
| 729 |
&& $orderedRoles[$role] >= $orderedRoles[$uamOptions['full_access_role']] |
| 730 |
|| $role == 'administrator' |
| 731 |
|| is_super_admin($current_user->ID) |
| 732 |
|| ($allowedCapability && $current_user->has_cap($allowedCapability)) |
| 733 |
) { |
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
return false; |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Returns the roles as assoziative array. |
| 742 |
* |
| 743 |
* @return array |
| 744 |
*/ |
| 745 |
public function getRolesOrdered() |
| 746 |
{ |
| 747 |
$orderedRoles = array( |
| 748 |
'norole' => 0, |
| 749 |
'subscriber' => 1, |
| 750 |
'contributor' => 2, |
| 751 |
'author' => 3, |
| 752 |
'editor' => 4, |
| 753 |
'administrator' => 5 |
| 754 |
); |
| 755 |
|
| 756 |
return $orderedRoles; |
| 757 |
} |
| 758 |
|
| 759 |
/** |
| 760 |
* Registers object that should be handelt by the user access manager. |
| 761 |
* |
| 762 |
* @param array $object The object which you want to register. |
| 763 |
* |
| 764 |
* @return boolean |
| 765 |
*/ |
| 766 |
public function registerPlObject($object) |
| 767 |
{ |
| 768 |
if (!isset($object['name']) |
| 769 |
|| !isset($object['reference']) |
| 770 |
|| !isset($object['getFull']) |
| 771 |
|| !isset($object['getFullObjects']) |
| 772 |
) { |
| 773 |
return false; |
| 774 |
} |
| 775 |
|
| 776 |
$this->plObjects[$object['name']] = $object; |
| 777 |
|
| 778 |
return true; |
| 779 |
} |
| 780 |
|
| 781 |
/** |
| 782 |
* Returns a registerd pluggable object. |
| 783 |
* |
| 784 |
* @param string $objectName The name of the object which should be returned. |
| 785 |
* |
| 786 |
* @return array |
| 787 |
*/ |
| 788 |
public function getPlObject($objectName) |
| 789 |
{ |
| 790 |
if (isset($this->plObjects[$objectName])) { |
| 791 |
return $this->plObjects[$objectName]; |
| 792 |
} |
| 793 |
|
| 794 |
return array(); |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* Returns all registerd pluggable objects. |
| 799 |
* |
| 800 |
* @return array |
| 801 |
*/ |
| 802 |
public function getPlObjects() |
| 803 |
{ |
| 804 |
return $this->plObjects; |
| 805 |
} |
| 806 |
} |