]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/getfile.php
Don't use PEAR MIME lib when we have PECL fileinfo
[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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * An action for returning a requested file
35  *
36  * The StatusNet system will do an implicit user check if the site is
37  * private before allowing this to continue
38  *
39  * @category  PrivateAttachments
40  * @package   StatusNet
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/
45  */
46 class GetfileAction extends Action
47 {
48     /**
49      * Path of file to return
50      */
51     var $path = null;
52
53     /**
54      * Get file name
55      *
56      * @param array $args $_REQUEST array
57      *
58      * @return success flag
59      */
60     protected function prepare(array $args=array())
61     {
62         parent::prepare($args);
63
64         $filename = $this->trimmed('filename');
65         $path = null;
66
67         if ($filename && File::validFilename($filename)) {
68             $path = File::path($filename);
69         }
70
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);
74             return false;
75         }
76         if (!is_readable($path)) {
77             // TRANS: Client error displayed when requesting a file without having read access to it.
78             $this->clientError(_('Cannot read file.'), 403);
79             return false;
80         }
81
82         $this->path = $path;
83         return true;
84     }
85
86     /**
87      * Is this page read-only?
88      *
89      * @return boolean true
90      */
91     function isReadOnly($args)
92     {
93         return true;
94     }
95
96     /**
97      * Last-modified date for file
98      *
99      * @return int last-modified date as unix timestamp
100      */
101     function lastModified()
102     {
103         if (common_config('site', 'use_x_sendfile')) {
104             return null;
105         }
106
107         return filemtime($this->path);
108     }
109
110     /**
111      * etag for file
112      *
113      * This returns the same data (inode, size, mtime) as Apache would,
114      * but in decimal instead of hex.
115      *
116      * @return string etag http header
117      */
118     function etag()
119     {
120         if (common_config('site', 'use_x_sendfile')) {
121             return null;
122         }
123
124         $cache = Cache::instance();
125         if($cache) {
126             $key = Cache::key('attachments:etag:' . $this->path);
127             $etag = $cache->get($key);
128             if($etag === false) {
129                 $etag = crc32(file_get_contents($this->path));
130                 $cache->set($key,$etag);
131             }
132             return $etag;
133         }
134
135         $stat = stat($this->path);
136         return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
137     }
138
139     /**
140      * Handle input, produce output
141      *
142      * @return void
143      */
144     protected function handle()
145     {
146         // undo headers set by PHP sessions
147         $sec = session_cache_expire() * 60;
148         header('Expires: ' . date(DATE_RFC1123, time() + $sec));
149         header('Cache-Control: max-age=' . $sec);
150
151         parent::handle();
152
153         $path = $this->path;
154
155         $finfo = new finfo(FILEINFO_MIME_TYPE);
156
157         header('Content-Type: ' . $finfo->file($path));
158
159         if (common_config('site', 'use_x_sendfile')) {
160             header('X-Sendfile: ' . $path);
161         } else {
162             header('Content-Length: ' . filesize($path));
163             readfile($path);
164         }
165     }
166 }