* Use standard method to check user permissions
[lhc/web/wiklou.git] / includes / specials / SpecialLockdb.php
1 <?php
2 /**
3 * Implements Special:Lockdb
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A form to make the database readonly (eg for maintenance purposes).
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialLockdb extends SpecialPage {
30 var $reason = '';
31
32 public function __construct() {
33 parent::__construct( 'Lockdb', 'siteadmin' );
34 }
35
36 public function execute( $par ) {
37 global $wgUser, $wgOut, $wgRequest;
38
39 $this->setHeaders();
40
41 # Permission check
42 if( !$this->userCanExecute( $wgUser ) ) {
43 $this->displayRestrictionError();
44 return;
45 }
46
47 $this->outputHeader();
48
49 # If the lock file isn't writable, we can do sweet bugger all
50 global $wgReadOnlyFile;
51 if( !is_writable( dirname( $wgReadOnlyFile ) ) ) {
52 self::notWritable();
53 return;
54 }
55
56 $action = $wgRequest->getVal( 'action' );
57 $this->reason = $wgRequest->getVal( 'wpLockReason', '' );
58
59 if ( $action == 'success' ) {
60 $this->showSuccess();
61 } elseif ( $action == 'submit' && $wgRequest->wasPosted() &&
62 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
63 $this->doSubmit();
64 } else {
65 $this->showForm();
66 }
67 }
68
69 private function showForm( $err = '' ) {
70 global $wgOut, $wgUser;
71
72 $wgOut->addWikiMsg( 'lockdbtext' );
73
74 if ( $err != '' ) {
75 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
76 $wgOut->addHTML( '<p class="error">' . htmlspecialchars( $err ) . "</p>\n" );
77 }
78
79 $wgOut->addHTML(
80 Html::openElement( 'form', array( 'id' => 'lockdb', 'method' => 'POST',
81 'action' => $this->getTitle()->getLocalURL( 'action=submit' ) ) ). "\n" .
82 wfMsgHtml( 'enterlockreason' ) . ":\n" .
83 Html::textarea( 'wpLockReason', $this->reason, array( 'rows' => 4 ) ). "
84 <table>
85 <tr>
86 " . Html::openElement( 'td', array( 'style' => 'text-align:right' ) ) . "
87 " . Html::input( 'wpLockConfirm', null, 'checkbox' ) . "
88 </td>
89 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) .
90 wfMsgHtml( 'lockconfirm' ) . "</td>
91 </tr>
92 <tr>
93 <td>&#160;</td>
94 " . Html::openElement( 'td', array( 'style' => 'text-align:left' ) ) . "
95 " . Html::input( 'wpLock', wfMsg( 'lockbtn' ), 'submit' ) . "
96 </td>
97 </tr>
98 </table>\n" .
99 Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n" .
100 Html::closeElement( 'form' )
101 );
102
103 }
104
105 private function doSubmit() {
106 global $wgOut, $wgUser, $wgContLang, $wgRequest;
107 global $wgReadOnlyFile;
108
109 if ( ! $wgRequest->getCheck( 'wpLockConfirm' ) ) {
110 $this->showForm( wfMsg( 'locknoconfirm' ) );
111 return;
112 }
113
114 wfSuppressWarnings();
115 $fp = fopen( $wgReadOnlyFile, 'w' );
116 wfRestoreWarnings();
117
118 if ( false === $fp ) {
119 # This used to show a file not found error, but the likeliest reason for fopen()
120 # to fail at this point is insufficient permission to write to the file...good old
121 # is_writable() is plain wrong in some cases, it seems...
122 self::notWritable();
123 return;
124 }
125 fwrite( $fp, $this->reason );
126 $timestamp = wfTimestampNow();
127 fwrite( $fp, "\n<p>" . wfMsgExt(
128 'lockedbyandtime',
129 array( 'content', 'parsemag' ),
130 $wgUser->getName(),
131 $wgContLang->date( $timestamp ),
132 $wgContLang->time( $timestamp )
133 ) . "</p>\n" );
134 fclose( $fp );
135
136 $wgOut->redirect( $this->getTitle()->getFullURL( 'action=success' ) );
137 }
138
139 private function showSuccess() {
140 global $wgOut;
141
142 $wgOut->setPagetitle( wfMsg( 'lockdb' ) );
143 $wgOut->setSubtitle( wfMsg( 'lockdbsuccesssub' ) );
144 $wgOut->addWikiMsg( 'lockdbsuccesstext' );
145 }
146
147 public static function notWritable() {
148 global $wgOut;
149 $wgOut->showErrorPage( 'lockdb', 'lockfilenotwritable' );
150 }
151 }