]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/getfile.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / actions / getfile.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * Return a requested file
7  *
8  * PHP version 5
9  *
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.
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  PrivateAttachments
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 require_once 'MIME/Type.php';
36
37 /**
38  * An action for returning a requested file
39  *
40  * The StatusNet system will do an implicit user check if the site is
41  * private before allowing this to continue
42  *
43  * @category  PrivateAttachments
44  * @package   StatusNet
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/
49  */
50 class GetfileAction extends Action
51 {
52     /**
53      * Path of file to return
54      */
55     var $path = null;
56
57     /**
58      * Get file name
59      *
60      * @param array $args $_REQUEST array
61      *
62      * @return success flag
63      */
64     function prepare($args)
65     {
66         parent::prepare($args);
67
68         $filename = $this->trimmed('filename');
69         $path = null;
70
71         if ($filename && File::validFilename($filename)) {
72             $path = File::path($filename);
73         }
74
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);
78             return false;
79         }
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);
83             return false;
84         }
85
86         $this->path = $path;
87         return true;
88     }
89
90     /**
91      * Is this page read-only?
92      *
93      * @return boolean true
94      */
95     function isReadOnly($args)
96     {
97         return true;
98     }
99
100     /**
101      * Last-modified date for file
102      *
103      * @return int last-modified date as unix timestamp
104      */
105     function lastModified()
106     {
107         if (common_config('site', 'use_x_sendfile')) {
108             return null;
109         }
110
111         return filemtime($this->path);
112     }
113
114     /**
115      * etag for file
116      *
117      * This returns the same data (inode, size, mtime) as Apache would,
118      * but in decimal instead of hex.
119      *
120      * @return string etag http header
121      */
122     function etag()
123     {
124         if (common_config('site', 'use_x_sendfile')) {
125             return null;
126         }
127
128         $cache = Cache::instance();
129         if($cache) {
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);
135             }
136             return $etag;
137         }
138
139         $stat = stat($this->path);
140         return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
141     }
142
143     /**
144      * Handle input, produce output
145      *
146      * @param array $args $_REQUEST contents
147      *
148      * @return void
149      */
150     function handle($args)
151     {
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);
156
157         parent::handle($args);
158
159         $path = $this->path;
160
161         header('Content-Type: ' . MIME_Type::autoDetect($path));
162
163         if (common_config('site', 'use_x_sendfile')) {
164             header('X-Sendfile: ' . $path);
165         } else {
166             header('Content-Length: ' . filesize($path));
167             readfile($path);
168         }
169     }
170 }