we need a $title here
[lhc/web/wiklou.git] / thumb.php
1 <?php
2
3 /**
4 * PHP script to stream out an image thumbnail.
5 *
6 * @file
7 * @ingroup Media
8 */
9 define( 'MW_NO_OUTPUT_COMPRESSION', 1 );
10 require_once( './includes/WebStart.php' );
11
12 $wgTrivialMimeDetection = true; //don't use fancy mime detection, just check the file extension for jpg/gif/png.
13
14 require_once( "$IP/includes/StreamFile.php" );
15
16 wfThumbMain();
17 wfLogProfilingData();
18
19 //--------------------------------------------------------------------------
20
21 function wfThumbMain() {
22 wfProfileIn( __METHOD__ );
23 // Get input parameters
24 if ( get_magic_quotes_gpc() ) {
25 $params = array_map( 'stripslashes', $_REQUEST );
26 } else {
27 $params = $_REQUEST;
28 }
29
30 $fileName = isset( $params['f'] ) ? $params['f'] : '';
31 unset( $params['f'] );
32
33 // Backwards compatibility parameters
34 if ( isset( $params['w'] ) ) {
35 $params['width'] = $params['w'];
36 unset( $params['w'] );
37 }
38 if ( isset( $params['p'] ) ) {
39 $params['page'] = $params['p'];
40 }
41 unset( $params['r'] );
42
43 // Is this a thumb of an archived file?
44 $isOld = (isset( $params['archived'] ) && $params['archived']);
45 unset( $params['archived'] );
46
47 // Some basic input validation
48 $fileName = strtr( $fileName, '\\/', '__' );
49
50 // Actually fetch the image. Method depends on whether it is archived or not.
51 if( $isOld ) {
52 // Format is <timestamp>!<name>
53 $bits = explode( '!', $fileName, 2 );
54 if( !isset($bits[1]) ) {
55 wfThumbError( 404, wfMsg( 'badtitletext' ) );
56 return;
57 }
58 $title = Title::makeTitle( NS_IMAGE, $bits[1] );
59 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
60 } else {
61 $img = wfLocalFile( $fileName );
62 }
63
64 if ( !$img ) {
65 wfThumbError( 404, wfMsg( 'badtitletext' ) );
66 return;
67 }
68 if ( !$img->exists() ) {
69 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
70 return;
71 }
72 $sourcePath = $img->getPath();
73 if ( $sourcePath === false ) {
74 wfThumbError( 500, 'The source file is not locally accessible.' );
75 return;
76 }
77
78 // Check IMS against the source file
79 // This means that clients can keep a cached copy even after it has been deleted on the server
80 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
81 // Fix IE brokenness
82 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
83 // Calculate time
84 wfSuppressWarnings();
85 $imsUnix = strtotime( $imsString );
86 wfRestoreWarnings();
87 $stat = @stat( $sourcePath );
88 if ( $stat['mtime'] <= $imsUnix ) {
89 header( 'HTTP/1.1 304 Not Modified' );
90 return;
91 }
92 }
93
94 // Stream the file if it exists already
95 try {
96 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
97 $thumbPath = $img->getThumbPath( $thumbName );
98
99 if ( is_file( $thumbPath ) ) {
100 wfStreamFile( $thumbPath );
101 return;
102 }
103 }
104 } catch ( MWException $e ) {
105 wfThumbError( 500, $e->getHTML() );
106 return;
107 }
108
109 try {
110 $thumb = $img->transform( $params, File::RENDER_NOW );
111 } catch( Exception $ex ) {
112 // Tried to select a page on a non-paged file?
113 $thumb = false;
114 }
115
116 $errorMsg = false;
117 if ( !$thumb ) {
118 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
119 } elseif ( $thumb->isError() ) {
120 $errorMsg = $thumb->getHtmlMsg();
121 } elseif ( !$thumb->getPath() ) {
122 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
123 } elseif ( $thumb->getPath() == $img->getPath() ) {
124 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
125 'is the requested width bigger than the source?' );
126 } else {
127 wfStreamFile( $thumb->getPath() );
128 }
129 if ( $errorMsg !== false ) {
130 wfThumbError( 500, $errorMsg );
131 }
132
133 wfProfileOut( __METHOD__ );
134 }
135
136 function wfThumbError( $status, $msg ) {
137 global $wgShowHostnames;
138 header( 'Cache-Control: no-cache' );
139 header( 'Content-Type: text/html; charset=utf-8' );
140 if ( $status == 404 ) {
141 header( 'HTTP/1.1 404 Not found' );
142 } else {
143 header( 'HTTP/1.1 500 Internal server error' );
144 }
145 if( $wgShowHostnames ) {
146 $url = htmlspecialchars( @$_SERVER['REQUEST_URI'] );
147 $hostname = htmlspecialchars( wfHostname() );
148 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
149 } else {
150 $debug = "";
151 }
152 echo <<<EOT
153 <html><head><title>Error generating thumbnail</title></head>
154 <body>
155 <h1>Error generating thumbnail</h1>
156 <p>
157 $msg
158 </p>
159 $debug
160 </body>
161 </html>
162
163 EOT;
164 }
165