new feature: $wgBlockAllowsUTEdit
[lhc/web/wiklou.git] / includes / SpecialGroups.php
1 <?php
2 /**
3 * Provide an administration interface
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /** */
9 require_once('HTMLForm.php');
10 require_once('Group.php');
11
12 /** Entry point */
13 function wfSpecialGroups() {
14 global $wgRequest;
15
16 $form = new GroupsForm($wgRequest);
17 $form->execute();
18 }
19
20 /**
21 * A class to manage group levels rights.
22 * @package MediaWiki
23 * @subpackage SpecialPage
24 */
25 class GroupsForm extends HTMLForm {
26 var $mPosted, $mRequest, $mSaveprefs, $mChangeAllowed;
27 var $mNewName, $mDescription, $mOldName, $mRights, $mId;
28 var $mAdd, $mEdit;
29
30 /** Escaped local url name*/
31 var $action, $location;
32
33 /** Constructor*/
34 function GroupsForm ( &$request ) {
35 global $wgUser;
36
37 $this->mPosted = $request->wasPosted();
38 $this->mRequest =& $request;
39 $this->mName = 'groups';
40
41 $this->mNewName = trim( $request->getText('editgroup-name') );
42 $this->mOldName = trim( $request->getText('editgroup-oldname' ) );
43 $this->mDescription = trim( $request->getText( 'editgroup-description' ) );
44 $this->mRights = $request->getArray( 'editgroup-getrights' );
45 $this->mId = $this->mRequest->getInt('id');
46 $this->mEdit = $request->getCheck('edit');
47 $this->mAdd = $request->getCheck('add');
48
49
50 $titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
51 $this->action = $titleObj->escapeLocalURL();
52 if ( $this->mAdd ) {
53 $this->location = $titleObj->getLocalURL( "add=1&id={$this->mId}" );
54 } elseif ( $this->mEdit ) {
55 $this->location = $titleObj->getLocalURL( "edit=1&id={$this->mId}" );
56 } else {
57 $this->location = $this->action;
58 }
59
60 $this->mChangeAllowed = $wgUser->isAllowed( 'grouprights' ) && !Group::getStaticGroups();
61 }
62
63 /**
64 * Manage forms to be shown according to posted data
65 * Depending on the submit button used, call a form or a saving function.
66 */
67 function execute() {
68 global $wgOut;
69
70 if ( $this->mRequest->getBool( 'showrecord' ) ) {
71 $this->showRecord();
72 } elseif ( $this->mPosted && $this->mChangeAllowed && $this->mRequest->getCheck('savegroup') ) {
73 // save settings
74 $this->saveGroup();
75 } elseif ( $this->mEdit ) {
76 if ( $this->mPosted ) {
77 $wgOut->redirect( $this->location );
78 } else {
79 $this->switchForm();
80 $this->editGroupForm( $this->mId );
81 }
82 } elseif ( $this->mAdd ) {
83 if ( $this->mPosted ) {
84 $wgOut->redirect( $this->location );
85 } else {
86 $this->switchForm();
87 $this->editGroupForm( );
88 }
89 } else {
90 $this->showAllGroups();
91 if ( $this->mChangeAllowed ) {
92 $this->switchForm();
93 }
94 }
95 }
96
97 /**
98 * Save a group
99 */
100 function saveGroup() {
101 global $wgOut;
102
103 $this->mNewName = trim($this->mNewName);
104
105 if ( $this->mNewName == '' ) {
106 $this->editGroupForm( $this->mGroupID, 'groups-noname' );
107 return false;
108 }
109
110 if($this->mOldName == '') {
111 // Check if the group already exists
112 $add = true;
113 $g = Group::newFromName( $this->mNewName );
114 if ( $g ) {
115 $this->editGroupForm( 0, 'groups-already-exists' );
116 return;
117 }
118
119 // Create a new group
120 $g = new Group();
121 $g->addToDatabase();
122 } else {
123 $add = false;
124 $g = Group::newFromName($this->mOldName);
125 if ( !$g ) {
126 $this->editGroupForm( 0, 'groups-noname' );
127 return;
128 }
129 }
130
131 // save stuff
132 $g->setName($this->mNewName);
133 $g->setDescription($this->mDescription);
134 if( is_array( $this->mRights ) ) {
135 $g->setRights( implode(',',$this->mRights) );
136 }
137
138 $g->save();
139
140 // Make the log entry
141 $log = new LogPage( 'rights' );
142 $dummyTitle = Title::makeTitle( 0, '' );
143 if ( $add ) {
144 $log->addEntry( 'addgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
145 } else {
146 if ( $this->mOldName != $this->mNewName ) {
147 // Abbreviated action name, must be less than 10 bytes
148 $log->addEntry( 'rngroup', $dummyTitle, '', array( Group::getMessageForContent( $this->mOldName ),
149 $g->getNameForContent() ) );
150 } else {
151 $log->addEntry( 'chgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
152 }
153 }
154
155 // Success, go back to all groups page
156 $titleObj = Title::makeTitle( NS_SPECIAL, 'Groups' );
157 $url = $titleObj->getLocalURL();
158
159 $wgOut->redirect( $url );
160 }
161
162 /**
163 * The entry form
164 * It allows a user to edit or eventually add a group
165 */
166 function switchForm() {
167 global $wgOut;
168
169 // group selection
170 $wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">\n" );
171 $wgOut->addHTML( $this->fieldset( 'lookup-group',
172 HTMLSelectGroups('id', $this->mName.'-group-edit', array(0 => $this->mRequest->getVal('id')) ) .
173 ' <input type="submit" name="edit" value="'.wfMsg('editgroup').'" />' .
174 '<br /><input type="submit" name="add" value="'.wfMsg('addgroup').'" />'
175 ));
176 $wgOut->addHTML( "</form>\n" );
177 }
178
179 /**
180 * Edit a group properties and rights.
181 * @param string $groupname Name of a group to be edited.
182 * @param string $error message name of the error to display
183 */
184 function editGroupForm($groupID = 0, $error = '') {
185 global $wgOut;
186
187 if ( $error ) {
188 $errText = wfMsg( $error );
189 $wgOut->addHTML( "<p class='error'>$errText</p>" );
190 }
191
192 if($this->mRequest->getVal('edit')) {
193 // fetch data if we edit a group
194 $g = Group::newFromID($groupID);
195 $fieldname = 'editgroup';
196 } else {
197 // default data when we add a group
198 $g = new Group();
199 $fieldname = 'addgroup';
200 }
201
202 $gName = htmlspecialchars( $g->getName() );
203 $gDescription = htmlspecialchars( $g->getDescription() );
204
205
206 $wgOut->addHTML( "<form name=\"editGroup\" action=\"{$this->action}\" method=\"post\">\n".
207 '<input type="hidden" name="editgroup-oldname" value="'.$gName."\" />\n" );
208
209 $wgOut->addHTML( $this->fieldset( $fieldname,
210 '<p>' . wfMsg( 'groups-editgroup-preamble' ) . "</p>\n" .
211 $this->textbox( 'editgroup-name', $gName ) .
212 $this->textareabox( 'editgroup-description', $gDescription ) .
213 '<br /><table border="0" align="center"><tr><td>'.
214 HTMLSelectRights($g->getRights()).
215 '</td></tr></table>'."\n".
216 '<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'" />'
217 ));
218
219 $wgOut->addHTML( "</form>\n" );
220 }
221
222 function showAllGroups() {
223 global $wgOut;
224 $groups =& Group::getAllGroups();
225
226 $groupsExisting = wfMsg( 'groups-existing' );
227 $groupsHeader = wfMsg( 'groups-tableheader' );
228
229 $s = "{| border=1
230 |+'''$groupsExisting'''
231 |-
232 !$groupsHeader
233 ";
234 foreach ( $groups as $group ) {
235 $s .= "|-\n| " . $group->getId() . ' || ' .
236 $group->getExpandedName() . ' || ' .
237 $group->getExpandedDescription() . ' || '.
238 // Insert spaces to make it wrap
239 str_replace( ',', ', ', $group->getRights() ) . "\n";
240 }
241 $s .= "|}\n";
242 $wgOut->addWikiText( $s );
243 }
244
245 function showRecord() {
246 global $wgOut;
247
248 $groups =& Group::getAllGroups();
249 $rec = serialize( $groups );
250 // Split it into lines
251 $rec = explode( "\r\n", chunk_split( $rec ) );
252 $s = '';
253 foreach ( $rec as $index => $line ) {
254 if ( trim( $line ) != '' ) {
255 if ( $s ) {
256 $s .= "' .\n\t'";
257 }
258 // Escape it for PHP
259 $line = str_replace( array( '\\', "'" ), array( '\\\\', "\\'" ), $line );
260 // Escape it for HTML
261 $line = htmlspecialchars( $line );
262 // Add it to the string
263 $s .= $line;
264 }
265 }
266 $s .= "';";
267 $s = "<p>Copy the following into LocalSettings.php:</p>\n" .
268 "<textarea readonly rows=20>\n" .
269 "\$wgStaticGroups = \n\t'$s\n" .
270 "</textarea>";
271 $wgOut->addHTML( $s );
272 }
273
274 } // end class GroupsForm
275 ?>