]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/showstream.php
e12a227e3bdef339ff7dbcf6da7d10bdaeb47a6d
[quix0rs-gnu-social.git] / actions / showstream.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/stream.php');
23
24 define('SUBSCRIPTIONS_PER_ROW', 4);
25 define('SUBSCRIPTIONS', 80);
26
27 class ShowstreamAction extends StreamAction {
28
29         function handle($args) {
30
31                 parent::handle($args);
32
33         $nickname_arg = $this->arg('nickname');
34                 $nickname = common_canonical_nickname($nickname_arg);
35
36         # Permanent redirect on non-canonical nickname
37
38         if ($nickname_arg != $nickname) {
39             $args = array('nickname' => $nickname);
40             if ($this->arg('page') && $this->arg('page') != 1) {
41                 $args['page'] = $this->arg['page'];
42             }
43             common_redirect(common_local_url('showstream', $args), 301);
44             return;
45         }
46
47                 $user = User::staticGet('nickname', $nickname);
48
49                 if (!$user) {
50                         $this->no_such_user();
51                         return;
52                 }
53
54                 $profile = $user->getProfile();
55
56                 if (!$profile) {
57                         common_server_error(_('User has no profile.'));
58                         return;
59                 }
60
61                 # Looks like we're good; start output
62
63                 # For YADIS discovery, we also have a <meta> tag
64
65                 header('X-XRDS-Location: '. common_local_url('xrds', array('nickname' =>
66                                                                                                                                    $user->nickname)));
67
68                 common_show_header($profile->nickname,
69                                                    array($this, 'show_header'), $user,
70                                                    array($this, 'show_top'));
71
72                 $this->show_profile($profile);
73
74                 $this->show_notices($user);
75
76                 common_show_footer();
77         }
78
79         function show_top($user) {
80                 $cur = common_current_user();
81
82                 if ($cur && $cur->id == $user->id) {
83                         common_notice_form('showstream');
84                 }
85
86                 $this->views_menu();
87
88                 $this->show_feeds_list(array(0=>array('href'=>common_local_url('userrss', array('nickname' => $user->nickname)),
89                                                                                           'type' => 'rss',
90                                                                                           'version' => 'RSS 1.0',
91                                                                                           'item' => 'notices'),
92                                                                          1=>array('href'=>common_local_url('usertimeline', array('nickname' => $user->nickname)),
93                                                                                           'type' => 'atom',
94                                                                                           'version' => 'Atom 1.0',
95                                                                                           'item' => 'usertimeline'),
96
97                                                                          2=>array('href'=>common_local_url('foaf',array('nickname' => $user->nickname)),
98                                                                                           'type' => 'rdf',
99                                                                                           'version' => 'FOAF',
100                                                                                           'item' => 'foaf')));
101         }
102
103         function show_header($user) {
104                 # Feeds
105                 common_element('link', array('rel' => 'alternate',
106                                                                          'href' => common_local_url('api',
107                                                                                                                                 array('apiaction' => 'statuses',
108                                                                                                                                           'method' => 'user_timeline.rss',
109                                                                                                                                           'argument' => $user->nickname)),
110                                                                          'type' => 'application/rss+xml',
111                                                                          'title' => sprintf(_('Notice feed for %s'), $user->nickname)));
112                 common_element('link', array('rel' => 'alternate feed',
113                                                                          'href' => common_local_url('api',
114                                                                                                                                 array('apiaction' => 'statuses',
115                                                                                                                                           'method' => 'user_timeline.atom',
116                                                                                                                                           'argument' => $user->nickname)),
117                                                                          'type' => 'application/atom+xml',
118                                                                          'title' => sprintf(_('Notice feed for %s'), $user->nickname)));
119                 common_element('link', array('rel' => 'alternate',
120                                                                          'href' => common_local_url('userrss', array('nickname' =>
121                                                                                                                                                            $user->nickname)),
122                                                                          'type' => 'application/rdf+xml',
123                                                                          'title' => sprintf(_('Notice feed for %s'), $user->nickname)));
124                 # FOAF
125                 common_element('link', array('rel' => 'meta',
126                                                                          'href' => common_local_url('foaf', array('nickname' =>
127                                                                                                                                                           $user->nickname)),
128                                                                          'type' => 'application/rdf+xml',
129                                                                          'title' => 'FOAF'));
130                 # for remote subscriptions etc.
131                 common_element('meta', array('http-equiv' => 'X-XRDS-Location',
132                                                                          'content' => common_local_url('xrds', array('nickname' =>
133                                                                                                                                                            $user->nickname))));
134                 $profile = $user->getProfile();
135                 if ($profile->bio) {
136                         common_element('meta', array('name' => 'description',
137                                                                                  'content' => $profile->bio));
138                 }
139
140                 if ($user->emailmicroid && $user->email && $profile->profileurl) {
141                         common_element('meta', array('name' => 'microid',
142                                                                                  'content' => "mailto+http:sha1:" . sha1(sha1('mailto:' . $user->email) . sha1($profile->profileurl))));
143                 }
144                 if ($user->jabbermicroid && $user->jabber && $profile->profileurl) {
145                         common_element('meta', array('name' => 'microid',
146                                                                                  'content' => "xmpp+http:sha1:" . sha1(sha1('xmpp:' . $user->jabber) . sha1($profile->profileurl))));
147                 }
148
149                 # See https://wiki.mozilla.org/Microsummaries
150
151                 common_element('link', array('rel' => 'microsummary',
152                                                                          'href' => common_local_url('microsummary',
153                                                                                                                                 array('nickname' => $profile->nickname))));
154         }
155
156         function no_such_user() {
157                 $this->client_error(_('No such user.'), 404);
158         }
159
160         function show_profile($profile) {
161
162                 common_element_start('div', array('id' => 'profile', 'class' => 'vcard'));
163
164                 $this->show_personal($profile);
165
166                 $this->show_last_notice($profile);
167
168                 $cur = common_current_user();
169
170                 $this->show_subscriptions($profile);
171
172                 common_element_end('div');
173         }
174
175         function show_personal($profile) {
176
177                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
178                 common_element_start('div', array('id' => 'profile_avatar'));
179                 common_element('img', array('src' => ($avatar) ? common_avatar_display_url($avatar) : common_default_avatar(AVATAR_PROFILE_SIZE),
180                                                                         'class' => 'avatar profile photo',
181                                                                         'width' => AVATAR_PROFILE_SIZE,
182                                                                         'height' => AVATAR_PROFILE_SIZE,
183                                                                         'alt' => $profile->nickname));
184
185         common_element_start('ul', array('id' => 'profile_actions'));
186
187         common_element_start('li', array('id' => 'profile_subscribe'));
188                 $cur = common_current_user();
189                 if ($cur) {
190                         if ($cur->id != $profile->id) {
191                                 if ($cur->isSubscribed($profile)) {
192                                         common_unsubscribe_form($profile);
193                                 } else {
194                                         common_subscribe_form($profile);
195                                 }
196                         }
197                 } else {
198                         $this->show_remote_subscribe_link($profile);
199                 }
200         common_element_end('li');
201
202                 $user = User::staticGet('id', $profile->id);
203                 common_profile_new_message_nudge($cur, $user, $profile);
204
205         if ($cur) {
206             common_element_start('li', array('id' => 'profile_block'));
207             common_block_form($profile);
208             common_element_end('li');
209         }
210
211                 common_element_end('ul');
212
213                 common_element_end('div');
214
215                 common_element_start('div', array('id' => 'profile_information'));
216
217                 if ($profile->fullname) {
218                         common_element('h1', array('class' => 'fn'), $profile->fullname . ' (' . $profile->nickname . ')');
219                 } else {
220                         common_element('h1', array('class' => 'fn nickname'), $profile->nickname);
221                 }
222
223                 if ($profile->location) {
224                         common_element('p', 'location', $profile->location);
225                 }
226                 if ($profile->bio) {
227                         common_element('p', 'description note', $profile->bio);
228                 }
229                 if ($profile->homepage) {
230                         common_element_start('p', 'website');
231                         common_element('a', array('href' => $profile->homepage,
232                                                                           'rel' => 'me', 'class' => 'url'),
233                                                    $profile->homepage);
234                         common_element_end('p');
235                 }
236
237                 $this->show_statistics($profile);
238
239                 common_element_end('div');
240         }
241
242         function show_remote_subscribe_link($profile) {
243                 $url = common_local_url('remotesubscribe',
244                                         array('nickname' => $profile->nickname));
245                 common_element('a', array('href' => $url,
246                                                                   'id' => 'remotesubscribe'),
247                                            _('Subscribe'));
248         }
249
250         function show_unsubscribe_form($profile) {
251                 common_element_start('form', array('id' => 'unsubscribe', 'method' => 'post',
252                                                                                    'action' => common_local_url('unsubscribe')));
253                 common_hidden('token', common_session_token());
254                 common_element('input', array('id' => 'unsubscribeto',
255                                                                           'name' => 'unsubscribeto',
256                                                                           'type' => 'hidden',
257                                                                           'value' => $profile->nickname));
258                 common_element('input', array('type' => 'submit',
259                                                                           'class' => 'submit',
260                                                                           'value' => _('Unsubscribe')));
261                 common_element_end('form');
262         }
263
264         function show_subscriptions($profile) {
265                 global $config;
266
267                 $subs = DB_DataObject::factory('subscription');
268                 $subs->subscriber = $profile->id;
269                 $subs->whereAdd('subscribed != ' . $profile->id);
270
271                 $subs->orderBy('created DESC');
272
273                 # We ask for an extra one to know if we need to do another page
274
275                 $subs->limit(0, SUBSCRIPTIONS + 1);
276
277                 $subs_count = $subs->find();
278
279                 common_element_start('div', array('id' => 'subscriptions'));
280
281                 common_element('h2', NULL, _('Subscriptions'));
282
283                 if ($subs_count > 0) {
284
285                         common_element_start('ul', array('id' => 'subscriptions_avatars'));
286
287                         for ($i = 0; $i < min($subs_count, SUBSCRIPTIONS); $i++) {
288
289                                 if (!$subs->fetch()) {
290                                         common_debug('Weirdly, broke out of subscriptions loop early', __FILE__);
291                                         break;
292                                 }
293
294                                 $other = Profile::staticGet($subs->subscribed);
295
296                                 if (!$other) {
297                                         common_log_db_error($subs, 'SELECT', __FILE__);
298                                         continue;
299                                 }
300
301                                 common_element_start('li', 'vcard');
302                                 common_element_start('a', array('title' => ($other->fullname) ?
303                                                                                                 $other->fullname :
304                                                                                                 $other->nickname,
305                                                                                                 'href' => $other->profileurl,
306                                                                                                 'rel' => 'contact',
307                                                                                                 'class' => 'subscription fn url'));
308                                 $avatar = $other->getAvatar(AVATAR_MINI_SIZE);
309                                 common_element('img', array('src' => (($avatar) ? common_avatar_display_url($avatar) :  common_default_avatar(AVATAR_MINI_SIZE)),
310                                                                                         'width' => AVATAR_MINI_SIZE,
311                                                                                         'height' => AVATAR_MINI_SIZE,
312                                                                                         'class' => 'avatar mini photo',
313                                                                                         'alt' =>  ($other->fullname) ?
314                                                                                         $other->fullname :
315                                                                                         $other->nickname));
316                                 common_element_end('a');
317                                 common_element_end('li');
318                         }
319
320                         common_element_end('ul');
321                 }
322
323                 if ($subs_count > SUBSCRIPTIONS) {
324                         common_element_start('p', array('id' => 'subscriptions_viewall'));
325
326                         common_element('a', array('href' => common_local_url('subscriptions',
327                                                                                                                                  array('nickname' => $profile->nickname)),
328                                                                           'class' => 'moresubscriptions'),
329                                                    _('All subscriptions'));
330                         common_element_end('p');
331                 }
332
333                 common_element_end('div');
334         }
335
336         function show_statistics($profile) {
337
338                 // XXX: WORM cache this
339                 $subs = DB_DataObject::factory('subscription');
340                 $subs->subscriber = $profile->id;
341                 $subs_count = (int) $subs->count() - 1;
342
343                 $subbed = DB_DataObject::factory('subscription');
344                 $subbed->subscribed = $profile->id;
345                 $subbed_count = (int) $subbed->count() - 1;
346
347                 $notices = DB_DataObject::factory('notice');
348                 $notices->profile_id = $profile->id;
349                 $notice_count = (int) $notices->count();
350
351                 common_element_start('div', 'statistics');
352                 common_element('h2', 'statistics', _('Statistics'));
353
354                 # Other stats...?
355                 common_element_start('dl', 'statistics');
356                 common_element('dt', 'membersince', _('Member since'));
357                 common_element('dd', 'membersince', date('j M Y',
358                                                                                                  strtotime($profile->created)));
359
360                 common_element_start('dt', 'subscriptions');
361                 common_element('a', array('href' => common_local_url('subscriptions',
362                                                                                                                          array('nickname' => $profile->nickname))),
363                                            _('Subscriptions'));
364                 common_element_end('dt');
365                 common_element('dd', 'subscriptions', (is_int($subs_count)) ? $subs_count : '0');
366                 common_element_start('dt', 'subscribers');
367                 common_element('a', array('href' => common_local_url('subscribers',
368                                                                                                                          array('nickname' => $profile->nickname))),
369                                            _('Subscribers'));
370                 common_element_end('dt');
371                 common_element('dd', 'subscribers', (is_int($subbed_count)) ? $subbed_count : '0');
372                 common_element('dt', 'notices', _('Notices'));
373                 common_element('dd', 'notices', (is_int($notice_count)) ? $notice_count : '0');
374                 # XXX: link these to something
375                 common_element('dt', 'tags', _('Tags'));
376                 common_element_start('dd', 'tags');
377                 $tags = Profile_tag::getTags($profile->id, $profile->id);
378
379                 common_element_start('ul', 'tags xoxo');
380                 foreach ($tags as $tag) {
381                         common_element_start('li');
382                         common_element('a', array('rel' => 'bookmark tag',
383                                                                           'href' => common_local_url('peopletag',
384                                                                                                                                  array('tag' => $tag))),
385                                                    $tag);
386                         common_element_end('li');
387                 }
388                 common_element_end('ul');
389             common_element_end('dd');
390
391                 common_element_end('dl');
392
393                 common_element_end('div');
394         }
395
396         function show_notices($user) {
397
398                 $page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
399
400                 $notice = $user->getNotices(($page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1);
401
402                 $cnt = 0;
403
404                 if ($notice) {
405
406                         common_element_start('ul', array('id' => 'notices'));
407
408                         while ($notice->fetch()) {
409                                 $cnt++;
410                                 if ($cnt > NOTICES_PER_PAGE) {
411                                         break;
412                                 }
413                                 $this->show_notice($notice);
414                         }
415
416                         common_element_end('ul');
417                 }
418
419                 common_pagination($page>1, $cnt>NOTICES_PER_PAGE, $page,
420                                                   'showstream', array('nickname' => $user->nickname));
421         }
422
423         function show_last_notice($profile) {
424
425                 common_element('h2', NULL, _('Currently'));
426
427                 $notice = $profile->getCurrentNotice();
428
429                 if ($notice) {
430                         # FIXME: URL, image, video, audio
431                         common_element_start('p', array('class' => 'notice_current'));
432                         if ($notice->rendered) {
433                                 common_raw($notice->rendered);
434                         } else {
435                                 # XXX: may be some uncooked notices in the DB,
436                                 # we cook them right now. This can probably disappear in future
437                                 # versions (>> 0.4.x)
438                                 common_raw(common_render_content($notice->content, $notice));
439                         }
440                         common_element_end('p');
441                 }
442         }
443
444         function show_notice($notice) {
445                 $profile = $notice->getProfile();
446                 $user = common_current_user();
447
448                 # XXX: RDFa
449                 common_element_start('li', array('class' => 'notice_single hentry',
450                                                                                  'id' => 'notice-' . $notice->id));
451                 if ($user) {
452                         if ($user->hasFave($notice)) {
453                                 common_disfavor_form($notice);
454                         } else {
455                                 common_favor_form($notice);
456                         }
457                 }
458                 $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
459                 # FIXME: URL, image, video, audio
460                 common_element_start('p', 'entry-title');
461                 if ($notice->rendered) {
462                         common_raw($notice->rendered);
463                 } else {
464                         # XXX: may be some uncooked notices in the DB,
465                         # we cook them right now. This can probably disappear in future
466                         # versions (>> 0.4.x)
467                         common_raw(common_render_content($notice->content, $notice));
468                 }
469                 common_element_end('p');
470                 common_element_start('p', array('class' => 'time'));
471                 common_element_start('a', array('class' => 'permalink',
472                                                                   'rel' => 'bookmark',
473                                                                   'href' => $noticeurl));
474                 common_element('abbr', array('class' => 'published',
475                                                                          'title' => common_date_iso8601($notice->created)),
476                                                 common_date_string($notice->created));
477                 common_element_end('a');
478
479                 if ($notice->source) {
480                         common_text(_(' from '));
481                         $this->source_link($notice->source);
482                 }
483                 if ($notice->reply_to) {
484                         $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
485                         common_text(' (');
486                         common_element('a', array('class' => 'inreplyto',
487                                                                           'href' => $replyurl),
488                                                    _('in reply to...'));
489                         common_text(')');
490                 }
491                 common_element_start('a',
492                                                          array('href' => common_local_url('newnotice',
493                                                                                                                           array('replyto' => $profile->nickname)),
494                                                                    'onclick' => 'doreply("'.$profile->nickname.'"); return false',
495                                                                    'title' => _('reply'),
496                                                                    'class' => 'replybutton'));
497                 common_raw('&rarr;');
498                 common_element_end('a');
499                 if ($user && $notice->profile_id == $user->id) {
500                         $deleteurl = common_local_url('deletenotice', array('notice' => $notice->id));
501                         common_element_start('a', array('class' => 'deletenotice',
502                                                                                         'href' => $deleteurl,
503                                                                                         'title' => _('delete')));
504                         common_raw('&times;');
505                         common_element_end('a');
506                 }
507
508                 common_element_end('p');
509                 common_element_end('li');
510         }
511 }