123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class GetfileAction extends Action
- {
-
- var $path = null;
-
- protected function prepare(array $args=array())
- {
- parent::prepare($args);
- $filename = $this->trimmed('filename');
- $path = null;
- if ($filename && File::validFilename($filename)) {
- $path = File::path($filename);
- }
- if (empty($path) or !file_exists($path)) {
-
- $this->clientError(_('No such file.'), 404);
- }
- if (!is_readable($path)) {
-
- $this->clientError(_('Cannot read file.'), 403);
- }
- $this->path = $path;
- return true;
- }
-
- function isReadOnly($args)
- {
- return true;
- }
-
- function lastModified()
- {
- if (common_config('site', 'use_x_sendfile')) {
- return null;
- }
- return filemtime($this->path);
- }
-
- function etag()
- {
- if (common_config('site', 'use_x_sendfile')) {
- return null;
- }
- $cache = Cache::instance();
- if($cache) {
- $key = Cache::key('attachments:etag:' . $this->path);
- $etag = $cache->get($key);
- if($etag === false) {
- $etag = crc32(file_get_contents($this->path));
- $cache->set($key,$etag);
- }
- return $etag;
- }
- $stat = stat($this->path);
- return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
- }
-
- protected function handle()
- {
-
- $sec = session_cache_expire() * 60;
- header('Expires: ' . date(DATE_RFC1123, time() + $sec));
- header('Cache-Control: max-age=' . $sec);
- parent::handle();
- $path = $this->path;
- $finfo = new finfo(FILEINFO_MIME_TYPE);
- header('Content-Type: ' . $finfo->file($path));
- if (common_config('site', 'use_x_sendfile')) {
- header('X-Sendfile: ' . $path);
- } else {
- header('Content-Length: ' . filesize($path));
- readfile($path);
- }
- }
- }
|