check 'archived' param
[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 $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $fileName );
53 } else {
54 $img = wfLocalFile( $fileName );
55 }
56
57 if ( !$img ) {
58 wfThumbError( 404, wfMsg( 'badtitletext' ) );
59 return;
60 }
61 if ( !$img->exists() ) {
62 wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
63 return;
64 }
65 $sourcePath = $img->getPath();
66 if ( $sourcePath === false ) {
67 wfThumbError( 500, 'The source file is not locally accessible.' );
68 return;
69 }
70
71 // Check IMS against the source file
72 // This means that clients can keep a cached copy even after it has been deleted on the server
73 if ( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
74 // Fix IE brokenness
75 $imsString = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
76 // Calculate time
77 wfSuppressWarnings();
78 $imsUnix = strtotime( $imsString );
79 wfRestoreWarnings();
80 $stat = @stat( $sourcePath );
81 if ( $stat['mtime'] <= $imsUnix ) {
82 header( 'HTTP/1.1 304 Not Modified' );
83 return;
84 }
85 }
86
87 // Stream the file if it exists already
88 try {
89 if ( false != ( $thumbName = $img->thumbName( $params ) ) ) {
90 $thumbPath = $img->getThumbPath( $thumbName );
91
92 if ( is_file( $thumbPath ) ) {
93 wfStreamFile( $thumbPath );
94 return;
95 }
96 }
97 } catch ( MWException $e ) {
98 wfThumbError( 500, $e->getHTML() );
99 return;
100 }
101
102 try {
103 $thumb = $img->transform( $params, File::RENDER_NOW );
104 } catch( Exception $ex ) {
105 // Tried to select a page on a non-paged file?
106 $thumb = false;
107 }
108
109 $errorMsg = false;
110 if ( !$thumb ) {
111 $errorMsg = wfMsgHtml( 'thumbnail_error', 'File::transform() returned false' );
112 } elseif ( $thumb->isError() ) {
113 $errorMsg = $thumb->getHtmlMsg();
114 } elseif ( !$thumb->getPath() ) {
115 $errorMsg = wfMsgHtml( 'thumbnail_error', 'No path supplied in thumbnail object' );
116 } elseif ( $thumb->getPath() == $img->getPath() ) {
117 $errorMsg = wfMsgHtml( 'thumbnail_error', 'Image was not scaled, ' .
118 'is the requested width bigger than the source?' );
119 } else {
120 wfStreamFile( $thumb->getPath() );
121 }
122 if ( $errorMsg !== false ) {
123 wfThumbError( 500, $errorMsg );
124 }
125
126 wfProfileOut( __METHOD__ );
127 }
128
129 function wfThumbError( $status, $msg ) {
130 global $wgShowHostnames;
131 header( 'Cache-Control: no-cache' );
132 header( 'Content-Type: text/html; charset=utf-8' );
133 if ( $status == 404 ) {
134 header( 'HTTP/1.1 404 Not found' );
135 } else {
136 header( 'HTTP/1.1 500 Internal server error' );
137 }
138 if( $wgShowHostnames ) {
139 $url = htmlspecialchars( @$_SERVER['REQUEST_URI'] );
140 $hostname = htmlspecialchars( wfHostname() );
141 $debug = "<!-- $url -->\n<!-- $hostname -->\n";
142 } else {
143 $debug = "";
144 }
145 echo <<<EOT
146 <html><head><title>Error generating thumbnail</title></head>
147 <body>
148 <h1>Error generating thumbnail</h1>
149 <p>
150 $msg
151 </p>
152 $debug
153 </body>
154 </html>
155
156 EOT;
157 }
158