]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newmessage.php
trac750 Automatically update Identi.ca profile box with user's latest dent
[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     
25     function handle($args)
26     {
27         parent::handle($args);
28
29         if (!common_logged_in()) {
30             $this->client_error(_('Not logged in.'), 403);
31         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
32             $this->save_new_message();
33         } else {
34             $this->show_form();
35         }
36     }
37
38     function save_new_message()
39     {
40         $user = common_current_user();
41         assert($user); # XXX: maybe an error instead...
42
43         # CSRF protection
44         
45         $token = $this->trimmed('token');
46         if (!$token || $token != common_session_token()) {
47             $this->show_form(_('There was a problem with your session token. Try again, please.'));
48             return;
49         }
50         
51         $content = $this->trimmed('content');
52         $to = $this->trimmed('to');
53         
54         if (!$content) {
55             $this->show_form(_('No content!'));
56             return;
57         } else {
58             $content_shortened = common_shorten_links($content);
59
60             if (mb_strlen($content_shortened) > 140) {
61                 common_debug("Content = '$content_shortened'", __FILE__);
62                 common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__);
63                 $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
64                 return;
65             }
66         }
67
68         $other = User::staticGet('id', $to);
69         
70         if (!$other) {
71             $this->show_form(_('No recipient specified.'));
72             return;
73         } else if (!$user->mutuallySubscribed($other)) {
74             $this->client_error(_('You can\'t send a message to this user.'), 404);
75             return;
76         } else if ($user->id == $other->id) {
77             $this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'), 403);
78             return;
79         }
80         
81         $message = Message::saveNew($user->id, $other->id, $content, 'web');
82         
83         if (is_string($message)) {
84             $this->show_form($message);
85             return;
86         }
87
88         $this->notify($user, $other, $message);
89
90         $url = common_local_url('outbox', array('nickname' => $user->nickname));
91
92         common_redirect($url, 303);
93     }
94
95     function show_top($params)
96     {
97
98         list($content, $user, $to) = $params;
99         
100         assert(!is_null($user));
101
102         common_message_form($content, $user, $to);
103     }
104
105     function show_form($msg=null)
106     {
107         
108         $content = $this->trimmed('content');
109         $user = common_current_user();
110
111         $to = $this->trimmed('to');
112         
113         $other = User::staticGet('id', $to);
114
115         if (!$other) {
116             $this->client_error(_('No such user'), 404);
117             return;
118         }
119
120         if (!$user->mutuallySubscribed($other)) {
121             $this->client_error(_('You can\'t send a message to this user.'), 404);
122             return;
123         }
124         
125         common_show_header(_('New message'), null,
126                            array($content, $user, $other),
127                            array($this, 'show_top'));
128         
129         if ($msg) {
130             common_element('p', array('id'=>'error'), $msg);
131         }
132         
133         common_show_footer();
134     }
135     
136     function notify($from, $to, $message)
137     {
138         mail_notify_message($message, $from, $to);
139         # XXX: Jabber, SMS notifications... probably queued
140     }
141 }