]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
Better fallback on UnsupportedMediaException
[quix0rs-gnu-social.git] / lib / attachmentlist.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') && !defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * widget for displaying a list of notice attachments
37  *
38  * There are a number of actions that display a list of notices, in
39  * reverse chronological order. This widget abstracts out most of the
40  * code for UI for notice lists. It's overridden to hide some
41  * data for e.g. the profile page.
42  *
43  * @category UI
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  * @see      Notice
49  * @see      NoticeListItem
50  * @see      ProfileNoticeList
51  */
52 class AttachmentList extends Widget
53 {
54     /** the current stream of notices being displayed. */
55
56     var $notice = null;
57
58     /**
59      * constructor
60      *
61      * @param Notice $notice stream of notices from DB_DataObject
62      */
63     function __construct($notice, $out=null)
64     {
65         parent::__construct($out);
66         $this->notice = $notice;
67     }
68
69     /**
70      * show the list of attachments
71      *
72      * "Uses up" the stream by looping through it. So, probably can't
73      * be called twice on the same list.
74      *
75      * @return int count of items listed.
76      */
77     function show()
78     {
79         $att = $this->notice->attachments();
80         if (empty($att)) return 0;
81         $this->showListStart();
82
83         foreach ($att as $n=>$attachment) {
84             $item = $this->newListItem($attachment);
85             $item->show();
86         }
87
88         $this->showListEnd();
89
90         return count($att);
91     }
92
93     function showListStart()
94     {
95         $this->out->elementStart('ol', array('class' => 'attachments entry-content'));
96     }
97
98     function showListEnd()
99     {
100         $this->out->elementEnd('ol');
101     }
102
103     /**
104      * returns a new list item for the current attachment
105      *
106      * @param File $attachment the current attachment
107      *
108      * @return AttachmentListItem a list item for displaying the attachment
109      */
110     function newListItem(File $attachment)
111     {
112         return new AttachmentListItem($attachment, $this->out);
113     }
114 }
115
116 /**
117  * widget for displaying a single notice
118  *
119  * This widget has the core smarts for showing a single notice: what to display,
120  * where, and under which circumstances. Its key method is show(); this is a recipe
121  * that calls all the other show*() methods to build up a single notice. The
122  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
123  * author info (since that's implicit by the data in the page).
124  *
125  * @category UI
126  * @package  StatusNet
127  * @author   Evan Prodromou <evan@status.net>
128  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
129  * @link     http://status.net/
130  * @see      NoticeList
131  * @see      ProfileNoticeListItem
132  */
133 class AttachmentListItem extends Widget
134 {
135     /** The attachment this item will show. */
136
137     var $attachment = null;
138
139     var $oembed = null;
140
141     /**
142      * @param File $attachment the attachment we will display
143      */
144     function __construct(File $attachment, $out=null)
145     {
146         parent::__construct($out);
147         $this->attachment  = $attachment;
148         $this->oembed = File_oembed::getKV('file_id', $this->attachment->id);
149     }
150
151     function title() {
152         if (empty($this->attachment->title)) {
153             if (empty($this->oembed->title)) {
154                 $title = $this->attachment->filename;
155             } else {
156                 $title = $this->oembed->title;
157             }
158         } else {
159             $title = $this->attachment->title;
160         }
161
162         return $title;
163     }
164
165     function linkTitle() {
166         return $this->title();
167     }
168
169     /**
170      * recipe function for displaying a single notice.
171      *
172      * This uses all the other methods to correctly display a notice. Override
173      * it or one of the others to fine-tune the output.
174      *
175      * @return void
176      */
177     function show()
178     {
179         $this->showStart();
180         $this->showNoticeAttachment();
181         $this->showEnd();
182     }
183
184     function linkAttr() {
185         return array('class' => 'attachment',
186                      'href' => $this->attachment->url,
187                      'id' => 'attachment-' . $this->attachment->id,
188                      'title' => $this->linkTitle());
189     }
190
191     function showLink() {
192         $this->out->elementStart('a', $this->linkAttr());
193         $this->out->element('span', null, $this->linkTitle());
194         $this->showRepresentation();
195         $this->out->elementEnd('a');
196     }
197
198     function showNoticeAttachment()
199     {
200         $this->showLink();
201     }
202
203     function showRepresentation() {
204         try {
205             $thumb = $this->attachment->getThumbnail();
206             $this->out->element('img', array('alt' => '', 'src' => $thumb->getUrl(), 'width' => $thumb->width, 'height' => $thumb->height));
207         } catch (UnsupportedMediaException $e) {
208             // Image representation unavailable
209         }
210     }
211
212     /**
213      * start a single notice.
214      *
215      * @return void
216      */
217     function showStart()
218     {
219         // XXX: RDFa
220         // TODO: add notice_type class e.g., notice_video, notice_image
221         $this->out->elementStart('li');
222     }
223
224     /**
225      * finish the notice
226      *
227      * Close the last elements in the notice list item
228      *
229      * @return void
230      */
231     function showEnd()
232     {
233         $this->out->elementEnd('li');
234     }
235 }
236
237 /**
238  * used for one-off attachment action
239  */
240 class Attachment extends AttachmentListItem
241 {
242     function showLink() {
243         $this->out->elementStart('div', array('id' => 'attachment_view',
244                                               'class' => 'hentry'));
245         $this->out->elementStart('div', 'entry-title');
246         $this->out->element('a', $this->linkAttr(), $this->linkTitle());
247         $this->out->elementEnd('div');
248
249         $this->out->elementStart('div', 'entry-content');
250         $this->showRepresentation();
251         $this->out->elementEnd('div');
252
253         if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
254             $this->out->elementStart('div', array('id' => 'oembed_info',
255                                                   'class' => 'entry-content'));
256             if (!empty($this->oembed->author_name)) {
257                 $this->out->elementStart('div', 'fn vcard author');
258                 if (empty($this->oembed->author_url)) {
259                     $this->out->text($this->oembed->author_name);
260                 } else {
261                     $this->out->element('a', array('href' => $this->oembed->author_url,
262                                                    'class' => 'url'), $this->oembed->author_name);
263                 }
264             }
265             if (!empty($this->oembed->provider)) {
266                 $this->out->elementStart('div', 'fn vcard');
267                 if (empty($this->oembed->provider_url)) {
268                     $this->out->text($this->oembed->provider);
269                 } else {
270                     $this->out->element('a', array('href' => $this->oembed->provider_url,
271                                                    'class' => 'url'), $this->oembed->provider);
272                 }
273             }
274             $this->out->elementEnd('div');
275         }
276         $this->out->elementEnd('div');
277     }
278
279     function show() {
280         $this->showNoticeAttachment();
281     }
282
283     function linkAttr() {
284         return array('rel' => 'external', 'href' => $this->attachment->url);
285     }
286
287     function linkTitle() {
288         return $this->attachment->url;
289     }
290
291     function showRepresentation() {
292         if (empty($this->oembed->type)) {
293             if (empty($this->attachment->mimetype)) {
294                 $this->showFallback();
295             } else {
296                 switch ($this->attachment->mimetype) {
297                 case 'image/gif':
298                 case 'image/png':
299                 case 'image/jpg':
300                 case 'image/jpeg':
301                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
302                     break;
303
304                 case 'application/ogg':
305                     $arr  = array('type' => $this->attachment->mimetype,
306                         'data' => $this->attachment->url,
307                         'width' => 320,
308                         'height' => 240
309                     );
310                     $this->out->elementStart('object', $arr);
311                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
312                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
313                     $this->out->elementEnd('object');
314                     break;
315
316                 case 'audio/ogg':
317                 case 'audio/x-speex':
318                 case 'video/mpeg':
319                 case 'audio/mpeg':
320                 case 'video/mp4':
321                 case 'video/ogg':
322                 case 'video/quicktime':
323                 case 'video/webm':
324                     $mediatype = common_get_mime_media($this->attachment->mimetype);
325                     try {
326                         $thumb = $this->attachment->getThumbnail();
327                         $poster = $thumb->getUrl();
328                         unset ($thumb);
329                     } catch (Exception $e) {
330                         $poster = null;
331                     }
332                     $this->out->elementStart($mediatype,
333                                         array('class'=>'attachment_player',
334                                             'poster'=>$poster,
335                                             'controls'=>'controls'));
336                     $this->out->element('source',
337                                         array('src'=>$this->attachment->url,
338                                             'type'=>$this->attachment->mimetype));
339                     $this->out->elementEnd($mediatype);
340                     break;
341
342                 case 'text/html':
343                     if ($this->attachment->filename) {
344                         // Locally-uploaded HTML. Scrub and display inline.
345                         $this->showHtmlFile($this->attachment);
346                         break;
347                     }
348                     // Fall through to default.
349
350                 default:
351                     $this->showFallback();
352                 }
353             }
354         } else {
355             switch ($this->oembed->type) {
356             case 'rich':
357             case 'video':
358             case 'link':
359                 if (!empty($this->oembed->html)) {
360                     require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
361                     $config = array(
362                         'safe'=>1,
363                         'elements'=>'*+object+embed');
364                     $this->out->raw(htmLawed($this->oembed->html,$config));
365                     //$this->out->raw($this->oembed->html);
366                 }
367                 break;
368
369             case 'photo':
370                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
371                 break;
372
373             default:
374                 $this->showFallback();
375             }
376         }
377     }
378
379     protected function showHtmlFile(File $attachment)
380     {
381         $body = $this->scrubHtmlFile($attachment);
382         if ($body) {
383             $this->out->raw($body);
384         }
385     }
386
387     /**
388      * @return mixed false on failure, HTML fragment string on success
389      */
390     protected function scrubHtmlFile(File $attachment)
391     {
392         $path = File::path($attachment->filename);
393         if (!file_exists($path) || !is_readable($path)) {
394             common_log(LOG_ERR, "Missing local HTML attachment $path");
395             return false;
396         }
397         $raw = file_get_contents($path);
398
399         // Normalize...
400         $dom = new DOMDocument();
401         if(!$dom->loadHTML($raw)) {
402             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
403             return false;
404         }
405
406         // Remove <script>s or htmlawed will dump their contents into output!
407         // Note: removing child nodes while iterating seems to mess things up,
408         // hence the double loop.
409         $scripts = array();
410         foreach ($dom->getElementsByTagName('script') as $script) {
411             $scripts[] = $script;
412         }
413         foreach ($scripts as $script) {
414             common_log(LOG_DEBUG, $script->textContent);
415             $script->parentNode->removeChild($script);
416         }
417
418         // Trim out everything outside the body...
419         $body = $dom->saveHTML();
420         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
421         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
422
423         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
424         $config = array('safe' => 1,
425                         'deny_attribute' => 'id,style,on*',
426                         'comment' => 1); // remove comments
427         $scrubbed = htmLawed($body, $config);
428
429         return $scrubbed;
430     }
431
432     function showFallback()
433     {
434         // still needed: should show a link?
435     }
436 }