350eb9237696a2a6ccfb63238b4b0a40207bb61a
[lhc/web/wiklou.git] / includes / SpecialMakesysop.php
1 <?php
2 include_once( "LinksUpdate.php" );
3
4 function wfSpecialMakesysop()
5 {
6 global $wgUser, $wgOut, $action, $target;
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();
23
24 if ( $_POST['wpMakesysopSubmit'] ) {
25 $f->doSubmit();
26 } else {
27 $f->showForm( "" );
28 }
29 }
30
31 class MakesysopForm {
32 function showForm( $err = "")
33 {
34 global $wgOut, $wgUser, $wgLang;
35 global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target, $wpRights, $wpMakesysopUser;
36
37 if ( $wgUser->isDeveloper() ) {
38 $wgOut->setPageTitle( wfMsg( "set_user_rights" ) );
39 } else {
40 $wgOut->setPagetitle( wfMsg( "makesysoptitle" ) );
41 }
42
43 $wgOut->addWikiText( wfMsg( "makesysoptext" ) );
44
45 $titleObj = Title::makeTitle( NS_SPECIAL, "Makesysop" );
46 $action = $titleObj->getURL( "action=submit", true );
47
48 if ( "" != $err ) {
49 $wgOut->setSubtitle( wfMsg( "formerror" ) );
50 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
51 }
52 $namedesc = wfMsg( "makesysopname" );
53 if ( isset( $wpMakesysopUser ) ) {
54 $encUser = htmlspecialchars( $wpMakesysopUser );
55 } else {
56 $encUser = "";
57 }
58
59 $wgOut->addHTML( "<p>
60 <form id=\"makesysop\" method=\"post\" action=\"{$action}\">
61 <table border=0>
62 <tr>
63 <td align=right>$namedesc</td>
64 <td align=left>
65 <input type=text size=40 name=\"wpMakesysopUser\" value=\"$encUser\">
66 </td>
67 </tr>"
68 );
69 /*
70 $makeburo = wfMsg( "setbureaucratflag" );
71 $wgOut->addHTML(
72 "<tr>
73 <td>&nbsp;</td><td align=left>
74 <input type=checkbox name=\"wpSetBureaucrat\" value=1>$makeburo
75 </td>
76 </tr>"
77 );*/
78
79 if ( $wgUser->isDeveloper() ) {
80 $rights = wfMsg( "rights" );
81 if ( isset( $wpRights ) ) {
82 $encRights = htmlspecialchars( $wpRights );
83 } else {
84 $encRights = "sysop";
85 }
86
87 $wgOut->addHTML( "
88 <tr>
89 <td align=right>$rights</td>
90 <td align=left>
91 <input type=text size=40 name=\"wpRights\" value=\"$encRights\">
92 </td>
93 </tr>"
94 );
95 }
96
97 if ( $wgUser->isDeveloper() ) {
98 $mss = wfMsg( "set_user_rights" );
99 } else {
100 $mss = wfMsg( "makesysopsubmit" );
101 }
102 $wgOut->addHTML(
103 "<tr>
104 <td>&nbsp;</td><td align=left>
105 <input type=submit name=\"wpMakesysopSubmit\" value=\"{$mss}\">
106 </td></tr></table>
107 </form>\n"
108 );
109
110 }
111
112 function doSubmit()
113 {
114 global $wgOut, $wgUser, $wgLang, $wpMakesysopUser, $wpSetBureaucrat;
115 global $wgDBname, $wgMemc, $wpRights, $wgLocalDatabases;
116
117 $parts = explode( "@", $wpMakesysopUser );
118 if( count( $parts ) == 2 && $wgUser->isDeveloper() ){
119 $username = wfStrencode( $parts[0] );
120 if ( array_key_exists( $parts[1], $wgLocalDatabases ) ) {
121 $dbName = $wgLocalDatabases[$parts[1]];
122 $usertable = $dbName . ".user";
123 } else {
124 $this->showFail();
125 return;
126 }
127 } else {
128 $username = wfStrencode( $wpMakesysopUser );
129 $usertable = "user";
130 $dbName = $wgDBname;
131 }
132 $prev = wfIgnoreSQLErrors( TRUE );
133 $res = wfQuery("SELECT user_id, user_rights FROM $usertable WHERE user_name = '{$username}'", DB_WRITE);
134 wfIgnoreSQLErrors( $prev );
135
136 if( wfLastErrno() || ! $username || wfNumRows( $res ) == 0 ){
137 $this->showFail();
138 return;
139 }
140
141 $row = wfFetchObject( $res );
142 $id = intval( $row->user_id );
143 $rightsNotation = array();
144
145 if ( $wgUser->isDeveloper() ) {
146 $newrights = (string)$wpRights;
147 $rightsNotation[] = "=$wpRights";
148 } else {
149 if( $row->user_rights ){
150 $rights = explode(",", $row->user_rights );
151 if(! in_array("sysop", $rights ) ){
152 $rights[] = "sysop";
153 $rightsNotation[] = "+sysop ";
154 }
155 if ( $wpSetBureaucrat && !in_array( "bureaucrat", $rights ) ) {
156 $rights[] = "bureaucrat";
157 $rightsNotation[] = "+bureaucrat ";
158 }
159 $newrights = addslashes( implode( ",", $rights ) );
160 } else {
161 $newrights = "sysop";
162 $rightsNotation[] = "+sysop";
163 if ( $wpSetBureaucrat ) {
164 $rightsNotation[] = "+bureaucrat";
165 $newrights .= ",bureaucrat";
166 }
167 }
168 }
169
170 if ( count( $rightsNotation ) == 0 ) {
171 $this->showFail();
172 } else {
173 $sql = "UPDATE $usertable SET user_rights = '{$newrights}' WHERE user_id = $id LIMIT 1";
174 wfQuery($sql, DB_WRITE);
175 $wgMemc->delete( "$dbName:user:id:$id" );
176
177 $bureaucratLog = wfMsg( "bureaucratlog" );
178 $action = wfMsg( "bureaucratlogentry", $wpMakesysopUser, implode( " ", $rightsNotation ) );
179
180 $log = new LogPage( $bureaucratLog );
181 $log->addEntry( $action, "" );
182
183 $this->showSuccess();
184 }
185 }
186
187 function showSuccess()
188 {
189 global $wgOut, $wpMakesysopUser, $wgUser;
190
191 $wgOut->setPagetitle( wfMsg( "makesysoptitle" ) );
192
193 if ( $wgUser->isDeveloper() ) {
194 $text = wfMsg( "user_rights_set", $wpMakesysopUser );
195 } else {
196 $text = wfMsg( "makesysopok", $wpMakesysopUser );
197 }
198 $text .= "\n\n";
199 $wgOut->addWikiText( $text );
200 $this->showForm();
201
202 }
203
204 function showFail()
205 {
206 global $wgOut, $wpMakesysopUser, $wgUser;
207
208 $wgOut->setPagetitle( wfMsg( "makesysoptitle" ) );
209 if ( $wgUser->isDeveloper() ) {
210 $this->showForm( wfMsg( "set_rights_fail", $wpMakesysopUser ) );
211 } else {
212 $this->showForm( wfMsg( "makesysopfail", $wpMakesysopUser ) );
213 }
214 }
215 }
216 ?>