Changed single-quotes to double quotes in string containing $group
[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 $id = $u->idForName();
119 if($id == 0) {
120 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
121 return;
122 }
123 $u->setID( $id );
124
125 $groups = $u->getGroups();
126 $logcomment = ' ';
127 // remove then add groups
128 if(isset($removegroup)) {
129 $groups = array_diff($groups, $removegroup);
130 $logcomment .= implode( ' -', $removegroup);
131 }
132 if(isset($addgroup)) {
133 $groups = array_merge($groups, $addgroup);
134 $logcomment .= implode( ' +', $addgroup );
135 }
136 // save groups in user object and database
137 $u->setGroups($groups);
138 $u->saveSettings();
139
140 $log = new LogPage( 'rights' );
141 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), $logcomment );
142 }
143
144 /**
145 * The entry form
146 * It allow a user to select or eventually add a group as well as looking up
147 * for a username.
148 */
149 function switchForm() {
150 global $wgOut;
151
152 // group selection
153 $wgOut->addHTML( "<form name=\"ulgroup\" action=\"$this->action\" method=\"post\">\n" );
154 $wgOut->addHTML( $this->fieldset( 'lookup-group',
155 $this->HTMLSelectGroups('group-edit', array(0 => $this->mRequest->getVal($this->mName.'-group-edit')) ) .
156 ' <input type="submit" name="seditgroup" value="'.wfMsg('editgroup').'" />' .
157 '<br /><input type="submit" name="saddgroup" value="'.wfMsg('addgroup').'" />'
158 ));
159 $wgOut->addHTML( "</form>\n" );
160
161 // user selection
162 $wgOut->addHTML( "<form name=\"uluser\" action=\"$this->action\" method=\"post\">\n" );
163 $wgOut->addHTML( $this->fieldset( 'lookup-user',
164 $this->textbox( 'user-editname' ) .
165 '<input type="submit" name="ssearchuser" value="'.wfMsg('editusergroup').'" />'
166 ));
167 $wgOut->addHTML( "</form>\n" );
168 }
169
170 /**
171 * Edit a group properties and rights.
172 * @param string $groupname Name of a group to be edited.
173 */
174 function editGroupForm($groupID = 0) {
175 global $wgOut;
176
177 if($this->mRequest->getVal('seditgroup')) {
178 // fetch data if we edit a group
179 $g = Group::newFromID($groupID);
180 $fieldname = 'editgroup';
181 } else {
182 // default datas when we add a group
183 $g = new group();
184 $fieldname = 'addgroup';
185 }
186
187 $gName = $g->getName();
188 $gDescription = $g->getDescription();
189
190
191 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
192 '<input type="hidden" name="editgroup-oldname" value="'.$gName.'" />');
193 $wgOut->addHTML( $this->fieldset( $fieldname,
194 $this->textbox( 'editgroup-name', $gName ) .
195 $this->textareabox( 'editgroup-description', $gDescription ) .
196 '<br /><table border="0" align="center"><tr><td>'.
197 $this->HTMLSelectRights($g->getRights()).
198 '</td></tr></table>'."\n".
199 '<input type="submit" name="savegroup" value="'.wfMsg('savegroup').'" />'
200 ));
201
202 $wgOut->addHTML( "</form>\n" );
203 }
204
205 /**
206 * Edit user groups membership
207 * @param string $username Name of the user.
208 */
209 function editUserGroupsForm($username) {
210 global $wgOut;
211
212 $user = User::newFromName($username);
213 if(is_null($user)) {
214 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
215 return;
216 }
217 $id = $user->idForName();
218 if($id == 0) {
219 $wgOut->addHTML('<p>'.wfMsg('nosuchusershort',$username).'</p>');
220 return;
221 }
222 $user->setID( $id );
223
224 $groups = $user->getGroups();
225
226 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
227 '<input type="hidden" name="user-editname" value="'.$username.'" />');
228 $wgOut->addHTML( $this->fieldset( 'editusergroup',
229 wfMsg('editing', $this->mRequest->getVal('user-editname')).".<br />\n" .
230 '<table border="0" align="center"><tr><td>'.
231 $this->HTMLSelectGroups('groupsmember', $groups,true,6).
232 '</td><td>'.
233 $this->HTMLSelectGroups('groupsavailable', $groups,true,6,true).
234 '</td></tr></table>'."\n".
235 '<p>'.wfMsg('userlevels-groupshelp').'</p>'."\n".
236 '<input type="submit" name="saveusergroups" value="'.wfMsg('saveusergroups').'" />'
237 ));
238 $wgOut->addHTML( "</form>\n" );
239 }
240
241
242 /** Build a select with all existent groups
243 * @param string $selectname Name of this element. Name of form is automaticly prefixed.
244 * @param array $selected Array of element selected when posted. Multiples will only show them.
245 * @param boolean $multiple A multiple elements select.
246 * @param integer $size Number of element to be shown ignored for non multiple (default 6).
247 * @param boolean $reverse If true, multiple select will hide selected elements (default false).
248 */
249 function HTMLSelectGroups($selectname, $selected=array(), $multiple=false, $size=6, $reverse=false) {
250 $selectname = $this->mName.'-'.$selectname;
251 $dbr =& wfGetDB( DB_SLAVE );
252 $group = $dbr->tableName( 'group' );
253 $sql = "SELECT group_id, group_name FROM $group";
254 $res = $dbr->query($sql,'wfSpecialAdmin');
255
256 $out = wfMsg($selectname);
257 $out .= '<select name="'.$selectname;
258 if($multiple) { $out.='[]" multiple="multiple" size="'.$size; }
259 $out.= "\">\n";
260
261 while($g = $dbr->fetchObject( $res ) ) {
262 if($multiple) {
263 // for multiple will only show the things we want
264 if(in_array($g->group_id, $selected) xor $reverse) {
265 $out .= '<option value="'.$g->group_id.'">'.$g->group_name."</option>\n";
266 }
267 } else {
268 $out .= '<option ';
269 if(in_array($g->group_id, $selected)) { $out .= 'selected="selected" '; }
270 $out .= 'value="'.$g->group_id.'">'.$g->group_name."</option>\n";
271 }
272 }
273 $out .= "</select>\n";
274 return $out;
275 }
276
277 function HTMLSelectRights($selected='') {
278 global $wgAvailableRights;
279 $out = '<select name="editgroup-getrights[]" multiple="multiple">';
280 $groupRights = explode(',',$selected);
281
282 foreach($wgAvailableRights as $right) {
283
284 // check box when right exist
285 if(in_array($right, $groupRights)) { $selected = 'selected="selected" '; }
286 else { $selected = ''; }
287
288 $out .= '<option value="'.$right.'" '.$selected.'>'.$right."</option>\n";
289 }
290 $out .= "</select>\n";
291 return $out;
292 }
293 }
294 ?>