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