]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
0a537ad2d30970cc939379d35a17f3e735b36028
[quix0rs-gnu-social.git] / actions / newmessage.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class NewmessageAction extends Action {
23     
24     function handle($args)
25     {
26         parent::handle($args);
27
28         if (!common_logged_in()) {
29             $this->client_error(_('Not logged in.'), 403);
30         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
31             $this->save_new_message();
32         } else {
33             $this->show_form();
34         }
35     }
36
37     function save_new_message()
38     {
39         $user = common_current_user();
40         assert($user); # XXX: maybe an error instead...
41
42         # CSRF protection
43         
44         $token = $this->trimmed('token');
45         if (!$token || $token != common_session_token()) {
46             $this->show_form(_('There was a problem with your session token. Try again, please.'));
47             return;
48         }
49         
50         $content = $this->trimmed('content');
51         $to = $this->trimmed('to');
52         
53         if (!$content) {
54             $this->show_form(_('No content!'));
55             return;
56         } else {
57             $content_shortened = common_shorten_links($content);
58
59             if (mb_strlen($content_shortened) > 140) {
60                 common_debug("Content = '$content_shortened'", __FILE__);
61                 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
62                 $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
63                 return;
64             }
65         }
66
67         $other = User::staticGet('id', $to);
68         
69         if (!$other) {
70             $this->show_form(_('No recipient specified.'));
71             return;
72         } else if (!$user->mutuallySubscribed($other)) {
73             $this->client_error(_('You can\'t send a message to this user.'), 404);
74             return;
75         } else if ($user->id == $other->id) {
76             $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'), 403);
77             return;
78         }
79         
80         $message = Message::saveNew($user->id, $other->id, $content, 'web');
81         
82         if (is_string($message)) {
83             $this->show_form($message);
84             return;
85         }
86
87         $this->notify($user, $other, $message);
88
89         $url = common_local_url('outbox', array('nickname' => $user->nickname));
90
91         common_redirect($url, 303);
92     }
93
94     function show_top($params)
95     {
96
97         list($content, $user, $to) = $params;
98         
99         assert(!is_null($user));
100
101         common_message_form($content, $user, $to);
102     }
103
104     function show_form($msg=null)
105     {
106         
107         $content = $this->trimmed('content');
108         $user = common_current_user();
109
110         $to = $this->trimmed('to');
111         
112         $other = User::staticGet('id', $to);
113
114         if (!$other) {
115             $this->client_error(_('No such user'), 404);
116             return;
117         }
118
119         if (!$user->mutuallySubscribed($other)) {
120             $this->client_error(_('You can\'t send a message to this user.'), 404);
121             return;
122         }
123         
124         common_show_header(_('New message'), null,
125                            array($content, $user, $other),
126                            array($this, 'show_top'));
127         
128         if ($msg) {
129             common_element('p', array('id'=>'error'), $msg);
130         }
131         
132         common_show_footer();
133     }
134     
135     function notify($from, $to, $message)
136     {
137         mail_notify_message($message, $from, $to);
138         # XXX: Jabber, SMS notifications... probably queued
139     }
140 }