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