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