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