SpecialRestrictUser.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. function wfSpecialRestrictUser( $par = null ) {
  3. global $wgOut, $wgRequest;
  4. $user = $userOrig = null;
  5. if( $par ) {
  6. $userOrig = $par;
  7. } elseif( $wgRequest->getVal( 'user' ) ) {
  8. $userOrig = $wgRequest->getVal( 'user' );
  9. } else {
  10. $wgOut->addHTML( RestrictUserForm::selectUserForm() );
  11. return;
  12. }
  13. $isIP = User::isIP( $userOrig );
  14. $user = $isIP ? $userOrig : User::getCanonicalName( $userOrig );
  15. $uid = User::idFromName( $user );
  16. if( !$uid && !$isIP ) {
  17. $err = '<strong class="error">' . wfMsgHtml( 'restrictuser-notfound' ) . '</strong>';
  18. $wgOut->addHTML( RestrictUserForm::selectUserForm( $userOrig, $err ) );
  19. return;
  20. }
  21. $wgOut->addHTML( RestrictUserForm::selectUserForm( $user ) );
  22. UserRestriction::purgeExpired();
  23. $old = UserRestriction::fetchForUser( $user, true );
  24. RestrictUserForm::pageRestrictionForm( $uid, $user, $old );
  25. RestrictUserForm::namespaceRestrictionForm( $uid, $user, $old );
  26. // Renew it after possible changes in previous two functions
  27. $old = UserRestriction::fetchForUser( $user, true );
  28. if( $old ) {
  29. $wgOut->addHTML( RestrictUserForm::existingRestrictions( $old ) );
  30. }
  31. }
  32. class RestrictUserForm {
  33. public static function selectUserForm( $val = null, $error = null ) {
  34. global $wgScript, $wgTitle;
  35. $action = htmlspecialchars( $wgScript );
  36. $s = Xml::fieldset( wfMsg( 'restrictuser-userselect' ) ) . "<form action=\"{$action}\">";
  37. if( $error )
  38. $s .= '<p>' . $error . '</p>';
  39. $s .= Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() );
  40. $form = array( 'restrictuser-user' => Xml::input( 'user', false, $val ) );
  41. $s .= Xml::buildForm( $form, 'restrictuser-go' );
  42. $s .= "</form></fieldset>";
  43. return $s;
  44. }
  45. public static function existingRestrictions( $restrictions ) {
  46. //TODO: autoload?
  47. require_once( dirname( __FILE__ ) . '/SpecialListUserRestrictions.php' );
  48. $s = Xml::fieldset( wfMsg( 'restrictuser-existing' ) ) . '<ul>';
  49. foreach( $restrictions as $r )
  50. $s .= UserRestrictionsPager::formatRestriction( $r );
  51. $s .= "</ul></fieldset>";
  52. return $s;
  53. }
  54. public static function pageRestrictionForm( $uid, $user, $oldRestrictions ) {
  55. global $wgOut, $wgTitle, $wgRequest, $wgUser;
  56. $error = '';
  57. $success = false;
  58. if( $wgRequest->wasPosted() && $wgRequest->getVal( 'type' ) == UserRestriction::PAGE &&
  59. $wgUser->matchEditToken( $wgRequest->getVal( 'edittoken' ) ) ) {
  60. $title = Title::newFromText( $wgRequest->getVal( 'page' ) );
  61. if( !$title ) {
  62. $error = array( 'restrictuser-badtitle', $wgRequest->getVal( 'page' ) );
  63. } elseif( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) === false ) {
  64. $error = array( 'restrictuser-badexpiry', $wgRequest->getVal( 'expiry' ) );
  65. } else {
  66. foreach( $oldRestrictions as $r ) {
  67. if( $r->isPage() && $r->getPage()->equals( $title ) )
  68. $error = array( 'restrictuser-duptitle' );
  69. }
  70. }
  71. if( !$error ) {
  72. self::doPageRestriction( $uid, $user );
  73. $success = array('restrictuser-success', $user);
  74. }
  75. }
  76. $useRequestValues = $wgRequest->getVal( 'type' ) == UserRestriction::PAGE;
  77. $wgOut->addHTML( Xml::fieldset( wfMsg( 'restrictuser-legend-page' ) ) );
  78. self::printSuccessError( $success, $error );
  79. $wgOut->addHTML( Xml::openElement( 'form', array( 'action' => $wgTitle->getLocalUrl(),
  80. 'method' => 'post' ) ) );
  81. $wgOut->addHTML( Xml::hidden( 'type', UserRestriction::PAGE ) );
  82. $wgOut->addHTML( Xml::hidden( 'edittoken', $wgUser->editToken() ) );
  83. $wgOut->addHTML( Xml::hidden( 'user', $user ) );
  84. $form = array();
  85. $form['restrictuser-title'] = Xml::input( 'page', false,
  86. $useRequestValues ? $wgRequest->getVal( 'page' ) : false );
  87. $form['restrictuser-expiry'] = Xml::input( 'expiry', false,
  88. $useRequestValues ? $wgRequest->getVal( 'expiry' ) : false );
  89. $form['restrictuser-reason'] = Xml::input( 'reason', false,
  90. $useRequestValues ? $wgRequest->getVal( 'reason' ) : false );
  91. $wgOut->addHTML( Xml::buildForm( $form, 'restrictuser-submit' ) );
  92. $wgOut->addHTML( "</form></fieldset>" );
  93. }
  94. public static function printSuccessError( $success, $error ) {
  95. global $wgOut;
  96. if ( $error )
  97. $wgOut->wrapWikiMsg( '<strong class="error">$1</strong>', $error );
  98. if ( $success )
  99. $wgOut->wrapWikiMsg( '<strong class="success">$1</strong>', $success );
  100. }
  101. public static function doPageRestriction( $uid, $user ) {
  102. global $wgUser, $wgRequest;
  103. $r = new UserRestriction();
  104. $r->setType( UserRestriction::PAGE );
  105. $r->setPage( Title::newFromText( $wgRequest->getVal( 'page' ) ) );
  106. $r->setSubjectId( $uid );
  107. $r->setSubjectText( $user );
  108. $r->setBlockerId( $wgUser->getId() );
  109. $r->setBlockerText( $wgUser->getName() );
  110. $r->setReason( $wgRequest->getVal( 'reason' ) );
  111. $r->setExpiry( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) );
  112. $r->setTimestamp( wfTimestampNow( TS_MW ) );
  113. $r->commit();
  114. $logExpiry = $wgRequest->getVal( 'expiry' ) ? $wgRequest->getVal( 'expiry' ) : Block::infinity();
  115. $l = new LogPage( 'restrict' );
  116. $l->addEntry( 'restrict', Title::makeTitle( NS_USER, $user ), $r->getReason(),
  117. array( $r->getType(), $r->getPage()->getFullText(), $logExpiry) );
  118. }
  119. public static function namespaceRestrictionForm( $uid, $user, $oldRestrictions ) {
  120. global $wgOut, $wgTitle, $wgRequest, $wgUser, $wgContLang;
  121. $error = '';
  122. $success = false;
  123. if( $wgRequest->wasPosted() && $wgRequest->getVal( 'type' ) == UserRestriction::NAMESPACE &&
  124. $wgUser->matchEditToken( $wgRequest->getVal( 'edittoken' ) ) ) {
  125. $ns = $wgRequest->getVal( 'namespace' );
  126. if( $wgContLang->getNsText( $ns ) === false )
  127. $error = wfMsgExt( 'restrictuser-badnamespace', 'parseinline' );
  128. elseif( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) === false )
  129. $error = wfMsgExt( 'restrictuser-badexpiry', 'parseinline', $wgRequest->getVal( 'expiry' ) );
  130. else
  131. foreach( $oldRestrictions as $r )
  132. if( $r->isNamespace() && $r->getNamespace() == $ns )
  133. $error = wfMsgExt( 'restrictuser-dupnamespace', 'parse' );
  134. if( !$error ) {
  135. self::doNamespaceRestriction( $uid, $user );
  136. $success = array('restrictuser-success', $user);
  137. }
  138. }
  139. $useRequestValues = $wgRequest->getVal( 'type' ) == UserRestriction::NAMESPACE;
  140. $wgOut->addHTML( Xml::fieldset( wfMsg( 'restrictuser-legend-namespace' ) ) );
  141. self::printSuccessError( $success, $error );
  142. $wgOut->addHTML( Xml::openElement( 'form', array( 'action' => $wgTitle->getLocalUrl(),
  143. 'method' => 'post' ) ) );
  144. $wgOut->addHTML( Xml::hidden( 'type', UserRestriction::NAMESPACE ) );
  145. $wgOut->addHTML( Xml::hidden( 'edittoken', $wgUser->editToken() ) );
  146. $wgOut->addHTML( Xml::hidden( 'user', $user ) );
  147. $form = array();
  148. $form['restrictuser-namespace'] = Xml::namespaceSelector( $wgRequest->getVal( 'namespace' ) );
  149. $form['restrictuser-expiry'] = Xml::input( 'expiry', false,
  150. $useRequestValues ? $wgRequest->getVal( 'expiry' ) : false );
  151. $form['restrictuser-reason'] = Xml::input( 'reason', false,
  152. $useRequestValues ? $wgRequest->getVal( 'reason' ) : false );
  153. $wgOut->addHTML( Xml::buildForm( $form, 'restrictuser-submit' ) );
  154. $wgOut->addHTML( "</form></fieldset>" );
  155. }
  156. public static function doNamespaceRestriction( $uid, $user ) {
  157. global $wgUser, $wgRequest;
  158. $r = new UserRestriction();
  159. $r->setType( UserRestriction::NAMESPACE );
  160. $r->setNamespace( $wgRequest->getVal( 'namespace' ) );
  161. $r->setSubjectId( $uid );
  162. $r->setSubjectText( $user );
  163. $r->setBlockerId( $wgUser->getId() );
  164. $r->setBlockerText( $wgUser->getName() );
  165. $r->setReason( $wgRequest->getVal( 'reason' ) );
  166. $r->setExpiry( UserRestriction::convertExpiry( $wgRequest->getVal( 'expiry' ) ) );
  167. $r->setTimestamp( wfTimestampNow( TS_MW ) );
  168. $r->commit();
  169. $logExpiry = $wgRequest->getVal( 'expiry' ) ? $wgRequest->getVal( 'expiry' ) : Block::infinity();
  170. $l = new LogPage( 'restrict' );
  171. $l->addEntry( 'restrict', Title::makeTitle( NS_USER, $user ), $r->getReason(),
  172. array( $r->getType(), $r->getNamespace(), $logExpiry ) );
  173. }
  174. }