attachment.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Show notice attachments
  19. *
  20. * @category Personal
  21. * @package GNUsocial
  22. * @author Evan Prodromou <evan@status.net>
  23. * @copyright 2008-2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. class AttachmentAction extends ManagedAction
  27. {
  28. /**
  29. * Attachment File object to show
  30. */
  31. public $attachment = null;
  32. public $filehash = null;
  33. public $filepath = null;
  34. public $filesize = null;
  35. public $mimetype = null;
  36. public $filename = null;
  37. /**
  38. * Load attributes based on database arguments
  39. *
  40. * Loads all the DB stuff
  41. *
  42. * @param array $args $_REQUEST array
  43. *
  44. * @return bool flag
  45. * @throws ClientException
  46. * @throws FileNotFoundException
  47. * @throws FileNotStoredLocallyException
  48. * @throws InvalidFilenameException
  49. * @throws ServerException
  50. */
  51. protected function prepare(array $args = [])
  52. {
  53. parent::prepare($args);
  54. try {
  55. if (!empty($id = $this->trimmed('attachment'))) {
  56. $this->attachment = File::getByID((int) $id);
  57. } elseif (!empty($this->filehash = $this->trimmed('filehash'))) {
  58. $file = File::getByHash($this->filehash);
  59. $file->fetch();
  60. $this->attachment = $file;
  61. }
  62. } catch (Exception $e) {
  63. // Not found
  64. }
  65. if (!$this->attachment instanceof File) {
  66. // TRANS: Client error displayed trying to get a non-existing attachment.
  67. $this->clientError(_m('No such attachment.'), 404);
  68. }
  69. $this->filesize = $this->attachment->size;
  70. $this->mimetype = $this->attachment->mimetype;
  71. $this->filename = $this->attachment->filename;
  72. if ($this->attachment->isLocal()) {
  73. $this->filepath = $this->attachment->getFileOrThumbnailPath();
  74. if (empty($this->filepath)) {
  75. $this->clientError(
  76. _m('Requested local URL for a file that is not stored locally.'),
  77. 404
  78. );
  79. }
  80. $this->filesize = $this->attachment->getFileOrThumbnailSize();
  81. $this->mimetype = $this->attachment->getFileOrThumbnailMimetype();
  82. $this->filename = MediaFile::getDisplayName($this->attachment);
  83. }
  84. return true;
  85. }
  86. /**
  87. * Is this action read-only?
  88. *
  89. * @return bool true
  90. */
  91. public function isReadOnly($args): bool
  92. {
  93. return true;
  94. }
  95. /**
  96. * Title of the page
  97. *
  98. * @return string title of the page
  99. */
  100. public function title(): string
  101. {
  102. $a = new Attachment($this->attachment);
  103. return $a->title();
  104. }
  105. public function showPage(): void
  106. {
  107. if (
  108. !$this->attachment->isLocal()
  109. || empty($this->filepath)
  110. || !file_exists($this->filepath)
  111. ) {
  112. // If it's not a locally stored file, get lost
  113. common_redirect($this->attachment->getUrl(), 303);
  114. }
  115. parent::showPage();
  116. }
  117. /**
  118. * Fill the content area of the page
  119. *
  120. * Shows a single notice list item.
  121. *
  122. * @return void
  123. */
  124. public function showContent(): void
  125. {
  126. $ali = new Attachment($this->attachment, $this);
  127. $ali->show();
  128. }
  129. /**
  130. * Don't show page notice
  131. *
  132. * @return void
  133. */
  134. public function showPageNoticeBlock(): void
  135. {
  136. }
  137. /**
  138. * Show aside: this attachments appears in what notices
  139. *
  140. * @return void
  141. */
  142. public function showSections(): void
  143. {
  144. $ns = new AttachmentNoticeSection($this);
  145. $ns->show();
  146. }
  147. /**
  148. * Last-modified date for file
  149. *
  150. * @return int last-modified date as unix timestamp
  151. * @throws ServerException
  152. */
  153. public function lastModified(): ?int
  154. {
  155. if (common_config('site', 'use_x_sendfile')) {
  156. return null;
  157. }
  158. $path = $this->filepath;
  159. if (!empty($path)) {
  160. return filemtime($path);
  161. } else {
  162. return null;
  163. }
  164. }
  165. /**
  166. * etag header for file
  167. *
  168. * This returns the same data (inode, size, mtime) as Apache would,
  169. * but in decimal instead of hex.
  170. *
  171. * @return string etag http header
  172. * @throws ServerException
  173. */
  174. public function etag(): ?string
  175. {
  176. if (common_config('site', 'use_x_sendfile')) {
  177. return null;
  178. }
  179. $path = $this->filepath;
  180. $cache = Cache::instance();
  181. if ($cache) {
  182. if (empty($path)) {
  183. return null;
  184. }
  185. $key = Cache::key('attachments:etag:' . $path);
  186. $etag = $cache->get($key);
  187. if ($etag === false) {
  188. $etag = crc32(file_get_contents($path));
  189. $cache->set($key, $etag);
  190. }
  191. return $etag;
  192. }
  193. if (!empty($path)) {
  194. $stat = stat($path);
  195. return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
  196. } else {
  197. return null;
  198. }
  199. }
  200. }