Replaced old setIgnoreSQLErrors by newer ignoreErrors
[lhc/web/wiklou.git] / includes / SpecialMakesysop.php
1 <?php
2 require_once( "LinksUpdate.php" );
3
4 function wfSpecialMakesysop()
5 {
6 global $wgUser, $wgOut, $wgRequest;
7
8 if ( 0 == $wgUser->getID() or $wgUser->isBlocked() ) {
9 $wgOut->errorpage( "movenologin", "movenologintext" );
10 return;
11 }
12 if (! $wgUser->isBureaucrat() && ! $wgUser->isDeveloper() ){
13 $wgOut->errorpage( "bureaucrattitle", "bureaucrattext" );
14 return;
15 }
16
17 if ( wfReadOnly() ) {
18 $wgOut->readOnlyPage();
19 return;
20 }
21
22 $f = new MakesysopForm( $wgRequest );
23
24 if ( $f->mSubmit ) {
25 $f->doSubmit();
26 } else {
27 $f->showForm( "" );
28 }
29 }
30
31 class MakesysopForm {
32 var $mTarget, $mAction, $mRights, $mUser, $mSubmit;
33
34 function MakesysopForm( &$request )
35 {
36 $this->mAction = $request->getText( 'action' );
37 $this->mRights = $request->getVal( 'wpRights' );
38 $this->mUser = $request->getText( 'wpMakesysopUser' );
39 $this->mSubmit = $request->getBool( 'wpMakesysopSubmit' ) && $request->wasPosted();
40 $this->mBuro = $request->getBool( 'wpSetBureaucrat' );
41 }
42
43 function showForm( $err = "")
44 {
45 global $wgOut, $wgUser, $wgLang;
46
47 if ( $wgUser->isDeveloper() ) {
48 $wgOut->setPageTitle( wfMsg( "set_user_rights" ) );
49 } else {
50 $wgOut->setPagetitle( wfMsg( "makesysoptitle" ) );
51 }
52
53 $wgOut->addWikiText( wfMsg( "makesysoptext" ) );
54
55 $titleObj = Title::makeTitle( NS_SPECIAL, "Makesysop" );
56 $action = $titleObj->escapeLocalURL( "action=submit" );
57
58 if ( "" != $err ) {
59 $wgOut->setSubtitle( wfMsg( "formerror" ) );
60 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
61 }
62 $namedesc = wfMsg( "makesysopname" );
63 if ( !is_null( $this->mUser ) ) {
64 $encUser = htmlspecialchars( $this->mUser );
65 } else {
66 $encUser = "";
67 }
68
69 $wgOut->addHTML( "
70 <form id=\"makesysop\" method=\"post\" action=\"{$action}\">
71 <table border='0'>
72 <tr>
73 <td align='right'>$namedesc</td>
74 <td align='left'>
75 <input type='text' size='40' name=\"wpMakesysopUser\" value=\"$encUser\" />
76 </td>
77 </tr>"
78 );
79
80 $makeburo = wfMsg( "setbureaucratflag" );
81 $wgOut->addHTML(
82 "<tr>
83 <td>&nbsp;</td><td align=left>
84 <input type=checkbox name=\"wpSetBureaucrat\" value=1>$makeburo
85 </td>
86 </tr>"
87 );
88
89 if ( $wgUser->isDeveloper() ) {
90 $rights = wfMsg( "rights" );
91 if ( !is_null( $this->mRights ) ) {
92 $encRights = htmlspecialchars( $this->mRights );
93 } else {
94 $encRights = "sysop";
95 }
96
97 $wgOut->addHTML( "
98 <tr>
99 <td align='right'>$rights</td>
100 <td align='left'>
101 <input type='text' size='40' name=\"wpRights\" value=\"$encRights\" />
102 </td>
103 </tr>"
104 );
105 }
106
107 if ( $wgUser->isDeveloper() ) {
108 $mss = wfMsg( "set_user_rights" );
109 } else {
110 $mss = wfMsg( "makesysopsubmit" );
111 }
112 $wgOut->addHTML(
113 "<tr>
114 <td>&nbsp;</td><td align='left'>
115 <input type='submit' name=\"wpMakesysopSubmit\" value=\"{$mss}\" />
116 </td></tr></table>
117 </form>\n"
118 );
119
120 }
121
122 function doSubmit()
123 {
124 global $wgOut, $wgUser, $wgLang;
125 global $wgDBname, $wgMemc, $wgLocalDatabases;
126
127 $dbw =& wfGetDB( DB_MASTER );
128 $parts = explode( "@", $this->mUser );
129 $usertable = $dbw->tableName( 'user' );
130
131 if( count( $parts ) == 2 && $wgUser->isDeveloper() && strpos( '.', $usertable ) === false ){
132 $username = $dbw->strencode( $parts[0] );
133 if ( array_key_exists( $parts[1], $wgLocalDatabases ) ) {
134 $dbName = $wgLocalDatabases[$parts[1]];
135 $usertable = $dbName . "." . $usertable;
136 } else {
137 $this->showFail();
138 return;
139 }
140 } else {
141 $username = wfStrencode( $this->mUser );
142 $dbName = $wgDBname;
143 }
144 if ( $username{0} == "#" ) {
145 $id = intval( substr( $username, 1 ) );
146 $sql = "SELECT user_id,user_rights FROM $usertable WHERE user_id=$id FOR UPDATE";
147 } else {
148 $encName = $dbw->strencode( $username );
149 $sql = "SELECT user_id, user_rights FROM $usertable WHERE user_name = '{$username}' FOR UPDATE";
150 }
151
152 $prev = $dbw->ignoreErrors( TRUE );
153 $res = $dbw->query( $sql );
154 $dbw->ignoreErrors( $prev );
155
156 if( $dbw->lastErrno() || ! $username || $dbw->numRows( $res ) == 0 ){
157 $this->showFail();
158 return;
159 }
160
161 $row = $dbw->fetchObject( $res );
162 $id = intval( $row->user_id );
163 $rightsNotation = array();
164
165 if ( $wgUser->isDeveloper() ) {
166 $newrights = (string)$this->mRights;
167 $rightsNotation[] = "=$this->mRights";
168 } else {
169 if( $row->user_rights ){
170 $rights = explode(",", $row->user_rights );
171 if(! in_array("sysop", $rights ) ){
172 $rights[] = "sysop";
173 $rightsNotation[] = "+sysop ";
174 }
175 if ( $this->mBuro && !in_array( "bureaucrat", $rights ) ) {
176 $rights[] = "bureaucrat";
177 $rightsNotation[] = "+bureaucrat ";
178 }
179 $newrights = addslashes( implode( ",", $rights ) );
180 } else {
181 $newrights = "sysop";
182 $rightsNotation[] = "+sysop";
183 if ( $this->mBuro ) {
184 $rightsNotation[] = "+bureaucrat";
185 $newrights .= ",bureaucrat";
186 }
187 }
188 }
189
190 if ( count( $rightsNotation ) == 0 ) {
191 $this->showFail();
192 } else {
193 $sql = "UPDATE $usertable SET user_rights = '{$newrights}' WHERE user_id = $id LIMIT 1";
194 $dbw->query($sql);
195 $wgMemc->delete( "$dbName:user:id:$id" );
196
197 $log = new LogPage( 'rights' );
198 $log->addEntry( 'rights', Title::makeTitle( NS_USER, $this->mUser ),
199 implode( " ", $rightsNotation ) );
200
201 $this->showSuccess();
202 }
203 }
204
205 function showSuccess()
206 {
207 global $wgOut, $wgUser;
208
209 $wgOut->setPagetitle( wfMsg( "makesysoptitle" ) );
210
211 if ( $wgUser->isDeveloper() ) {
212 $text = wfMsg( "user_rights_set", $this->mUser );
213 } else {
214 $text = wfMsg( "makesysopok", $this->mUser );
215 }
216 $text .= "\n\n";
217 $wgOut->addWikiText( $text );
218 $this->showForm();
219
220 }
221
222 function showFail()
223 {
224 global $wgOut, $wgUser;
225
226 $wgOut->setPagetitle( wfMsg( "makesysoptitle" ) );
227 if ( $wgUser->isDeveloper() ) {
228 $this->showForm( wfMsg( "set_rights_fail", $this->mUser ) );
229 } else {
230 $this->showForm( wfMsg( "makesysopfail", $this->mUser ) );
231 }
232 }
233 }
234 ?>