]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
define LACONICA and accept LACONICA for backwards compatibility
[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             $this->clientError(_('Only logged-in users can send direct messages.'), 403);
103             return false;
104         }
105
106         $this->content = $this->trimmed('content');
107         $this->to = $this->trimmed('to');
108
109         if ($this->to) {
110
111             $this->other = User::staticGet('id', $this->to);
112
113             if (!$this->other) {
114                 $this->clientError(_('No such user'), 404);
115                 return false;
116             }
117
118             if (!$user->mutuallySubscribed($this->other)) {
119                 $this->clientError(_('You can\'t send a message to this user.'), 404);
120                 return false;
121             }
122         }
123
124         return true;
125     }
126
127     function saveNewMessage()
128     {
129         // CSRF protection
130
131         $token = $this->trimmed('token');
132         if (!$token || $token != common_session_token()) {
133             $this->showForm(_('There was a problem with your session token. ' .
134                 'Try again, please.'));
135             return;
136         }
137
138         $user = common_current_user();
139         assert($user); // XXX: maybe an error instead...
140
141         if (!$this->content) {
142             $this->showForm(_('No content!'));
143             return;
144         } else {
145             $content_shortened = common_shorten_links($this->content);
146
147             if (mb_strlen($content_shortened) > 140) {
148                 $this->showForm(_('That\'s too long. ' .
149                     'Max message size is 140 chars.'));
150                 return;
151             }
152         }
153
154         if (!$this->other) {
155             $this->showForm(_('No recipient specified.'));
156             return;
157         } else if (!$user->mutuallySubscribed($this->other)) {
158             $this->clientError(_('You can\'t send a message to this user.'), 404);
159             return;
160         } else if ($user->id == $this->other->id) {
161             $this->clientError(_('Don\'t send a message to yourself; ' .
162                 'just say it to yourself quietly instead.'), 403);
163             return;
164         }
165
166         $message = Message::saveNew($user->id, $this->other->id, $this->content, 'web');
167
168         if (is_string($message)) {
169             $this->showForm($message);
170             return;
171         }
172
173         $this->notify($user, $this->other, $message);
174
175         if ($this->boolean('ajax')) {
176             $this->startHTML('text/xml;charset=utf-8');
177             $this->elementStart('head');
178             $this->element('title', null, _('Message sent'));
179             $this->elementEnd('head');
180             $this->elementStart('body');
181             $this->element('p', array('id' => 'command_result'),
182                 sprintf(_('Direct message to %s sent'),
183                     $this->other->nickname));
184             $this->elementEnd('body');
185             $this->elementEnd('html');
186         } else {
187             $url = common_local_url('outbox',
188                 array('nickname' => $user->nickname));
189             common_redirect($url, 303);
190         }
191     }
192
193     /**
194      * Show an Ajax-y error message
195      *
196      * Goes back to the browser, where it's shown in a popup.
197      *
198      * @param string $msg Message to show
199      *
200      * @return void
201      */
202
203     function ajaxErrorMsg($msg)
204     {
205         $this->startHTML('text/xml;charset=utf-8', true);
206         $this->elementStart('head');
207         $this->element('title', null, _('Ajax Error'));
208         $this->elementEnd('head');
209         $this->elementStart('body');
210         $this->element('p', array('id' => 'error'), $msg);
211         $this->elementEnd('body');
212         $this->elementEnd('html');
213     }
214
215     function showForm($msg = null)
216     {
217         if ($msg && $this->boolean('ajax')) {
218             $this->ajaxErrorMsg($msg);
219             return;
220         }
221
222         $this->msg = $msg;
223         $this->showPage();
224     }
225
226     function showPageNotice()
227     {
228         if ($this->msg) {
229             $this->element('p', 'error', $this->msg);
230         }
231     }
232
233     function notify($from, $to, $message)
234     {
235         mail_notify_message($message, $from, $to);
236         // XXX: Jabber, SMS notifications... probably queued
237     }
238
239     // Do nothing (override)
240
241     function showNoticeForm()
242     {
243         $message_form = new MessageForm($this, $this->other, $this->content);
244         $message_form->show();
245     }
246 }