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