1 | <?php |
---|
2 | |
---|
3 | /* ----------------------------------------------------------------------------- |
---|
4 | class name: GPCAllowedAccess, GPCGroups, GPCUsers |
---|
5 | class version : 2.1.0 |
---|
6 | plugin version : 3.4.0 |
---|
7 | date : 2010-03-30 |
---|
8 | ------------------------------------------------------------------------------ |
---|
9 | author: grum at piwog.org |
---|
10 | << May the Little SpaceFrog be with you >> |
---|
11 | ------------------------------------------------------------------------------ |
---|
12 | |
---|
13 | this classes provides base functions to manage users/groups access |
---|
14 | groups and users classes extends GPCAllowedAccess classes |
---|
15 | |
---|
16 | - constructor GPCAllowedAccess($alloweds = array(), $accessMode='a') |
---|
17 | - constructor groups($alloweds = array(), $accessMode='a') |
---|
18 | - constructor users($alloweds = array(), $accessMode='a') |
---|
19 | - (public) function getList() |
---|
20 | - (public) function setAllowed($id, $allowed) |
---|
21 | - (public) function setAlloweds($idList, $allowed) |
---|
22 | - (public) function getAlloweds() |
---|
23 | - (public) function isAllowed($id) |
---|
24 | - (private) function initList() |
---|
25 | |
---|
26 | |
---|
27 | | release | date | |
---|
28 | | 1.1.0 | 2009/11/29 | * add 'webmaster' status for users |
---|
29 | | | | |
---|
30 | | 2.0.0 | 2010/03/28 | * Uses piwigo pwg_db_* functions instead of mysql_* |
---|
31 | | | | functions |
---|
32 | | | | * update classes & functions names |
---|
33 | | | | |
---|
34 | | 2.1.0 | 2011/01/15 | * remove html function |
---|
35 | | | | |
---|
36 | | | | * implement accessMode |
---|
37 | | | | |
---|
38 | | | | |
---|
39 | |
---|
40 | ---------------------------------------------------------------------- */ |
---|
41 | class GPCAllowedAccess |
---|
42 | { |
---|
43 | protected $accessList; |
---|
44 | protected $accessMode='a'; // 'a' : allowed, 'n' : not allowed |
---|
45 | |
---|
46 | /** |
---|
47 | * constructor initialize default values |
---|
48 | * |
---|
49 | * @param Array $alloweds : list of items |
---|
50 | * String $alloweds : list of items (separator : '/') |
---|
51 | * |
---|
52 | * @param String $accessMode : 'a' = access is allowed by default for all values, $allowed param is a list of not allowed values |
---|
53 | * 'n' = access is not allowed by default for all values, $allowed param is a list of allowed values |
---|
54 | * priority is given to the $allowed value |
---|
55 | */ |
---|
56 | public function __construct($alloweds = array(), $accessMode='a') |
---|
57 | { |
---|
58 | $this->initList(); |
---|
59 | $this->setAlloweds($alloweds, $accessMode=='n'); |
---|
60 | } |
---|
61 | |
---|
62 | public function __destruct() |
---|
63 | { |
---|
64 | unset($this->accessList); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * destroy the groups list |
---|
69 | */ |
---|
70 | protected function initList() |
---|
71 | { |
---|
72 | $this->accessList=array(); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * returns list of items (as an array) |
---|
77 | * each array item is an array : |
---|
78 | * 'id' : (String) id of item |
---|
79 | * 'name' : (String) name of item |
---|
80 | * 'allowed' : (Bool) access is allowed or not |
---|
81 | * |
---|
82 | * @return Array |
---|
83 | */ |
---|
84 | function getList() |
---|
85 | { |
---|
86 | return($this->accessList); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * set allowed value for an item |
---|
91 | * |
---|
92 | * @param String $id : id of item |
---|
93 | * @param Bool $allowed : access allowed or not |
---|
94 | */ |
---|
95 | function setAllowed($id, $allowed) |
---|
96 | { |
---|
97 | if(isset($this->accessList[$id])) |
---|
98 | { |
---|
99 | $this->accessList[$id]['allowed']=$allowed; |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * set alloweds items (can be given as an array or a string with separator '/') |
---|
106 | * according to the |
---|
107 | * |
---|
108 | * @param Array $idList : list of items to set |
---|
109 | * @param String $idList |
---|
110 | * @param Bool $allowed : access allowed or not |
---|
111 | */ |
---|
112 | function setAlloweds($idList, $allowed) |
---|
113 | { |
---|
114 | if(!is_array($idList)) $idList=explode("/", $idList); |
---|
115 | |
---|
116 | $idList=array_flip($idList); |
---|
117 | |
---|
118 | foreach($this->accessList as $key => $val) |
---|
119 | { |
---|
120 | if(isset($idList[$key])) |
---|
121 | { |
---|
122 | $this->accessList[$key]['allowed']=$allowed; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | $this->accessList[$key]['allowed']=!$allowed; |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | /** |
---|
133 | * return list of alloweds items |
---|
134 | * |
---|
135 | * @return Array |
---|
136 | */ |
---|
137 | function getAlloweds() |
---|
138 | { |
---|
139 | $returned=Array(); |
---|
140 | |
---|
141 | foreach($this->accessList as $key => $val) |
---|
142 | { |
---|
143 | if($val['allowed']) $returned[]=$val; |
---|
144 | } |
---|
145 | return($returned); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | /** |
---|
150 | * returns true if is allowed |
---|
151 | * |
---|
152 | * @param String $id : item id |
---|
153 | * @retrun Bool |
---|
154 | */ |
---|
155 | function isAllowed($id) |
---|
156 | { |
---|
157 | if(isset($this->accessList[$id])) |
---|
158 | { |
---|
159 | return($this->accessList[$id]['allowed']); |
---|
160 | } |
---|
161 | else |
---|
162 | { |
---|
163 | return($this->accessMode=='a'); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | } //GPCAllowedAccess |
---|
168 | |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | /** |
---|
177 | * ---------------------------------------------------------------------------- |
---|
178 | * this class provides base functions to manage groups access |
---|
179 | * initList redefined to initialize accessList from database GROUPS |
---|
180 | * ---------------------------------------------------------------------------- |
---|
181 | */ |
---|
182 | class GPCGroups extends GPCAllowedAccess |
---|
183 | { |
---|
184 | /** |
---|
185 | * initialize the groups list |
---|
186 | */ |
---|
187 | protected function initList() |
---|
188 | { |
---|
189 | $this->accessList=array(); |
---|
190 | $sql="SELECT id, name FROM ".GROUPS_TABLE." ORDER BY name"; |
---|
191 | $result=pwg_query($sql); |
---|
192 | if($result) |
---|
193 | { |
---|
194 | while($row=pwg_db_fetch_assoc($result)) |
---|
195 | { |
---|
196 | $this->accessList[$row['id']]=array( |
---|
197 | 'id' => $row['id'], |
---|
198 | 'name' => $row['name'], |
---|
199 | 'allowed' => ($this->accessMode=='a') |
---|
200 | ); |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | /** |
---|
214 | * ---------------------------------------------------------------------------- |
---|
215 | * this class provides base functions to manage users access |
---|
216 | * initList redefined to initialize accessList from piwigo's predefined values |
---|
217 | * ---------------------------------------------------------------------------- |
---|
218 | */ |
---|
219 | class GPCUsers extends GPCAllowedAccess |
---|
220 | { |
---|
221 | /** |
---|
222 | * initialize the users list |
---|
223 | */ |
---|
224 | protected function initList() |
---|
225 | { |
---|
226 | $usersList = array('guest', 'generic', 'normal', 'webmaster', 'admin'); |
---|
227 | $this->accessList=array(); |
---|
228 | foreach($usersList as $val) |
---|
229 | { |
---|
230 | $this->accessList[$val]=array( |
---|
231 | 'id' => $val, |
---|
232 | 'name' => l10n('user_status_'.$val), |
---|
233 | 'allowed' => ($this->accessMode=='a') |
---|
234 | ); |
---|
235 | } |
---|
236 | } |
---|
237 | } //class users |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | ?> |
---|