MediaWiki  REL1_20
img_auth.php
Go to the documentation of this file.
00001 <?php
00042 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
00043 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
00044         require ( 'core/includes/WebStart.php' );
00045 } else {
00046         require ( __DIR__ . '/includes/WebStart.php' );
00047 }
00048 wfProfileIn( 'img_auth.php' );
00049 
00050 # Set action base paths so that WebRequest::getPathInfo()
00051 # recognizes the "X" as the 'title' in ../image_auth/X urls.
00052 $wgArticlePath = false; # Don't let a "/*" article path clober our action path
00053 $wgActionPaths = array( "$wgUploadPath/" );
00054 
00055 wfImageAuthMain();
00056 wfLogProfilingData();
00057 
00058 function wfImageAuthMain() {
00059         global $wgImgAuthPublicTest, $wgRequest;
00060 
00061         // See if this is a public Wiki (no protections).
00062         if ( $wgImgAuthPublicTest
00063                 && in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) )
00064         {
00065                 // This is a public wiki, so disable this script (for private wikis only)
00066                 wfForbidden( 'img-auth-accessdenied', 'img-auth-public' );
00067                 return;
00068         }
00069 
00070         // Get the requested file path (source file or thumbnail)
00071         $matches = WebRequest::getPathInfo();
00072         if ( !isset( $matches['title'] ) ) {
00073                 wfForbidden( 'img-auth-accessdenied', 'img-auth-nopathinfo' );
00074                 return;
00075         }
00076         $path = $matches['title'];
00077         if ( $path && $path[0] !== '/' ) {
00078                 // Make sure $path has a leading /
00079                 $path = "/" . $path;
00080         }
00081 
00082         // Check for bug 28235: QUERY_STRING overriding the correct extension
00083         $whitelist = array();
00084         $dotPos = strrpos( $path, '.' );
00085         if ( $dotPos !== false ) {
00086                 $whitelist[] = substr( $path, $dotPos + 1 );
00087         }
00088         if ( !$wgRequest->checkUrlExtension( $whitelist ) ) {
00089                 return;
00090         }
00091 
00092         // Get the local file repository
00093         $repo = RepoGroup::singleton()->getRepo( 'local' );
00094 
00095         // Get the full file storage path and extract the source file name.
00096         // (e.g. 120px-Foo.png => Foo.png or page2-120px-Foo.png => Foo.png).
00097         // This only applies to thumbnails, and all thumbnails should
00098         // be under a folder that has the source file name.
00099         if ( strpos( $path, '/thumb/' ) === 0 ) {
00100                 $name = wfBaseName( dirname( $path ) ); // file is a thumbnail
00101                 $filename = $repo->getZonePath( 'thumb' ) . substr( $path, 6 ); // strip "/thumb"
00102         } else {
00103                 $name = wfBaseName( $path ); // file is a source file
00104                 $filename = $repo->getZonePath( 'public' ) . $path;
00105         }
00106 
00107         // Check to see if the file exists
00108         if ( !$repo->fileExists( $filename ) ) {
00109                 wfForbidden( 'img-auth-accessdenied','img-auth-nofile', $filename );
00110                 return;
00111         }
00112 
00113         $title = Title::makeTitleSafe( NS_FILE, $name );
00114         if ( !$title instanceof Title ) { // files have valid titles
00115                 wfForbidden( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
00116                 return;
00117         }
00118 
00119         // Run hook for extension authorization plugins
00120         if ( !wfRunHooks( 'ImgAuthBeforeStream', array( &$title, &$path, &$name, &$result ) ) ) {
00121                 wfForbidden( $result[0], $result[1], array_slice( $result, 2 ) );
00122                 return;
00123         }
00124 
00125         // Check user authorization for this title
00126         // Checks Whitelist too
00127         if ( !$title->userCan( 'read' ) ) {
00128                 wfForbidden( 'img-auth-accessdenied', 'img-auth-noread', $name );
00129                 return;
00130         }
00131 
00132         // Stream the requested file
00133         wfDebugLog( 'img_auth', "Streaming `".$filename."`." );
00134         $repo->streamFile( $filename, array( 'Cache-Control: private', 'Vary: Cookie' ) );
00135 }
00136 
00144 function wfForbidden( $msg1, $msg2 ) {
00145         global $wgImgAuthDetails;
00146 
00147         $args = func_get_args();
00148         array_shift( $args );
00149         array_shift( $args );
00150 
00151         $msgHdr = wfMessage( $msg1 )->escaped();
00152         $detailMsgKey = $wgImgAuthDetails ? $msg2 : 'badaccess-group0';
00153         $detailMsg = wfMessage( $detailMsgKey, $args )->escaped();
00154 
00155         wfDebugLog( 'img_auth',
00156                 "wfForbidden Hdr:" . wfMessage( $msg1 )->inLanguage( 'en' )->text() . " Msg: ".
00157                 wfMessage( $msg2, $args )->inLanguage( 'en' )->text()
00158         );
00159 
00160         header( 'HTTP/1.0 403 Forbidden' );
00161         header( 'Cache-Control: no-cache' );
00162         header( 'Content-Type: text/html; charset=utf-8' );
00163         echo <<<ENDS
00164 <html>
00165 <body>
00166 <h1>$msgHdr</h1>
00167 <p>$detailMsg</p>
00168 </body>
00169 </html>
00170 ENDS;
00171 }