]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
The overloaded DB_DataObject function staticGet is now called getKV
[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         // TRANS: Page title for new direct message page.
72         return _('New message');
73     }
74
75     /**
76      * Handle input, produce output
77      *
78      * @param array $args $_REQUEST contents
79      *
80      * @return void
81      */
82
83     function handle($args)
84     {
85         parent::handle($args);
86
87         if (!common_logged_in()) {
88             // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
89             $this->clientError(_('Not logged in.'), 403);
90         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
91             $this->saveNewMessage();
92         } else {
93             $this->showForm();
94         }
95     }
96
97     function prepare($args)
98     {
99         parent::prepare($args);
100
101         $user = common_current_user();
102
103         if (!$user) {
104             /* Go log in, and then come back. */
105             common_set_returnto($_SERVER['REQUEST_URI']);
106             common_redirect(common_local_url('login'));
107             return false;
108         }
109
110         $this->content = $this->trimmed('content');
111         $this->to = $this->trimmed('to');
112
113         if ($this->to) {
114
115             $this->other = User::getKV('id', $this->to);
116
117             if (!$this->other) {
118                 // TRANS: Client error displayed trying to send a direct message to a non-existing user.
119                 $this->clientError(_('No such user.'), 404);
120                 return false;
121             }
122
123             if (!$user->mutuallySubscribed($this->other)) {
124                 // TRANS: Client error displayed trying to send a direct message to a user while sender and
125                 // TRANS: receiver are not subscribed to each other.
126                 $this->clientError(_('You cannot send a message to this user.'), 404);
127                 return false;
128             }
129         }
130
131         return true;
132     }
133
134     function saveNewMessage()
135     {
136         // CSRF protection
137
138         $token = $this->trimmed('token');
139         if (!$token || $token != common_session_token()) {
140             // TRANS: Client error displayed when the session token does not match or is not given.
141             $this->showForm(_('There was a problem with your session token. ' .
142                 'Try again, please.'));
143             return;
144         }
145
146         $user = common_current_user();
147         assert($user); // XXX: maybe an error instead...
148
149         if (!$this->content) {
150             // TRANS: Form validator error displayed trying to send a direct message without content.
151             $this->showForm(_('No content!'));
152             return;
153         } else {
154             $content_shortened = $user->shortenLinks($this->content);
155
156             if (Message::contentTooLong($content_shortened)) {
157                 // TRANS: Form validation error displayed when message content is too long.
158                 // TRANS: %d is the maximum number of characters for a message.
159                 $this->showForm(sprintf(_m('That\'s too long. Maximum message size is %d character.',
160                                            'That\'s too long. Maximum message size is %d characters.',
161                                            Message::maxContent()),
162                                         Message::maxContent()));
163                 return;
164             }
165         }
166
167         if (!$this->other) {
168             // TRANS: Form validation error displayed trying to send a direct message without specifying a recipient.
169             $this->showForm(_('No recipient specified.'));
170             return;
171         } else if (!$user->mutuallySubscribed($this->other)) {
172             // TRANS: Client error displayed trying to send a direct message to a user while sender and
173             // TRANS: receiver are not subscribed to each other.
174             $this->clientError(_('You cannot send a message to this user.'), 404);
175             return;
176         } else if ($user->id == $this->other->id) {
177             // TRANS: Client error displayed trying to send a direct message to self.
178             $this->clientError(_('Do not send a message to yourself; ' .
179                 'just say it to yourself quietly instead.'), 403);
180             return;
181         }
182
183         $message = Message::saveNew($user->id, $this->other->id, $this->content, 'web');
184
185         if (is_string($message)) {
186             $this->showForm($message);
187             return;
188         }
189
190         $message->notify();
191
192         if ($this->boolean('ajax')) {
193             $this->startHTML('text/xml;charset=utf-8');
194             $this->elementStart('head');
195             // TRANS: Page title after sending a direct message.
196             $this->element('title', null, _('Message sent'));
197             $this->elementEnd('head');
198             $this->elementStart('body');
199             $this->element('p', array('id' => 'command_result'),
200                 // TRANS: Confirmation text after sending a direct message.
201                 // TRANS: %s is the direct message recipient.
202                 sprintf(_('Direct message to %s sent.'),
203                     $this->other->nickname));
204             $this->elementEnd('body');
205             $this->elementEnd('html');
206         } else {
207             $url = common_local_url('outbox',
208                 array('nickname' => $user->nickname));
209             common_redirect($url, 303);
210         }
211     }
212
213     /**
214      * Show an Ajax-y error message
215      *
216      * Goes back to the browser, where it's shown in a popup.
217      *
218      * @param string $msg Message to show
219      *
220      * @return void
221      */
222
223     function ajaxErrorMsg($msg)
224     {
225         $this->startHTML('text/xml;charset=utf-8', true);
226         $this->elementStart('head');
227         // TRANS: Page title after an AJAX error occurred on the "send direct message" page.
228         $this->element('title', null, _('Ajax Error'));
229         $this->elementEnd('head');
230         $this->elementStart('body');
231         $this->element('p', array('id' => 'error'), $msg);
232         $this->elementEnd('body');
233         $this->elementEnd('html');
234     }
235
236     function showForm($msg = null)
237     {
238         if ($msg && $this->boolean('ajax')) {
239             $this->ajaxErrorMsg($msg);
240             return;
241         }
242
243         $this->msg = $msg;
244         if ($this->trimmed('ajax')) {
245             header('Content-Type: text/xml;charset=utf-8');
246             $this->xw->startDocument('1.0', 'UTF-8');
247             $this->elementStart('html');
248             $this->elementStart('head');
249             // TRANS: Page title on page for sending a direct message.
250             $this->element('title', null, _('New message'));
251             $this->elementEnd('head');
252             $this->elementStart('body');
253             $this->showNoticeForm();
254             $this->elementEnd('body');
255             $this->endHTML();
256         }
257         else {
258             $this->showPage();
259         }
260     }
261
262     function showPageNotice()
263     {
264         if ($this->msg) {
265             $this->element('p', 'error', $this->msg);
266         }
267     }
268
269     // Do nothing (override)
270
271     function showNoticeForm()
272     {
273         $message_form = new MessageForm($this, $this->other, $this->content);
274         $message_form->show();
275     }
276 }