]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
Merge branch 'master' into 0.9.x
[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  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
33     exit(1);
34 }
35
36 /**
37  * Action for posting new direct messages
38  *
39  * @category Personal
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @author   Zach Copley <zach@status.net>
43  * @author   Sarven Capadisli <csarven@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class NewmessageAction extends Action
49 {
50
51     /**
52      * Error message, if any
53      */
54
55     var $msg = null;
56
57     var $content = null;
58     var $to = null;
59     var $other = null;
60
61     /**
62      * Title of the page
63      *
64      * Note that this usually doesn't get called unless something went wrong
65      *
66      * @return string page title
67      */
68
69     function title()
70     {
71         return _('New message');
72     }
73
74     /**
75      * Handle input, produce output
76      *
77      * @param array $args $_REQUEST contents
78      *
79      * @return void
80      */
81
82     function handle($args)
83     {
84         parent::handle($args);
85
86         if (!common_logged_in()) {
87             $this->clientError(_('Not logged in.'), 403);
88         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
89             $this->saveNewMessage();
90         } else {
91             $this->showForm();
92         }
93     }
94
95     function prepare($args)
96     {
97         parent::prepare($args);
98
99         $user = common_current_user();
100
101         if (!$user) {
102             /* Go log in, and then come back. */
103             common_set_returnto($_SERVER['REQUEST_URI']);
104             common_redirect(common_local_url('login'));
105             return false;
106         }
107
108         $this->content = $this->trimmed('content');
109         $this->to = $this->trimmed('to');
110
111         if ($this->to) {
112
113             $this->other = User::staticGet('id', $this->to);
114
115             if (!$this->other) {
116                 $this->clientError(_('No such user.'), 404);
117                 return false;
118             }
119
120             if (!$user->mutuallySubscribed($this->other)) {
121                 $this->clientError(_('You can\'t send a message to this user.'), 404);
122                 return false;
123             }
124         }
125
126         return true;
127     }
128
129     function saveNewMessage()
130     {
131         // CSRF protection
132
133         $token = $this->trimmed('token');
134         if (!$token || $token != common_session_token()) {
135             $this->showForm(_('There was a problem with your session token. ' .
136                 'Try again, please.'));
137             return;
138         }
139
140         $user = common_current_user();
141         assert($user); // XXX: maybe an error instead...
142
143         if (!$this->content) {
144             $this->showForm(_('No content!'));
145             return;
146         } else {
147             $content_shortened = $user->shortenLinks($this->content);
148
149             if (Message::contentTooLong($content_shortened)) {
150                 // TRANS: Form validation error displayed when message content is too long.
151                 // TRANS: %d is the maximum number of characters for a message.
152                 $this->showForm(sprintf(_m('That\'s too long. Maximum message size is %d character.',
153                                            'That\'s too long. Maximum message size is %d characters.',
154                                            Message::maxContent()),
155                                         Message::maxContent()));
156                 return;
157             }
158         }
159
160         if (!$this->other) {
161             $this->showForm(_('No recipient specified.'));
162             return;
163         } else if (!$user->mutuallySubscribed($this->other)) {
164             $this->clientError(_('You can\'t send a message to this user.'), 404);
165             return;
166         } else if ($user->id == $this->other->id) {
167             $this->clientError(_('Don\'t send a message to yourself; ' .
168                 'just say it to yourself quietly instead.'), 403);
169             return;
170         }
171
172         $message = Message::saveNew($user->id, $this->other->id, $this->content, 'web');
173
174         if (is_string($message)) {
175             $this->showForm($message);
176             return;
177         }
178
179         $message->notify();
180
181         if ($this->boolean('ajax')) {
182             $this->startHTML('text/xml;charset=utf-8');
183             $this->elementStart('head');
184             $this->element('title', null, _('Message sent'));
185             $this->elementEnd('head');
186             $this->elementStart('body');
187             $this->element('p', array('id' => 'command_result'),
188                 sprintf(_('Direct message to %s sent.'),
189                     $this->other->nickname));
190             $this->elementEnd('body');
191             $this->elementEnd('html');
192         } else {
193             $url = common_local_url('outbox',
194                 array('nickname' => $user->nickname));
195             common_redirect($url, 303);
196         }
197     }
198
199     /**
200      * Show an Ajax-y error message
201      *
202      * Goes back to the browser, where it's shown in a popup.
203      *
204      * @param string $msg Message to show
205      *
206      * @return void
207      */
208
209     function ajaxErrorMsg($msg)
210     {
211         $this->startHTML('text/xml;charset=utf-8', true);
212         $this->elementStart('head');
213         $this->element('title', null, _('Ajax Error'));
214         $this->elementEnd('head');
215         $this->elementStart('body');
216         $this->element('p', array('id' => 'error'), $msg);
217         $this->elementEnd('body');
218         $this->elementEnd('html');
219     }
220
221     function showForm($msg = null)
222     {
223         if ($msg && $this->boolean('ajax')) {
224             $this->ajaxErrorMsg($msg);
225             return;
226         }
227
228         $this->msg = $msg;
229         if ($this->trimmed('ajax')) {
230             header('Content-Type: text/xml;charset=utf-8');
231             $this->xw->startDocument('1.0', 'UTF-8');
232             $this->elementStart('html');
233             $this->elementStart('head');
234             $this->element('title', null, _('New message'));
235             $this->elementEnd('head');
236             $this->elementStart('body');
237             $this->showNoticeForm();
238             $this->elementEnd('body');
239             $this->endHTML();
240         }
241         else {
242             $this->showPage();
243         }
244     }
245
246     function showPageNotice()
247     {
248         if ($this->msg) {
249             $this->element('p', 'error', $this->msg);
250         }
251     }
252
253     // Do nothing (override)
254
255     function showNoticeForm()
256     {
257         $message_form = new MessageForm($this, $this->other, $this->content);
258         $message_form->show();
259     }
260 }