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