00001 <?php
00002
00009 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
00010 require_once( './includes/WebStart.php' );
00011
00012 $wgTrivialMimeDetection = true;
00013
00014 require_once( "$IP/includes/StreamFile.php" );
00015
00016 wfThumbMain();
00017 wfLogProfilingData();
00018
00019
00020
00021 function wfThumbMain() {
00022 wfProfileIn( __METHOD__ );
00023
00024 $headers = array();
00025
00026
00027 if ( get_magic_quotes_gpc() ) {
00028 $params = array_map( 'stripslashes', $_REQUEST );
00029 } else {
00030 $params = $_REQUEST;
00031 }
00032
00033 $fileName = isset( $params['f'] ) ? $params['f'] : '';
00034 unset( $params['f'] );
00035
00036
00037 if ( isset( $params['w'] ) ) {
00038 $params['width'] = $params['w'];
00039 unset( $params['w'] );
00040 }
00041 if ( isset( $params['p'] ) ) {
00042 $params['page'] = $params['p'];
00043 }
00044 unset( $params['r'] );
00045
00046
00047 $isOld = (isset( $params['archived'] ) && $params['archived']);
00048 unset( $params['archived'] );
00049
00050
00051 $fileName = strtr( $fileName, '\\/', '__' );
00052
00053
00054 if( $isOld ) {
00055
00056 $bits = explode( '!', $fileName, 2 );
00057 if( !isset($bits[1]) ) {
00058 wfThumbError( 404, wfMsg( 'badtitletext' ) );
00059 return;
00060 }
00061 $title = Title::makeTitleSafe( NS_FILE, $bits[1] );
00062 if( is_null($title) ) {
00063 wfThumbError( 404, wfMsg( 'badtitletext' ) );
00064 return;
00065 }
00066 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
00067 } else {
00068 $img = wfLocalFile( $fileName );
00069 }
00070
00071
00072 if ( !in_array( 'read', User::getGroupPermissions( array( '*' ) ), true ) ) {
00073 if ( !$img->getTitle()->userCanRead() ) {
00074 wfThumbError( 403, 'Access denied. You do not have permission to access ' .
00075 'the source file.' );
00076 return;
00077 }
00078 $headers[] = 'Cache-Control: private';
00079 $headers[] = 'Vary: Cookie';
00080 }
00081
00082 if ( !$img ) {
00083 wfThumbError( 404, wfMsg( 'badtitletext' ) );
00084 return;
00085 }
00086 if ( !$img->exists() ) {
00087 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
00088 return;
00089 }
00090 $sourcePath = $img->getPath();
00091 if ( $sourcePath === false ) {
00092 wfThumbError( 500, 'The source file is not locally accessible.' );
00093 return;
00094 }
00095
00096
00097
00098 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
00099
00100 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
00101
00102 wfSuppressWarnings();
00103 $imsUnix = strtotime( $imsString );
00104 wfRestoreWarnings();
00105 $stat = @stat( $sourcePath );
00106 if ( $stat['mtime'] <= $imsUnix ) {
00107 header( 'HTTP/1.1 304 Not Modified' );
00108 return;
00109 }
00110 }
00111
00112
00113 try {
00114 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
00115 $thumbPath = $img->getThumbPath( $thumbName );
00116
00117 if ( is_file( $thumbPath ) ) {
00118 wfStreamFile( $thumbPath, $headers );
00119 return;
00120 }
00121 }
00122 } catch ( MWException $e ) {
00123 wfThumbError( 500, $e->getHTML() );
00124 return;
00125 }
00126
00127 try {
00128 $thumb = $img->transform( $params, File::RENDER_NOW );
00129 } catch( Exception $ex ) {
00130
00131 $thumb = false;
00132 }
00133
00134 $errorMsg = false;
00135 if ( !$thumb ) {
00136 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
00137 } elseif ( $thumb->isError() ) {
00138 $errorMsg = $thumb->getHtmlMsg();
00139 } elseif ( !$thumb->getPath() ) {
00140 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
00141 } elseif ( $thumb->getPath() == $img->getPath() ) {
00142 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
00143 'is the requested width bigger than the source?' );
00144 } else {
00145 wfStreamFile( $thumb->getPath(), $headers );
00146 }
00147 if ( $errorMsg !== false ) {
00148 wfThumbError( 500, $errorMsg );
00149 }
00150
00151 wfProfileOut( __METHOD__ );
00152 }
00153
00154 function wfThumbError( $status, $msg ) {
00155 global $wgShowHostnames;
00156 header( 'Cache-Control: no-cache' );
00157 header( 'Content-Type: text/html; charset=utf-8' );
00158 if ( $status == 404 ) {
00159 header( 'HTTP/1.1 404 Not found' );
00160 } elseif ( $status == 403 ) {
00161 header( 'HTTP/1.1 403 Forbidden' );
00162 header( 'Vary: Cookie' );
00163 } else {
00164 header( 'HTTP/1.1 500 Internal server error' );
00165 }
00166 if( $wgShowHostnames ) {
00167 $url = htmlspecialchars( @$_SERVER['REQUEST_URI'] );
00168 $hostname = htmlspecialchars( wfHostname() );
00169 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
00170 } else {
00171 $debug = "";
00172 }
00173 echo <<<EOT
00174 <html><head><title>Error generating thumbnail</title></head>
00175 <body>
00176 <h1>Error generating thumbnail</h1>
00177 <p>
00178 $msg
00179 </p>
00180 $debug
00181 </body>
00182 </html>
00183
00184 EOT;
00185 }
00186