3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * Return a requested file
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.
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.
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/>.
23 * @category PrivateAttachments
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/
31 if (!defined('STATUSNET')) {
35 require_once 'MIME/Type.php';
38 * An action for returning a requested file
40 * The StatusNet system will do an implicit user check if the site is
41 * private before allowing this to continue
43 * @category PrivateAttachments
45 * @author Jeffery To <jeffery.to@gmail.com>
46 * @copyright 2009 StatusNet, Inc.
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
48 * @link http://status.net/
50 class GetfileAction extends Action
53 * Path of file to return
60 * @param array $args $_REQUEST array
62 * @return success flag
64 function prepare($args)
66 parent::prepare($args);
68 $filename = $this->trimmed('filename');
71 if ($filename && File::validFilename($filename)) {
72 $path = File::path($filename);
75 if (empty($path) or !file_exists($path)) {
76 // TRANS: Client error displayed when requesting a non-existent file.
77 $this->clientError(_('No such file.'), 404);
80 if (!is_readable($path)) {
81 // TRANS: Client error displayed when requesting a file without having read access to it.
82 $this->clientError(_('Cannot read file.'), 403);
91 * Is this page read-only?
93 * @return boolean true
95 function isReadOnly($args)
101 * Last-modified date for file
103 * @return int last-modified date as unix timestamp
105 function lastModified()
107 if (common_config('site', 'use_x_sendfile')) {
111 return filemtime($this->path);
117 * This returns the same data (inode, size, mtime) as Apache would,
118 * but in decimal instead of hex.
120 * @return string etag http header
124 if (common_config('site', 'use_x_sendfile')) {
128 $cache = Cache::instance();
130 $key = Cache::key('attachments:etag:' . $this->path);
131 $etag = $cache->get($key);
132 if($etag === false) {
133 $etag = crc32(file_get_contents($this->path));
134 $cache->set($key,$etag);
139 $stat = stat($this->path);
140 return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
144 * Handle input, produce output
146 * @param array $args $_REQUEST contents
150 function handle($args)
152 // undo headers set by PHP sessions
153 $sec = session_cache_expire() * 60;
154 header('Expires: ' . date(DATE_RFC1123, time() + $sec));
155 header('Cache-Control: max-age=' . $sec);
157 parent::handle($args);
161 header('Content-Type: ' . MIME_Type::autoDetect($path));
163 if (common_config('site', 'use_x_sendfile')) {
164 header('X-Sendfile: ' . $path);
166 header('Content-Length: ' . filesize($path));