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