]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/noticelistitem.php
*** Privacy Leak fixed: ***
[quix0rs-gnu-social.git] / lib / noticelistitem.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * An item in a notice list
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Widget
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * widget for displaying a single notice
35  *
36  * This widget has the core smarts for showing a single notice: what to display,
37  * where, and under which circumstances. Its key method is show(); this is a recipe
38  * that calls all the other show*() methods to build up a single notice. The
39  * ProfileNoticeListItem subclass, for example, overrides showAuthor() to skip
40  * author info (since that's implicit by the data in the page).
41  *
42  * @category UI
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  * @see      NoticeList
48  * @see      ProfileNoticeListItem
49  */
50 class NoticeListItem extends Widget
51 {
52     /** The notice this item will show. */
53     var $notice = null;
54
55     /** The notice that was repeated. */
56     var $repeat = null;
57
58     /** The profile of the author of the notice, extracted once for convenience. */
59     var $profile = null;
60
61     protected $addressees = true;
62     protected $attachments = true;
63     protected $id_prefix = null;
64     protected $options = true;
65     protected $maxchars = 0;   // if <= 0 it means use full posts
66     protected $item_tag = 'li';
67     protected $pa = null;
68
69     /**
70      * constructor
71      *
72      * Also initializes the profile attribute.
73      *
74      * @param Notice $notice The notice we'll display
75      */
76     function __construct(Notice $notice, Action $out=null, array $prefs=array())
77     {
78         parent::__construct($out);
79         if (!empty($notice->repeat_of)) {
80             $original = Notice::getKV('id', $notice->repeat_of);
81             if (!$original instanceof Notice) { // could have been deleted
82                 $this->notice = $notice;
83             } else {
84                 $this->notice = $original;
85                 $this->repeat = $notice;
86             }
87         } else {
88             $this->notice  = $notice;
89         }
90
91         $this->profile = $this->notice->getProfile();
92         
93         // integer preferences
94         foreach(array('maxchars') as $key) {
95             if (array_key_exists($key, $prefs)) {
96                 $this->$key = (int)$prefs[$key];
97             }
98         }
99         // boolean preferences
100         foreach(array('addressees', 'attachments', 'options') as $key) {
101             if (array_key_exists($key, $prefs)) {
102                 $this->$key = (bool)$prefs[$key];
103             }
104         }
105         // string preferences
106         foreach(array('id_prefix', 'item_tag') as $key) {
107             if (array_key_exists($key, $prefs)) {
108                 $this->$key = $prefs[$key];
109             }
110         }
111     }
112
113     /**
114      * recipe function for displaying a single notice.
115      *
116      * This uses all the other methods to correctly display a notice. Override
117      * it or one of the others to fine-tune the output.
118      *
119      * @return void
120      */
121     function show()
122     {
123         if (empty($this->notice)) {
124             common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
125             return;
126         } else if (empty($this->profile)) {
127             common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
128             return;
129         }
130
131         $this->showStart();
132         if (Event::handle('StartShowNoticeItem', array($this))) {
133             $this->showNotice();
134             Event::handle('EndShowNoticeItem', array($this));
135         }
136         $this->showEnd();
137     }
138
139     function showNotice()
140     {
141         if (Event::handle('StartShowNoticeItemNotice', array($this))) {
142             $this->showNoticeHeaders();
143             $this->showContent();
144             $this->showNoticeFooter();
145             Event::handle('EndShowNoticeItemNotice', array($this));
146         }
147     }
148
149     function showNoticeHeaders()
150     {
151         $this->elementStart('section', array('class'=>'notice-headers'));
152         $this->showNoticeTitle();
153         $this->showAuthor();
154
155         if (!empty($this->notice->reply_to) || count($this->getProfileAddressees()) > 0) {
156             $this->elementStart('div', array('class' => 'parents'));
157             try {
158                 $this->showParent();
159             } catch (NoParentNoticeException $e) {
160                 // no parent notice
161             } catch (InvalidUrlException $e) {
162                 // parent had an invalid URL so we can't show it
163             }
164             if ($this->addressees) { $this->showAddressees(); }
165             $this->elementEnd('div');
166         }
167         $this->elementEnd('section');
168     }
169
170     function showNoticeFooter()
171     {
172         $this->elementStart('footer');
173         $this->showNoticeInfo();
174         if ($this->options) { $this->showNoticeOptions(); }
175         if ($this->attachments) { $this->showNoticeAttachments(); }
176         $this->elementEnd('footer');
177     }
178
179     function showNoticeTitle()
180     {
181         if (Event::handle('StartShowNoticeTitle', array($this))) {
182             $nameClass = $this->notice->getTitle(false) ? 'p-name ' : '';
183             $this->element('a', array('href' => $this->notice->getUri(),
184                                       'class' => $nameClass . 'u-uid'),
185                            $this->notice->getTitle());
186             Event::handle('EndShowNoticeTitle', array($this));
187         }
188     }
189
190     function showNoticeInfo()
191     {
192         if (Event::handle('StartShowNoticeInfo', array($this))) {
193             $this->showContextLink();
194             $this->showNoticeLink();
195             $this->showNoticeSource();
196             $this->showNoticeLocation();
197             $this->showPermalink();
198             Event::handle('EndShowNoticeInfo', array($this));
199         }
200     }
201
202     function showNoticeOptions()
203     {
204         if (Event::handle('StartShowNoticeOptions', array($this))) {
205             $user = common_current_user();
206             if ($user) {
207                 $this->out->elementStart('div', 'notice-options');
208                 if (Event::handle('StartShowNoticeOptionItems', array($this))) {
209                     $this->showReplyLink();
210                     $this->showDeleteLink();
211                     Event::handle('EndShowNoticeOptionItems', array($this));
212                 }
213                 $this->out->elementEnd('div');
214             }
215             Event::handle('EndShowNoticeOptions', array($this));
216         }
217     }
218
219     /**
220      * start a single notice.
221      *
222      * @return void
223      */
224     function showStart()
225     {
226         if (Event::handle('StartOpenNoticeListItemElement', array($this))) {
227             $id = (empty($this->repeat)) ? $this->notice->id : $this->repeat->id;
228             $class = 'h-entry notice';
229             if ($this->notice->scope != 0 && $this->notice->scope != 1) {
230                 $class .= ' limited-scope';
231             }
232             try {
233                 $class .= ' notice-source-'.common_to_alphanumeric($this->notice->source);
234             } catch (Exception $e) {
235                 // either source or what we filtered out was a zero-length string
236             }
237             $id_prefix = (strlen($this->id_prefix) ? $this->id_prefix . '-' : '');
238             $this->out->elementStart($this->item_tag, array('class' => $class,
239                                                  'id' => "${id_prefix}notice-${id}"));
240             Event::handle('EndOpenNoticeListItemElement', array($this));
241         }
242     }
243
244     /**
245      * show the author of a notice
246      *
247      * By default, this shows the avatar and (linked) nickname of the author.
248      *
249      * @return void
250      */
251
252     function showAuthor()
253     {
254         $attrs = array('href' => $this->profile->getUrl(),
255                        'class' => 'h-card',
256                        'title' => $this->profile->getHtmlTitle());
257         if(empty($this->repeat)) { $attrs['class'] .= ' p-author'; }
258
259         if (Event::handle('StartShowNoticeItemAuthor', array($this->profile, $this->out, &$attrs))) {
260             $this->out->elementStart('a', $attrs);
261             $this->showAvatar($this->profile);
262             $this->out->text($this->profile->getStreamName());
263             $this->out->elementEnd('a');
264             Event::handle('EndShowNoticeItemAuthor', array($this->profile, $this->out));
265         }
266     }
267
268     function showParent()
269     {
270         $this->out->element(
271             'a',
272             array(
273                 'href' => $this->notice->getParent()->getUrl(),
274                 'class' => 'u-in-reply-to',
275                 'rel' => 'in-reply-to'
276             ),
277             'in reply to'
278         );
279     }
280
281     function showAddressees()
282     {
283         $pa = $this->getProfileAddressees();
284
285         if (!empty($pa)) {
286             $this->out->elementStart('ul', 'addressees');
287             $first = true;
288             foreach ($pa as $addr) {
289                 $this->out->elementStart('li');
290                 $text = $addr['text'];
291                 unset($addr['text']);
292                 $this->out->element('a', $addr, $text);
293                 $this->out->elementEnd('li');
294             }
295             $this->out->elementEnd('ul', 'addressees');
296         }
297     }
298
299     function getProfileAddressees()
300     {
301         if($this->pa) { return $this->pa; }
302         $this->pa = array();
303
304         $attentions = $this->getAttentionProfiles();
305
306         foreach ($attentions as $attn) {
307             if ($attn->isGroup()) {
308                 $class = 'group';
309                 $profileurl = common_local_url('groupbyid', array('id' => $attn->getGroup()->getID()));
310             } else {
311                 $class = 'account';
312                 $profileurl = common_local_url('userbyid', array('id' => $attn->getID()));
313             }
314             $this->pa[] = array('href' => $profileurl,
315                                 'title' => $attn->getHtmlTitle(),
316                                 'class' => "addressee {$class} p-name u-url",
317                                 'text' => $attn->getStreamName());
318         }
319
320         return $this->pa;
321     }
322
323     function getAttentionProfiles()
324     {
325         return $this->notice->getAttentionProfiles();
326     }
327
328     /**
329      * show the nickname of the author
330      *
331      * Links to the author's profile page
332      *
333      * @return void
334      */
335     function showNickname()
336     {
337         $this->out->raw('<span class="p-name">' .
338                         htmlspecialchars($this->profile->getNickname()) .
339                         '</span>');
340     }
341
342     /**
343      * show the content of the notice
344      *
345      * Shows the content of the notice. This is pre-rendered for efficiency
346      * at save time. Some very old notices might not be pre-rendered, so
347      * they're rendered on the spot.
348      *
349      * @return void
350      */
351     function showContent()
352     {
353         // FIXME: URL, image, video, audio
354         $nameClass = $this->notice->getTitle(false) ? '' : 'p-name ';
355         $this->out->elementStart('article', array('class' => $nameClass . 'e-content'));
356         if (Event::handle('StartShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()))) {
357             if ($this->maxchars > 0 && mb_strlen($this->notice->content) > $this->maxchars) {
358                 $this->out->text(mb_substr($this->notice->content, 0, $this->maxchars) . '[…]');
359             } else {
360                 $this->out->raw($this->notice->getRendered());
361             }
362             Event::handle('EndShowNoticeContent', array($this->notice, $this->out, $this->out->getScoped()));
363         }
364         $this->out->elementEnd('article');
365     }
366
367     function showNoticeAttachments() {
368         if (common_config('attachments', 'show_thumbs')) {
369             $al = new InlineAttachmentList($this->notice, $this->out);
370             $al->show();
371         }
372     }
373
374     /**
375      * show the link to the main page for the notice
376      *
377      * Displays a local link to the rendered notice, with "relative" time.
378      *
379      * @return void
380      */
381     function showNoticeLink()
382     {
383         $this->out->element('time', array('class' => 'dt-published',
384                                           'datetime' => common_date_iso8601($this->notice->created),
385                                           'title' => common_exact_date($this->notice->created)),
386                             common_date_string($this->notice->created));
387     }
388
389     /**
390      * show the notice location
391      *
392      * shows the notice location in the correct language.
393      *
394      * If an URL is available, makes a link. Otherwise, just a span.
395      *
396      * @return void
397      */
398     function showNoticeLocation()
399     {
400         try {
401             $location = Notice_location::locFromStored($this->notice);
402         } catch (NoResultException $e) {
403             return;
404         } catch (ServerException $e) {
405             return;
406         }
407
408         $name = $location->getName();
409
410         $lat = $location->lat;
411         $lon = $location->lon;
412         $latlon = (!empty($lat) && !empty($lon)) ? $lat.';'.$lon : '';
413
414         if (empty($name)) {
415             $latdms = $this->decimalDegreesToDMS(abs($lat));
416             $londms = $this->decimalDegreesToDMS(abs($lon));
417             // TRANS: Used in coordinates as abbreviation of north.
418             $north = _('N');
419             // TRANS: Used in coordinates as abbreviation of south.
420             $south = _('S');
421             // TRANS: Used in coordinates as abbreviation of east.
422             $east = _('E');
423             // TRANS: Used in coordinates as abbreviation of west.
424             $west = _('W');
425             $name = sprintf(
426                 // TRANS: Coordinates message.
427                 // TRANS: %1$s is lattitude degrees, %2$s is lattitude minutes,
428                 // TRANS: %3$s is lattitude seconds, %4$s is N (north) or S (south) depending on lattitude,
429                 // TRANS: %5$s is longitude degrees, %6$s is longitude minutes,
430                 // TRANS: %7$s is longitude seconds, %8$s is E (east) or W (west) depending on longitude,
431                 _('%1$u°%2$u\'%3$u"%4$s %5$u°%6$u\'%7$u"%8$s'),
432                 $latdms['deg'],$latdms['min'], $latdms['sec'],($lat>0? $north:$south),
433                 $londms['deg'],$londms['min'], $londms['sec'],($lon>0? $east:$west));
434         }
435
436         $url  = $location->getUrl();
437
438         $this->out->text(' ');
439         $this->out->elementStart('span', array('class' => 'location'));
440         // TRANS: Followed by geo location.
441         $this->out->text(_('at'));
442         $this->out->text(' ');
443         if (empty($url)) {
444             $this->out->element('abbr', array('class' => 'geo',
445                                               'title' => $latlon),
446                                 $name);
447         } else {
448             $xstr = new XMLStringer(false);
449             $xstr->elementStart('a', array('href' => $url,
450                                            'rel' => 'external'));
451             $xstr->element('abbr', array('class' => 'geo',
452                                          'title' => $latlon),
453                            $name);
454             $xstr->elementEnd('a');
455             $this->out->raw($xstr->getString());
456         }
457         $this->out->elementEnd('span');
458     }
459
460     /**
461      * @param number $dec decimal degrees
462      * @return array split into 'deg', 'min', and 'sec'
463      */
464     function decimalDegreesToDMS($dec)
465     {
466         $deg = intval($dec);
467         $tempma = abs($dec) - abs($deg);
468
469         $tempma = $tempma * 3600;
470         $min = floor($tempma / 60);
471         $sec = $tempma - ($min*60);
472
473         return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
474     }
475
476     /**
477      * Show the source of the notice
478      *
479      * Either the name (and link) of the API client that posted the notice,
480      * or one of other other channels.
481      *
482      * @return void
483      */
484     function showNoticeSource()
485     {
486         $ns = $this->notice->getSource();
487
488         if (!$ns instanceof Notice_source) {
489             return false;
490         }
491
492         // TRANS: A possible notice source (web interface).
493         $source_name = (empty($ns->name)) ? ($ns->code ? _($ns->code) : _m('SOURCE','web')) : _($ns->name);
494         $this->out->text(' ');
495         $this->out->elementStart('span', 'source');
496         // @todo FIXME: probably i18n issue. If "from" is followed by text, that should be a parameter to "from" (from %s).
497         // TRANS: Followed by notice source.
498         $this->out->text(_('from'));
499         $this->out->text(' ');
500
501         $name  = $source_name;
502         $url   = $ns->url;
503         $title = null;
504
505         if (Event::handle('StartNoticeSourceLink', array($this->notice, &$name, &$url, &$title))) {
506             $name = $source_name;
507             $url  = $ns->url;
508         }
509         Event::handle('EndNoticeSourceLink', array($this->notice, &$name, &$url, &$title));
510
511         // if $ns->name and $ns->url are populated we have
512         // configured a source attr somewhere
513         if (!empty($name) && !empty($url)) {
514             $this->out->elementStart('span', 'device');
515
516             $attrs = array(
517                 'href' => $url,
518                 'rel' => 'external'
519             );
520
521             if (!empty($title)) {
522                 $attrs['title'] = $title;
523             }
524
525             $this->out->element('a', $attrs, $name);
526             $this->out->elementEnd('span');
527         } else {
528             $this->out->element('span', 'device', $name);
529         }
530
531         $this->out->elementEnd('span');
532     }
533
534     /**
535      * show link to single-notice view for this notice item
536      *
537      * A permalink that goes to this specific object and nothing else
538      *
539      * @return void
540      */
541     function showPermalink()
542     {
543         $class = 'permalink u-url';
544         if (!$this->notice->isLocal()) {
545             $class .= ' external';
546         }
547
548         try {
549             if($this->repeat) {
550                 $this->out->element('a',
551                             array('href' => $this->repeat->getUrl(),
552                                   'class' => 'u-url'),
553                             '');
554                 $class = str_replace('u-url', 'u-repost-of', $class);
555             }
556         } catch (InvalidUrlException $e) {
557             // no permalink available
558         }
559
560         try {
561             $this->out->element('a',
562                         array('href' => $this->notice->getUrl(true),
563                               'class' => $class),
564                         // TRANS: Addition in notice list item for single-notice view.
565                         _('permalink'));
566         } catch (InvalidUrlException $e) {
567             // no permalink available
568         }
569     }
570
571     /**
572      * Show link to conversation view.
573      */
574     function showContextLink()
575     {
576         $this->out->element('a', array('rel' => 'bookmark',
577                                             'class' => 'timestamp',
578                                             'href' => Conversation::getUrlFromNotice($this->notice)),
579                             // TRANS: A link to the conversation view of a notice, on the local server.
580                             _('In conversation'));
581     }
582
583     /**
584      * show a link to reply to the current notice
585      *
586      * Should either do the reply in the current notice form (if available), or
587      * link out to the notice-posting form. A little flakey, doesn't always work.
588      *
589      * @return void
590      */
591     function showReplyLink()
592     {
593         if (common_logged_in()) {
594             $this->out->text(' ');
595             $reply_url = common_local_url('newnotice',
596                                           array('replyto' => $this->profile->getNickname(), 'inreplyto' => $this->notice->id));
597             $this->out->elementStart('a', array('href' => $reply_url,
598                                                 'class' => 'notice_reply',
599                                                 // TRANS: Link title in notice list item to reply to a notice.
600                                                 'title' => _('Reply to this notice.')));
601             // TRANS: Link text in notice list item to reply to a notice.
602             $this->out->text(_('Reply'));
603             $this->out->text(' ');
604             $this->out->element('span', 'notice_id', $this->notice->id);
605             $this->out->elementEnd('a');
606         }
607     }
608
609     /**
610      * if the user is the author, let them delete the notice
611      *
612      * @return void
613      */
614     function showDeleteLink()
615     {
616         $user = common_current_user();
617
618         $todel = (empty($this->repeat)) ? $this->notice : $this->repeat;
619
620         if (!empty($user) &&
621             !$this->notice->isVerb([ActivityVerb::DELETE]) &&
622             ($todel->profile_id == $user->id || $user->hasRight(Right::DELETEOTHERSNOTICE))) {
623             $this->out->text(' ');
624             $deleteurl = common_local_url('deletenotice',
625                                           array('notice' => $todel->id));
626             $this->out->element('a', array('href' => $deleteurl,
627                                            'class' => 'notice_delete popup',
628                                            // TRANS: Link title in notice list item to delete a notice.
629                                            'title' => _('Delete this notice from the timeline.')),
630                                            // TRANS: Link text in notice list item to delete a notice.
631                                            _('Delete'));
632         }
633     }
634
635     /**
636      * finish the notice
637      *
638      * Close the last elements in the notice list item
639      *
640      * @return void
641      */
642     function showEnd()
643     {
644         if (Event::handle('StartCloseNoticeListItemElement', array($this))) {
645             $this->out->elementEnd('li');
646             Event::handle('EndCloseNoticeListItemElement', array($this));
647         }
648     }
649
650     /**
651      * Get the notice in question
652      *
653      * For hooks, etc., this may be useful
654      *
655      * @return Notice The notice we're showing
656      */
657
658     function getNotice()
659     {
660         return $this->notice;
661     }
662 }