]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/getfile.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / actions / getfile.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * Return a requested file
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  PrivateAttachments
24  * @package   StatusNet
25  * @author    Jeffery To <jeffery.to@gmail.com>
26  * @copyright 2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * An action for returning a requested file
35  *
36  * The StatusNet system will do an implicit user check if the site is
37  * private before allowing this to continue
38  *
39  * @category  PrivateAttachments
40  * @package   StatusNet
41  * @author    Jeffery To <jeffery.to@gmail.com>
42  * @copyright 2009 StatusNet, Inc.
43  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44  * @link      http://status.net/
45  */
46 class GetfileAction extends Action
47 {
48     /**
49      * Path of file to return
50      */
51     var $path = null;
52
53     /**
54      * Get file name
55      *
56      * @param array $args $_REQUEST array
57      *
58      * @return success flag
59      */
60     protected function prepare(array $args=array())
61     {
62         parent::prepare($args);
63
64         $filename = $this->trimmed('filename');
65         $path = null;
66
67         if ($filename && File::validFilename($filename)) {
68             $path = File::path($filename);
69         }
70
71         if (empty($path) or !file_exists($path)) {
72             // TRANS: Client error displayed when requesting a non-existent file.
73             $this->clientError(_('No such file.'), 404);
74         }
75         if (!is_readable($path)) {
76             // TRANS: Client error displayed when requesting a file without having read access to it.
77             $this->clientError(_('Cannot read file.'), 403);
78         }
79
80         $this->path = $path;
81         return true;
82     }
83
84     /**
85      * Is this page read-only?
86      *
87      * @return boolean true
88      */
89     function isReadOnly($args)
90     {
91         return true;
92     }
93
94     /**
95      * Last-modified date for file
96      *
97      * @return int last-modified date as unix timestamp
98      */
99     function lastModified()
100     {
101         if (common_config('site', 'use_x_sendfile')) {
102             return null;
103         }
104
105         return filemtime($this->path);
106     }
107
108     /**
109      * etag for file
110      *
111      * This returns the same data (inode, size, mtime) as Apache would,
112      * but in decimal instead of hex.
113      *
114      * @return string etag http header
115      */
116     function etag()
117     {
118         if (common_config('site', 'use_x_sendfile')) {
119             return null;
120         }
121
122         $cache = Cache::instance();
123         if($cache) {
124             $key = Cache::key('attachments:etag:' . $this->path);
125             $etag = $cache->get($key);
126             if($etag === false) {
127                 $etag = crc32(file_get_contents($this->path));
128                 $cache->set($key,$etag);
129             }
130             return $etag;
131         }
132
133         $stat = stat($this->path);
134         return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
135     }
136
137     /**
138      * Handle input, produce output
139      *
140      * @return void
141      */
142     protected function handle()
143     {
144         // undo headers set by PHP sessions
145         $sec = session_cache_expire() * 60;
146         header('Expires: ' . date(DATE_RFC1123, time() + $sec));
147         header('Cache-Control: max-age=' . $sec);
148
149         parent::handle();
150
151         $path = $this->path;
152
153         $finfo = new finfo(FILEINFO_MIME_TYPE);
154
155         header('Content-Type: ' . $finfo->file($path));
156
157         if (common_config('site', 'use_x_sendfile')) {
158             header('X-Sendfile: ' . $path);
159         } else {
160             header('Content-Length: ' . filesize($path));
161             readfile($path);
162         }
163     }
164 }