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