PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/Crosswiki/Block/CrosswikiBlock.page.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 374 lines | 310 code | 42 blank | 22 comment | 46 complexity | 3bbdbf875898f520c23aae78c24b1ffe MD5 | raw file
  1. <?php
  2. if ( !defined( 'MEDIAWIKI' ) ) {
  3. exit( 1 );
  4. }
  5. /**
  6. * Special page allows stewards to block users on other wiki
  7. */
  8. class SpecialCrosswikiBlock extends SpecialPage {
  9. var $mUserProxy, $mUsername, $mDatabase;
  10. /**
  11. * Constructor
  12. */
  13. public function __construct() {
  14. parent::__construct( 'Crosswikiblock', 'crosswikiblock' );
  15. }
  16. /**
  17. * Show the special page
  18. *
  19. * @param mixed $par Parameter passed to the page
  20. */
  21. public function execute( $par ) {
  22. global $wgOut, $wgUser, $wgRequest;
  23. # Add messages
  24. $this->setHeaders();
  25. if ( !$wgUser->isAllowed( 'crosswikiblock' ) ) {
  26. $wgOut->permissionRequired( 'crosswikiblock' );
  27. return;
  28. }
  29. if ( wfReadOnly() ) {
  30. $wgOut->readOnlyPage();
  31. return;
  32. }
  33. $action = $wgRequest->getVal( 'action' );
  34. if ( $action == 'submit' ) {
  35. $blockAddress = $wgRequest->getVal( 'wpBlockAddress' );
  36. $expiryStr = $wgRequest->getVal( 'wpBlockExpiry' );
  37. $reason = $wgRequest->getVal( 'wpBlockReason' );
  38. $options = array(
  39. 'anononly' => $wgRequest->getCheck( 'wpAnonOnly' ),
  40. 'nocreate' => $wgRequest->getCheck( 'wpCreateAccount' ),
  41. 'autoblock' => $wgRequest->getCheck( 'wpEnableAutoblock' ),
  42. 'noemail' => $wgRequest->getCheck( 'wpEmailBan' ),
  43. );
  44. if ( !$blockAddress ) {
  45. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-nousername' ) );
  46. } elseif ( $this->checkUser( $blockAddress, true ) ) {
  47. } elseif ( !( $expiry = $this->convertExpiry( $expiryStr ) ) ) {
  48. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-noexpiry', htmlspecialchars( $expiryStr ) ) );
  49. } elseif ( !$reason ) {
  50. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-noreason', htmlspecialchars( $reason ) ) );
  51. } elseif ( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
  52. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-notoken' ) );
  53. } else {
  54. CrosswikiBlock::normalizeOptions( $this->mUsername, $options );
  55. $block = new CrosswikiBlock( $this->mDatabase, $this->mUsername, $this->mUserProxy,
  56. $wgUser, $expiry, $reason, wfTimestampNow(), 0, $options );
  57. $affected = $block->commit();
  58. if ( $affected ) {
  59. $wgOut->addHTML( wfMsgWikiHtml( 'crosswikiblock-success',
  60. htmlspecialchars( $this->mUsername ),
  61. htmlspecialchars( $this->mDatabase ),
  62. htmlspecialchars( $blockAddress ),
  63. htmlspecialchars( Title::newMainPage()->getFullText() ) ) );
  64. $block->log( $expiryStr );
  65. } else {
  66. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-alreadyblocked',
  67. htmlspecialchars( $this->mUsername ),
  68. htmlspecialchars( $this->mDatabase ),
  69. htmlspecialchars( $blockAddress ) ) );
  70. }
  71. }
  72. } else {
  73. $this->showForm();
  74. }
  75. }
  76. public function checkUser( $username, $output = false ) {
  77. global $wgOut;
  78. $bits = explode( '@', $username, 2 );
  79. if ( count( $bits ) == 1 ) {
  80. if ( $output )
  81. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-local' ) );
  82. return array( 'local' );
  83. }
  84. list( $name, $db ) = $bits;
  85. if ( !UserRightsProxy::validDatabase( $db ) ) {
  86. if ( $output )
  87. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-dbnotfound', htmlspecialchars( $db ) ) );
  88. return array( 'dbnotfound', $db );
  89. }
  90. if ( !User::isIP( $name ) && !User::isCreatableName( $name ) ) {
  91. if ( $output )
  92. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-noname', htmlspecialchars( $name ) ) );
  93. return array( 'invalidname', $name );
  94. }
  95. if ( !User::isIP( $name ) ) {
  96. $userProxy = UserRightsProxy::newFromName( $db, $name );
  97. $this->mUserProxy = $userProxy;
  98. if ( !$userProxy ) {
  99. if ( $output )
  100. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-nouser',
  101. htmlspecialchars( $name ), htmlspecialchars( $db ), htmlspecialchars( $username ) ) );
  102. return array( 'usernotfound', $name, $db, $username );
  103. }
  104. }
  105. $this->mUsername = $name;
  106. $this->mDatabase = $db;
  107. return false;
  108. }
  109. public function convertExpiry( $str ) {
  110. if ( strlen( $str ) == 0 ) {
  111. return;
  112. }
  113. if ( $str == 'infinite' || $str == 'indefinite' ) {
  114. return Block::infinity();
  115. } else {
  116. # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
  117. $expiry = strtotime( $str );
  118. if ( $expiry < 0 || $expiry === false ) {
  119. return;
  120. }
  121. return wfTimestamp( TS_MW, $expiry );
  122. }
  123. }
  124. public function showForm( $err = '' ) {
  125. global $wgOut, $wgContLang, $wgRequest, $wgUser;
  126. global $wgStylePath, $wgStyleVersion;
  127. $titleObj = $this->getTitle();
  128. $action = $titleObj->escapeLocalURL( "action=submit" );
  129. $alignRight = $wgContLang->isRtl() ? 'left' : 'right';
  130. $mIpaddress = Xml::label( wfMsg( 'crosswikiblock-target' ), 'mw-bi-target' );
  131. $mIpbexpiry = Xml::label( wfMsg( 'crosswikiblock-expiry' ), 'mw-bi-target' );
  132. $mIpbreason = Xml::label( wfMsg( 'crosswikiblock-reason' ), 'mw-bi-target' );
  133. if ( "" != $err ) {
  134. $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
  135. $wgOut->addHTML( "<strong class='error'>{$err}</strong>\n" );
  136. }
  137. $wgOut->addHTML( wfMsgWikiHtml( 'crosswikiblock-header' ) );
  138. $wgOut->addHTML( " <form id=\"blockip\" method=\"post\" action=\"{$action}\">
  139. <table border='0'>
  140. <tr>
  141. <td align=\"$alignRight\">{$mIpaddress}</td>
  142. <td>
  143. " . Xml::input( 'wpBlockAddress', 45, htmlspecialchars( strval( $wgRequest->getVal( 'wpBlockAddress' ) ) ),
  144. array(
  145. 'tabindex' => '1',
  146. 'id' => 'mw-bi-target', ) ) . "
  147. </td>
  148. </tr>
  149. <tr id='wpBlockExpiry'>
  150. <td align=\"$alignRight\">{$mIpbexpiry}</td>
  151. <td>
  152. " . Xml::input( 'wpBlockExpiry', 45, htmlspecialchars( strval( $wgRequest->getVal( 'wpBlockExpiry' ) ) ),
  153. array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
  154. </td>
  155. </tr>
  156. <tr id=\"wpBlockReason\">
  157. <td align=\"$alignRight\">{$mIpbreason}</td>
  158. <td>
  159. " . Xml::input(
  160. 'wpBlockReason',
  161. 45,
  162. htmlspecialchars( strval( $wgRequest->getVal( 'wpBlockReason' ) ) ),
  163. array(
  164. 'tabindex' => '5',
  165. 'id' => 'mw-bi-reason',
  166. 'maxlength' => '200'
  167. ) ) . "
  168. </td>
  169. </tr>
  170. <tr id='wpAnonOnlyRow'>
  171. <td>&#160;</td>
  172. <td>
  173. " . Xml::checkLabel( wfMsgHtml( 'crosswikiblock-anononly' ),
  174. 'wpAnonOnly', 'wpAnonOnly', $wgRequest->getCheck( 'wpAnonOnly' ),
  175. array( 'tabindex' => '6' ) ) . "
  176. </td>
  177. </tr>
  178. <tr id='wpCreateAccountRow'>
  179. <td>&#160;</td>
  180. <td>
  181. " . Xml::checkLabel( wfMsgHtml( 'crosswikiblock-nocreate' ),
  182. 'wpCreateAccount', 'wpCreateAccount', $wgRequest->getCheck( 'wpAnonOnly' ),
  183. array( 'tabindex' => '7' ) ) . "
  184. </td>
  185. </tr>
  186. <tr id='wpEnableAutoblockRow'>
  187. <td>&#160;</td>
  188. <td>
  189. " . Xml::checkLabel( wfMsgHtml( 'crosswikiblock-autoblock' ),
  190. 'wpEnableAutoblock', 'wpEnableAutoblock', $wgRequest->getCheck( 'wpAnonOnly' ),
  191. array( 'tabindex' => '8' ) ) . "
  192. </td>
  193. </tr>" );
  194. global $wgSysopEmailBans;
  195. if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
  196. $wgOut->addHTML( "
  197. <tr id='wpEnableEmailBan'>
  198. <td>&#160;</td>
  199. <td>
  200. " . Xml::checkLabel( wfMsgHtml( 'crosswikiblock-noemail' ),
  201. 'wpEmailBan', 'wpEmailBan', $wgRequest->getCheck( 'wpEmailBan' ),
  202. array( 'tabindex' => '10' ) ) . "
  203. </td>
  204. </tr>
  205. " );
  206. }
  207. $wgOut->addHTML( "
  208. <tr>
  209. <td style='padding-top: 1em'>&#160;</td>
  210. <td style='padding-top: 1em'>
  211. " . Xml::submitButton( wfMsg( 'crosswikiblock-submit' ),
  212. array( 'name' => 'wpBlock', 'tabindex' => '11' ) ) . "
  213. </td>
  214. </tr>" );
  215. $token = $wgUser->editToken();
  216. $wgOut->addHTML( Html::Hidden( 'wpEditToken', $token ) );
  217. $wgOut->addHTML( ' </table>
  218. </form>' );
  219. }
  220. }
  221. class SpecialCrosswikiUnblock extends SpecialPage {
  222. var $mUserProxy, $mUsername, $mDatabase;
  223. /**
  224. * Constructor
  225. */
  226. public function __construct() {
  227. parent::__construct( 'Crosswikiunblock', 'crosswikiblock' );
  228. }
  229. /**
  230. * Show the special page
  231. *
  232. * @param mixed $par Parameter passed to the page
  233. */
  234. public function execute( $par ) {
  235. global $wgOut, $wgUser, $wgRequest;
  236. # Add messages
  237. $this->setHeaders();
  238. if ( !$wgUser->isAllowed( 'crosswikiblock' ) ) {
  239. $wgOut->permissionRequired( 'crosswikiblock' );
  240. return;
  241. }
  242. if ( wfReadOnly() ) {
  243. $wgOut->readOnlyPage();
  244. return;
  245. }
  246. $action = $wgRequest->getVal( 'action' );
  247. if ( $action == 'submit' ) {
  248. $target = $wgRequest->getVal( 'wpUnblockTarget' );
  249. $reason = $wgRequest->getVal( 'wpUnblockReason' );
  250. $parsedUsername = CrosswikiBlock::parseBlockAddress( $target );
  251. if ( isset( $parsedUsername['error'] ) ) {
  252. switch( $parsedUsername['error'] ) {
  253. case 'nowiki':
  254. $this->showForm( wfMsgWikiHtml( 'crosswikiunblock-local' ) );
  255. break;
  256. case 'invalidwiki':
  257. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-dbnotfound', $parsedUsername['wiki'] ) );
  258. break;
  259. case 'invalidusername':
  260. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-noname', $parsedUsername['username'] ) );
  261. break;
  262. default:
  263. $this->showForm( wfMsgWikiHtml( 'unknownerror' ) );
  264. }
  265. } else {
  266. $blockRow = CrosswikiBlock::getBlockRow( $parsedUsername );
  267. if ( !$blockRow ) {
  268. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-noblock' ) );
  269. return;
  270. }
  271. $block = CrosswikiBlock::newFromRow( $blockRow, $parsedUsername['wiki'] );
  272. $result = $block->remove();
  273. if ( !$result ) {
  274. $this->showForm( wfMsgWikiHtml( 'crosswikiblock-noblock' ) );
  275. } else {
  276. $block->logUnblock( $reason );
  277. $wgOut->addHTML( wfMsgWikiHtml( 'crosswikiunblock-success', htmlspecialchars( $target ),
  278. Title::newMainPage()->getFullText() ) );
  279. }
  280. }
  281. } else {
  282. $this->showForm();
  283. }
  284. }
  285. public function showForm( $err = '' ) {
  286. global $wgOut, $wgContLang, $wgRequest, $wgUser;
  287. global $wgStylePath, $wgStyleVersion;
  288. $titleObj = $this->getTitle();
  289. $action = $titleObj->escapeLocalURL( "action=submit" );
  290. $alignRight = $wgContLang->isRtl() ? 'left' : 'right';
  291. $mIpbuser = Xml::label( wfMsg( 'crosswikiunblock-user' ), 'mw-bi-target' );
  292. $mIpbreason = Xml::label( wfMsg( 'crosswikiunblock-reason' ), 'mw-bi-target' );
  293. if ( "" != $err ) {
  294. $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
  295. $wgOut->addHTML( "<strong class='error'>{$err}</strong>\n" );
  296. }
  297. $wgOut->addHTML( wfMsgWikiHtml( 'crosswikiunblock-header' ) );
  298. $wgOut->addHTML( "<form id=\"crosswikiunblock\" method=\"post\" action=\"{$action}\">
  299. <table border='0'>
  300. <tr>
  301. <td align=\"$alignRight\">{$mIpbuser}</td>
  302. <td>
  303. " . Xml::input( 'wpUnblockTarget', 45, htmlspecialchars( strval( $wgRequest->getVal( 'wpUnblockTarget' ) ) ),
  304. array(
  305. 'tabindex' => '1',
  306. 'id' => 'mw-bi-target', ) ) . "
  307. </td>
  308. </tr>
  309. <tr id=\"wpBlockReason\">
  310. <td align=\"$alignRight\">{$mIpbreason}</td>
  311. <td>
  312. " . Xml::input( 'wpUnblockReason', 45, htmlspecialchars( strval( $wgRequest->getVal( 'wpUnblockReason' ) ) ),
  313. array(
  314. 'tabindex' => '5',
  315. 'id' => 'mw-bi-reason',
  316. 'maxlength' => '200'
  317. ) ) . "
  318. </td>
  319. </tr>" );
  320. $wgOut->addHTML( "
  321. <tr>
  322. <td style='padding-top: 1em'>&#160;</td>
  323. <td style='padding-top: 1em'>
  324. " . Xml::submitButton( wfMsg( 'crosswikiunblock-submit' ),
  325. array( 'name' => 'wpBlock', 'tabindex' => '11' ) ) . "
  326. </td>
  327. </tr>" );
  328. $token = $wgUser->editToken();
  329. $wgOut->addHTML( Html::Hidden( 'wpEditToken', $token ) );
  330. $wgOut->addHTML( '</table></form>' );
  331. }
  332. }