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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
<?php
/**
* Provide an administration interface
* @package MediaWiki
* @subpackage SpecialPage
*/
/** */
require_once('HTMLForm.php');
require_once('Group.php');
/** Entry point */
function wfSpecialGroups() {
global $wgRequest;
$form = new GroupsForm($wgRequest);
$form->execute();
}
/**
* A class to manage group levels rights.
* @package MediaWiki
* @subpackage SpecialPage
*/
class GroupsForm extends HTMLForm {
var $mPosted, $mRequest, $mSaveprefs, $mChangeAllowed;
var $mNewName, $mDescription, $mOldName, $mRights, $mId;
var $mAdd, $mEdit;
/** Escaped local url name*/
var $action, $location;
/** Constructor*/
function GroupsForm ( &$request ) {
global $wgUser;
$this->mPosted = $request->wasPosted();
$this->mRequest =& $request;
$this->mName = 'groups';
$this->mNewName = trim( $request->getText('editgroup-name') );
$this->mOldName = trim( $request->getText('editgroup-oldname' ) );
$this->mDescription = trim( $request->getText( 'editgroup-description' ) );
$this->mRights = $request->getArray( 'editgroup-getrights' );
$this->mId = $this->mRequest->getInt('id');
$this->mEdit = $request->getCheck('edit');
$this->mAdd = $request->getCheck('add');
$titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
$this->action = $titleObj->escapeLocalURL();
if ( $this->mAdd ) {
$this->location = $titleObj->getLocalURL( "add=1&id={$this->mId}" );
} elseif ( $this->mEdit ) {
$this->location = $titleObj->getLocalURL( "edit=1&id={$this->mId}" );
} else {
$this->location = $this->action;
}
$this->mChangeAllowed = $wgUser->isAllowed( 'grouprights' ) && !Group::getStaticGroups();
}
/**
* Manage forms to be shown according to posted data
* Depending on the submit button used, call a form or a saving function.
*/
function execute() {
global $wgOut;
if ( $this->mRequest->getBool( 'showrecord' ) ) {
$this->showRecord();
} elseif ( $this->mPosted && $this->mChangeAllowed && $this->mRequest->getCheck('savegroup') ) {
// save settings
$this->saveGroup();
} elseif ( $this->mEdit ) {
if ( $this->mPosted ) {
$wgOut->redirect( $this->location );
} else {
$this->switchForm();
$this->editGroupForm( $this->mId );
}
} elseif ( $this->mAdd ) {
if ( $this->mPosted ) {
$wgOut->redirect( $this->location );
} else {
$this->switchForm();
$this->editGroupForm( );
}
} else {
$this->showAllGroups();
if ( $this->mChangeAllowed ) {
$this->switchForm();
}
}
}
/**
* Save a group
*/
function saveGroup() {
global $wgOut;
$this->mNewName = trim($this->mNewName);
if ( $this->mNewName == '' ) {
$this->editGroupForm( $this->mGroupID, 'groups-noname' );
return false;
}
if($this->mOldName == '') {
// Check if the group already exists
$add = true;
$g = Group::newFromName( $this->mNewName );
if ( $g ) {
$this->editGroupForm( 0, 'groups-already-exists' );
return;
}
// Create a new group
$g = new Group();
$g->addToDatabase();
} else {
$add = false;
$g = Group::newFromName($this->mOldName);
if ( !$g ) {
$this->editGroupForm( 0, 'groups-noname' );
return;
}
}
// save stuff
$g->setName($this->mNewName);
$g->setDescription($this->mDescription);
if( is_array( $this->mRights ) ) {
$g->setRights( implode(',',$this->mRights) );
}
$g->save();
// Make the log entry
$log = new LogPage( 'rights' );
$dummyTitle = Title::makeTitle( 0, '' );
if ( $add ) {
$log->addEntry( 'addgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
} else {
if ( $this->mOldName != $this->mNewName ) {
// Abbreviated action name, must be less than 10 bytes
$log->addEntry( 'rngroup', $dummyTitle, '', array( Group::getMessageForContent( $this->mOldName ),
$g->getNameForContent() ) );
} else {
$log->addEntry( 'chgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
}
}
// Success, go back to all groups page
$titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
$url = $titleObj->getLocalURL();
$wgOut->redirect( $url );
}
/**
* The entry form
* It allows a user to edit or eventually add a group
*/
function switchForm() {
global $wgOut;
// group selection
$wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">\n" );
$wgOut->addHTML( $this->fieldset( 'lookup-group',
HTMLSelectGroups('id', $this->mName.'-group-edit', array(0 => $this->mRequest->getVal('id')) ) .
' <input type="submit" name="edit" value="'.wfMsg('editgroup').'" />' .
'<br /><input type="submit" name="add" value="'.wfMsg('addgroup').'" />'
));
$wgOut->addHTML( "</form>\n" );
}
/**
* Edit a group properties and rights.
* @param string $groupname Name of a group to be edited.
* @param string $error message name of the error to display
*/
function editGroupForm($groupID = 0, $error = '') {
global $wgOut;
if ( $error ) {
$errText = wfMsg( $error );
$wgOut->addHTML( "<p class='error'>$errText</p>" );
}
if($this->mRequest->getVal('edit')) {
// fetch data if we edit a group
$g = Group::newFromID($groupID);
$fieldname = 'editgroup';
} else {
// default data when we add a group
$g = new Group();
$fieldname = 'addgroup';
}
$gName = htmlspecialchars( $g->getName() );
$gDescription = htmlspecialchars( $g->getDescription() );
$wgOut->addHTML( "<form name=\"editGroup\" action=\"{$this->action}\" method=\"post\">\n".
'<input type="hidden" name="editgroup-oldname" value="'.$gName."\" />\n" );
$wgOut->addHTML( $this->fieldset( $fieldname,
'<p>' . wfMsg( 'groups-editgroup-preamble' ) . "</p>\n" .
$this->textbox( 'editgroup-name', $gName ) .
$this->textareabox( 'editgroup-description', $gDescription ) .
'<br /><table border="0" align="center"><tr><td>'.
HTMLSelectRights($g->getRights()).
'</td></tr></table>'."\n".
'<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'" />'
));
$wgOut->addHTML( "</form>\n" );
}
function showAllGroups() {
global $wgOut;
$groups =& Group::getAllGroups();
$groupsExisting = wfMsg( 'groups-existing' );
$groupsHeader = wfMsg( 'groups-tableheader' );
$s = "{| border=1
|+'''$groupsExisting'''
|-
!$groupsHeader
";
foreach ( $groups as $group ) {
$s .= "|-\n| " . $group->getId() . ' || ' .
$group->getExpandedName() . ' || ' .
$group->getExpandedDescription() . ' || '.
// Insert spaces to make it wrap
str_replace( ',', ', ', $group->getRights() ) . "\n";
}
$s .= "|}\n";
$wgOut->addWikiText( $s );
}
function showRecord() {
global $wgOut;
$groups =& Group::getAllGroups();
$rec = serialize( $groups );
// Split it into lines
$rec = explode( "\r\n", chunk_split( $rec ) );
$s = '';
foreach ( $rec as $index => $line ) {
if ( trim( $line ) != '' ) {
if ( $s ) {
$s .= "' .\n\t'";
}
// Escape it for PHP
$line = str_replace( array( '\\', "'" ), array( '\\\\', "\\'" ), $line );
// Escape it for HTML
$line = htmlspecialchars( $line );
// Add it to the string
$s .= $line;
}
}
$s .= "';";
$s = "<p>Copy the following into LocalSettings.php:</p>\n" .
"<textarea readonly rows=20>\n" .
"\$wgStaticGroups = \n\t'$s\n" .
"</textarea>";
$wgOut->addHTML( $s );
}
} // end class GroupsForm
?>
|