PluginProbe
User Access Manager / 1.0.2
User Access Manager v1.0.2
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.class.php

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

530 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $postUserGroups = array();
32 protected $categoryUserGroups = array();
33 protected $userUserGroups = array();
34 protected $postAccess = array();
35 protected $categroyAccess = array();
36 protected $userGroups = array(
37 'filtered' => array(),
38 'noneFiltered' => array(),
39 );
40 protected $pluggableObjects = array();
41
42 /**
43 * The consturctor
44 *
45 * @param object &$userAccessManager The user access manager object.
46 *
47 * @return null
48 */
49 function __construct(&$userAccessManager)
50 {
51 $this->userAccessManager = $userAccessManager;
52 }
53
54 /**
55 * Returns the user access manager object.
56 *
57 * @return object
58 */
59 function &getUserAccessManager()
60 {
61 return $this->userAccessManager;
62 }
63
64 /**
65 * Filter the user groups of an object if authors_can_add_posts_to_groups
66 * option is enabled
67 *
68 * @param array $userGroups The user groups.
69 *
70 * @return array
71 */
72 private function _filterUserGroups($userGroups)
73 {
74 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
75
76 if ($uamOptions['authors_can_add_posts_to_groups'] == 'true'
77 && !$this->checkUserAccess()
78 && $this->getUserAccessManager()->atAdminPanel
79 ) {
80 global $current_user;
81
82 $userGroupsForUser
83 = $this->getUserGroupsForUser($current_user->ID);
84
85 foreach ($userGroups as $key => $uamUserGroup) {
86 if (!array_key_exists($uamUserGroup->getId(), $userGroupsForUser)) {
87 unset($userGroups[$key]);
88 }
89 }
90 }
91
92 return $userGroups;
93 }
94
95 /**
96 * Returns all user groups or one requested by the user group id.
97 *
98 * @param integer $userGroupId The id of the single user group
99 * which should be returned.
100 * @param boolean $filter Filter the groups.
101 *
102 * @return array|object
103 */
104 function getUserGroups($userGroupId = null, $filter = true)
105 {
106 if ($filter) {
107 $filterAttr = 'filtered';
108 } else {
109 $filterAttr = 'noneFiltered';
110 }
111
112 if ($userGroupId == null
113 && $this->userGroups[$filterAttr] != array()
114 ) {
115 return $this->userGroups[$filterAttr];
116 } elseif ($userGroupId != null
117 && $this->userGroups[$filterAttr] != array()
118 ) {
119 if (isset($this->userGroups[$filterAttr][$userGroupId])) {
120 return $this->userGroups[$filterAttr][$userGroupId];
121 } else {
122 return null;
123 }
124 }
125
126 $this->userGroups[$filterAttr] = array();
127
128 global $wpdb;
129
130 $userGroupsDb = $wpdb->get_results(
131 "SELECT ID
132 FROM " . DB_ACCESSGROUP . "
133 ORDER BY ID", ARRAY_A
134 );
135
136 if (isset($userGroupsDb)) {
137 foreach ($userGroupsDb as $userGroupDb) {
138 $this->userGroups[$filterAttr][$userGroupDb['ID']]
139 = new UamUserGroup(&$this, $userGroupDb['ID']);
140 }
141 }
142
143 //Filter the user groups
144 if ($filter) {
145 $this->userGroups[$filterAttr]
146 = $this->_filterUserGroups($this->userGroups[$filterAttr]);
147 }
148
149 if ($userGroupId == null) {
150 return $this->userGroups[$filterAttr];
151 } elseif ($userGroupId != null) {
152 if (isset($this->userGroups[$filterAttr][$userGroupId])) {
153 return $this->userGroups[$filterAttr][$userGroupId];
154 } else {
155 return null;
156 }
157 }
158 }
159
160 /**
161 * Adds a user group.
162 *
163 * @param object $userGroup The user group which we want to add.
164 *
165 * @return null
166 */
167 function addUserGroup($userGroup)
168 {
169 $this->getUserGroups();
170 $this->userGroups[$userGroup->getId()] = $userGroup;
171 }
172
173 /**
174 * Deletes a user group.
175 *
176 * @param integer $userGroupId The user group id which we want to delete.
177 *
178 * @return null
179 */
180 function deleteUserGroup($userGroupId)
181 {
182 if ($this->getUserGroups($userGroupId) != null) {
183 $this->getUserGroups($userGroupId)->delete();
184 unset($this->userGroups[$userGroupId]);
185 }
186 }
187
188 /**
189 * Returns the user groups for the given object.
190 *
191 * @param integer $objectId The id of the object.
192 * @param string $type The type for what we want the groups.
193 * @param boolean $filter Filter the groups.
194 *
195 * @return array
196 */
197 private function _getUserGroupsForObject($objectId, $type, $filter = true)
198 {
199 $objectUserGroups = array();
200
201 $userGroups = $this->getUserGroups(null, $filter);
202
203 if (isset($userGroups)) {
204 foreach ($userGroups as $userGroup) {
205 $objectMembership = $userGroup->{$type.'IsMember'}($objectId, true);
206
207 if ($objectMembership !== false) {
208 if (isset($objectMembership['byPost'])
209 || isset($objectMembership['byCategory'])
210 || isset($objectMembership['byRole'])
211 ) {
212 $userGroup->setRecursive[$objectId] = $objectMembership;
213 }
214
215 $objectUserGroups[$userGroup->getId()]
216 = $userGroup;
217 }
218 }
219 }
220
221 //Filter the user groups
222 if ($filter) {
223 $objectUserGroups = $this->_filterUserGroups($objectUserGroups);
224 }
225
226 return $objectUserGroups;
227 }
228
229 /**
230 * Returns the user groups of the given post.
231 *
232 * @param integer $postId The id of the post from which we want the groups.
233 * @param boolean $filter Filter the groups.
234 *
235 * @return array
236 */
237 function getUserGroupsForPost($postId, $filter = true)
238 {
239 if ($filter) {
240 $filterAttr = 'filtered';
241 } else {
242 $filterAttr = 'noneFiltered';
243 }
244
245 if (isset($this->postUserGroups[$filterAttr][$postId])) {
246 return $this->postUserGroups[$filterAttr][$postId];
247 }
248
249 $this->postUserGroups[$filterAttr][$postId]
250 = $this->_getUserGroupsForObject($postId, 'post', $filter);
251
252 return $this->postUserGroups[$filterAttr][$postId];
253 }
254
255 /**
256 * Returns the user groups of the given category.
257 *
258 * @param integer $categoryId The id of the category from which
259 * we want the groups.
260 * @param boolean $filter Filter the groups.
261 *
262 * @return array
263 */
264 function getUserGroupsForCategory($categoryId, $filter = true)
265 {
266 if ($filter) {
267 $filterAttr = 'filtered';
268 } else {
269 $filterAttr = 'noneFiltered';
270 }
271
272 if (isset($this->categoryUserGroups[$filterAttr][$categoryId])) {
273 return $this->categoryUserGroups[$filterAttr][$categoryId];
274 }
275
276 $this->categoryUserGroups[$filterAttr][$categoryId]
277 = $this->_getUserGroupsForObject($categoryId, 'category', $filter);
278
279 return $this->categoryUserGroups[$filterAttr][$categoryId];
280 }
281
282 /**
283 * Returns the user groups of the given user.
284 *
285 * @param integer $userId The id of the user from which we want the groups.
286 *
287 * @return array
288 */
289 function getUserGroupsForUser($userId)
290 {
291 if (isset($this->userUserGroups[$userId])) {
292 return $this->userUserGroups[$userId];
293 }
294
295 $this->userUserGroups[$userId]
296 = $this->_getUserGroupsForObject($userId, 'user', false);
297
298 return $this->userUserGroups[$userId];
299 }
300
301 /**
302 * Checks if the current_user has access to the given post.
303 *
304 * @param integer $objectId The id of the object.
305 * @param array $membership The group membership for the object.
306 * @param string $type The object type which should be checked.
307 *
308 * @return boolean
309 */
310 private function _checkAccess($objectId, $membership, $type = null)
311 {
312 global $current_user;
313
314 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
315
316 if ($type == 'post') {
317 $post = get_post($objectId);
318 $authorId = $post->post_author;
319 } else {
320 $authorId = -1;
321 }
322
323 if ($membership == array()
324 || $this->checkUserAccess()
325 || $current_user->ID == $authorId
326 && $uamOptions['authors_has_access_to_own'] == 'true'
327 ) {
328 return true;
329 }
330
331 $curIp = explode(".", $_SERVER['REMOTE_ADDR']);
332
333 foreach ($membership as $key => $userGroup) {
334 if ($this->checkUserIp($curIp, $userGroup->getIpRange())
335 || $userGroup->userIsMember($current_user->ID)
336 ) {
337 return true;
338 }
339
340 if ($this->getUserAccessManager()->atAdminPanel
341 && $userGroup->getWriteAccess() == 'all'
342 || !$this->getUserAccessManager()->atAdminPanel
343 && $userGroup->getReadAccess() == 'all'
344 ) {
345 unset($membership[$key]);
346 }
347 }
348
349 if ($membership == array()) {
350 return true;
351 }
352
353 return false;
354 }
355
356 /**
357 * Checks if the current_user has access to the given post.
358 *
359 * @param integer $postId The id of the post which we want to check.
360 *
361 * @return boolean
362 */
363 function checkPostAccess($postId)
364 {
365 if (isset($this->postAccess[$postId])) {
366 return $this->postAccess[$postId];
367 }
368
369 $postMembership = $this->getUserGroupsForPost($postId, false);
370
371 $this->postAccess[$postId]
372 = $this->_checkAccess($postId, $postMembership, 'post');
373
374 return $this->postAccess[$postId];
375 }
376
377 /**
378 * Checks if the current_user has access to the given category.
379 *
380 * @param integer $categoryId The id of the category which we want to check.
381 *
382 * @return boolean
383 */
384 function checkCategoryAccess($categoryId)
385 {
386 if (isset($this->categroyAccess[$categoryId])) {
387 return $this->categroyAccess[$categoryId];
388 }
389
390 $categoryMembership = $this->getUserGroupsForCategory($categoryId, false);
391
392 $this->categroyAccess[$categoryId]
393 = $this->_checkAccess($categoryId, $categoryMembership);
394
395 return $this->categroyAccess[$categoryId];
396 }
397
398 /**
399 * Checks if the given ip matches with the range.
400 *
401 * @param string $curIp The ip of the current user.
402 * @param array $ipRanges The ip ranges.
403 *
404 * @return boolean
405 */
406 function checkUserIp($curIp, $ipRanges)
407 {
408 if (isset($ipRanges)) {
409 foreach ($ipRanges as $ipRange) {
410 $ipRange = explode("-", $ipRange);
411 $rangeBegin = explode(".", $ipRange[0]);
412
413 if (isset($ipRange[1])) {
414 $rangeEnd = explode(".", $ipRange[1]);
415 } else {
416 $rangeEnd = explode(".", $ipRange[0]);
417 }
418
419 if ($rangeBegin[0] <= $curIp[0]
420 && $curIp[0] <= $rangeEnd[0]
421 && $rangeBegin[1] <= $curIp[1]
422 && $curIp[1] <= $rangeEnd[1]
423 && $rangeBegin[2] <= $curIp[2]
424 && $curIp[2] <= $rangeEnd[2]
425 && $rangeBegin[3] <= $curIp[3]
426 && $curIp[3] <= $rangeEnd[3]
427 ) {
428 return true;
429 }
430 }
431 }
432
433 return false;
434 }
435
436 /**
437 * Checks the user access by user level.
438 *
439 * @return boolean
440 */
441 function checkUserAccess()
442 {
443 global $current_user, $wpdb;
444
445 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
446 $curUserdata = get_userdata($current_user->ID);
447
448 if (!isset($curUserdata->user_level)) {
449 $curUserdata->user_level = null;
450 }
451
452 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
453 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
454 } else {
455 $capabilities = null;
456 }
457
458 $role = is_array($capabilities) ?
459 array_keys($capabilities) : array('norole');
460
461 $role = trim($role[0]);
462
463 $orderedRoles = $this->getRolesOrdered();
464
465 if ($orderedRoles[$role] >= $orderedRoles[$uamOptions['full_access_role']]
466 || $role == 'administrator'
467 ) {
468 return true;
469 }
470
471 return false;
472 }
473
474 /**
475 * Returns the roles as assoziative array.
476 *
477 * @return array
478 */
479 function getRolesOrdered()
480 {
481 $orderedRoles = array(
482 'norole' => 0,
483 'subscriber' => 1,
484 'contributor' => 2,
485 'author' => 3,
486 'editor' => 4,
487 'administrator' => 5
488 );
489
490 return $orderedRoles;
491 }
492
493 /**
494 * Registers object that should be handelt by the user access manager.
495 *
496 * @param array $object The object which you want to register.
497 *
498 * @return null
499 */
500 function registerPluggableObject($object)
501 {
502 $this->pluggableObjects[$object['name']] = $object;
503 }
504
505 /**
506 * Returns a registerd pluggable object.
507 *
508 * @param string $object The name of the object which should be returned.
509 *
510 * @return array
511 */
512 function getPluggableObject($object)
513 {
514 if (isset($this->pluggableObjects[$object])) {
515 return $this->pluggableObjects[$object];
516 }
517
518 return array();
519 }
520
521 /**
522 * Returns all registerd pluggable objects.
523 *
524 * @return array
525 */
526 function getPluggableObjects()
527 {
528 return $this->pluggableObjects;
529 }
530 }