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

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