Fix 8335 : Image syntax does not parse "px" in image caption correctly
[lhc/web/wiklou.git] / includes / SpecialUserrights.php
1 <?php
2 /**
3 * Provide an administration interface
4 * DO NOT USE: INSECURE.
5 *
6 * TODO : remove everything related to group editing (SpecialGrouplevels.php)
7 * @package MediaWiki
8 * @subpackage SpecialPage
9 */
10
11 /** */
12 require_once('HTMLForm.php');
13
14 /** Entry point */
15 function wfSpecialUserrights() {
16 global $wgRequest;
17 $form = new UserrightsForm($wgRequest);
18 $form->execute();
19 }
20
21 /**
22 * A class to manage user levels rights.
23 * @package MediaWiki
24 * @subpackage SpecialPage
25 */
26 class UserrightsForm extends HTMLForm {
27 var $mPosted, $mRequest, $mSaveprefs;
28 /** Escaped local url name*/
29 var $action;
30
31 /** Constructor*/
32 function UserrightsForm ( &$request ) {
33 $this->mPosted = $request->wasPosted();
34 $this->mRequest =& $request;
35 $this->mName = 'userrights';
36
37 $titleObj = SpecialPage::getTitleFor( 'Userrights' );
38 $this->action = $titleObj->escapeLocalURL();
39 }
40
41 /**
42 * Manage forms to be shown according to posted data.
43 * Depending on the submit button used, call a form or a save function.
44 */
45 function execute() {
46 // show the general form
47 $this->switchForm();
48 if( $this->mPosted ) {
49 // show some more forms
50 if( $this->mRequest->getCheck( 'ssearchuser' ) ) {
51 $this->editUserGroupsForm( $this->mRequest->getVal( 'user-editname' ) );
52 }
53
54 // save settings
55 if( $this->mRequest->getCheck( 'saveusergroups' ) ) {
56 global $wgUser;
57 $username = $this->mRequest->getVal( 'user-editname' );
58 if( $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ), $username ) ) {
59 $this->saveUserGroups( $username,
60 $this->mRequest->getArray( 'member' ),
61 $this->mRequest->getArray( 'available' ) );
62 }
63 }
64 }
65 }
66
67 /**
68 * Save user groups changes in the database.
69 * Data comes from the editUserGroupsForm() form function
70 *
71 * @param string $username Username to apply changes to.
72 * @param array $removegroup id of groups to be removed.
73 * @param array $addgroup id of groups to be added.
74 *
75 */
76 function saveUserGroups( $username, $removegroup, $addgroup) {
77 global $wgOut;
78 $u = User::newFromName($username);
79
80 if(is_null($u)) {
81 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
82 return;
83 }
84
85 if($u->getID() == 0) {
86 $wgOut->addWikiText( wfMsg( 'nosuchusershort', htmlspecialchars( $username ) ) );
87 return;
88 }
89
90 $oldGroups = $u->getGroups();
91 $newGroups = $oldGroups;
92 // remove then add groups
93 if(isset($removegroup)) {
94 $newGroups = array_diff($newGroups, $removegroup);
95 foreach( $removegroup as $group ) {
96 $u->removeGroup( $group );
97 }
98 }
99 if(isset($addgroup)) {
100 $newGroups = array_merge($newGroups, $addgroup);
101 foreach( $addgroup as $group ) {
102 $u->addGroup( $group );
103 }
104 }
105 $newGroups = array_unique( $newGroups );
106
107 wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) );
108 wfDebug( 'newGroups: ' . print_r( $newGroups, true ) );
109
110 wfRunHooks( 'UserRights', array( &$u, $addgroup, $removegroup ) );
111 $log = new LogPage( 'rights' );
112 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $u->getName() ), '', array( $this->makeGroupNameList( $oldGroups ),
113 $this->makeGroupNameList( $newGroups ) ) );
114 }
115
116 function makeGroupNameList( $ids ) {
117 return implode( ', ', $ids );
118 }
119
120 /**
121 * The entry form
122 * It allows a user to look for a username and edit its groups membership
123 */
124 function switchForm() {
125 global $wgOut;
126
127 // user selection
128 $wgOut->addHTML( "<form name=\"uluser\" action=\"$this->action\" method=\"post\">\n" );
129 $wgOut->addHTML( $this->fieldset( 'lookup-user',
130 $this->textbox( 'user-editname' ) .
131 wfElement( 'input', array(
132 'type' => 'submit',
133 'name' => 'ssearchuser',
134 'value' => wfMsg( 'editusergroup' ) ) )
135 ));
136 $wgOut->addHTML( "</form>\n" );
137 }
138
139 /**
140 * Edit user groups membership
141 * @param string $username Name of the user.
142 */
143 function editUserGroupsForm($username) {
144 global $wgOut, $wgUser;
145
146 $user = User::newFromName($username);
147 if( is_null( $user ) ) {
148 $wgOut->addWikiText( wfMsg( 'nouserspecified' ) );
149 return;
150 } elseif( $user->getID() == 0 ) {
151 $wgOut->addWikiText( wfMsg( 'nosuchusershort', wfEscapeWikiText( $username ) ) );
152 return;
153 }
154
155 $groups = $user->getGroups();
156
157 $wgOut->addHTML( "<form name=\"editGroup\" action=\"$this->action\" method=\"post\">\n".
158 wfElement( 'input', array(
159 'type' => 'hidden',
160 'name' => 'user-editname',
161 'value' => $username ) ) .
162 wfElement( 'input', array(
163 'type' => 'hidden',
164 'name' => 'wpEditToken',
165 'value' => $wgUser->editToken( $username ) ) ) .
166 $this->fieldset( 'editusergroup',
167 $wgOut->parse( wfMsg('editinguser', $username ) ) .
168 '<table border="0" align="center"><tr><td>'.
169 HTMLSelectGroups('member', $this->mName.'-groupsmember', $groups,true,6).
170 '</td><td>'.
171 HTMLSelectGroups('available', $this->mName.'-groupsavailable', $groups,true,6,true).
172 '</td></tr></table>'."\n".
173 $wgOut->parse( wfMsg('userrights-groupshelp') ) .
174 wfElement( 'input', array(
175 'type' => 'submit',
176 'name' => 'saveusergroups',
177 'value' => wfMsg( 'saveusergroups' ) ) )
178 ));
179 $wgOut->addHTML( "</form>\n" );
180 }
181 } // end class UserrightsForm
182 ?>