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