searchsub.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. defined('GNUSOCIAL') || die();
  17. /**
  18. * Search subscription action
  19. *
  20. * Takes parameters:
  21. *
  22. * - token: session token to prevent CSRF attacks
  23. * - ajax: boolean; whether to return Ajax or full-browser results
  24. *
  25. * Only works if the current user is logged in.
  26. *
  27. * @category Plugin
  28. * @package SearchSubPlugin
  29. * @author Evan Prodromou <evan@status.net>
  30. * @author Brion Vibber <brion@status.net>
  31. * @copyright 2011-2019 Free Software Foundation, Inc http://www.fsf.org
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class SearchsubAction extends Action
  35. {
  36. public $user;
  37. public $search;
  38. /**
  39. * Check pre-requisites and instantiate attributes
  40. *
  41. * @param array $args array of arguments (URL, GET, POST)
  42. *
  43. * @return bool success flag
  44. * @throws ClientException
  45. */
  46. public function prepare(array $args = [])
  47. {
  48. parent::prepare($args);
  49. if ($this->boolean('ajax')) {
  50. GNUsocial::setApi(true);
  51. }
  52. // Only allow POST requests
  53. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  54. // TRANS: Client error displayed trying to perform any request method other than POST.
  55. // TRANS: Do not translate POST.
  56. $this->clientError(_m('This action only accepts POST requests.'));
  57. }
  58. // CSRF protection
  59. $token = $this->trimmed('token');
  60. if (!$token || $token != common_session_token()) {
  61. // TRANS: Client error displayed when the session token is not okay.
  62. $this->clientError(_m('There was a problem with your session token.' .
  63. ' Try again, please.'));
  64. }
  65. // Only for logged-in users
  66. $this->user = common_current_user();
  67. if (empty($this->user)) {
  68. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  69. $this->clientError(_m('Not logged in.'));
  70. }
  71. // Profile to subscribe to
  72. $this->search = $this->arg('search');
  73. if (empty($this->search)) {
  74. // TRANS: Client error displayed trying to subscribe to a non-existing profile.
  75. $this->clientError(_m('No such profile.'));
  76. }
  77. return true;
  78. }
  79. /**
  80. * Handle request
  81. *
  82. * Does the subscription and returns results.
  83. *
  84. * @return void
  85. * @throws ClientException
  86. */
  87. public function handle()
  88. {
  89. // Throws exception on error
  90. SearchSub::start(
  91. $this->user->getProfile(),
  92. $this->search
  93. );
  94. if ($this->boolean('ajax')) {
  95. $this->startHTML('text/xml;charset=utf-8');
  96. $this->elementStart('head');
  97. // TRANS: Page title when search subscription succeeded.
  98. $this->element('title', null, _m('Subscribed'));
  99. $this->elementEnd('head');
  100. $this->elementStart('body');
  101. $unsubscribe = new SearchUnsubForm($this, $this->search);
  102. $unsubscribe->show();
  103. $this->elementEnd('body');
  104. $this->endHTML();
  105. } else {
  106. $url = common_local_url(
  107. 'search',
  108. array('search' => $this->search)
  109. );
  110. common_redirect($url, 303);
  111. }
  112. }
  113. }