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