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/
51 class GetfileAction extends Action
54 * Path of file to return
62 * @param array $args $_REQUEST array
64 * @return success flag
67 function prepare($args)
69 parent::prepare($args);
71 $filename = $this->trimmed('filename');
75 $path = File::path($filename);
78 if (empty($path) or !file_exists($path)) {
79 $this->clientError(_('No such file.'), 404);
82 if (!is_readable($path)) {
83 $this->clientError(_('Cannot read file.'), 403);
92 * Is this page read-only?
94 * @return boolean true
97 function isReadOnly($args)
103 * Last-modified date for file
105 * @return int last-modified date as unix timestamp
108 function lastModified()
110 if (common_config('site', 'use_x_sendfile')) {
114 return filemtime($this->path);
120 * This returns the same data (inode, size, mtime) as Apache would,
121 * but in decimal instead of hex.
123 * @return string etag http header
128 if (common_config('site', 'use_x_sendfile')) {
132 $cache = common_memcache();
134 $key = common_cache_key('attachments:etag:' . $this->path);
135 $etag = $cache->get($key);
136 if($etag === false) {
137 $etag = crc32(file_get_contents($this->path));
138 $cache->set($key,$etag);
143 $stat = stat($this->path);
144 return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
148 * Handle input, produce output
150 * @param array $args $_REQUEST contents
155 function handle($args)
157 // undo headers set by PHP sessions
158 $sec = session_cache_expire() * 60;
159 header('Expires: ' . date(DATE_RFC1123, time() + $sec));
160 header('Cache-Control: max-age=' . $sec);
162 parent::handle($args);
166 header('Content-Type: ' . MIME_Type::autoDetect($path));
168 if (common_config('site', 'use_x_sendfile')) {
169 header('X-Sendfile: ' . $path);
171 header('Content-Length: ' . filesize($path));