]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
Merge branch 'master' of gitorious.org:social/mainline
[quix0rs-gnu-social.git] / actions / newmessage.php
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
33 if (!defined('STATUSNET')) {
34     exit(1);
35 }
36
37 /**
38  * Action for posting new direct messages
39  *
40  * @category Personal
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Zach Copley <zach@status.net>
44  * @author   Sarven Capadisli <csarven@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46  * @link     http://status.net/
47  */
48
49 class NewmessageAction extends FormAction
50 {
51     var $content = null;
52     var $to = null;
53     var $other = null;
54
55     /**
56      * Title of the page
57      *
58      * Note that this usually doesn't get called unless something went wrong
59      *
60      * @return string page title
61      */
62
63     function title()
64     {
65         // TRANS: Page title for new direct message page.
66         return _('New message');
67     }
68
69     /**
70      * Handle input, produce output
71      *
72      * @param array $args $_REQUEST contents
73      *
74      * @return void
75      */
76
77     protected function prepare(array $args=array())
78     {
79         parent::prepare($args);
80
81         $this->content = $this->trimmed('content');
82         $this->to = $this->trimmed('to');
83
84         if ($this->to) {
85
86             $this->other = Profile::getKV('id', $this->to);
87
88             if (!$this->other instanceof Profile) {
89                 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
90                 $this->clientError(_('No such user.'), 404);
91             }
92
93             if (!$this->other->isLocal()) {
94                 // TRANS: Explains that current federation does not support direct, private messages yet.
95                 $this->clientError(_('You cannot send direct messages to federated users yet.'));
96             }
97
98             if (!$this->scoped->mutuallySubscribed($this->other)) {
99                 // TRANS: Client error displayed trying to send a direct message to a user while sender and
100                 // TRANS: receiver are not subscribed to each other.
101                 $this->clientError(_('You cannot send a message to this user.'), 404);
102             }
103         }
104
105         return true;
106     }
107
108     protected function handlePost()
109     {
110         parent::handlePost();
111
112         assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
113
114         if (!$this->content) {
115             // TRANS: Form validator error displayed trying to send a direct message without content.
116             $this->clientError(_('No content!'));
117         } else {
118             $content_shortened = $this->scoped->shortenLinks($this->content);
119
120             if (Message::contentTooLong($content_shortened)) {
121                 // TRANS: Form validation error displayed when message content is too long.
122                 // TRANS: %d is the maximum number of characters for a message.
123                 $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
124                                            'That\'s too long. Maximum message size is %d characters.',
125                                            Message::maxContent()),
126                                         Message::maxContent()));
127             }
128         }
129
130         if (!$this->other) {
131             // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
132             $this->clientError(_('No recipient specified.'));
133         } else if (!$this->scoped->mutuallySubscribed($this->other)) {
134             // TRANS: Client error displayed trying to send a direct message to a user while sender and
135             // TRANS: receiver are not subscribed to each other.
136             $this->clientError(_('You cannot send a message to this user.'), 404);
137         } else if ($this->scoped->id == $this->other->id) {
138             // TRANS: Client error displayed trying to send a direct message to self.
139             $this->clientError(_('Do not send a message to yourself; ' .
140                 'just say it to yourself quietly instead.'), 403);
141         }
142
143         $message = Message::saveNew($this->scoped->id, $this->other->id, $this->content, 'web');
144         $message->notify();
145
146         if ($this->boolean('ajax')) {
147             $this->startHTML('text/xml;charset=utf-8');
148             $this->elementStart('head');
149             // TRANS: Page title after sending a direct message.
150             $this->element('title', null, _('Message sent'));
151             $this->elementEnd('head');
152             $this->elementStart('body');
153             $this->element('p', array('id' => 'command_result'),
154                 // TRANS: Confirmation text after sending a direct message.
155                 // TRANS: %s is the direct message recipient.
156                 sprintf(_('Direct message to %s sent.'),
157                     $this->other->nickname));
158             $this->elementEnd('body');
159             $this->endHTML();
160         } else {
161             $url = common_local_url('outbox',
162                 array('nickname' => $this->scoped->nickname));
163             common_redirect($url, 303);
164         }
165     }
166
167     /**
168      * Show an Ajax-y error message
169      *
170      * Goes back to the browser, where it's shown in a popup.
171      *
172      * @param string $msg Message to show
173      *
174      * @return void
175      */
176
177     function ajaxErrorMsg($msg)
178     {
179         $this->startHTML('text/xml;charset=utf-8', true);
180         $this->elementStart('head');
181         // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
182         $this->element('title', null, _('Ajax Error'));
183         $this->elementEnd('head');
184         $this->elementStart('body');
185         $this->element('p', array('id' => 'error'), $msg);
186         $this->elementEnd('body');
187         $this->endHTML();
188     }
189
190     function showForm($msg = null)
191     {
192         if ($msg && $this->boolean('ajax')) {
193             $this->ajaxErrorMsg($msg);
194             return;
195         }
196
197         $this->msg = $msg;
198         if ($this->trimmed('ajax')) {
199             $this->startHTML('text/xml;charset=utf-8');
200             $this->elementStart('head');
201             // TRANS: Page title on page for sending a direct message.
202             $this->element('title', null, _('New message'));
203             $this->elementEnd('head');
204             $this->elementStart('body');
205             $this->showNoticeForm();
206             $this->elementEnd('body');
207             $this->endHTML();
208         }
209         else {
210             $this->showPage();
211         }
212     }
213
214     function showPageNotice()
215     {
216         if ($this->msg) {
217             $this->element('p', 'error', $this->msg);
218         }
219     }
220
221     // Do nothing (override)
222
223     function showNoticeForm()
224     {
225         $message_form = new MessageForm($this, $this->other, $this->content);
226         $message_form->show();
227     }
228 }