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