attachmentlistitem.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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() ?: _('Untitled 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. $this->showNoticeAttachment();
  78. $this->showEnd();
  79. }
  80. function linkAttr() {
  81. return array(
  82. 'class' => 'u-url',
  83. 'href' => $this->attachment->getAttachmentUrl(),
  84. 'title' => $this->linkTitle());
  85. }
  86. function showNoticeAttachment()
  87. {
  88. $this->showRepresentation();
  89. }
  90. function showRepresentation() {
  91. $enclosure = $this->attachment->getEnclosure();
  92. if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
  93. $this->out->elementStart('label');
  94. $this->out->element('a', $this->linkAttr(), $this->title());
  95. $this->out->elementEnd('label');
  96. if (!empty($enclosure->mimetype)) {
  97. // First, prepare a thumbnail if it exists.
  98. $thumb = null;
  99. try {
  100. // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
  101. $thumb = $this->attachment->getThumbnail(null, null, false, false);
  102. } catch (UseFileAsThumbnailException $e) {
  103. $thumb = null;
  104. } catch (UnsupportedMediaException $e) {
  105. // FIXME: Show a good representation of unsupported/unshowable images
  106. $thumb = null;
  107. }
  108. // Then get the kind of mediatype we're dealing with
  109. $mediatype = common_get_mime_media($enclosure->mimetype);
  110. // FIXME: Get proper mime recognition of Ogg files! If system has 'mediainfo', this should do it:
  111. // $ mediainfo --inform='General;%InternetMediaType%'
  112. if ($this->attachment->mimetype === 'application/ogg') {
  113. $mediatype = 'video'; // because this element can handle Ogg/Vorbis etc. on its own
  114. }
  115. // Ugly hack to show text/html links which have a thumbnail (such as from oEmbed/OpenGraph image URLs)
  116. if (!in_array($mediatype, ['image','audio','video']) && $thumb instanceof File_thumbnail) {
  117. $mediatype = 'image';
  118. }
  119. switch ($mediatype) {
  120. // Anything we understand as an image, if we need special treatment, do it in StartShowAttachmentRepresentation
  121. case 'image':
  122. if ($thumb instanceof File_thumbnail) {
  123. $this->out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo', 'alt' => '']));
  124. } else {
  125. try {
  126. // getUrl(true) because we don't want to hotlink, could be made configurable
  127. $this->out->element('img', ['class'=>'u-photo', 'src'=>$this->attachment->getUrl(true), 'alt' => $this->attachment->getTitle()]);
  128. } catch (FileNotStoredLocallyException $e) {
  129. $url = $e->file->getUrl(false);
  130. $this->out->element('a', ['href'=>$url, 'rel'=>'external'], $url);
  131. }
  132. }
  133. unset($thumb); // there's no need carrying this along after this
  134. break;
  135. // HTML5 media elements
  136. case 'audio':
  137. case 'video':
  138. if ($thumb instanceof File_thumbnail) {
  139. $poster = $thumb->getUrl();
  140. unset($thumb); // there's no need carrying this along after this
  141. } else {
  142. $poster = null;
  143. }
  144. $this->out->elementStart($mediatype,
  145. array('class'=>"attachment_player u-{$mediatype}",
  146. 'poster'=>$poster,
  147. 'controls'=>'controls'));
  148. $this->out->element('source',
  149. array('src'=>$this->attachment->getUrl(),
  150. 'type'=>$this->attachment->mimetype));
  151. $this->out->elementEnd($mediatype);
  152. break;
  153. default:
  154. unset($thumb); // there's no need carrying this along
  155. switch (common_bare_mime($this->attachment->mimetype)) {
  156. case 'text/plain':
  157. $this->element('div', ['class'=>'e-content plaintext'], file_get_contents($this->attachment->getPath()));
  158. break;
  159. case 'text/html':
  160. if (!empty($this->attachment->filename)
  161. && (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
  162. // Locally-uploaded HTML. Scrub and display inline.
  163. $this->showHtmlFile($this->attachment);
  164. break;
  165. }
  166. // Fall through to default if it wasn't a _local_ text/html File object
  167. default:
  168. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  169. }
  170. }
  171. } else {
  172. Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
  173. }
  174. }
  175. Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
  176. }
  177. protected function showHtmlFile(File $attachment)
  178. {
  179. $body = $this->scrubHtmlFile($attachment);
  180. if ($body) {
  181. $this->out->raw($body);
  182. }
  183. }
  184. /**
  185. * @return mixed false on failure, HTML fragment string on success
  186. */
  187. protected function scrubHtmlFile(File $attachment)
  188. {
  189. $path = $attachment->getPath();
  190. $raw = file_get_contents($path);
  191. // Normalize...
  192. $dom = new DOMDocument();
  193. if(!$dom->loadHTML($raw)) {
  194. common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
  195. return false;
  196. }
  197. // Remove <script>s or htmlawed will dump their contents into output!
  198. // Note: removing child nodes while iterating seems to mess things up,
  199. // hence the double loop.
  200. $scripts = array();
  201. foreach ($dom->getElementsByTagName('script') as $script) {
  202. $scripts[] = $script;
  203. }
  204. foreach ($scripts as $script) {
  205. common_log(LOG_DEBUG, $script->textContent);
  206. $script->parentNode->removeChild($script);
  207. }
  208. // Trim out everything outside the body...
  209. $body = $dom->saveHTML();
  210. $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
  211. $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
  212. require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
  213. $purifier = new HTMLPurifier();
  214. return $purifier->purify($body);
  215. }
  216. /**
  217. * start a single notice.
  218. *
  219. * @return void
  220. */
  221. function showStart()
  222. {
  223. // XXX: RDFa
  224. // TODO: add notice_type class e.g., notice_video, notice_image
  225. $this->out->elementStart('li');
  226. }
  227. /**
  228. * finish the notice
  229. *
  230. * Close the last elements in the notice list item
  231. *
  232. * @return void
  233. */
  234. function showEnd()
  235. {
  236. $this->out->elementEnd('li');
  237. }
  238. }