tagsub.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2011, StatusNet, Inc.
  5. *
  6. * Tag 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 Brion Vibber <brion@status.net>
  26. * @author Evan Prodromou <evan@status.net>
  27. * @copyright 2008-2010 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. /**
  35. * Tag subscription action
  36. *
  37. * Takes parameters:
  38. *
  39. * - token: session token to prevent CSRF attacks
  40. * - ajax: boolean; whether to return Ajax or full-browser results
  41. *
  42. * Only works if the current user is logged in.
  43. *
  44. * @category Action
  45. * @package StatusNet
  46. * @author Evan Prodromou <evan@status.net>
  47. * @author Brion Vibber <brion@status.net>
  48. * @copyright 2008-2011 StatusNet, Inc.
  49. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  50. * @link http://status.net/
  51. */
  52. class TagsubAction extends Action
  53. {
  54. public $user;
  55. public $tag;
  56. /**
  57. * Check pre-requisites and instantiate attributes
  58. *
  59. * @param Array $args array of arguments (URL, GET, POST)
  60. *
  61. * @return boolean success flag
  62. * @throws ClientException
  63. */
  64. public function prepare(array $args = array())
  65. {
  66. parent::prepare($args);
  67. if ($this->boolean('ajax')) {
  68. GNUsocial::setApi(true);
  69. }
  70. // Only allow POST requests
  71. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  72. // TRANS: Client error displayed trying to perform any request method other than POST.
  73. // TRANS: Do not translate POST.
  74. $this->clientError(_m('This action only accepts POST requests.'));
  75. }
  76. // CSRF protection
  77. $token = $this->trimmed('token');
  78. if (!$token || $token != common_session_token()) {
  79. // TRANS: Client error displayed when the session token is not okay.
  80. $this->clientError(_m('There was a problem with your session token.' .
  81. ' Try again, please.'));
  82. }
  83. // Only for logged-in users
  84. $this->user = common_current_user();
  85. if (empty($this->user)) {
  86. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  87. $this->clientError(_m('Not logged in.'));
  88. }
  89. // Profile to subscribe to
  90. $this->tag = $this->arg('tag');
  91. if (empty($this->tag)) {
  92. // TRANS: Client error displayed trying to subscribe to a non-existing profile.
  93. $this->clientError(_m('No such profile.'));
  94. }
  95. return true;
  96. }
  97. /**
  98. * Handle request
  99. *
  100. * Does the subscription and returns results.
  101. *
  102. * @return void
  103. * @throws ClientException
  104. */
  105. public function handle()
  106. {
  107. // Throws exception on error
  108. TagSub::start(
  109. $this->user->getProfile(),
  110. $this->tag
  111. );
  112. if ($this->boolean('ajax')) {
  113. $this->startHTML('text/xml;charset=utf-8');
  114. $this->elementStart('head');
  115. // TRANS: Page title when tag subscription succeeded.
  116. $this->element('title', null, _m('Subscribed'));
  117. $this->elementEnd('head');
  118. $this->elementStart('body');
  119. $unsubscribe = new TagUnsubForm($this, $this->tag);
  120. $unsubscribe->show();
  121. $this->elementEnd('body');
  122. $this->endHTML();
  123. } else {
  124. $url = common_local_url(
  125. 'tag',
  126. array('tag' => $this->tag)
  127. );
  128. common_redirect($url, 303);
  129. }
  130. }
  131. }