Working on new access system...
[estigi.git] / core / user / user.module
blobf71df62d37af40da92e45fff42ffd44244c79c42
1 <?php
3 /**
4  * \defgroup User User module
5  */
6 /*@{*/
9 /**
10  * @file user.module
11  * Main user module file.
12  * Core Module. Deals with user integration.
13  *
14  */
16 global $user;
18 /**
19  * Verifies user access to certain utilities
20  *
21  * @param $access
22  *  The access permition to be looked for
23  * @bug
24  *  The data casting (array)$access is not working, no idea why
25  * @bug
26  *  The query to select perms will return a count($p) of one when there are no perms set for the group. Could be a bug in the db_query function actually
27  * @test
28  *  Working with regular accounts, not only the root admin and an entirely new system
29  *
30  */
31 function user_access($access, $owner){
33         global $user;
35         //TRUE or FALSE access with an array
36         if(is_array($access)){
38                 //$access = array($access);
40                 for($i = 0; $i < count($access); $i++){
41                         //If we find any permission from the array, then it is a valid permission
42                         if(core_variable_get($access, FALSE, $owner, 'perms')){
43                                 return TRUE;
44                         }
45                 }
46                 return FALSE;
47         }
49         //Give main account an all access pass and Dungeon Master group if settings apply
50         if($user['uid'] == 1 || (core_variable_get('dungeon_full_access', FALSE, 'user') && $user['uid'] == 2)){
51                 return TRUE;
52         }
54         return core_variable_get($access, FALSE, $owner, 'perms');
58 /**
59  * Loads a users details
60  *
61  * @param $uid
62  *  User id
63  * @param $logged
64  *  No idea, I guess it won't be used
65  * @todo
66  *  Remove the $logged param use in calling functions
67  */
68 function user_load($uid){
70         global $db;
72         //Security for main admin and anonymous
73         if($uid < 1){
74                 return FALSE;
75         }
77         $user = db_query("SELECT * FROM {PRE_}users WHERE uid = ".$uid." AND uid > 0", TRUE, TRUE);
79         if($user){
80                 unset($user['pass']);
81         }
83         $roles = db_query("SELECT ur. * , r. * FROM {PRE_}users_roles ur INNER JOIN role r WHERE ur.rid = r.rid AND ur.uid = ".$uid."", TRUE);
85         if(count($roles) >1){
86                 foreach($roles as $r){
87                         $user['roles'][$r['rid']] = $r['name'];
88                 }
89         }
90         //Add the role registered because anyone with an account is a registred user
91         //if($logged){
92                 $user['roles'][3] = user_roles(3);
93         //}
95         //Unserialize data
96         $user['data'] = unserialize($user['data']);
98         return $user;
103  * Manage user sessions
105  * @param $key
106  *  The session key
108  */
109 function user_sessions($key){
111         global $user;
113         $u = db_query("SELECT uid FROM {PRE_}sessions WHERE sid = '".$key."'", TRUE, TRUE);
115         if($u != ''){
116                 if($u['uid'] == 0){
117                         user_anonymous();
118                         $user['session'] = '';
119                         return TRUE;
120                 }
121                 $user = user_load($u['uid'], TRUE);
122                 $user['session'] = $u;
123                 return TRUE;
124         }
125                 else{
126                 return FALSE;
127                 }
132  * Sets details for anonymous users browsing the website
134  * @param $session
135  *  Session information
137  */
138 function user_anonymous($session = ''){
140         global $user;
142         $user['uid'] = 0;
143         $user['hostname'] = core_ip_address();
144         $user['roles'] = array(2 => user_roles(2));
145         $user['session'] = $session;
150  * Retrieves a list of users roles and their public names
152  * @param $rid
153  *  Specific role id if you just want one
155  */
156 function user_roles($rid = NULL){
158         static $roles;
160         if(!isset($roles)){
161                 $role = db_query("SELECT * FROM {PRE_}role", TRUE);
162         
163                 foreach($role as $r){
164                         $roles[$r['rid']] = $r['name'];
165                 }
166         }
168         if(isset($rid)){
169                 return $roles[$rid];
170         }
172         return $roles;
177  * Add tabs to the user pages pages
178  * @param $uid
179  *  User id
181  */
182 function user_tabs($uid){
184         $skin_tabs = array('user/'.$uid => 'View',
185                                                          'user/edit/'.$uid => 'Edit'
186                                                 );
188         skin_tabs($skin_tabs);
193  * Displays a user profile
194  * 
195  * @param $uid
196  *  User id
198  */
199 function user_display($uid = NULL){
201         global $user, $user_profile;
203         $uid = (!$uid ? $user['uid'] : $uid);
205         $user_profile = user_load($uid);
207         user_tabs($uid);
209         //Roles to which s/he belongs to
210         $user_profile['roles'] = 'roles-' . implode(' roles-', $user_profile['roles']);
211         
212         hooks_invoke('user_display');
214         return skin_dressme_tpl('user', 'user_VAR_', $user_profile, 'tmp');
218 /*@}*/