addpeopletag.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2010, StatusNet, Inc.
  5. *
  6. * Action to add a people tag to a user.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * PHP version 5
  22. *
  23. * @category Action
  24. * @package StatusNet
  25. * @author Shashi Gowda <connect2shashi@gmail.com>
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/lib/togglepeopletag.php';
  33. /**
  34. *
  35. * Action to tag a profile with a single tag.
  36. *
  37. * Takes parameters:
  38. *
  39. * - tagged: the ID of the profile being tagged
  40. * - token: session token to prevent CSRF attacks
  41. * - ajax: boolean; whether to return Ajax or full-browser results
  42. * - peopletag_id: the ID of the tag being used
  43. *
  44. * Only works if the current user is logged in.
  45. *
  46. * @category Action
  47. * @package StatusNet
  48. * @author Shashi Gowda <connect2shashi@gmail.com>
  49. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  50. * @link http://status.net/
  51. */
  52. class AddpeopletagAction extends Action
  53. {
  54. var $user;
  55. var $tagged;
  56. var $peopletag;
  57. /**
  58. * Check pre-requisites and instantiate attributes
  59. *
  60. * @param Array $args array of arguments (URL, GET, POST)
  61. *
  62. * @return boolean success flag
  63. */
  64. function prepare($args)
  65. {
  66. parent::prepare($args);
  67. // CSRF protection
  68. $token = $this->trimmed('token');
  69. if (!$token || $token != common_session_token()) {
  70. // TRANS: Client error displayed when the session token does not match or is not given.
  71. $this->clientError(_('There was a problem with your session token.'.
  72. ' Try again, please.'));
  73. }
  74. // Only for logged-in users
  75. $this->user = common_current_user();
  76. if (empty($this->user)) {
  77. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  78. $this->clientError(_('Not logged in.'));
  79. }
  80. // Profile to subscribe to
  81. $tagged_id = $this->arg('tagged');
  82. $this->tagged = Profile::getKV('id', $tagged_id);
  83. if (empty($this->tagged)) {
  84. // TRANS: Client error displayed trying to perform an action related to a non-existing profile.
  85. $this->clientError(_('No such profile.'));
  86. }
  87. $id = $this->arg('peopletag_id');
  88. $this->peopletag = Profile_list::getKV('id', $id);
  89. if (empty($this->peopletag)) {
  90. // TRANS: Client error displayed trying to reference a non-existing list.
  91. $this->clientError(_('No such list.'));
  92. }
  93. return true;
  94. }
  95. /**
  96. * Handle request
  97. *
  98. * Does the tagging and returns results.
  99. *
  100. * @param Array $args unused.
  101. *
  102. * @return void
  103. */
  104. function handle($args)
  105. {
  106. // Throws exception on error
  107. $ptag = Profile_tag::setTag($this->user->id, $this->tagged->id,
  108. $this->peopletag->tag);
  109. if (!$ptag) {
  110. $user = User::getKV('id', $id);
  111. if ($user) {
  112. $this->clientError(
  113. // TRANS: Client error displayed when an unknown error occurs when adding a user to a list.
  114. // TRANS: %s is a username.
  115. sprintf(_('There was an unexpected error while listing %s.'),
  116. $user->nickname));
  117. } else {
  118. // TRANS: Client error displayed when an unknown error occurs when adding a user to a list.
  119. // TRANS: %s is a profile URL.
  120. $this->clientError(sprintf(_('There was a problem listing %s. ' .
  121. 'The remote server is probably not responding correctly. ' .
  122. 'Please try retrying later.'), $this->profile->profileurl));
  123. }
  124. }
  125. if ($this->boolean('ajax')) {
  126. $this->startHTML('text/xml;charset=utf-8');
  127. $this->elementStart('head');
  128. // TRANS: Title after adding a user to a list.
  129. $this->element('title', null, _m('TITLE','Listed'));
  130. $this->elementEnd('head');
  131. $this->elementStart('body');
  132. $unsubscribe = new UntagButton($this, $this->tagged, $this->peopletag);
  133. $unsubscribe->show();
  134. $this->elementEnd('body');
  135. $this->endHTML();
  136. } else {
  137. $url = common_local_url('subscriptions',
  138. array('nickname' => $this->user->nickname));
  139. common_redirect($url, 303);
  140. }
  141. }
  142. }