]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/getfile.php
Merge branch '0.9.x' into 1.0.x
[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
51 class GetfileAction extends Action
52 {
53     /**
54      * Path of file to return
55      */
56
57     var $path = null;
58
59     /**
60      * Get file name
61      *
62      * @param array $args $_REQUEST array
63      *
64      * @return success flag
65      */
66
67     function prepare($args)
68     {
69         parent::prepare($args);
70
71         $filename = $this->trimmed('filename');
72         $path = null;
73
74         if ($filename && File::validFilename($filename)) {
75             $path = File::path($filename);
76         }
77
78         if (empty($path) or !file_exists($path)) {
79             $this->clientError(_('No such file.'), 404);
80             return false;
81         }
82         if (!is_readable($path)) {
83             $this->clientError(_('Cannot read file.'), 403);
84             return false;
85         }
86
87         $this->path = $path;
88         return true;
89     }
90
91     /**
92      * Is this page read-only?
93      *
94      * @return boolean true
95      */
96
97     function isReadOnly($args)
98     {
99         return true;
100     }
101
102     /**
103      * Last-modified date for file
104      *
105      * @return int last-modified date as unix timestamp
106      */
107
108     function lastModified()
109     {
110         if (common_config('site', 'use_x_sendfile')) {
111             return null;
112         }
113
114         return filemtime($this->path);
115     }
116
117     /**
118      * etag for file
119      *
120      * This returns the same data (inode, size, mtime) as Apache would,
121      * but in decimal instead of hex.
122      *
123      * @return string etag http header
124      */
125
126     function etag()
127     {
128         if (common_config('site', 'use_x_sendfile')) {
129             return null;
130         }
131
132         $cache = Cache::instance();
133         if($cache) {
134             $key = Cache::key('attachments:etag:' . $this->path);
135             $etag = $cache->get($key);
136             if($etag === false) {
137                 $etag = crc32(file_get_contents($this->path));
138                 $cache->set($key,$etag);
139             }
140             return $etag;
141         }
142
143         $stat = stat($this->path);
144         return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
145     }
146
147     /**
148      * Handle input, produce output
149      *
150      * @param array $args $_REQUEST contents
151      *
152      * @return void
153      */
154
155     function handle($args)
156     {
157         // undo headers set by PHP sessions
158         $sec = session_cache_expire() * 60;
159         header('Expires: ' . date(DATE_RFC1123, time() + $sec));
160         header('Cache-Control: max-age=' . $sec);
161
162         parent::handle($args);
163
164         $path = $this->path;
165
166         header('Content-Type: ' . MIME_Type::autoDetect($path));
167
168         if (common_config('site', 'use_x_sendfile')) {
169             header('X-Sendfile: ' . $path);
170         } else {
171             header('Content-Length: ' . filesize($path));
172             readfile($path);
173         }
174     }
175 }