subscribe.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2011, StatusNet, Inc.
  5. *
  6. * Subscription action.
  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 Evan Prodromou <evan@status.net>
  26. * @copyright 2008-2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Subscription action
  35. *
  36. * Subscribing to a profile. Likely to work for OStatus profiles.
  37. *
  38. * Takes parameters:
  39. *
  40. * - subscribeto: a profile ID
  41. * - token: session token to prevent CSRF attacks
  42. * - ajax: boolean; whether to return Ajax or full-browser results
  43. *
  44. * Only works if the current user is logged in.
  45. *
  46. * @category Action
  47. * @package StatusNet
  48. * @author Evan Prodromou <evan@status.net>
  49. * @copyright 2008-2010 StatusNet, Inc.
  50. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  51. * @link http://status.net/
  52. */
  53. class SubscribeAction extends Action
  54. {
  55. var $user;
  56. var $other;
  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. // Only allow POST requests
  68. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  69. // TRANS: Client error displayed trying to perform any request method other than POST.
  70. // TRANS: Do not translate POST.
  71. $this->clientError(_('This action only accepts POST requests.'));
  72. }
  73. // CSRF protection
  74. $token = $this->trimmed('token');
  75. if (!$token || $token != common_session_token()) {
  76. // TRANS: Client error displayed when the session token is not okay.
  77. $this->clientError(_('There was a problem with your session token.'.
  78. ' Try again, please.'));
  79. }
  80. // Only for logged-in users
  81. $this->user = common_current_user();
  82. if (empty($this->user)) {
  83. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  84. $this->clientError(_('Not logged in.'));
  85. }
  86. // Profile to subscribe to
  87. $other_id = $this->arg('subscribeto');
  88. $this->other = Profile::getKV('id', $other_id);
  89. if (empty($this->other)) {
  90. // TRANS: Client error displayed trying to subscribe to a non-existing profile.
  91. $this->clientError(_('No such profile.'));
  92. }
  93. return true;
  94. }
  95. /**
  96. * Handle request
  97. *
  98. * Does the subscription 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. $sub = Subscription::ensureStart($this->user->getProfile(),
  108. $this->other);
  109. if ($this->boolean('ajax')) {
  110. $this->startHTML('text/xml;charset=utf-8');
  111. $this->elementStart('head');
  112. // TRANS: Page title when subscription succeeded.
  113. $this->element('title', null, _('Subscribed'));
  114. $this->elementEnd('head');
  115. $this->elementStart('body');
  116. if ($sub instanceof Subscription) {
  117. $form = new UnsubscribeForm($this, $this->other);
  118. } else {
  119. $form = new CancelSubscriptionForm($this, $this->other);
  120. }
  121. $form->show();
  122. $this->elementEnd('body');
  123. $this->endHTML();
  124. } else {
  125. $url = common_local_url('subscriptions',
  126. array('nickname' => $this->user->nickname));
  127. common_redirect($url, 303);
  128. }
  129. }
  130. }