]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/profileaction.php
Merge branch 'fixes/private_scope_on_tags' into social-master
[quix0rs-gnu-social.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 ManagedAction
50 {
51     var $page    = null;
52     var $tag     = null;
53
54     protected $target  = null;    // Profile that we're showing
55
56     protected function prepare(array $args=array())
57     {
58         parent::prepare($args);
59
60         try {
61             $nickname_arg = $this->arg('nickname');
62             $nickname     = common_canonical_nickname($nickname_arg);
63
64             // Permanent redirect on non-canonical nickname
65
66             if ($nickname_arg != $nickname) {
67                 $args = array('nickname' => $nickname);
68                 if ($this->arg('page') && $this->arg('page') != 1) {
69                     $args['page'] = $this->arg['page'];
70                 }
71                 common_redirect(common_local_url($this->getActionName(), $args), 301);
72             }
73             $this->user = User::getKV('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             }
79
80             $this->target = $this->user->getProfile();
81         } catch (NicknameException $e) {
82             $id = (int)$this->arg('id');
83             $this->target = Profile::getKV('id', $id);
84
85             if (!$this->target instanceof Profile) {
86                 // TRANS: Error message displayed when referring to a user without a profile.
87                 $this->serverError(_m('Profile ID does not exist.'));
88             }
89
90             if ($this->target->isLocal()) {
91                 // For local users when accessed by id number, redirect to
92                 // the same action but using the nickname as argument.
93                 common_redirect(common_local_url($this->getActionName(),
94                                                 array('nickname'=>$user->getNickname())));
95             }
96         }
97
98         if ($this->target->hasRole(Profile_role::SILENCED) &&
99             (empty($this->scoped) || !$this->scoped->hasRight(Right::SILENCEUSER))) {
100             throw new ClientException(_('This profile has been silenced by site moderators'), 403);
101         }
102
103         // backwards compatibility until all actions are fixed to use $this->target
104         $this->profile = $this->target;
105
106         $this->tag = $this->trimmed('tag');
107         $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
108         common_set_returnto($this->selfUrl());
109         return true;
110     }
111
112     function isReadOnly(array $args=array())
113     {
114         return true;
115     }
116
117     function showSections()
118     {
119         $this->showSubscriptions();
120         $this->showSubscribers();
121         $this->showGroups();
122         $this->showLists();
123         $this->showStatistics();
124     }
125
126     /**
127      * Convenience function for common pattern of links to subscription/groups sections.
128      *
129      * @param string $actionClass
130      * @param string $title
131      * @param string $cssClass
132      */
133     private function statsSectionLink($actionClass, $title, $cssClass='')
134     {
135         $this->element('a', array('href' => common_local_url($actionClass,
136                                                              array('nickname' => $this->target->getNickname())),
137                                   'class' => $cssClass),
138                        $title);
139     }
140
141     function showSubscriptions()
142     {
143         $this->elementStart('div', array('id' => 'entity_subscriptions',
144                                          'class' => 'section'));
145         if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
146             $this->elementStart('h2');
147             // TRANS: H2 text for user subscription statistics.
148             $this->statsSectionLink('subscriptions', _('Following'));
149             $this->text(' ');
150             $this->text($this->target->subscriptionCount());
151             $this->elementEnd('h2');
152         
153             try {
154                 $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
155                 $pml = new ProfileMiniList($profile, $this);
156                 $pml->show();
157             } catch (NoResultException $e) {
158                 // TRANS: Text for user subscription statistics if the user has no subscription
159                 $this->element('p', null, _('(None)'));
160             }
161
162             Event::handle('EndShowSubscriptionsMiniList', array($this));
163         }
164         $this->elementEnd('div');
165     }
166
167     function showSubscribers()
168     {
169         $this->elementStart('div', array('id' => 'entity_subscribers',
170                                          'class' => 'section'));
171
172         if (Event::handle('StartShowSubscribersMiniList', array($this))) {
173
174             $this->elementStart('h2');
175             // TRANS: H2 text for user subscriber statistics.
176             $this->statsSectionLink('subscribers', _('Followers'));
177             $this->text(' ');
178             $this->text($this->target->subscriberCount());
179             $this->elementEnd('h2');
180
181             try {
182                 $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
183                 $sml = new SubscribersMiniList($profile, $this);
184                 $sml->show();
185             } catch (NoResultException $e) {
186                 // TRANS: Text for user subscriber statistics if user has no subscribers.
187                 $this->element('p', null, _('(None)'));
188             }
189
190             Event::handle('EndShowSubscribersMiniList', array($this));
191         }
192
193         $this->elementEnd('div');
194     }
195
196     function showStatistics()
197     {
198         $notice_count = $this->target->noticeCount();
199         $age_days     = (time() - strtotime($this->target->created)) / 86400;
200         if ($age_days < 1) {
201             // Rather than extrapolating out to a bajillion...
202             $age_days = 1;
203         }
204         $daily_count = round($notice_count / $age_days);
205
206         $this->elementStart('div', array('id' => 'entity_statistics',
207                                          'class' => 'section'));
208
209         // TRANS: H2 text for user statistics.
210         $this->element('h2', null, _('Statistics'));
211
212         $actionParams = array('nickname' => $this->target->nickname);
213         $stats = array(
214             array(
215                 'id' => 'user-id',
216                 // TRANS: Label for user statistics.
217                 'label' => _('User ID'),
218                 'value' => $this->target->id,
219             ),
220             array(
221                 'id' => 'member-since',
222                 // TRANS: Label for user statistics.
223                 'label' => _('Member since'),
224                 'value' => date('j M Y', strtotime($this->target->created))
225             ),
226             array(
227                 'id' => 'notices',
228                 // TRANS: Label for user statistics.
229                 'label' => _('Notices'),
230                 'value' => $notice_count,
231             ),
232             array(
233                 'id' => 'daily_notices',
234                 // TRANS: Label for user statistics.
235                 // TRANS: Average count of posts made per day since account registration.
236                 'label' => _('Daily average'),
237                 'value' => $daily_count
238             )
239         );
240
241         // Give plugins a chance to add stats entries
242         Event::handle('ProfileStats', array($this->target, &$stats));
243
244         foreach ($stats as $row) {
245             $this->showStatsRow($row);
246         }
247         $this->elementEnd('div');
248     }
249
250     private function showStatsRow($row)
251     {
252         $this->elementStart('dl', 'entity_' . $row['id']);
253         $this->elementStart('dt');
254         if (!empty($row['link'])) {
255             $this->element('a', array('href' => $row['link']), $row['label']);
256         } else {
257             $this->text($row['label']);
258         }
259         $this->elementEnd('dt');
260         $this->element('dd', null, $row['value']);
261         $this->elementEnd('dl');
262     }
263
264     function showGroups()
265     {
266         $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
267
268         $this->elementStart('div', array('id' => 'entity_groups',
269                                          'class' => 'section'));
270         if (Event::handle('StartShowGroupsMiniList', array($this))) {
271             $this->elementStart('h2');
272             // TRANS: H2 text for user group membership statistics.
273             $this->statsSectionLink('usergroups', _('Groups'));
274             $this->text(' ');
275             $this->text($this->target->getGroupCount());
276             $this->elementEnd('h2');
277
278             if ($groups instanceof User_group) {
279                 $gml = new GroupMiniList($groups, $this->target, $this);
280                 $cnt = $gml->show();
281             } else {
282                 // TRANS: Text for user user group membership statistics if user is not a member of any group.
283                 $this->element('p', null, _('(None)'));
284             }
285
286             Event::handle('EndShowGroupsMiniList', array($this));
287         }
288             $this->elementEnd('div');
289     }
290
291     function showLists()
292     {
293         $lists = $this->target->getLists($this->scoped);
294
295         if ($lists->N > 0) {
296             $this->elementStart('div', array('id' => 'entity_lists',
297                                              'class' => 'section'));
298
299             if (Event::handle('StartShowListsMiniList', array($this))) {
300
301                 $url = common_local_url('peopletagsbyuser',
302                                         array('nickname' => $this->target->getNickname()));
303
304                 $this->elementStart('h2');
305                 $this->element('a',
306                                array('href' => $url),
307                                // TRANS: H2 text for user list membership statistics.
308                                _('Lists'));
309                 $this->text(' ');
310                 $this->text($lists->N);
311                 $this->elementEnd('h2');
312
313                 $this->elementStart('ul');
314
315
316                 $first = true;
317
318                 while ($lists->fetch()) {
319                     if (!empty($lists->mainpage)) {
320                         $url = $lists->mainpage;
321                     } else {
322                         $url = common_local_url('showprofiletag',
323                                                 array('tagger' => $this->target->getNickname(),
324                                                       'tag'    => $lists->tag));
325                     }
326                     if (!$first) {
327                         $this->text(', ');
328                     } else {
329                         $first = false;
330                     }
331
332                     $this->element('a', array('href' => $url),
333                                    $lists->tag);
334                 }
335
336                 $this->elementEnd('ul');
337
338                 Event::handle('EndShowListsMiniList', array($this));
339             }
340             $this->elementEnd('div');
341         }
342     }
343 }
344
345 class SubscribersMiniList extends ProfileMiniList
346 {
347     function newListItem(Profile $profile)
348     {
349         return new SubscribersMiniListItem($profile, $this->action);
350     }
351 }
352
353 class SubscribersMiniListItem extends ProfileMiniListItem
354 {
355     function linkAttributes()
356     {
357         $aAttrs = parent::linkAttributes();
358         if (common_config('nofollow', 'subscribers')) {
359             $aAttrs['rel'] .= ' nofollow';
360         }
361         return $aAttrs;
362     }
363 }