4f484a974db60e8d49c0506b351b65c913d6aaae
[lhc/web/wiklou.git] / includes / SpecialUserlevels.php
1 <?php
2 /**
3 * Provide an administration interface
4 * DO NOT USE: INSECURE.
5 */
6
7 /** */
8 require_once('HTMLForm.php');
9 require_once('Group.php');
10
11 /** Entry point */
12 function wfSpecialUserlevels($par=null) {
13 global $wgRequest;
14 # Debug statement
15 // print_r($_POST);
16 $form = new UserlevelsForm($wgRequest);
17 $form->execute();
18 }
19
20 /**
21 * A class to manage user levels rights.
22 */
23 class UserlevelsForm extends HTMLForm {
24 var $mPosted, $mRequest, $mSaveprefs;
25 /** Escaped local url name*/
26 var $action;
27
28 /** Constructor*/
29 function UserlevelsForm ( &$request ) {
30 $this->mPosted = $request->wasPosted();
31 $this->mRequest = $request;
32 $this->mName = 'userlevels';
33
34 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlevels' );
35 $this->action = $titleObj->escapeLocalURL();
36 }
37
38 /**
39 * Manage forms to be shown according to posted datas.
40 * Depending on the submit button used : Call a form or a saving function.
41 */
42 function execute() {
43 // show the general form
44 $this->switchForm();
45 if ( $this->mPosted ) {
46 // show some more forms
47 if($this->mRequest->getCheck('seditgroup')) {
48 $this->editGroupForm( $this->mRequest->getVal($this->mName.'-group-edit') ); }
49 if($this->mRequest->getCheck('saddgroup')) {
50 $this->editGroupForm( ); }
51 if($this->mRequest->getCheck('ssearchuser')) {
52 $this->editUserGroupsForm( $this->mRequest->getVal('user-editname')); }
53
54 // save settings
55 if($this->mRequest->getCheck('savegroup')) {
56 $this->saveGroup($this->mRequest->getVal('editgroup-name'),
57 $this->mRequest->getVal('editgroup-oldname'),
58 $this->mRequest->getVal('editgroup-description'),
59 $this->mRequest->getVal('editgroup-getrights'));
60
61 } elseif($this->mRequest->getCheck('saveusergroups')) {
62 $this->saveUserGroups($this->mRequest->getVal('user-editname'),
63 $this->mRequest->getVal($this->mName.'-groupsmember'),
64 $this->mRequest->getVal($this->mName.'-groupsavailable'));
65 }
66 }
67 }
68
69 // save things !!
70 /**
71 * Save a group
72 * @param string $newname Group name.
73 * @param string $oldname Old (current) group name.
74 * @param string $description Group description.
75 *
76 * @todo FIXME : doesnt validate anything. Log is incorrect.
77 */
78 function saveGroup($newname, $oldname, $description, $rights) {
79 $newame = trim($newname);
80
81 if($oldname == '') {
82 // We create a new group
83 $g = new group();
84 $g->addToDatabase();
85 } else {
86 $g = Group::newFromName($oldname);
87 }
88
89 // save stuff
90 $g->setName($newname);
91 $g->setDescription($description);
92 if(isset($rights)) { $g->setRights( implode(',',$rights) ); }
93
94 $g->save();
95
96 $log = new LogPage( 'rights' );
97 $log->addEntry( 'rights', Title::makeTitle( NS_SPECIAL, $g->getName()) , ' '.$g->getRights() );
98
99 }
100
101 /**
102 * Save user groups changes in the database.
103 * Datas comes from the editUserGroupsForm() form function
104 *
105 * @param string $username Username to apply changes to.
106 * @param array $removegroup id of groups to be removed.
107 * @param array $addgroup id of groups to be added.
108 *
109 * @todo Log groupname instead of group id.
110 */
111 function saveUserGroups($username,$removegroup,$addgroup) {
112 $u = User::NewFromName($username);
113
114 if(is_null($u)) {
115 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
116 return;
117 }
118
119 if($u->getID() == 0) {
120 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
121 return;
122 }
123
124 $groups = $u->getGroups();
125 $logcomment = ' ';
126 // remove then add groups
127 if(isset($removegroup)) {
128 $groups = array_diff($groups, $removegroup);
129 $logcomment .= implode( ' -', $removegroup);
130 }
131 if(isset($addgroup)) {
132 $groups = array_merge($groups, $addgroup);
133 $logcomment .= implode( ' +', $addgroup );
134 }
135 // save groups in user object and database
136 $u->setGroups($groups);
137 $u->saveSettings();
138
139 $log = new LogPage( 'rights' );
140 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), $logcomment );
141 }
142
143 /**
144 * The entry form
145 * It allows a user to select or eventually add a group as well as looking up
146 * for a username.
147 */
148 function switchForm() {
149 global $wgOut;
150
151 // group selection
152 $wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">\n" );
153 $wgOut->addHTML( $this->fieldset( 'lookup-group',
154 $this->HTMLSelectGroups('group-edit', array(0 => $this->mRequest->getVal($this->mName.'-group-edit')) ) .
155 ' <input type="submit" name="seditgroup" value="'.wfMsg('editgroup').'" />' .
156 '<br /><input type="submit" name="saddgroup" value="'.wfMsg('addgroup').'" />'
157 ));
158 $wgOut->addHTML( "</form>\n" );
159
160 // user selection
161 $wgOut->addHTML( "<form name=\"uluser\" action=\"$this->action\" method=\"post\">\n" );
162 $wgOut->addHTML( $this->fieldset( 'lookup-user',
163 $this->textbox( 'user-editname' ) .
164 '<input type="submit" name="ssearchuser" value="'.wfMsg('editusergroup').'" />'
165 ));
166 $wgOut->addHTML( "</form>\n" );
167 }
168
169 /**
170 * Edit a group properties and rights.
171 * @param string $groupname Name of a group to be edited.
172 */
173 function editGroupForm($groupID = 0) {
174 global $wgOut;
175
176 if($this->mRequest->getVal('seditgroup')) {
177 // fetch data if we edit a group
178 $g = Group::newFromID($groupID);
179 $fieldname = 'editgroup';
180 } else {
181 // default datas when we add a group
182 $g = new group();
183 $fieldname = 'addgroup';
184 }
185
186 $gName = $g->getName();
187 $gDescription = $g->getDescription();
188
189
190 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
191 '<input type="hidden" name="editgroup-oldname" value="'.$gName.'" />');
192 $wgOut->addHTML( $this->fieldset( $fieldname,
193 $this->textbox( 'editgroup-name', $gName ) .
194 $this->textareabox( 'editgroup-description', $gDescription ) .
195 '<br /><table border="0" align="center"><tr><td>'.
196 $this->HTMLSelectRights($g->getRights()).
197 '</td></tr></table>'."\n".
198 '<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'" />'
199 ));
200
201 $wgOut->addHTML( "</form>\n" );
202 }
203
204 /**
205 * Edit user groups membership
206 * @param string $username Name of the user.
207 */
208 function editUserGroupsForm($username) {
209 global $wgOut;
210
211 $user = User::newFromName($username);
212 if(is_null($user)) {
213 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
214 return;
215 }
216
217 if($user->getID() == 0) {
218 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
219 return;
220 }
221
222 $groups = $user->getGroups();
223
224 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
225 '<input type="hidden" name="user-editname" value="'.$username.'" />');
226 $wgOut->addHTML( $this->fieldset( 'editusergroup',
227 wfMsg('editing', $this->mRequest->getVal('user-editname')).".<br />\n" .
228 '<table border="0" align="center"><tr><td>'.
229 $this->HTMLSelectGroups('groupsmember', $groups,true,6).
230 '</td><td>'.
231 $this->HTMLSelectGroups('groupsavailable', $groups,true,6,true).
232 '</td></tr></table>'."\n".
233 '<p>'.wfMsg('userlevels-groupshelp').'</p>'."\n".
234 '<input type="submit" name="saveusergroups" value="'.wfMsg('saveusergroups').'" />'
235 ));
236 $wgOut->addHTML( "</form>\n" );
237 }
238
239
240 /** Build a select with all existent groups
241 * @param string $selectname Name of this element. Name of form is automaticly prefixed.
242 * @param array $selected Array of element selected when posted. Multiples will only show them.
243 * @param boolean $multiple A multiple elements select.
244 * @param integer $size Number of element to be shown ignored for non multiple (default 6).
245 * @param boolean $reverse If true, multiple select will hide selected elements (default false).
246 */
247 function HTMLSelectGroups($selectname, $selected=array(), $multiple=false, $size=6, $reverse=false) {
248 $selectname = $this->mName.'-'.$selectname;
249 $dbr =& wfGetDB( DB_SLAVE );
250 $group = $dbr->tableName( 'group' );
251 $sql = "SELECT group_id, group_name FROM $group";
252 $res = $dbr->query($sql,'wfSpecialAdmin');
253
254 $out = wfMsg($selectname);
255 $out .= '<select name="'.$selectname;
256 if($multiple) { $out.='[]" multiple="multiple" size="'.$size; }
257 $out.= "\">\n";
258
259 while($g = $dbr->fetchObject( $res ) ) {
260 if($multiple) {
261 // for multiple will only show the things we want
262 if(in_array($g->group_id, $selected) xor $reverse) {
263 $out .= '<option value="'.$g->group_id.'">'.$g->group_name."</option>\n";
264 }
265 } else {
266 $out .= '<option ';
267 if(in_array($g->group_id, $selected)) { $out .= 'selected="selected" '; }
268 $out .= 'value="'.$g->group_id.'">'.$g->group_name."</option>\n";
269 }
270 }
271 $out .= "</select>\n";
272 return $out;
273 }
274
275 function HTMLSelectRights($selected='') {
276 global $wgAvailableRights;
277 $out = '<select name="editgroup-getrights[]" multiple="multiple">';
278 $groupRights = explode(',',$selected);
279
280 foreach($wgAvailableRights as $right) {
281
282 // check box when right exist
283 if(in_array($right, $groupRights)) { $selected = 'selected="selected" '; }
284 else { $selected = ''; }
285
286 $out .= '<option value="'.$right.'" '.$selected.'>'.$right."</option>\n";
287 }
288 $out .= "</select>\n";
289 return $out;
290 }
291 }
292 ?>