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