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