1 | <?php |
---|
2 | |
---|
3 | /* ----------------------------------------------------------------------------- |
---|
4 | class name: allowed_access, groups, users |
---|
5 | class version: 1.0 |
---|
6 | date: 2007-10-31 |
---|
7 | ------------------------------------------------------------------------------ |
---|
8 | author: grum at grum.dnsalias.com |
---|
9 | << May the Little SpaceFrog be with you >> |
---|
10 | ------------------------------------------------------------------------------ |
---|
11 | |
---|
12 | this classes provides base functions to manage users/groups access |
---|
13 | groups and users classes extends allowed_access classes |
---|
14 | |
---|
15 | - constructor allowed_access($alloweds="") |
---|
16 | - constructor groups($alloweds="") |
---|
17 | - constructor users($alloweds="") |
---|
18 | - (public) function get_list() |
---|
19 | - (public) function set_allowed($id, $allowed) |
---|
20 | - (public) function set_alloweds() |
---|
21 | - (public) function get_alloweds($return_type) |
---|
22 | - (public) function is_allowed($id) |
---|
23 | - (public) function html_view($sep=", ", $empty="") |
---|
24 | - (public) function html_form($basename) |
---|
25 | - (private) function init_list() |
---|
26 | ---------------------------------------------------------------------- */ |
---|
27 | class allowed_access |
---|
28 | { |
---|
29 | var $access_list; |
---|
30 | |
---|
31 | /* |
---|
32 | constructor initialize the groups_list |
---|
33 | */ |
---|
34 | function allowed_access($alloweds = "") |
---|
35 | { |
---|
36 | $this->init_list(); |
---|
37 | $this->set_alloweds($alloweds); |
---|
38 | } |
---|
39 | |
---|
40 | /* |
---|
41 | initialize the groups list |
---|
42 | */ |
---|
43 | function init_list() |
---|
44 | { |
---|
45 | $this->access_list=array(); |
---|
46 | } |
---|
47 | |
---|
48 | /* |
---|
49 | returns list (as an array) |
---|
50 | */ |
---|
51 | function get_list() |
---|
52 | { |
---|
53 | return($this->access_list); |
---|
54 | } |
---|
55 | |
---|
56 | /* |
---|
57 | set element an allowed state |
---|
58 | */ |
---|
59 | function set_allowed($id, $allowed) |
---|
60 | { |
---|
61 | if(isset($this->access_list[$id])) |
---|
62 | { |
---|
63 | $this->access_list[$id]['allowed']=$allowed; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /* |
---|
68 | set a group enabled/disabled state |
---|
69 | */ |
---|
70 | function set_state($id, $enabled) |
---|
71 | { |
---|
72 | if(isset($this->access_list[$id])) |
---|
73 | { |
---|
74 | $this->access_list[$id]['enabled']=$enabled; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | /* |
---|
79 | set alloweds list |
---|
80 | $list is string of id, separated with "/" |
---|
81 | */ |
---|
82 | function set_alloweds($list) |
---|
83 | { |
---|
84 | $alloweds=explode("/", $list); |
---|
85 | $alloweds=array_flip($alloweds); |
---|
86 | foreach($this->access_list as $key => $val) |
---|
87 | { |
---|
88 | if(isset($alloweds[$key])) |
---|
89 | { |
---|
90 | $this->access_list[$key]['allowed']=true; |
---|
91 | } |
---|
92 | else |
---|
93 | { |
---|
94 | $this->access_list[$key]['allowed']=false; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /* |
---|
100 | get alloweds list |
---|
101 | return a string of groups, separated with "/" |
---|
102 | */ |
---|
103 | function get_alloweds($return_type = 'name') |
---|
104 | { |
---|
105 | $returned=""; |
---|
106 | foreach($this->access_list as $key => $val) |
---|
107 | { |
---|
108 | if($val['allowed']) |
---|
109 | { $returned.=$val[$return_type]."/"; } |
---|
110 | } |
---|
111 | return($returned); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /* |
---|
116 | returns true if is allowed |
---|
117 | */ |
---|
118 | function is_allowed($id) |
---|
119 | { |
---|
120 | if(isset($this->access_list[$id])) |
---|
121 | { return($this->access_list[$id]['allowed']); } |
---|
122 | else |
---|
123 | { return(false); } |
---|
124 | } |
---|
125 | |
---|
126 | /* |
---|
127 | returns true if all or one is allowed |
---|
128 | ids is an array |
---|
129 | */ |
---|
130 | function are_allowed($ids, $all=false) |
---|
131 | { |
---|
132 | foreach($ids as $val) |
---|
133 | { |
---|
134 | if($all) |
---|
135 | { |
---|
136 | if(!$this->is_allowed($val)) |
---|
137 | { |
---|
138 | return(false); |
---|
139 | } |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | if($this->is_allowed($val)) |
---|
144 | { |
---|
145 | return(true); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | return(false); |
---|
150 | } |
---|
151 | |
---|
152 | /* |
---|
153 | returns an HTML list with label rather than id |
---|
154 | */ |
---|
155 | function html_view($sep=", ", $empty="") |
---|
156 | { |
---|
157 | $returned=""; |
---|
158 | foreach($this->access_list as $key => $val) |
---|
159 | { |
---|
160 | if($val['allowed']) |
---|
161 | { |
---|
162 | if($returned!="") |
---|
163 | { |
---|
164 | $returned.=$sep; |
---|
165 | } |
---|
166 | $returned.=$val['name']; |
---|
167 | } |
---|
168 | } |
---|
169 | if($returned=="") |
---|
170 | { |
---|
171 | $returned=$empty; |
---|
172 | } |
---|
173 | return($returned); |
---|
174 | } |
---|
175 | /* |
---|
176 | returns a generic HTML form to manage the groups access |
---|
177 | */ |
---|
178 | function html_form($basename) |
---|
179 | { |
---|
180 | /* |
---|
181 | <!-- BEGIN allowed_group_row --> |
---|
182 | <label><input type="checkbox" name="fmypolls_att_allowed_groups_{allowed_group_row.ID}" {allowed_group_row.CHECKED}/> {allowed_group_row.NAME}</label> |
---|
183 | <!-- END allowed_group_row --> |
---|
184 | */ |
---|
185 | $text=''; |
---|
186 | foreach($this->access_list as $key => $val) |
---|
187 | { |
---|
188 | if($val['allowed']) |
---|
189 | { |
---|
190 | $checked=' checked'; |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | $checked=''; |
---|
195 | } |
---|
196 | |
---|
197 | if($val['enabled']) |
---|
198 | { |
---|
199 | $enabled=''; |
---|
200 | } |
---|
201 | else |
---|
202 | { |
---|
203 | $enabled=' disabled'; |
---|
204 | } |
---|
205 | |
---|
206 | $text.='<label><input type="checkbox" name="'.$basename.$val['id'].'" '.$checked.$enabled.'/> |
---|
207 | '.$val['name'].'</label> '; |
---|
208 | } |
---|
209 | return($text); |
---|
210 | } |
---|
211 | } //allowed_access |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | /* ---------------------------------------------------------------------- |
---|
221 | this class provides base functions to manage groups access |
---|
222 | init_list redefined to initialize access_list from database GROUPS |
---|
223 | ---------------------------------------------------------------------- */ |
---|
224 | class groups extends allowed_access |
---|
225 | { |
---|
226 | /* |
---|
227 | initialize the groups list |
---|
228 | */ |
---|
229 | function init_list() |
---|
230 | { |
---|
231 | $this->access_list=array(); |
---|
232 | $sql="SELECT id, name FROM ".GROUPS_TABLE." ORDER BY name"; |
---|
233 | $result=pwg_query($sql); |
---|
234 | if($result) |
---|
235 | { |
---|
236 | while($row=mysql_fetch_assoc($result)) |
---|
237 | { |
---|
238 | $this->access_list[$row['id']] = |
---|
239 | array('id' => $row['id'], |
---|
240 | 'name' => $row['name'], |
---|
241 | 'allowed' => false, |
---|
242 | 'enabled' => true); |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | /* ----------------------------------------------------------------------------- |
---|
256 | this class provides base functions to manage users access |
---|
257 | ----------------------------------------------------------------------------- */ |
---|
258 | class users extends allowed_access |
---|
259 | { |
---|
260 | /* |
---|
261 | constructor |
---|
262 | */ |
---|
263 | function users($alloweds = "") |
---|
264 | { |
---|
265 | parent::allowed_access($alloweds); |
---|
266 | $this->set_state('admin', false); |
---|
267 | $this->set_allowed('admin', true); |
---|
268 | } |
---|
269 | |
---|
270 | /* |
---|
271 | initialize the groups list |
---|
272 | */ |
---|
273 | function init_list() |
---|
274 | { |
---|
275 | $users_list = array('guest', 'generic', 'normal', 'admin'); |
---|
276 | $this->access_list=array(); |
---|
277 | foreach($users_list as $val) |
---|
278 | { |
---|
279 | $this->access_list[$val] = |
---|
280 | array('id' => $val, |
---|
281 | 'name' => l10n('user_status_'.$val), |
---|
282 | 'allowed' => false, |
---|
283 | 'enabled' => true); |
---|
284 | } |
---|
285 | } |
---|
286 | } //class users |
---|
287 | |
---|
288 | |
---|
289 | |
---|
290 | ?> |
---|