attachment.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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() || $this->attachment->isFetchedRemoteFile()) {
  73. $this->filesize = $this->attachment->getFileOrThumbnailSize();
  74. $this->mimetype = $this->attachment->getFileOrThumbnailMimetype();
  75. $this->filename = MediaFile::getDisplayName($this->attachment);
  76. }
  77. return true;
  78. }
  79. /**
  80. * Is this action read-only?
  81. *
  82. * @return bool true
  83. */
  84. public function isReadOnly($args): bool
  85. {
  86. return true;
  87. }
  88. /**
  89. * Title of the page
  90. *
  91. * @return string title of the page
  92. */
  93. public function title(): string
  94. {
  95. $a = new Attachment($this->attachment);
  96. return $a->title();
  97. }
  98. public function showPage(): void
  99. {
  100. parent::showPage();
  101. }
  102. /**
  103. * Fill the content area of the page
  104. *
  105. * Shows a single notice list item.
  106. *
  107. * @return void
  108. */
  109. public function showContent(): void
  110. {
  111. $ali = new Attachment($this->attachment, $this);
  112. $ali->show();
  113. }
  114. /**
  115. * Don't show page notice
  116. *
  117. * @return void
  118. */
  119. public function showPageNoticeBlock(): void
  120. {
  121. }
  122. /**
  123. * Show aside: this attachments appears in what notices
  124. *
  125. * @return void
  126. */
  127. public function showSections(): void
  128. {
  129. $ns = new AttachmentNoticeSection($this);
  130. $ns->show();
  131. }
  132. /**
  133. * Last-modified date for file
  134. *
  135. * @return int last-modified date as unix timestamp
  136. * @throws ServerException
  137. */
  138. public function lastModified(): ?int
  139. {
  140. if (common_config('site', 'use_x_sendfile')) {
  141. return null;
  142. }
  143. $path = $this->filepath;
  144. if (!empty($path)) {
  145. return filemtime($path);
  146. } else {
  147. return null;
  148. }
  149. }
  150. /**
  151. * etag header for file
  152. *
  153. * This returns the same data (inode, size, mtime) as Apache would,
  154. * but in decimal instead of hex.
  155. *
  156. * @return string etag http header
  157. * @throws ServerException
  158. */
  159. public function etag(): ?string
  160. {
  161. if (common_config('site', 'use_x_sendfile')) {
  162. return null;
  163. }
  164. $path = $this->filepath;
  165. $cache = Cache::instance();
  166. if ($cache) {
  167. if (empty($path)) {
  168. return null;
  169. }
  170. $key = Cache::key('attachments:etag:' . $path);
  171. $etag = $cache->get($key);
  172. if ($etag === false) {
  173. $etag = crc32(file_get_contents($path));
  174. $cache->set($key, $etag);
  175. }
  176. return $etag;
  177. }
  178. if (!empty($path)) {
  179. $stat = stat($path);
  180. return '"' . $stat['ino'] . '-' . $stat['size'] . '-' . $stat['mtime'] . '"';
  181. } else {
  182. return null;
  183. }
  184. }
  185. }