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