]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/attachmentlist.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.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->out->elementStart('dl', array('id' =>'attachments',
83                                              'class' => 'entry-content'));
84         // TRANS: DT element label in attachment list.
85         $this->out->element('dt', null, _('Attachments'));
86         $this->out->elementStart('dd');
87         $this->out->elementStart('ol', array('class' => 'attachments'));
88
89         foreach ($att as $n=>$attachment) {
90             $item = $this->newListItem($attachment);
91             $item->show();
92         }
93
94         $this->out->elementEnd('dd');
95         $this->out->elementEnd('ol');
96         $this->out->elementEnd('dl');
97
98         return count($att);
99     }
100
101     /**
102      * returns a new list item for the current notice
103      *
104      * Recipe (factory?) method; overridden by sub-classes to give
105      * a different list item class.
106      *
107      * @param Notice $notice the current notice
108      *
109      * @return NoticeListItem a list item for displaying the notice
110      */
111     function newListItem($attachment)
112     {
113         return new AttachmentListItem($attachment, $this->out);
114     }
115 }
116
117 /**
118  * widget for displaying a single notice
119  *
120  * This widget has the core smarts for showing a single notice: what to display,
121  * where, and under which circumstances. Its key method is show(); this is a recipe
122  * that calls all the other show*() methods to build up a single notice. The
123  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
124  * author info (since that's implicit by the data in the page).
125  *
126  * @category UI
127  * @package  StatusNet
128  * @author   Evan Prodromou <evan@status.net>
129  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
130  * @link     http://status.net/
131  * @see      NoticeList
132  * @see      ProfileNoticeListItem
133  */
134 class AttachmentListItem extends Widget
135 {
136     /** The attachment this item will show. */
137
138     var $attachment = null;
139
140     var $oembed = null;
141
142     /**
143      * constructor
144      *
145      * Also initializes the profile attribute.
146      *
147      * @param Notice $notice The notice we'll display
148      */
149     function __construct($attachment, $out=null)
150     {
151         parent::__construct($out);
152         $this->attachment  = $attachment;
153         $this->oembed = File_oembed::staticGet('file_id', $this->attachment->id);
154     }
155
156     function title() {
157         if (empty($this->attachment->title)) {
158             if (empty($this->oembed->title)) {
159                 $title = $this->attachment->url;
160             } else {
161                 $title = $this->oembed->title;
162             }
163         } else {
164             $title = $this->attachment->title;
165         }
166
167         return $title;
168     }
169
170     function linkTitle() {
171         return $this->title();
172     }
173
174     /**
175      * recipe function for displaying a single notice.
176      *
177      * This uses all the other methods to correctly display a notice. Override
178      * it or one of the others to fine-tune the output.
179      *
180      * @return void
181      */
182     function show()
183     {
184         $this->showStart();
185         $this->showNoticeAttachment();
186         $this->showEnd();
187     }
188
189     function linkAttr() {
190         return array('class' => 'attachment', 'href' => $this->attachment->url, 'id' => 'attachment-' . $this->attachment->id);
191     }
192
193     function showLink() {
194         $this->out->elementStart('a', $this->linkAttr());
195         $this->out->element('span', null, $this->linkTitle());
196         $this->showRepresentation();
197         $this->out->elementEnd('a');
198     }
199
200     function showNoticeAttachment()
201     {
202         $this->showLink();
203     }
204
205     function showRepresentation() {
206         $thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id);
207         if (!empty($thumbnail)) {
208             $this->out->element('img', array('alt' => '', 'src' => $thumbnail->url, 'width' => $thumbnail->width, 'height' => $thumbnail->height));
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 class Attachment extends AttachmentListItem
238 {
239     function showLink() {
240         $this->out->elementStart('div', array('id' => 'attachment_view',
241                                               'class' => 'hentry'));
242         $this->out->elementStart('div', 'entry-title');
243         $this->out->element('a', $this->linkAttr(), $this->linkTitle());
244         $this->out->elementEnd('div');
245
246         $this->out->elementStart('div', 'entry-content');
247         $this->showRepresentation();
248         $this->out->elementEnd('div');
249
250         if (!empty($this->oembed->author_name) || !empty($this->oembed->provider)) {
251             $this->out->elementStart('div', array('id' => 'oembed_info',
252                                                   'class' => 'entry-content'));
253             if (!empty($this->oembed->author_name)) {
254                 $this->out->elementStart('dl', 'vcard author');
255                 // TRANS: DT element label in attachment list item.
256                 $this->out->element('dt', null, _('Author'));
257                 $this->out->elementStart('dd', 'fn');
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                 $this->out->elementEnd('dd');
265                 $this->out->elementEnd('dl');
266             }
267             if (!empty($this->oembed->provider)) {
268                 $this->out->elementStart('dl', 'vcard');
269                 // TRANS: DT element label in attachment list item.
270                 $this->out->element('dt', null, _('Provider'));
271                 $this->out->elementStart('dd', 'fn');
272                 if (empty($this->oembed->provider_url)) {
273                     $this->out->text($this->oembed->provider);
274                 } else {
275                     $this->out->element('a', array('href' => $this->oembed->provider_url,
276                                                    'class' => 'url'), $this->oembed->provider);
277                 }
278                 $this->out->elementEnd('dd');
279                 $this->out->elementEnd('dl');
280             }
281             $this->out->elementEnd('div');
282         }
283         $this->out->elementEnd('div');
284     }
285
286     function show() {
287         $this->showNoticeAttachment();
288     }
289
290     function linkAttr() {
291         return array('rel' => 'external', 'href' => $this->attachment->url);
292     }
293
294     function linkTitle() {
295         return $this->attachment->url;
296     }
297
298     function showRepresentation() {
299         if (empty($this->oembed->type)) {
300             if (empty($this->attachment->mimetype)) {
301                 $this->showFallback();
302             } else {
303                 switch ($this->attachment->mimetype) {
304                 case 'image/gif':
305                 case 'image/png':
306                 case 'image/jpg':
307                 case 'image/jpeg':
308                     $this->out->element('img', array('src' => $this->attachment->url, 'alt' => 'alt'));
309                     break;
310
311                 case 'application/ogg':
312                 case 'audio/x-speex':
313                 case 'video/mpeg':
314                 case 'audio/mpeg':
315                 case 'video/mp4':
316                 case 'video/quicktime':
317                     $arr  = array('type' => $this->attachment->mimetype,
318                         'data' => $this->attachment->url,
319                         'width' => 320,
320                         'height' => 240
321                     );
322                     $this->out->elementStart('object', $arr);
323                     $this->out->element('param', array('name' => 'src', 'value' => $this->attachment->url));
324                     $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
325                     $this->out->elementEnd('object');
326                     break;
327
328                 case 'text/html':
329                     if ($this->attachment->filename) {
330                         // Locally-uploaded HTML. Scrub and display inline.
331                         $this->showHtmlFile($this->attachment);
332                         break;
333                     }
334                     // Fall through to default.
335
336                 default:
337                     $this->showFallback();
338                 }
339             }
340         } else {
341             switch ($this->oembed->type) {
342             case 'rich':
343             case 'video':
344             case 'link':
345                 if (!empty($this->oembed->html)) {
346                     require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
347                     $config = array(
348                         'safe'=>1,
349                         'elements'=>'*+object+embed');
350                     $this->out->raw(htmLawed($this->oembed->html,$config));
351                     //$this->out->raw($this->oembed->html);
352                 }
353                 break;
354
355             case 'photo':
356                 $this->out->element('img', array('src' => $this->oembed->url, 'width' => $this->oembed->width, 'height' => $this->oembed->height, 'alt' => 'alt'));
357                 break;
358
359             default:
360                 $this->showFallback();
361             }
362         }
363     }
364
365     protected function showHtmlFile(File $attachment)
366     {
367         $body = $this->scrubHtmlFile($attachment);
368         if ($body) {
369             $this->out->raw($body);
370         }
371     }
372
373     /**
374      * @return mixed false on failure, HTML fragment string on success
375      */
376     protected function scrubHtmlFile(File $attachment)
377     {
378         $path = File::path($attachment->filename);
379         if (!file_exists($path) || !is_readable($path)) {
380             common_log(LOG_ERR, "Missing local HTML attachment $path");
381             return false;
382         }
383         $raw = file_get_contents($path);
384
385         // Normalize...
386         $dom = new DOMDocument();
387         if(!$dom->loadHTML($raw)) {
388             common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
389             return false;
390         }
391
392         // Remove <script>s or htmlawed will dump their contents into output!
393         // Note: removing child nodes while iterating seems to mess things up,
394         // hence the double loop.
395         $scripts = array();
396         foreach ($dom->getElementsByTagName('script') as $script) {
397             $scripts[] = $script;
398         }
399         foreach ($scripts as $script) {
400             common_log(LOG_DEBUG, $script->textContent);
401             $script->parentNode->removeChild($script);
402         }
403
404         // Trim out everything outside the body...
405         $body = $dom->saveHTML();
406         $body = preg_replace('/^.*<body[^>]*>/is', '', $body);
407         $body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
408
409         require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
410         $config = array('safe' => 1,
411                         'deny_attribute' => 'id,style,on*',
412                         'comment' => 1); // remove comments
413         $scrubbed = htmLawed($body, $config);
414
415         return $scrubbed;
416     }
417
418     function showFallback()
419     {
420         // If we don't know how to display an attachment inline, we probably
421         // shouldn't have gotten to this point.
422         //
423         // But, here we are... displaying details on a file or remote URL
424         // either on the main view or in an ajax-loaded lightbox. As a lesser
425         // of several evils, we'll try redirecting to the actual target via
426         // client-side JS.
427
428         common_log(LOG_ERR, "Empty or unknown type for file id {$this->attachment->id}; falling back to client-side redirect.");
429         $this->out->raw('<script>window.location = ' . json_encode($this->attachment->url) . ';</script>');
430     }
431 }