searchsub.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008-2011, StatusNet, Inc.
  5. *
  6. * Search 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. * Search 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 SearchsubAction extends Action
  53. {
  54. var $user;
  55. var $search;
  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. */
  63. function prepare(array $args = array())
  64. {
  65. parent::prepare($args);
  66. if ($this->boolean('ajax')) {
  67. GNUsocial::setApi(true);
  68. }
  69. // Only allow POST requests
  70. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  71. // TRANS: Client error displayed trying to perform any request method other than POST.
  72. // TRANS: Do not translate POST.
  73. $this->clientError(_m('This action only accepts POST requests.'));
  74. }
  75. // CSRF protection
  76. $token = $this->trimmed('token');
  77. if (!$token || $token != common_session_token()) {
  78. // TRANS: Client error displayed when the session token is not okay.
  79. $this->clientError(_m('There was a problem with your session token.'.
  80. ' Try again, please.'));
  81. }
  82. // Only for logged-in users
  83. $this->user = common_current_user();
  84. if (empty($this->user)) {
  85. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  86. $this->clientError(_m('Not logged in.'));
  87. }
  88. // Profile to subscribe to
  89. $this->search = $this->arg('search');
  90. if (empty($this->search)) {
  91. // TRANS: Client error displayed trying to subscribe to a non-existing profile.
  92. $this->clientError(_m('No such profile.'));
  93. }
  94. return true;
  95. }
  96. /**
  97. * Handle request
  98. *
  99. * Does the subscription and returns results.
  100. *
  101. * @param Array $args unused.
  102. *
  103. * @return void
  104. */
  105. function handle()
  106. {
  107. // Throws exception on error
  108. SearchSub::start($this->user->getProfile(),
  109. $this->search);
  110. if ($this->boolean('ajax')) {
  111. $this->startHTML('text/xml;charset=utf-8');
  112. $this->elementStart('head');
  113. // TRANS: Page title when search subscription succeeded.
  114. $this->element('title', null, _m('Subscribed'));
  115. $this->elementEnd('head');
  116. $this->elementStart('body');
  117. $unsubscribe = new SearchUnsubForm($this, $this->search);
  118. $unsubscribe->show();
  119. $this->elementEnd('body');
  120. $this->endHTML();
  121. } else {
  122. $url = common_local_url('search',
  123. array('search' => $this->search));
  124. common_redirect($url, 303);
  125. }
  126. }
  127. }