]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/getfile.php
Merge commit 'refs/merge-requests/1900' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / actions / getfile.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Returns a given file attachment, allowing private sites to only allow
6  * access to file attachments after login.
7  *
8  * PHP version 5
9  *
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.
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  Personal
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once 'MIME/Type.php';
36
37 /**
38  * Action for getting a file attachment
39  *
40  * @category Personal
41  * @package  StatusNet
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/
45  */
46
47 class GetfileAction extends Action
48 {
49     /**
50      * Path of file to return
51      */
52
53     var $path = null;
54
55     /**
56      * Get file name
57      *
58      * @param array $args $_REQUEST array
59      *
60      * @return success flag
61      */
62
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         $filename = $this->trimmed('filename');
68         $path = null;
69
70         if ($filename) {
71             $path = common_config('attachments', 'dir') . $filename;
72         }
73
74         if (empty($path) or !file_exists($path)) {
75             $this->clientError(_('No such file.'), 404);
76             return false;
77         }
78         if (!is_readable($path)) {
79             $this->clientError(_('Cannot read file.'), 403);
80             return false;
81         }
82
83         $this->path = $path;
84         return true;
85     }
86
87     /**
88      * Is this page read-only?
89      *
90      * @return boolean true
91      */
92
93     function isReadOnly($args)
94     {
95         return true;
96     }
97
98     /**
99      * Last-modified date for file
100      *
101      * @return int last-modified date as unix timestamp
102      */
103
104     function lastModified()
105     {
106         return filemtime($this->path);
107     }
108
109     /**
110      * etag for file
111      *
112      * This returns the same data (inode, size, mtime) as Apache would,
113      * but in decimal instead of hex.
114      *
115      * @return string etag http header
116      */
117     function etag()
118     {
119         $stat = stat($this->path);
120         return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
121     }
122
123     /**
124      * Handle input, produce output
125      *
126      * @param array $args $_REQUEST contents
127      *
128      * @return void
129      */
130
131     function handle($args)
132     {
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');
138
139         parent::handle($args);
140
141         $path = $this->path;
142         header('Content-Type: ' . MIME_Type::autoDetect($path));
143         readfile($path);
144     }
145 }