profileaction.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. if (!defined('GNUSOCIAL')) { exit(1); }
  31. /**
  32. * Profile action common superclass
  33. *
  34. * Abstracts out common code from profile and personal tabs
  35. *
  36. * @category Personal
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. abstract class ProfileAction extends ManagedAction
  43. {
  44. var $page = null;
  45. var $tag = null;
  46. protected $target = null; // Profile that we're showing
  47. protected function doPreparation()
  48. {
  49. try {
  50. $nickname_arg = $this->arg('nickname');
  51. $nickname = common_canonical_nickname($nickname_arg);
  52. // Permanent redirect on non-canonical nickname
  53. if ($nickname_arg != $nickname) {
  54. $args = array('nickname' => $nickname);
  55. if ($this->arg('page') && $this->arg('page') != 1) {
  56. $args['page'] = $this->arg['page'];
  57. }
  58. common_redirect(common_local_url($this->getActionName(), $args), 301);
  59. }
  60. $this->user = User::getKV('nickname', $nickname);
  61. if (!$this->user) {
  62. $group = Local_group::getKV('nickname', $nickname);
  63. if ($group instanceof Local_group) {
  64. common_redirect($group->getProfile()->getUrl());
  65. }
  66. // TRANS: Client error displayed when calling a profile action without specifying a user.
  67. $this->clientError(_('No such user.'), 404);
  68. }
  69. $this->target = $this->user->getProfile();
  70. } catch (NicknameException $e) {
  71. $id = (int)$this->arg('id');
  72. $this->target = Profile::getKV('id', $id);
  73. if (!$this->target instanceof Profile) {
  74. // TRANS: Error message displayed when referring to a user without a profile.
  75. $this->serverError(_m('Profile ID does not exist.'));
  76. }
  77. if ($this->target->isLocal()) {
  78. // For local users when accessed by id number, redirect to
  79. // the same action but using the nickname as argument.
  80. common_redirect(common_local_url($this->getActionName(),
  81. array('nickname'=>$user->getNickname())));
  82. }
  83. }
  84. if ($this->target->hasRole(Profile_role::SILENCED) &&
  85. (empty($this->scoped) || !$this->scoped->hasRight(Right::SILENCEUSER))) {
  86. throw new ClientException(_('This profile has been silenced by site moderators'), 403);
  87. }
  88. // backwards compatibility until all actions are fixed to use $this->target
  89. $this->profile = $this->target;
  90. $this->tag = $this->trimmed('tag');
  91. $this->page = ($this->arg('page')) ? ($this->arg('page')+0) : 1;
  92. common_set_returnto($this->selfUrl());
  93. return $this->profileActionPreparation();
  94. }
  95. protected function profileActionPreparation()
  96. {
  97. // No-op by default.
  98. }
  99. function isReadOnly($args)
  100. {
  101. return true;
  102. }
  103. function showSections()
  104. {
  105. $this->showSubscriptions();
  106. $this->showSubscribers();
  107. $this->showGroups();
  108. $this->showLists();
  109. $this->showStatistics();
  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->target->getNickname())),
  122. 'class' => $cssClass),
  123. $title);
  124. }
  125. function showSubscriptions()
  126. {
  127. $this->elementStart('div', array('id' => 'entity_subscriptions',
  128. 'class' => 'section'));
  129. if (Event::handle('StartShowSubscriptionsMiniList', array($this))) {
  130. $this->elementStart('h2');
  131. // TRANS: H2 text for user subscription statistics.
  132. $this->statsSectionLink('subscriptions', _('Following'));
  133. $this->text(' ');
  134. $this->text($this->target->subscriptionCount());
  135. $this->elementEnd('h2');
  136. try {
  137. $profile = $this->target->getSubscribed(0, PROFILES_PER_MINILIST + 1);
  138. $pml = new ProfileMiniList($profile, $this);
  139. $pml->show();
  140. } catch (NoResultException $e) {
  141. // TRANS: Text for user subscription statistics if the user has no subscription
  142. $this->element('p', null, _('(None)'));
  143. }
  144. Event::handle('EndShowSubscriptionsMiniList', array($this));
  145. }
  146. $this->elementEnd('div');
  147. }
  148. function showSubscribers()
  149. {
  150. $this->elementStart('div', array('id' => 'entity_subscribers',
  151. 'class' => 'section'));
  152. if (Event::handle('StartShowSubscribersMiniList', array($this))) {
  153. $this->elementStart('h2');
  154. // TRANS: H2 text for user subscriber statistics.
  155. $this->statsSectionLink('subscribers', _('Followers'));
  156. $this->text(' ');
  157. $this->text($this->target->subscriberCount());
  158. $this->elementEnd('h2');
  159. try {
  160. $profile = $this->target->getSubscribers(0, PROFILES_PER_MINILIST + 1);
  161. $sml = new SubscribersMiniList($profile, $this);
  162. $sml->show();
  163. } catch (NoResultException $e) {
  164. // TRANS: Text for user subscriber statistics if user has no subscribers.
  165. $this->element('p', null, _('(None)'));
  166. }
  167. Event::handle('EndShowSubscribersMiniList', array($this));
  168. }
  169. $this->elementEnd('div');
  170. }
  171. function showStatistics()
  172. {
  173. $notice_count = $this->target->noticeCount();
  174. $age_days = (time() - strtotime($this->target->created)) / 86400;
  175. if ($age_days < 1) {
  176. // Rather than extrapolating out to a bajillion...
  177. $age_days = 1;
  178. }
  179. $daily_count = round($notice_count / $age_days);
  180. $this->elementStart('div', array('id' => 'entity_statistics',
  181. 'class' => 'section'));
  182. // TRANS: H2 text for user statistics.
  183. $this->element('h2', null, _('Statistics'));
  184. $profile = $this->target;
  185. $actionParams = array('nickname' => $profile->nickname);
  186. $stats = array(
  187. array(
  188. 'id' => 'user-id',
  189. // TRANS: Label for user statistics.
  190. 'label' => _('User ID'),
  191. 'value' => $profile->id,
  192. ),
  193. array(
  194. 'id' => 'member-since',
  195. // TRANS: Label for user statistics.
  196. 'label' => _('Member since'),
  197. 'value' => date('j M Y', strtotime($profile->created))
  198. ),
  199. array(
  200. 'id' => 'notices',
  201. // TRANS: Label for user statistics.
  202. 'label' => _('Notices'),
  203. 'value' => $notice_count,
  204. ),
  205. array(
  206. 'id' => 'daily_notices',
  207. // TRANS: Label for user statistics.
  208. // TRANS: Average count of posts made per day since account registration.
  209. 'label' => _('Daily average'),
  210. 'value' => $daily_count
  211. )
  212. );
  213. // Give plugins a chance to add stats entries
  214. Event::handle('ProfileStats', array($profile, &$stats));
  215. foreach ($stats as $row) {
  216. $this->showStatsRow($row);
  217. }
  218. $this->elementEnd('div');
  219. }
  220. private function showStatsRow($row)
  221. {
  222. $this->elementStart('dl', 'entity_' . $row['id']);
  223. $this->elementStart('dt');
  224. if (!empty($row['link'])) {
  225. $this->element('a', array('href' => $row['link']), $row['label']);
  226. } else {
  227. $this->text($row['label']);
  228. }
  229. $this->elementEnd('dt');
  230. $this->element('dd', null, $row['value']);
  231. $this->elementEnd('dl');
  232. }
  233. function showGroups()
  234. {
  235. $groups = $this->target->getGroups(0, GROUPS_PER_MINILIST + 1);
  236. $this->elementStart('div', array('id' => 'entity_groups',
  237. 'class' => 'section'));
  238. if (Event::handle('StartShowGroupsMiniList', array($this))) {
  239. $this->elementStart('h2');
  240. // TRANS: H2 text for user group membership statistics.
  241. $this->statsSectionLink('usergroups', _('Groups'));
  242. $this->text(' ');
  243. $this->text($this->target->getGroupCount());
  244. $this->elementEnd('h2');
  245. if ($groups instanceof User_group) {
  246. $gml = new GroupMiniList($groups, $this->target, $this);
  247. $cnt = $gml->show();
  248. } else {
  249. // TRANS: Text for user user group membership statistics if user is not a member of any group.
  250. $this->element('p', null, _('(None)'));
  251. }
  252. Event::handle('EndShowGroupsMiniList', array($this));
  253. }
  254. $this->elementEnd('div');
  255. }
  256. function showLists()
  257. {
  258. $lists = $this->target->getLists($this->scoped);
  259. if ($lists->N > 0) {
  260. $this->elementStart('div', array('id' => 'entity_lists',
  261. 'class' => 'section'));
  262. if (Event::handle('StartShowListsMiniList', array($this))) {
  263. $url = common_local_url('peopletagsbyuser',
  264. array('nickname' => $this->target->getNickname()));
  265. $this->elementStart('h2');
  266. $this->element('a',
  267. array('href' => $url),
  268. // TRANS: H2 text for user list membership statistics.
  269. _('Lists'));
  270. $this->text(' ');
  271. $this->text($lists->N);
  272. $this->elementEnd('h2');
  273. $this->elementStart('ul');
  274. $first = true;
  275. while ($lists->fetch()) {
  276. if (!empty($lists->mainpage)) {
  277. $url = $lists->mainpage;
  278. } else {
  279. $url = common_local_url('showprofiletag',
  280. array('tagger' => $this->target->getNickname(),
  281. 'tag' => $lists->tag));
  282. }
  283. if (!$first) {
  284. $this->text(', ');
  285. } else {
  286. $first = false;
  287. }
  288. $this->element('a', array('href' => $url),
  289. $lists->tag);
  290. }
  291. $this->elementEnd('ul');
  292. Event::handle('EndShowListsMiniList', array($this));
  293. }
  294. $this->elementEnd('div');
  295. }
  296. }
  297. }
  298. class SubscribersMiniList extends ProfileMiniList
  299. {
  300. function newListItem($profile)
  301. {
  302. return new SubscribersMiniListItem($profile, $this->action);
  303. }
  304. }
  305. class SubscribersMiniListItem extends ProfileMiniListItem
  306. {
  307. function linkAttributes()
  308. {
  309. $aAttrs = parent::linkAttributes();
  310. if (common_config('nofollow', 'subscribers')) {
  311. $aAttrs['rel'] .= ' nofollow';
  312. }
  313. return $aAttrs;
  314. }
  315. }