]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
split out InlineAttachmentList from AttachmentList
[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', 'href' => $this->attachment->url, 'id' => 'attachment-' . $this->attachment->id);
201     }
202
203     function showLink() {
204         $this->out->elementStart('a', $this->linkAttr());
205         $this->out->element('span', null, $this->linkTitle());
206         $this->showRepresentation();
207         $this->out->elementEnd('a');
208     }
209
210     function showNoticeAttachment()
211     {
212         $this->showLink();
213     }
214
215     function showRepresentation() {
216         $thumb = $this->getThumbInfo();
217         if ($thumb) {
218             $thumb = $this->sizeThumb($thumb);
219             $this->out->element('img', array('alt' => '', 'src' => $thumb->url, 'width' => $thumb->width, 'height' => $thumb->height));
220         }
221     }
222
223     /**
224      * Pull a thumbnail image reference for the given file.
225      * In order we check:
226      *  1) file_thumbnail table (thumbnails found via oEmbed)
227      *  2) image URL from direct dereference or oEmbed 'photo' type URL
228      *  3) ???
229      *
230      * @return mixed object with (url, width, height) properties, or false
231      */
232     function getThumbInfo()
233     {
234         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
235         if ($thumbnail) {
236             return $thumbnail;
237         }
238         $enc = $this->attachment->getEnclosure();
239         if ($enc) {
240             switch ($enc->mimetype) {
241                 case 'image/gif':
242                 case 'image/png':
243                 case 'image/jpg':
244                 case 'image/jpeg':
245                     $thumb = (object)array();
246                     $thumb->url = $enc->url;
247                     $thumb->width = 100;
248                     $thumb->height = 75; // @fixme
249                     return $thumb;
250             }
251         }
252         return false;
253     }
254
255     function sizeThumb($thumbnail) {
256         $maxWidth = 100;
257         $maxHeight = 75;
258         if ($thumbnail->width > $maxWidth) {
259             $thumb = clone($thumbnail);
260             $thumb->width = $maxWidth;
261             $thumb->height = intval($thumbnail->height * $maxWidth / $thumbnail->width);
262             return $thumb;
263         } else {
264             return $thumbnail;
265         }
266     }
267
268     /**
269      * start a single notice.
270      *
271      * @return void
272      */
273     function showStart()
274     {
275         // XXX: RDFa
276         // TODO: add notice_type class e.g., notice_video, notice_image
277         $this->out->elementStart('li');
278     }
279
280     /**
281      * finish the notice
282      *
283      * Close the last elements in the notice list item
284      *
285      * @return void
286      */
287     function showEnd()
288     {
289         $this->out->elementEnd('li');
290     }
291 }
292
293 /**
294  * used for one-off attachment action
295  */
296 class Attachment extends AttachmentListItem
297 {
298     function showLink() {
299         $this->out->elementStart('div', array('id' => 'attachment_view',
300                                               'class' => 'hentry'));
301         $this->out->elementStart('div', 'entry-title');
302         $this->out->element('a', $this->linkAttr(), $this->linkTitle());
303         $this->out->elementEnd('div');
304
305         $this->out->elementStart('div', 'entry-content');
306         $this->showRepresentation();
307         $this->out->elementEnd('div');
308
309         if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
310             $this->out->elementStart('div', array('id' => 'oembed_info',
311                                                   'class' => 'entry-content'));
312             if (!empty($this->oembed->author_name)) {
313                 $this->out->elementStart('dl', 'vcard author');
314                 // TRANS: DT element label in attachment list item.
315                 $this->out->element('dt', null, _('Author'));
316                 $this->out->elementStart('dd', 'fn');
317                 if (empty($this->oembed->author_url)) {
318                     $this->out->text($this->oembed->author_name);
319                 } else {
320                     $this->out->element('a', array('href' => $this->oembed->author_url,
321                                                    'class' => 'url'), $this->oembed->author_name);
322                 }
323                 $this->out->elementEnd('dd');
324                 $this->out->elementEnd('dl');
325             }
326             if (!empty($this->oembed->provider)) {
327                 $this->out->elementStart('dl', 'vcard');
328                 // TRANS: DT element label in attachment list item.
329                 $this->out->element('dt', null, _('Provider'));
330                 $this->out->elementStart('dd', 'fn');
331                 if (empty($this->oembed->provider_url)) {
332                     $this->out->text($this->oembed->provider);
333                 } else {
334                     $this->out->element('a', array('href' => $this->oembed->provider_url,
335                                                    'class' => 'url'), $this->oembed->provider);
336                 }
337                 $this->out->elementEnd('dd');
338                 $this->out->elementEnd('dl');
339             }
340             $this->out->elementEnd('div');
341         }
342         $this->out->elementEnd('div');
343     }
344
345     function show() {
346         $this->showNoticeAttachment();
347     }
348
349     function linkAttr() {
350         return array('rel' => 'external', 'href' => $this->attachment->url);
351     }
352
353     function linkTitle() {
354         return $this->attachment->url;
355     }
356
357     function showRepresentation() {
358         if (empty($this->oembed->type)) {
359             if (empty($this->attachment->mimetype)) {
360                 $this->showFallback();
361             } else {
362                 switch ($this->attachment->mimetype) {
363                 case 'image/gif':
364                 case 'image/png':
365                 case 'image/jpg':
366                 case 'image/jpeg':
367                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
368                     break;
369
370                 case 'application/ogg':
371                 case 'audio/x-speex':
372                 case 'video/mpeg':
373                 case 'audio/mpeg':
374                 case 'video/mp4':
375                 case 'video/quicktime':
376                     $arr  = array('type' => $this->attachment->mimetype,
377                         'data' => $this->attachment->url,
378                         'width' => 320,
379                         'height' => 240
380                     );
381                     $this->out->elementStart('object', $arr);
382                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
383                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
384                     $this->out->elementEnd('object');
385                     break;
386
387                 case 'text/html':
388                     if ($this->attachment->filename) {
389                         // Locally-uploaded HTML. Scrub and display inline.
390                         $this->showHtmlFile($this->attachment);
391                         break;
392                     }
393                     // Fall through to default.
394
395                 default:
396                     $this->showFallback();
397                 }
398             }
399         } else {
400             switch ($this->oembed->type) {
401             case 'rich':
402             case 'video':
403             case 'link':
404                 if (!empty($this->oembed->html)) {
405                     require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
406                     $config = array(
407                         'safe'=>1,
408                         'elements'=>'*+object+embed');
409                     $this->out->raw(htmLawed($this->oembed->html,$config));
410                     //$this->out->raw($this->oembed->html);
411                 }
412                 break;
413
414             case 'photo':
415                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
416                 break;
417
418             default:
419                 $this->showFallback();
420             }
421         }
422     }
423
424     protected function showHtmlFile(File $attachment)
425     {
426         $body = $this->scrubHtmlFile($attachment);
427         if ($body) {
428             $this->out->raw($body);
429         }
430     }
431
432     /**
433      * @return mixed false on failure, HTML fragment string on success
434      */
435     protected function scrubHtmlFile(File $attachment)
436     {
437         $path = File::path($attachment->filename);
438         if (!file_exists($path) || !is_readable($path)) {
439             common_log(LOG_ERR, "Missing local HTML attachment $path");
440             return false;
441         }
442         $raw = file_get_contents($path);
443
444         // Normalize...
445         $dom = new DOMDocument();
446         if(!$dom->loadHTML($raw)) {
447             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
448             return false;
449         }
450
451         // Remove <script>s or htmlawed will dump their contents into output!
452         // Note: removing child nodes while iterating seems to mess things up,
453         // hence the double loop.
454         $scripts = array();
455         foreach ($dom->getElementsByTagName('script') as $script) {
456             $scripts[] = $script;
457         }
458         foreach ($scripts as $script) {
459             common_log(LOG_DEBUG, $script->textContent);
460             $script->parentNode->removeChild($script);
461         }
462
463         // Trim out everything outside the body...
464         $body = $dom->saveHTML();
465         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
466         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
467
468         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
469         $config = array('safe' => 1,
470                         'deny_attribute' => 'id,style,on*',
471                         'comment' => 1); // remove comments
472         $scrubbed = htmLawed($body, $config);
473
474         return $scrubbed;
475     }
476 }