3 * StatusNet, the distributed open-source microblogging tool
5 * Returns a given file attachment, allowing private sites to only allow
6 * access to file attachments after login.
10 * LICENCE: 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/>.
25 * @author Jeffery To <jeffery.to@gmail.com>
26 * @copyright 2008-2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once 'MIME/Type.php';
38 * Action for getting a file attachment
42 * @author Jeffery To <jeffery.to@gmail.com>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
47 class GetfileAction extends Action
50 * Path of file to return
58 * @param array $args $_REQUEST array
60 * @return success flag
63 function prepare($args)
65 parent::prepare($args);
67 $filename = $this->trimmed('filename');
71 $path = common_config('attachments', 'dir') . $filename;
74 if (empty($path) or !file_exists($path)) {
75 $this->clientError(_('No such file.'), 404);
78 if (!is_readable($path)) {
79 $this->clientError(_('Cannot read file.'), 403);
88 * Is this page read-only?
90 * @return boolean true
93 function isReadOnly($args)
99 * Last-modified date for file
101 * @return int last-modified date as unix timestamp
104 function lastModified()
106 return filemtime($this->path);
112 * This returns the same data (inode, size, mtime) as Apache would,
113 * but in decimal instead of hex.
115 * @return string etag http header
119 $stat = stat($this->path);
120 return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
124 * Handle input, produce output
126 * @param array $args $_REQUEST contents
131 function handle($args)
133 // undo headers set by PHP sessions
134 $sec = session_cache_expire() * 60;
135 header('Expires: ' . date(DATE_RFC1123, time() + $sec));
136 header('Cache-Control: public, max-age=' . $sec);
137 header('Pragma: public');
139 parent::handle($args);
142 header('Content-Type: ' . MIME_Type::autoDetect($path));