MediaWiki REL1_34
img_auth.php
Go to the documentation of this file.
1<?php
41define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
42define( 'MW_ENTRY_POINT', 'img_auth' );
43require __DIR__ . '/includes/WebStart.php';
44
45# Set action base paths so that WebRequest::getPathInfo()
46# recognizes the "X" as the 'title' in ../img_auth.php/X urls.
47$wgArticlePath = false; # Don't let a "/*" article path clober our action path
48$wgActionPaths = [ "$wgUploadPath/" ];
49
50wfImageAuthMain();
51
52$mediawiki = new MediaWiki();
53$mediawiki->doPostOutputShutdown( 'fast' );
54
55function wfImageAuthMain() {
56 global $wgImgAuthUrlPathMap;
57 $permissionManager = \MediaWiki\MediaWikiServices::getInstance()->getPermissionManager();
58
59 $request = RequestContext::getMain()->getRequest();
60 $publicWiki = in_array( 'read', $permissionManager->getGroupPermissions( [ '*' ] ), true );
61
62 // Get the requested file path (source file or thumbnail)
63 $matches = WebRequest::getPathInfo();
64 if ( !isset( $matches['title'] ) ) {
65 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
66 return;
67 }
68 $path = $matches['title'];
69 if ( $path && $path[0] !== '/' ) {
70 // Make sure $path has a leading /
71 $path = "/" . $path;
72 }
73
74 // Check for T30235: QUERY_STRING overriding the correct extension
75 $whitelist = [];
76 $extension = FileBackend::extensionFromPath( $path, 'rawcase' );
77 if ( $extension != '' ) {
78 $whitelist[] = $extension;
79 }
80 if ( !$request->checkUrlExtension( $whitelist ) ) {
81 return;
82 }
83
84 $user = RequestContext::getMain()->getUser();
85
86 // Various extensions may have their own backends that need access.
87 // Check if there is a special backend and storage base path for this file.
88 foreach ( $wgImgAuthUrlPathMap as $prefix => $storageDir ) {
89 $prefix = rtrim( $prefix, '/' ) . '/'; // implicit trailing slash
90 if ( strpos( $path, $prefix ) === 0 ) {
91 $be = FileBackendGroup::singleton()->backendFromPath( $storageDir );
92 $filename = $storageDir . substr( $path, strlen( $prefix ) ); // strip prefix
93 // Check basic user authorization
94 if ( !$user->isAllowed( 'read' ) ) {
95 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $path );
96 return;
97 }
98 if ( $be->fileExists( [ 'src' => $filename ] ) ) {
99 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
100 $be->streamFile( [
101 'src' => $filename,
102 'headers' => [ 'Cache-Control: private', 'Vary: Cookie' ]
103 ] );
104 } else {
105 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $path );
106 }
107 return;
108 }
109 }
110
111 // Get the local file repository
112 $repo = RepoGroup::singleton()->getRepo( 'local' );
113 $zone = strstr( ltrim( $path, '/' ), '/', true );
114
115 // Get the full file storage path and extract the source file name.
116 // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
117 // This only applies to thumbnails/transcoded, and each of them should
118 // be under a folder that has the source file name.
119 if ( $zone === 'thumb' || $zone === 'transcoded' ) {
120 $name = wfBaseName( dirname( $path ) );
121 $filename = $repo->getZonePath( $zone ) . substr( $path, strlen( "/" . $zone ) );
122 // Check to see if the file exists
123 if ( !$repo->fileExists( $filename ) ) {
124 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
125 return;
126 }
127 } else {
128 $name = wfBaseName( $path ); // file is a source file
129 $filename = $repo->getZonePath( 'public' ) . $path;
130 // Check to see if the file exists and is not deleted
131 $bits = explode( '!', $name, 2 );
132 if ( substr( $path, 0, 9 ) === '/archive/' && count( $bits ) == 2 ) {
133 $file = $repo->newFromArchiveName( $bits[1], $name );
134 } else {
135 $file = $repo->newFile( $name );
136 }
137 if ( !$file->exists() || $file->isDeleted( File::DELETED_FILE ) ) {
138 wfForbidden( 'img-auth-accessdenied', 'img-auth-nofile', $filename );
139 return;
140 }
141 }
142
143 $headers = []; // extra HTTP headers to send
144
145 $title = Title::makeTitleSafe( NS_FILE, $name );
146
147 if ( !$publicWiki ) {
148 // For private wikis, run extra auth checks and set cache control headers
149 $headers['Cache-Control'] = 'private';
150 $headers['Vary'] = 'Cookie';
151
152 if ( !$title instanceof Title ) { // files have valid titles
153 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
154 return;
155 }
156
157 // Run hook for extension authorization plugins
159 $result = null;
160 if ( !Hooks::run( 'ImgAuthBeforeStream', [ &$title, &$path, &$name, &$result ] ) ) {
161 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
162 return;
163 }
164
165 // Check user authorization for this title
166 // Checks Whitelist too
167
168 if ( !$permissionManager->userCan( 'read', $user, $title ) ) {
169 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
170 return;
171 }
172 }
173
174 if ( isset( $_SERVER['HTTP_RANGE'] ) ) {
175 $headers['Range'] = $_SERVER['HTTP_RANGE'];
176 }
177 if ( isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
178 $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
179 }
180
181 if ( $request->getCheck( 'download' ) ) {
182 $headers['Content-Disposition'] = 'attachment';
183 }
184
185 // Allow modification of headers before streaming a file
186 Hooks::run( 'ImgAuthModifyHeaders', [ $title->getTitleValue(), &$headers ] );
187
188 // Stream the requested file
189 list( $headers, $options ) = HTTPFileStreamer::preprocessHeaders( $headers );
190 wfDebugLog( 'img_auth', "Streaming `" . $filename . "`." );
191 $repo->streamFileWithStatus( $filename, $headers, $options );
192}
193
203function wfForbidden( $msg1, $msg2, ...$args ) {
204 global $wgImgAuthDetails;
205
206 $args = ( isset( $args[0] ) && is_array( $args[0] ) ) ? $args[0] : $args;
207
208 $msgHdr = wfMessage( $msg1 )->escaped();
209 $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
210 $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
211
212 wfDebugLog( 'img_auth',
213 "wfForbidden Hdr: " . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: " .
214 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
215 );
216
217 HttpStatus::header( 403 );
218 header( 'Cache-Control: no-cache' );
219 header( 'Content-Type: text/html; charset=utf-8' );
220 echo <<<ENDS
221<!DOCTYPE html>
222<html>
223<head>
224<meta charset="UTF-8" />
225<title>$msgHdr</title>
226</head>
227<body>
228<h1>$msgHdr</h1>
229<p>$detailMsg</p>
230</body>
231</html>
232ENDS;
233}
$wgArticlePath
Definition img_auth.php:47
Base interface for content objects.
Definition Content.php:34