00001 <?php
00002
00003 function wfSpecialRestrictUser( $par = null ) {
00004 global $wgOut, $wgRequest;
00005 $user = $userOrig = null;
00006 if( $par ) {
00007 $userOrig = $par;
00008 } elseif( $wgRequest->getVal( 'user' ) ) {
00009 $userOrig = $wgRequest->getVal( 'user' );
00010 } else {
00011 $wgOut->addHTML( RestrictUserForm::selectUserForm() );
00012 return;
00013 }
00014 $isIP = User::isIP( $userOrig );
00015 $user = $isIP ? $userOrig : User::getCanonicalName( $userOrig );
00016 $uid = User::idFromName( $user );
00017 if( !$uid && !$isIP ) {
00018 $err = '<strong class="error">' . wfMsgHtml( 'restrictuser-notfound' ) . '</strong>';
00019 $wgOut->addHTML( RestrictUserForm::selectUserForm( $userOrig, $err ) );
00020 return;
00021 }
00022 $wgOut->addHTML( RestrictUserForm::selectUserForm( $user ) );
00023
00024 UserRestriction::purgeExpired();
00025 $old = UserRestriction::fetchForUser( $user, true );
00026
00027 RestrictUserForm::pageRestrictionForm( $uid, $user, $old );
00028 RestrictUserForm::namespaceRestrictionForm( $uid, $user, $old );
00029
00030
00031 $old = UserRestriction::fetchForUser( $user, true );
00032 if( $old ) {
00033 $wgOut->addHTML( RestrictUserForm::existingRestrictions( $old ) );
00034 }
00035 }
00036
00037 class RestrictUserForm {
00038 public static function selectUserForm( $val = null, $error = null ) {
00039 global $wgScript, $wgTitle;
00040 $action = htmlspecialchars( $wgScript );
00041 $s = Xml::fieldset( wfMsg( 'restrictuser-userselect' ) ) . "<form action=\"{$action}\">";
00042 if( $error )
00043 $s .= '<p>' . $error . '</p>';
00044 $s .= Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() );
00045 $form = array( 'restrictuser-user' => Xml::input( 'user', false, $val ) );
00046 $s .= Xml::buildForm( $form, 'restrictuser-go' );
00047 $s .= "</form></fieldset>";
00048 return $s;
00049 }
00050
00051 public static function existingRestrictions( $restrictions ) {
00052
00053 require_once( dirname( __FILE__ ) . '/SpecialListUserRestrictions.php' );
00054 $s = Xml::fieldset( wfMsg( 'restrictuser-existing' ) ) . '<ul>';
00055 foreach( $restrictions as $r )
00056 $s .= UserRestrictionsPager::formatRestriction( $r );
00057 $s .= "</ul></fieldset>";
00058 return $s;
00059 }
00060
00061 public static function pageRestrictionForm( $uid, $user, $oldRestrictions ) {
00062 global $wgOut, $wgTitle, $wgRequest, $wgUser;
00063 $error = '';
00064 $success = false;
00065 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'type' ) == UserRestriction::PAGE &&
00066 $wgUser->matchEditToken( $wgRequest->getVal( 'edittoken' ) ) ) {
00067
00068 $title = Title::newFromText( $wgRequest->getVal( 'page' ) );
00069 if( !$title ) {
00070 $error = array( 'restrictuser-badtitle', $wgRequest->getVal( 'page' ) );
00071 } elseif( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) === false ) {
00072 $error = array( 'restrictuser-badexpiry', $wgRequest->getVal( 'expiry' ) );
00073 } else {
00074 foreach( $oldRestrictions as $r ) {
00075 if( $r->isPage() && $r->getPage()->equals( $title ) )
00076 $error = array( 'restrictuser-duptitle' );
00077 }
00078 }
00079 if( !$error ) {
00080 self::doPageRestriction( $uid, $user );
00081 $success = array('restrictuser-success', $user);
00082 }
00083 }
00084 $useRequestValues = $wgRequest->getVal( 'type' ) == UserRestriction::PAGE;
00085 $wgOut->addHTML( Xml::fieldset( wfMsg( 'restrictuser-legend-page' ) ) );
00086
00087 self::printSuccessError( $success, $error );
00088
00089 $wgOut->addHTML( Xml::openElement( 'form', array( 'action' => $wgTitle->getLocalUrl(),
00090 'method' => 'post' ) ) );
00091 $wgOut->addHTML( Xml::hidden( 'type', UserRestriction::PAGE ) );
00092 $wgOut->addHTML( Xml::hidden( 'edittoken', $wgUser->editToken() ) );
00093 $wgOut->addHTML( Xml::hidden( 'user', $user ) );
00094 $form = array();
00095 $form['restrictuser-title'] = Xml::input( 'page', false,
00096 $useRequestValues ? $wgRequest->getVal( 'page' ) : false );
00097 $form['restrictuser-expiry'] = Xml::input( 'expiry', false,
00098 $useRequestValues ? $wgRequest->getVal( 'expiry' ) : false );
00099 $form['restrictuser-reason'] = Xml::input( 'reason', false,
00100 $useRequestValues ? $wgRequest->getVal( 'reason' ) : false );
00101 $wgOut->addHTML( Xml::buildForm( $form, 'restrictuser-submit' ) );
00102 $wgOut->addHTML( "</form></fieldset>" );
00103 }
00104
00105 public static function printSuccessError( $success, $error ) {
00106 global $wgOut;
00107 if ( $error )
00108 $wgOut->wrapWikiMsg( '<strong class="error">$1</strong>', $error );
00109 if ( $success )
00110 $wgOut->wrapWikiMsg( '<strong class="success">$1</strong>', $success );
00111 }
00112
00113 public static function doPageRestriction( $uid, $user ) {
00114 global $wgUser, $wgRequest;
00115 $r = new UserRestriction();
00116 $r->setType( UserRestriction::PAGE );
00117 $r->setPage( Title::newFromText( $wgRequest->getVal( 'page' ) ) );
00118 $r->setSubjectId( $uid );
00119 $r->setSubjectText( $user );
00120 $r->setBlockerId( $wgUser->getId() );
00121 $r->setBlockerText( $wgUser->getName() );
00122 $r->setReason( $wgRequest->getVal( 'reason' ) );
00123 $r->setExpiry( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) );
00124 $r->setTimestamp( wfTimestampNow( TS_MW ) );
00125 $r->commit();
00126 $logExpiry = $wgRequest->getVal( 'expiry' ) ? $wgRequest->getVal( 'expiry' ) : Block::infinity();
00127 $l = new LogPage( 'restrict' );
00128 $l->addEntry( 'restrict', Title::makeTitle( NS_USER, $user ), $r->getReason(),
00129 array( $r->getType(), $r->getPage()->getFullText(), $logExpiry) );
00130 }
00131
00132 public static function namespaceRestrictionForm( $uid, $user, $oldRestrictions ) {
00133 global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgContLang;
00134 $error = '';
00135 $success = false;
00136 if( $wgRequest->wasPosted() && $wgRequest->getVal( 'type' ) == UserRestriction::NAMESPACE &&
00137 $wgUser->matchEditToken( $wgRequest->getVal( 'edittoken' ) ) ) {
00138 $ns = $wgRequest->getVal( 'namespace' );
00139 if( $wgContLang->getNsText( $ns ) === false )
00140 $error = wfMsgExt( 'restrictuser-badnamespace', 'parseinline' );
00141 elseif( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) === false )
00142 $error = wfMsgExt( 'restrictuser-badexpiry', 'parseinline', $wgRequest->getVal( 'expiry' ) );
00143 else
00144 foreach( $oldRestrictions as $r )
00145 if( $r->isNamespace() && $r->getNamespace() == $ns )
00146 $error = wfMsgExt( 'restrictuser-dupnamespace', 'parse' );
00147 if( !$error ) {
00148 self::doNamespaceRestriction( $uid, $user );
00149 $success = array('restrictuser-success', $user);
00150 }
00151 }
00152 $useRequestValues = $wgRequest->getVal( 'type' ) == UserRestriction::NAMESPACE;
00153 $wgOut->addHTML( Xml::fieldset( wfMsg( 'restrictuser-legend-namespace' ) ) );
00154
00155 self::printSuccessError( $success, $error );
00156
00157 $wgOut->addHTML( Xml::openElement( 'form', array( 'action' => $wgTitle->getLocalUrl(),
00158 'method' => 'post' ) ) );
00159 $wgOut->addHTML( Xml::hidden( 'type', UserRestriction::NAMESPACE ) );
00160 $wgOut->addHTML( Xml::hidden( 'edittoken', $wgUser->editToken() ) );
00161 $wgOut->addHTML( Xml::hidden( 'user', $user ) );
00162 $form = array();
00163 $form['restrictuser-namespace'] = Xml::namespaceSelector( $wgRequest->getVal( 'namespace' ) );
00164 $form['restrictuser-expiry'] = Xml::input( 'expiry', false,
00165 $useRequestValues ? $wgRequest->getVal( 'expiry' ) : false );
00166 $form['restrictuser-reason'] = Xml::input( 'reason', false,
00167 $useRequestValues ? $wgRequest->getVal( 'reason' ) : false );
00168 $wgOut->addHTML( Xml::buildForm( $form, 'restrictuser-submit' ) );
00169 $wgOut->addHTML( "</form></fieldset>" );
00170 }
00171
00172 public static function doNamespaceRestriction( $uid, $user ) {
00173 global $wgUser, $wgRequest;
00174 $r = new UserRestriction();
00175 $r->setType( UserRestriction::NAMESPACE );
00176 $r->setNamespace( $wgRequest->getVal( 'namespace' ) );
00177 $r->setSubjectId( $uid );
00178 $r->setSubjectText( $user );
00179 $r->setBlockerId( $wgUser->getId() );
00180 $r->setBlockerText( $wgUser->getName() );
00181 $r->setReason( $wgRequest->getVal( 'reason' ) );
00182 $r->setExpiry( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) );
00183 $r->setTimestamp( wfTimestampNow( TS_MW ) );
00184 $r->commit();
00185 $logExpiry = $wgRequest->getVal( 'expiry' ) ? $wgRequest->getVal( 'expiry' ) : Block::infinity();
00186 $l = new LogPage( 'restrict' );
00187 $l->addEntry( 'restrict', Title::makeTitle( NS_USER, $user ), $r->getReason(),
00188 array( $r->getType(), $r->getNamespace(), $logExpiry ) );
00189 }
00190 }