Fix fucked up unblock links in Special:Ipblocklist
[lhc/web/wiklou.git] / includes / SpecialIpblocklist.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * @todo document
10 */
11 function wfSpecialIpblocklist() {
12 global $wgUser, $wgOut, $wgRequest;
13
14 $ip = $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) );
15 $reason = $wgRequest->getText( 'wpUnblockReason' );
16 $action = $wgRequest->getText( 'action' );
17
18 $ipu = new IPUnblockForm( $ip, $reason );
19
20 if ( "success" == $action ) {
21 $msg = wfMsg( "ipusuccess", htmlspecialchars( $ip ) );
22 $ipu->showList( $msg );
23 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
24 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
25 if ( ! $wgUser->isAllowed('block') ) {
26 $wgOut->sysopRequired();
27 return;
28 }
29 $ipu->doSubmit();
30 } else if ( "unblock" == $action ) {
31 $ipu->showForm( "" );
32 } else {
33 $ipu->showList( "" );
34 }
35 }
36
37 /**
38 *
39 * @package MediaWiki
40 * @subpackage SpecialPage
41 */
42 class IPUnblockForm {
43 var $ip, $reason;
44
45 function IPUnblockForm( $ip, $reason ) {
46 $this->ip = $ip;
47 $this->reason = $reason;
48 }
49
50 function showForm( $err ) {
51 global $wgOut, $wgUser, $wgSysopUserBans;
52
53 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
54 $wgOut->addWikiText( wfMsg( 'unblockiptext' ) );
55
56 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
57 $ipr = wfMsgHtml( 'ipbreason' );
58 $ipus = wfMsgHtml( 'ipusubmit' );
59 $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
60 $action = $titleObj->escapeLocalURL( "action=submit" );
61
62 if ( "" != $err ) {
63 $wgOut->setSubtitle( wfMsg( "formerror" ) );
64 $wgOut->addWikitext( "<span class='error'>{$err}</span>\n" );
65 }
66 $token = htmlspecialchars( $wgUser->editToken() );
67
68 $wgOut->addHTML( "
69 <form id=\"unblockip\" method=\"post\" action=\"{$action}\">
70 <table border='0'>
71 <tr>
72 <td align='right'>{$ipa}:</td>
73 <td align='left'>
74 <input tabindex='1' type='text' size='20' name=\"wpUnblockAddress\" value=\"" . htmlspecialchars( $this->ip ) . "\" />
75 </td>
76 </tr>
77 <tr>
78 <td align='right'>{$ipr}:</td>
79 <td align='left'>
80 <input tabindex='1' type='text' size='40' name=\"wpUnblockReason\" value=\"" . htmlspecialchars( $this->reason ) . "\" />
81 </td>
82 </tr>
83 <tr>
84 <td>&nbsp;</td>
85 <td align='left'>
86 <input tabindex='2' type='submit' name=\"wpBlock\" value=\"{$ipus}\" />
87 </td>
88 </tr>
89 </table>
90 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
91 </form>\n" );
92
93 }
94
95 function doSubmit() {
96 global $wgOut, $wgUser;
97
98 $block = new Block();
99 $this->ip = trim( $this->ip );
100
101 if ( $this->ip{0} == "#" ) {
102 $block->mId = substr( $this->ip, 1 );
103 } else {
104 $block->mAddress = $this->ip;
105 }
106
107 # Delete block (if it exists)
108 # We should probably check for errors rather than just declaring success
109 $block->delete();
110
111 # Make log entry
112 $log = new LogPage( 'block' );
113 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $this->ip ), $this->reason );
114
115 # Report to the user
116 $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
117 $success = $titleObj->getFullURL( "action=success&ip=" . urlencode( $this->ip ) );
118 $wgOut->redirect( $success );
119 }
120
121 function showList( $msg ) {
122 global $wgOut;
123
124 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
125 if ( "" != $msg ) {
126 $wgOut->setSubtitle( $msg );
127 }
128 global $wgRequest;
129 list( $this->limit, $this->offset ) = $wgRequest->getLimitOffset();
130 $this->counter = 0;
131
132 $paging = '<p>' . wfViewPrevNext( $this->offset, $this->limit,
133 Title::makeTitle( NS_SPECIAL, 'Ipblocklist' ),
134 'ip=' . urlencode( $this->ip ) ) . "</p>\n";
135 $wgOut->addHTML( $paging );
136
137 $search = $this->searchForm();
138 $wgOut->addHTML( $search );
139
140 $wgOut->addHTML( "<ul>" );
141 if( !Block::enumBlocks( array( &$this, "addRow" ), 0 ) ) {
142 // FIXME hack to solve #bug 1487
143 $wgOut->addHTML( '<li>'.wfMsgHtml( 'ipblocklistempty' ).'</li>' );
144 }
145 $wgOut->addHTML( "</ul>\n" );
146 $wgOut->addHTML( $paging );
147 }
148
149 function searchForm() {
150 global $wgTitle;
151 return
152 wfElement( 'form', array(
153 'action' => $wgTitle->getLocalUrl() ),
154 null ) .
155 wfElement( 'input', array(
156 'type' => 'hidden',
157 'name' => 'action',
158 'value' => 'search' ) ).
159 wfElement( 'input', array(
160 'type' => 'hidden',
161 'name' => 'limit',
162 'value' => $this->limit ) ).
163 wfElement( 'input', array(
164 'name' => 'ip',
165 'value' => $this->ip ) ) .
166 wfElement( 'input', array(
167 'type' => 'submit',
168 'value' => wfMsg( 'search' ) ) ) .
169 '</form>';
170 }
171
172 /**
173 * Callback function to output a block
174 */
175 function addRow( $block, $tag ) {
176 global $wgOut, $wgUser, $wgLang;
177
178 if( $this->ip != '' ) {
179 if( $block->mAuto ) {
180 if( stristr( $block->mId, $this->ip ) == false ) {
181 return;
182 }
183 } else {
184 if( stristr( $block->mAddress, $this->ip ) == false ) {
185 return;
186 }
187 }
188 }
189
190 // Loading blocks is fast; displaying them is slow.
191 // Quick hack for paging.
192 $this->counter++;
193 if( $this->counter <= $this->offset ) {
194 return;
195 }
196 if( $this->counter - $this->offset > $this->limit ) {
197 return;
198 }
199
200 $fname = 'IPUnblockForm-addRow';
201 wfProfileIn( $fname );
202
203 static $sk=null, $msg=null;
204
205 if( is_null( $sk ) )
206 $sk = $wgUser->getSkin();
207 if( is_null( $msg ) ) {
208 $msg = array();
209 foreach( array( 'infiniteblock', 'expiringblock', 'contribslink', 'unblocklink' ) as $key ) {
210 $msg[$key] = wfMsgHtml( $key );
211 }
212 $msg['blocklistline'] = wfMsg( 'blocklistline' );
213 $msg['contribslink'] = wfMsg( 'contribslink' );
214 }
215
216
217 # Prepare links to the blocker's user and talk pages
218 $blocker_name = $block->getByName();
219 $blocker = $sk->MakeKnownLinkObj( Title::makeTitle( NS_USER, $blocker_name ), $blocker_name );
220 $blocker .= ' (' . $sk->makeKnownLinkObj( Title::makeTitle( NS_USER_TALK, $blocker_name ), $wgLang->getNsText( NS_TALK ) ) . ')';
221
222 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
223 if( $block->mAuto ) {
224 $target = '#' . $block->mID; # Hide the IP addresses of auto-blocks; privacy
225 } else {
226 $target = $sk->makeKnownLinkObj( Title::makeTitle( NS_USER, $block->mAddress ), $block->mAddress );
227 $target .= ' (' . $sk->makeKnownLinkObj( Title::makeTitle( NS_SPECIAL, 'Contributions' ), $msg['contribslink'], 'target=' . $block->mAddress ) . ')';
228 }
229
230 # Prep the address for the unblock link, masking autoblocks as before
231 $addr = $block->mAuto ? '#' . $block->mID : $block->mAddress;
232
233 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
234
235 if ( $block->mExpiry === "" ) {
236 $formattedExpiry = $msg['infiniteblock'];
237 } else {
238 $formattedExpiry = wfMsgReplaceArgs( $msg['expiringblock'],
239 array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
240 }
241
242 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $formattedExpiry ) );
243
244 $wgOut->addHTML( "<li>{$line}" );
245
246 if ( $wgUser->isAllowed('block') ) {
247 $titleObj = Title::makeTitle( NS_SPECIAL, "Ipblocklist" );
248 $wgOut->addHTML( ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&ip=' . urlencode( $addr ) ) . ')' );
249 }
250 $wgOut->addHTML( $sk->commentBlock( $block->mReason ) );
251 $wgOut->addHTML( "</li>\n" );
252 wfProfileOut( $fname );
253 }
254 }
255
256 ?>