Merge "Avoid an infinite redirect in $wgSecureLogin handling"
[lhc/web/wiklou.git] / includes / installer / WebInstallerOutput.php
1 <?php
2 /**
3 * Output handler for the web installer.
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 Deployment
22 */
23
24 /**
25 * Output class modelled on OutputPage.
26 *
27 * I've opted to use a distinct class rather than derive from OutputPage here in
28 * the interests of separation of concerns: if we used a subclass, there would be
29 * quite a lot of things you could do in OutputPage that would break the installer,
30 * that wouldn't be immediately obvious.
31 *
32 * @ingroup Deployment
33 * @since 1.17
34 */
35 class WebInstallerOutput {
36
37 /**
38 * The WebInstaller object this WebInstallerOutput is used by.
39 *
40 * @var WebInstaller
41 */
42 public $parent;
43
44 /**
45 * Buffered contents that haven't been output yet
46 * @var string
47 */
48 private $contents = '';
49
50 /**
51 * Has the header (or short header) been output?
52 * @var bool
53 */
54 private $headerDone = false;
55
56 /**
57 * @var string
58 */
59 public $redirectTarget;
60
61 /**
62 * Does the current page need to allow being used as a frame?
63 * If not, X-Frame-Options will be output to forbid it.
64 *
65 * @var bool
66 */
67 public $allowFrames = false;
68
69 /**
70 * Whether to use the limited header (used during CC license callbacks)
71 * @var bool
72 */
73 private $useShortHeader = false;
74
75 /**
76 * @param WebInstaller $parent
77 */
78 public function __construct( WebInstaller $parent ) {
79 $this->parent = $parent;
80 }
81
82 /**
83 * @param string $html
84 */
85 public function addHTML( $html ) {
86 $this->contents .= $html;
87 $this->flush();
88 }
89
90 /**
91 * @param string $text
92 */
93 public function addWikiText( $text ) {
94 $this->addHTML( $this->parent->parse( $text ) );
95 }
96
97 /**
98 * @param string $html
99 */
100 public function addHTMLNoFlush( $html ) {
101 $this->contents .= $html;
102 }
103
104 /**
105 * @param string $url
106 *
107 * @throws MWException
108 */
109 public function redirect( $url ) {
110 if ( $this->headerDone ) {
111 throw new MWException( __METHOD__ . ' called after sending headers' );
112 }
113 $this->redirectTarget = $url;
114 }
115
116 public function output() {
117 $this->flush();
118 $this->outputFooter();
119 }
120
121 /**
122 * Get the raw vector CSS, flipping if needed
123 *
124 * @todo Possibly get rid of this function and use ResourceLoader in the manner it was
125 * designed to be used in, rather than just grabbing a list of filenames from it,
126 * and not properly handling such details as media types in module definitions.
127 *
128 * @param string $dir 'ltr' or 'rtl'
129 *
130 * @return string
131 */
132 public function getCSS( $dir ) {
133 // All CSS files these modules reference will be concatenated in sequence
134 // and loaded as one file.
135 $moduleNames = array(
136 'mediawiki.legacy.shared',
137 'mediawiki.skinning.interface',
138 'skins.vector.styles',
139 'mediawiki.legacy.config',
140 );
141
142 $prepend = '';
143 $css = '';
144
145 $resourceLoader = new ResourceLoader();
146 foreach ( $moduleNames as $moduleName ) {
147 /** @var ResourceLoaderFileModule $module */
148 $module = $resourceLoader->getModule( $moduleName );
149 $cssFileNames = $module->getAllStyleFiles();
150
151 wfSuppressWarnings();
152 foreach ( $cssFileNames as $cssFileName ) {
153 if ( !file_exists( $cssFileName ) ) {
154 $prepend .= ResourceLoader::makeComment( "Unable to find $cssFileName." );
155 continue;
156 }
157
158 if ( !is_readable( $cssFileName ) ) {
159 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName. " .
160 "Please check file permissions." );
161 continue;
162 }
163
164 try {
165
166 if ( preg_match( '/\.less$/', $cssFileName ) ) {
167 // Run the LESS compiler for *.less files (bug 55589)
168 $compiler = ResourceLoader::getLessCompiler();
169 $cssFileContents = $compiler->compileFile( $cssFileName );
170 } else {
171 // Regular CSS file
172 $cssFileContents = file_get_contents( $cssFileName );
173 }
174
175 if ( $cssFileContents ) {
176 // Rewrite URLs, though don't bother embedding images. While static image
177 // files may be cached, CSS returned by this function is definitely not.
178 $cssDirName = dirname( $cssFileName );
179 $css .= CSSMin::remap(
180 /* source */ $cssFileContents,
181 /* local */ $cssDirName,
182 /* remote */ '..' . str_replace(
183 array( $GLOBALS['IP'], DIRECTORY_SEPARATOR ),
184 array( '', '/' ),
185 $cssDirName
186 ),
187 /* embedData */ false
188 );
189 } else {
190 $prepend .= ResourceLoader::makeComment( "Unable to read $cssFileName." );
191 }
192 } catch ( Exception $e ) {
193 $prepend .= ResourceLoader::formatException( $e );
194 }
195
196 $css .= "\n";
197 }
198 wfRestoreWarnings();
199 }
200
201 $css = $prepend . $css;
202
203 if ( $dir == 'rtl' ) {
204 $css = CSSJanus::transform( $css, true );
205 }
206
207 return $css;
208 }
209
210 /**
211 * "<link>" to index.php?css=foobar for the "<head>"
212 *
213 * @return string
214 */
215 private function getCssUrl() {
216 return Html::linkedStyle( $_SERVER['PHP_SELF'] . '?css=' . $this->getDir() );
217 }
218
219 public function useShortHeader( $use = true ) {
220 $this->useShortHeader = $use;
221 }
222
223 public function allowFrames( $allow = true ) {
224 $this->allowFrames = $allow;
225 }
226
227 public function flush() {
228 if ( !$this->headerDone ) {
229 $this->outputHeader();
230 }
231 if ( !$this->redirectTarget && strlen( $this->contents ) ) {
232 echo $this->contents;
233 flush();
234 $this->contents = '';
235 }
236 }
237
238 /**
239 * @return string
240 */
241 public function getDir() {
242 global $wgLang;
243
244 return is_object( $wgLang ) ? $wgLang->getDir() : 'ltr';
245 }
246
247 /**
248 * @return string
249 */
250 public function getLanguageCode() {
251 global $wgLang;
252
253 return is_object( $wgLang ) ? $wgLang->getCode() : 'en';
254 }
255
256 /**
257 * @return string[]
258 */
259 public function getHeadAttribs() {
260 return array(
261 'dir' => $this->getDir(),
262 'lang' => $this->getLanguageCode(),
263 );
264 }
265
266 /**
267 * Get whether the header has been output
268 *
269 * @return bool
270 */
271 public function headerDone() {
272 return $this->headerDone;
273 }
274
275 public function outputHeader() {
276 $this->headerDone = true;
277 $this->parent->request->response()->header( 'Content-Type: text/html; charset=utf-8' );
278
279 if ( !$this->allowFrames ) {
280 $this->parent->request->response()->header( 'X-Frame-Options: DENY' );
281 }
282
283 if ( $this->redirectTarget ) {
284 $this->parent->request->response()->header( 'Location: ' . $this->redirectTarget );
285
286 return;
287 }
288
289 if ( $this->useShortHeader ) {
290 $this->outputShortHeader();
291
292 return;
293 }
294 ?>
295 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
296 <head>
297 <meta name="robots" content="noindex, nofollow" />
298 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
299 <title><?php $this->outputTitle(); ?></title>
300 <?php echo $this->getCssUrl() . "\n"; ?>
301 <?php echo $this->getJQuery() . "\n"; ?>
302 <?php echo Html::linkedScript( '../skins/common/config.js' ) . "\n"; ?>
303 </head>
304
305 <?php echo Html::openElement( 'body', array( 'class' => $this->getDir() ) ) . "\n"; ?>
306 <div id="mw-page-base"></div>
307 <div id="mw-head-base"></div>
308 <div id="content">
309 <div id="bodyContent">
310
311 <h1><?php $this->outputTitle(); ?></h1>
312 <?php
313 }
314
315 public function outputFooter() {
316 if ( $this->useShortHeader ) {
317 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
318
319 return;
320 }
321 ?>
322
323 </div></div>
324
325 <div id="mw-panel">
326 <div class="portal" id="p-logo">
327 <a style="background-image: url(../skins/common/images/mediawiki.png);"
328 href="https://www.mediawiki.org/"
329 title="Main Page"></a>
330 </div>
331 <div class="portal"><div class="body">
332 <?php
333 echo $this->parent->parse( wfMessage( 'config-sidebar' )->plain(), true );
334 ?>
335 </div></div>
336 </div>
337
338 <?php
339 echo Html::closeElement( 'body' ) . Html::closeElement( 'html' );
340 }
341
342 public function outputShortHeader() {
343 ?>
344 <?php echo Html::htmlHeader( $this->getHeadAttribs() ); ?>
345 <head>
346 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
347 <meta name="robots" content="noindex, nofollow" />
348 <title><?php $this->outputTitle(); ?></title>
349 <?php echo $this->getCssUrl() . "\n"; ?>
350 <?php echo $this->getJQuery(); ?>
351 <?php echo Html::linkedScript( '../skins/common/config.js' ); ?>
352 </head>
353
354 <body style="background-image: none">
355 <?php
356 }
357
358 public function outputTitle() {
359 global $wgVersion;
360 echo wfMessage( 'config-title', $wgVersion )->escaped();
361 }
362
363 /**
364 * @return string
365 */
366 public function getJQuery() {
367 return Html::linkedScript( "../resources/lib/jquery/jquery.js" );
368 }
369
370 }