newmessage.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Handler for posting new messages
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Personal
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Zach Copley <zach@status.net>
  26. * @author Sarven Capadisli <csarven@status.net>
  27. * @copyright 2008-2009 StatusNet, Inc.
  28. * @copyright 2013 Free Software Foundation, Inc.
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  30. * @link http://status.net/
  31. */
  32. if (!defined('STATUSNET')) {
  33. exit(1);
  34. }
  35. /**
  36. * Action for posting new direct messages
  37. *
  38. * @category Personal
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Zach Copley <zach@status.net>
  42. * @author Sarven Capadisli <csarven@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. */
  46. class NewmessageAction extends FormAction
  47. {
  48. var $content = null;
  49. var $to = null;
  50. var $other = null;
  51. protected $form = 'message'; // will become MessageForm later
  52. /**
  53. * Title of the page
  54. *
  55. * Note that this usually doesn't get called unless something went wrong
  56. *
  57. * @return string page title
  58. */
  59. function title()
  60. {
  61. // TRANS: Page title for new direct message page.
  62. return _('New message');
  63. }
  64. /**
  65. * Handle input, produce output
  66. *
  67. * @param array $args $_REQUEST contents
  68. *
  69. * @return void
  70. */
  71. protected function prepare(array $args=array())
  72. {
  73. parent::prepare($args);
  74. $this->content = $this->trimmed('content');
  75. $this->to = $this->trimmed('to');
  76. if ($this->to) {
  77. $this->other = Profile::getKV('id', $this->to);
  78. if (!$this->other instanceof Profile) {
  79. // TRANS: Client error displayed trying to send a direct message to a non-existing user.
  80. $this->clientError(_('No such user.'), 404);
  81. }
  82. if (!$this->other->isLocal()) {
  83. // TRANS: Explains that current federation does not support direct, private messages yet.
  84. $this->clientError(_('You cannot send direct messages to federated users yet.'));
  85. }
  86. if (!$this->scoped->mutuallySubscribed($this->other)) {
  87. // TRANS: Client error displayed trying to send a direct message to a user while sender and
  88. // TRANS: receiver are not subscribed to each other.
  89. $this->clientError(_('You cannot send a message to this user.'), 404);
  90. }
  91. }
  92. return true;
  93. }
  94. protected function handlePost()
  95. {
  96. parent::handlePost();
  97. assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
  98. if (!$this->content) {
  99. // TRANS: Form validator error displayed trying to send a direct message without content.
  100. $this->clientError(_('No content!'));
  101. } else {
  102. $content_shortened = $this->scoped->shortenLinks($this->content);
  103. if (Message::contentTooLong($content_shortened)) {
  104. // TRANS: Form validation error displayed when message content is too long.
  105. // TRANS: %d is the maximum number of characters for a message.
  106. $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
  107. 'That\'s too long. Maximum message size is %d characters.',
  108. Message::maxContent()),
  109. Message::maxContent()));
  110. }
  111. }
  112. if (!$this->other) {
  113. // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
  114. $this->clientError(_('No recipient specified.'));
  115. } else if (!$this->scoped->mutuallySubscribed($this->other)) {
  116. // TRANS: Client error displayed trying to send a direct message to a user while sender and
  117. // TRANS: receiver are not subscribed to each other.
  118. $this->clientError(_('You cannot send a message to this user.'), 404);
  119. } else if ($this->scoped->id == $this->other->id) {
  120. // TRANS: Client error displayed trying to send a direct message to self.
  121. $this->clientError(_('Do not send a message to yourself; ' .
  122. 'just say it to yourself quietly instead.'), 403);
  123. }
  124. $message = Message::saveNew($this->scoped->id, $this->other->id, $this->content, 'web');
  125. $message->notify();
  126. if ($this->boolean('ajax')) {
  127. $this->startHTML('text/xml;charset=utf-8');
  128. $this->elementStart('head');
  129. // TRANS: Page title after sending a direct message.
  130. $this->element('title', null, _('Message sent'));
  131. $this->elementEnd('head');
  132. $this->elementStart('body');
  133. $this->element('p', array('id' => 'command_result'),
  134. // TRANS: Confirmation text after sending a direct message.
  135. // TRANS: %s is the direct message recipient.
  136. sprintf(_('Direct message to %s sent.'),
  137. $this->other->nickname));
  138. $this->elementEnd('body');
  139. $this->endHTML();
  140. } else {
  141. $url = common_local_url('outbox',
  142. array('nickname' => $this->scoped->nickname));
  143. common_redirect($url, 303);
  144. }
  145. }
  146. /**
  147. * Show an Ajax-y error message
  148. *
  149. * Goes back to the browser, where it's shown in a popup.
  150. *
  151. * @param string $msg Message to show
  152. *
  153. * @return void
  154. */
  155. function ajaxErrorMsg($msg)
  156. {
  157. $this->startHTML('text/xml;charset=utf-8', true);
  158. $this->elementStart('head');
  159. // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
  160. $this->element('title', null, _('Ajax Error'));
  161. $this->elementEnd('head');
  162. $this->elementStart('body');
  163. $this->element('p', array('id' => 'error'), $msg);
  164. $this->elementEnd('body');
  165. $this->endHTML();
  166. }
  167. function showForm($msg = null)
  168. {
  169. if ($msg && $this->boolean('ajax')) {
  170. $this->ajaxErrorMsg($msg);
  171. return;
  172. }
  173. $this->msg = $msg;
  174. if ($this->trimmed('ajax')) {
  175. $this->startHTML('text/xml;charset=utf-8');
  176. $this->elementStart('head');
  177. // TRANS: Page title on page for sending a direct message.
  178. $this->element('title', null, _('New message'));
  179. $this->elementEnd('head');
  180. $this->elementStart('body');
  181. $this->showNoticeForm();
  182. $this->elementEnd('body');
  183. $this->endHTML();
  184. }
  185. else {
  186. $this->showPage();
  187. }
  188. }
  189. function showPageNotice()
  190. {
  191. if ($this->msg) {
  192. $this->element('p', 'error', $this->msg);
  193. }
  194. }
  195. // Do nothing (override)
  196. function showNoticeForm()
  197. {
  198. $message_form = new MessageForm($this, $this->other, $this->content);
  199. $message_form->show();
  200. }
  201. }