* Marked Special:UploadStash unlisted since accessing it only subpage param only...
[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 UnlistedSpecialPage {
20 // UploadStash
21 private $stash;
22
23 // we should not be reading in really big files and serving them out
24 private $maxServeFileSize = 262144; // 256K
25
26 // $request is the request (usually wgRequest)
27 // $subpage is everything in the URL after Special:UploadStash
28 // FIXME: These parameters don't match SpecialPage::__construct()'s params at all, and are unused --RK
29 public function __construct( $request = null, $subpage = null ) {
30 parent::__construct( 'UploadStash', 'upload' );
31 $this->stash = new UploadStash();
32 }
33
34 /**
35 * If file available in stash, cats it out to the client as a simple HTTP response.
36 * n.b. Most sanity checking done in UploadStashLocalFile, so this is straightforward.
37 *
38 * @param {String} $subPage: subpage, e.g. in http://example.com/wiki/Special:UploadStash/foo.jpg, the "foo.jpg" part
39 * @return {Boolean} success
40 */
41 public function execute( $subPage ) {
42 global $wgOut, $wgUser;
43
44 if ( !$this->userCanExecute( $wgUser ) ) {
45 $this->displayRestrictionError();
46 return;
47 }
48
49 // prevent callers from doing standard HTML output -- we'll take it from here
50 $wgOut->disable();
51
52 try {
53 $file = $this->getStashFile( $subPage );
54 if ( $file->getSize() > $this->maxServeFileSize ) {
55 throw new MWException( 'file size too large' );
56 }
57 $this->outputFile( $file );
58 return true;
59
60 } catch( UploadStashFileNotFoundException $e ) {
61 $code = 404;
62 } catch( UploadStashBadPathException $e ) {
63 $code = 403;
64 } catch( Exception $e ) {
65 $code = 500;
66 }
67
68 wfHttpError( $code, OutputPage::getStatusMessage( $code ), $e->getMessage() );
69 return false;
70 }
71
72
73 /**
74 * Convert the incoming url portion (subpage of Special page) into a stashed file, if available.
75 * @param {String} $subPage
76 * @return {File} file object
77 * @throws MWException, UploadStashFileNotFoundException, UploadStashBadPathException
78 */
79 private function getStashFile( $subPage ) {
80 // due to an implementation quirk (and trying to be compatible with older method)
81 // the stash key doesn't have an extension
82 $key = $subPage;
83 $n = strrpos( $subPage, '.' );
84 if ( $n !== false ) {
85 $key = $n ? substr( $subPage, 0, $n ) : $subPage;
86 }
87
88 try {
89 $file = $this->stash->getFile( $key );
90 } catch ( UploadStashFileNotFoundException $e ) {
91 // if we couldn't find it, and it looks like a thumbnail,
92 // and it looks like we have the original, go ahead and generate it
93 $matches = array();
94 if ( ! preg_match( '/^(\d+)px-(.*)$/', $key, $matches ) ) {
95 // that doesn't look like a thumbnail. re-raise exception
96 throw $e;
97 }
98
99 list( $dummy, $width, $origKey ) = $matches;
100
101 // do not trap exceptions, if key is in bad format, or file not found,
102 // let exceptions propagate to caller.
103 $origFile = $this->stash->getFile( $origKey );
104
105 // ok we're here so the original must exist. Generate the thumbnail.
106 // because the file is a UploadStashFile, this thumbnail will also be stashed,
107 // and a thumbnailFile will be created in the thumbnailImage composite object
108 $thumbnailImage = $origFile->transform( array( 'width' => $width ) );
109 if ( !$thumbnailImage ) {
110 throw new MWException( 'Could not obtain thumbnail' );
111 }
112 $file = $thumbnailImage->thumbnailFile;
113 }
114
115 return $file;
116 }
117
118 /**
119 * Output HTTP response for file
120 * Side effects, obviously, of echoing lots of stuff to stdout.
121 * @param {File} file
122 */
123 private function outputFile( $file ) {
124 header( 'Content-Type: ' . $file->getMimeType(), true );
125 header( 'Content-Transfer-Encoding: binary', true );
126 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true );
127 header( 'Pragma: public', true );
128 header( 'Content-Length: ' . $file->getSize(), true ); // FIXME: PHP can handle Content-Length for you just fine --RK
129 readfile( $file->getPath() );
130 }
131 }