attachment.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 StatusNet
  22. * @author Evan Prodromou <evan@status.net>
  23. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  24. * @link http://status.net/
  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($id);
  57. } elseif (!empty($this->filehash = $this->trimmed('filehash'))) {
  58. $this->attachment = File::getByHash($this->filehash);
  59. }
  60. } catch (Exception $e) {
  61. // Not found
  62. }
  63. if (!$this->attachment instanceof File) {
  64. // TRANS: Client error displayed trying to get a non-existing attachment.
  65. $this->clientError(_m('No such attachment.'), 404);
  66. }
  67. $this->filepath = $this->attachment->getFileOrThumbnailPath();
  68. if (empty($this->filepath)) {
  69. $this->clientError(_m('Requested local URL for a file that is not stored locally.'), 404);
  70. }
  71. $this->filesize = $this->attachment->getFileOrThumbnailSize();
  72. $this->mimetype = $this->attachment->getFileOrThumbnailMimetype();
  73. $this->filename = MediaFile::getDisplayName($this->attachment);
  74. return true;
  75. }
  76. /**
  77. * Is this action read-only?
  78. *
  79. * @return bool true
  80. */
  81. public function isReadOnly($args): bool
  82. {
  83. return true;
  84. }
  85. /**
  86. * Title of the page
  87. *
  88. * @return string title of the page
  89. */
  90. public function title(): string
  91. {
  92. $a = new Attachment($this->attachment);
  93. return $a->title();
  94. }
  95. public function showPage(): void
  96. {
  97. if (empty($this->filepath)) {
  98. // if it's not a local file, gtfo
  99. common_redirect($this->attachment->getUrl(), 303);
  100. }
  101. parent::showPage();
  102. }
  103. /**
  104. * Fill the content area of the page
  105. *
  106. * Shows a single notice list item.
  107. *
  108. * @return void
  109. */
  110. public function showContent(): void
  111. {
  112. $ali = new Attachment($this->attachment, $this);
  113. $ali->show();
  114. }
  115. /**
  116. * Don't show page notice
  117. *
  118. * @return void
  119. */
  120. public function showPageNoticeBlock(): void
  121. {
  122. }
  123. /**
  124. * Show aside: this attachments appears in what notices
  125. *
  126. * @return void
  127. */
  128. public function showSections(): void
  129. {
  130. $ns = new AttachmentNoticeSection($this);
  131. $ns->show();
  132. }
  133. /**
  134. * Last-modified date for file
  135. *
  136. * @return int last-modified date as unix timestamp
  137. * @throws ServerException
  138. */
  139. public function lastModified(): ?int
  140. {
  141. if (common_config('site', 'use_x_sendfile')) {
  142. return null;
  143. }
  144. $path = $this->filepath;
  145. if (!empty($path)) {
  146. return filemtime($path);
  147. } else {
  148. return null;
  149. }
  150. }
  151. /**
  152. * etag header for file
  153. *
  154. * This returns the same data (inode, size, mtime) as Apache would,
  155. * but in decimal instead of hex.
  156. *
  157. * @return string etag http header
  158. * @throws ServerException
  159. */
  160. public function etag(): ?string
  161. {
  162. if (common_config('site', 'use_x_sendfile')) {
  163. return null;
  164. }
  165. $path = $this->filepath;
  166. $cache = Cache::instance();
  167. if ($cache) {
  168. if (empty($path)) {
  169. return null;
  170. }
  171. $key = Cache::key('attachments:etag:' . $path);
  172. $etag = $cache->get($key);
  173. if ($etag === false) {
  174. $etag = crc32(file_get_contents($path));
  175. $cache->set($key, $etag);
  176. }
  177. return $etag;
  178. }
  179. if (!empty($path)) {
  180. $stat = stat($path);
  181. return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
  182. } else {
  183. return null;
  184. }
  185. }
  186. }