| 1 |
<?php |
| 2 |
/** |
| 3 |
* UamUserGroup.class.php |
| 4 |
* |
| 5 |
* The UamUserGroup class file. |
| 6 |
* |
| 7 |
* PHP versions 5 |
| 8 |
* |
| 9 |
* @category UserAccessManager |
| 10 |
* @package UserAccessManager |
| 11 |
* @author Alexander Schneider <alexanderschneider85@googlemail.com> |
| 12 |
* @copyright 2008-2013 Alexander Schneider |
| 13 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 14 |
* @version SVN: $Id$ |
| 15 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 16 |
*/ |
| 17 |
|
| 18 |
/** |
| 19 |
* The user group class. |
| 20 |
* |
| 21 |
* @category UserAccessManager |
| 22 |
* @package UserAccessManager |
| 23 |
* @author Alexander Schneider <alexanderschneider85@gmail.com> |
| 24 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 |
| 25 |
* @link http://wordpress.org/extend/plugins/user-access-manager/ |
| 26 |
*/ |
| 27 |
|
| 28 |
class UamUserGroup |
| 29 |
{ |
| 30 |
protected $_oAccessHandler = null; |
| 31 |
protected $_iId = null; |
| 32 |
protected $_sGroupName = null; |
| 33 |
protected $_sGroupDesc = null; |
| 34 |
protected $_sReadAccess = null; |
| 35 |
protected $_sWriteAccess = null; |
| 36 |
protected $_sIpRange = null; |
| 37 |
protected $_aRoles = array(); |
| 38 |
protected $_aObjects = null; |
| 39 |
protected $_aSingleObjects = null; |
| 40 |
protected $_aAssignedObjects = null; |
| 41 |
protected $_aPlObjects = array(); |
| 42 |
public $aSetRecursive = array(); |
| 43 |
|
| 44 |
/** |
| 45 |
* Constructor |
| 46 |
* |
| 47 |
* @param object $oUamAccessHandler The access handler object. |
| 48 |
* @param integer $iId The _iId of the user group. |
| 49 |
*/ |
| 50 |
public function __construct($oUamAccessHandler, $iId = null) |
| 51 |
{ |
| 52 |
$this->_oAccessHandler = $oUamAccessHandler; |
| 53 |
|
| 54 |
//Create default values for the _aObjects. |
| 55 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 56 |
$this->_aObjects[$sObjectType] = array( |
| 57 |
'real' => -1, |
| 58 |
'full' => -1 |
| 59 |
); |
| 60 |
$this->_aAssignedObjects[$sObjectType] = null; |
| 61 |
} |
| 62 |
|
| 63 |
if ($iId !== null) { |
| 64 |
/** |
| 65 |
* @var wpdb $wpdb |
| 66 |
*/ |
| 67 |
global $wpdb; |
| 68 |
|
| 69 |
$this->_iId = $iId; |
| 70 |
|
| 71 |
$aDbUserGroup = $wpdb->get_row( |
| 72 |
"SELECT * |
| 73 |
FROM ".DB_ACCESSGROUP." |
| 74 |
WHERE ID = ".$this->getId()." |
| 75 |
LIMIT 1", |
| 76 |
ARRAY_A |
| 77 |
); |
| 78 |
|
| 79 |
$this->_sGroupName = $aDbUserGroup['groupname']; |
| 80 |
$this->_sGroupDesc = $aDbUserGroup['groupdesc']; |
| 81 |
$this->_sReadAccess = $aDbUserGroup['read_access']; |
| 82 |
$this->_sWriteAccess = $aDbUserGroup['write_access']; |
| 83 |
$this->_sIpRange = $aDbUserGroup['ip_range']; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the user access handler object. |
| 89 |
* |
| 90 |
* @return UamAccessHandler |
| 91 |
*/ |
| 92 |
public function &getAccessHandler() |
| 93 |
{ |
| 94 |
return $this->_oAccessHandler; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Deletes the user group. |
| 99 |
* |
| 100 |
* @return boolean |
| 101 |
*/ |
| 102 |
public function delete() |
| 103 |
{ |
| 104 |
if ($this->_iId == null) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @var wpdb $wpdb |
| 110 |
*/ |
| 111 |
global $wpdb; |
| 112 |
|
| 113 |
$wpdb->query( |
| 114 |
"DELETE FROM " . DB_ACCESSGROUP . " |
| 115 |
WHERE ID = $this->_iId LIMIT 1" |
| 116 |
); |
| 117 |
|
| 118 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 119 |
$this->_deleteObjectsFromDb($sObjectType); |
| 120 |
} |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Saves the user group. |
| 127 |
* |
| 128 |
* @return null; |
| 129 |
*/ |
| 130 |
public function save() |
| 131 |
{ |
| 132 |
/** |
| 133 |
* @var wpdb $wpdb |
| 134 |
*/ |
| 135 |
global $wpdb; |
| 136 |
|
| 137 |
if ($this->_iId == null) { |
| 138 |
$wpdb->query( |
| 139 |
"INSERT INTO " . DB_ACCESSGROUP . " ( |
| 140 |
ID, |
| 141 |
groupname, |
| 142 |
groupdesc, |
| 143 |
read_access, |
| 144 |
write_access, |
| 145 |
ip_range |
| 146 |
) |
| 147 |
VALUES ( |
| 148 |
NULL, |
| 149 |
'" . $this->_sGroupName . "', |
| 150 |
'" . $this->_sGroupDesc . "', |
| 151 |
'" . $this->_sReadAccess . "', |
| 152 |
'" . $this->_sWriteAccess . "', |
| 153 |
'" . $this->_sIpRange . "' |
| 154 |
)" |
| 155 |
); |
| 156 |
|
| 157 |
$this->_iId = $wpdb->insert_id; |
| 158 |
} else { |
| 159 |
$wpdb->query( |
| 160 |
"UPDATE " . DB_ACCESSGROUP . " |
| 161 |
SET groupname = '" . $this->_sGroupName . "', |
| 162 |
groupdesc = '" . $this->_sGroupDesc . "', |
| 163 |
read_access = '" . $this->_sReadAccess . "', |
| 164 |
write_access = '" . $this->_sWriteAccess . "', |
| 165 |
ip_range = '" . $this->_sIpRange . "' |
| 166 |
WHERE ID = " . $this->_iId |
| 167 |
); |
| 168 |
|
| 169 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 170 |
//Load to object |
| 171 |
$this->getObjectsFromType($sObjectType); |
| 172 |
//Delete form database |
| 173 |
$this->_deleteObjectsFromDb($sObjectType); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
foreach ($this->getAllObjectTypes() as $sObjectType) { |
| 178 |
$aKeys = array_keys($this->getObjectsFromType($sObjectType)); |
| 179 |
|
| 180 |
if (count($aKeys) > 0) { |
| 181 |
$sSql = $this->_getSqlQuery($sObjectType, 'insert', $aKeys); |
| 182 |
$wpdb->query($sSql); |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* |
| 189 |
* Primary values. |
| 190 |
*/ |
| 191 |
|
| 192 |
/** |
| 193 |
* Returns the group _iId. |
| 194 |
* |
| 195 |
* @return integer |
| 196 |
*/ |
| 197 |
public function getId() |
| 198 |
{ |
| 199 |
return $this->_iId; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Returns the group name. |
| 204 |
* |
| 205 |
* @return string |
| 206 |
*/ |
| 207 |
public function getGroupName() |
| 208 |
{ |
| 209 |
return $this->_sGroupName; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Sets the group name. |
| 214 |
* |
| 215 |
* @param string $sGroupName The new group name. |
| 216 |
* |
| 217 |
* @return null |
| 218 |
*/ |
| 219 |
public function setGroupName($sGroupName) |
| 220 |
{ |
| 221 |
$this->_sGroupName = $sGroupName; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Returns the group description. |
| 226 |
* |
| 227 |
* @return string |
| 228 |
*/ |
| 229 |
public function getGroupDesc() |
| 230 |
{ |
| 231 |
return $this->_sGroupDesc; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Sets the group description. |
| 236 |
* |
| 237 |
* @param string $sGroupDesc The new group description. |
| 238 |
* |
| 239 |
* @return null |
| 240 |
*/ |
| 241 |
public function setGroupDesc($sGroupDesc) |
| 242 |
{ |
| 243 |
$this->_sGroupDesc = $sGroupDesc; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the read access. |
| 248 |
* |
| 249 |
* @return string |
| 250 |
*/ |
| 251 |
public function getReadAccess() |
| 252 |
{ |
| 253 |
return $this->_sReadAccess; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Sets the read access. |
| 258 |
* |
| 259 |
* @param string $sReadAccess The read access. |
| 260 |
* |
| 261 |
* @return null |
| 262 |
*/ |
| 263 |
public function setReadAccess($sReadAccess) |
| 264 |
{ |
| 265 |
$this->_sReadAccess = $sReadAccess; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Returns the write access. |
| 270 |
* |
| 271 |
* @return string |
| 272 |
*/ |
| 273 |
public function getWriteAccess() |
| 274 |
{ |
| 275 |
return $this->_sWriteAccess; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Sets the write access. |
| 280 |
* |
| 281 |
* @param string $sWriteAccess The wirte access. |
| 282 |
* |
| 283 |
* @return null |
| 284 |
*/ |
| 285 |
public function setWriteAccess($sWriteAccess) |
| 286 |
{ |
| 287 |
$this->_sWriteAccess = $sWriteAccess; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Returns the ip range. |
| 292 |
* |
| 293 |
* @param string $sType The return type. |
| 294 |
* |
| 295 |
* @return array|string |
| 296 |
*/ |
| 297 |
public function getIpRange($sType = null) |
| 298 |
{ |
| 299 |
if ($sType == 'string') { |
| 300 |
return $this->_sIpRange; |
| 301 |
} |
| 302 |
|
| 303 |
$aIpRange = explode(';', $this->_sIpRange); |
| 304 |
|
| 305 |
if ($aIpRange[0] == null) { |
| 306 |
return null; |
| 307 |
} |
| 308 |
|
| 309 |
return $aIpRange; |
| 310 |
} |
| 311 |
|
| 312 |
/** |
| 313 |
* Sets the ip range. |
| 314 |
* |
| 315 |
* @param string|array $mIpRange The new ip range. |
| 316 |
* |
| 317 |
* @return null |
| 318 |
*/ |
| 319 |
public function setIpRange($mIpRange) |
| 320 |
{ |
| 321 |
if (is_array($mIpRange)) { |
| 322 |
$mIpRange = implode(';', $mIpRange); |
| 323 |
} |
| 324 |
|
| 325 |
$this->_sIpRange = $mIpRange; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Returns all _aObjects types. |
| 330 |
* |
| 331 |
* @return array |
| 332 |
*/ |
| 333 |
public function getAllObjectTypes() |
| 334 |
{ |
| 335 |
return $this->getAccessHandler()->getAllObjectTypes(); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
/* |
| 340 |
* Meta functions. |
| 341 |
*/ |
| 342 |
|
| 343 |
/** |
| 344 |
* Magic method getter. |
| 345 |
* |
| 346 |
* @param string $sName The name of the function |
| 347 |
* @param array $aArguments The arguments for the function |
| 348 |
* |
| 349 |
* @return mixed |
| 350 |
*/ |
| 351 |
public function __call($sName, $aArguments) |
| 352 |
{ |
| 353 |
$oUam = $this->getAccessHandler()->getUserAccessManager(); |
| 354 |
|
| 355 |
$sPrefix = ''; |
| 356 |
|
| 357 |
if ($oUam->startsWith($sName, 'add')) { |
| 358 |
$sPrefix = 'add'; |
| 359 |
} elseif ($oUam->startsWith($sName, 'remove')) { |
| 360 |
$sPrefix = 'remove'; |
| 361 |
} elseif ($oUam->startsWith($sName, 'isMember')) { |
| 362 |
$sPrefix = 'isMember'; |
| 363 |
} |
| 364 |
|
| 365 |
$sObjectType = str_replace($sPrefix, '', $sName); |
| 366 |
$sObjectType = strtolower($sObjectType); |
| 367 |
|
| 368 |
$iObjectId = $aArguments[0]; |
| 369 |
|
| 370 |
if ($sPrefix == 'add') { |
| 371 |
return $this->addObject( |
| 372 |
$sObjectType, |
| 373 |
$iObjectId |
| 374 |
); |
| 375 |
} elseif ($sPrefix == 'remove') { |
| 376 |
return $this->removeObject( |
| 377 |
$sObjectType, |
| 378 |
$iObjectId |
| 379 |
); |
| 380 |
} elseif ($sPrefix == 'ismember') { |
| 381 |
$blWithInfo = $aArguments[1]; |
| 382 |
|
| 383 |
return $this->objectIsMember( |
| 384 |
$sObjectType, |
| 385 |
$iObjectId, |
| 386 |
$blWithInfo |
| 387 |
); |
| 388 |
} |
| 389 |
|
| 390 |
return null; |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Returns the sql query. |
| 395 |
* |
| 396 |
* @param string $sObjectType The object type. |
| 397 |
* @param string $sAction The sql action. |
| 398 |
* @param array $aKeys The keys for the insert query. |
| 399 |
* |
| 400 |
* @return string |
| 401 |
*/ |
| 402 |
protected function _getSqlQuery($sObjectType, $sAction, $aKeys = array()) |
| 403 |
{ |
| 404 |
$sSql = ''; |
| 405 |
|
| 406 |
if ($sAction == 'select') { |
| 407 |
$sSql = "SELECT object_id as id |
| 408 |
FROM ".DB_ACCESSGROUP_TO_OBJECT." |
| 409 |
WHERE group_id = ".$this->getId()." |
| 410 |
AND object_type = '".$sObjectType ."'"; |
| 411 |
} elseif ($sAction == 'delete') { |
| 412 |
$sSql = "DELETE FROM ".DB_ACCESSGROUP_TO_OBJECT." |
| 413 |
WHERE group_id = ".$this->getId()." |
| 414 |
AND object_type = '".$sObjectType ."'"; |
| 415 |
} elseif ($sAction == 'insert') { |
| 416 |
$sSql = "INSERT INTO ".DB_ACCESSGROUP_TO_OBJECT." ( |
| 417 |
group_id, |
| 418 |
object_id, |
| 419 |
object_type |
| 420 |
) VALUES "; |
| 421 |
|
| 422 |
foreach ($aKeys as $sKey) { |
| 423 |
$sKey = trim($sKey); |
| 424 |
$sSql .= "('".$this->getId()."', '".$sKey."', '".$sObjectType."'), "; |
| 425 |
} |
| 426 |
|
| 427 |
$sSql = rtrim($sSql, ', '); |
| 428 |
$sSql .= " ON DUPLICATE KEY UPDATE group_id = group_id "; |
| 429 |
} |
| 430 |
|
| 431 |
return $sSql; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Adds a object of the given type. |
| 436 |
* |
| 437 |
* @param string $sObjectType The object type. |
| 438 |
* @param integer $iObjectId The object _iId. |
| 439 |
* |
| 440 |
* @return null |
| 441 |
*/ |
| 442 |
public function addObject($sObjectType, $iObjectId) |
| 443 |
{ |
| 444 |
if (!in_array($sObjectType, $this->getAllObjectTypes())) { |
| 445 |
return; |
| 446 |
} |
| 447 |
|
| 448 |
$this->getAccessHandler()->unsetUserGroupsForObject(); |
| 449 |
$this->getObjectsFromType($sObjectType); |
| 450 |
|
| 451 |
$oObject = new stdClass; |
| 452 |
$oObject->iId = $iObjectId; |
| 453 |
|
| 454 |
$this->_aObjects[$sObjectType]['real'][$iObjectId] = $oObject; |
| 455 |
$this->_aObjects[$sObjectType]['full'] = -1; |
| 456 |
|
| 457 |
$this->_aAssignedObjects[$sObjectType][$iObjectId] = $iObjectId; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Removes a object of the given type. |
| 462 |
* |
| 463 |
* @param string $sObjectType The object type. |
| 464 |
* @param integer $sObjectId The object _iId. |
| 465 |
* |
| 466 |
* @return null |
| 467 |
*/ |
| 468 |
public function removeObject($sObjectType, $sObjectId) |
| 469 |
{ |
| 470 |
if (!in_array($sObjectType, $this->getAllObjectTypes())) { |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
$this->getAccessHandler()->unsetUserGroupsForObject(); |
| 475 |
$this->getObjectsFromType($sObjectType); |
| 476 |
|
| 477 |
unset($this->_aObjects[$sObjectType]['real'][$sObjectId]); |
| 478 |
$this->_aObjects[$sObjectType]['full'] = -1; |
| 479 |
|
| 480 |
unset($this->_aSingleObjects[$sObjectType][$sObjectId]); |
| 481 |
unset($this->_aAssignedObjects[$sObjectType][$sObjectId]); |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Returns the assigned _aObjects. |
| 486 |
* |
| 487 |
* @param string $sObjectType The object type. |
| 488 |
* |
| 489 |
* @return array |
| 490 |
*/ |
| 491 |
protected function _getAssignedObjects($sObjectType) |
| 492 |
{ |
| 493 |
if ($this->_aAssignedObjects[$sObjectType] !== null) { |
| 494 |
return $this->_aAssignedObjects[$sObjectType]; |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* @var wpdb $wpdb |
| 499 |
*/ |
| 500 |
global $wpdb; |
| 501 |
|
| 502 |
$aDbObjects = $wpdb->get_results( |
| 503 |
$this->_getSqlQuery($sObjectType, 'select') |
| 504 |
); |
| 505 |
|
| 506 |
$this->_aAssignedObjects[$sObjectType] = array(); |
| 507 |
|
| 508 |
foreach ($aDbObjects as $oDbObject) { |
| 509 |
$this->_aAssignedObjects[$sObjectType][$oDbObject->id] |
| 510 |
= $oDbObject->id; |
| 511 |
} |
| 512 |
|
| 513 |
return $this->_aAssignedObjects[$sObjectType]; |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* Checks if the object is assigned to the group. |
| 518 |
* |
| 519 |
* @param string $sObjectType The object type. |
| 520 |
* @param integer $iObjectId The object _iId. |
| 521 |
* |
| 522 |
* @return boolean |
| 523 |
*/ |
| 524 |
protected function _isObjectAssignedToGroup($sObjectType, $iObjectId) |
| 525 |
{ |
| 526 |
return array_key_exists( |
| 527 |
$iObjectId, |
| 528 |
$this->_getAssignedObjects($sObjectType) |
| 529 |
); |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* Unset the objects. |
| 534 |
* |
| 535 |
* @param string $sObjectType The object type. |
| 536 |
* @param boolean $blPlusRemove If true also database entrys will remove. |
| 537 |
* |
| 538 |
* @return null; |
| 539 |
*/ |
| 540 |
public function unsetObjects($sObjectType, $blPlusRemove = false) |
| 541 |
{ |
| 542 |
if (!in_array($sObjectType, $this->getAllObjectTypes())) { |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
if ($blPlusRemove) { |
| 547 |
$this->_deleteObjectsFromDb($sObjectType); |
| 548 |
} |
| 549 |
|
| 550 |
$this->_aObjects[$sObjectType] = array( |
| 551 |
'real' => array(), |
| 552 |
'full' => array(), |
| 553 |
); |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* Removes all _aObjects from the user group. |
| 558 |
* |
| 559 |
* @param string $sObjectType The object type. |
| 560 |
* |
| 561 |
* @return null |
| 562 |
*/ |
| 563 |
protected function _deleteObjectsFromDb($sObjectType) |
| 564 |
{ |
| 565 |
if (isset($this->_iId)) { |
| 566 |
/** |
| 567 |
* @var wpdb $wpdb |
| 568 |
*/ |
| 569 |
global $wpdb; |
| 570 |
|
| 571 |
$wpdb->query( |
| 572 |
$this->_getSqlQuery($sObjectType, 'delete') |
| 573 |
); |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Checks if the given object is a member of the group. |
| 579 |
* |
| 580 |
* @param string $sObjectType The object type. |
| 581 |
* @param integer $iObjectId The _iId of the object which should be checked. |
| 582 |
* @param boolean $blWithInfo If true then we return additional infos. |
| 583 |
* |
| 584 |
* @return boolean |
| 585 |
*/ |
| 586 |
public function objectIsMember($sObjectType, $iObjectId, $blWithInfo = false) |
| 587 |
{ |
| 588 |
if (!in_array($sObjectType, $this->getAllObjectTypes())) { |
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
$oObject = $this->_getSingleObject($sObjectType, $iObjectId, 'full'); |
| 593 |
|
| 594 |
if ($oObject !== null) { |
| 595 |
if (isset($oObject->recursiveMember) |
| 596 |
&& $blWithInfo |
| 597 |
) { |
| 598 |
return $oObject->recursiveMember; |
| 599 |
} |
| 600 |
|
| 601 |
return true; |
| 602 |
} |
| 603 |
|
| 604 |
return false; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Returns all objects of the given type. |
| 609 |
* |
| 610 |
* @param string $sObjectType The object type. |
| 611 |
* @param string $sType The return type, could be real or full. |
| 612 |
* |
| 613 |
* @return array |
| 614 |
*/ |
| 615 |
function getObjectsFromType($sObjectType, $sType = 'real') |
| 616 |
{ |
| 617 |
if (!in_array($sObjectType, $this->getAllObjectTypes())) { |
| 618 |
return null; |
| 619 |
} |
| 620 |
|
| 621 |
if ($this->_iId == null || $sType != 'real' && $sType != 'full') { |
| 622 |
return array(); |
| 623 |
} |
| 624 |
|
| 625 |
if (isset($this->_aObjects[$sObjectType]) |
| 626 |
&& isset($this->_aObjects[$sObjectType][$sType]) |
| 627 |
&& $this->_aObjects[$sObjectType][$sType] != -1 |
| 628 |
) { |
| 629 |
return $this->_aObjects[$sObjectType][$sType]; |
| 630 |
} else { |
| 631 |
$this->_aObjects[$sObjectType][$sType] = array(); |
| 632 |
} |
| 633 |
|
| 634 |
/** |
| 635 |
* @var wpdb $wpdb |
| 636 |
*/ |
| 637 |
global $wpdb; |
| 638 |
|
| 639 |
$aDbObjects = $wpdb->get_results( |
| 640 |
$this->_getSqlQuery($sObjectType, 'select') |
| 641 |
); |
| 642 |
|
| 643 |
foreach ($aDbObjects as $oDbObject) { |
| 644 |
$oObject = $this->_getSingleObject($sObjectType, $oDbObject->id, $sType); |
| 645 |
|
| 646 |
if ($oObject !== null) { |
| 647 |
$this->_aObjects[$sObjectType][$sType][$oObject->id] = $oObject; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
$aPostableTypes = $this->getAccessHandler()->getPostableTypes(); |
| 652 |
|
| 653 |
if ($sType == 'full' && !in_array($sObjectType, $aPostableTypes) && $sObjectType != 'role') { |
| 654 |
if ($sObjectType == 'category') { |
| 655 |
$this->_aObjects[$sObjectType][$sType] = $this->getFullCategories($this->_aObjects[$sObjectType][$sType]); |
| 656 |
} elseif ($sObjectType == 'user') { |
| 657 |
$this->_aObjects[$sObjectType][$sType] = $this->getFullUsers(); |
| 658 |
} else { |
| 659 |
$oPlObject = $this->getAccessHandler()->getPlObject($sObjectType); |
| 660 |
$this->_aObjects[$sObjectType][$sType] = $oPlObject['reference']->{$oPlObject['getFullObjects']}( |
| 661 |
$this->_aObjects[$sObjectType][$sType], |
| 662 |
$this |
| 663 |
); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
return $this->_aObjects[$sObjectType][$sType]; |
| 668 |
} |
| 669 |
|
| 670 |
/** |
| 671 |
* Returns a single object. |
| 672 |
* |
| 673 |
* @param string $sObjectType The object type. |
| 674 |
* @param integer $iObjectId The _iId of the object which should be checked. |
| 675 |
* @param string $sType The return type. Can be real or full. |
| 676 |
* |
| 677 |
* @return object |
| 678 |
*/ |
| 679 |
protected function _getSingleObject($sObjectType, $iObjectId, $sType) |
| 680 |
{ |
| 681 |
if (isset($this->_aSingleObjects[$sObjectType][$iObjectId])) { |
| 682 |
return $this->_aSingleObjects[$sObjectType][$iObjectId]; |
| 683 |
} |
| 684 |
|
| 685 |
$aIsRecursiveMember = array(); |
| 686 |
|
| 687 |
if ($sType == 'full' && $sObjectType != 'role') { |
| 688 |
$aPostableTypes = $this->getAccessHandler()->getPostableTypes(); |
| 689 |
|
| 690 |
if (in_array($sObjectType, $aPostableTypes)) { |
| 691 |
$aIsRecursiveMember = $this->_getFullPost($sObjectType, $iObjectId); |
| 692 |
} else if ($sObjectType == 'category') { |
| 693 |
$aIsRecursiveMember = $this->_getFullCategory($iObjectId); |
| 694 |
} else if ($sObjectType == 'user') { |
| 695 |
$aIsRecursiveMember = $this->_getFullUser($iObjectId); |
| 696 |
} else { |
| 697 |
$aIsRecursiveMember = $this->_getFullPlObject($sObjectType, $iObjectId); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
if ($this->_isObjectAssignedToGroup($sObjectType, $iObjectId) |
| 702 |
|| $aIsRecursiveMember != array() |
| 703 |
) { |
| 704 |
$oObject = new stdClass; |
| 705 |
$oObject->id = $iObjectId; |
| 706 |
|
| 707 |
if ($aIsRecursiveMember != array()) { |
| 708 |
$oObject->recursiveMember = $aIsRecursiveMember; |
| 709 |
} |
| 710 |
|
| 711 |
$this->_aSingleObjects[$sObjectType][$iObjectId] = $oObject; |
| 712 |
} else { |
| 713 |
$this->_aSingleObjects[$sObjectType][$iObjectId] = null; |
| 714 |
} |
| 715 |
|
| 716 |
return $this->_aSingleObjects[$sObjectType][$iObjectId]; |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
/* |
| 721 |
* Group users functions. |
| 722 |
*/ |
| 723 |
|
| 724 |
/** |
| 725 |
* Returns a single user. |
| 726 |
* |
| 727 |
* @param integer $iObjectId The object _iId. |
| 728 |
* |
| 729 |
* @return object |
| 730 |
*/ |
| 731 |
protected function _getFullUser($iObjectId) |
| 732 |
{ |
| 733 |
$aIsRecursiveMember = array(); |
| 734 |
|
| 735 |
global $wpdb; |
| 736 |
|
| 737 |
$oCurUserData = get_userdata($iObjectId); |
| 738 |
|
| 739 |
if (isset($oCurUserData->{$wpdb->prefix . "capabilities"})) { |
| 740 |
$aCapabilities = $oCurUserData->{$wpdb->prefix . "capabilities"}; |
| 741 |
} else { |
| 742 |
$aCapabilities = array(); |
| 743 |
} |
| 744 |
|
| 745 |
$aRole = (count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole'); |
| 746 |
$sRole = $aRole[0]; |
| 747 |
$aObjects = $this->getObjectsFromType('role'); |
| 748 |
|
| 749 |
if (isset($aObjects[$sRole])) { |
| 750 |
$oRoleObject = new stdClass(); |
| 751 |
$oRoleObject->name = $sRole; |
| 752 |
|
| 753 |
$aIsRecursiveMember = array('role' => array()); |
| 754 |
$aIsRecursiveMember['role'][] = $oRoleObject; |
| 755 |
} |
| 756 |
|
| 757 |
return $aIsRecursiveMember; |
| 758 |
} |
| 759 |
|
| 760 |
/** |
| 761 |
* Returns the users in the group |
| 762 |
* |
| 763 |
* @return array |
| 764 |
*/ |
| 765 |
public function getFullUsers() |
| 766 |
{ |
| 767 |
/** |
| 768 |
* @var wpdb $wpdb |
| 769 |
*/ |
| 770 |
global $wpdb; |
| 771 |
|
| 772 |
$aDbUsers = $wpdb->get_results( |
| 773 |
"SELECT ID, user_nicename |
| 774 |
FROM ".$wpdb->users |
| 775 |
); |
| 776 |
|
| 777 |
$aFullUsers = array(); |
| 778 |
|
| 779 |
if (isset($aDbUsers)) { |
| 780 |
foreach ($aDbUsers as $oDbUser) { |
| 781 |
$oUser = $this->_getSingleObject('user', $oDbUser->ID, 'full'); |
| 782 |
|
| 783 |
if ($oUser !== null) { |
| 784 |
$aFullUsers[$oUser->id] = $oUser; |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
return $aFullUsers; |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
/* |
| 794 |
* Group categories functions. |
| 795 |
*/ |
| 796 |
|
| 797 |
/** |
| 798 |
* Returns a single category. |
| 799 |
* |
| 800 |
* @param integer $iObjectId The object _iId. |
| 801 |
* |
| 802 |
* @return object |
| 803 |
*/ |
| 804 |
protected function _getFullCategory($iObjectId) |
| 805 |
{ |
| 806 |
$oCategory = get_category($iObjectId); |
| 807 |
|
| 808 |
$aIsRecursiveMember = array(); |
| 809 |
|
| 810 |
$oUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 811 |
$aUamOptions = $oUserAccessManager->getAdminOptions(); |
| 812 |
|
| 813 |
if ($aUamOptions['lock_recursive'] == 'true') { |
| 814 |
if (isset($oCategory->parent) |
| 815 |
&& !is_null($oCategory->parent) |
| 816 |
) { |
| 817 |
$oParentCategory = $this->_getSingleObject( |
| 818 |
'category', |
| 819 |
$oCategory->parent, |
| 820 |
'full' |
| 821 |
); |
| 822 |
|
| 823 |
if ($oParentCategory !== null) { |
| 824 |
$oCurCategory = get_category($iObjectId); |
| 825 |
$oParentCategory->name = $oCurCategory->name; |
| 826 |
|
| 827 |
if (isset($oParentCategory->recursiveMember)) { |
| 828 |
$aIsRecursiveMember['category'][] |
| 829 |
= $oParentCategory; |
| 830 |
} else { |
| 831 |
$aIsRecursiveMember['category'][] |
| 832 |
= $oParentCategory; |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
return $aIsRecursiveMember; |
| 839 |
} |
| 840 |
|
| 841 |
/** |
| 842 |
* Returns the categories in the group |
| 843 |
* |
| 844 |
* @param array $aCategories The real categories. |
| 845 |
* |
| 846 |
* @return array |
| 847 |
*/ |
| 848 |
public function getFullCategories($aCategories) |
| 849 |
{ |
| 850 |
$aUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 851 |
$aUamOptions = $aUserAccessManager->getAdminOptions(); |
| 852 |
|
| 853 |
foreach ($aCategories as $oCategory) { |
| 854 |
if ($oCategory != null) { |
| 855 |
if ($aUamOptions['lock_recursive'] == 'true') { |
| 856 |
//We have to remove the filter to get all categories |
| 857 |
$blRemoveSuccess = remove_filter( |
| 858 |
'get_terms', |
| 859 |
array( |
| 860 |
$this->getAccessHandler()->getUserAccessManager(), |
| 861 |
'showCategory' |
| 862 |
) |
| 863 |
); |
| 864 |
|
| 865 |
if ($blRemoveSuccess) { |
| 866 |
$aArgs = array( |
| 867 |
'child_of' => $oCategory->id, |
| 868 |
'hide_empty' => false |
| 869 |
); |
| 870 |
|
| 871 |
$aCategoryChildren = get_categories($aArgs); |
| 872 |
|
| 873 |
add_filter( |
| 874 |
'get_terms', |
| 875 |
array($aUserAccessManager, 'showCategory') |
| 876 |
); |
| 877 |
|
| 878 |
|
| 879 |
foreach ($aCategoryChildren as $oCategoryChild) { |
| 880 |
$oCurCategoryChild = new stdClass(); |
| 881 |
$oCurCategoryChild->id = $oCategoryChild->term_id; |
| 882 |
$oCurCategoryChild->name = $oCategoryChild->name; |
| 883 |
|
| 884 |
$oCurCategoryChild->recursiveMember = array('category' => array()); |
| 885 |
$oCurCategoryChild->recursiveMember['category'][] = $oCurCategoryChild; |
| 886 |
$aCategories[$oCurCategoryChild->id] = $oCurCategoryChild; |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
$aCategories[$oCategory->id] = $oCategory; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
return $aCategories; |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
/* |
| 900 |
* Group posts functions. |
| 901 |
*/ |
| 902 |
|
| 903 |
/** |
| 904 |
* Checks if the give post in the give category. |
| 905 |
* |
| 906 |
* @param integer $iPostId The post _iId. |
| 907 |
* @param integer $iCategoryId The category _iId. |
| 908 |
* |
| 909 |
* @return boolean |
| 910 |
*/ |
| 911 |
protected function _isPostInCategory($iPostId, $iCategoryId) |
| 912 |
{ |
| 913 |
/** |
| 914 |
* @var wpdb $wpdb |
| 915 |
*/ |
| 916 |
global $wpdb; |
| 917 |
|
| 918 |
$iCount = (int)$wpdb->get_var( |
| 919 |
"SELECT COUNT(*) |
| 920 |
FROM ".$wpdb->term_relationships." AS tr, |
| 921 |
".$wpdb->term_taxonomy." AS tt |
| 922 |
WHERE tr.object_id = '".$iPostId."' |
| 923 |
AND tt.term_id = '".$iCategoryId."' |
| 924 |
AND tr.term_taxonomy_id = tt.term_taxonomy_id |
| 925 |
AND tt.taxonomy = 'category'" |
| 926 |
); |
| 927 |
|
| 928 |
return ($iCount > 0) ? true : false; |
| 929 |
} |
| 930 |
|
| 931 |
/** |
| 932 |
* Returns the membership of a single post. |
| 933 |
* |
| 934 |
* @param string $sPostType The post type needed for the intern representation. |
| 935 |
* @param integer $sObjectId The object _iId. |
| 936 |
* |
| 937 |
* @return object |
| 938 |
*/ |
| 939 |
protected function _getFullPost($sPostType, $sObjectId) |
| 940 |
{ |
| 941 |
$oPost = get_post($sObjectId); |
| 942 |
|
| 943 |
$aIsRecursiveMember = array(); |
| 944 |
|
| 945 |
$oUserAccessManager = $this->getAccessHandler()->getUserAccessManager(); |
| 946 |
$aUamOptions = $oUserAccessManager->getAdminOptions(); |
| 947 |
|
| 948 |
foreach ($this->getObjectsFromType('category', 'full') as $oCategory) { |
| 949 |
//TODO in_category($oCategory->_iId, $oPost->ID) is very slow check if own function works also |
| 950 |
|
| 951 |
if ($this->_isPostInCategory($oPost->ID, $oCategory->id)) { |
| 952 |
$oCategoryObject = get_category($oCategory->id); |
| 953 |
$oCategory->name = $oCategoryObject->name; |
| 954 |
|
| 955 |
$aIsRecursiveMember['category'][] = $oCategory; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
if (get_option('show_on_front') == 'page' |
| 960 |
&& $oPost->post_parent == 0 |
| 961 |
&& $oPost->post_type == 'post' |
| 962 |
&& get_option('page_for_posts') != $sObjectId |
| 963 |
) { |
| 964 |
$iParentId = get_option('page_for_posts'); |
| 965 |
} else { |
| 966 |
$iParentId = $oPost->post_parent; |
| 967 |
} |
| 968 |
|
| 969 |
if ($aUamOptions['lock_recursive'] == 'true' |
| 970 |
&& $iParentId != 0 |
| 971 |
) { |
| 972 |
$oParent = get_post($iParentId); |
| 973 |
|
| 974 |
$oParentPost = $this->_getSingleObject( |
| 975 |
$oParent->post_type, |
| 976 |
$iParentId, |
| 977 |
'full' |
| 978 |
); |
| 979 |
|
| 980 |
if ($oParentPost !== null) { |
| 981 |
$oPostObject = get_post($oParentPost->id); |
| 982 |
$oParentPost->name = $oPostObject->post_title; |
| 983 |
|
| 984 |
$aIsRecursiveMember[$oParent->post_type][] = $oParentPost; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
return $aIsRecursiveMember; |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
/* |
| 993 |
* Group pluggable _aObjects functions. |
| 994 |
*/ |
| 995 |
|
| 996 |
/** |
| 997 |
* Returns a the recursive membership for a pluggable object. |
| 998 |
* |
| 999 |
* @param string $sObjectType The pluggable object type. |
| 1000 |
* @param integer $iObjectId The object _iId. |
| 1001 |
* |
| 1002 |
* @return array |
| 1003 |
*/ |
| 1004 |
protected function _getFullPlObject($sObjectType, $iObjectId) |
| 1005 |
{ |
| 1006 |
$blIsRecursiveMember = array(); |
| 1007 |
|
| 1008 |
$oPlObject = $this->getAccessHandler()->getPlObject($sObjectType); |
| 1009 |
|
| 1010 |
if (isset($oPlObject['reference']) |
| 1011 |
&& isset($oPlObject['getFull']) |
| 1012 |
) { |
| 1013 |
$aPlRecMember = $oPlObject['reference']->{$oPlObject['getFull']}( |
| 1014 |
$iObjectId, |
| 1015 |
$this |
| 1016 |
); |
| 1017 |
|
| 1018 |
if (is_array($aPlRecMember)) { |
| 1019 |
$blIsRecursiveMember = $aPlRecMember; |
| 1020 |
} |
| 1021 |
} |
| 1022 |
|
| 1023 |
return $blIsRecursiveMember; |
| 1024 |
} |
| 1025 |
} |