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