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