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('GNUSOCIAL')) { exit(1); }
34 * An action for returning a requested file
36 * The StatusNet system will do an implicit user check if the site is
37 * private before allowing this to continue
39 * @category PrivateAttachments
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/
46 class GetfileAction extends Action
49 * Path of file to return
56 * @param array $args $_REQUEST array
58 * @return success flag
60 protected function prepare(array $args=array())
62 parent::prepare($args);
64 $filename = $this->trimmed('filename');
67 if ($filename && File::validFilename($filename)) {
68 $path = File::path($filename);
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);
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);
85 * Is this page read-only?
87 * @return boolean true
89 function isReadOnly($args)
95 * Last-modified date for file
97 * @return int last-modified date as unix timestamp
99 function lastModified()
101 if (common_config('site', 'use_x_sendfile')) {
105 return filemtime($this->path);
111 * This returns the same data (inode, size, mtime) as Apache would,
112 * but in decimal instead of hex.
114 * @return string etag http header
118 if (common_config('site', 'use_x_sendfile')) {
122 $cache = Cache::instance();
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);
133 $stat = stat($this->path);
134 return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
138 * Handle input, produce output
142 protected function handle()
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);
153 $finfo = new finfo(FILEINFO_MIME_TYPE);
155 header('Content-Type: ' . $finfo->file($path));
157 if (common_config('site', 'use_x_sendfile')) {
158 header('X-Sendfile: ' . $path);
160 header('Content-Length: ' . filesize($path));