]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlistitem.php
Merge remote-tracking branch 'upstream/master' into social-master
[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();
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('class' => 'attachment',
90                      'href' => $this->attachment->getUrl(),
91                      'id' => 'attachment-' . $this->attachment->id,
92                      'title' => $this->linkTitle());
93     }
94
95     function showLink() {
96         $this->out->elementStart('a', $this->linkAttr());
97         $this->out->element('span', null, $this->linkTitle());
98         $this->showRepresentation();
99         $this->out->elementEnd('a');
100     }
101
102     function showNoticeAttachment()
103     {
104         $this->showLink();
105     }
106
107     function showRepresentation() {
108         if (Event::handle('StartShowAttachmentRepresentation', array($this->out, $this->attachment))) {
109             if (!empty($this->attachment->mimetype)) {
110                 $mediatype = common_get_mime_media($this->attachment->mimetype);
111
112                 // FIXME: Get proper mime recognition of Ogg files! If system has 'mediainfo', this should do it:
113                 // $ mediainfo --inform='General;%InternetMediaType%'
114                 if ($this->attachment->mimetype === 'application/ogg') {
115                     $mediatype = 'video';   // because this element can handle Ogg/Vorbis etc. on its own
116                 }
117                 switch ($mediatype) {
118                 // Anything we understand as an image, if we need special treatment, do it in StartShowAttachmentRepresentation
119                 case 'image':
120                     try {
121                         // Tell getThumbnail that we can show an animated image if it has one (4th arg, "force_still")
122                         $thumb = $this->attachment->getThumbnail(null, null, false, false);
123                         $this->out->element('img', $thumb->getHtmlAttrs(['class'=>'u-photo', 'alt' => '']));
124                     } catch (UseFileAsThumbnailException $e) {
125                         $this->out->element('img', array('class'=>'u-photo', 'src' => $e->file->getUrl(), 'alt' => $e->file->title));
126                     } catch (UnsupportedMediaException $e) {
127                         // FIXME: Show a good representation of unsupported/unshowable images
128                     }
129                     break;
130
131                 // HTML5 media elements
132                 case 'audio':
133                 case 'video':
134                     try {
135                         $thumb = $this->attachment->getThumbnail();
136                         $poster = $thumb->getUrl();
137                         unset ($thumb);
138                     } catch (Exception $e) {
139                         $poster = null;
140                     }
141                     $this->out->elementStart($mediatype,
142                                         array('class'=>"attachment_player u-{$mediatype}",
143                                             'poster'=>$poster,
144                                             'controls'=>'controls'));
145                     $this->out->element('source',
146                                         array('src'=>$this->attachment->getUrl(),
147                                             'type'=>$this->attachment->mimetype));
148                     $this->out->elementEnd($mediatype);
149                     break;
150
151                 default:
152                     switch ($this->attachment->mimetype) {
153                     case 'text/html':
154                         if (!empty($this->attachment->filename)
155                                 && (GNUsocial::isAjax() || common_config('attachments', 'show_html'))) {
156                             // Locally-uploaded HTML. Scrub and display inline.
157                             $this->showHtmlFile($this->attachment);
158                             break;
159                         }
160                         // Fall through to default if it wasn't a _local_ text/html File object
161                     default:
162                         Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
163                     }
164                 }
165             } else {
166                 Event::handle('ShowUnsupportedAttachmentRepresentation', array($this->out, $this->attachment));
167             }
168         }
169         Event::handle('EndShowAttachmentRepresentation', array($this->out, $this->attachment));
170     }
171
172     protected function showHtmlFile(File $attachment)
173     {
174         $body = $this->scrubHtmlFile($attachment);
175         if ($body) {
176             $this->out->raw($body);
177         }
178     }
179
180     /**
181      * @return mixed false on failure, HTML fragment string on success
182      */
183     protected function scrubHtmlFile(File $attachment)
184     {
185         $path = File::path($attachment->filename);
186         if (!file_exists($path) || !is_readable($path)) {
187             common_log(LOG_ERR, "Missing local HTML attachment $path");
188             return false;
189         }
190         $raw = file_get_contents($path);
191
192         // Normalize...
193         $dom = new DOMDocument();
194         if(!$dom->loadHTML($raw)) {
195             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
196             return false;
197         }
198
199         // Remove <script>s or htmlawed will dump their contents into output!
200         // Note: removing child nodes while iterating seems to mess things up,
201         // hence the double loop.
202         $scripts = array();
203         foreach ($dom->getElementsByTagName('script') as $script) {
204             $scripts[] = $script;
205         }
206         foreach ($scripts as $script) {
207             common_debug($script->textContent);
208             $script->parentNode->removeChild($script);
209         }
210
211         // Trim out everything outside the body...
212         $body = $dom->saveHTML();
213         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
214         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
215
216         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
217         $config = array('safe' => 1,
218                         'deny_attribute' => 'id,style,on*',
219                         'comment' => 1); // remove comments
220         $scrubbed = htmLawed($body, $config);
221
222         return $scrubbed;
223     }
224
225     /**
226      * start a single notice.
227      *
228      * @return void
229      */
230     function showStart()
231     {
232         // XXX: RDFa
233         // TODO: add notice_type class e.g., notice_video, notice_image
234         $this->out->elementStart('li');
235     }
236
237     /**
238      * finish the notice
239      *
240      * Close the last elements in the notice list item
241      *
242      * @return void
243      */
244     function showEnd()
245     {
246         $this->out->elementEnd('li');
247     }
248 }