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