hide silenced users' profile pages
[statusnet:mainline.git] / lib / profileaction.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Common parent of Personal and Profile actions
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Personal
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Sarven Capadisli <csarven@status.net>
26  * @copyright 2008-2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/profileminilist.php';
36 require_once INSTALLDIR.'/lib/groupminilist.php';
37
38 /**
39  * Profile action common superclass
40  *
41  * Abstracts out common code from profile and personal tabs
42  *
43  * @category Personal
44  * @package  StatusNet
45  * @author   Evan Prodromou <evan@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49 class ProfileAction extends Action
50 {
51     var $page    = null;
52     var $profile = null;
53     var $tag     = null;
54
55     function prepare($args)
56     {
57         parent::prepare($args);
58
59         $nickname_arg = $this->arg('nickname');
60         $nickname     = common_canonical_nickname($nickname_arg);
61
62         // Permanent redirect on non-canonical nickname
63
64         if ($nickname_arg != $nickname) {
65             $args = array('nickname' => $nickname);
66             if ($this->arg('page') && $this->arg('page') != 1) {
67                 $args['page'] = $this->arg['page'];
68             }
69             common_redirect(common_local_url($this->trimmed('action'), $args), 301);
70             return false;
71         }
72
73         $this->user = User::staticGet('nickname', $nickname);
74
75         if (!$this->user) {
76             // TRANS: Client error displayed when calling a profile action without specifying a user.
77             $this->clientError(_('No such user.'), 404);
78             return false;
79         }
80
81         $this->profile = $this->user->getProfile();
82
83         if (!$this->profile) {
84             // TRANS: Error message displayed when referring to a user without a profile.
85             $this->serverError(_('User has no profile.'));
86             return false;
87         }
88
89         $user = common_current_user();
90
91         if ($this->profile->hasRole(Profile_role::SILENCED) &&
92             (empty($user) || !$user->hasRight(Right::SILENCEUSER))) {
93             throw new ClientException(_('This profile has been silenced by site moderators'), 403);
94         }
95
96         $this->tag = $this->trimmed('tag');
97         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
98         common_set_returnto($this->selfUrl());
99         return true;
100     }
101
102     function showSections()
103     {
104         $this->showSubscriptions();
105         $this->showSubscribers();
106         $this->showGroups();
107         $this->showLists();
108         $this->showStatistics();
109     }
110
111     /**
112      * Convenience function for common pattern of links to subscription/groups sections.
113      *
114      * @param string $actionClass
115      * @param string $title
116      * @param string $cssClass
117      */
118     private function statsSectionLink($actionClass, $title, $cssClass='')
119     {
120         $this->element('a', array('href' => common_local_url($actionClass,
121                                                              array('nickname' => $this->profile->nickname)),
122                                   'class' => $cssClass),
123                        $title);
124     }
125
126     function showSubscriptions()
127     {
128         $profile = $this->profile->getSubscriptions(0, PROFILES_PER_MINILIST + 1);
129
130         $this->elementStart('div', array('id' => 'entity_subscriptions',
131                                          'class' => 'section'));
132         if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
133             $this->elementStart('h2');
134             // TRANS: H2 text for user subscription statistics.
135             $this->statsSectionLink('subscriptions', _('Following'));
136             $this->text(' ');
137             $this->text($this->profile->subscriptionCount());
138             $this->elementEnd('h2');
139
140             $cnt = 0;
141
142             if (!empty($profile)) {
143                 $pml = new ProfileMiniList($profile, $this);
144                 $cnt = $pml->show();
145                 if ($cnt == 0) {
146                     // TRANS: Text for user subscription statistics if the user has no subscriptions.
147                     $this->element('p', null, _('(None)'));
148                 }
149             }
150
151             Event::handle('EndShowSubscriptionsMiniList', array($this));
152         }
153         $this->elementEnd('div');
154     }
155
156     function showSubscribers()
157     {
158         $profile = $this->profile->getSubscribers(0, PROFILES_PER_MINILIST + 1);
159
160         $this->elementStart('div', array('id' => 'entity_subscribers',
161                                          'class' => 'section'));
162
163         if (Event::handle('StartShowSubscribersMiniList', array($this))) {
164
165             $this->elementStart('h2');
166             // TRANS: H2 text for user subscriber statistics.
167             $this->statsSectionLink('subscribers', _('Followers'));
168             $this->text(' ');
169             $this->text($this->profile->subscriberCount());
170             $this->elementEnd('h2');
171
172             $cnt = 0;
173
174             if (!empty($profile)) {
175                 $sml = new SubscribersMiniList($profile, $this);
176                 $cnt = $sml->show();
177                 if ($cnt == 0) {
178                     // TRANS: Text for user subscriber statistics if user has no subscribers.
179                     $this->element('p', null, _('(None)'));
180                 }
181             }
182
183             Event::handle('EndShowSubscribersMiniList', array($this));
184         }
185
186         $this->elementEnd('div');
187     }
188
189     function showStatistics()
190     {
191         $notice_count = $this->profile->noticeCount();
192         $age_days     = (time() - strtotime($this->profile->created)) / 86400;
193         if ($age_days < 1) {
194             // Rather than extrapolating out to a bajillion...
195             $age_days = 1;
196         }
197         $daily_count = round($notice_count / $age_days);
198
199         $this->elementStart('div', array('id' => 'entity_statistics',
200                                          'class' => 'section'));
201
202         // TRANS: H2 text for user statistics.
203         $this->element('h2', null, _('Statistics'));
204
205         $profile = $this->profile;
206         $actionParams = array('nickname' => $profile->nickname);
207         $stats = array(
208             array(
209                 'id' => 'user-id',
210                 // TRANS: Label for user statistics.
211                 'label' => _('User ID'),
212                 'value' => $profile->id,
213             ),
214             array(
215                 'id' => 'member-since',
216                 // TRANS: Label for user statistics.
217                 'label' => _('Member since'),
218                 'value' => date('j M Y', strtotime($profile->created))
219             ),
220             array(
221                 'id' => 'notices',
222                 // TRANS: Label for user statistics.
223                 'label' => _('Notices'),
224                 'value' => $notice_count,
225             ),
226             array(
227                 'id' => 'daily_notices',
228                 // TRANS: Label for user statistics.
229                 // TRANS: Average count of posts made per day since account registration.
230                 'label' => _('Daily average'),
231                 'value' => $daily_count
232             )
233         );
234
235         // Give plugins a chance to add stats entries
236         Event::handle('ProfileStats', array($profile, &$stats));
237
238         foreach ($stats as $row) {
239             $this->showStatsRow($row);
240         }
241         $this->elementEnd('div');
242     }
243
244     private function showStatsRow($row)
245     {
246         $this->elementStart('dl', 'entity_' . $row['id']);
247         $this->elementStart('dt');
248         if (!empty($row['link'])) {
249             $this->element('a', array('href' => $row['link']), $row['label']);
250         } else {
251             $this->text($row['label']);
252         }
253         $this->elementEnd('dt');
254         $this->element('dd', null, $row['value']);
255         $this->elementEnd('dl');
256     }
257
258     function showGroups()
259     {
260         $groups = $this->profile->getGroups(0, GROUPS_PER_MINILIST + 1);
261
262         $this->elementStart('div', array('id' => 'entity_groups',
263                                          'class' => 'section'));
264         if (Event::handle('StartShowGroupsMiniList', array($this))) {
265             $this->elementStart('h2');
266             // TRANS: H2 text for user group membership statistics.
267             $this->statsSectionLink('usergroups', _('Groups'));
268             $this->text(' ');
269             $this->text($this->profile->getGroups(0, null)->N);
270             $this->elementEnd('h2');
271
272             if ($groups) {
273                 $gml = new GroupMiniList($groups, $this->profile, $this);
274                 $cnt = $gml->show();
275                 if ($cnt == 0) {
276                     // TRANS: Text for user user group membership statistics if user is not a member of any group.
277                     $this->element('p', null, _('(None)'));
278                 }
279             }
280
281             Event::handle('EndShowGroupsMiniList', array($this));
282         }
283             $this->elementEnd('div');
284     }
285
286     function showLists()
287     {
288         $cur = common_current_user();
289
290         $lists = $this->profile->getLists($cur);
291
292         if ($lists->N > 0) {
293             $this->elementStart('div', array('id' => 'entity_lists',
294                                              'class' => 'section'));
295
296             if (Event::handle('StartShowListsMiniList', array($this))) {
297
298                 $url = common_local_url('peopletagsbyuser',
299                                         array('nickname' => $this->profile->nickname));
300
301                 $this->elementStart('h2');
302                 $this->element('a',
303                                array('href' => $url),
304                                // TRANS: H2 text for user list membership statistics.
305                                _('Lists'));
306                 $this->text(' ');
307                 $this->text($lists->N);
308                 $this->elementEnd('h2');
309
310                 $this->elementStart('ul');
311
312
313                 $first = true;
314
315                 while ($lists->fetch()) {
316                     if (!empty($lists->mainpage)) {
317                         $url = $lists->mainpage;
318                     } else {
319                         $url = common_local_url('showprofiletag',
320                                                 array('tagger' => $this->profile->nickname,
321                                                       'tag'    => $lists->tag));
322                     }
323                     if (!$first) {
324                         $this->text(', ');
325                     } else {
326                         $first = false;
327                     }
328
329                     $this->element('a', array('href' => $url),
330                                    $lists->tag);
331                 }
332
333                 $this->elementEnd('ul');
334
335                 Event::handle('EndShowListsMiniList', array($this));
336             }
337             $this->elementEnd('div');
338         }
339     }
340 }
341
342 class SubscribersMiniList extends ProfileMiniList
343 {
344     function newListItem($profile)
345     {
346         return new SubscribersMiniListItem($profile, $this->action);
347     }
348 }
349
350 class SubscribersMiniListItem extends ProfileMiniListItem
351 {
352     function linkAttributes()
353     {
354         $aAttrs = parent::linkAttributes();
355         if (common_config('nofollow', 'subscribers')) {
356             $aAttrs['rel'] .= ' nofollow';
357         }
358         return $aAttrs;
359     }
360 }