]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
Merge branch 'oembed-thumbnails' into 0.9.x
[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('STATUSNET') && !defined('LACONICA')) {
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 notices
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 notices listed.
76      */
77     function show()
78     {
79         $atts = new File;
80         $att = $atts->getAttachments($this->notice->id);
81         if (empty($att)) return 0;
82         $this->showListStart();
83
84         foreach ($att as $n=>$attachment) {
85             $item = $this->newListItem($attachment);
86             $item->show();
87         }
88
89         $this->showListEnd();
90
91         return count($att);
92     }
93
94     function showListStart()
95     {
96         $this->out->elementStart('dl', array('id' =>'attachments',
97                                              'class' => 'entry-content'));
98         // TRANS: DT element label in attachment list.
99         $this->out->element('dt', null, _('Attachments'));
100         $this->out->elementStart('dd');
101         $this->out->elementStart('ol', array('class' => 'attachments'));
102     }
103
104     function showListEnd()
105     {
106         $this->out->elementEnd('dd');
107         $this->out->elementEnd('ol');
108         $this->out->elementEnd('dl');
109     }
110
111     /**
112      * returns a new list item for the current notice
113      *
114      * Recipe (factory?) method; overridden by sub-classes to give
115      * a different list item class.
116      *
117      * @param Notice $notice the current notice
118      *
119      * @return NoticeListItem a list item for displaying the notice
120      */
121     function newListItem($attachment)
122     {
123         return new AttachmentListItem($attachment, $this->out);
124     }
125 }
126
127 /**
128  * widget for displaying a single notice
129  *
130  * This widget has the core smarts for showing a single notice: what to display,
131  * where, and under which circumstances. Its key method is show(); this is a recipe
132  * that calls all the other show*() methods to build up a single notice. The
133  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
134  * author info (since that's implicit by the data in the page).
135  *
136  * @category UI
137  * @package  StatusNet
138  * @author   Evan Prodromou <evan@status.net>
139  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
140  * @link     http://status.net/
141  * @see      NoticeList
142  * @see      ProfileNoticeListItem
143  */
144 class AttachmentListItem extends Widget
145 {
146     /** The attachment this item will show. */
147
148     var $attachment = null;
149
150     var $oembed = null;
151
152     /**
153      * constructor
154      *
155      * Also initializes the profile attribute.
156      *
157      * @param Notice $notice The notice we'll display
158      */
159     function __construct($attachment, $out=null)
160     {
161         parent::__construct($out);
162         $this->attachment  = $attachment;
163         $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
164     }
165
166     function title() {
167         if (empty($this->attachment->title)) {
168             if (empty($this->oembed->title)) {
169                 $title = $this->attachment->url;
170             } else {
171                 $title = $this->oembed->title;
172             }
173         } else {
174             $title = $this->attachment->title;
175         }
176
177         return $title;
178     }
179
180     function linkTitle() {
181         return $this->title();
182     }
183
184     /**
185      * recipe function for displaying a single notice.
186      *
187      * This uses all the other methods to correctly display a notice. Override
188      * it or one of the others to fine-tune the output.
189      *
190      * @return void
191      */
192     function show()
193     {
194         $this->showStart();
195         $this->showNoticeAttachment();
196         $this->showEnd();
197     }
198
199     function linkAttr() {
200         return array('class' => 'attachment',
201                      'href' => $this->attachment->url,
202                      'id' => 'attachment-' . $this->attachment->id,
203                      'title' => $this->title());
204     }
205
206     function showLink() {
207         $this->out->elementStart('a', $this->linkAttr());
208         $this->out->element('span', null, $this->linkTitle());
209         $this->showRepresentation();
210         $this->out->elementEnd('a');
211     }
212
213     function showNoticeAttachment()
214     {
215         $this->showLink();
216     }
217
218     function showRepresentation() {
219         $thumb = $this->getThumbInfo();
220         if ($thumb) {
221             $thumb = $this->sizeThumb($thumb);
222             $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
223         }
224     }
225
226     /**
227      * Pull a thumbnail image reference for the given file.
228      * In order we check:
229      *  1) file_thumbnail table (thumbnails found via oEmbed)
230      *  2) image URL from direct dereference or oEmbed 'photo' type URL
231      *  3) ???
232      *
233      * @return mixed object with (url, width, height) properties, or false
234      */
235     function getThumbInfo()
236     {
237         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
238         if ($thumbnail) {
239             return $thumbnail;
240         }
241         $enc = $this->attachment->getEnclosure();
242         if ($enc) {
243             switch ($enc->mimetype) {
244                 case 'image/gif':
245                 case 'image/png':
246                 case 'image/jpg':
247                 case 'image/jpeg':
248                     $thumb = (object)array();
249                     $thumb->url = $enc->url;
250                     // @fixme use the given width/height aspect
251                     $thumb->width = common_config('attachments', 'thumb_width');
252                     $thumb->height = common_config('attachments', 'thumb_height');
253                     return $thumb;
254             }
255         }
256         return false;
257     }
258
259     function sizeThumb($thumbnail) {
260         $maxWidth = 100;
261         $maxHeight = 75;
262         if ($thumbnail->width > $maxWidth) {
263             $thumb = clone($thumbnail);
264             $thumb->width = $maxWidth;
265             $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
266             return $thumb;
267         } else {
268             return $thumbnail;
269         }
270     }
271
272     /**
273      * start a single notice.
274      *
275      * @return void
276      */
277     function showStart()
278     {
279         // XXX: RDFa
280         // TODO: add notice_type class e.g., notice_video, notice_image
281         $this->out->elementStart('li');
282     }
283
284     /**
285      * finish the notice
286      *
287      * Close the last elements in the notice list item
288      *
289      * @return void
290      */
291     function showEnd()
292     {
293         $this->out->elementEnd('li');
294     }
295 }
296
297 /**
298  * used for one-off attachment action
299  */
300 class Attachment extends AttachmentListItem
301 {
302     function showLink() {
303         $this->out->elementStart('div', array('id' => 'attachment_view',
304                                               'class' => 'hentry'));
305         $this->out->elementStart('div', 'entry-title');
306         $this->out->element('a', $this->linkAttr(), $this->linkTitle());
307         $this->out->elementEnd('div');
308
309         $this->out->elementStart('div', 'entry-content');
310         $this->showRepresentation();
311         $this->out->elementEnd('div');
312
313         if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
314             $this->out->elementStart('div', array('id' => 'oembed_info',
315                                                   'class' => 'entry-content'));
316             if (!empty($this->oembed->author_name)) {
317                 $this->out->elementStart('dl', 'vcard author');
318                 // TRANS: DT element label in attachment list item.
319                 $this->out->element('dt', null, _('Author'));
320                 $this->out->elementStart('dd', 'fn');
321                 if (empty($this->oembed->author_url)) {
322                     $this->out->text($this->oembed->author_name);
323                 } else {
324                     $this->out->element('a', array('href' => $this->oembed->author_url,
325                                                    'class' => 'url'), $this->oembed->author_name);
326                 }
327                 $this->out->elementEnd('dd');
328                 $this->out->elementEnd('dl');
329             }
330             if (!empty($this->oembed->provider)) {
331                 $this->out->elementStart('dl', 'vcard');
332                 // TRANS: DT element label in attachment list item.
333                 $this->out->element('dt', null, _('Provider'));
334                 $this->out->elementStart('dd', 'fn');
335                 if (empty($this->oembed->provider_url)) {
336                     $this->out->text($this->oembed->provider);
337                 } else {
338                     $this->out->element('a', array('href' => $this->oembed->provider_url,
339                                                    'class' => 'url'), $this->oembed->provider);
340                 }
341                 $this->out->elementEnd('dd');
342                 $this->out->elementEnd('dl');
343             }
344             $this->out->elementEnd('div');
345         }
346         $this->out->elementEnd('div');
347     }
348
349     function show() {
350         $this->showNoticeAttachment();
351     }
352
353     function linkAttr() {
354         return array('rel' => 'external', 'href' => $this->attachment->url);
355     }
356
357     function linkTitle() {
358         return $this->attachment->url;
359     }
360
361     function showRepresentation() {
362         if (empty($this->oembed->type)) {
363             if (empty($this->attachment->mimetype)) {
364                 $this->showFallback();
365             } else {
366                 switch ($this->attachment->mimetype) {
367                 case 'image/gif':
368                 case 'image/png':
369                 case 'image/jpg':
370                 case 'image/jpeg':
371                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
372                     break;
373
374                 case 'application/ogg':
375                 case 'audio/x-speex':
376                 case 'video/mpeg':
377                 case 'audio/mpeg':
378                 case 'video/mp4':
379                 case 'video/quicktime':
380                     $arr  = array('type' => $this->attachment->mimetype,
381                         'data' => $this->attachment->url,
382                         'width' => 320,
383                         'height' => 240
384                     );
385                     $this->out->elementStart('object', $arr);
386                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
387                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
388                     $this->out->elementEnd('object');
389                     break;
390
391                 case 'text/html':
392                     if ($this->attachment->filename) {
393                         // Locally-uploaded HTML. Scrub and display inline.
394                         $this->showHtmlFile($this->attachment);
395                         break;
396                     }
397                     // Fall through to default.
398
399                 default:
400                     $this->showFallback();
401                 }
402             }
403         } else {
404             switch ($this->oembed->type) {
405             case 'rich':
406             case 'video':
407             case 'link':
408                 if (!empty($this->oembed->html)) {
409                     require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
410                     $config = array(
411                         'safe'=>1,
412                         'elements'=>'*+object+embed');
413                     $this->out->raw(htmLawed($this->oembed->html,$config));
414                     //$this->out->raw($this->oembed->html);
415                 }
416                 break;
417
418             case 'photo':
419                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
420                 break;
421
422             default:
423                 $this->showFallback();
424             }
425         }
426     }
427
428     protected function showHtmlFile(File $attachment)
429     {
430         $body = $this->scrubHtmlFile($attachment);
431         if ($body) {
432             $this->out->raw($body);
433         }
434     }
435
436     /**
437      * @return mixed false on failure, HTML fragment string on success
438      */
439     protected function scrubHtmlFile(File $attachment)
440     {
441         $path = File::path($attachment->filename);
442         if (!file_exists($path) || !is_readable($path)) {
443             common_log(LOG_ERR, "Missing local HTML attachment $path");
444             return false;
445         }
446         $raw = file_get_contents($path);
447
448         // Normalize...
449         $dom = new DOMDocument();
450         if(!$dom->loadHTML($raw)) {
451             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
452             return false;
453         }
454
455         // Remove <script>s or htmlawed will dump their contents into output!
456         // Note: removing child nodes while iterating seems to mess things up,
457         // hence the double loop.
458         $scripts = array();
459         foreach ($dom->getElementsByTagName('script') as $script) {
460             $scripts[] = $script;
461         }
462         foreach ($scripts as $script) {
463             common_log(LOG_DEBUG, $script->textContent);
464             $script->parentNode->removeChild($script);
465         }
466
467         // Trim out everything outside the body...
468         $body = $dom->saveHTML();
469         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
470         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
471
472         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
473         $config = array('safe' => 1,
474                         'deny_attribute' => 'id,style,on*',
475                         'comment' => 1); // remove comments
476         $scrubbed = htmLawed($body, $config);
477
478         return $scrubbed;
479     }
480 }