PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/ProtectionForm.php

https://github.com/tav/confluence
PHP | 552 lines | 404 code | 67 blank | 81 comment | 92 complexity | caee5d07d52dbe5563240d452179d702 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
  4. * http://www.mediawiki.org/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. */
  21. /**
  22. * Handles the page protection UI and backend
  23. */
  24. class ProtectionForm {
  25. /** A map of action to restriction level, from request or default */
  26. var $mRestrictions = array();
  27. /** The custom/additional protection reason */
  28. var $mReason = '';
  29. /** The reason selected from the list, blank for other/additional */
  30. var $mReasonSelection = '';
  31. /** True if the restrictions are cascading, from request or existing protection */
  32. var $mCascade = false;
  33. /** Map of action to "other" expiry time. Used in preference to mExpirySelection. */
  34. var $mExpiry = array();
  35. /**
  36. * Map of action to value selected in expiry drop-down list.
  37. * Will be set to 'othertime' whenever mExpiry is set.
  38. */
  39. var $mExpirySelection = array();
  40. /** Permissions errors for the protect action */
  41. var $mPermErrors = array();
  42. /** Types (i.e. actions) for which levels can be selected */
  43. var $mApplicableTypes = array();
  44. /** Map of action to the expiry time of the existing protection */
  45. var $mExistingExpiry = array();
  46. function __construct( Article $article ) {
  47. global $wgRequest, $wgUser;
  48. global $wgRestrictionTypes, $wgRestrictionLevels;
  49. $this->mArticle = $article;
  50. $this->mTitle = $article->mTitle;
  51. $this->mApplicableTypes = $this->mTitle->exists() ? $wgRestrictionTypes : array('create');
  52. $this->mCascade = $this->mTitle->areRestrictionsCascading();
  53. // The form will be available in read-only to show levels.
  54. $this->mPermErrors = $this->mTitle->getUserPermissionsErrors('protect',$wgUser);
  55. $this->disabled = wfReadOnly() || $this->mPermErrors != array();
  56. $this->disabledAttrib = $this->disabled
  57. ? array( 'disabled' => 'disabled' )
  58. : array();
  59. $this->mReason = $wgRequest->getText( 'mwProtect-reason' );
  60. $this->mReasonSelection = $wgRequest->getText( 'wpProtectReasonSelection' );
  61. $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade', $this->mCascade );
  62. foreach( $this->mApplicableTypes as $action ) {
  63. // Fixme: this form currently requires individual selections,
  64. // but the db allows multiples separated by commas.
  65. $this->mRestrictions[$action] = implode( '', $this->mTitle->getRestrictions( $action ) );
  66. if ( !$this->mRestrictions[$action] ) {
  67. // No existing expiry
  68. $existingExpiry = '';
  69. } else {
  70. $existingExpiry = $this->mTitle->getRestrictionExpiry( $action );
  71. }
  72. $this->mExistingExpiry[$action] = $existingExpiry;
  73. $requestExpiry = $wgRequest->getText( "mwProtect-expiry-$action" );
  74. $requestExpirySelection = $wgRequest->getVal( "wpProtectExpirySelection-$action" );
  75. if ( $requestExpiry ) {
  76. // Custom expiry takes precedence
  77. $this->mExpiry[$action] = $requestExpiry;
  78. $this->mExpirySelection[$action] = 'othertime';
  79. } elseif ( $requestExpirySelection ) {
  80. // Expiry selected from list
  81. $this->mExpiry[$action] = '';
  82. $this->mExpirySelection[$action] = $requestExpirySelection;
  83. } elseif ( $existingExpiry == 'infinity' ) {
  84. // Existing expiry is infinite, use "infinite" in drop-down
  85. $this->mExpiry[$action] = '';
  86. $this->mExpirySelection[$action] = 'infinite';
  87. } elseif ( $existingExpiry ) {
  88. // Use existing expiry in its own list item
  89. $this->mExpiry[$action] = '';
  90. $this->mExpirySelection[$action] = $existingExpiry;
  91. } else {
  92. // Final default: infinite
  93. $this->mExpiry[$action] = '';
  94. $this->mExpirySelection[$action] = 'infinite';
  95. }
  96. $val = $wgRequest->getVal( "mwProtect-level-$action" );
  97. if( isset( $val ) && in_array( $val, $wgRestrictionLevels ) ) {
  98. // Prevent users from setting levels that they cannot later unset
  99. if( $val == 'sysop' ) {
  100. // Special case, rewrite sysop to either protect and editprotected
  101. if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') )
  102. continue;
  103. } else {
  104. if( !$wgUser->isAllowed($val) )
  105. continue;
  106. }
  107. $this->mRestrictions[$action] = $val;
  108. }
  109. }
  110. }
  111. /**
  112. * Get the expiry time for a given action, by combining the relevant inputs.
  113. * Returns a 14-char timestamp or "infinity", or false if the input was invalid
  114. */
  115. function getExpiry( $action ) {
  116. if ( $this->mExpirySelection[$action] == 'existing' ) {
  117. return $this->mExistingExpiry[$action];
  118. } elseif ( $this->mExpirySelection[$action] == 'othertime' ) {
  119. $value = $this->mExpiry[$action];
  120. } else {
  121. $value = $this->mExpirySelection[$action];
  122. }
  123. if ( $value == 'infinite' || $value == 'indefinite' || $value == 'infinity' ) {
  124. $time = Block::infinity();
  125. } else {
  126. $unix = strtotime( $value );
  127. if ( !$unix || $unix === -1 ) {
  128. return false;
  129. }
  130. // Fixme: non-qualified absolute times are not in users specified timezone
  131. // and there isn't notice about it in the ui
  132. $time = wfTimestamp( TS_MW, $unix );
  133. }
  134. return $time;
  135. }
  136. function execute() {
  137. global $wgRequest, $wgOut;
  138. if( $wgRequest->wasPosted() ) {
  139. if( $this->save() ) {
  140. $q = $this->mArticle->isRedirect() ? 'redirect=no' : '';
  141. $wgOut->redirect( $this->mTitle->getFullUrl( $q ) );
  142. }
  143. } else {
  144. $this->show();
  145. }
  146. }
  147. function show( $err = null ) {
  148. global $wgOut, $wgUser;
  149. $wgOut->setRobotPolicy( 'noindex,nofollow' );
  150. if( is_null( $this->mTitle ) ||
  151. $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
  152. $wgOut->showFatalError( wfMsg( 'badarticleerror' ) );
  153. return;
  154. }
  155. list( $cascadeSources, /* $restrictions */ ) = $this->mTitle->getCascadeProtectionSources();
  156. if ( "" != $err ) {
  157. $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
  158. $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
  159. }
  160. if ( $cascadeSources && count($cascadeSources) > 0 ) {
  161. $titles = '';
  162. foreach ( $cascadeSources as $title ) {
  163. $titles .= '* [[:' . $title->getPrefixedText() . "]]\n";
  164. }
  165. $wgOut->wrapWikiMsg( "$1\n$titles", array( 'protect-cascadeon', count($cascadeSources) ) );
  166. }
  167. $sk = $wgUser->getSkin();
  168. $titleLink = $sk->makeLinkObj( $this->mTitle );
  169. $wgOut->setPageTitle( wfMsg( 'protect-title', $this->mTitle->getPrefixedText() ) );
  170. $wgOut->setSubtitle( wfMsg( 'protect-backlink', $titleLink ) );
  171. # Show an appropriate message if the user isn't allowed or able to change
  172. # the protection settings at this time
  173. if( $this->disabled ) {
  174. if( wfReadOnly() ) {
  175. $wgOut->readOnlyPage();
  176. } elseif( $this->mPermErrors ) {
  177. $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $this->mPermErrors ) );
  178. }
  179. } else {
  180. $wgOut->addWikiMsg( 'protect-text', $this->mTitle->getPrefixedText() );
  181. }
  182. $wgOut->addHTML( $this->buildForm() );
  183. $this->showLogExtract( $wgOut );
  184. }
  185. function save() {
  186. global $wgRequest, $wgUser, $wgOut;
  187. # Permission check!
  188. if ( $this->disabled ) {
  189. $this->show();
  190. return false;
  191. }
  192. $token = $wgRequest->getVal( 'wpEditToken' );
  193. if ( !$wgUser->matchEditToken( $token ) ) {
  194. $this->show( wfMsg( 'sessionfailure' ) );
  195. return false;
  196. }
  197. # Create reason string. Use list and/or custom string.
  198. $reasonstr = $this->mReasonSelection;
  199. if ( $reasonstr != 'other' && $this->mReason != '' ) {
  200. // Entry from drop down menu + additional comment
  201. $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->mReason;
  202. } elseif ( $reasonstr == 'other' ) {
  203. $reasonstr = $this->mReason;
  204. }
  205. $expiry = array();
  206. foreach( $this->mApplicableTypes as $action ) {
  207. $expiry[$action] = $this->getExpiry( $action );
  208. if( empty($this->mRestrictions[$action]) )
  209. continue; // unprotected
  210. if ( !$expiry[$action] ) {
  211. $this->show( wfMsg( 'protect_expiry_invalid' ) );
  212. return false;
  213. }
  214. if ( $expiry[$action] < wfTimestampNow() ) {
  215. $this->show( wfMsg( 'protect_expiry_old' ) );
  216. return false;
  217. }
  218. }
  219. # They shouldn't be able to do this anyway, but just to make sure, ensure that cascading restrictions aren't being applied
  220. # to a semi-protected page.
  221. global $wgGroupPermissions;
  222. $edit_restriction = $this->mRestrictions['edit'];
  223. $this->mCascade = $wgRequest->getBool( 'mwProtect-cascade' );
  224. if ($this->mCascade && ($edit_restriction != 'protect') &&
  225. !(isset($wgGroupPermissions[$edit_restriction]['protect']) && $wgGroupPermissions[$edit_restriction]['protect'] ) )
  226. $this->mCascade = false;
  227. if ($this->mTitle->exists()) {
  228. $ok = $this->mArticle->updateRestrictions( $this->mRestrictions, $reasonstr, $this->mCascade, $expiry );
  229. } else {
  230. $ok = $this->mTitle->updateTitleProtection( $this->mRestrictions['create'], $reasonstr, $expiry['create'] );
  231. }
  232. if( !$ok ) {
  233. throw new FatalError( "Unknown error at restriction save time." );
  234. }
  235. if( $wgRequest->getCheck( 'mwProtectWatch' ) ) {
  236. $this->mArticle->doWatch();
  237. } elseif( $this->mTitle->userIsWatching() ) {
  238. $this->mArticle->doUnwatch();
  239. }
  240. return $ok;
  241. }
  242. /**
  243. * Build the input form
  244. *
  245. * @return $out string HTML form
  246. */
  247. function buildForm() {
  248. global $wgUser, $wgLang;
  249. $mProtectreasonother = Xml::label( wfMsg( 'protectcomment' ), 'wpProtectReasonSelection' );
  250. $mProtectreason = Xml::label( wfMsg( 'protect-otherreason' ), 'mwProtect-reason' );
  251. $out = '';
  252. if( !$this->disabled ) {
  253. $out .= $this->buildScript();
  254. $out .= Xml::openElement( 'form', array( 'method' => 'post',
  255. 'action' => $this->mTitle->getLocalUrl( 'action=protect' ),
  256. 'id' => 'mw-Protect-Form', 'onsubmit' => 'ProtectionForm.enableUnchainedInputs(true)' ) );
  257. $out .= Xml::hidden( 'wpEditToken',$wgUser->editToken() );
  258. }
  259. $out .= Xml::openElement( 'fieldset' ) .
  260. Xml::element( 'legend', null, wfMsg( 'protect-legend' ) ) .
  261. Xml::openElement( 'table', array( 'id' => 'mwProtectSet' ) ) .
  262. Xml::openElement( 'tbody' );
  263. foreach( $this->mRestrictions as $action => $selected ) {
  264. /* Not all languages have V_x <-> N_x relation */
  265. $msg = wfMsg( 'restriction-' . $action );
  266. if( wfEmptyMsg( 'restriction-' . $action, $msg ) ) {
  267. $msg = $action;
  268. }
  269. $out .= "<tr><td>".
  270. Xml::openElement( 'fieldset' ) .
  271. Xml::element( 'legend', null, $msg ) .
  272. Xml::openElement( 'table', array( 'id' => "mw-protect-table-$action" ) ) .
  273. "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>";
  274. $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection',
  275. wfMsgForContent( 'protect-dropdown' ),
  276. wfMsgForContent( 'protect-otherreason-op' ),
  277. $this->mReasonSelection,
  278. 'mwProtect-reason', 4 );
  279. $scExpiryOptions = wfMsgForContent( 'protect-expiry-options' );
  280. $showProtectOptions = ($scExpiryOptions !== '-' && !$this->disabled);
  281. $mProtectexpiry = Xml::label( wfMsg( 'protectexpiry' ), "mwProtectExpirySelection-$action" );
  282. $mProtectother = Xml::label( wfMsg( 'protect-othertime' ), "mwProtect-$action-expires" );
  283. $expiryFormOptions = '';
  284. if ( $this->mExistingExpiry[$action] && $this->mExistingExpiry[$action] != 'infinity' ) {
  285. $timestamp = $wgLang->timeanddate( $this->mExistingExpiry[$action] );
  286. $d = $wgLang->date( $this->mExistingExpiry[$action] );
  287. $t = $wgLang->time( $this->mExistingExpiry[$action] );
  288. $expiryFormOptions .=
  289. Xml::option(
  290. wfMsg( 'protect-existing-expiry', $timestamp, $d, $t ),
  291. 'existing',
  292. $this->mExpirySelection[$action] == 'existing'
  293. ) . "\n";
  294. }
  295. $expiryFormOptions .= Xml::option( wfMsg( 'protect-othertime-op' ), "othertime" ) . "\n";
  296. foreach( explode(',', $scExpiryOptions) as $option ) {
  297. if ( strpos($option, ":") === false ) {
  298. $show = $value = $option;
  299. } else {
  300. list($show, $value) = explode(":", $option);
  301. }
  302. $show = htmlspecialchars($show);
  303. $value = htmlspecialchars($value);
  304. $expiryFormOptions .= Xml::option( $show, $value, $this->mExpirySelection[$action] === $value ) . "\n";
  305. }
  306. # Add expiry dropdown
  307. if( $showProtectOptions && !$this->disabled ) {
  308. $out .= "
  309. <table><tr>
  310. <td class='mw-label'>
  311. {$mProtectexpiry}
  312. </td>
  313. <td class='mw-input'>" .
  314. Xml::tags( 'select',
  315. array(
  316. 'id' => "mwProtectExpirySelection-$action",
  317. 'name' => "wpProtectExpirySelection-$action",
  318. 'onchange' => "ProtectionForm.updateExpiryList(this)",
  319. 'tabindex' => '2' ) + $this->disabledAttrib,
  320. $expiryFormOptions ) .
  321. "</td>
  322. </tr></table>";
  323. }
  324. # Add custom expiry field
  325. $attribs = array( 'id' => "mwProtect-$action-expires",
  326. 'onkeyup' => 'ProtectionForm.updateExpiry(this)' ) + $this->disabledAttrib;
  327. $out .= "<table><tr>
  328. <td class='mw-label'>" .
  329. $mProtectother .
  330. '</td>
  331. <td class="mw-input">' .
  332. Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) .
  333. '</td>
  334. </tr></table>';
  335. $out .= "</td></tr>" .
  336. Xml::closeElement( 'table' ) .
  337. Xml::closeElement( 'fieldset' ) .
  338. "</td></tr>";
  339. }
  340. $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
  341. // JavaScript will add another row with a value-chaining checkbox
  342. if( $this->mTitle->exists() ) {
  343. $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table2' ) ) .
  344. Xml::openElement( 'tbody' );
  345. $out .= '<tr>
  346. <td></td>
  347. <td class="mw-input">' .
  348. Xml::checkLabel( wfMsg( 'protect-cascade' ), 'mwProtect-cascade', 'mwProtect-cascade',
  349. $this->mCascade, $this->disabledAttrib ) .
  350. "</td>
  351. </tr>\n";
  352. $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
  353. }
  354. # Add manual and custom reason field/selects as well as submit
  355. if( !$this->disabled ) {
  356. $out .= Xml::openElement( 'table', array( 'id' => 'mw-protect-table3' ) ) .
  357. Xml::openElement( 'tbody' );
  358. $out .= "
  359. <tr>
  360. <td class='mw-label'>
  361. {$mProtectreasonother}
  362. </td>
  363. <td class='mw-input'>
  364. {$reasonDropDown}
  365. </td>
  366. </tr>
  367. <tr>
  368. <td class='mw-label'>
  369. {$mProtectreason}
  370. </td>
  371. <td class='mw-input'>" .
  372. Xml::input( 'mwProtect-reason', 60, $this->mReason, array( 'type' => 'text',
  373. 'id' => 'mwProtect-reason', 'maxlength' => 255 ) ) .
  374. "</td>
  375. </tr>
  376. <tr>
  377. <td></td>
  378. <td class='mw-input'>" .
  379. Xml::checkLabel( wfMsg( 'watchthis' ),
  380. 'mwProtectWatch', 'mwProtectWatch',
  381. $this->mTitle->userIsWatching() || $wgUser->getOption( 'watchdefault' ) ) .
  382. "</td>
  383. </tr>
  384. <tr>
  385. <td></td>
  386. <td class='mw-submit'>" .
  387. Xml::submitButton( wfMsg( 'confirm' ), array( 'id' => 'mw-Protect-submit' ) ) .
  388. "</td>
  389. </tr>\n";
  390. $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' );
  391. }
  392. $out .= Xml::closeElement( 'fieldset' );
  393. if ( $wgUser->isAllowed( 'editinterface' ) ) {
  394. $linkTitle = Title::makeTitleSafe( NS_MEDIAWIKI, 'protect-dropdown' );
  395. $link = $wgUser->getSkin()->Link ( $linkTitle, wfMsgHtml( 'protect-edit-reasonlist' ) );
  396. $out .= '<p class="mw-protect-editreasons">' . $link . '</p>';
  397. }
  398. if ( !$this->disabled ) {
  399. $out .= Xml::closeElement( 'form' ) .
  400. $this->buildCleanupScript();
  401. }
  402. return $out;
  403. }
  404. function buildSelector( $action, $selected ) {
  405. global $wgRestrictionLevels, $wgUser;
  406. $levels = array();
  407. foreach( $wgRestrictionLevels as $key ) {
  408. //don't let them choose levels above their own (aka so they can still unprotect and edit the page). but only when the form isn't disabled
  409. if( $key == 'sysop' ) {
  410. //special case, rewrite sysop to protect and editprotected
  411. if( !$wgUser->isAllowed('protect') && !$wgUser->isAllowed('editprotected') && !$this->disabled )
  412. continue;
  413. } else {
  414. if( !$wgUser->isAllowed($key) && !$this->disabled )
  415. continue;
  416. }
  417. $levels[] = $key;
  418. }
  419. $id = 'mwProtect-level-' . $action;
  420. $attribs = array(
  421. 'id' => $id,
  422. 'name' => $id,
  423. 'size' => count( $levels ),
  424. 'onchange' => 'ProtectionForm.updateLevels(this)',
  425. ) + $this->disabledAttrib;
  426. $out = Xml::openElement( 'select', $attribs );
  427. foreach( $levels as $key ) {
  428. $out .= Xml::option( $this->getOptionLabel( $key ), $key, $key == $selected );
  429. }
  430. $out .= Xml::closeElement( 'select' );
  431. return $out;
  432. }
  433. /**
  434. * Prepare the label for a protection selector option
  435. *
  436. * @param string $permission Permission required
  437. * @return string
  438. */
  439. private function getOptionLabel( $permission ) {
  440. if( $permission == '' ) {
  441. return wfMsg( 'protect-default' );
  442. } else {
  443. $key = "protect-level-{$permission}";
  444. $msg = wfMsg( $key );
  445. if( wfEmptyMsg( $key, $msg ) )
  446. $msg = wfMsg( 'protect-fallback', $permission );
  447. return $msg;
  448. }
  449. }
  450. function buildScript() {
  451. global $wgStylePath, $wgStyleVersion;
  452. return Xml::tags( 'script', array(
  453. 'type' => 'text/javascript',
  454. 'src' => $wgStylePath . "/common/protect.js?$wgStyleVersion.1" ), '' );
  455. }
  456. function buildCleanupScript() {
  457. global $wgRestrictionLevels, $wgGroupPermissions;
  458. $script = 'var wgCascadeableLevels=';
  459. $CascadeableLevels = array();
  460. foreach( $wgRestrictionLevels as $key ) {
  461. if ( (isset($wgGroupPermissions[$key]['protect']) && $wgGroupPermissions[$key]['protect']) || $key == 'protect' ) {
  462. $CascadeableLevels[] = "'" . Xml::escapeJsString( $key ) . "'";
  463. }
  464. }
  465. $script .= "[" . implode(',',$CascadeableLevels) . "];\n";
  466. $options = (object)array(
  467. 'tableId' => 'mw-protect-table-move',
  468. 'labelText' => wfMsg( 'protect-unchain' ),
  469. 'numTypes' => count($this->mApplicableTypes),
  470. 'existingMatch' => 1 == count( array_unique( $this->mExistingExpiry ) ),
  471. );
  472. $encOptions = Xml::encodeJsVar( $options );
  473. $script .= "ProtectionForm.init($encOptions)";
  474. return Xml::tags( 'script', array( 'type' => 'text/javascript' ), $script );
  475. }
  476. /**
  477. * @param OutputPage $out
  478. * @access private
  479. */
  480. function showLogExtract( &$out ) {
  481. # Show relevant lines from the protection log:
  482. $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'protect' ) ) );
  483. LogEventsList::showLogExtract( $out, 'protect', $this->mTitle->getPrefixedText() );
  484. }
  485. }