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