Fixed dependencies for jquery.collapsibleTabs
[lhc/web/wiklou.git] / includes / filebackend / FSFileBackend.php
1 <?php
2 /**
3 * File system based backend.
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 FileBackend
22 * @author Aaron Schulz
23 */
24
25 /**
26 * @brief Class for a file system (FS) based file backend.
27 *
28 * All "containers" each map to a directory under the backend's base directory.
29 * For backwards-compatibility, some container paths can be set to custom paths.
30 * The wiki ID will not be used in any custom paths, so this should be avoided.
31 *
32 * Having directories with thousands of files will diminish performance.
33 * Sharding can be accomplished by using FileRepo-style hash paths.
34 *
35 * Status messages should avoid mentioning the internal FS paths.
36 * PHP warnings are assumed to be logged rather than output.
37 *
38 * @ingroup FileBackend
39 * @since 1.19
40 */
41 class FSFileBackend extends FileBackendStore {
42 protected $basePath; // string; directory holding the container directories
43 /** @var Array Map of container names to root paths */
44 protected $containerPaths = array(); // for custom container paths
45 protected $fileMode; // integer; file permission mode
46 protected $fileOwner; // string; required OS username to own files
47 protected $currentUser; // string; OS username running this script
48
49 protected $hadWarningErrors = array();
50
51 /**
52 * @see FileBackendStore::__construct()
53 * Additional $config params include:
54 * - basePath : File system directory that holds containers.
55 * - containerPaths : Map of container names to custom file system directories.
56 * This should only be used for backwards-compatibility.
57 * - fileMode : Octal UNIX file permissions to use on files stored.
58 */
59 public function __construct( array $config ) {
60 parent::__construct( $config );
61
62 // Remove any possible trailing slash from directories
63 if ( isset( $config['basePath'] ) ) {
64 $this->basePath = rtrim( $config['basePath'], '/' ); // remove trailing slash
65 } else {
66 $this->basePath = null; // none; containers must have explicit paths
67 }
68
69 if ( isset( $config['containerPaths'] ) ) {
70 $this->containerPaths = (array)$config['containerPaths'];
71 foreach ( $this->containerPaths as &$path ) {
72 $path = rtrim( $path, '/' ); // remove trailing slash
73 }
74 }
75
76 $this->fileMode = isset( $config['fileMode'] ) ? $config['fileMode'] : 0644;
77 if ( isset( $config['fileOwner'] ) && function_exists( 'posix_getuid' ) ) {
78 $this->fileOwner = $config['fileOwner'];
79 $info = posix_getpwuid( posix_getuid() );
80 $this->currentUser = $info['name']; // cache this, assuming it doesn't change
81 }
82 }
83
84 /**
85 * @see FileBackendStore::resolveContainerPath()
86 * @param $container string
87 * @param $relStoragePath string
88 * @return null|string
89 */
90 protected function resolveContainerPath( $container, $relStoragePath ) {
91 // Check that container has a root directory
92 if ( isset( $this->containerPaths[$container] ) || isset( $this->basePath ) ) {
93 // Check for sane relative paths (assume the base paths are OK)
94 if ( $this->isLegalRelPath( $relStoragePath ) ) {
95 return $relStoragePath;
96 }
97 }
98 return null;
99 }
100
101 /**
102 * Sanity check a relative file system path for validity
103 *
104 * @param $path string Normalized relative path
105 * @return bool
106 */
107 protected function isLegalRelPath( $path ) {
108 // Check for file names longer than 255 chars
109 if ( preg_match( '![^/]{256}!', $path ) ) { // ext3/NTFS
110 return false;
111 }
112 if ( wfIsWindows() ) { // NTFS
113 return !preg_match( '![:*?"<>|]!', $path );
114 } else {
115 return true;
116 }
117 }
118
119 /**
120 * Given the short (unresolved) and full (resolved) name of
121 * a container, return the file system path of the container.
122 *
123 * @param $shortCont string
124 * @param $fullCont string
125 * @return string|null
126 */
127 protected function containerFSRoot( $shortCont, $fullCont ) {
128 if ( isset( $this->containerPaths[$shortCont] ) ) {
129 return $this->containerPaths[$shortCont];
130 } elseif ( isset( $this->basePath ) ) {
131 return "{$this->basePath}/{$fullCont}";
132 }
133 return null; // no container base path defined
134 }
135
136 /**
137 * Get the absolute file system path for a storage path
138 *
139 * @param $storagePath string Storage path
140 * @return string|null
141 */
142 protected function resolveToFSPath( $storagePath ) {
143 list( $fullCont, $relPath ) = $this->resolveStoragePathReal( $storagePath );
144 if ( $relPath === null ) {
145 return null; // invalid
146 }
147 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $storagePath );
148 $fsPath = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
149 if ( $relPath != '' ) {
150 $fsPath .= "/{$relPath}";
151 }
152 return $fsPath;
153 }
154
155 /**
156 * @see FileBackendStore::isPathUsableInternal()
157 * @return bool
158 */
159 public function isPathUsableInternal( $storagePath ) {
160 $fsPath = $this->resolveToFSPath( $storagePath );
161 if ( $fsPath === null ) {
162 return false; // invalid
163 }
164 $parentDir = dirname( $fsPath );
165
166 if ( file_exists( $fsPath ) ) {
167 $ok = is_file( $fsPath ) && is_writable( $fsPath );
168 } else {
169 $ok = is_dir( $parentDir ) && is_writable( $parentDir );
170 }
171
172 if ( $this->fileOwner !== null && $this->currentUser !== $this->fileOwner ) {
173 $ok = false;
174 trigger_error( __METHOD__ . ": PHP process owner is not '{$this->fileOwner}'." );
175 }
176
177 return $ok;
178 }
179
180 /**
181 * @see FileBackendStore::doStoreInternal()
182 * @return Status
183 */
184 protected function doStoreInternal( array $params ) {
185 $status = Status::newGood();
186
187 $dest = $this->resolveToFSPath( $params['dst'] );
188 if ( $dest === null ) {
189 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
190 return $status;
191 }
192
193 if ( file_exists( $dest ) ) {
194 if ( !empty( $params['overwrite'] ) ) {
195 $ok = unlink( $dest );
196 if ( !$ok ) {
197 $status->fatal( 'backend-fail-delete', $params['dst'] );
198 return $status;
199 }
200 } else {
201 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
202 return $status;
203 }
204 }
205
206 if ( !empty( $params['async'] ) ) { // deferred
207 $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
208 wfEscapeShellArg( $this->cleanPathSlashes( $params['src'] ) ),
209 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
210 ) );
211 $status->value = new FSFileOpHandle( $this, $params, 'Store', $cmd, $dest );
212 } else { // immediate write
213 $ok = copy( $params['src'], $dest );
214 // In some cases (at least over NFS), copy() returns true when it fails
215 if ( !$ok || ( filesize( $params['src'] ) !== filesize( $dest ) ) ) {
216 if ( $ok ) { // PHP bug
217 unlink( $dest ); // remove broken file
218 trigger_error( __METHOD__ . ": copy() failed but returned true." );
219 }
220 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
221 return $status;
222 }
223 $this->chmod( $dest );
224 }
225
226 return $status;
227 }
228
229 /**
230 * @see FSFileBackend::doExecuteOpHandlesInternal()
231 */
232 protected function _getResponseStore( $errors, Status $status, array $params, $cmd ) {
233 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
234 $status->fatal( 'backend-fail-store', $params['src'], $params['dst'] );
235 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
236 }
237 }
238
239 /**
240 * @see FileBackendStore::doCopyInternal()
241 * @return Status
242 */
243 protected function doCopyInternal( array $params ) {
244 $status = Status::newGood();
245
246 $source = $this->resolveToFSPath( $params['src'] );
247 if ( $source === null ) {
248 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
249 return $status;
250 }
251
252 $dest = $this->resolveToFSPath( $params['dst'] );
253 if ( $dest === null ) {
254 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
255 return $status;
256 }
257
258 if ( file_exists( $dest ) ) {
259 if ( !empty( $params['overwrite'] ) ) {
260 $ok = unlink( $dest );
261 if ( !$ok ) {
262 $status->fatal( 'backend-fail-delete', $params['dst'] );
263 return $status;
264 }
265 } else {
266 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
267 return $status;
268 }
269 }
270
271 if ( !empty( $params['async'] ) ) { // deferred
272 $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
273 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
274 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
275 ) );
276 $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd, $dest );
277 } else { // immediate write
278 $ok = copy( $source, $dest );
279 // In some cases (at least over NFS), copy() returns true when it fails
280 if ( !$ok || ( filesize( $source ) !== filesize( $dest ) ) ) {
281 if ( $ok ) { // PHP bug
282 unlink( $dest ); // remove broken file
283 trigger_error( __METHOD__ . ": copy() failed but returned true." );
284 }
285 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
286 return $status;
287 }
288 $this->chmod( $dest );
289 }
290
291 return $status;
292 }
293
294 /**
295 * @see FSFileBackend::doExecuteOpHandlesInternal()
296 */
297 protected function _getResponseCopy( $errors, Status $status, array $params, $cmd ) {
298 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
299 $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
300 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
301 }
302 }
303
304 /**
305 * @see FileBackendStore::doMoveInternal()
306 * @return Status
307 */
308 protected function doMoveInternal( array $params ) {
309 $status = Status::newGood();
310
311 $source = $this->resolveToFSPath( $params['src'] );
312 if ( $source === null ) {
313 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
314 return $status;
315 }
316
317 $dest = $this->resolveToFSPath( $params['dst'] );
318 if ( $dest === null ) {
319 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
320 return $status;
321 }
322
323 if ( file_exists( $dest ) ) {
324 if ( !empty( $params['overwrite'] ) ) {
325 // Windows does not support moving over existing files
326 if ( wfIsWindows() ) {
327 $ok = unlink( $dest );
328 if ( !$ok ) {
329 $status->fatal( 'backend-fail-delete', $params['dst'] );
330 return $status;
331 }
332 }
333 } else {
334 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
335 return $status;
336 }
337 }
338
339 if ( !empty( $params['async'] ) ) { // deferred
340 $cmd = implode( ' ', array( wfIsWindows() ? 'MOVE' : 'mv',
341 wfEscapeShellArg( $this->cleanPathSlashes( $source ) ),
342 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
343 ) );
344 $status->value = new FSFileOpHandle( $this, $params, 'Move', $cmd );
345 } else { // immediate write
346 $ok = rename( $source, $dest );
347 clearstatcache(); // file no longer at source
348 if ( !$ok ) {
349 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
350 return $status;
351 }
352 }
353
354 return $status;
355 }
356
357 /**
358 * @see FSFileBackend::doExecuteOpHandlesInternal()
359 */
360 protected function _getResponseMove( $errors, Status $status, array $params, $cmd ) {
361 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
362 $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
363 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
364 }
365 }
366
367 /**
368 * @see FileBackendStore::doDeleteInternal()
369 * @return Status
370 */
371 protected function doDeleteInternal( array $params ) {
372 $status = Status::newGood();
373
374 $source = $this->resolveToFSPath( $params['src'] );
375 if ( $source === null ) {
376 $status->fatal( 'backend-fail-invalidpath', $params['src'] );
377 return $status;
378 }
379
380 if ( !is_file( $source ) ) {
381 if ( empty( $params['ignoreMissingSource'] ) ) {
382 $status->fatal( 'backend-fail-delete', $params['src'] );
383 }
384 return $status; // do nothing; either OK or bad status
385 }
386
387 if ( !empty( $params['async'] ) ) { // deferred
388 $cmd = implode( ' ', array( wfIsWindows() ? 'DEL' : 'unlink',
389 wfEscapeShellArg( $this->cleanPathSlashes( $source ) )
390 ) );
391 $status->value = new FSFileOpHandle( $this, $params, 'Copy', $cmd );
392 } else { // immediate write
393 $ok = unlink( $source );
394 if ( !$ok ) {
395 $status->fatal( 'backend-fail-delete', $params['src'] );
396 return $status;
397 }
398 }
399
400 return $status;
401 }
402
403 /**
404 * @see FSFileBackend::doExecuteOpHandlesInternal()
405 */
406 protected function _getResponseDelete( $errors, Status $status, array $params, $cmd ) {
407 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
408 $status->fatal( 'backend-fail-delete', $params['src'] );
409 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
410 }
411 }
412
413 /**
414 * @see FileBackendStore::doCreateInternal()
415 * @return Status
416 */
417 protected function doCreateInternal( array $params ) {
418 $status = Status::newGood();
419
420 $dest = $this->resolveToFSPath( $params['dst'] );
421 if ( $dest === null ) {
422 $status->fatal( 'backend-fail-invalidpath', $params['dst'] );
423 return $status;
424 }
425
426 if ( file_exists( $dest ) ) {
427 if ( !empty( $params['overwrite'] ) ) {
428 $ok = unlink( $dest );
429 if ( !$ok ) {
430 $status->fatal( 'backend-fail-delete', $params['dst'] );
431 return $status;
432 }
433 } else {
434 $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
435 return $status;
436 }
437 }
438
439 if ( !empty( $params['async'] ) ) { // deferred
440 $tempFile = TempFSFile::factory( 'create_', 'tmp' );
441 if ( !$tempFile ) {
442 $status->fatal( 'backend-fail-create', $params['dst'] );
443 return $status;
444 }
445 $bytes = file_put_contents( $tempFile->getPath(), $params['content'] );
446 if ( $bytes === false ) {
447 $status->fatal( 'backend-fail-create', $params['dst'] );
448 return $status;
449 }
450 $cmd = implode( ' ', array( wfIsWindows() ? 'COPY' : 'cp',
451 wfEscapeShellArg( $this->cleanPathSlashes( $tempFile->getPath() ) ),
452 wfEscapeShellArg( $this->cleanPathSlashes( $dest ) )
453 ) );
454 $status->value = new FSFileOpHandle( $this, $params, 'Create', $cmd, $dest );
455 $tempFile->bind( $status->value );
456 } else { // immediate write
457 $bytes = file_put_contents( $dest, $params['content'] );
458 if ( $bytes === false ) {
459 $status->fatal( 'backend-fail-create', $params['dst'] );
460 return $status;
461 }
462 $this->chmod( $dest );
463 }
464
465 return $status;
466 }
467
468 /**
469 * @see FSFileBackend::doExecuteOpHandlesInternal()
470 */
471 protected function _getResponseCreate( $errors, Status $status, array $params, $cmd ) {
472 if ( $errors !== '' && !( wfIsWindows() && $errors[0] === " " ) ) {
473 $status->fatal( 'backend-fail-create', $params['dst'] );
474 trigger_error( "$cmd\n$errors", E_USER_WARNING ); // command output
475 }
476 }
477
478 /**
479 * @see FileBackendStore::doPrepareInternal()
480 * @return Status
481 */
482 protected function doPrepareInternal( $fullCont, $dirRel, array $params ) {
483 $status = Status::newGood();
484 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
485 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
486 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
487 $existed = is_dir( $dir ); // already there?
488 if ( !wfMkdirParents( $dir ) ) { // make directory and its parents
489 $status->fatal( 'directorycreateerror', $params['dir'] ); // fails on races
490 } elseif ( !is_writable( $dir ) ) {
491 $status->fatal( 'directoryreadonlyerror', $params['dir'] );
492 } elseif ( !is_readable( $dir ) ) {
493 $status->fatal( 'directorynotreadableerror', $params['dir'] );
494 }
495 if ( is_dir( $dir ) && !$existed ) {
496 // Respect any 'noAccess' or 'noListing' flags...
497 $status->merge( $this->doSecureInternal( $fullCont, $dirRel, $params ) );
498 }
499 return $status;
500 }
501
502 /**
503 * @see FileBackendStore::doSecureInternal()
504 * @return Status
505 */
506 protected function doSecureInternal( $fullCont, $dirRel, array $params ) {
507 $status = Status::newGood();
508 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
509 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
510 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
511 // Seed new directories with a blank index.html, to prevent crawling...
512 if ( !empty( $params['noListing'] ) && !file_exists( "{$dir}/index.html" ) ) {
513 $bytes = file_put_contents( "{$dir}/index.html", $this->indexHtmlPrivate() );
514 if ( $bytes === false ) {
515 $status->fatal( 'backend-fail-create', $params['dir'] . '/index.html' );
516 return $status;
517 }
518 }
519 // Add a .htaccess file to the root of the container...
520 if ( !empty( $params['noAccess'] ) && !file_exists( "{$contRoot}/.htaccess" ) ) {
521 $bytes = file_put_contents( "{$contRoot}/.htaccess", $this->htaccessPrivate() );
522 if ( $bytes === false ) {
523 $storeDir = "mwstore://{$this->name}/{$shortCont}";
524 $status->fatal( 'backend-fail-create', "{$storeDir}/.htaccess" );
525 return $status;
526 }
527 }
528 return $status;
529 }
530
531 /**
532 * @see FileBackendStore::doPublishInternal()
533 * @return Status
534 */
535 protected function doPublishInternal( $fullCont, $dirRel, array $params ) {
536 $status = Status::newGood();
537 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
538 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
539 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
540 // Unseed new directories with a blank index.html, to allow crawling...
541 if ( !empty( $params['listing'] ) && is_file( "{$dir}/index.html" ) ) {
542 $exists = ( file_get_contents( "{$dir}/index.html" ) === $this->indexHtmlPrivate() );
543 if ( $exists && !unlink( "{$dir}/index.html" ) ) { // reverse secure()
544 $status->fatal( 'backend-fail-delete', $params['dir'] . '/index.html' );
545 return $status;
546 }
547 }
548 // Remove the .htaccess file from the root of the container...
549 if ( !empty( $params['access'] ) && is_file( "{$contRoot}/.htaccess" ) ) {
550 $exists = ( file_get_contents( "{$contRoot}/.htaccess" ) === $this->htaccessPrivate() );
551 if ( $exists && !unlink( "{$contRoot}/.htaccess" ) ) { // reverse secure()
552 $storeDir = "mwstore://{$this->name}/{$shortCont}";
553 $status->fatal( 'backend-fail-delete', "{$storeDir}/.htaccess" );
554 return $status;
555 }
556 }
557 return $status;
558 }
559
560 /**
561 * @see FileBackendStore::doCleanInternal()
562 * @return Status
563 */
564 protected function doCleanInternal( $fullCont, $dirRel, array $params ) {
565 $status = Status::newGood();
566 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
567 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
568 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
569 wfSuppressWarnings();
570 if ( is_dir( $dir ) ) {
571 rmdir( $dir ); // remove directory if empty
572 }
573 wfRestoreWarnings();
574 return $status;
575 }
576
577 /**
578 * @see FileBackendStore::doFileExists()
579 * @return array|bool|null
580 */
581 protected function doGetFileStat( array $params ) {
582 $source = $this->resolveToFSPath( $params['src'] );
583 if ( $source === null ) {
584 return false; // invalid storage path
585 }
586
587 $this->trapWarnings(); // don't trust 'false' if there were errors
588 $stat = is_file( $source ) ? stat( $source ) : false; // regular files only
589 $hadError = $this->untrapWarnings();
590
591 if ( $stat ) {
592 return array(
593 'mtime' => wfTimestamp( TS_MW, $stat['mtime'] ),
594 'size' => $stat['size']
595 );
596 } elseif ( !$hadError ) {
597 return false; // file does not exist
598 } else {
599 return null; // failure
600 }
601 }
602
603 /**
604 * @see FileBackendStore::doClearCache()
605 */
606 protected function doClearCache( array $paths = null ) {
607 clearstatcache(); // clear the PHP file stat cache
608 }
609
610 /**
611 * @see FileBackendStore::doDirectoryExists()
612 * @return bool|null
613 */
614 protected function doDirectoryExists( $fullCont, $dirRel, array $params ) {
615 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
616 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
617 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
618
619 $this->trapWarnings(); // don't trust 'false' if there were errors
620 $exists = is_dir( $dir );
621 $hadError = $this->untrapWarnings();
622
623 return $hadError ? null : $exists;
624 }
625
626 /**
627 * @see FileBackendStore::getDirectoryListInternal()
628 * @return Array|null
629 */
630 public function getDirectoryListInternal( $fullCont, $dirRel, array $params ) {
631 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
632 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
633 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
634 $exists = is_dir( $dir );
635 if ( !$exists ) {
636 wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" );
637 return array(); // nothing under this dir
638 } elseif ( !is_readable( $dir ) ) {
639 wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" );
640 return null; // bad permissions?
641 }
642 return new FSFileBackendDirList( $dir, $params );
643 }
644
645 /**
646 * @see FileBackendStore::getFileListInternal()
647 * @return array|FSFileBackendFileList|null
648 */
649 public function getFileListInternal( $fullCont, $dirRel, array $params ) {
650 list( $b, $shortCont, $r ) = FileBackend::splitStoragePath( $params['dir'] );
651 $contRoot = $this->containerFSRoot( $shortCont, $fullCont ); // must be valid
652 $dir = ( $dirRel != '' ) ? "{$contRoot}/{$dirRel}" : $contRoot;
653 $exists = is_dir( $dir );
654 if ( !$exists ) {
655 wfDebug( __METHOD__ . "() given directory does not exist: '$dir'\n" );
656 return array(); // nothing under this dir
657 } elseif ( !is_readable( $dir ) ) {
658 wfDebug( __METHOD__ . "() given directory is unreadable: '$dir'\n" );
659 return null; // bad permissions?
660 }
661 return new FSFileBackendFileList( $dir, $params );
662 }
663
664 /**
665 * @see FileBackendStore::getLocalReference()
666 * @return FSFile|null
667 */
668 public function getLocalReference( array $params ) {
669 $source = $this->resolveToFSPath( $params['src'] );
670 if ( $source === null ) {
671 return null;
672 }
673 return new FSFile( $source );
674 }
675
676 /**
677 * @see FileBackendStore::getLocalCopy()
678 * @return null|TempFSFile
679 */
680 public function getLocalCopy( array $params ) {
681 $source = $this->resolveToFSPath( $params['src'] );
682 if ( $source === null ) {
683 return null;
684 }
685
686 // Create a new temporary file with the same extension...
687 $ext = FileBackend::extensionFromPath( $params['src'] );
688 $tmpFile = TempFSFile::factory( 'localcopy_', $ext );
689 if ( !$tmpFile ) {
690 return null;
691 }
692 $tmpPath = $tmpFile->getPath();
693
694 // Copy the source file over the temp file
695 $ok = copy( $source, $tmpPath );
696 if ( !$ok ) {
697 return null;
698 }
699
700 $this->chmod( $tmpPath );
701
702 return $tmpFile;
703 }
704
705 /**
706 * @see FileBackendStore::directoriesAreVirtual()
707 * @return bool
708 */
709 protected function directoriesAreVirtual() {
710 return false;
711 }
712
713 /**
714 * @see FileBackendStore::doExecuteOpHandlesInternal()
715 * @return Array List of corresponding Status objects
716 */
717 protected function doExecuteOpHandlesInternal( array $fileOpHandles ) {
718 $statuses = array();
719
720 $pipes = array();
721 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
722 $pipes[$index] = popen( "{$fileOpHandle->cmd} 2>&1", 'r' );
723 }
724
725 $errs = array();
726 foreach ( $pipes as $index => $pipe ) {
727 // Result will be empty on success in *NIX. On Windows,
728 // it may be something like " 1 file(s) [copied|moved].".
729 $errs[$index] = stream_get_contents( $pipe );
730 fclose( $pipe );
731 }
732
733 foreach ( $fileOpHandles as $index => $fileOpHandle ) {
734 $status = Status::newGood();
735 $function = '_getResponse' . $fileOpHandle->call;
736 $this->$function( $errs[$index], $status, $fileOpHandle->params, $fileOpHandle->cmd );
737 $statuses[$index] = $status;
738 if ( $status->isOK() && $fileOpHandle->chmodPath ) {
739 $this->chmod( $fileOpHandle->chmodPath );
740 }
741 }
742
743 clearstatcache(); // files changed
744 return $statuses;
745 }
746
747 /**
748 * Chmod a file, suppressing the warnings
749 *
750 * @param $path string Absolute file system path
751 * @return bool Success
752 */
753 protected function chmod( $path ) {
754 wfSuppressWarnings();
755 $ok = chmod( $path, $this->fileMode );
756 wfRestoreWarnings();
757
758 return $ok;
759 }
760
761 /**
762 * Return the text of an index.html file to hide directory listings
763 *
764 * @return string
765 */
766 protected function indexHtmlPrivate() {
767 return '';
768 }
769
770 /**
771 * Return the text of a .htaccess file to make a directory private
772 *
773 * @return string
774 */
775 protected function htaccessPrivate() {
776 return "Deny from all\n";
777 }
778
779 /**
780 * Clean up directory separators for the given OS
781 *
782 * @param $path string FS path
783 * @return string
784 */
785 protected function cleanPathSlashes( $path ) {
786 return wfIsWindows() ? strtr( $path, '/', '\\' ) : $path;
787 }
788
789 /**
790 * Listen for E_WARNING errors and track whether any happen
791 *
792 * @return bool
793 */
794 protected function trapWarnings() {
795 $this->hadWarningErrors[] = false; // push to stack
796 set_error_handler( array( $this, 'handleWarning' ), E_WARNING );
797 return false; // invoke normal PHP error handler
798 }
799
800 /**
801 * Stop listening for E_WARNING errors and return true if any happened
802 *
803 * @return bool
804 */
805 protected function untrapWarnings() {
806 restore_error_handler(); // restore previous handler
807 return array_pop( $this->hadWarningErrors ); // pop from stack
808 }
809
810 /**
811 * @return bool
812 */
813 private function handleWarning() {
814 $this->hadWarningErrors[count( $this->hadWarningErrors ) - 1] = true;
815 return true; // suppress from PHP handler
816 }
817 }
818
819 /**
820 * @see FileBackendStoreOpHandle
821 */
822 class FSFileOpHandle extends FileBackendStoreOpHandle {
823 public $cmd; // string; shell command
824 public $chmodPath; // string; file to chmod
825
826 /**
827 * @param $backend
828 * @param $params array
829 * @param $call
830 * @param $cmd
831 * @param $chmodPath null
832 */
833 public function __construct( $backend, array $params, $call, $cmd, $chmodPath = null ) {
834 $this->backend = $backend;
835 $this->params = $params;
836 $this->call = $call;
837 $this->cmd = $cmd;
838 $this->chmodPath = $chmodPath;
839 }
840 }
841
842 /**
843 * Wrapper around RecursiveDirectoryIterator/DirectoryIterator that
844 * catches exception or does any custom behavoir that we may want.
845 * Do not use this class from places outside FSFileBackend.
846 *
847 * @ingroup FileBackend
848 */
849 abstract class FSFileBackendList implements Iterator {
850 /** @var Iterator */
851 protected $iter;
852 protected $suffixStart; // integer
853 protected $pos = 0; // integer
854 /** @var Array */
855 protected $params = array();
856
857 /**
858 * @param $dir string file system directory
859 * @param $params array
860 */
861 public function __construct( $dir, array $params ) {
862 $dir = realpath( $dir ); // normalize
863 $this->suffixStart = strlen( $dir ) + 1; // size of "path/to/dir/"
864 $this->params = $params;
865
866 try {
867 $this->iter = $this->initIterator( $dir );
868 } catch ( UnexpectedValueException $e ) {
869 $this->iter = null; // bad permissions? deleted?
870 }
871 }
872
873 /**
874 * Return an appropriate iterator object to wrap
875 *
876 * @param $dir string file system directory
877 * @return Iterator
878 */
879 protected function initIterator( $dir ) {
880 if ( !empty( $this->params['topOnly'] ) ) { // non-recursive
881 # Get an iterator that will get direct sub-nodes
882 return new DirectoryIterator( $dir );
883 } else { // recursive
884 # Get an iterator that will return leaf nodes (non-directories)
885 # RecursiveDirectoryIterator extends FilesystemIterator.
886 # FilesystemIterator::SKIP_DOTS default is inconsistent in PHP 5.3.x.
887 $flags = FilesystemIterator::CURRENT_AS_SELF | FilesystemIterator::SKIP_DOTS;
888 return new RecursiveIteratorIterator(
889 new RecursiveDirectoryIterator( $dir, $flags ),
890 RecursiveIteratorIterator::CHILD_FIRST // include dirs
891 );
892 }
893 }
894
895 /**
896 * @see Iterator::key()
897 * @return integer
898 */
899 public function key() {
900 return $this->pos;
901 }
902
903 /**
904 * @see Iterator::current()
905 * @return string|bool String or false
906 */
907 public function current() {
908 return $this->getRelPath( $this->iter->current()->getPathname() );
909 }
910
911 /**
912 * @see Iterator::next()
913 * @return void
914 */
915 public function next() {
916 try {
917 $this->iter->next();
918 $this->filterViaNext();
919 } catch ( UnexpectedValueException $e ) {
920 $this->iter = null;
921 }
922 ++$this->pos;
923 }
924
925 /**
926 * @see Iterator::rewind()
927 * @return void
928 */
929 public function rewind() {
930 $this->pos = 0;
931 try {
932 $this->iter->rewind();
933 $this->filterViaNext();
934 } catch ( UnexpectedValueException $e ) {
935 $this->iter = null;
936 }
937 }
938
939 /**
940 * @see Iterator::valid()
941 * @return bool
942 */
943 public function valid() {
944 return $this->iter && $this->iter->valid();
945 }
946
947 /**
948 * Filter out items by advancing to the next ones
949 */
950 protected function filterViaNext() {}
951
952 /**
953 * Return only the relative path and normalize slashes to FileBackend-style.
954 * Uses the "real path" since the suffix is based upon that.
955 *
956 * @param $path string
957 * @return string
958 */
959 protected function getRelPath( $path ) {
960 return strtr( substr( realpath( $path ), $this->suffixStart ), '\\', '/' );
961 }
962 }
963
964 class FSFileBackendDirList extends FSFileBackendList {
965 protected function filterViaNext() {
966 while ( $this->iter->valid() ) {
967 if ( $this->iter->current()->isDot() || !$this->iter->current()->isDir() ) {
968 $this->iter->next(); // skip non-directories and dot files
969 } else {
970 break;
971 }
972 }
973 }
974 }
975
976 class FSFileBackendFileList extends FSFileBackendList {
977 protected function filterViaNext() {
978 while ( $this->iter->valid() ) {
979 if ( !$this->iter->current()->isFile() ) {
980 $this->iter->next(); // skip non-files and dot files
981 } else {
982 break;
983 }
984 }
985 }
986 }