The official, canonical postActiv repository. http://www.postactiv.com

apiblockcreate.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /* ============================================================================
  3. * Title: APIBlockCreate
  4. * Block a user via the API
  5. *
  6. * postActiv:
  7. * the micro-blogging software
  8. *
  9. * Copyright:
  10. * Copyright (C) 2016-2018, Maiyannah Bishop
  11. *
  12. * Derived from code copyright various sources:
  13. * o GNU Social (C) 2013-2016, Free Software Foundation, Inc
  14. * o StatusNet (C) 2008-2012, StatusNet, Inc
  15. * ----------------------------------------------------------------------------
  16. * License:
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * <https://www.gnu.org/licenses/agpl.html>
  31. * ----------------------------------------------------------------------------
  32. * About:
  33. * Block a user via the API
  34. *
  35. * PHP version:
  36. * Tested with PHP 7.0
  37. * ----------------------------------------------------------------------------
  38. * File Authors:
  39. * o Zach Copley
  40. * o Siebrand Mazeland <s.mazeland@xs4all.nl>
  41. * o Evan Prodromou
  42. * o Mikael Nordfeldth <mmn@hethane.se>
  43. * o Maiyannah Bishop <maiyannah.bishop@postactiv.com>
  44. *
  45. * Web:
  46. * o postActiv <http://www.postactiv.com>
  47. * o GNU social <https://www.gnu.org/s/social/>
  48. * ============================================================================
  49. */
  50. // This file is formatted so that it provides useful documentation output in
  51. // NaturalDocs. Please be considerate of this before changing formatting.
  52. if (!defined('POSTACTIV')) { exit(1); }
  53. /**
  54. * Blocks the user specified in the ID parameter as the authenticating user.
  55. * Destroys a friendship to the blocked user if it exists. Returns the
  56. * blocked user in the requested format when successful.
  57. */
  58. class ApiBlockCreateAction extends ApiAuthAction
  59. {
  60. protected $needPost = true;
  61. var $other = null;
  62. /**
  63. * Take arguments for running
  64. *
  65. * @param array $args $_REQUEST args
  66. *
  67. * @return boolean success flag
  68. *
  69. */
  70. protected function prepare(array $args=array())
  71. {
  72. parent::prepare($args);
  73. $this->other = $this->getTargetProfile($this->arg('id'));
  74. return true;
  75. }
  76. /**
  77. * Handle the request
  78. *
  79. * Save the new message
  80. *
  81. * @param array $args $_REQUEST data (unused)
  82. *
  83. * @return void
  84. */
  85. protected function handle()
  86. {
  87. parent::handle();
  88. if (empty($this->user) || empty($this->other)) {
  89. // TRANS: Client error displayed when trying to block a non-existing user or a user from another site.
  90. $this->clientError(_('No such user.'), 404);
  91. }
  92. // Don't allow blocking yourself!
  93. if ($this->user->id == $this->other->id) {
  94. // TRANS: Client error displayed when users try to block themselves.
  95. $this->clientError(_("You cannot block yourself!"), 403);
  96. }
  97. if (!$this->user->hasBlocked($this->other)) {
  98. if (Event::handle('StartBlockProfile', array($this->user, $this->other))) {
  99. $result = $this->user->block($this->other);
  100. if ($result) {
  101. Event::handle('EndBlockProfile', array($this->user, $this->other));
  102. }
  103. }
  104. }
  105. if ($this->user->hasBlocked($this->other)) {
  106. $this->initDocument($this->format);
  107. $this->showProfile($this->other, $this->format);
  108. $this->endDocument($this->format);
  109. } else {
  110. // TRANS: Server error displayed when blocking a user has failed.
  111. $this->serverError(_('Block user failed.'), 500);
  112. }
  113. }
  114. }
  115. // END OF FILE
  116. // ============================================================================
  117. ?>