-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAccessList.php
249 lines (223 loc) · 6.57 KB
/
AccessList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* AccessList allows to do the following operations:
* - create and read a list of user and groups, that are parmitted to access certain section
*
* PHP version 5
*
* @category Encryption
* @package PageProtectionPlus
* @author Fabian Schmitt <[email protected]>, Pawel Wilk <[email protected]>
* @copyright 2006, 2007 Fabian Schmitt
* @license http://www.gnu.org/licenses/gpl.html General Public License version 2 or higher
* @version 2.3b
* @link http://www.mediawiki.org/wiki/Extension:PPP
*/
/**
* Stores a list of users and groups that are permitted to access a certain
* section.
*/
class AccessList
{
/**
* Private fields.
*/
protected $mUsers = array();
protected $mGroups = array();
/**
* Constructor. Creates default user-list and ensures groups in
* $wgPppDefaultGroups are in allowed groups.
* @param users Array or comma-separated users-list.
* @param groups Array or comma-separated groups-list.
*/
function AccessList($users = null, $groups = null)
{
$this->AddUsers($this->getArray($users));
$this->AddGroups($this->getArray($groups));
}
/**
* Fills an AccessList with default values. This must not be done
* in the Constructor since we need empty AccessLists every now and
* then (e.g. to check who is allowed to edit pages).
*
*/
function AccessListDefaults() {
global $wgPppDefaultGroups;
if (is_array($wgPppDefaultGroups)) {
foreach ($wgPppDefaultGroups as $defaultGroup) {
$this->AddGroup($defaultGroup);
}
}
}
/**
* Checks if parameter is an array of comma-separated list
* and returns array containing the items.
* @param $s Array or comma-separated list.
* @return Array.
*/
private function getArray($s)
{
if (!is_array($s))
{
$s = explode(",", $s);
}
return $this->removeEmpty($s);
}
/**
* Returns parameter-string for protect-tag with permitted
* users of this object.
* @return Parameter-string for users.
*/
public function getUsersParam()
{
$this->mUsers = $this->removeEmpty($this->mUsers);
return "users=\"".implode(",", $this->mUsers)."\"";
}
/**
* Returns parameter-string for protect-tag with permitted
* groups of this object.
* @return Parameter-string for groups.
*/
public function getGroupsParam()
{
$this->mGroups = $this->removeEmpty($this->mGroups);
return "groups=\"".implode(",", $this->mGroups)."\"";
}
/**
* Returns a string containing comma-separated list of the
* permitted users with links to their homepages.
* @return Comma-separated list of users.
*/
public function getUserList()
{
$usersString = "";
$users = array();
// create links to user-pages
foreach($this->mUsers as $user) {
$title = Title::makeTitle(NS_USER, $user);
$userPage = "[[".$title->getNsText().":".$user."|".$user."]]";
$users[] = $userPage;
}
return implode(", ", $users);
}
/**
* Returns a string containing comma-separated list of the
* permitted groups of this object.
* @return Comma-separated groups-list.
*/
public function getGroupList() {
return implode(", ", $this->mGroups);
}
/**
* Checks if a given user is in this AccessList.
* @param user User-object to check for.
* @return true, if user is in list of users or in one of the allowed groups,
* false otherwise.
*/
public function hasAccess(&$user) {
require_once("includes/User.php");
foreach($this->mGroups as $group) {
if(in_array($group, $user->mGroups)){
return true;
}
}
if (in_array($user->getName(), $this->mUsers)) {
return true;
}
return false;
}
/**
* Adds a user to the list of permitted users.
* @param user Username to add
*/
public function AddUser($user)
{
if (!in_array($user, $this->mUsers))
{
$this->mUsers[] = $user;
}
}
/**
* Adds a list of users to the list of permitted users.
* @param user Array of Username to add
*/
public function AddUsers($users)
{
foreach($users as $user)
{
$this->AddUser($user);
}
}
/**
* Adds a group to the list of permitted groups.
* @param group Groupname to add
*/
public function AddGroup($group)
{
if (!in_array($group, $this->mGroups))
{
$this->mGroups[] = $group;
}
}
/**
* Adds a list of groups to the list of permitted groups.
* @param group Array of Groupnames to add
*/
public function AddGroups($groups)
{
foreach ($groups as $group)
{
$this->AddGroup($group);
}
}
/**
* Restricts current permissions to the users supplied as
* parameter and the users that are currently allowed.
* @param users Users to intersect current users-list with.
*/
public function RestrictUsers($users)
{
$this->mUsers = $this->intersect($this->mUsers,
$this->getArray($users));
}
/**
* Restricts current permissions to the groups supplied as
* parameter and the groups that are currently allowed.
* @param groups Groups to intersect current groups-list with.
*/
public function RestrictGroups($groups)
{
$this->mGroups = $this->intersect($this->mGroups,
$this->getArray($groups));
}
/**
* Intersects two array and returns the result. If one of the given arrays
* is empty, the other one will be returned.
* @param a1 First array
* @param a2 Second array
* @return Intersection
*/
private function intersect($a1, $a2) {
if (count($a1) == 0) {
return $a2;
}
if (count($a2) == 0) {
return $a1;
}
return array_intersect($a1, $a2);
}
/**
* Removes empty fields from an array.
* @param arr Array.
* @return Array without fields that contain empty strings.
*/
private function removeEmpty($arr) {
foreach ($arr as $i => $a) {
if ($a == "") {
unset($arr[$i]);
}
}
return $arr;
}
}
?>