* Removed the need to supply a reason for a block.
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
index f03e535..d5e58c6 100644 (file)
-<?
-
-function wfSpecialBlockip()
-{
-       global $wgUser, $wgOut, $action;
-
-       if ( ! $wgUser->isSysop() ) {
+<?php
+/**
+ * Constructor for Special:Blockip page
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
+
+/**
+ * Constructor
+ */
+function wfSpecialBlockip() {
+       global $wgUser, $wgOut, $wgRequest;
+
+       if ( ! $wgUser->isAllowed('block') ) {
                $wgOut->sysopRequired();
                return;
        }
-       $fields = array( "wpBlockAddress", "wpBlockReason" );
-       wfCleanFormFields( $fields );
        $ipb = new IPBlockForm();
 
-       if ( "success" == $action ) { $ipb->showSuccess(); }
-       else if ( "submit" == $action ) { $ipb->doSubmit(); }
-       else { $ipb->showForm( "" ); }
+       $action = $wgRequest->getVal( 'action' );
+       if ( 'success' == $action ) {
+               $ipb->showSuccess();
+       } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
+               $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
+               $ipb->doSubmit();
+       } else {
+               $ipb->showForm( '' );
+       }
 }
 
+/**
+ * Form object
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
 class IPBlockForm {
+       var $BlockAddress, $BlockExpiry, $BlockReason;
 
-       function showForm( $err )
-       {
-               global $wgOut, $wgUser, $wgLang;
-               global $ip, $wpBlockAddress, $wpBlockReason;
-
-               $wgOut->setPagetitle( wfMsg( "blockip" ) );
-               $wgOut->addWikiText( wfMsg( "blockiptext" ) );
-
-               if ( ! $wpBlockAddress ) { $wpBlockAddress = $ip; }
-               $ipa = wfMsg( "ipaddress" );
-               $reason = wfMsg( "ipbreason" );
-               $ipbs = wfMsg( "ipbsubmit" );
-               $action = wfLocalUrlE( $wgLang->specialPage( "Blockip" ),
-                 "action=submit" );
+       function IPBlockForm() {
+               global $wgRequest;
+               $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip' ) );
+               $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
+               $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry' );
+       }
+       
+       function showForm( $err ) {
+               global $wgOut, $wgUser, $wgLang, $wgBlockExpiryOptions;
+               global $wgRequest, $wgSysopUserBans;
+
+               $wgOut->setPagetitle( htmlspecialchars( wfMsg( 'blockip' ) ) );
+               $wgOut->addWikiText( wfMsg( 'blockiptext' ) );
+
+               if($wgSysopUserBans) {
+                       $mIpaddress = htmlspecialchars( wfMsg( 'ipadressorusername' ) );
+               } else {
+                       $mIpaddress = htmlspecialchars( wfMsg( 'ipaddress' ) );
+               }
+               $mIpbexpiry = htmlspecialchars( wfMsg( 'ipbexpiry' ) );
+               $mIpbreason = htmlspecialchars( wfMsg( 'ipbreason' ) );
+               $mIpbsubmit = htmlspecialchars( wfMsg( 'ipbsubmit' ) );
+               $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
+               $action = $titleObj->escapeLocalURL( "action=submit" );
 
                if ( "" != $err ) {
-                       $wgOut->setSubtitle( wfMsg( "formerror" ) );
-                       $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
+                       $wgOut->setSubtitle( htmlspecialchars( wfMsg( 'formerror' ) ) );
+                       $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
                }
-               $wgOut->addHTML( "<p>
+
+               $scBlockAddress = htmlspecialchars( $this->BlockAddress );
+               $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
+               $scBlockReason = htmlspecialchars( $this->BlockReason );
+
+               $blockExpiryFormOptions = '<option>' .
+                       implode("</option>\n\t\t\t\t\t<option>", explode(',', $wgBlockExpiryOptions)) .
+                       '</option>';
+
+               $token = htmlspecialchars( $wgUser->editToken() );
+               
+               $wgOut->addHTML( "
 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
-<table border=0><tr>
-<td align=\"right\">{$ipa}:</td>
-<td align=\"left\">
-<input tabindex=1 type=text size=20 name=\"wpBlockAddress\" value=\"{$wpBlockAddress}\">
-</td></tr><tr>
-<td align=\"right\">{$reason}:</td>
-<td align=\"left\">
-<input tabindex=2 type=text size=40 name=\"wpBlockReason\" value=\"{$wpBlockReason}\">
-</td></tr><tr>
-<td>&nbsp;</td><td align=\"left\">
-<input tabindex=3 type=submit name=\"wpBlock\" value=\"{$ipbs}\">
-</td></tr></table>
+       <table border='0'>
+               <tr>
+                       <td align=\"right\">{$mIpaddress}:</td>
+                       <td align=\"left\">
+                               <input tabindex='1' type='text' size='20' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
+                       </td>
+               </tr>
+               <tr>
+                       <td align=\"right\">{$mIpbexpiry}:</td>
+                       <td align=\"left\">
+                               <select tabindex='2' name=\"wpBlockExpiry\">
+                                       $blockExpiryFormOptions
+                               </select>
+                       </td>
+               </tr>
+               <tr>
+                       <td align=\"right\">{$mIpbreason}:</td>
+                       <td align=\"left\">
+                               <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
+                       </td>
+               </tr>
+               <tr>
+                       <td>&nbsp;</td>
+                       <td align=\"left\">
+                               <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
+                       </td>
+               </tr>
+       </table>
+       <input type='hidden' name='wpEditToken' value=\"{$token}\" />
 </form>\n" );
 
        }
 
-       function doSubmit()
-       {
+       function doSubmit() {
                global $wgOut, $wgUser, $wgLang;
-               global $ip, $wpBlockAddress, $wpBlockReason, $wgSysopUserBans;
+               global $wgSysopUserBans, $wgSysopRangeBans;
                
                $userId = 0;
-               $wpBlockAddress = trim( $wpBlockAddress );
-
-               if ( ! preg_match( "/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$/",
-                 $wpBlockAddress ) ) 
-               {
-                       if ( $wgSysopUserBans ) {       
-                               $userId = User::idFromName( $wpBlockAddress );
-                               if ( $userId == 0 ) {
-                                       $this->showForm( wfMsg( "badipaddress" ) );
+               $this->BlockAddress = trim( $this->BlockAddress );
+               $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
+
+               # Check for invalid specifications
+               if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
+                       if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
+                               if ( $wgSysopRangeBans ) {
+                                       if ( $matches[2] > 31 || $matches[2] < 16 ) {
+                                               $this->showForm( wfMsg( 'ip_range_invalid' ) );
+                                               return;
+                                       }
+                                       $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
+                               } else {
+                                       # Range block illegal
+                                       $this->showForm( wfMsg( 'range_block_disabled' ) );
                                        return;
                                }
                        } else {
-                               $this->showForm( wfMsg( "badipaddress" ) );
-                               return;
-                       }               
+                               # Username block
+                               if ( $wgSysopUserBans ) {       
+                                       $userId = User::idFromName( $this->BlockAddress );
+                                       if ( $userId == 0 ) {
+                                               $this->showForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->BlockAddress ) ) );
+                                               return;
+                                       }
+                               } else {
+                                       $this->showForm( wfMsg( 'badipaddress' ) );
+                                       return;
+                               }
+                       }
                }
-               if ( "" == $wpBlockReason ) {
-                       $this->showForm( wfMsg( "noblockreason" ) );
-                       return;
+
+               if ( $this->BlockExpiry == 'infinite' || $this->BlockExpiry == 'indefinite' ) {
+                       $expiry = '';
+               } else {
+                       # Convert GNU-style date, returns -1 on error
+                       $expiry = strtotime( $this->BlockExpiry );
+
+                       if ( $expiry < 0 ) {
+                               $this->showForm( wfMsg( 'ipb_expiry_invalid' ) );
+                               return;
+                       }
+                       
+                       $expiry = wfTimestamp( TS_MW, $expiry );
+
                }
                
                # Create block
                # Note: for a user block, ipb_address is only for display purposes
-               $ban = new Block( $wpBlockAddress, $userId, $wgUser->getID(), 
-                       wfStrencode( $wpBlockReason ), wfTimestampNow(), 0 );
-               $ban->insert();
-
-               # Make log entry
-               $log = new LogPage( wfMsg( "blocklogpage" ), wfMsg( "blocklogtext" ) );
-               $action = wfMsg( "blocklogentry", $wgBlockAddress );
-               $log->addEntry( $action, $wpBlockReason );
-
-               # Report to the user
-               $success = wfLocalUrl( $wgLang->specialPage( "Blockip" ),
-                 "action=success&ip={$wpBlockAddress}" );
-               $wgOut->redirect( $success );
+
+               $ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(), 
+                       $this->BlockReason, wfTimestampNow(), 0, $expiry );
+               
+               if (wfRunHooks('BlockIp', array(&$ban, &$wgUser))) {
+                       
+                       $ban->insert();
+                       
+                       wfRunHooks('BlockIpComplete', array($ban, $wgUser));
+                       
+                       # Make log entry
+                       $log = new LogPage( 'block' );
+                       $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ), 
+                         $this->BlockReason, $this->BlockExpiry );
+
+                       # Report to the user
+                       $titleObj = Title::makeTitle( NS_SPECIAL, 'Blockip' );
+                       $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
+                               urlencode( $this->BlockAddress ) ) );
+               }
        }
 
-       function showSuccess()
-       {
+       function showSuccess() {
                global $wgOut, $wgUser;
-               global $ip;
 
-               $wgOut->setPagetitle( wfMsg( "blockip" ) );
-               $wgOut->setSubtitle( wfMsg( "blockipsuccesssub" ) );
-               $text = wfMsg( "blockipsuccesstext", $ip );
+               $wgOut->setPagetitle( wfMsg( 'blockip' ) );
+               $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
+               $text = wfMsg( 'blockipsuccesstext', $this->BlockAddress );
                $wgOut->addWikiText( $text );
        }
 }