Revert r76177 and use AutoLoader
[lhc/web/wiklou.git] / includes / specials / SpecialUploadStash.php
1 <?php
2 /**
3 * Special:UploadStash
4 *
5 * Web access for files temporarily stored by UploadStash.
6 *
7 * For example -- files that were uploaded with the UploadWizard extension are stored temporarily
8 * before committing them to the db. But we want to see their thumbnails and get other information
9 * about them.
10 *
11 * Since this is based on the user's session, in effect this creates a private temporary file area.
12 * However, the URLs for the files cannot be shared.
13 *
14 * @file
15 * @ingroup SpecialPage
16 * @ingroup Upload
17 */
18
19 class SpecialUploadStash extends SpecialPage {
20
21 static $HttpErrors = array( // FIXME: Use OutputPage::getStatusMessage() --RK
22 400 => 'Bad Request',
23 403 => 'Access Denied',
24 404 => 'File not found',
25 500 => 'Internal Server Error',
26 );
27
28 // UploadStash
29 private $stash;
30
31 // we should not be reading in really big files and serving them out
32 private $maxServeFileSize = 262144; // 256K
33
34 // $request is the request (usually wgRequest)
35 // $subpage is everything in the URL after Special:UploadStash
36 // FIXME: These parameters don't match SpecialPage::__construct()'s params at all, and are unused --RK
37 public function __construct( $request = null, $subpage = null ) {
38 parent::__construct( 'UploadStash', 'upload' );
39 $this->stash = new UploadStash();
40 }
41
42 /**
43 * If file available in stash, cats it out to the client as a simple HTTP response.
44 * n.b. Most sanity checking done in UploadStashLocalFile, so this is straightforward.
45 *
46 * @param {String} $subPage: subpage, e.g. in http://example.com/wiki/Special:UploadStash/foo.jpg, the "foo.jpg" part
47 * @return {Boolean} success
48 */
49 public function execute( $subPage ) {
50 global $wgOut, $wgUser;
51
52 if ( !$this->userCanExecute( $wgUser ) ) {
53 $this->displayRestrictionError();
54 return;
55 }
56
57 // prevent callers from doing standard HTML output -- we'll take it from here
58 $wgOut->disable();
59
60 try {
61 $file = $this->getStashFile( $subPage );
62 if ( $file->getSize() > $this->maxServeFileSize ) {
63 throw new MWException( 'file size too large' );
64 }
65 $this->outputFile( $file );
66 return true;
67
68 } catch( UploadStashFileNotFoundException $e ) {
69 $code = 404;
70 } catch( UploadStashBadPathException $e ) {
71 $code = 403;
72 } catch( Exception $e ) {
73 $code = 500;
74 }
75
76 wfHttpError( $code, self::$HttpErrors[$code], $e->getCode(), $e->getMessage() );
77 return false;
78 }
79
80
81 /**
82 * Convert the incoming url portion (subpage of Special page) into a stashed file, if available.
83 * @param {String} $subPage
84 * @return {File} file object
85 * @throws MWException, UploadStashFileNotFoundException, UploadStashBadPathException
86 */
87 private function getStashFile( $subPage ) {
88 // due to an implementation quirk (and trying to be compatible with older method)
89 // the stash key doesn't have an extension
90 $key = $subPage;
91 $n = strrpos( $subPage, '.' );
92 if ( $n !== false ) {
93 $key = $n ? substr( $subPage, 0, $n ) : $subPage;
94 }
95
96 try {
97 $file = $this->stash->getFile( $key );
98 } catch ( UploadStashFileNotFoundException $e ) {
99 // if we couldn't find it, and it looks like a thumbnail,
100 // and it looks like we have the original, go ahead and generate it
101 $matches = array();
102 if ( ! preg_match( '/^(\d+)px-(.*)$/', $key, $matches ) ) {
103 // that doesn't look like a thumbnail. re-raise exception
104 throw $e;
105 }
106
107 list( $dummy, $width, $origKey ) = $matches;
108
109 // do not trap exceptions, if key is in bad format, or file not found,
110 // let exceptions propagate to caller.
111 $origFile = $this->stash->getFile( $origKey );
112
113 // ok we're here so the original must exist. Generate the thumbnail.
114 // because the file is a UploadStashFile, this thumbnail will also be stashed,
115 // and a thumbnailFile will be created in the thumbnailImage composite object
116 $thumbnailImage = $origFile->transform( array( 'width' => $width ) );
117 if ( !$thumbnailImage ) {
118 throw new MWException( 'Could not obtain thumbnail' );
119 }
120 $file = $thumbnailImage->thumbnailFile;
121 }
122
123 return $file;
124 }
125
126 /**
127 * Output HTTP response for file
128 * Side effects, obviously, of echoing lots of stuff to stdout.
129 * @param {File} file
130 */
131 private function outputFile( $file ) {
132 header( 'Content-Type: ' . $file->getMimeType(), true );
133 header( 'Content-Transfer-Encoding: binary', true );
134 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
135 header( 'Pragma: public', true );
136 header( 'Content-Length: ' . $file->getSize(), true ); // FIXME: PHP can handle Content-Length for you just fine --RK
137 readfile( $file->getPath() );
138 }
139 }
140