3 * StatusNet, the distributed open-source microblogging tool
5 * widget for displaying a list of notice attachments
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.
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.
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/>.
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/
31 if (!defined('GNUSOCIAL')) { exit(1); }
34 * widget for displaying a single notice
36 * This widget has the core smarts for showing a single notice: what to display,
37 * where, and under which circumstances. Its key method is show(); this is a recipe
38 * that calls all the other show*() methods to build up a single notice. The
39 * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
40 * author info (since that's implicit by the data in the page).
44 * @author Evan Prodromou <evan@status.net>
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://status.net/
48 * @see ProfileNoticeListItem
50 class AttachmentListItem extends Widget
52 /** The attachment this item will show. */
54 var $attachment = null;
57 * @param File $attachment the attachment we will display
59 function __construct(File $attachment, $out=null)
61 parent::__construct($out);
62 $this->attachment = $attachment;
66 return $this->attachment->getTitle() ?: _('Untitled attachment');
69 function linkTitle() {
70 return $this->title();
74 * recipe function for displaying a single notice.
76 * This uses all the other methods to correctly display a notice. Override
77 * it or one of the others to fine-tune the output.
85 $this->showNoticeAttachment();
86 } catch (Exception $e) {
87 $this->element('div', ['class'=>'error'], $e->getMessage());
88 common_debug($e->getMessage());
96 'href' => $this->attachment->getAttachmentUrl(),
97 'title' => $this->linkTitle());
100 function showNoticeAttachment()
102 $this->showRepresentation();
105 function showRepresentation() {
106 $enclosure = $this->attachment->getEnclosure();
108 if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
110 $this->out->elementStart('label');
111 $this->out->element('a', $this->linkAttr(), $this->title());
112 $this->out->elementEnd('label');
114 if (!empty($enclosure->mimetype)) {
115 // First, prepare a thumbnail if it exists.
118 // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
119 $thumb = $this->attachment->getThumbnail(null, null, false, false);
120 } catch (UseFileAsThumbnailException $e) {
122 } catch (UnsupportedMediaException $e) {
123 // FIXME: Show a good representation of unsupported/unshowable images
127 // Then get the kind of mediatype we're dealing with
128 $mediatype = common_get_mime_media($enclosure->mimetype);
130 // FIXME: Get proper mime recognition of Ogg files! If system has 'mediainfo', this should do it:
131 // $ mediainfo --inform='General;%InternetMediaType%'
132 if ($this->attachment->mimetype === 'application/ogg') {
133 $mediatype = 'video'; // because this element can handle Ogg/Vorbis etc. on its own
136 // Ugly hack to show text/html links which have a thumbnail (such as from oEmbed/OpenGraph image URLs)
137 if (!in_array($mediatype, ['image','audio','video']) && $thumb instanceof File_thumbnail) {
138 $mediatype = 'image';
141 switch ($mediatype) {
142 // Anything we understand as an image, if we need special treatment, do it in StartShowAttachmentRepresentation
144 if ($thumb instanceof File_thumbnail) {
145 $this->out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo', 'alt' => '']));
148 // getUrl(true) because we don't want to hotlink, could be made configurable
149 $this->out->element('img', ['class'=>'u-photo', 'src'=>$this->attachment->getUrl(true), 'alt' => $this->attachment->getTitle()]);
150 } catch (FileNotStoredLocallyException $e) {
151 $url = $e->file->getUrl(false);
152 $this->out->element('a', ['href'=>$url, 'rel'=>'external'], $url);
155 unset($thumb); // there's no need carrying this along after this
158 // HTML5 media elements
161 if ($thumb instanceof File_thumbnail) {
162 $poster = $thumb->getUrl();
163 unset($thumb); // there's no need carrying this along after this
168 $this->out->elementStart($mediatype,
169 array('class'=>"attachment_player u-{$mediatype}",
171 'controls'=>'controls'));
172 $this->out->element('source',
173 array('src'=>$this->attachment->getUrl(),
174 'type'=>$this->attachment->mimetype));
175 $this->out->elementEnd($mediatype);
179 unset($thumb); // there's no need carrying this along
180 switch (common_bare_mime($this->attachment->mimetype)) {
182 $this->element('div', ['class'=>'e-content plaintext'], file_get_contents($this->attachment->getPath()));
185 if (!empty($this->attachment->filename)
186 && (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
187 // Locally-uploaded HTML. Scrub and display inline.
188 $this->showHtmlFile($this->attachment);
191 // Fall through to default if it wasn't a _local_ text/html File object
193 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
197 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
200 Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
203 protected function showHtmlFile(File $attachment)
205 $body = $this->scrubHtmlFile($attachment);
207 $this->out->raw($body);
212 * @return mixed false on failure, HTML fragment string on success
214 protected function scrubHtmlFile(File $attachment)
216 $path = $attachment->getPath();
217 $raw = file_get_contents($path);
220 $dom = new DOMDocument();
221 if(!$dom->loadHTML($raw)) {
222 common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
226 // Remove <script>s or htmlawed will dump their contents into output!
227 // Note: removing child nodes while iterating seems to mess things up,
228 // hence the double loop.
230 foreach ($dom->getElementsByTagName('script') as $script) {
231 $scripts[] = $script;
233 foreach ($scripts as $script) {
234 common_log(LOG_DEBUG, $script->textContent);
235 $script->parentNode->removeChild($script);
238 // Trim out everything outside the body...
239 $body = $dom->saveHTML();
240 $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
241 $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
243 require_once INSTALLDIR.'/extlib/HTMLPurifier/HTMLPurifier.auto.php';
244 $purifier = new HTMLPurifier();
245 return $purifier->purify($body);
249 * start a single notice.
256 // TODO: add notice_type class e.g., notice_video, notice_image
257 $this->out->elementStart('li');
263 * Close the last elements in the notice list item
269 $this->out->elementEnd('li');