3 * Provide an administration interface
4 * DO NOT USE: INSECURE.
6 * @subpackage SpecialPage
10 require_once('HTMLForm.php');
11 require_once('Group.php');
14 function wfSpecialGroups($par=null) {
18 $form = new GroupsForm($wgRequest);
23 * A class to manage group levels rights.
25 * @subpackage SpecialPage
27 class GroupsForm
extends HTMLForm
{
28 var $mPosted, $mRequest, $mSaveprefs, $mChangeAllowed;
29 var $mNewName, $mDescription, $mOldName, $mRights, $mId;
32 /** Escaped local url name*/
33 var $action, $location;
36 function GroupsForm ( &$request ) {
39 $this->mPosted
= $request->wasPosted();
40 $this->mRequest
= $request;
41 $this->mName
= 'groups';
43 $this->mNewName
= trim( $request->getText('editgroup-name') );
44 $this->mOldName
= trim( $request->getText('editgroup-oldname' ) );
45 $this->mDescription
= trim( $request->getText( 'editgroup-description' ) );
46 $this->mRights
= $request->getArray( 'editgroup-getrights' );
47 $this->mId
= $this->mRequest
->getInt('id');
48 $this->mEdit
= $request->getCheck('edit');
49 $this->mAdd
= $request->getCheck('add');
52 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Groups' );
53 $this->action
= $titleObj->escapeLocalURL();
55 $this->location
= $titleObj->getLocalURL( "add=1&id={$this->mId}" );
56 } elseif ( $this->mEdit
) {
57 $this->location
= $titleObj->getLocalURL( "edit=1&id={$this->mId}" );
59 $this->location
= $this->action
;
62 $this->mChangeAllowed
= $wgUser->isAllowed( 'grouprights' ) && !Group
::getStaticGroups();
66 * Manage forms to be shown according to posted datas.
67 * Depending on the submit button used : Call a form or a saving function.
72 if ( $this->mRequest
->getBool( 'showrecord' ) ) {
74 } elseif ( $this->mPosted
&& $this->mChangeAllowed
&& $this->mRequest
->getCheck('savegroup') ) {
77 } elseif ( $this->mEdit
) {
78 if ( $this->mPosted
) {
79 $wgOut->redirect( $this->location
);
82 $this->editGroupForm( $this->mId
);
84 } elseif ( $this->mAdd
) {
85 if ( $this->mPosted
) {
86 $wgOut->redirect( $this->location
);
89 $this->editGroupForm( );
92 $this->showAllGroups();
93 if ( $this->mChangeAllowed
) {
101 * @todo FIXME : Log is incorrect.
103 function saveGroup() {
106 $this->mNewName
= trim($this->mNewName
);
108 if ( $this->mNewName
== '' ) {
109 $this->editGroupForm( $this->mGroupID
, 'groups-noname' );
113 if($this->mOldName
== '') {
114 // Check if the group already exists
116 $g = Group
::newFromName( $this->mNewName
);
118 $this->editGroupForm( 0, 'groups-already-exists' );
122 // Create a new group
127 $g = Group
::newFromName($this->mOldName
);
129 $this->editGroupForm( 0, 'groups-noname' );
135 $g->setName($this->mNewName
);
136 $g->setDescription($this->mDescription
);
137 if( is_array( $this->mRights
) ) {
138 $g->setRights( implode(',',$this->mRights
) );
143 // Make the log entry
144 $log = new LogPage( 'rights' );
145 $dummyTitle = Title
::makeTitle( 0, '' );
147 $log->addEntry( 'addgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
149 if ( $this->mOldName
!= $this->mNewName
) {
150 // Abbreviated action name, must be less than 10 bytes
151 $log->addEntry( 'rngroup', $dummyTitle, '', array( Group
::getMessageForContent( $this->mOldName
),
152 $g->getNameForContent() ) );
154 $log->addEntry( 'chgroup', $dummyTitle, '', array( $g->getNameForContent() ) );
158 // Success, go back to all groups page
159 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Groups' );
160 $url = $titleObj->getLocalURL();
162 $wgOut->redirect( $url );
167 * It allows a user to edit or eventually add a group
169 function switchForm() {
173 $wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">\n" );
174 $wgOut->addHTML( $this->fieldset( 'lookup-group',
175 HTMLSelectGroups('id', $this->mName
.'-group-edit', array(0 => $this->mRequest
->getVal('id')) ) .
176 ' <input type="submit" name="edit" value="'.wfMsg('editgroup').'" />' .
177 '<br /><input type="submit" name="add" value="'.wfMsg('addgroup').'" />'
179 $wgOut->addHTML( "</form>\n" );
183 * Edit a group properties and rights.
184 * @param string $groupname Name of a group to be edited.
185 * @param string $error message name of the error to display
187 function editGroupForm($groupID = 0, $error = '') {
191 $errText = wfMsg( $error );
192 $wgOut->addHTML( "<p class='error'>$errText</p>" );
195 if($this->mRequest
->getVal('edit')) {
196 // fetch data if we edit a group
197 $g = Group
::newFromID($groupID);
198 $fieldname = 'editgroup';
200 // default datas when we add a group
202 $fieldname = 'addgroup';
205 $gName = $g->getName();
206 $gDescription = $g->getDescription();
209 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
210 '<input type="hidden" name="editgroup-oldname" value="'.$gName."\" />\n" );
212 $wgOut->addHTML( $this->fieldset( $fieldname,
213 '<p>' . wfMsg( 'groups-editgroup-preamble' ) . "</p>\n" .
214 $this->textbox( 'editgroup-name', $gName ) .
215 $this->textareabox( 'editgroup-description', $gDescription ) .
216 '<br /><table border="0" align="center"><tr><td>'.
217 HTMLSelectRights($g->getRights()).
218 '</td></tr></table>'."\n".
219 '<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'" />'
222 $wgOut->addHTML( "</form>\n" );
225 function showAllGroups() {
227 $groups =& Group
::getAllGroups();
229 $groupsExisting = wfMsg( 'groups-existing' );
230 $groupsHeader = wfMsg( 'groups-tableheader' );
233 |+'''$groupsExisting'''
237 foreach ( $groups as $group ) {
238 $s .= "|-\n| " . $group->getId() . ' || ' .
239 $group->getExpandedName() . ' || ' .
240 $group->getExpandedDescription() . ' || '.
241 // Insert spaces to make it wrap
242 str_replace( ',', ', ', $group->getRights() ) . "\n";
245 $wgOut->addWikiText( $s );
248 function showRecord() {
251 $groups =& Group
::getAllGroups();
252 $rec = serialize( $groups );
254 $rec = str_replace( array( '\\', "'" ), array( '\\\\', "\\'" ), $rec );
255 // Escape it for HTML
256 $rec = htmlspecialchars( $rec );
257 $s = "<p>Copy the following into LocalSettings.php:</p>\n" .
258 "<textarea readonly rows=20>\n" .
259 "\$wgStaticGroups = '$rec';\n" .
261 $wgOut->addHTML( $s );
264 } // end class GroupsForm