attachmentlistitem.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * widget for displaying a list of notice attachments
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category UI
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2008 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * widget for displaying a single notice
  33. *
  34. * This widget has the core smarts for showing a single notice: what to display,
  35. * where, and under which circumstances. Its key method is show(); this is a recipe
  36. * that calls all the other show*() methods to build up a single notice. The
  37. * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
  38. * author info (since that's implicit by the data in the page).
  39. *
  40. * @category UI
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. * @see NoticeList
  46. * @see ProfileNoticeListItem
  47. */
  48. class AttachmentListItem extends Widget
  49. {
  50. /** The attachment this item will show. */
  51. var $attachment = null;
  52. /**
  53. * @param File $attachment the attachment we will display
  54. */
  55. function __construct(File $attachment, $out=null)
  56. {
  57. parent::__construct($out);
  58. $this->attachment = $attachment;
  59. }
  60. function title() {
  61. return $this->attachment->getTitle() ?: MediaFile::getDisplayName($this->attachment);
  62. }
  63. function linkTitle() {
  64. return $this->title();
  65. }
  66. /**
  67. * recipe function for displaying a single notice.
  68. *
  69. * This uses all the other methods to correctly display a notice. Override
  70. * it or one of the others to fine-tune the output.
  71. *
  72. * @return void
  73. */
  74. function show()
  75. {
  76. $this->showStart();
  77. try {
  78. $this->showNoticeAttachment();
  79. } catch (Exception $e) {
  80. $this->element('div', ['class'=>'error'], $e->getMessage());
  81. common_debug($e->getMessage());
  82. }
  83. $this->showEnd();
  84. }
  85. function linkAttr() {
  86. return [
  87. 'class' => 'u-url',
  88. 'href' => $this->attachment->getAttachmentDownloadUrl(),
  89. 'title' => $this->linkTitle()
  90. ];
  91. }
  92. function showNoticeAttachment()
  93. {
  94. $this->showRepresentation();
  95. }
  96. function showRepresentation() {
  97. $enclosure = $this->attachment->getEnclosure();
  98. if (Event::handle('StartShowAttachmentRepresentation', [$this->out, $this->attachment])) {
  99. $this->out->elementStart('label');
  100. $this->out->element('a', ['rel' => 'external', 'href' => $this->attachment->getAttachmentUrl()], $this->title());
  101. $this->out->elementEnd('label');
  102. $this->out->element('br');
  103. try {
  104. if (!empty($enclosure->mimetype)) {
  105. // First, prepare a thumbnail if it exists.
  106. $thumb = null;
  107. try {
  108. // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
  109. $thumb = File_thumbnail::fromFileObject($this->attachment, null, null, false, false);
  110. } catch (UseFileAsThumbnailException $e) {
  111. $thumb = null;
  112. } catch (UnsupportedMediaException $e) {
  113. // FIXME: Show a good representation of unsupported/unshowable images
  114. $thumb = null;
  115. } catch (FileNotFoundException $e) {
  116. // Remote file
  117. $thumb = null;
  118. }
  119. // Then get the kind of mediatype we're dealing with
  120. $mediatype = common_get_mime_media($enclosure->mimetype);
  121. // FIXME: Get proper mime recognition of Ogg files! If system has 'mediainfo', this should do it:
  122. // $ mediainfo --inform='General;%InternetMediaType%'
  123. if ($this->attachment->mimetype === 'application/ogg') {
  124. $mediatype = 'video'; // because this element can handle Ogg/Vorbis etc. on its own
  125. }
  126. // Ugly hack to show text/html links which have a thumbnail (such as from oEmbed/OpenGraph image URLs)
  127. if (!in_array($mediatype, ['image', 'audio', 'video']) && $thumb instanceof File_thumbnail) {
  128. $mediatype = 'image';
  129. }
  130. switch ($mediatype) {
  131. // Anything we understand as an image, if we need special treatment, do it in StartShowAttachmentRepresentation
  132. case 'image':
  133. if ($thumb instanceof File_thumbnail) {
  134. $this->out->element('img', $thumb->getHtmlAttrs(['class' => 'u-photo', 'alt' => '']));
  135. } else {
  136. try {
  137. // getUrl(true) because we don't want to hotlink, could be made configurable
  138. $this->out->element('img', ['class' => 'u-photo',
  139. 'src' => $this->attachment->getUrl(true),
  140. 'alt' => $this->attachment->getTitle()]);
  141. } catch (FileNotStoredLocallyException $e) {
  142. $url = $e->file->getUrl(false);
  143. $this->out->element('a', ['href' => $url, 'rel' => 'external'], $url);
  144. }
  145. }
  146. unset($thumb); // there's no need carrying this along after this
  147. break;
  148. // HTML5 media elements
  149. case 'audio':
  150. case 'video':
  151. if ($thumb instanceof File_thumbnail) {
  152. $poster = $thumb->getUrl();
  153. unset($thumb); // there's no need carrying this along after this
  154. } else {
  155. $poster = null;
  156. }
  157. $this->out->elementStart($mediatype,
  158. array('class' => "attachment_player u-{$mediatype}",
  159. 'poster' => $poster,
  160. 'controls' => 'controls'));
  161. $this->out->element('source',
  162. array('src' => $this->attachment->getUrl(),
  163. 'type' => $this->attachment->mimetype));
  164. $this->out->elementEnd($mediatype);
  165. break;
  166. default:
  167. unset($thumb); // there's no need carrying this along
  168. switch (common_bare_mime($this->attachment->mimetype)) {
  169. case 'text/plain':
  170. $this->element('div', ['class' => 'e-content plaintext'],
  171. file_get_contents($this->attachment->getPath()));
  172. break;
  173. case 'text/html':
  174. if (!empty($this->attachment->filename)
  175. && (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
  176. // Locally-uploaded HTML. Scrub and display inline.
  177. $this->showHtmlFile($this->attachment);
  178. break;
  179. }
  180. // Fall through to default if it wasn't a _local_ text/html File object
  181. default:
  182. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  183. }
  184. }
  185. } else {
  186. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  187. }
  188. } catch (FileNotFoundException $e) {
  189. if (!$this->attachment->isLocal()) {
  190. throw $e;
  191. }
  192. }
  193. }
  194. Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
  195. }
  196. protected function showHtmlFile(File $attachment)
  197. {
  198. $body = $this->scrubHtmlFile($attachment);
  199. if ($body) {
  200. $this->out->raw($body);
  201. }
  202. }
  203. /**
  204. * @return mixed false on failure, HTML fragment string on success
  205. */
  206. protected function scrubHtmlFile(File $attachment)
  207. {
  208. $path = $attachment->getPath();
  209. $raw = file_get_contents($path);
  210. // Normalize...
  211. $dom = new DOMDocument();
  212. if(!$dom->loadHTML($raw)) {
  213. common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
  214. return false;
  215. }
  216. // Remove <script>s or htmlawed will dump their contents into output!
  217. // Note: removing child nodes while iterating seems to mess things up,
  218. // hence the double loop.
  219. $scripts = array();
  220. foreach ($dom->getElementsByTagName('script') as $script) {
  221. $scripts[] = $script;
  222. }
  223. foreach ($scripts as $script) {
  224. common_log(LOG_DEBUG, $script->textContent);
  225. $script->parentNode->removeChild($script);
  226. }
  227. // Trim out everything outside the body...
  228. $body = $dom->saveHTML();
  229. $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
  230. $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
  231. require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
  232. $purifier = new HTMLPurifier();
  233. return $purifier->purify($body);
  234. }
  235. /**
  236. * start a single notice.
  237. *
  238. * @return void
  239. */
  240. function showStart()
  241. {
  242. // XXX: RDFa
  243. // TODO: add notice_type class e.g., notice_video, notice_image
  244. $this->out->elementStart('li');
  245. }
  246. /**
  247. * finish the notice
  248. *
  249. * Close the last elements in the notice list item
  250. *
  251. * @return void
  252. */
  253. function showEnd()
  254. {
  255. $this->out->elementEnd('li');
  256. }
  257. }