newgroupmessage.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Action for adding a new group message
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Cache
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Action for adding a new group message
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class NewgroupmessageAction extends Action
  46. {
  47. var $group;
  48. var $user;
  49. var $text;
  50. /**
  51. * For initializing members of the class.
  52. *
  53. * @param array $args misc. arguments
  54. *
  55. * @return boolean true
  56. * @throws ClientException
  57. * @throws NicknameException
  58. */
  59. function prepare(array $args = [])
  60. {
  61. parent::prepare($args);
  62. $this->user = common_current_user();
  63. if (empty($this->user)) {
  64. // TRANS: Client exception thrown when trying to send a private group message while not logged in.
  65. throw new ClientException(_m('Must be logged in.'), 403);
  66. }
  67. if (!$this->user->hasRight(Right::NEWMESSAGE)) {
  68. // TRANS: Exception thrown when user %s is not allowed to send a private group message.
  69. throw new Exception(sprintf(_m('User %s is not allowed to send private messages.'),
  70. $this->user->nickname));
  71. }
  72. $nicknameArg = $this->trimmed('nickname');
  73. $nickname = common_canonical_nickname($nicknameArg);
  74. if ($nickname != $nicknameArg) {
  75. $url = common_local_url('newgroupmessage', array('nickname' => $nickname));
  76. common_redirect($url, 301);
  77. }
  78. $localGroup = Local_group::getKV('nickname', $nickname);
  79. if (empty($localGroup)) {
  80. // TRANS: Client exception thrown when trying to send a private group message to a non-existing group.
  81. throw new ClientException(_m('No such group.'), 404);
  82. }
  83. $this->group = User_group::getKV('id', $localGroup->group_id);
  84. if (empty($this->group)) {
  85. // TRANS: Client exception thrown when trying to send a private group message to a non-existing group.
  86. throw new ClientException(_m('No such group.'), 404);
  87. }
  88. // This throws an exception on error
  89. Group_privacy_settings::ensurePost($this->user, $this->group);
  90. // If we're posted to, check session token and get text
  91. if ($this->isPost()) {
  92. $this->checkSessionToken();
  93. $this->text = $this->trimmed('content');
  94. }
  95. return true;
  96. }
  97. /**
  98. * Handler method
  99. *
  100. * @return void
  101. */
  102. function handle()
  103. {
  104. if ($this->isPost()) {
  105. $this->sendNewMessage();
  106. } else {
  107. $this->showPage();
  108. }
  109. }
  110. function sendNewMessage()
  111. {
  112. $gm = Group_message::send($this->user, $this->group, $this->text);
  113. if ($this->boolean('ajax')) {
  114. $this->startHTML('text/xml;charset=utf-8');
  115. $this->elementStart('head');
  116. // TRANS: Title after sending a private group message.
  117. $this->element('title', null, _m('Message sent'));
  118. $this->elementEnd('head');
  119. $this->elementStart('body');
  120. $this->element('p',
  121. array('id' => 'command_result'),
  122. // TRANS: Succes text after sending a direct message to group %s.
  123. sprintf(_m('Direct message to %s sent.'),
  124. $this->group->nickname));
  125. $this->elementEnd('body');
  126. $this->endHTML();
  127. } else {
  128. common_redirect($gm->url, 303);
  129. }
  130. }
  131. function showNoticeForm()
  132. {
  133. $form = new GroupMessageForm($this, $this->group);
  134. $form->show();
  135. }
  136. function title()
  137. {
  138. // TRANS: Title of form for new private group message.
  139. return sprintf(_m('New message to group %s'), $this->group->nickname);
  140. }
  141. }